FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dpx.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 238 466 51.1%
Branches: 107 238 45.0%

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