From 94d3d6a468b0cba859157f12959b6b82b4fc6c79 Mon Sep 17 00:00:00 2001 From: Jimmy Christensen Date: Fri, 12 Jun 2009 05:29:29 +0000 Subject: [PATCH] DPX (Digital Picture Exchange) image decoder patch by Jimmy Christensen, jimmy ghost dk Originally committed as revision 19163 to svn://svn.ffmpeg.org/ffmpeg/trunk --- Changelog | 1 + doc/general.texi | 2 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/dpx.c | 228 +++++++++++++++++++++++++++++++++++++++++ libavformat/img2.c | 1 + libavformat/isom.c | 1 + libavformat/riff.c | 1 + 9 files changed, 237 insertions(+) create mode 100644 libavcodec/dpx.c diff --git a/Changelog b/Changelog index 677c972f24..f67c1f2ecc 100644 --- a/Changelog +++ b/Changelog @@ -23,6 +23,7 @@ version : - QCP demuxer - SoX native format muxer and demuxer - AMR-NB decoding/encoding, AMR-WB decoding via OpenCORE libraries +- DPX image decoder diff --git a/doc/general.texi b/doc/general.texi index 3e95e2417e..58bb33c434 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -258,6 +258,8 @@ following image formats are supported: @tab Only uncompressed GIFs are generated. @item BMP @tab X @tab X @tab Microsoft BMP image +@item DPX @tab @tab X + @tab Digital Picture Exchange @item JPEG @tab X @tab X @tab Progressive JPEG is not supported. @item JPEG 2000 @tab @tab E diff --git a/libavcodec/Makefile b/libavcodec/Makefile index be12f6345f..13153eda9f 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -64,6 +64,7 @@ OBJS-$(CONFIG_CYUV_DECODER) += cyuv.o OBJS-$(CONFIG_DCA_DECODER) += dca.o OBJS-$(CONFIG_DNXHD_DECODER) += dnxhddec.o dnxhddata.o OBJS-$(CONFIG_DNXHD_ENCODER) += dnxhdenc.o dnxhddata.o mpegvideo_enc.o motion_est.o ratecontrol.o mpeg12data.o mpegvideo.o +OBJS-$(CONFIG_DPX_DECODER) += dpx.o OBJS-$(CONFIG_DSICINAUDIO_DECODER) += dsicinav.o OBJS-$(CONFIG_DSICINVIDEO_DECODER) += dsicinav.o OBJS-$(CONFIG_DVBSUB_DECODER) += dvbsubdec.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 803699b7cd..76cde27216 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -76,6 +76,7 @@ void avcodec_register_all(void) REGISTER_DECODER (CSCD, cscd); REGISTER_DECODER (CYUV, cyuv); REGISTER_ENCDEC (DNXHD, dnxhd); + REGISTER_DECODER (DPX, dpx); REGISTER_DECODER (DSICINVIDEO, dsicinvideo); REGISTER_ENCDEC (DVVIDEO, dvvideo); REGISTER_DECODER (DXA, dxa); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index aaa95b55bc..be85ef113b 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -196,6 +196,7 @@ enum CodecID { CODEC_ID_V210X, CODEC_ID_TMV, CODEC_ID_V210, + CODEC_ID_DPX, /* various PCM "codecs" */ CODEC_ID_PCM_S16LE= 0x10000, diff --git a/libavcodec/dpx.c b/libavcodec/dpx.c new file mode 100644 index 0000000000..fec12f94ca --- /dev/null +++ b/libavcodec/dpx.c @@ -0,0 +1,228 @@ +/* + * 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" +#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; +} + +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, + 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; + uint8_t *ptr; + + int magic_num, offset, endian; + int x, y; + int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size; + + unsigned int rgbBuffer; + + 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); + // 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; + bits_per_color = buf[0]; + + 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) { + avctx->pix_fmt = PIX_FMT_RGBA; + } else { + avctx->pix_fmt = PIX_FMT_RGB24; + } + source_packet_size = elements; + target_packet_size = elements; + break; + case 10: + avctx->pix_fmt = PIX_FMT_RGB48; + target_packet_size = 6; + source_packet_size = elements * 2; + break; + case 12: + case 16: + if (endian) { + avctx->pix_fmt = PIX_FMT_RGB48BE; + } else { + avctx->pix_fmt = PIX_FMT_RGB48LE; + } + 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); + return -1; + } + + if (s->picture.data[0]) + avctx->release_buffer(avctx, &s->picture); + if (avcodec_check_dimensions(avctx, w, h)) + 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; + + ptr = p->data[0]; + stride = p->linesize[0]; + + 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; + *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; +} + +AVCodec dpx_decoder = { + "dpx", + CODEC_TYPE_VIDEO, + CODEC_ID_DPX, + sizeof(DPXContext), + decode_init, + NULL, + decode_end, + decode_frame, + 0, + NULL, + .long_name = NULL_IF_CONFIG_SMALL("DPX image"), +}; diff --git a/libavformat/img2.c b/libavformat/img2.c index 48f3dfc4a5..ad22d224bf 100644 --- a/libavformat/img2.c +++ b/libavformat/img2.c @@ -72,6 +72,7 @@ static const IdStrMap img_tags[] = { { CODEC_ID_SUNRAST , "im24"}, { CODEC_ID_SUNRAST , "sunras"}, { CODEC_ID_JPEG2000 , "jp2"}, + { CODEC_ID_DPX , "dpx"}, { CODEC_ID_NONE , NULL} }; diff --git a/libavformat/isom.c b/libavformat/isom.c index b706c45194..0a410c17c4 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -168,6 +168,7 @@ const AVCodecTag codec_movvideo_tags[] = { { CODEC_ID_DIRAC, MKTAG('d', 'r', 'a', 'c') }, { CODEC_ID_DNXHD, MKTAG('A', 'V', 'd', 'n') }, /* AVID DNxHD */ { CODEC_ID_SGI, MKTAG('s', 'g', 'i', ' ') }, /* SGI */ + { CODEC_ID_DPX, MKTAG('d', 'p', 'x', ' ') }, /* DPX */ { CODEC_ID_NONE, 0 }, }; diff --git a/libavformat/riff.c b/libavformat/riff.c index 858c924b7e..d9b4f449a7 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -212,6 +212,7 @@ const AVCodecTag codec_bmp_tags[] = { { CODEC_ID_SP5X, MKTAG('S', 'P', '5', '4') }, { CODEC_ID_AURA, MKTAG('A', 'U', 'R', 'A') }, { CODEC_ID_AURA2, MKTAG('A', 'U', 'R', '2') }, + { CODEC_ID_DPX, MKTAG('d', 'p', 'x', ' ') }, { CODEC_ID_NONE, 0 } };