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