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)
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 >> 22);
64 
65  return *lbuf & 0x3FF;
66 }
67 
68 static int decode_frame(AVCodecContext *avctx,
69  void *data,
70  int *got_frame,
71  AVPacket *avpkt)
72 {
73  const uint8_t *buf = avpkt->data;
74  int buf_size = avpkt->size;
75  AVFrame *const p = data;
77 
78  unsigned int offset;
79  int magic_num, endian;
80  int x, y, stride, i, ret;
81  int w, h, bits_per_color, descriptor, elements, packing;
82  int encoding, need_align = 0;
83 
84  unsigned int rgbBuffer = 0;
85  int n_datum = 0;
86 
87  if (avpkt->size <= 1634) {
88  av_log(avctx, AV_LOG_ERROR, "Packet too small for DPX header\n");
89  return AVERROR_INVALIDDATA;
90  }
91 
92  magic_num = AV_RB32(buf);
93  buf += 4;
94 
95  /* Check if the files "magic number" is "SDPX" which means it uses
96  * big-endian or XPDS which is for little-endian files */
97  if (magic_num == AV_RL32("SDPX")) {
98  endian = 0;
99  } else if (magic_num == AV_RB32("SDPX")) {
100  endian = 1;
101  } else {
102  av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");
103  return AVERROR_INVALIDDATA;
104  }
105 
106  offset = read32(&buf, endian);
107  if (avpkt->size <= offset) {
108  av_log(avctx, AV_LOG_ERROR, "Invalid data start offset\n");
109  return AVERROR_INVALIDDATA;
110  }
111 
112  // Check encryption
113  buf = avpkt->data + 660;
114  ret = read32(&buf, endian);
115  if (ret != 0xFFFFFFFF) {
116  avpriv_report_missing_feature(avctx, "Encryption");
117  av_log(avctx, AV_LOG_WARNING, "The image is encrypted and may "
118  "not properly decode.\n");
119  }
120 
121  // Need to end in 0x304 offset from start of file
122  buf = avpkt->data + 0x304;
123  w = read32(&buf, endian);
124  h = read32(&buf, endian);
125 
126  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
127  return ret;
128 
129  // Need to end in 0x320 to read the descriptor
130  buf += 20;
131  descriptor = buf[0];
132 
133  // Need to end in 0x323 to read the bits per color
134  buf += 3;
135  avctx->bits_per_raw_sample =
136  bits_per_color = buf[0];
137  buf++;
138  packing = read16(&buf, endian);
139  encoding = read16(&buf, endian);
140 
141  if (packing > 1) {
142  avpriv_report_missing_feature(avctx, "Packing %d", packing);
143  return AVERROR_PATCHWELCOME;
144  }
145  if (encoding) {
146  avpriv_report_missing_feature(avctx, "Encoding %d", encoding);
147  return AVERROR_PATCHWELCOME;
148  }
149 
150  buf += 820;
151  avctx->sample_aspect_ratio.num = read32(&buf, endian);
152  avctx->sample_aspect_ratio.den = read32(&buf, endian);
153  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
156  0x10000);
157  else
158  avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
159 
160  if (offset >= 1724 + 4) {
161  buf = avpkt->data + 1724;
162  i = read32(&buf, endian);
163  if(i) {
164  AVRational q = av_d2q(av_int2float(i), 4096);
165  if (q.num > 0 && q.den > 0)
166  avctx->framerate = q;
167  }
168  }
169 
170  switch (descriptor) {
171  case 6: // Y
172  elements = 1;
173  break;
174  case 52: // ABGR
175  case 51: // RGBA
176  case 103: // UYVA4444
177  elements = 4;
178  break;
179  case 50: // RGB
180  case 102: // UYV444
181  elements = 3;
182  break;
183  case 100: // UYVY422
184  elements = 2;
185  break;
186  default:
187  avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
188  return AVERROR_PATCHWELCOME;
189  }
190 
191  switch (bits_per_color) {
192  case 8:
193  stride = avctx->width * elements;
194  break;
195  case 10:
196  if (!packing) {
197  av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
198  return -1;
199  }
200  stride = (avctx->width * elements + 2) / 3 * 4;
201  break;
202  case 12:
203  if (!packing) {
204  av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
205  return -1;
206  }
207  stride = 2 * avctx->width * elements;
208  break;
209  case 16:
210  stride = 2 * avctx->width * elements;
211  break;
212  case 1:
213  case 32:
214  case 64:
215  avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
216  return AVERROR_PATCHWELCOME;
217  default:
218  return AVERROR_INVALIDDATA;
219  }
220 
221  // Table 3c: Runs will always break at scan line boundaries. Packing
222  // will always break to the next 32-bit word at scan-line boundaries.
223  // Unfortunately, the encoder produced invalid files, so attempt
224  // to detect it
225  need_align = FFALIGN(stride, 4);
226  if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
227  // Alignment seems unappliable, try without
228  if (stride*avctx->height + (int64_t)offset > avpkt->size) {
229  av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
230  return AVERROR_INVALIDDATA;
231  } else {
232  av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
233  "alignment.\n");
234  need_align = 0;
235  }
236  } else {
237  need_align -= stride;
238  stride = FFALIGN(stride, 4);
239  }
240 
241  switch (1000 * descriptor + 10 * bits_per_color + endian) {
242  case 6081:
243  case 6080:
244  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
245  break;
246  case 50081:
247  case 50080:
248  avctx->pix_fmt = AV_PIX_FMT_RGB24;
249  break;
250  case 52081:
251  case 52080:
252  avctx->pix_fmt = AV_PIX_FMT_ABGR;
253  break;
254  case 51081:
255  case 51080:
256  avctx->pix_fmt = AV_PIX_FMT_RGBA;
257  break;
258  case 50100:
259  case 50101:
260  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
261  break;
262  case 51100:
263  case 51101:
264  avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
265  break;
266  case 50120:
267  case 50121:
268  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
269  break;
270  case 51120:
271  case 51121:
272  avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
273  break;
274  case 6161:
275  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
276  break;
277  case 6160:
278  avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
279  break;
280  case 50161:
281  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
282  break;
283  case 50160:
284  avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
285  break;
286  case 51161:
287  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
288  break;
289  case 51160:
290  avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
291  break;
292  case 100081:
293  avctx->pix_fmt = AV_PIX_FMT_UYVY422;
294  break;
295  case 102081:
296  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
297  break;
298  case 103081:
299  avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
300  break;
301  default:
302  av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
303  return AVERROR_PATCHWELCOME;
304  }
305 
306  ff_set_sar(avctx, avctx->sample_aspect_ratio);
307 
308  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
309  return ret;
310 
311  // Move pointer to offset from start of file
312  buf = avpkt->data + offset;
313 
314  for (i=0; i<AV_NUM_DATA_POINTERS; i++)
315  ptr[i] = p->data[i];
316 
317  switch (bits_per_color) {
318  case 10:
319  for (x = 0; x < avctx->height; x++) {
320  uint16_t *dst[4] = {(uint16_t*)ptr[0],
321  (uint16_t*)ptr[1],
322  (uint16_t*)ptr[2],
323  (uint16_t*)ptr[3]};
324  for (y = 0; y < avctx->width; y++) {
325  *dst[2]++ = read10in32(&buf, &rgbBuffer,
326  &n_datum, endian);
327  *dst[0]++ = read10in32(&buf, &rgbBuffer,
328  &n_datum, endian);
329  *dst[1]++ = read10in32(&buf, &rgbBuffer,
330  &n_datum, endian);
331  if (elements == 4)
332  *dst[3]++ =
333  read10in32(&buf, &rgbBuffer,
334  &n_datum, endian);
335  }
336  n_datum = 0;
337  for (i = 0; i < elements; i++)
338  ptr[i] += p->linesize[i];
339  }
340  break;
341  case 12:
342  for (x = 0; x < avctx->height; x++) {
343  uint16_t *dst[4] = {(uint16_t*)ptr[0],
344  (uint16_t*)ptr[1],
345  (uint16_t*)ptr[2],
346  (uint16_t*)ptr[3]};
347  for (y = 0; y < avctx->width; y++) {
348  *dst[2] = read16(&buf, endian) >> 4;
349  dst[2]++;
350  *dst[0] = read16(&buf, endian) >> 4;
351  dst[0]++;
352  *dst[1] = read16(&buf, endian) >> 4;
353  dst[1]++;
354  if (elements == 4)
355  *dst[3]++ = read16(&buf, endian) >> 4;
356  }
357  for (i = 0; i < elements; i++)
358  ptr[i] += p->linesize[i];
359  // Jump to next aligned position
360  buf += need_align;
361  }
362  break;
363  case 16:
364  elements *= 2;
365  case 8:
366  if ( avctx->pix_fmt == AV_PIX_FMT_YUVA444P
367  || avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
368  for (x = 0; x < avctx->height; x++) {
369  ptr[0] = p->data[0] + x * p->linesize[0];
370  ptr[1] = p->data[1] + x * p->linesize[1];
371  ptr[2] = p->data[2] + x * p->linesize[2];
372  ptr[3] = p->data[3] + x * p->linesize[3];
373  for (y = 0; y < avctx->width; y++) {
374  *ptr[1]++ = *buf++;
375  *ptr[0]++ = *buf++;
376  *ptr[2]++ = *buf++;
377  if (avctx->pix_fmt == AV_PIX_FMT_YUVA444P)
378  *ptr[3]++ = *buf++;
379  }
380  }
381  } else {
382  av_image_copy_plane(ptr[0], p->linesize[0],
383  buf, stride,
384  elements * avctx->width, avctx->height);
385  }
386  break;
387  }
388 
389  *got_frame = 1;
390 
391  return buf_size;
392 }
393 
395  .name = "dpx",
396  .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
397  .type = AVMEDIA_TYPE_VIDEO,
398  .id = AV_CODEC_ID_DPX,
399  .decode = decode_frame,
400  .capabilities = AV_CODEC_CAP_DR1,
401 };
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:82
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: dpx.c:68
AVRational framerate
Definition: avcodec.h:3375
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_NUM_DATA_POINTERS
Definition: frame.h:185
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:361
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
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:64
else temp
Definition: vf_mcdeint.c:259
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:210
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:1602
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:357
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:2087
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3077
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:3600
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
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:111
AVCodec ff_dpx_decoder
Definition: dpx.c:394
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:217
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:95
uint8_t * data
Definition: avcodec.h:1601
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:225
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:176
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:362
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int width
picture width / height.
Definition: avcodec.h:1863
#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:215
static uint16_t read10in32(const uint8_t **ptr, uint32_t *lbuf, int *n_datum, int is_big)
Definition: dpx.c:53
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:189
main external API structure.
Definition: avcodec.h:1676
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
void * buf
Definition: avisynth_c.h:690
Y , 16bpp, big-endian.
Definition: pixfmt.h:98
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:358
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:198
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:70
common internal api header.
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:110
int den
Denominator.
Definition: rational.h:60
Y , 16bpp, little-endian.
Definition: pixfmt.h:99
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:287
#define stride
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1578
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
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:218