00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/imgutils.h"
00024 #include "bytestream.h"
00025 #include "avcodec.h"
00026 #include "internal.h"
00027
00028 typedef struct DPXContext {
00029 AVFrame picture;
00030 } DPXContext;
00031
00032
00033 static unsigned int read32(const uint8_t **ptr, int is_big)
00034 {
00035 unsigned int temp;
00036 if (is_big) {
00037 temp = AV_RB32(*ptr);
00038 } else {
00039 temp = AV_RL32(*ptr);
00040 }
00041 *ptr += 4;
00042 return temp;
00043 }
00044
00045 static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
00046 int * n_datum, int is_big)
00047 {
00048 if (*n_datum)
00049 (*n_datum)--;
00050 else {
00051 *lbuf = read32(ptr, is_big);
00052 *n_datum = 2;
00053 }
00054
00055 *lbuf = (*lbuf << 10) | (*lbuf >> 22);
00056
00057 return *lbuf & 0x3FF;
00058 }
00059
00060 static int decode_frame(AVCodecContext *avctx,
00061 void *data,
00062 int *got_frame,
00063 AVPacket *avpkt)
00064 {
00065 const uint8_t *buf = avpkt->data;
00066 int buf_size = avpkt->size;
00067 DPXContext *const s = avctx->priv_data;
00068 AVFrame *picture = data;
00069 AVFrame *const p = &s->picture;
00070 uint8_t *ptr[AV_NUM_DATA_POINTERS];
00071
00072 unsigned int offset;
00073 int magic_num, endian;
00074 int x, y, i;
00075 int w, h, bits_per_color, descriptor, elements, packing, total_size;
00076
00077 unsigned int rgbBuffer = 0;
00078 int n_datum = 0;
00079
00080 if (avpkt->size <= 1634) {
00081 av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
00082 return AVERROR_INVALIDDATA;
00083 }
00084
00085 magic_num = AV_RB32(buf);
00086 buf += 4;
00087
00088
00089
00090 if (magic_num == AV_RL32("SDPX")) {
00091 endian = 0;
00092 } else if (magic_num == AV_RB32("SDPX")) {
00093 endian = 1;
00094 } else {
00095 av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
00096 return -1;
00097 }
00098
00099 offset = read32(&buf, endian);
00100 if (avpkt->size <= offset) {
00101 av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
00102 return AVERROR_INVALIDDATA;
00103 }
00104
00105 buf = avpkt->data + 0x304;
00106 w = read32(&buf, endian);
00107 h = read32(&buf, endian);
00108 if (av_image_check_size(w, h, 0, avctx))
00109 return AVERROR(EINVAL);
00110
00111 if (w != avctx->width || h != avctx->height)
00112 avcodec_set_dimensions(avctx, w, h);
00113
00114
00115 buf += 20;
00116 descriptor = buf[0];
00117
00118
00119 buf += 3;
00120 avctx->bits_per_raw_sample =
00121 bits_per_color = buf[0];
00122 buf++;
00123 packing = *((uint16_t*)buf);
00124
00125 buf += 824;
00126 avctx->sample_aspect_ratio.num = read32(&buf, endian);
00127 avctx->sample_aspect_ratio.den = read32(&buf, endian);
00128 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
00129 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
00130 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den,
00131 0x10000);
00132 else
00133 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
00134
00135 switch (descriptor) {
00136 case 51:
00137 elements = 4;
00138 break;
00139 case 50:
00140 elements = 3;
00141 break;
00142 default:
00143 av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);
00144 return -1;
00145 }
00146
00147 switch (bits_per_color) {
00148 case 8:
00149 if (elements == 4) {
00150 avctx->pix_fmt = AV_PIX_FMT_RGBA;
00151 } else {
00152 avctx->pix_fmt = AV_PIX_FMT_RGB24;
00153 }
00154 total_size = avctx->width * avctx->height * elements;
00155 break;
00156 case 10:
00157 if (!packing) {
00158 av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
00159 return -1;
00160 }
00161 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
00162 total_size = (4 * avctx->width * avctx->height * elements) / 3;
00163 break;
00164 case 12:
00165 if (!packing) {
00166 av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
00167 return -1;
00168 }
00169 if (endian) {
00170 avctx->pix_fmt = AV_PIX_FMT_GBRP12BE;
00171 } else {
00172 avctx->pix_fmt = AV_PIX_FMT_GBRP12LE;
00173 }
00174 total_size = 2 * avctx->width * avctx->height * elements;
00175 break;
00176 case 16:
00177 if (endian) {
00178 avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGB48BE;
00179 } else {
00180 avctx->pix_fmt = elements == 4 ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGB48LE;
00181 }
00182 total_size = 2 * avctx->width * avctx->height * elements;
00183 break;
00184 default:
00185 av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);
00186 return -1;
00187 }
00188
00189 if (s->picture.data[0])
00190 avctx->release_buffer(avctx, &s->picture);
00191 if (ff_get_buffer(avctx, p) < 0) {
00192 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00193 return -1;
00194 }
00195
00196
00197 buf = avpkt->data + offset;
00198
00199 for (i=0; i<AV_NUM_DATA_POINTERS; i++)
00200 ptr[i] = p->data[i];
00201
00202 if (total_size > avpkt->size) {
00203 av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
00204 return -1;
00205 }
00206 switch (bits_per_color) {
00207 case 10:
00208 for (x = 0; x < avctx->height; x++) {
00209 uint16_t *dst[3] = {(uint16_t*)ptr[0],
00210 (uint16_t*)ptr[1],
00211 (uint16_t*)ptr[2]};
00212 for (y = 0; y < avctx->width; y++) {
00213 *dst[2]++ = read10in32(&buf, &rgbBuffer,
00214 &n_datum, endian);
00215 *dst[0]++ = read10in32(&buf, &rgbBuffer,
00216 &n_datum, endian);
00217 *dst[1]++ = read10in32(&buf, &rgbBuffer,
00218 &n_datum, endian);
00219
00220 if (elements == 4)
00221 read10in32(&buf, &rgbBuffer,
00222 &n_datum, endian);
00223 }
00224 for (i = 0; i < 3; i++)
00225 ptr[i] += p->linesize[i];
00226 }
00227 break;
00228 case 12:
00229 for (x = 0; x < avctx->height; x++) {
00230 uint16_t *dst[3] = {(uint16_t*)ptr[0],
00231 (uint16_t*)ptr[1],
00232 (uint16_t*)ptr[2]};
00233 for (y = 0; y < avctx->width; y++) {
00234 *dst[2] = *((uint16_t*)buf);
00235 *dst[2] = (*dst[2] >> 4) | (*dst[2] << 12);
00236 dst[2]++;
00237 buf += 2;
00238 *dst[0] = *((uint16_t*)buf);
00239 *dst[0] = (*dst[0] >> 4) | (*dst[0] << 12);
00240 dst[0]++;
00241 buf += 2;
00242 *dst[1] = *((uint16_t*)buf);
00243 *dst[1] = (*dst[1] >> 4) | (*dst[1] << 12);
00244 dst[1]++;
00245 buf += 2;
00246
00247 if (elements == 4)
00248 buf += 2;
00249 }
00250 for (i = 0; i < 3; i++)
00251 ptr[i] += p->linesize[i];
00252 }
00253 break;
00254 case 16:
00255 elements *= 2;
00256 case 8:
00257 for (x = 0; x < avctx->height; x++) {
00258 memcpy(ptr[0], buf, elements*avctx->width);
00259 ptr[0] += p->linesize[0];
00260 buf += elements*avctx->width;
00261 }
00262 break;
00263 }
00264
00265 *picture = s->picture;
00266 *got_frame = 1;
00267
00268 return buf_size;
00269 }
00270
00271 static av_cold int decode_init(AVCodecContext *avctx)
00272 {
00273 DPXContext *s = avctx->priv_data;
00274 avcodec_get_frame_defaults(&s->picture);
00275 avctx->coded_frame = &s->picture;
00276 return 0;
00277 }
00278
00279 static av_cold int decode_end(AVCodecContext *avctx)
00280 {
00281 DPXContext *s = avctx->priv_data;
00282 if (s->picture.data[0])
00283 avctx->release_buffer(avctx, &s->picture);
00284
00285 return 0;
00286 }
00287
00288 AVCodec ff_dpx_decoder = {
00289 .name = "dpx",
00290 .type = AVMEDIA_TYPE_VIDEO,
00291 .id = AV_CODEC_ID_DPX,
00292 .priv_data_size = sizeof(DPXContext),
00293 .init = decode_init,
00294 .close = decode_end,
00295 .decode = decode_frame,
00296 .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
00297 .capabilities = CODEC_CAP_DR1,
00298 };