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