lavc: AV-prefix all codec flags
[libav.git] / libavcodec / dnxhddec.c
CommitLineData
52b6bad2
BC
1/*
2 * VC3/DNxHD decoder.
406792e7 3 * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
5ab21439
JA
4 * Copyright (c) 2011 MirriAd Ltd
5 *
6 * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
52b6bad2 7 *
2912e87a 8 * This file is part of Libav.
52b6bad2 9 *
2912e87a 10 * Libav is free software; you can redistribute it and/or
52b6bad2
BC
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 *
2912e87a 15 * Libav is distributed in the hope that it will be useful,
52b6bad2
BC
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
2912e87a 21 * License along with Libav; if not, write to the Free Software
52b6bad2
BC
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
737eb597 25#include "libavutil/imgutils.h"
fb0c9d41 26#include "libavutil/timer.h"
52b6bad2 27#include "avcodec.h"
e74433a8 28#include "blockdsp.h"
9106a698 29#include "get_bits.h"
52b6bad2 30#include "dnxhddata.h"
e3fcb143 31#include "idctdsp.h"
594d4d5d 32#include "internal.h"
52b6bad2 33
bb32fded 34typedef struct DNXHDContext {
52b6bad2 35 AVCodecContext *avctx;
52b6bad2 36 GetBitContext gb;
e74433a8 37 BlockDSPContext bdsp;
52b6bad2
BC
38 int cid; ///< compression id
39 unsigned int width, height;
40 unsigned int mb_width, mb_height;
41 uint32_t mb_scan_index[68]; /* max for 1080p */
42 int cur_field; ///< current interlaced field
52b6bad2 43 VLC ac_vlc, dc_vlc, run_vlc;
52b6bad2 44 int last_dc[3];
e3fcb143 45 IDCTDSPContext idsp;
e95018b6 46 DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
20e7c8ae 47 ScanTable scantable;
52b6bad2 48 const CIDEntry *cid_table;
5ab21439 49 int bit_depth; // 8, 10 or 0 if not initialized at all.
e95018b6 50 int is_444;
88bd7fdc 51 void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
bb32fded 52 int n, int qscale);
52b6bad2
BC
53} DNXHDContext;
54
52b6bad2 55#define DNXHD_VLC_BITS 9
cd52a9ca 56#define DNXHD_DC_VLC_BITS 7
52b6bad2 57
89ef08c9
VG
58static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
59 int n, int qscale);
60static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
61 int n, int qscale);
62static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
63 int n, int qscale);
bb32fded 64
98a6fff9 65static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
52b6bad2
BC
66{
67 DNXHDContext *ctx = avctx->priv_data;
68
69 ctx->avctx = avctx;
52b6bad2
BC
70 return 0;
71}
72
73static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
74{
11a32d53 75 if (cid != ctx->cid) {
52b6bad2
BC
76 int index;
77
d3dc629c 78 if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
52b6bad2 79 av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
28e508a9 80 return AVERROR(ENOSYS);
52b6bad2 81 }
d3dc629c 82 ctx->cid_table = &ff_dnxhd_cid_table[index];
5980c924 83 av_log(ctx->avctx, AV_LOG_VERBOSE, "Profile cid %d.\n", cid);
11a32d53 84
e96b4a53
MS
85 ff_free_vlc(&ctx->ac_vlc);
86 ff_free_vlc(&ctx->dc_vlc);
87 ff_free_vlc(&ctx->run_vlc);
11a32d53 88
52b6bad2 89 init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
940e4bf9
BC
90 ctx->cid_table->ac_bits, 1, 1,
91 ctx->cid_table->ac_codes, 2, 2, 0);
5ab21439 92 init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
940e4bf9
BC
93 ctx->cid_table->dc_bits, 1, 1,
94 ctx->cid_table->dc_codes, 1, 1, 0);
52b6bad2 95 init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
940e4bf9
BC
96 ctx->cid_table->run_bits, 1, 1,
97 ctx->cid_table->run_codes, 2, 2, 0);
52b6bad2 98
e3fcb143 99 ff_init_scantable(ctx->idsp.idct_permutation, &ctx->scantable,
89ef08c9 100 ff_zigzag_direct);
11a32d53 101 ctx->cid = cid;
52b6bad2
BC
102 }
103 return 0;
104}
105
759001c5 106static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
89ef08c9
VG
107 const uint8_t *buf, int buf_size,
108 int first_field)
52b6bad2 109{
e95018b6
KS
110 static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
111 static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
28e508a9 112 int i, cid, ret;
3ecb82dd 113 int old_bit_depth = ctx->bit_depth;
52b6bad2 114
41e03e28
VG
115 if (buf_size < 0x280) {
116 av_log(ctx->avctx, AV_LOG_ERROR, "buffer too small (%d < 640).\n",
117 buf_size);
28e508a9 118 return AVERROR_INVALIDDATA;
41e03e28 119 }
52b6bad2 120
e95018b6 121 if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {
d84429d4
VG
122 av_log(ctx->avctx, AV_LOG_ERROR,
123 "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
124 buf[0], buf[1], buf[2], buf[3], buf[4]);
28e508a9 125 return AVERROR_INVALIDDATA;
52b6bad2 126 }
6b259e43
BC
127 if (buf[5] & 2) { /* interlaced */
128 ctx->cur_field = buf[5] & 1;
759001c5
AK
129 frame->interlaced_frame = 1;
130 frame->top_field_first = first_field ^ ctx->cur_field;
89ef08c9
VG
131 av_log(ctx->avctx, AV_LOG_DEBUG,
132 "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
52b6bad2
BC
133 }
134
135 ctx->height = AV_RB16(buf + 0x18);
136 ctx->width = AV_RB16(buf + 0x1a);
137
6a85dfc8 138 ff_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
52b6bad2 139
598f7d04
VG
140 if (buf[0x21] == 0x58) { /* 10 bit */
141 ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 10;
142
143 if (buf[0x4] == 0x2) {
e95018b6 144 ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
598f7d04
VG
145 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
146 ctx->is_444 = 1;
147 } else {
bb32fded 148 ctx->decode_dct_block = dnxhd_decode_dct_block_10;
598f7d04 149 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
5ab21439 150 }
598f7d04
VG
151 } else if (buf[0x21] == 0x38) { /* 8 bit */
152 ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 8;
153
716d413c 154 ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
598f7d04
VG
155 ctx->decode_dct_block = dnxhd_decode_dct_block_8;
156 } else {
157 av_log(ctx->avctx, AV_LOG_ERROR, "invalid bit depth value (%d).\n",
158 buf[0x21]);
159 return AVERROR_INVALIDDATA;
52b6bad2 160 }
3ecb82dd
CEH
161 if (ctx->bit_depth != old_bit_depth) {
162 ff_blockdsp_init(&ctx->bdsp, ctx->avctx);
163 ff_idctdsp_init(&ctx->idsp, ctx->avctx);
164 }
52b6bad2 165
11a32d53 166 cid = AV_RB32(buf + 0x28);
6a85dfc8 167 ff_dlog(ctx->avctx, "compression id %d\n", cid);
52b6bad2 168
28e508a9
AK
169 if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
170 return ret;
52b6bad2 171
1a07df31
VG
172 // make sure profile size constraints are respected
173 // DNx100 allows 1920->1440 and 1280->960 subsampling
174 if (ctx->width != ctx->cid_table->width) {
175 av_reduce(&ctx->avctx->sample_aspect_ratio.num,
176 &ctx->avctx->sample_aspect_ratio.den,
177 ctx->width, ctx->cid_table->width, 255);
178 ctx->width = ctx->cid_table->width;
179 }
180
035bc647 181 if (buf_size < ctx->cid_table->coding_unit_size) {
41e03e28
VG
182 av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %d).\n",
183 buf_size, ctx->cid_table->coding_unit_size);
28e508a9 184 return AVERROR_INVALIDDATA;
52b6bad2
BC
185 }
186
89ef08c9 187 ctx->mb_width = ctx->width >> 4;
52b6bad2
BC
188 ctx->mb_height = buf[0x16d];
189
6a85dfc8 190 ff_dlog(ctx->avctx,
89ef08c9 191 "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
1307463d 192
89ef08c9 193 if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)
1307463d
BC
194 ctx->height <<= 1;
195
196 if (ctx->mb_height > 68 ||
89ef08c9
VG
197 (ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {
198 av_log(ctx->avctx, AV_LOG_ERROR,
199 "mb height too big: %d\n", ctx->mb_height);
28e508a9 200 return AVERROR_INVALIDDATA;
52b6bad2
BC
201 }
202
52b6bad2 203 for (i = 0; i < ctx->mb_height; i++) {
89ef08c9 204 ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
6a85dfc8 205 ff_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
52b6bad2 206 if (buf_size < ctx->mb_scan_index[i] + 0x280) {
41e03e28
VG
207 av_log(ctx->avctx, AV_LOG_ERROR,
208 "invalid mb scan index (%d < %d).\n",
209 buf_size, ctx->mb_scan_index[i] + 0x280);
28e508a9 210 return AVERROR_INVALIDDATA;
52b6bad2
BC
211 }
212 }
213
214 return 0;
215}
216
bb32fded 217static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
88bd7fdc 218 int16_t *block, int n,
bb32fded
MR
219 int qscale,
220 int index_bits,
221 int level_bias,
222 int level_shift)
52b6bad2 223{
bb32fded 224 int i, j, index1, index2, len;
52b6bad2 225 int level, component, sign;
bb32fded
MR
226 const uint8_t *weight_matrix;
227 OPEN_READER(bs, &ctx->gb);
52b6bad2 228
e95018b6 229 if (!ctx->is_444) {
89ef08c9
VG
230 if (n & 2) {
231 component = 1 + (n & 1);
e95018b6
KS
232 weight_matrix = ctx->cid_table->chroma_weight;
233 } else {
89ef08c9 234 component = 0;
e95018b6
KS
235 weight_matrix = ctx->cid_table->luma_weight;
236 }
52b6bad2 237 } else {
e95018b6
KS
238 component = (n >> 1) % 3;
239 if (component) {
240 weight_matrix = ctx->cid_table->chroma_weight;
241 } else {
242 weight_matrix = ctx->cid_table->luma_weight;
243 }
52b6bad2
BC
244 }
245
bb32fded
MR
246 UPDATE_CACHE(bs, &ctx->gb);
247 GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
248 if (len) {
249 level = GET_CACHE(bs, &ctx->gb);
250 LAST_SKIP_BITS(bs, &ctx->gb, len);
251 sign = ~level >> 31;
252 level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
253 ctx->last_dc[component] += level;
254 }
52b6bad2 255 block[0] = ctx->last_dc[component];
bb32fded 256
52b6bad2 257 for (i = 1; ; i++) {
bb32fded
MR
258 UPDATE_CACHE(bs, &ctx->gb);
259 GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
260 DNXHD_VLC_BITS, 2);
bb32fded 261 level = ctx->cid_table->ac_level[index1];
6f6b0311 262 if (!level) /* EOB */
bb32fded 263 break;
52b6bad2 264
bb32fded
MR
265 sign = SHOW_SBITS(bs, &ctx->gb, 1);
266 SKIP_BITS(bs, &ctx->gb, 1);
267
268 if (ctx->cid_table->ac_index_flag[index1]) {
269 level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6;
270 SKIP_BITS(bs, &ctx->gb, index_bits);
52b6bad2
BC
271 }
272
bb32fded
MR
273 if (ctx->cid_table->ac_run_flag[index1]) {
274 UPDATE_CACHE(bs, &ctx->gb);
275 GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
276 DNXHD_VLC_BITS, 2);
9a0a2c9f 277 i += ctx->cid_table->run[index2];
52b6bad2
BC
278 }
279
a417d041
BC
280 if (i > 63) {
281 av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
bb32fded 282 break;
a417d041
BC
283 }
284
89ef08c9
VG
285 j = ctx->scantable.permutated[i];
286 level = (2 * level + 1) * qscale * weight_matrix[i];
b4cfb825 287 if (level_bias < 32 || weight_matrix[i] != level_bias)
bb32fded
MR
288 level += level_bias;
289 level >>= level_shift;
290
89ef08c9 291 block[j] = (level ^ sign) - sign;
52b6bad2 292 }
bb32fded
MR
293
294 CLOSE_READER(bs, &ctx->gb);
295}
296
88bd7fdc 297static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
bb32fded
MR
298 int n, int qscale)
299{
300 dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
301}
302
88bd7fdc 303static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
bb32fded
MR
304 int n, int qscale)
305{
306 dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
52b6bad2
BC
307}
308
e95018b6
KS
309static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
310 int n, int qscale)
311{
312 dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 32, 6);
313}
314
89ef08c9
VG
315static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame,
316 int x, int y)
52b6bad2 317{
5ab21439 318 int shift1 = ctx->bit_depth == 10;
759001c5
AK
319 int dct_linesize_luma = frame->linesize[0];
320 int dct_linesize_chroma = frame->linesize[1];
52b6bad2 321 uint8_t *dest_y, *dest_u, *dest_v;
5ab21439 322 int dct_y_offset, dct_x_offset;
52b6bad2
BC
323 int qscale, i;
324
52b6bad2
BC
325 qscale = get_bits(&ctx->gb, 11);
326 skip_bits1(&ctx->gb);
52b6bad2
BC
327
328 for (i = 0; i < 8; i++) {
e74433a8 329 ctx->bdsp.clear_block(ctx->blocks[i]);
bb32fded 330 ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
52b6bad2 331 }
e95018b6
KS
332 if (ctx->is_444) {
333 for (; i < 12; i++) {
e74433a8 334 ctx->bdsp.clear_block(ctx->blocks[i]);
e95018b6
KS
335 ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
336 }
337 }
6b259e43 338
759001c5 339 if (frame->interlaced_frame) {
6b259e43
BC
340 dct_linesize_luma <<= 1;
341 dct_linesize_chroma <<= 1;
342 }
343
759001c5 344 dest_y = frame->data[0] + ((y * dct_linesize_luma) << 4) + (x << (4 + shift1));
e95018b6
KS
345 dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
346 dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
52b6bad2 347
6b259e43 348 if (ctx->cur_field) {
759001c5
AK
349 dest_y += frame->linesize[0];
350 dest_u += frame->linesize[1];
351 dest_v += frame->linesize[2];
6b259e43
BC
352 }
353
5ab21439
JA
354 dct_y_offset = dct_linesize_luma << 3;
355 dct_x_offset = 8 << shift1;
e95018b6 356 if (!ctx->is_444) {
e3fcb143
DB
357 ctx->idsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
358 ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
359 ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[4]);
360 ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
e95018b6 361
7c6eb0a1 362 if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
e95018b6 363 dct_y_offset = dct_linesize_chroma << 3;
e3fcb143
DB
364 ctx->idsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
365 ctx->idsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[3]);
366 ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
367 ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
e95018b6
KS
368 }
369 } else {
e3fcb143
DB
370 ctx->idsp.idct_put(dest_y, dct_linesize_luma, ctx->blocks[0]);
371 ctx->idsp.idct_put(dest_y + dct_x_offset, dct_linesize_luma, ctx->blocks[1]);
372 ctx->idsp.idct_put(dest_y + dct_y_offset, dct_linesize_luma, ctx->blocks[6]);
373 ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[7]);
e95018b6 374
7c6eb0a1 375 if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
e95018b6 376 dct_y_offset = dct_linesize_chroma << 3;
e3fcb143
DB
377 ctx->idsp.idct_put(dest_u, dct_linesize_chroma, ctx->blocks[2]);
378 ctx->idsp.idct_put(dest_u + dct_x_offset, dct_linesize_chroma, ctx->blocks[3]);
379 ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[8]);
380 ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[9]);
381 ctx->idsp.idct_put(dest_v, dct_linesize_chroma, ctx->blocks[4]);
382 ctx->idsp.idct_put(dest_v + dct_x_offset, dct_linesize_chroma, ctx->blocks[5]);
383 ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[10]);
384 ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, ctx->blocks[11]);
e95018b6 385 }
52b6bad2
BC
386 }
387
388 return 0;
389}
390
759001c5
AK
391static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
392 const uint8_t *buf, int buf_size)
52b6bad2
BC
393{
394 int x, y;
395 for (y = 0; y < ctx->mb_height; y++) {
36ebf912
BC
396 ctx->last_dc[0] =
397 ctx->last_dc[1] =
5ab21439 398 ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
52b6bad2
BC
399 init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
400 for (x = 0; x < ctx->mb_width; x++) {
401 //START_TIMER;
759001c5 402 dnxhd_decode_macroblock(ctx, frame, x, y);
52b6bad2
BC
403 //STOP_TIMER("decode macroblock");
404 }
405 }
406 return 0;
407}
408
89ef08c9
VG
409static int dnxhd_decode_frame(AVCodecContext *avctx, void *data,
410 int *got_frame, AVPacket *avpkt)
52b6bad2 411{
7a00bbad
TB
412 const uint8_t *buf = avpkt->data;
413 int buf_size = avpkt->size;
52b6bad2
BC
414 DNXHDContext *ctx = avctx->priv_data;
415 AVFrame *picture = data;
6b259e43 416 int first_field = 1;
759001c5 417 int ret;
52b6bad2 418
6a85dfc8 419 ff_dlog(avctx, "frame size %d\n", buf_size);
52b6bad2 420
89ef08c9 421decode_coding_unit:
28e508a9
AK
422 if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
423 return ret;
52b6bad2 424
1307463d
BC
425 if ((avctx->width || avctx->height) &&
426 (ctx->width != avctx->width || ctx->height != avctx->height)) {
427 av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
428 avctx->width, avctx->height, ctx->width, ctx->height);
429 first_field = 1;
430 }
431
c7a5acab
AK
432 ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
433 if (ret < 0)
434 return ret;
52b6bad2 435
6b259e43 436 if (first_field) {
759001c5 437 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
3c605485 438 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
759001c5 439 return ret;
3c605485 440 }
759001c5
AK
441 picture->pict_type = AV_PICTURE_TYPE_I;
442 picture->key_frame = 1;
6b259e43 443 }
52b6bad2 444
759001c5 445 dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
52b6bad2 446
759001c5 447 if (first_field && picture->interlaced_frame) {
6b259e43
BC
448 buf += ctx->cid_table->coding_unit_size;
449 buf_size -= ctx->cid_table->coding_unit_size;
450 first_field = 0;
451 goto decode_coding_unit;
452 }
453
df9b9567 454 *got_frame = 1;
239e1b6f 455 return buf_size;
52b6bad2
BC
456}
457
98a6fff9 458static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
52b6bad2
BC
459{
460 DNXHDContext *ctx = avctx->priv_data;
461
e96b4a53
MS
462 ff_free_vlc(&ctx->ac_vlc);
463 ff_free_vlc(&ctx->dc_vlc);
464 ff_free_vlc(&ctx->run_vlc);
52b6bad2
BC
465 return 0;
466}
467
d36beb3f 468AVCodec ff_dnxhd_decoder = {
ec6402b7 469 .name = "dnxhd",
b2bed932 470 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
ec6402b7 471 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 472 .id = AV_CODEC_ID_DNXHD,
ec6402b7
AK
473 .priv_data_size = sizeof(DNXHDContext),
474 .init = dnxhd_decode_init,
475 .close = dnxhd_decode_close,
476 .decode = dnxhd_decode_frame,
477 .capabilities = CODEC_CAP_DR1,
52b6bad2 478};