2009-06-12 08:29:29 +03:00
|
|
|
/*
|
|
|
|
* DPX (.dpx) image decoder
|
|
|
|
* Copyright (c) 2009 Jimmy Christensen
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2009-06-12 08:29:29 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2009-06-12 08:29:29 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2009-06-12 08:29:29 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2009-06-12 08:29:29 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2009-06-12 08:29:29 +03:00
|
|
|
#include "bytestream.h"
|
|
|
|
#include "avcodec.h"
|
2012-11-10 15:22:56 +03:00
|
|
|
#include "internal.h"
|
2009-06-12 08:29:29 +03:00
|
|
|
|
|
|
|
typedef struct DPXContext {
|
|
|
|
AVFrame picture;
|
|
|
|
} DPXContext;
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned int read32(const uint8_t **ptr, int is_big)
|
|
|
|
{
|
|
|
|
unsigned int temp;
|
|
|
|
if (is_big) {
|
|
|
|
temp = AV_RB32(*ptr);
|
|
|
|
} else {
|
|
|
|
temp = AV_RL32(*ptr);
|
|
|
|
}
|
|
|
|
*ptr += 4;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned make_16bit(unsigned value)
|
|
|
|
{
|
|
|
|
// mask away invalid bits
|
|
|
|
value &= 0xFFC0;
|
|
|
|
// correctly expand to 16 bits
|
|
|
|
return value + (value >> 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_frame(AVCodecContext *avctx,
|
|
|
|
void *data,
|
2012-11-13 21:35:22 +03:00
|
|
|
int *got_frame,
|
2009-06-12 08:29:29 +03:00
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
const uint8_t *buf = avpkt->data;
|
2011-01-07 21:55:22 +02:00
|
|
|
const uint8_t *buf_end = avpkt->data + avpkt->size;
|
2009-06-12 08:29:29 +03:00
|
|
|
int buf_size = avpkt->size;
|
|
|
|
DPXContext *const s = avctx->priv_data;
|
|
|
|
AVFrame *picture = data;
|
|
|
|
AVFrame *const p = &s->picture;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
2012-08-05 02:41:20 +03:00
|
|
|
unsigned int offset;
|
|
|
|
int magic_num, endian;
|
2012-11-16 20:24:55 +03:00
|
|
|
int x, y, ret;
|
2009-06-12 08:29:29 +03:00
|
|
|
int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;
|
|
|
|
|
|
|
|
unsigned int rgbBuffer;
|
|
|
|
|
2011-04-01 15:23:28 +03:00
|
|
|
if (avpkt->size <= 1634) {
|
2011-03-25 19:58:07 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2009-06-12 08:29:29 +03:00
|
|
|
magic_num = AV_RB32(buf);
|
|
|
|
buf += 4;
|
|
|
|
|
|
|
|
/* Check if the files "magic number" is "SDPX" which means it uses
|
|
|
|
* big-endian or XPDS which is for little-endian files */
|
|
|
|
if (magic_num == AV_RL32("SDPX")) {
|
|
|
|
endian = 0;
|
|
|
|
} else if (magic_num == AV_RB32("SDPX")) {
|
|
|
|
endian = 1;
|
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
|
2012-11-16 20:24:55 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-06-12 08:29:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
offset = read32(&buf, endian);
|
2011-03-25 19:58:07 +02:00
|
|
|
if (avpkt->size <= offset) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2009-06-12 08:29:29 +03:00
|
|
|
// Need to end in 0x304 offset from start of file
|
|
|
|
buf = avpkt->data + 0x304;
|
|
|
|
w = read32(&buf, endian);
|
|
|
|
h = read32(&buf, endian);
|
|
|
|
|
|
|
|
// Need to end in 0x320 to read the descriptor
|
|
|
|
buf += 20;
|
|
|
|
descriptor = buf[0];
|
|
|
|
|
|
|
|
// Need to end in 0x323 to read the bits per color
|
|
|
|
buf += 3;
|
2009-12-16 22:46:48 +02:00
|
|
|
avctx->bits_per_raw_sample =
|
2009-06-12 08:29:29 +03:00
|
|
|
bits_per_color = buf[0];
|
|
|
|
|
2011-04-01 15:23:28 +03:00
|
|
|
buf += 825;
|
|
|
|
avctx->sample_aspect_ratio.num = read32(&buf, endian);
|
|
|
|
avctx->sample_aspect_ratio.den = read32(&buf, endian);
|
|
|
|
|
2009-06-12 08:29:29 +03:00
|
|
|
switch (descriptor) {
|
|
|
|
case 51: // RGBA
|
|
|
|
elements = 4;
|
|
|
|
break;
|
|
|
|
case 50: // RGB
|
|
|
|
elements = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
|
2012-11-16 20:24:55 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-06-12 08:29:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (bits_per_color) {
|
|
|
|
case 8:
|
|
|
|
if (elements == 4) {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA;
|
2009-06-12 08:29:29 +03:00
|
|
|
} else {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2009-06-12 08:29:29 +03:00
|
|
|
}
|
|
|
|
source_packet_size = elements;
|
|
|
|
target_packet_size = elements;
|
|
|
|
break;
|
|
|
|
case 10:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB48;
|
2009-06-12 08:29:29 +03:00
|
|
|
target_packet_size = 6;
|
2011-03-25 19:58:07 +02:00
|
|
|
source_packet_size = 4;
|
2009-06-12 08:29:29 +03:00
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
case 16:
|
|
|
|
if (endian) {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
|
2009-06-12 08:29:29 +03:00
|
|
|
} else {
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
|
2009-06-12 08:29:29 +03:00
|
|
|
}
|
|
|
|
target_packet_size = 6;
|
|
|
|
source_packet_size = elements * 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
|
2012-11-16 20:24:55 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-06-12 08:29:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
2012-11-16 20:24:55 +03:00
|
|
|
if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
|
|
|
|
return ret;
|
2009-06-12 08:29:29 +03:00
|
|
|
if (w != avctx->width || h != avctx->height)
|
|
|
|
avcodec_set_dimensions(avctx, w, h);
|
2012-11-16 20:24:55 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, p)) < 0) {
|
2009-06-12 08:29:29 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
2012-11-16 20:24:55 +03:00
|
|
|
return ret;
|
2009-06-12 08:29:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move pointer to offset from start of file
|
|
|
|
buf = avpkt->data + offset;
|
|
|
|
|
|
|
|
ptr = p->data[0];
|
|
|
|
stride = p->linesize[0];
|
|
|
|
|
2011-03-25 19:58:07 +02:00
|
|
|
if (source_packet_size*avctx->width*avctx->height > buf_end - buf) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
|
2012-11-16 20:24:55 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-03-25 19:58:07 +02:00
|
|
|
}
|
2009-06-12 08:29:29 +03:00
|
|
|
switch (bits_per_color) {
|
|
|
|
case 10:
|
|
|
|
for (x = 0; x < avctx->height; x++) {
|
|
|
|
uint16_t *dst = (uint16_t*)ptr;
|
|
|
|
for (y = 0; y < avctx->width; y++) {
|
|
|
|
rgbBuffer = read32(&buf, endian);
|
|
|
|
// Read out the 10-bit colors and convert to 16-bit
|
|
|
|
*dst++ = make_16bit(rgbBuffer >> 16);
|
|
|
|
*dst++ = make_16bit(rgbBuffer >> 6);
|
|
|
|
*dst++ = make_16bit(rgbBuffer << 4);
|
|
|
|
}
|
|
|
|
ptr += stride;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
case 12: // Treat 12-bit as 16-bit
|
|
|
|
case 16:
|
|
|
|
if (source_packet_size == target_packet_size) {
|
|
|
|
for (x = 0; x < avctx->height; x++) {
|
|
|
|
memcpy(ptr, buf, target_packet_size*avctx->width);
|
|
|
|
ptr += stride;
|
|
|
|
buf += source_packet_size*avctx->width;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (x = 0; x < avctx->height; x++) {
|
|
|
|
uint8_t *dst = ptr;
|
|
|
|
for (y = 0; y < avctx->width; y++) {
|
|
|
|
memcpy(dst, buf, target_packet_size);
|
|
|
|
dst += target_packet_size;
|
|
|
|
buf += source_packet_size;
|
|
|
|
}
|
|
|
|
ptr += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*picture = s->picture;
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2009-06-12 08:29:29 +03:00
|
|
|
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
DPXContext *s = avctx->priv_data;
|
|
|
|
avcodec_get_frame_defaults(&s->picture);
|
|
|
|
avctx->coded_frame = &s->picture;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
DPXContext *s = avctx->priv_data;
|
|
|
|
if (s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_dpx_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "dpx",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_DPX,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(DPXContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
|
|
|
.decode = decode_frame,
|
2012-04-06 19:19:39 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("DPX image"),
|
2012-10-17 22:39:04 +03:00
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2009-06-12 08:29:29 +03:00
|
|
|
};
|