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