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