FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dpx.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 263 513 51.3%
Functions: 8 11 72.7%
Branches: 112 262 42.7%

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