]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpx.c
avcodec/dpx: Read alternative frame rate from television header
[ffmpeg] / libavcodec / dpx.c
1 /*
2  * DPX (.dpx) image decoder
3  * Copyright (c) 2009 Jimmy Christensen
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/intfloat.h"
25 #include "libavutil/imgutils.h"
26 #include "bytestream.h"
27 #include "avcodec.h"
28 #include "internal.h"
29
30 static unsigned int read16(const uint8_t **ptr, int is_big)
31 {
32     unsigned int temp;
33     if (is_big) {
34         temp = AV_RB16(*ptr);
35     } else {
36         temp = AV_RL16(*ptr);
37     }
38     *ptr += 2;
39     return temp;
40 }
41
42 static unsigned int read32(const uint8_t **ptr, int is_big)
43 {
44     unsigned int temp;
45     if (is_big) {
46         temp = AV_RB32(*ptr);
47     } else {
48         temp = AV_RL32(*ptr);
49     }
50     *ptr += 4;
51     return temp;
52 }
53
54 static uint16_t read10in32_gray(const uint8_t **ptr, uint32_t *lbuf,
55                                 int *n_datum, int is_big, int shift)
56 {
57     uint16_t temp;
58
59     if (*n_datum)
60         (*n_datum)--;
61     else {
62         *lbuf = read32(ptr, is_big);
63         *n_datum = 2;
64     }
65
66     temp = *lbuf >> shift & 0x3FF;
67     *lbuf = *lbuf >> 10;
68
69     return temp;
70 }
71
72 static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf,
73                            int *n_datum, int is_big, int shift)
74 {
75     if (*n_datum)
76         (*n_datum)--;
77     else {
78         *lbuf = read32(ptr, is_big);
79         *n_datum = 2;
80     }
81
82     *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF;
83
84     return *lbuf & 0x3FF;
85 }
86
87 static uint16_t read12in32(const uint8_t **ptr, uint32_t *lbuf,
88                            int *n_datum, int is_big)
89 {
90     if (*n_datum)
91         (*n_datum)--;
92     else {
93         *lbuf = read32(ptr, is_big);
94         *n_datum = 7;
95     }
96
97     switch (*n_datum){
98     case 7: return *lbuf & 0xFFF;
99     case 6: return (*lbuf >> 12) & 0xFFF;
100     case 5: {
101             uint32_t c = *lbuf >> 24;
102             *lbuf = read32(ptr, is_big);
103             c |= *lbuf << 8;
104             return c & 0xFFF;
105             }
106     case 4: return (*lbuf >> 4) & 0xFFF;
107     case 3: return (*lbuf >> 16) & 0xFFF;
108     case 2: {
109             uint32_t c = *lbuf >> 28;
110             *lbuf = read32(ptr, is_big);
111             c |= *lbuf << 4;
112             return c & 0xFFF;
113             }
114     case 1: return (*lbuf >> 8) & 0xFFF;
115     default: return *lbuf >> 20;
116     }
117 }
118
119 static int decode_frame(AVCodecContext *avctx,
120                         void *data,
121                         int *got_frame,
122                         AVPacket *avpkt)
123 {
124     const uint8_t *buf = avpkt->data;
125     int buf_size       = avpkt->size;
126     AVFrame *const p = data;
127     uint8_t *ptr[AV_NUM_DATA_POINTERS];
128     uint32_t header_version, version = 0;
129     char creator[101];
130     char input_device[33];
131
132     unsigned int offset;
133     int magic_num, endian;
134     int x, y, stride, i, ret;
135     int w, h, bits_per_color, descriptor, elements, packing;
136     int encoding, need_align = 0;
137
138     unsigned int rgbBuffer = 0;
139     int n_datum = 0;
140
141     if (avpkt->size <= 1634) {
142         av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
143         return AVERROR_INVALIDDATA;
144     }
145
146     magic_num = AV_RB32(buf);
147     buf += 4;
148
149     /* Check if the files "magic number" is "SDPX" which means it uses
150      * big-endian or XPDS which is for little-endian files */
151     if (magic_num == AV_RL32("SDPX")) {
152         endian = 0;
153     } else if (magic_num == AV_RB32("SDPX")) {
154         endian = 1;
155     } else {
156         av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
157         return AVERROR_INVALIDDATA;
158     }
159
160     offset = read32(&buf, endian);
161     if (avpkt->size <= offset) {
162         av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
163         return AVERROR_INVALIDDATA;
164     }
165
166     header_version = read32(&buf, 0);
167     if (header_version == MKTAG('V','1','.','0'))
168         version = 1;
169     if (header_version == MKTAG('V','2','.','0'))
170         version = 2;
171     if (!version)
172         av_log(avctx, AV_LOG_WARNING, "Unknown header format version %s.\n",
173                av_fourcc2str(header_version));
174
175     // Check encryption
176     buf = avpkt->data + 660;
177     ret = read32(&buf, endian);
178     if (ret != 0xFFFFFFFF) {
179         avpriv_report_missing_feature(avctx, "Encryption");
180         av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
181                "not properly decode.\n");
182     }
183
184     // Need to end in 0x304 offset from start of file
185     buf = avpkt->data + 0x304;
186     w = read32(&buf, endian);
187     h = read32(&buf, endian);
188
189     if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
190         return ret;
191
192     // Need to end in 0x320 to read the descriptor
193     buf += 20;
194     descriptor = buf[0];
195
196     // Need to end in 0x323 to read the bits per color
197     buf += 3;
198     avctx->bits_per_raw_sample =
199     bits_per_color = buf[0];
200     buf++;
201     packing = read16(&buf, endian);
202     encoding = read16(&buf, endian);
203
204     if (encoding) {
205         avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
206         return AVERROR_PATCHWELCOME;
207     }
208
209     buf += 820;
210     avctx->sample_aspect_ratio.num = read32(&buf, endian);
211     avctx->sample_aspect_ratio.den = read32(&buf, endian);
212     if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
213         av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
214                    avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den,
215                   0x10000);
216     else
217         avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
218
219     /* preferred frame rate from Motion-picture film header */
220     if (offset >= 1724 + 4) {
221         buf = avpkt->data + 1724;
222         i = read32(&buf, endian);
223         if(i && i != 0xFFFFFFFF) {
224             AVRational q = av_d2q(av_int2float(i), 4096);
225             if (q.num > 0 && q.den > 0)
226                 avctx->framerate = q;
227         }
228     }
229
230     /* alternative frame rate from television header */
231     if (offset >= 1940 + 4 &&
232         !(avctx->framerate.num && avctx->framerate.den)) {
233         buf = avpkt->data + 1940;
234         i = read32(&buf, endian);
235         if(i && i != 0xFFFFFFFF) {
236             AVRational q = av_d2q(av_int2float(i), 4096);
237             if (q.num > 0 && q.den > 0)
238                 avctx->framerate = q;
239         }
240     }
241
242     switch (descriptor) {
243     case 6:  // Y
244         elements = 1;
245         break;
246     case 52: // ABGR
247     case 51: // RGBA
248     case 103: // UYVA4444
249         elements = 4;
250         break;
251     case 50: // RGB
252     case 102: // UYV444
253         elements = 3;
254         break;
255     case 100: // UYVY422
256         elements = 2;
257         break;
258     default:
259         avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
260         return AVERROR_PATCHWELCOME;
261     }
262
263     switch (bits_per_color) {
264     case 8:
265         stride = avctx->width * elements;
266         break;
267     case 10:
268         if (!packing) {
269             av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
270             return -1;
271         }
272         stride = (avctx->width * elements + 2) / 3 * 4;
273         break;
274     case 12:
275         stride = avctx->width * elements;
276         if (packing) {
277             stride *= 2;
278         } else {
279             stride *= 3;
280             if (stride % 8) {
281                 stride /= 8;
282                 stride++;
283                 stride *= 8;
284             }
285             stride /= 2;
286         }
287         break;
288     case 16:
289         stride = 2 * avctx->width * elements;
290         break;
291     case 1:
292     case 32:
293     case 64:
294         avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
295         return AVERROR_PATCHWELCOME;
296     default:
297         return AVERROR_INVALIDDATA;
298     }
299
300     // Table 3c: Runs will always break at scan line boundaries. Packing
301     // will always break to the next 32-bit word at scan-line boundaries.
302     // Unfortunately, the encoder produced invalid files, so attempt
303     // to detect it
304     need_align = FFALIGN(stride, 4);
305     if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
306         // Alignment seems unappliable, try without
307         if (stride*avctx->height + (int64_t)offset > avpkt->size) {
308             av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
309             return AVERROR_INVALIDDATA;
310         } else {
311             av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
312                    "alignment.\n");
313             need_align = 0;
314         }
315     } else {
316         need_align -= stride;
317         stride = FFALIGN(stride, 4);
318     }
319
320     switch (1000 * descriptor + 10 * bits_per_color + endian) {
321     case 6081:
322     case 6080:
323         avctx->pix_fmt = AV_PIX_FMT_GRAY8;
324         break;
325     case 6121:
326     case 6120:
327         avctx->pix_fmt = AV_PIX_FMT_GRAY12;
328         break;
329     case 50081:
330     case 50080:
331         avctx->pix_fmt = AV_PIX_FMT_RGB24;
332         break;
333     case 52081:
334     case 52080:
335         avctx->pix_fmt = AV_PIX_FMT_ABGR;
336         break;
337     case 51081:
338     case 51080:
339         avctx->pix_fmt = AV_PIX_FMT_RGBA;
340         break;
341     case 50100:
342     case 50101:
343         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
344         break;
345     case 51100:
346     case 51101:
347         avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
348         break;
349     case 50120:
350     case 50121:
351         avctx->pix_fmt = AV_PIX_FMT_GBRP12;
352         break;
353     case 51120:
354     case 51121:
355         avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
356         break;
357     case 6100:
358     case 6101:
359         avctx->pix_fmt = AV_PIX_FMT_GRAY10;
360         break;
361     case 6161:
362         avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
363         break;
364     case 6160:
365         avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
366         break;
367     case 50161:
368         avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
369         break;
370     case 50160:
371         avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
372         break;
373     case 51161:
374         avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
375         break;
376     case 51160:
377         avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
378         break;
379     case 100081:
380         avctx->pix_fmt = AV_PIX_FMT_UYVY422;
381         break;
382     case 102081:
383         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
384         break;
385     case 103081:
386         avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
387         break;
388     default:
389         av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
390         return AVERROR_PATCHWELCOME;
391     }
392
393     ff_set_sar(avctx, avctx->sample_aspect_ratio);
394
395     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
396         return ret;
397
398     av_strlcpy(creator, avpkt->data + 160, 100);
399     creator[100] = '\0';
400     av_dict_set(&p->metadata, "Creator", creator, 0);
401
402     av_strlcpy(input_device, avpkt->data + 1556, 32);
403     input_device[32] = '\0';
404     av_dict_set(&p->metadata, "Input Device", input_device, 0);
405
406     // Move pointer to offset from start of file
407     buf =  avpkt->data + offset;
408
409     for (i=0; i<AV_NUM_DATA_POINTERS; i++)
410         ptr[i] = p->data[i];
411
412     switch (bits_per_color) {
413     case 10:
414         for (x = 0; x < avctx->height; x++) {
415             uint16_t *dst[4] = {(uint16_t*)ptr[0],
416                                 (uint16_t*)ptr[1],
417                                 (uint16_t*)ptr[2],
418                                 (uint16_t*)ptr[3]};
419             int shift = elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
420             for (y = 0; y < avctx->width; y++) {
421                 if (elements >= 3)
422                     *dst[2]++ = read10in32(&buf, &rgbBuffer,
423                                            &n_datum, endian, shift);
424                 if (elements == 1)
425                     *dst[0]++ = read10in32_gray(&buf, &rgbBuffer,
426                                                 &n_datum, endian, shift);
427                 else
428                     *dst[0]++ = read10in32(&buf, &rgbBuffer,
429                                            &n_datum, endian, shift);
430                 if (elements >= 2)
431                     *dst[1]++ = read10in32(&buf, &rgbBuffer,
432                                            &n_datum, endian, shift);
433                 if (elements == 4)
434                     *dst[3]++ =
435                     read10in32(&buf, &rgbBuffer,
436                                &n_datum, endian, shift);
437             }
438             if (memcmp(input_device, "Scanity", 7))
439                 n_datum = 0;
440             for (i = 0; i < elements; i++)
441                 ptr[i] += p->linesize[i];
442         }
443         break;
444     case 12:
445         for (x = 0; x < avctx->height; x++) {
446             uint16_t *dst[4] = {(uint16_t*)ptr[0],
447                                 (uint16_t*)ptr[1],
448                                 (uint16_t*)ptr[2],
449                                 (uint16_t*)ptr[3]};
450             int shift = packing == 1 ? 4 : 0;
451             for (y = 0; y < avctx->width; y++) {
452                 if (packing) {
453                     if (elements >= 3)
454                         *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
455                     *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
456                     if (elements >= 2)
457                         *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
458                     if (elements == 4)
459                         *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
460                 } else {
461                     if (elements >= 3)
462                         *dst[2]++ = read12in32(&buf, &rgbBuffer,
463                                                &n_datum, endian);
464                     *dst[0]++ = read12in32(&buf, &rgbBuffer,
465                                            &n_datum, endian);
466                     if (elements >= 2)
467                         *dst[1]++ = read12in32(&buf, &rgbBuffer,
468                                                &n_datum, endian);
469                     if (elements == 4)
470                         *dst[3]++ = read12in32(&buf, &rgbBuffer,
471                                                &n_datum, endian);
472                 }
473             }
474             n_datum = 0;
475             for (i = 0; i < elements; i++)
476                 ptr[i] += p->linesize[i];
477             // Jump to next aligned position
478             buf += need_align;
479         }
480         break;
481     case 16:
482         elements *= 2;
483     case 8:
484         if (   avctx->pix_fmt == AV_PIX_FMT_YUVA444P
485             || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
486             for (x = 0; x < avctx->height; x++) {
487                 ptr[0] = p->data[0] + x * p->linesize[0];
488                 ptr[1] = p->data[1] + x * p->linesize[1];
489                 ptr[2] = p->data[2] + x * p->linesize[2];
490                 ptr[3] = p->data[3] + x * p->linesize[3];
491                 for (y = 0; y < avctx->width; y++) {
492                     *ptr[1]++ = *buf++;
493                     *ptr[0]++ = *buf++;
494                     *ptr[2]++ = *buf++;
495                     if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
496                         *ptr[3]++ = *buf++;
497                 }
498             }
499         } else {
500         av_image_copy_plane(ptr[0], p->linesize[0],
501                             buf, stride,
502                             elements * avctx->width, avctx->height);
503         }
504         break;
505     }
506
507     *got_frame = 1;
508
509     return buf_size;
510 }
511
512 AVCodec ff_dpx_decoder = {
513     .name           = "dpx",
514     .long_name      = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
515     .type           = AVMEDIA_TYPE_VIDEO,
516     .id             = AV_CODEC_ID_DPX,
517     .decode         = decode_frame,
518     .capabilities   = AV_CODEC_CAP_DR1,
519 };