]> git.sesse.net Git - ffmpeg/blob - libavcodec/dnxhddec.c
bink: Factorize bink put_pixel
[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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; 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
34 typedef struct DNXHDContext {
35     AVCodecContext *avctx;
36     GetBitContext gb;
37     BlockDSPContext bdsp;
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
43     VLC ac_vlc, dc_vlc, run_vlc;
44     int last_dc[3];
45     IDCTDSPContext idsp;
46     DECLARE_ALIGNED(16, int16_t, blocks)[12][64];
47     ScanTable scantable;
48     const CIDEntry *cid_table;
49     int bit_depth; // 8, 10 or 0 if not initialized at all.
50     int is_444;
51     void (*decode_dct_block)(struct DNXHDContext *ctx, int16_t *block,
52                              int n, int qscale);
53 } DNXHDContext;
54
55 #define DNXHD_VLC_BITS 9
56 #define DNXHD_DC_VLC_BITS 7
57
58 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
59                                      int n, int qscale);
60 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
61                                       int n, int qscale);
62 static void dnxhd_decode_dct_block_10_444(DNXHDContext *ctx, int16_t *block,
63                                           int n, int qscale);
64
65 static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
66 {
67     DNXHDContext *ctx = avctx->priv_data;
68
69     ctx->avctx = avctx;
70     return 0;
71 }
72
73 static int dnxhd_init_vlc(DNXHDContext *ctx, int 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         ctx->cid_table = &ff_dnxhd_cid_table[index];
83         av_log(ctx->avctx, AV_LOG_VERBOSE, "Profile cid %d.\n", cid);
84
85         ff_free_vlc(&ctx->ac_vlc);
86         ff_free_vlc(&ctx->dc_vlc);
87         ff_free_vlc(&ctx->run_vlc);
88
89         init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
90                  ctx->cid_table->ac_bits, 1, 1,
91                  ctx->cid_table->ac_codes, 2, 2, 0);
92         init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
93                  ctx->cid_table->dc_bits, 1, 1,
94                  ctx->cid_table->dc_codes, 1, 1, 0);
95         init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
96                  ctx->cid_table->run_bits, 1, 1,
97                  ctx->cid_table->run_codes, 2, 2, 0);
98
99         ff_init_scantable(ctx->idsp.idct_permutation, &ctx->scantable,
100                           ff_zigzag_direct);
101         ctx->cid = cid;
102     }
103     return 0;
104 }
105
106 static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
107                                const uint8_t *buf, int buf_size,
108                                int first_field)
109 {
110     static const uint8_t header_prefix[]    = { 0x00, 0x00, 0x02, 0x80, 0x01 };
111     static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };
112     int i, cid, ret;
113     int old_bit_depth = ctx->bit_depth;
114
115     if (buf_size < 0x280) {
116         av_log(ctx->avctx, AV_LOG_ERROR, "buffer too small (%d < 640).\n",
117                buf_size);
118         return AVERROR_INVALIDDATA;
119     }
120
121     if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {
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]);
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     ff_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
139
140     if (buf[0x21] == 0x58) { /* 10 bit */
141         ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 10;
142
143         if (buf[0x4] == 0x2) {
144             ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
145             ctx->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
146             ctx->is_444 = 1;
147         } else {
148             ctx->decode_dct_block = dnxhd_decode_dct_block_10;
149             ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
150         }
151     } else if (buf[0x21] == 0x38) { /* 8 bit */
152         ctx->bit_depth = ctx->avctx->bits_per_raw_sample = 8;
153
154         ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
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;
160     }
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     }
165
166     cid = AV_RB32(buf + 0x28);
167     ff_dlog(ctx->avctx, "compression id %d\n", cid);
168
169     if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)
170         return ret;
171
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
181     if (buf_size < ctx->cid_table->coding_unit_size) {
182         av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %d).\n",
183                buf_size, ctx->cid_table->coding_unit_size);
184         return AVERROR_INVALIDDATA;
185     }
186
187     ctx->mb_width  = ctx->width >> 4;
188     ctx->mb_height = buf[0x16d];
189
190     ff_dlog(ctx->avctx,
191             "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
192
193     if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)
194         ctx->height <<= 1;
195
196     if (ctx->mb_height > 68 ||
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);
200         return AVERROR_INVALIDDATA;
201     }
202
203     for (i = 0; i < ctx->mb_height; i++) {
204         ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
205         ff_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
206         if (buf_size < ctx->mb_scan_index[i] + 0x280) {
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);
210             return AVERROR_INVALIDDATA;
211         }
212     }
213
214     return 0;
215 }
216
217 static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
218                                                     int16_t *block, int n,
219                                                     int qscale,
220                                                     int index_bits,
221                                                     int level_bias,
222                                                     int level_shift)
223 {
224     int i, j, index1, index2, len;
225     int level, component, sign;
226     const uint8_t *weight_matrix;
227     OPEN_READER(bs, &ctx->gb);
228
229     if (!ctx->is_444) {
230         if (n & 2) {
231             component     = 1 + (n & 1);
232             weight_matrix = ctx->cid_table->chroma_weight;
233         } else {
234             component     = 0;
235             weight_matrix = ctx->cid_table->luma_weight;
236         }
237     } else {
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         }
244     }
245
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     }
255     block[0] = ctx->last_dc[component];
256
257     for (i = 1; ; i++) {
258         UPDATE_CACHE(bs, &ctx->gb);
259         GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
260                 DNXHD_VLC_BITS, 2);
261         level = ctx->cid_table->ac_level[index1];
262         if (!level) /* EOB */
263             break;
264
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);
271         }
272
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);
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 = (2 * level + 1) * qscale * weight_matrix[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
294     CLOSE_READER(bs, &ctx->gb);
295 }
296
297 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, int16_t *block,
298                                      int n, int qscale)
299 {
300     dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
301 }
302
303 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, int16_t *block,
304                                       int n, int qscale)
305 {
306     dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
307 }
308
309 static 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
315 static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame,
316                                    int x, int y)
317 {
318     int shift1 = ctx->bit_depth == 10;
319     int dct_linesize_luma   = frame->linesize[0];
320     int dct_linesize_chroma = frame->linesize[1];
321     uint8_t *dest_y, *dest_u, *dest_v;
322     int dct_y_offset, dct_x_offset;
323     int qscale, i;
324
325     qscale = get_bits(&ctx->gb, 11);
326     skip_bits1(&ctx->gb);
327
328     for (i = 0; i < 8; i++) {
329         ctx->bdsp.clear_block(ctx->blocks[i]);
330         ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
331     }
332     if (ctx->is_444) {
333         for (; i < 12; i++) {
334             ctx->bdsp.clear_block(ctx->blocks[i]);
335             ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
336         }
337     }
338
339     if (frame->interlaced_frame) {
340         dct_linesize_luma   <<= 1;
341         dct_linesize_chroma <<= 1;
342     }
343
344     dest_y = frame->data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
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));
347
348     if (ctx->cur_field) {
349         dest_y += frame->linesize[0];
350         dest_u += frame->linesize[1];
351         dest_v += frame->linesize[2];
352     }
353
354     dct_y_offset = dct_linesize_luma << 3;
355     dct_x_offset = 8 << shift1;
356     if (!ctx->is_444) {
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]);
361
362         if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
363             dct_y_offset = dct_linesize_chroma << 3;
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]);
368         }
369     } else {
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]);
374
375         if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
376             dct_y_offset = dct_linesize_chroma << 3;
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]);
385         }
386     }
387
388     return 0;
389 }
390
391 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
392                                     const uint8_t *buf, int buf_size)
393 {
394     int x, y;
395     for (y = 0; y < ctx->mb_height; y++) {
396         ctx->last_dc[0] =
397         ctx->last_dc[1] =
398         ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
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;
402             dnxhd_decode_macroblock(ctx, frame, x, y);
403             //STOP_TIMER("decode macroblock");
404         }
405     }
406     return 0;
407 }
408
409 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data,
410                               int *got_frame, AVPacket *avpkt)
411 {
412     const uint8_t *buf = avpkt->data;
413     int buf_size = avpkt->size;
414     DNXHDContext *ctx = avctx->priv_data;
415     AVFrame *picture = data;
416     int first_field = 1;
417     int ret;
418
419     ff_dlog(avctx, "frame size %d\n", buf_size);
420
421 decode_coding_unit:
422     if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
423         return ret;
424
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
432     ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
433     if (ret < 0)
434         return ret;
435
436     if (first_field) {
437         if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
438             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
439             return ret;
440         }
441         picture->pict_type = AV_PICTURE_TYPE_I;
442         picture->key_frame = 1;
443     }
444
445     dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
446
447     if (first_field && picture->interlaced_frame) {
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
454     *got_frame = 1;
455     return buf_size;
456 }
457
458 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
459 {
460     DNXHDContext *ctx = avctx->priv_data;
461
462     ff_free_vlc(&ctx->ac_vlc);
463     ff_free_vlc(&ctx->dc_vlc);
464     ff_free_vlc(&ctx->run_vlc);
465     return 0;
466 }
467
468 AVCodec ff_dnxhd_decoder = {
469     .name           = "dnxhd",
470     .long_name      = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
471     .type           = AVMEDIA_TYPE_VIDEO,
472     .id             = AV_CODEC_ID_DNXHD,
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,
478 };