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