FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "libavutil/imgutils.h"
26 #include "libavutil/timer.h"
27 #include "avcodec.h"
28 #include "get_bits.h"
29 #include "dnxhddata.h"
30 #include "dsputil.h"
31 #include "internal.h"
32 #include "thread.h"
33 
34 typedef struct DNXHDContext {
37  int64_t cid; ///< compression id
38  unsigned int width, height;
39  unsigned int mb_width, mb_height;
40  uint32_t mb_scan_index[68]; /* max for 1080p */
41  int cur_field; ///< current interlaced field
43  int last_dc[3];
45  DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
48  int bit_depth; // 8, 10 or 0 if not initialized at all.
49  int is_444;
50  void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
51  int n, int qscale);
53  int luma_scale[64];
54  int chroma_scale[64];
55 } DNXHDContext;
56 
57 #define DNXHD_VLC_BITS 9
58 #define DNXHD_DC_VLC_BITS 7
59 
60 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block, int n, int qscale);
61 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block, int n, int qscale);
62 static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block, int n, int qscale);
63 
65 {
66  DNXHDContext *ctx = avctx->priv_data;
67 
68  ctx->avctx = avctx;
69  ctx->cid = -1;
70  return 0;
71 }
72 
73 static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid)
74 {
75  if (cid != ctx->cid) {
76  int index;
77 
78  if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
79  av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
80  return AVERROR(ENOSYS);
81  }
82  if (ff_dnxhd_cid_table[index].bit_depth != ctx->bit_depth) {
83  av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n", ff_dnxhd_cid_table[index].bit_depth, ctx->bit_depth);
84  return AVERROR_INVALIDDATA;
85  }
87 
88  ff_free_vlc(&ctx->ac_vlc);
89  ff_free_vlc(&ctx->dc_vlc);
90  ff_free_vlc(&ctx->run_vlc);
91 
92  init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
93  ctx->cid_table->ac_bits, 1, 1,
94  ctx->cid_table->ac_codes, 2, 2, 0);
95  init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
96  ctx->cid_table->dc_bits, 1, 1,
97  ctx->cid_table->dc_codes, 1, 1, 0);
98  init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
99  ctx->cid_table->run_bits, 1, 1,
100  ctx->cid_table->run_codes, 2, 2, 0);
101 
103  ctx->cid = cid;
104  }
105  return 0;
106 }
107 
109  const uint8_t *buf, int buf_size, int first_field)
110 {
111  static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
112  static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
113  int i, cid, ret;
114 
115  if (buf_size < 0x280)
116  return AVERROR_INVALIDDATA;
117 
118  if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {
119  av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
120  return AVERROR_INVALIDDATA;
121  }
122  if (buf[5] & 2) { /* interlaced */
123  ctx->cur_field = buf[5] & 1;
124  frame->interlaced_frame = 1;
125  frame->top_field_first = first_field ^ ctx->cur_field;
126  av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
127  }
128 
129  ctx->height = AV_RB16(buf + 0x18);
130  ctx->width = AV_RB16(buf + 0x1a);
131 
132  av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
133 
134  ctx->is_444 = 0;
135  if (buf[0x4] == 0x2) {
137  ctx->avctx->bits_per_raw_sample = 10;
138  if (ctx->bit_depth != 10) {
139  ff_dsputil_init(&ctx->dsp, ctx->avctx);
140  ctx->bit_depth = 10;
142  }
143  ctx->is_444 = 1;
144  } else if (buf[0x21] & 0x40) {
146  ctx->avctx->bits_per_raw_sample = 10;
147  if (ctx->bit_depth != 10) {
148  ff_dsputil_init(&ctx->dsp, ctx->avctx);
149  ctx->bit_depth = 10;
151  }
152  } else {
154  ctx->avctx->bits_per_raw_sample = 8;
155  if (ctx->bit_depth != 8) {
156  ff_dsputil_init(&ctx->dsp, ctx->avctx);
157  ctx->bit_depth = 8;
159  }
160  }
161 
162  cid = AV_RB32(buf + 0x28);
163  av_dlog(ctx->avctx, "compression id %d\n", cid);
164 
165  if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
166  return ret;
167 
168  if (buf_size < ctx->cid_table->coding_unit_size) {
169  av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
170  return AVERROR_INVALIDDATA;
171  }
172 
173  ctx->mb_width = ctx->width>>4;
174  ctx->mb_height = buf[0x16d];
175 
176  av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
177 
178  if ((ctx->height+15)>>4 == ctx->mb_height && frame->interlaced_frame)
179  ctx->height <<= 1;
180 
181  if (ctx->mb_height > 68 ||
182  (ctx->mb_height << frame->interlaced_frame) > (ctx->height+15)>>4) {
183  av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
184  return AVERROR_INVALIDDATA;
185  }
186 
187  for (i = 0; i < ctx->mb_height; i++) {
188  ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
189  av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
190  if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {
191  av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
192  return AVERROR_INVALIDDATA;
193  }
194  }
195 
196  return 0;
197 }
198 
200  int16_t *block, int n,
201  int qscale,
202  int index_bits,
203  int level_bias,
204  int level_shift)
205 {
206  int i, j, index1, index2, len, flags;
207  int level, component, sign;
208  const int *scale;
209  const uint8_t *weight_matrix;
210  const uint8_t *ac_level = ctx->cid_table->ac_level;
211  const uint8_t *ac_flags = ctx->cid_table->ac_flags;
212  const int eob_index = ctx->cid_table->eob_index;
213  OPEN_READER(bs, &ctx->gb);
214 
215  if (!ctx->is_444) {
216  if (n&2) {
217  component = 1 + (n&1);
218  scale = ctx->chroma_scale;
219  weight_matrix = ctx->cid_table->chroma_weight;
220  } else {
221  component = 0;
222  scale = ctx->luma_scale;
223  weight_matrix = ctx->cid_table->luma_weight;
224  }
225  } else {
226  component = (n >> 1) % 3;
227  if (component) {
228  scale = ctx->chroma_scale;
229  weight_matrix = ctx->cid_table->chroma_weight;
230  } else {
231  scale = ctx->luma_scale;
232  weight_matrix = ctx->cid_table->luma_weight;
233  }
234  }
235 
236  UPDATE_CACHE(bs, &ctx->gb);
237  GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
238  if (len) {
239  level = GET_CACHE(bs, &ctx->gb);
240  LAST_SKIP_BITS(bs, &ctx->gb, len);
241  sign = ~level >> 31;
242  level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
243  ctx->last_dc[component] += level;
244  }
245  block[0] = ctx->last_dc[component];
246 
247  i = 0;
248 
249  UPDATE_CACHE(bs, &ctx->gb);
250  GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
251  DNXHD_VLC_BITS, 2);
252 
253  while (index1 != eob_index) {
254  level = ac_level[index1];
255  flags = ac_flags[index1];
256 
257  sign = SHOW_SBITS(bs, &ctx->gb, 1);
258  SKIP_BITS(bs, &ctx->gb, 1);
259 
260  if (flags & 1) {
261  level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 7;
262  SKIP_BITS(bs, &ctx->gb, index_bits);
263  }
264 
265  if (flags & 2) {
266  UPDATE_CACHE(bs, &ctx->gb);
267  GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
268  DNXHD_VLC_BITS, 2);
269  i += ctx->cid_table->run[index2];
270  }
271 
272  if (++i > 63) {
273  av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
274  break;
275  }
276 
277  j = ctx->scantable.permutated[i];
278  level *= scale[i];
279  if (level_bias < 32 || weight_matrix[i] != level_bias)
280  level += level_bias;
281  level >>= level_shift;
282 
283  block[j] = (level^sign) - sign;
284 
285  UPDATE_CACHE(bs, &ctx->gb);
286  GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
287  DNXHD_VLC_BITS, 2);
288  }
289 
290  CLOSE_READER(bs, &ctx->gb);
291 }
292 
293 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
294  int n, int qscale)
295 {
296  dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
297 }
298 
299 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
300  int n, int qscale)
301 {
302  dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
303 }
304 
306  int n, int qscale)
307 {
308  dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 32, 6);
309 }
310 
311 static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame, int x, int y)
312 {
313  int shift1 = ctx->bit_depth == 10;
314  int dct_linesize_luma = frame->linesize[0];
315  int dct_linesize_chroma = frame->linesize[1];
316  uint8_t *dest_y, *dest_u, *dest_v;
317  int dct_y_offset, dct_x_offset;
318  int qscale, i;
319 
320  qscale = get_bits(&ctx->gb, 11);
321  skip_bits1(&ctx->gb);
322 
323  if (qscale != ctx->last_qscale) {
324  for (i = 0; i < 64; i++) {
325  ctx->luma_scale[i] = qscale * ctx->cid_table->luma_weight[i];
326  ctx->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
327  }
328  ctx->last_qscale = qscale;
329  }
330 
331  for (i = 0; i < 8; i++) {
332  ctx->dsp.clear_block(ctx->blocks[i]);
333  ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
334  }
335  if (ctx->is_444) {
336  for (; i < 12; i++) {
337  ctx->dsp.clear_block(ctx->blocks[i]);
338  ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
339  }
340  }
341 
342  if (frame->interlaced_frame) {
343  dct_linesize_luma <<= 1;
344  dct_linesize_chroma <<= 1;
345  }
346 
347  dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
348  dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
349  dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
350 
351  if (ctx->cur_field) {
352  dest_y += frame->linesize[0];
353  dest_u += frame->linesize[1];
354  dest_v += frame->linesize[2];
355  }
356 
357  dct_y_offset = dct_linesize_luma << 3;
358  dct_x_offset = 8 << shift1;
359  if (!ctx->is_444) {
360  ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
361  ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
362  ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
363  ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
364 
365  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
366  dct_y_offset = dct_linesize_chroma << 3;
367  ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
368  ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
369  ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
370  ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
371  }
372  } else {
373  ctx->dsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
374  ctx->dsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
375  ctx->dsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[6]);
376  ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[7]);
377 
378  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
379  dct_y_offset = dct_linesize_chroma << 3;
380  ctx->dsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
381  ctx->dsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, ctx->blocks[3]);
382  ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[8]);
383  ctx->dsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[9]);
384  ctx->dsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[4]);
385  ctx->dsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, ctx->blocks[5]);
386  ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[10]);
387  ctx->dsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[11]);
388  }
389  }
390 
391  return 0;
392 }
393 
395  const uint8_t *buf, int buf_size)
396 {
397  int x, y;
398  for (y = 0; y < ctx->mb_height; y++) {
399  ctx->last_dc[0] =
400  ctx->last_dc[1] =
401  ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
402  init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
403  for (x = 0; x < ctx->mb_width; x++) {
404  //START_TIMER;
405  dnxhd_decode_macroblock(ctx, frame, x, y);
406  //STOP_TIMER("decode macroblock");
407  }
408  }
409  return 0;
410 }
411 
412 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
413  AVPacket *avpkt)
414 {
415  const uint8_t *buf = avpkt->data;
416  int buf_size = avpkt->size;
417  DNXHDContext *ctx = avctx->priv_data;
418  ThreadFrame frame = { .f = data };
419  AVFrame *picture = data;
420  int first_field = 1;
421  int ret;
422 
423  av_dlog(avctx, "frame size %d\n", buf_size);
424 
425  decode_coding_unit:
426  if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
427  return ret;
428 
429  if ((avctx->width || avctx->height) &&
430  (ctx->width != avctx->width || ctx->height != avctx->height)) {
431  av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
432  avctx->width, avctx->height, ctx->width, ctx->height);
433  first_field = 1;
434  }
435 
436  ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
437  if (ret < 0)
438  return ret;
439 
440  if (first_field) {
441  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
442  return ret;
443  picture->pict_type = AV_PICTURE_TYPE_I;
444  picture->key_frame = 1;
445  }
446 
447  dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
448 
449  if (first_field && picture->interlaced_frame) {
450  buf += ctx->cid_table->coding_unit_size;
451  buf_size -= ctx->cid_table->coding_unit_size;
452  first_field = 0;
453  goto decode_coding_unit;
454  }
455 
456  *got_frame = 1;
457  return avpkt->size;
458 }
459 
461 {
462  DNXHDContext *ctx = avctx->priv_data;
463 
464  ff_free_vlc(&ctx->ac_vlc);
465  ff_free_vlc(&ctx->dc_vlc);
466  ff_free_vlc(&ctx->run_vlc);
467  return 0;
468 }
469 
471  .name = "dnxhd",
472  .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
473  .type = AVMEDIA_TYPE_VIDEO,
474  .id = AV_CODEC_ID_DNXHD,
475  .priv_data_size = sizeof(DNXHDContext),
479  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
480 };