2009-06-12 05:29:29 +00:00
|
|
|
/*
|
|
|
|
* DPX (.dpx) image decoder
|
|
|
|
* Copyright (c) 2009 Jimmy Christensen
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-02-07 13:37:08 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
2009-06-12 05:29:29 +00:00
|
|
|
#include "bytestream.h"
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-12 19:18:49 +00:00
|
|
|
static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
|
|
|
|
int * n_datum, int is_big)
|
|
|
|
{
|
|
|
|
if (*n_datum)
|
|
|
|
(*n_datum)--;
|
|
|
|
else {
|
|
|
|
*lbuf = read32(ptr, is_big);
|
|
|
|
*n_datum = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
*lbuf = (*lbuf << 10) | (*lbuf >> 22);
|
|
|
|
|
|
|
|
return *lbuf & 0x3FF;
|
|
|
|
}
|
|
|
|
|
2009-06-12 05:29:29 +00:00
|
|
|
static int decode_frame(AVCodecContext *avctx,
|
|
|
|
void *data,
|
|
|
|
int *data_size,
|
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
DPXContext *const s = avctx->priv_data;
|
|
|
|
AVFrame *picture = data;
|
|
|
|
AVFrame *const p = &s->picture;
|
2012-08-26 12:51:13 +00:00
|
|
|
uint8_t *ptr[AV_NUM_DATA_POINTERS];
|
2009-06-12 05:29:29 +00:00
|
|
|
|
2012-08-04 23:41:20 +00:00
|
|
|
unsigned int offset;
|
|
|
|
int magic_num, endian;
|
2012-08-26 12:51:13 +00:00
|
|
|
int x, y, i;
|
2012-10-12 19:18:49 +00:00
|
|
|
int w, h, bits_per_color, descriptor, elements, packing, total_size;
|
2009-06-12 05:29:29 +00:00
|
|
|
|
2012-10-12 19:18:49 +00:00
|
|
|
unsigned int rgbBuffer = 0;
|
|
|
|
int n_datum = 0;
|
2009-06-12 05:29:29 +00:00
|
|
|
|
2011-04-01 12:23:28 +00:00
|
|
|
if (avpkt->size <= 1634) {
|
2011-03-25 17:58:07 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2009-06-12 05:29:29 +00: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");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = read32(&buf, endian);
|
2011-03-25 17:58:07 +00:00
|
|
|
if (avpkt->size <= offset) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2009-06-12 05:29:29 +00: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 20:46:48 +00:00
|
|
|
avctx->bits_per_raw_sample =
|
2009-06-12 05:29:29 +00:00
|
|
|
bits_per_color = buf[0];
|
2012-10-12 19:18:49 +00:00
|
|
|
buf++;
|
|
|
|
packing = *((uint16_t*)buf);
|
2009-06-12 05:29:29 +00:00
|
|
|
|
2012-10-12 19:18:49 +00:00
|
|
|
buf += 824;
|
2011-04-01 12:23:28 +00:00
|
|
|
avctx->sample_aspect_ratio.num = read32(&buf, endian);
|
|
|
|
avctx->sample_aspect_ratio.den = read32(&buf, endian);
|
2012-06-18 09:39:26 +00:00
|
|
|
if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
|
|
|
|
av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
|
|
|
|
avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
|
|
|
|
0x10000);
|
|
|
|
else
|
2012-07-09 07:13:21 +00:00
|
|
|
avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
|
2011-04-01 12:23:28 +00:00
|
|
|
|
2009-06-12 05:29:29 +00: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);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (bits_per_color) {
|
|
|
|
case 8:
|
|
|
|
if (elements == 4) {
|
2012-10-06 10:10:34 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGBA;
|
2009-06-12 05:29:29 +00:00
|
|
|
} else {
|
2012-10-06 10:10:34 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB24;
|
2009-06-12 05:29:29 +00:00
|
|
|
}
|
2012-10-12 19:18:49 +00:00
|
|
|
total_size = avctx->width * avctx->height * elements;
|
2009-06-12 05:29:29 +00:00
|
|
|
break;
|
|
|
|
case 10:
|
2012-10-12 19:18:49 +00:00
|
|
|
if (!packing) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-08 18:54:00 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRP10;
|
2012-10-12 19:18:49 +00:00
|
|
|
total_size = (4 * avctx->width * avctx->height * elements) / 3;
|
2009-06-12 05:29:29 +00:00
|
|
|
break;
|
|
|
|
case 12:
|
2012-10-12 19:18:49 +00:00
|
|
|
if (!packing) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-26 12:51:13 +00:00
|
|
|
if (endian) {
|
2012-10-12 19:18:49 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRP12BE;
|
2012-08-26 12:51:13 +00:00
|
|
|
} else {
|
2012-10-12 19:18:49 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_GBRP12LE;
|
2012-08-26 12:51:13 +00:00
|
|
|
}
|
2012-10-12 19:18:49 +00:00
|
|
|
total_size = 2 * avctx->width * avctx->height * elements;
|
2012-08-26 12:51:13 +00:00
|
|
|
break;
|
2009-06-12 05:29:29 +00:00
|
|
|
case 16:
|
|
|
|
if (endian) {
|
2012-10-08 18:54:00 +00:00
|
|
|
avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGB48BE;
|
2009-06-12 05:29:29 +00:00
|
|
|
} else {
|
2012-10-08 18:54:00 +00:00
|
|
|
avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGB48LE;
|
2009-06-12 05:29:29 +00:00
|
|
|
}
|
2012-10-12 19:18:49 +00:00
|
|
|
total_size = 2 * avctx->width * avctx->height * elements;
|
2009-06-12 05:29:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->picture.data[0])
|
|
|
|
avctx->release_buffer(avctx, &s->picture);
|
2010-09-07 19:15:29 +00:00
|
|
|
if (av_image_check_size(w, h, 0, avctx))
|
2009-06-12 05:29:29 +00:00
|
|
|
return -1;
|
|
|
|
if (w != avctx->width || h != avctx->height)
|
|
|
|
avcodec_set_dimensions(avctx, w, h);
|
|
|
|
if (avctx->get_buffer(avctx, p) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move pointer to offset from start of file
|
|
|
|
buf = avpkt->data + offset;
|
|
|
|
|
2012-08-26 12:51:13 +00:00
|
|
|
for (i=0; i<AV_NUM_DATA_POINTERS; i++)
|
|
|
|
ptr[i] = p->data[i];
|
2009-06-12 05:29:29 +00:00
|
|
|
|
2012-10-12 19:18:49 +00:00
|
|
|
if (total_size > avpkt->size) {
|
2011-03-25 17:58:07 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-12 05:29:29 +00:00
|
|
|
switch (bits_per_color) {
|
2012-10-12 19:18:49 +00:00
|
|
|
case 10:
|
|
|
|
for (x = 0; x < avctx->height; x++) {
|
|
|
|
uint16_t *dst[3] = {(uint16_t*)ptr[0],
|
|
|
|
(uint16_t*)ptr[1],
|
|
|
|
(uint16_t*)ptr[2]};
|
|
|
|
for (y = 0; y < avctx->width; y++) {
|
|
|
|
*dst[2]++ = read10in32(&buf, &rgbBuffer,
|
|
|
|
&n_datum, endian);
|
|
|
|
*dst[0]++ = read10in32(&buf, &rgbBuffer,
|
|
|
|
&n_datum, endian);
|
|
|
|
*dst[1]++ = read10in32(&buf, &rgbBuffer,
|
|
|
|
&n_datum, endian);
|
|
|
|
// For 10 bit, ignore alpha
|
|
|
|
if (elements == 4)
|
|
|
|
read10in32(&buf, &rgbBuffer,
|
|
|
|
&n_datum, endian);
|
2009-06-12 05:29:29 +00:00
|
|
|
}
|
2012-10-12 19:18:49 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
ptr[i] += p->linesize[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
for (x = 0; x < avctx->height; x++) {
|
|
|
|
uint16_t *dst[3] = {(uint16_t*)ptr[0],
|
|
|
|
(uint16_t*)ptr[1],
|
|
|
|
(uint16_t*)ptr[2]};
|
|
|
|
for (y = 0; y < avctx->width; y++) {
|
|
|
|
*dst[2] = *((uint16_t*)buf);
|
|
|
|
*dst[2] = (*dst[2] >> 4) | (*dst[2] << 12);
|
|
|
|
dst[2]++;
|
|
|
|
buf += 2;
|
|
|
|
*dst[0] = *((uint16_t*)buf);
|
|
|
|
*dst[0] = (*dst[0] >> 4) | (*dst[0] << 12);
|
|
|
|
dst[0]++;
|
|
|
|
buf += 2;
|
|
|
|
*dst[1] = *((uint16_t*)buf);
|
|
|
|
*dst[1] = (*dst[1] >> 4) | (*dst[1] << 12);
|
|
|
|
dst[1]++;
|
|
|
|
buf += 2;
|
|
|
|
// For 12 bit, ignore alpha
|
|
|
|
if (elements == 4)
|
|
|
|
buf += 2;
|
2009-06-12 05:29:29 +00:00
|
|
|
}
|
2012-10-12 19:18:49 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
ptr[i] += p->linesize[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
elements *= 2;
|
|
|
|
case 8:
|
|
|
|
for (x = 0; x < avctx->height; x++) {
|
|
|
|
memcpy(ptr[0], buf, elements*avctx->width);
|
|
|
|
ptr[0] += p->linesize[0];
|
|
|
|
buf += elements*avctx->width;
|
|
|
|
}
|
|
|
|
break;
|
2009-06-12 05:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*picture = s->picture;
|
|
|
|
*data_size = sizeof(AVPicture);
|
|
|
|
|
|
|
|
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 21:40:11 +00:00
|
|
|
AVCodec ff_dpx_decoder = {
|
2011-07-17 10:54:31 +00:00
|
|
|
.name = "dpx",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_DPX,
|
2011-07-17 10:54:31 +00:00
|
|
|
.priv_data_size = sizeof(DPXContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
|
|
|
.decode = decode_frame,
|
2012-07-07 20:02:20 +00:00
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2012-04-06 16:19:39 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("DPX image"),
|
2009-06-12 05:29:29 +00:00
|
|
|
};
|