FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dpx.c
Go to the documentation of this file.
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/intreadwrite.h"
23 #include "libavutil/intfloat.h"
24 #include "libavutil/imgutils.h"
25 #include "bytestream.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 
29 static unsigned int read16(const uint8_t **ptr, int is_big)
30 {
31  unsigned int temp;
32  if (is_big) {
33  temp = AV_RB16(*ptr);
34  } else {
35  temp = AV_RL16(*ptr);
36  }
37  *ptr += 2;
38  return temp;
39 }
40 
41 static unsigned int read32(const uint8_t **ptr, int is_big)
42 {
43  unsigned int temp;
44  if (is_big) {
45  temp = AV_RB32(*ptr);
46  } else {
47  temp = AV_RL32(*ptr);
48  }
49  *ptr += 4;
50  return temp;
51 }
52 
53 static uint16_t read10in32(const uint8_t **ptr, uint32_t * lbuf,
54  int * n_datum, int is_big, int shift)
55 {
56  if (*n_datum)
57  (*n_datum)--;
58  else {
59  *lbuf = read32(ptr, is_big);
60  *n_datum = 2;
61  }
62 
63  *lbuf = *lbuf << 10 | *lbuf >> shift & 0x3FFFFF;
64 
65  return *lbuf & 0x3FF;
66 }
67 
68 static uint16_t read12in32(const uint8_t **ptr, uint32_t * lbuf,
69  int * n_datum, int is_big)
70 {
71  if (*n_datum)
72  (*n_datum)--;
73  else {
74  *lbuf = read32(ptr, is_big);
75  *n_datum = 7;
76  }
77 
78  switch (*n_datum){
79  case 7: return *lbuf & 0xFFF;
80  case 6: return (*lbuf >> 12) & 0xFFF;
81  case 5: {
82  uint32_t c = *lbuf >> 24;
83  *lbuf = read32(ptr, is_big);
84  c |= *lbuf << 8;
85  return c & 0xFFF;
86  }
87  case 4: return (*lbuf >> 4) & 0xFFF;
88  case 3: return (*lbuf >> 16) & 0xFFF;
89  case 2: {
90  uint32_t c = *lbuf >> 28;
91  *lbuf = read32(ptr, is_big);
92  c |= *lbuf << 4;
93  return c & 0xFFF;
94  }
95  case 1: return (*lbuf >> 8) & 0xFFF;
96  default: return *lbuf >> 20;
97  }
98 }
99 
100 static int decode_frame(AVCodecContext *avctx,
101  void *data,
102  int *got_frame,
103  AVPacket *avpkt)
104 {
105  const uint8_t *buf = avpkt->data;
106  int buf_size = avpkt->size;
107  AVFrame *const p = data;
109 
110  unsigned int offset;
111  int magic_num, endian;
112  int x, y, stride, i, ret;
113  int w, h, bits_per_color, descriptor, elements, packing;
114  int encoding, need_align = 0;
115 
116  unsigned int rgbBuffer = 0;
117  int n_datum = 0;
118 
119  if (avpkt->size <= 1634) {
120  av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
121  return AVERROR_INVALIDDATA;
122  }
123 
124  magic_num = AV_RB32(buf);
125  buf += 4;
126 
127  /* Check if the files "magic number" is "SDPX" which means it uses
128  * big-endian or XPDS which is for little-endian files */
129  if (magic_num == AV_RL32("SDPX")) {
130  endian = 0;
131  } else if (magic_num == AV_RB32("SDPX")) {
132  endian = 1;
133  } else {
134  av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
135  return AVERROR_INVALIDDATA;
136  }
137 
138  offset = read32(&buf, endian);
139  if (avpkt->size <= offset) {
140  av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  // Check encryption
145  buf = avpkt->data + 660;
146  ret = read32(&buf, endian);
147  if (ret != 0xFFFFFFFF) {
148  avpriv_report_missing_feature(avctx, "Encryption");
149  av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
150  "not properly decode.\n");
151  }
152 
153  // Need to end in 0x304 offset from start of file
154  buf = avpkt->data + 0x304;
155  w = read32(&buf, endian);
156  h = read32(&buf, endian);
157 
158  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
159  return ret;
160 
161  // Need to end in 0x320 to read the descriptor
162  buf += 20;
163  descriptor = buf[0];
164 
165  // Need to end in 0x323 to read the bits per color
166  buf += 3;
167  avctx->bits_per_raw_sample =
168  bits_per_color = buf[0];
169  buf++;
170  packing = read16(&buf, endian);
171  encoding = read16(&buf, endian);
172 
173  if (encoding) {
174  avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
175  return AVERROR_PATCHWELCOME;
176  }
177 
178  buf += 820;
179  avctx->sample_aspect_ratio.num = read32(&buf, endian);
180  avctx->sample_aspect_ratio.den = read32(&buf, endian);
181  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
184  0x10000);
185  else
186  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
187 
188  if (offset >= 1724 + 4) {
189  buf = avpkt->data + 1724;
190  i = read32(&buf, endian);
191  if(i) {
192  AVRational q = av_d2q(av_int2float(i), 4096);
193  if (q.num > 0 && q.den > 0)
194  avctx->framerate = q;
195  }
196  }
197 
198  switch (descriptor) {
199  case 6: // Y
200  elements = 1;
201  break;
202  case 52: // ABGR
203  case 51: // RGBA
204  case 103: // UYVA4444
205  elements = 4;
206  break;
207  case 50: // RGB
208  case 102: // UYV444
209  elements = 3;
210  break;
211  case 100: // UYVY422
212  elements = 2;
213  break;
214  default:
215  avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
216  return AVERROR_PATCHWELCOME;
217  }
218 
219  switch (bits_per_color) {
220  case 8:
221  stride = avctx->width * elements;
222  break;
223  case 10:
224  if (!packing) {
225  av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
226  return -1;
227  }
228  stride = (avctx->width * elements + 2) / 3 * 4;
229  break;
230  case 12:
231  stride = avctx->width * elements;
232  if (packing) {
233  stride *= 2;
234  } else {
235  stride *= 3;
236  if (stride % 8) {
237  stride /= 8;
238  stride++;
239  stride *= 8;
240  }
241  stride /= 2;
242  }
243  break;
244  case 16:
245  stride = 2 * avctx->width * elements;
246  break;
247  case 1:
248  case 32:
249  case 64:
250  avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
251  return AVERROR_PATCHWELCOME;
252  default:
253  return AVERROR_INVALIDDATA;
254  }
255 
256  // Table 3c: Runs will always break at scan line boundaries. Packing
257  // will always break to the next 32-bit word at scan-line boundaries.
258  // Unfortunately, the encoder produced invalid files, so attempt
259  // to detect it
260  need_align = FFALIGN(stride, 4);
261  if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
262  // Alignment seems unappliable, try without
263  if (stride*avctx->height + (int64_t)offset > avpkt->size) {
264  av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
265  return AVERROR_INVALIDDATA;
266  } else {
267  av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
268  "alignment.\n");
269  need_align = 0;
270  }
271  } else {
272  need_align -= stride;
273  stride = FFALIGN(stride, 4);
274  }
275 
276  switch (1000 * descriptor + 10 * bits_per_color + endian) {
277  case 6081:
278  case 6080:
279  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
280  break;
281  case 6121:
282  case 6120:
283  avctx->pix_fmt = AV_PIX_FMT_GRAY12;
284  break;
285  case 50081:
286  case 50080:
287  avctx->pix_fmt = AV_PIX_FMT_RGB24;
288  break;
289  case 52081:
290  case 52080:
291  avctx->pix_fmt = AV_PIX_FMT_ABGR;
292  break;
293  case 51081:
294  case 51080:
295  avctx->pix_fmt = AV_PIX_FMT_RGBA;
296  break;
297  case 50100:
298  case 50101:
299  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
300  break;
301  case 51100:
302  case 51101:
303  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
304  break;
305  case 50120:
306  case 50121:
307  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
308  break;
309  case 51120:
310  case 51121:
311  avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
312  break;
313  case 6161:
314  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
315  break;
316  case 6160:
317  avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
318  break;
319  case 50161:
320  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
321  break;
322  case 50160:
323  avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
324  break;
325  case 51161:
326  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
327  break;
328  case 51160:
329  avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
330  break;
331  case 100081:
332  avctx->pix_fmt = AV_PIX_FMT_UYVY422;
333  break;
334  case 102081:
335  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
336  break;
337  case 103081:
338  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
339  break;
340  default:
341  av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
342  return AVERROR_PATCHWELCOME;
343  }
344 
345  ff_set_sar(avctx, avctx->sample_aspect_ratio);
346 
347  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
348  return ret;
349 
350  // Move pointer to offset from start of file
351  buf = avpkt->data + offset;
352 
353  for (i=0; i<AV_NUM_DATA_POINTERS; i++)
354  ptr[i] = p->data[i];
355 
356  switch (bits_per_color) {
357  case 10:
358  for (x = 0; x < avctx->height; x++) {
359  uint16_t *dst[4] = {(uint16_t*)ptr[0],
360  (uint16_t*)ptr[1],
361  (uint16_t*)ptr[2],
362  (uint16_t*)ptr[3]};
363  int shift = packing == 1 ? 22 : 20;
364  for (y = 0; y < avctx->width; y++) {
365  *dst[2]++ = read10in32(&buf, &rgbBuffer,
366  &n_datum, endian, shift);
367  *dst[0]++ = read10in32(&buf, &rgbBuffer,
368  &n_datum, endian, shift);
369  *dst[1]++ = read10in32(&buf, &rgbBuffer,
370  &n_datum, endian, shift);
371  if (elements == 4)
372  *dst[3]++ =
373  read10in32(&buf, &rgbBuffer,
374  &n_datum, endian, shift);
375  }
376  n_datum = 0;
377  for (i = 0; i < elements; i++)
378  ptr[i] += p->linesize[i];
379  }
380  break;
381  case 12:
382  for (x = 0; x < avctx->height; x++) {
383  uint16_t *dst[4] = {(uint16_t*)ptr[0],
384  (uint16_t*)ptr[1],
385  (uint16_t*)ptr[2],
386  (uint16_t*)ptr[3]};
387  int shift = packing == 1 ? 4 : 0;
388  for (y = 0; y < avctx->width; y++) {
389  if (packing) {
390  if (elements >= 3)
391  *dst[2]++ = read16(&buf, endian) >> shift & 0xFFF;
392  *dst[0]++ = read16(&buf, endian) >> shift & 0xFFF;
393  if (elements >= 2)
394  *dst[1]++ = read16(&buf, endian) >> shift & 0xFFF;
395  if (elements == 4)
396  *dst[3]++ = read16(&buf, endian) >> shift & 0xFFF;
397  } else {
398  if (elements >= 3)
399  *dst[2]++ = read12in32(&buf, &rgbBuffer,
400  &n_datum, endian);
401  *dst[0]++ = read12in32(&buf, &rgbBuffer,
402  &n_datum, endian);
403  if (elements >= 2)
404  *dst[1]++ = read12in32(&buf, &rgbBuffer,
405  &n_datum, endian);
406  if (elements == 4)
407  *dst[3]++ = read12in32(&buf, &rgbBuffer,
408  &n_datum, endian);
409  }
410  }
411  n_datum = 0;
412  for (i = 0; i < elements; i++)
413  ptr[i] += p->linesize[i];
414  // Jump to next aligned position
415  buf += need_align;
416  }
417  break;
418  case 16:
419  elements *= 2;
420  case 8:
421  if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
422  || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
423  for (x = 0; x < avctx->height; x++) {
424  ptr[0] = p->data[0] + x * p->linesize[0];
425  ptr[1] = p->data[1] + x * p->linesize[1];
426  ptr[2] = p->data[2] + x * p->linesize[2];
427  ptr[3] = p->data[3] + x * p->linesize[3];
428  for (y = 0; y < avctx->width; y++) {
429  *ptr[1]++ = *buf++;
430  *ptr[0]++ = *buf++;
431  *ptr[2]++ = *buf++;
432  if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
433  *ptr[3]++ = *buf++;
434  }
435  }
436  } else {
437  av_image_copy_plane(ptr[0], p->linesize[0],
438  buf, stride,
439  elements * avctx->width, avctx->height);
440  }
441  break;
442  }
443 
444  *got_frame = 1;
445 
446  return buf_size;
447 }
448 
450  .name = "dpx",
451  .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
452  .type = AVMEDIA_TYPE_VIDEO,
453  .id = AV_CODEC_ID_DPX,
454  .decode = decode_frame,
455  .capabilities = AV_CODEC_CAP_DR1,
456 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dpx.c:100
AVRational framerate
Definition: avcodec.h:3056
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_NUM_DATA_POINTERS
Definition: frame.h:227
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:399
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
else temp
Definition: vf_mcdeint.c:256
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1446
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:395
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1912
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2757
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
AVCodec.
Definition: avcodec.h:3424
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:361
uint8_t
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:103
AVCodec ff_dpx_decoder
Definition: dpx.c:449
static unsigned int read16(const uint8_t **ptr, int is_big)
Definition: dpx.c:29
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:205
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf, int *n_datum, int is_big, int shift)
Definition: dpx.c:53
uint8_t * data
Definition: avcodec.h:1445
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:119
static unsigned int read32(const uint8_t **ptr, int is_big)
Definition: dpx.c:41
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:400
const char * name
Name of the codec implementation.
Definition: avcodec.h:3431
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
static uint16_t read12in32(const uint8_t **ptr, uint32_t *lbuf, int *n_datum, int is_big)
Definition: dpx.c:68
int width
picture width / height.
Definition: avcodec.h:1706
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:566
uint8_t w
Definition: llviddspenc.c:38
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
main external API structure.
Definition: avcodec.h:1533
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1918
void * buf
Definition: avisynth_c.h:690
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:396
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
Y , 8bpp.
Definition: pixfmt.h:74
common internal api header.
static double c[64]
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:102
int den
Denominator.
Definition: rational.h:60
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:338
#define stride
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:968
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:206