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