FFmpeg coverage


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