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->time_base = av_inv_q(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  elements = 4;
177  break;
178  case 50: // RGB
179  elements = 3;
180  break;
181  default:
182  avpriv_report_missing_feature(avctx, "Descriptor %d", descriptor);
183  return AVERROR_PATCHWELCOME;
184  }
185 
186  switch (bits_per_color) {
187  case 8:
188  stride = avctx->width * elements;
189  break;
190  case 10:
191  if (!packing) {
192  av_log(avctx, AV_LOG_ERROR, "Packing to 32bit required\n");
193  return -1;
194  }
195  stride = (avctx->width * elements + 2) / 3 * 4;
196  break;
197  case 12:
198  if (!packing) {
199  av_log(avctx, AV_LOG_ERROR, "Packing to 16bit required\n");
200  return -1;
201  }
202  stride = 2 * avctx->width * elements;
203  break;
204  case 16:
205  stride = 2 * avctx->width * elements;
206  break;
207  case 1:
208  case 32:
209  case 64:
210  avpriv_report_missing_feature(avctx, "Depth %d", bits_per_color);
211  return AVERROR_PATCHWELCOME;
212  default:
213  return AVERROR_INVALIDDATA;
214  }
215 
216  // Table 3c: Runs will always break at scan line boundaries. Packing
217  // will always break to the next 32-bit word at scan-line boundaries.
218  // Unfortunately, the encoder produced invalid files, so attempt
219  // to detect it
220  need_align = FFALIGN(stride, 4);
221  if (need_align*avctx->height + (int64_t)offset > avpkt->size) {
222  // Alignment seems unappliable, try without
223  if (stride*avctx->height + (int64_t)offset > avpkt->size) {
224  av_log(avctx, AV_LOG_ERROR, "Overread buffer. Invalid header?\n");
225  return AVERROR_INVALIDDATA;
226  } else {
227  av_log(avctx, AV_LOG_INFO, "Decoding DPX without scanline "
228  "alignment.\n");
229  need_align = 0;
230  }
231  } else {
232  need_align -= stride;
233  stride = FFALIGN(stride, 4);
234  }
235 
236  switch (1000 * descriptor + 10 * bits_per_color + endian) {
237  case 6081:
238  case 6080:
239  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
240  break;
241  case 50081:
242  case 50080:
243  avctx->pix_fmt = AV_PIX_FMT_RGB24;
244  break;
245  case 52081:
246  case 52080:
247  avctx->pix_fmt = AV_PIX_FMT_ABGR;
248  break;
249  case 51081:
250  case 51080:
251  avctx->pix_fmt = AV_PIX_FMT_RGBA;
252  break;
253  case 50100:
254  case 51100:
255  case 50101:
256  case 51101:
257  avctx->pix_fmt = AV_PIX_FMT_GBRP10;
258  break;
259  case 50120:
260  case 51120:
261  case 50121:
262  case 51121:
263  avctx->pix_fmt = AV_PIX_FMT_GBRP12;
264  break;
265  case 6161:
266  avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
267  break;
268  case 6160:
269  avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
270  break;
271  case 50161:
272  avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
273  break;
274  case 50160:
275  avctx->pix_fmt = AV_PIX_FMT_RGB48LE;
276  break;
277  case 51161:
278  avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
279  break;
280  case 51160:
281  avctx->pix_fmt = AV_PIX_FMT_RGBA64LE;
282  break;
283  default:
284  av_log(avctx, AV_LOG_ERROR, "Unsupported format\n");
285  return AVERROR_PATCHWELCOME;
286  }
287 
288  ff_set_sar(avctx, avctx->sample_aspect_ratio);
289 
290  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
291  return ret;
292 
293  // Move pointer to offset from start of file
294  buf = avpkt->data + offset;
295 
296  for (i=0; i<AV_NUM_DATA_POINTERS; i++)
297  ptr[i] = p->data[i];
298 
299  switch (bits_per_color) {
300  case 10:
301  for (x = 0; x < avctx->height; x++) {
302  uint16_t *dst[3] = {(uint16_t*)ptr[0],
303  (uint16_t*)ptr[1],
304  (uint16_t*)ptr[2]};
305  for (y = 0; y < avctx->width; y++) {
306  *dst[2]++ = read10in32(&buf, &rgbBuffer,
307  &n_datum, endian);
308  *dst[0]++ = read10in32(&buf, &rgbBuffer,
309  &n_datum, endian);
310  *dst[1]++ = read10in32(&buf, &rgbBuffer,
311  &n_datum, endian);
312  // For 10 bit, ignore alpha
313  if (elements == 4)
314  read10in32(&buf, &rgbBuffer,
315  &n_datum, endian);
316  }
317  n_datum = 0;
318  for (i = 0; i < 3; i++)
319  ptr[i] += p->linesize[i];
320  }
321  break;
322  case 12:
323  for (x = 0; x < avctx->height; x++) {
324  uint16_t *dst[3] = {(uint16_t*)ptr[0],
325  (uint16_t*)ptr[1],
326  (uint16_t*)ptr[2]};
327  for (y = 0; y < avctx->width; y++) {
328  *dst[2] = read16(&buf, endian) >> 4;
329  dst[2]++;
330  *dst[0] = read16(&buf, endian) >> 4;
331  dst[0]++;
332  *dst[1] = read16(&buf, endian) >> 4;
333  dst[1]++;
334  // For 12 bit, ignore alpha
335  if (elements == 4)
336  buf += 2;
337  // Jump to next aligned position
338  buf += need_align;
339  }
340  for (i = 0; i < 3; i++)
341  ptr[i] += p->linesize[i];
342  }
343  break;
344  case 16:
345  elements *= 2;
346  case 8:
347  av_image_copy_plane(ptr[0], p->linesize[0],
348  buf, stride,
349  elements * avctx->width, avctx->height);
350  break;
351  }
352 
353  *got_frame = 1;
354 
355  return buf_size;
356 }
357 
359  .name = "dpx",
360  .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
361  .type = AVMEDIA_TYPE_VIDEO,
362  .id = AV_CODEC_ID_DPX,
363  .decode = decode_frame,
364  .capabilities = CODEC_CAP_DR1,
365 };