• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum DPX_TRC {
32     DPX_TRC_USER_DEFINED       = 0,
33     DPX_TRC_PRINTING_DENSITY   = 1,
34     DPX_TRC_LINEAR             = 2,
35     DPX_TRC_LOGARITHMIC        = 3,
36     DPX_TRC_UNSPECIFIED_VIDEO  = 4,
37     DPX_TRC_SMPTE_274          = 5,
38     DPX_TRC_ITU_R_709_4        = 6,
39     DPX_TRC_ITU_R_601_625      = 7,
40     DPX_TRC_ITU_R_601_525      = 8,
41     DPX_TRC_SMPTE_170          = 9,
42     DPX_TRC_ITU_R_624_4_PAL    = 10,
43     DPX_TRC_Z_LINEAR           = 11,
44     DPX_TRC_Z_HOMOGENEOUS      = 12,
45 };
46 
47 enum DPX_COL_SPEC {
48     DPX_COL_SPEC_USER_DEFINED       = 0,
49     DPX_COL_SPEC_PRINTING_DENSITY   = 1,
50     /* 2 = N/A */
51     /* 3 = N/A */
52     DPX_COL_SPEC_UNSPECIFIED_VIDEO  = 4,
53     DPX_COL_SPEC_SMPTE_274          = 5,
54     DPX_COL_SPEC_ITU_R_709_4        = 6,
55     DPX_COL_SPEC_ITU_R_601_625      = 7,
56     DPX_COL_SPEC_ITU_R_601_525      = 8,
57     DPX_COL_SPEC_SMPTE_170          = 9,
58     DPX_COL_SPEC_ITU_R_624_4_PAL    = 10,
59     /* 11 = N/A */
60     /* 12 = N/A */
61 };
62 
read16(const uint8_t ** ptr,int is_big)63 static unsigned int read16(const uint8_t **ptr, int is_big)
64 {
65     unsigned int temp;
66     if (is_big) {
67         temp = AV_RB16(*ptr);
68     } else {
69         temp = AV_RL16(*ptr);
70     }
71     *ptr += 2;
72     return temp;
73 }
74 
read32(const uint8_t ** ptr,int is_big)75 static unsigned int read32(const uint8_t **ptr, int is_big)
76 {
77     unsigned int temp;
78     if (is_big) {
79         temp = AV_RB32(*ptr);
80     } else {
81         temp = AV_RL32(*ptr);
82     }
83     *ptr += 4;
84     return temp;
85 }
86 
read10in32_gray(const uint8_t ** ptr,uint32_t * lbuf,int * n_datum,int is_big,int shift)87 static uint16_t read10in32_gray(const uint8_t **ptr, uint32_t *lbuf,
88                                 int *n_datum, int is_big, int shift)
89 {
90     uint16_t temp;
91 
92     if (*n_datum)
93         (*n_datum)--;
94     else {
95         *lbuf = read32(ptr, is_big);
96         *n_datum = 2;
97     }
98 
99     temp = *lbuf >> shift & 0x3FF;
100     *lbuf = *lbuf >> 10;
101 
102     return temp;
103 }
104 
read10in32(const uint8_t ** ptr,uint32_t * lbuf,int * n_datum,int is_big,int shift)105 static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf,
106                            int *n_datum, int is_big, int shift)
107 {
108     if (*n_datum)
109         (*n_datum)--;
110     else {
111         *lbuf = read32(ptr, is_big);
112         *n_datum = 2;
113     }
114 
115     *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF;
116 
117     return *lbuf & 0x3FF;
118 }
119 
read12in32(const uint8_t ** ptr,uint32_t * lbuf,int * n_datum,int is_big)120 static uint16_t read12in32(const uint8_t **ptr, uint32_t *lbuf,
121                            int *n_datum, int is_big)
122 {
123     if (*n_datum)
124         (*n_datum)--;
125     else {
126         *lbuf = read32(ptr, is_big);
127         *n_datum = 7;
128     }
129 
130     switch (*n_datum){
131     case 7: return *lbuf & 0xFFF;
132     case 6: return (*lbuf >> 12) & 0xFFF;
133     case 5: {
134             uint32_t c = *lbuf >> 24;
135             *lbuf = read32(ptr, is_big);
136             c |= *lbuf << 8;
137             return c & 0xFFF;
138             }
139     case 4: return (*lbuf >> 4) & 0xFFF;
140     case 3: return (*lbuf >> 16) & 0xFFF;
141     case 2: {
142             uint32_t c = *lbuf >> 28;
143             *lbuf = read32(ptr, is_big);
144             c |= *lbuf << 4;
145             return c & 0xFFF;
146             }
147     case 1: return (*lbuf >> 8) & 0xFFF;
148     default: return *lbuf >> 20;
149     }
150 }
151 
decode_frame(AVCodecContext * avctx,void * data,int * got_frame,AVPacket * avpkt)152 static int decode_frame(AVCodecContext *avctx,
153                         void *data,
154                         int *got_frame,
155                         AVPacket *avpkt)
156 {
157     const uint8_t *buf = avpkt->data;
158     int buf_size       = avpkt->size;
159     AVFrame *const p = data;
160     uint8_t *ptr[AV_NUM_DATA_POINTERS];
161     uint32_t header_version, version = 0;
162     char creator[101] = { 0 };
163     char input_device[33] = { 0 };
164 
165     unsigned int offset;
166     int magic_num, endian;
167     int x, y, stride, i, j, ret;
168     int w, h, bits_per_color, descriptor, elements, packing;
169     int yuv, color_trc, color_spec;
170     int encoding, need_align = 0, unpadded_10bit = 0;
171 
172     unsigned int rgbBuffer = 0;
173     int n_datum = 0;
174 
175     if (avpkt->size <= 1634) {
176         av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
177         return AVERROR_INVALIDDATA;
178     }
179 
180     magic_num = AV_RB32(buf);
181     buf += 4;
182 
183     /* Check if the files "magic number" is "SDPX" which means it uses
184      * big-endian or XPDS which is for little-endian files */
185     if (magic_num == AV_RL32("SDPX")) {
186         endian = 0;
187     } else if (magic_num == AV_RB32("SDPX")) {
188         endian = 1;
189     } else {
190         av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
191         return AVERROR_INVALIDDATA;
192     }
193 
194     offset = read32(&buf, endian);
195     if (avpkt->size <= offset) {
196         av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
197         return AVERROR_INVALIDDATA;
198     }
199 
200     header_version = read32(&buf, 0);
201     if (header_version == MKTAG('V','1','.','0'))
202         version = 1;
203     if (header_version == MKTAG('V','2','.','0'))
204         version = 2;
205     if (!version)
206         av_log(avctx, AV_LOG_WARNING, "Unknown header format version %s.\n",
207                av_fourcc2str(header_version));
208 
209     // Check encryption
210     buf = avpkt->data + 660;
211     ret = read32(&buf, endian);
212     if (ret != 0xFFFFFFFF) {
213         avpriv_report_missing_feature(avctx, "Encryption");
214         av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
215                "not properly decode.\n");
216     }
217 
218     // Need to end in 0x304 offset from start of file
219     buf = avpkt->data + 0x304;
220     w = read32(&buf, endian);
221     h = read32(&buf, endian);
222 
223     if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
224         return ret;
225 
226     // Need to end in 0x320 to read the descriptor
227     buf += 20;
228     descriptor = buf[0];
229     color_trc = buf[1];
230     color_spec = buf[2];
231 
232     // Need to end in 0x323 to read the bits per color
233     buf += 3;
234     avctx->bits_per_raw_sample =
235     bits_per_color = buf[0];
236     buf++;
237     packing = read16(&buf, endian);
238     encoding = read16(&buf, endian);
239 
240     if (encoding) {
241         avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
242         return AVERROR_PATCHWELCOME;
243     }
244 
245     if (bits_per_color > 31)
246         return AVERROR_INVALIDDATA;
247 
248     buf += 820;
249     avctx->sample_aspect_ratio.num = read32(&buf, endian);
250     avctx->sample_aspect_ratio.den = read32(&buf, endian);
251     if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
252         av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
253                    avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den,
254                   0x10000);
255     else
256         avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
257 
258     /* preferred frame rate from Motion-picture film header */
259     if (offset >= 1724 + 4) {
260         buf = avpkt->data + 1724;
261         i = read32(&buf, endian);
262         if(i && i != 0xFFFFFFFF) {
263             AVRational q = av_d2q(av_int2float(i), 4096);
264             if (q.num > 0 && q.den > 0)
265                 avctx->framerate = q;
266         }
267     }
268 
269     /* alternative frame rate from television header */
270     if (offset >= 1940 + 4 &&
271         !(avctx->framerate.num && avctx->framerate.den)) {
272         buf = avpkt->data + 1940;
273         i = read32(&buf, endian);
274         if(i && i != 0xFFFFFFFF) {
275             AVRational q = av_d2q(av_int2float(i), 4096);
276             if (q.num > 0 && q.den > 0)
277                 avctx->framerate = q;
278         }
279     }
280 
281     /* SMPTE TC from television header */
282     if (offset >= 1920 + 4) {
283         uint32_t tc;
284         uint32_t *tc_sd;
285         char tcbuf[AV_TIMECODE_STR_SIZE];
286 
287         buf = avpkt->data + 1920;
288         // read32 to native endian, av_bswap32 to opposite of native for
289         // compatibility with av_timecode_make_smpte_tc_string2 etc
290         tc = av_bswap32(read32(&buf, endian));
291 
292         if (i != 0xFFFFFFFF) {
293             AVFrameSideData *tcside =
294                 av_frame_new_side_data(p, AV_FRAME_DATA_S12M_TIMECODE,
295                                        sizeof(uint32_t) * 4);
296             if (!tcside)
297                 return AVERROR(ENOMEM);
298 
299             tc_sd = (uint32_t*)tcside->data;
300             tc_sd[0] = 1;
301             tc_sd[1] = tc;
302 
303             av_timecode_make_smpte_tc_string2(tcbuf, avctx->framerate,
304                                               tc_sd[1], 0, 0);
305             av_dict_set(&p->metadata, "timecode", tcbuf, 0);
306         }
307     }
308 
309     /* color range from television header */
310     if (offset >= 1964 + 4) {
311         buf = avpkt->data + 1952;
312         i = read32(&buf, endian);
313 
314         buf = avpkt->data + 1964;
315         j = read32(&buf, endian);
316 
317         if (i != 0xFFFFFFFF && j != 0xFFFFFFFF) {
318             float minCV, maxCV;
319             minCV = av_int2float(i);
320             maxCV = av_int2float(j);
321             if (bits_per_color >= 1 &&
322                 minCV == 0.0f && maxCV == ((1U<<bits_per_color) - 1)) {
323                 avctx->color_range = AVCOL_RANGE_JPEG;
324             } else if (bits_per_color >= 8 &&
325                        minCV == (1  <<(bits_per_color - 4)) &&
326                        maxCV == (235<<(bits_per_color - 8))) {
327                 avctx->color_range = AVCOL_RANGE_MPEG;
328             }
329         }
330     }
331 
332     switch (descriptor) {
333     case 1:  // R
334     case 2:  // G
335     case 3:  // B
336     case 4:  // A
337     case 6:  // Y
338         elements = 1;
339         yuv = 1;
340         break;
341     case 50: // RGB
342         elements = 3;
343         yuv = 0;
344         break;
345     case 52: // ABGR
346     case 51: // RGBA
347         elements = 4;
348         yuv = 0;
349         break;
350     case 100: // UYVY422
351         elements = 2;
352         yuv = 1;
353         break;
354     case 102: // UYV444
355         elements = 3;
356         yuv = 1;
357         break;
358     case 103: // UYVA4444
359         elements = 4;
360         yuv = 1;
361         break;
362     default:
363         avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
364         return AVERROR_PATCHWELCOME;
365     }
366 
367     switch (bits_per_color) {
368     case 8:
369         stride = avctx->width * elements;
370         break;
371     case 10:
372         if (!packing) {
373             av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
374             return -1;
375         }
376         stride = (avctx->width * elements + 2) / 3 * 4;
377         break;
378     case 12:
379         stride = avctx->width * elements;
380         if (packing) {
381             stride *= 2;
382         } else {
383             stride *= 3;
384             if (stride % 8) {
385                 stride /= 8;
386                 stride++;
387                 stride *= 8;
388             }
389             stride /= 2;
390         }
391         break;
392     case 16:
393         stride = 2 * avctx->width * elements;
394         break;
395     case 32:
396         stride = 4 * avctx->width * elements;
397         break;
398     case 1:
399     case 64:
400         avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
401         return AVERROR_PATCHWELCOME;
402     default:
403         return AVERROR_INVALIDDATA;
404     }
405 
406     switch (color_trc) {
407     case DPX_TRC_LINEAR:
408         avctx->color_trc = AVCOL_TRC_LINEAR;
409         break;
410     case DPX_TRC_SMPTE_274:
411     case DPX_TRC_ITU_R_709_4:
412         avctx->color_trc = AVCOL_TRC_BT709;
413         break;
414     case DPX_TRC_ITU_R_601_625:
415     case DPX_TRC_ITU_R_601_525:
416     case DPX_TRC_SMPTE_170:
417         avctx->color_trc = AVCOL_TRC_SMPTE170M;
418         break;
419     case DPX_TRC_ITU_R_624_4_PAL:
420         avctx->color_trc = AVCOL_TRC_GAMMA28;
421         break;
422     case DPX_TRC_USER_DEFINED:
423     case DPX_TRC_UNSPECIFIED_VIDEO:
424         /* Nothing to do */
425         break;
426     default:
427         av_log(avctx, AV_LOG_VERBOSE, "Cannot map DPX transfer characteristic "
428             "%d to color_trc.\n", color_trc);
429         break;
430     }
431 
432     switch (color_spec) {
433     case DPX_COL_SPEC_SMPTE_274:
434     case DPX_COL_SPEC_ITU_R_709_4:
435         avctx->color_primaries = AVCOL_PRI_BT709;
436         break;
437     case DPX_COL_SPEC_ITU_R_601_625:
438     case DPX_COL_SPEC_ITU_R_624_4_PAL:
439         avctx->color_primaries = AVCOL_PRI_BT470BG;
440         break;
441     case DPX_COL_SPEC_ITU_R_601_525:
442     case DPX_COL_SPEC_SMPTE_170:
443         avctx->color_primaries = AVCOL_PRI_SMPTE170M;
444         break;
445     case DPX_COL_SPEC_USER_DEFINED:
446     case DPX_COL_SPEC_UNSPECIFIED_VIDEO:
447         /* Nothing to do */
448         break;
449     default:
450         av_log(avctx, AV_LOG_VERBOSE, "Cannot map DPX color specification "
451             "%d to color_primaries.\n", color_spec);
452         break;
453     }
454 
455     if (yuv) {
456         switch (color_spec) {
457         case DPX_COL_SPEC_SMPTE_274:
458         case DPX_COL_SPEC_ITU_R_709_4:
459             avctx->colorspace = AVCOL_SPC_BT709;
460             break;
461         case DPX_COL_SPEC_ITU_R_601_625:
462         case DPX_COL_SPEC_ITU_R_624_4_PAL:
463             avctx->colorspace = AVCOL_SPC_BT470BG;
464             break;
465         case DPX_COL_SPEC_ITU_R_601_525:
466         case DPX_COL_SPEC_SMPTE_170:
467             avctx->colorspace = AVCOL_SPC_SMPTE170M;
468             break;
469         case DPX_COL_SPEC_USER_DEFINED:
470         case DPX_COL_SPEC_UNSPECIFIED_VIDEO:
471             /* Nothing to do */
472             break;
473         default:
474             av_log(avctx, AV_LOG_INFO, "Cannot map DPX color specification "
475                 "%d to colorspace.\n", color_spec);
476             break;
477         }
478     } else {
479         avctx->colorspace = AVCOL_SPC_RGB;
480     }
481 
482     // Table 3c: Runs will always break at scan line boundaries. Packing
483     // will always break to the next 32-bit word at scan-line boundaries.
484     // Unfortunately, the encoder produced invalid files, so attempt
485     // to detect it
486     need_align = FFALIGN(stride, 4);
487     if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
488         // Alignment seems unappliable, try without
489         if (stride*avctx->height + (int64_t)offset > avpkt->size) {
490             av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
491             return AVERROR_INVALIDDATA;
492         } else {
493             av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
494                    "alignment.\n");
495             need_align = 0;
496         }
497     } else {
498         need_align -= stride;
499         stride = FFALIGN(stride, 4);
500     }
501 
502     switch (1000 * descriptor + 10 * bits_per_color + endian) {
503     case 1081:
504     case 1080:
505     case 2081:
506     case 2080:
507     case 3081:
508     case 3080:
509     case 4081:
510     case 4080:
511     case 6081:
512     case 6080:
513         avctx->pix_fmt = AV_PIX_FMT_GRAY8;
514         break;
515     case 6121:
516     case 6120:
517         avctx->pix_fmt = AV_PIX_FMT_GRAY12;
518         break;
519     case 1320:
520     case 2320:
521     case 3320:
522     case 4320:
523     case 6320:
524         avctx->pix_fmt = AV_PIX_FMT_GRAYF32LE;
525         break;
526     case 1321:
527     case 2321:
528     case 3321:
529     case 4321:
530     case 6321:
531         avctx->pix_fmt = AV_PIX_FMT_GRAYF32BE;
532         break;
533     case 50081:
534     case 50080:
535         avctx->pix_fmt = AV_PIX_FMT_RGB24;
536         break;
537     case 52081:
538     case 52080:
539         avctx->pix_fmt = AV_PIX_FMT_ABGR;
540         break;
541     case 51081:
542     case 51080:
543         avctx->pix_fmt = AV_PIX_FMT_RGBA;
544         break;
545     case 50100:
546     case 50101:
547         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
548         break;
549     case 51100:
550     case 51101:
551         avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
552         break;
553     case 50120:
554     case 50121:
555         avctx->pix_fmt = AV_PIX_FMT_GBRP12;
556         break;
557     case 51120:
558     case 51121:
559         avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
560         break;
561     case 6100:
562     case 6101:
563         avctx->pix_fmt = AV_PIX_FMT_GRAY10;
564         break;
565     case 6161:
566         avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
567         break;
568     case 6160:
569         avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
570         break;
571     case 50161:
572         avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
573         break;
574     case 50160:
575         avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
576         break;
577     case 51161:
578         avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
579         break;
580     case 51160:
581         avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
582         break;
583     case 50320:
584         avctx->pix_fmt = AV_PIX_FMT_GBRPF32LE;
585         break;
586     case 50321:
587         avctx->pix_fmt = AV_PIX_FMT_GBRPF32BE;
588         break;
589     case 51320:
590         avctx->pix_fmt = AV_PIX_FMT_GBRAPF32LE;
591         break;
592     case 51321:
593         avctx->pix_fmt = AV_PIX_FMT_GBRAPF32BE;
594         break;
595     case 100081:
596         avctx->pix_fmt = AV_PIX_FMT_UYVY422;
597         break;
598     case 102081:
599         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
600         break;
601     case 103081:
602         avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
603         break;
604     default:
605         av_log(avctx, AV_LOG_ERROR, "Unsupported format %d\n",
606                1000 * descriptor + 10 * bits_per_color + endian);
607         return AVERROR_PATCHWELCOME;
608     }
609 
610     ff_set_sar(avctx, avctx->sample_aspect_ratio);
611 
612     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
613         return ret;
614 
615     av_strlcpy(creator, avpkt->data + 160, 100);
616     creator[100] = '\0';
617     av_dict_set(&p->metadata, "Creator", creator, 0);
618 
619     av_strlcpy(input_device, avpkt->data + 1556, 32);
620     input_device[32] = '\0';
621     av_dict_set(&p->metadata, "Input Device", input_device, 0);
622 
623     // Some devices do not pad 10bit samples to whole 32bit words per row
624     if (!memcmp(input_device, "Scanity", 7) ||
625         !memcmp(creator, "Lasergraphics Inc.", 18)) {
626         unpadded_10bit = 1;
627     }
628 
629     // Move pointer to offset from start of file
630     buf =  avpkt->data + offset;
631 
632     for (i=0; i<AV_NUM_DATA_POINTERS; i++)
633         ptr[i] = p->data[i];
634 
635     switch (bits_per_color) {
636     case 10:
637         for (x = 0; x < avctx->height; x++) {
638             uint16_t *dst[4] = {(uint16_t*)ptr[0],
639                                 (uint16_t*)ptr[1],
640                                 (uint16_t*)ptr[2],
641                                 (uint16_t*)ptr[3]};
642             int shift = elements > 1 ? packing == 1 ? 22 : 20 : packing == 1 ? 2 : 0;
643             for (y = 0; y < avctx->width; y++) {
644                 if (elements >= 3)
645                     *dst[2]++ = read10in32(&buf, &rgbBuffer,
646                                            &n_datum, endian, shift);
647                 if (elements == 1)
648                     *dst[0]++ = read10in32_gray(&buf, &rgbBuffer,
649                                                 &n_datum, endian, shift);
650                 else
651                     *dst[0]++ = read10in32(&buf, &rgbBuffer,
652                                            &n_datum, endian, shift);
653                 if (elements >= 2)
654                     *dst[1]++ = read10in32(&buf, &rgbBuffer,
655                                            &n_datum, endian, shift);
656                 if (elements == 4)
657                     *dst[3]++ =
658                     read10in32(&buf, &rgbBuffer,
659                                &n_datum, endian, shift);
660             }
661             if (!unpadded_10bit)
662                 n_datum = 0;
663             for (i = 0; i < elements; i++)
664                 ptr[i] += p->linesize[i];
665         }
666         break;
667     case 12:
668         for (x = 0; x < avctx->height; x++) {
669             uint16_t *dst[4] = {(uint16_t*)ptr[0],
670                                 (uint16_t*)ptr[1],
671                                 (uint16_t*)ptr[2],
672                                 (uint16_t*)ptr[3]};
673             int shift = packing == 1 ? 4 : 0;
674             for (y = 0; y < avctx->width; y++) {
675                 if (packing) {
676                     if (elements >= 3)
677                         *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
678                     *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
679                     if (elements >= 2)
680                         *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
681                     if (elements == 4)
682                         *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
683                 } else {
684                     if (elements >= 3)
685                         *dst[2]++ = read12in32(&buf, &rgbBuffer,
686                                                &n_datum, endian);
687                     *dst[0]++ = read12in32(&buf, &rgbBuffer,
688                                            &n_datum, endian);
689                     if (elements >= 2)
690                         *dst[1]++ = read12in32(&buf, &rgbBuffer,
691                                                &n_datum, endian);
692                     if (elements == 4)
693                         *dst[3]++ = read12in32(&buf, &rgbBuffer,
694                                                &n_datum, endian);
695                 }
696             }
697             n_datum = 0;
698             for (i = 0; i < elements; i++)
699                 ptr[i] += p->linesize[i];
700             // Jump to next aligned position
701             buf += need_align;
702         }
703         break;
704     case 32:
705         if (elements == 1) {
706             av_image_copy_plane(ptr[0], p->linesize[0],
707                                 buf, stride,
708                                 elements * avctx->width * 4, avctx->height);
709         } else {
710             for (y = 0; y < avctx->height; y++) {
711                 ptr[0] = p->data[0] + y * p->linesize[0];
712                 ptr[1] = p->data[1] + y * p->linesize[1];
713                 ptr[2] = p->data[2] + y * p->linesize[2];
714                 ptr[3] = p->data[3] + y * p->linesize[3];
715                 for (x = 0; x < avctx->width; x++) {
716                     AV_WN32(ptr[2], AV_RN32(buf));
717                     AV_WN32(ptr[0], AV_RN32(buf + 4));
718                     AV_WN32(ptr[1], AV_RN32(buf + 8));
719                     if (avctx->pix_fmt == AV_PIX_FMT_GBRAPF32BE ||
720                         avctx->pix_fmt == AV_PIX_FMT_GBRAPF32LE) {
721                         AV_WN32(ptr[3], AV_RN32(buf + 12));
722                         buf += 4;
723                         ptr[3] += 4;
724                     }
725 
726                     buf += 12;
727                     ptr[2] += 4;
728                     ptr[0] += 4;
729                     ptr[1] += 4;
730                 }
731             }
732         }
733         break;
734     case 16:
735         elements *= 2;
736     case 8:
737         if (   avctx->pix_fmt == AV_PIX_FMT_YUVA444P
738             || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
739             for (x = 0; x < avctx->height; x++) {
740                 ptr[0] = p->data[0] + x * p->linesize[0];
741                 ptr[1] = p->data[1] + x * p->linesize[1];
742                 ptr[2] = p->data[2] + x * p->linesize[2];
743                 ptr[3] = p->data[3] + x * p->linesize[3];
744                 for (y = 0; y < avctx->width; y++) {
745                     *ptr[1]++ = *buf++;
746                     *ptr[0]++ = *buf++;
747                     *ptr[2]++ = *buf++;
748                     if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
749                         *ptr[3]++ = *buf++;
750                 }
751             }
752         } else {
753         av_image_copy_plane(ptr[0], p->linesize[0],
754                             buf, stride,
755                             elements * avctx->width, avctx->height);
756         }
757         break;
758     }
759 
760     *got_frame = 1;
761 
762     return buf_size;
763 }
764 
765 AVCodec ff_dpx_decoder = {
766     .name           = "dpx",
767     .long_name      = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
768     .type           = AVMEDIA_TYPE_VIDEO,
769     .id             = AV_CODEC_ID_DPX,
770     .decode         = decode_frame,
771     .capabilities   = AV_CODEC_CAP_DR1,
772 };
773