FFmpeg  1.2.12
dnxhddec.c
Go to the documentation of this file.
1 /*
2  * VC3/DNxHD decoder.
3  * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4  * Copyright (c) 2011 MirriAd Ltd
5  *
6  * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 //#define TRACE
26 //#define DEBUG
27 
28 #include "libavutil/imgutils.h"
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "dnxhddata.h"
32 #include "dsputil.h"
33 #include "internal.h"
34 #include "thread.h"
35 
36 typedef struct DNXHDContext {
40  int64_t cid;
41  unsigned int width, height;
43  unsigned int mb_width, mb_height;
44  uint32_t mb_scan_index[68]; /* max for 1080p */
45  int cur_field;
47  int last_dc[3];
49  DECLARE_ALIGNED(16, int16_t, blocks)[8][64];
52  int bit_depth; // 8, 10 or 0 if not initialized at all.
53  void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
54  int n, int qscale);
56  int luma_scale[64];
57  int chroma_scale[64];
58 } DNXHDContext;
59 
60 #define DNXHD_VLC_BITS 9
61 #define DNXHD_DC_VLC_BITS 7
62 
63 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block, int n, int qscale);
64 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block, int n, int qscale);
65 
67 {
68  DNXHDContext *ctx = avctx->priv_data;
69 
70  ctx->avctx = avctx;
71  avctx->coded_frame = &ctx->picture;
74  ctx->picture.key_frame = 1;
75  ctx->cid = -1;
76  return 0;
77 }
78 
79 static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid)
80 {
81  if (cid != ctx->cid) {
82  int index;
83 
84  if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
85  av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
86  return -1;
87  }
88  if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth) {
89  av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
90  return AVERROR_INVALIDDATA;
91  }
93 
94  ff_free_vlc(&ctx->ac_vlc);
95  ff_free_vlc(&ctx->dc_vlc);
96  ff_free_vlc(&ctx->run_vlc);
97 
98  init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
99  ctx->cid_table->ac_bits, 1, 1,
100  ctx->cid_table->ac_codes, 2, 2, 0);
101  init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
102  ctx->cid_table->dc_bits, 1, 1,
103  ctx->cid_table->dc_codes, 1, 1, 0);
104  init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
105  ctx->cid_table->run_bits, 1, 1,
106  ctx->cid_table->run_codes, 2, 2, 0);
107 
109  ctx->cid = cid;
110  }
111  return 0;
112 }
113 
114 static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
115 {
116  static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
117  int i, cid;
118 
119  if (buf_size < 0x280)
120  return -1;
121 
122  if (memcmp(buf, header_prefix, 5)) {
123  av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
124  return -1;
125  }
126  if (buf[5] & 2) { /* interlaced */
127  ctx->cur_field = buf[5] & 1;
128  ctx->picture.interlaced_frame = 1;
129  ctx->picture.top_field_first = first_field ^ ctx->cur_field;
130  av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
131  }
132 
133  ctx->height = AV_RB16(buf + 0x18);
134  ctx->width = AV_RB16(buf + 0x1a);
135 
136  av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
137 
138  if (buf[0x21] & 0x40) {
140  ctx->avctx->bits_per_raw_sample = 10;
141  if (ctx->bit_depth != 10) {
142  ff_dsputil_init(&ctx->dsp, ctx->avctx);
143  ctx->bit_depth = 10;
145  }
146  } else {
148  ctx->avctx->bits_per_raw_sample = 8;
149  if (ctx->bit_depth != 8) {
150  ff_dsputil_init(&ctx->dsp, ctx->avctx);
151  ctx->bit_depth = 8;
153  }
154  }
155 
156  cid = AV_RB32(buf + 0x28);
157  av_dlog(ctx->avctx, "compression id %d\n", cid);
158 
159  if (dnxhd_init_vlc(ctx, cid) < 0)
160  return -1;
161 
162  if (buf_size < ctx->cid_table->coding_unit_size) {
163  av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
164  return -1;
165  }
166 
167  ctx->mb_width = ctx->width>>4;
168  ctx->mb_height = buf[0x16d];
169 
170  av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
171 
172  if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
173  ctx->height <<= 1;
174 
175  if (ctx->mb_height > 68 ||
176  (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
177  av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
178  return -1;
179  }
180 
181  for (i = 0; i < ctx->mb_height; i++) {
182  ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
183  av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
184  if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {
185  av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
186  return -1;
187  }
188  }
189 
190  return 0;
191 }
192 
194  int16_t *block, int n,
195  int qscale,
196  int index_bits,
197  int level_bias,
198  int level_shift)
199 {
200  int i, j, index1, index2, len, flags;
201  int level, component, sign;
202  const int *scale;
203  const uint8_t *weight_matrix;
204  const uint8_t *ac_level = ctx->cid_table->ac_level;
205  const uint8_t *ac_flags = ctx->cid_table->ac_flags;
206  const int eob_index = ctx->cid_table->eob_index;
207  OPEN_READER(bs, &ctx->gb);
208 
209  if (n&2) {
210  component = 1 + (n&1);
211  scale = ctx->chroma_scale;
212  weight_matrix = ctx->cid_table->chroma_weight;
213  } else {
214  component = 0;
215  scale = ctx->luma_scale;
216  weight_matrix = ctx->cid_table->luma_weight;
217  }
218 
219  UPDATE_CACHE(bs, &ctx->gb);
220  GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
221  if (len) {
222  level = GET_CACHE(bs, &ctx->gb);
223  LAST_SKIP_BITS(bs, &ctx->gb, len);
224  sign = ~level >> 31;
225  level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
226  ctx->last_dc[component] += level;
227  }
228  block[0] = ctx->last_dc[component];
229 
230  i = 0;
231 
232  UPDATE_CACHE(bs, &ctx->gb);
233  GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
234  DNXHD_VLC_BITS, 2);
235 
236  while (index1 != eob_index) {
237  level = ac_level[index1];
238  flags = ac_flags[index1];
239 
240  sign = SHOW_SBITS(bs, &ctx->gb, 1);
241  SKIP_BITS(bs, &ctx->gb, 1);
242 
243  if (flags & 1) {
244  level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 7;
245  SKIP_BITS(bs, &ctx->gb, index_bits);
246  }
247 
248  if (flags & 2) {
249  UPDATE_CACHE(bs, &ctx->gb);
250  GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
251  DNXHD_VLC_BITS, 2);
252  i += ctx->cid_table->run[index2];
253  }
254 
255  if (++i > 63) {
256  av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
257  break;
258  }
259 
260  j = ctx->scantable.permutated[i];
261  level *= scale[i];
262  if (level_bias < 32 || weight_matrix[i] != level_bias)
263  level += level_bias;
264  level >>= level_shift;
265 
266  block[j] = (level^sign) - sign;
267 
268  UPDATE_CACHE(bs, &ctx->gb);
269  GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
270  DNXHD_VLC_BITS, 2);
271  }
272 
273  CLOSE_READER(bs, &ctx->gb);
274 }
275 
276 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
277  int n, int qscale)
278 {
279  dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
280 }
281 
282 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
283  int n, int qscale)
284 {
285  dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
286 }
287 
288 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
289 {
290  int shift1 = ctx->bit_depth == 10;
291  int dct_linesize_luma = ctx->picture.linesize[0];
292  int dct_linesize_chroma = ctx->picture.linesize[1];
293  uint8_t *dest_y, *dest_u, *dest_v;
294  int dct_y_offset, dct_x_offset;
295  int qscale, i;
296 
297  qscale = get_bits(&ctx->gb, 11);
298  skip_bits1(&ctx->gb);
299 
300  if (qscale != ctx->last_qscale) {
301  for (i = 0; i < 64; i++) {
302  ctx->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
303  ctx->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
304  }
305  ctx->last_qscale = qscale;
306  }
307 
308  for (i = 0; i < 8; i++) {
309  ctx->dsp.clear_block(ctx->blocks[i]);
310  ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
311  }
312 
313  if (ctx->picture.interlaced_frame) {
314  dct_linesize_luma <<= 1;
315  dct_linesize_chroma <<= 1;
316  }
317 
318  dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
319  dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
320  dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
321 
322  if (ctx->cur_field) {
323  dest_y += ctx->picture.linesize[0];
324  dest_u += ctx->picture.linesize[1];
325  dest_v += ctx->picture.linesize[2];
326  }
327 
328  dct_y_offset = dct_linesize_luma << 3;
329  dct_x_offset = 8 << shift1;
330  ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
331  ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
332  ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
333  ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
334 
335  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
336  dct_y_offset = dct_linesize_chroma << 3;
337  ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
338  ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
339  ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
340  ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
341  }
342 
343  return 0;
344 }
345 
346 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
347 {
348  int x, y;
349  for (y = 0; y < ctx->mb_height; y++) {
350  ctx->last_dc[0] =
351  ctx->last_dc[1] =
352  ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
353  init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
354  for (x = 0; x < ctx->mb_width; x++) {
355  //START_TIMER;
356  dnxhd_decode_macroblock(ctx, x, y);
357  //STOP_TIMER("decode macroblock");
358  }
359  }
360  return 0;
361 }
362 
363 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
364  AVPacket *avpkt)
365 {
366  const uint8_t *buf = avpkt->data;
367  int buf_size = avpkt->size;
368  DNXHDContext *ctx = avctx->priv_data;
369  AVFrame *picture = data;
370  int first_field = 1;
371  int ret;
372 
373  av_dlog(avctx, "frame size %d\n", buf_size);
374 
375  decode_coding_unit:
376  if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
377  return -1;
378 
379  if ((avctx->width || avctx->height) &&
380  (ctx->width != avctx->width || ctx->height != avctx->height)) {
381  av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
382  avctx->width, avctx->height, ctx->width, ctx->height);
383  first_field = 1;
384  }
385  if (avctx->pix_fmt != AV_PIX_FMT_NONE && avctx->pix_fmt != ctx->pix_fmt) {
386  av_log(avctx, AV_LOG_WARNING, "pix_fmt changed: %s -> %s\n",
388  first_field = 1;
389  }
390 
391  if (av_image_check_size(ctx->width, ctx->height, 0, avctx))
392  return -1;
393  avctx->pix_fmt = ctx->pix_fmt;
394  avcodec_set_dimensions(avctx, ctx->width, ctx->height);
395 
396  if (first_field) {
397  if (ctx->picture.data[0])
398  ff_thread_release_buffer(avctx, &ctx->picture);
399  if ((ret = ff_thread_get_buffer(avctx, &ctx->picture)) < 0) {
400  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
401  return ret;
402  }
403  }
404 
405  dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
406 
407  if (first_field && ctx->picture.interlaced_frame) {
408  buf += ctx->cid_table->coding_unit_size;
409  buf_size -= ctx->cid_table->coding_unit_size;
410  first_field = 0;
411  goto decode_coding_unit;
412  }
413 
414  *picture = ctx->picture;
415  *got_frame = 1;
416  return avpkt->size;
417 }
418 
420 {
421  DNXHDContext *ctx = avctx->priv_data;
422 
423  if (ctx->picture.data[0])
424  ff_thread_release_buffer(avctx, &ctx->picture);
425  ff_free_vlc(&ctx->ac_vlc);
426  ff_free_vlc(&ctx->dc_vlc);
427  ff_free_vlc(&ctx->run_vlc);
428  return 0;
429 }
430 
432  .name = "dnxhd",
433  .type = AVMEDIA_TYPE_VIDEO,
434  .id = AV_CODEC_ID_DNXHD,
435  .priv_data_size = sizeof(DNXHDContext),
439  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
440  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
441 };