]> git.sesse.net Git - ffmpeg/blob - libavcodec/dnxhddec.c
e988d68036a81308dc5d8b7892f211e46ebfda62
[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 //#define TRACE
26 //#define DEBUG
27
28 #include "libavutil/imgutils.h"
29 #include "avcodec.h"
30 #include "get_bits.h"
31 #include "dnxhddata.h"
32 #include "dsputil.h"
33 #include "internal.h"
34
35 typedef struct DNXHDContext {
36     AVCodecContext *avctx;
37     AVFrame picture;
38     GetBitContext gb;
39     int 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     DSPContext dsp;
47     DECLARE_ALIGNED(16, DCTELEM, blocks)[8][64];
48     ScanTable scantable;
49     const CIDEntry *cid_table;
50     int bit_depth; // 8, 10 or 0 if not initialized at all.
51     void (*decode_dct_block)(struct DNXHDContext *ctx, DCTELEM *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, DCTELEM *block, int n, int qscale);
59 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, DCTELEM *block, int n, int qscale);
60
61 static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
62 {
63     DNXHDContext *ctx = avctx->priv_data;
64
65     ctx->avctx = avctx;
66     avctx->coded_frame = &ctx->picture;
67     ctx->picture.type = AV_PICTURE_TYPE_I;
68     ctx->picture.key_frame = 1;
69     return 0;
70 }
71
72 static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
73 {
74     if (cid != ctx->cid) {
75         int index;
76
77         if ((index = ff_dnxhd_get_cid_table(cid)) < 0) {
78             av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %d\n", cid);
79             return -1;
80         }
81         ctx->cid_table = &ff_dnxhd_cid_table[index];
82
83         ff_free_vlc(&ctx->ac_vlc);
84         ff_free_vlc(&ctx->dc_vlc);
85         ff_free_vlc(&ctx->run_vlc);
86
87         init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
88                  ctx->cid_table->ac_bits, 1, 1,
89                  ctx->cid_table->ac_codes, 2, 2, 0);
90         init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
91                  ctx->cid_table->dc_bits, 1, 1,
92                  ctx->cid_table->dc_codes, 1, 1, 0);
93         init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
94                  ctx->cid_table->run_bits, 1, 1,
95                  ctx->cid_table->run_codes, 2, 2, 0);
96
97         ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, ff_zigzag_direct);
98         ctx->cid = cid;
99     }
100     return 0;
101 }
102
103 static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
104 {
105     static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
106     int i, cid;
107
108     if (buf_size < 0x280)
109         return -1;
110
111     if (memcmp(buf, header_prefix, 5)) {
112         av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");
113         return -1;
114     }
115     if (buf[5] & 2) { /* interlaced */
116         ctx->cur_field = buf[5] & 1;
117         ctx->picture.interlaced_frame = 1;
118         ctx->picture.top_field_first = first_field ^ ctx->cur_field;
119         av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
120     }
121
122     ctx->height = AV_RB16(buf + 0x18);
123     ctx->width  = AV_RB16(buf + 0x1a);
124
125     av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);
126
127     if (buf[0x21] & 0x40) {
128         ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
129         ctx->avctx->bits_per_raw_sample = 10;
130         if (ctx->bit_depth != 10) {
131             ff_dsputil_init(&ctx->dsp, ctx->avctx);
132             ctx->bit_depth = 10;
133             ctx->decode_dct_block = dnxhd_decode_dct_block_10;
134         }
135     } else {
136         ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
137         ctx->avctx->bits_per_raw_sample = 8;
138         if (ctx->bit_depth != 8) {
139             ff_dsputil_init(&ctx->dsp, ctx->avctx);
140             ctx->bit_depth = 8;
141             ctx->decode_dct_block = dnxhd_decode_dct_block_8;
142         }
143     }
144
145     cid = AV_RB32(buf + 0x28);
146     av_dlog(ctx->avctx, "compression id %d\n", cid);
147
148     if (dnxhd_init_vlc(ctx, cid) < 0)
149         return -1;
150
151     if (buf_size < ctx->cid_table->coding_unit_size) {
152         av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");
153         return -1;
154     }
155
156     ctx->mb_width = ctx->width>>4;
157     ctx->mb_height = buf[0x16d];
158
159     av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
160
161     if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
162         ctx->height <<= 1;
163
164     if (ctx->mb_height > 68 ||
165         (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
166         av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
167         return -1;
168     }
169
170     for (i = 0; i < ctx->mb_height; i++) {
171         ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i<<2));
172         av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);
173         if (buf_size < ctx->mb_scan_index[i] + 0x280) {
174             av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");
175             return -1;
176         }
177     }
178
179     return 0;
180 }
181
182 static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,
183                                                     DCTELEM *block, int n,
184                                                     int qscale,
185                                                     int index_bits,
186                                                     int level_bias,
187                                                     int level_shift)
188 {
189     int i, j, index1, index2, len;
190     int level, component, sign;
191     const uint8_t *weight_matrix;
192     OPEN_READER(bs, &ctx->gb);
193
194     if (n&2) {
195         component = 1 + (n&1);
196         weight_matrix = ctx->cid_table->chroma_weight;
197     } else {
198         component = 0;
199         weight_matrix = ctx->cid_table->luma_weight;
200     }
201
202     UPDATE_CACHE(bs, &ctx->gb);
203     GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
204     if (len) {
205         level = GET_CACHE(bs, &ctx->gb);
206         LAST_SKIP_BITS(bs, &ctx->gb, len);
207         sign  = ~level >> 31;
208         level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
209         ctx->last_dc[component] += level;
210     }
211     block[0] = ctx->last_dc[component];
212
213     for (i = 1; ; i++) {
214         UPDATE_CACHE(bs, &ctx->gb);
215         GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,
216                 DNXHD_VLC_BITS, 2);
217         level = ctx->cid_table->ac_level[index1];
218         if (!level) /* EOB */
219             break;
220
221         sign = SHOW_SBITS(bs, &ctx->gb, 1);
222         SKIP_BITS(bs, &ctx->gb, 1);
223
224         if (ctx->cid_table->ac_index_flag[index1]) {
225             level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6;
226             SKIP_BITS(bs, &ctx->gb, index_bits);
227         }
228
229         if (ctx->cid_table->ac_run_flag[index1]) {
230             UPDATE_CACHE(bs, &ctx->gb);
231             GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,
232                     DNXHD_VLC_BITS, 2);
233             i += ctx->cid_table->run[index2];
234         }
235
236         if (i > 63) {
237             av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
238             break;
239         }
240
241         j = ctx->scantable.permutated[i];
242         level = (2*level+1) * qscale * weight_matrix[i];
243         if (level_bias < 32 || weight_matrix[i] != level_bias)
244             level += level_bias;
245         level >>= level_shift;
246
247         block[j] = (level^sign) - sign;
248     }
249
250     CLOSE_READER(bs, &ctx->gb);
251 }
252
253 static void dnxhd_decode_dct_block_8(DNXHDContext *ctx, DCTELEM *block,
254                                      int n, int qscale)
255 {
256     dnxhd_decode_dct_block(ctx, block, n, qscale, 4, 32, 6);
257 }
258
259 static void dnxhd_decode_dct_block_10(DNXHDContext *ctx, DCTELEM *block,
260                                       int n, int qscale)
261 {
262     dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
263 }
264
265 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
266 {
267     int shift1 = ctx->bit_depth == 10;
268     int dct_linesize_luma   = ctx->picture.linesize[0];
269     int dct_linesize_chroma = ctx->picture.linesize[1];
270     uint8_t *dest_y, *dest_u, *dest_v;
271     int dct_y_offset, dct_x_offset;
272     int qscale, i;
273
274     qscale = get_bits(&ctx->gb, 11);
275     skip_bits1(&ctx->gb);
276
277     for (i = 0; i < 8; i++) {
278         ctx->dsp.clear_block(ctx->blocks[i]);
279         ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
280     }
281
282     if (ctx->picture.interlaced_frame) {
283         dct_linesize_luma   <<= 1;
284         dct_linesize_chroma <<= 1;
285     }
286
287     dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
288     dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
289     dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
290
291     if (ctx->cur_field) {
292         dest_y += ctx->picture.linesize[0];
293         dest_u += ctx->picture.linesize[1];
294         dest_v += ctx->picture.linesize[2];
295     }
296
297     dct_y_offset = dct_linesize_luma << 3;
298     dct_x_offset = 8 << shift1;
299     ctx->dsp.idct_put(dest_y,                               dct_linesize_luma, ctx->blocks[0]);
300     ctx->dsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, ctx->blocks[1]);
301     ctx->dsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, ctx->blocks[4]);
302     ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
303
304     if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
305         dct_y_offset = dct_linesize_chroma << 3;
306         ctx->dsp.idct_put(dest_u,                dct_linesize_chroma, ctx->blocks[2]);
307         ctx->dsp.idct_put(dest_v,                dct_linesize_chroma, ctx->blocks[3]);
308         ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
309         ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
310     }
311
312     return 0;
313 }
314
315 static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
316 {
317     int x, y;
318     for (y = 0; y < ctx->mb_height; y++) {
319         ctx->last_dc[0] =
320         ctx->last_dc[1] =
321         ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
322         init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
323         for (x = 0; x < ctx->mb_width; x++) {
324             //START_TIMER;
325             dnxhd_decode_macroblock(ctx, x, y);
326             //STOP_TIMER("decode macroblock");
327         }
328     }
329     return 0;
330 }
331
332 static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
333                               AVPacket *avpkt)
334 {
335     const uint8_t *buf = avpkt->data;
336     int buf_size = avpkt->size;
337     DNXHDContext *ctx = avctx->priv_data;
338     AVFrame *picture = data;
339     int first_field = 1;
340
341     av_dlog(avctx, "frame size %d\n", buf_size);
342
343  decode_coding_unit:
344     if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
345         return -1;
346
347     if ((avctx->width || avctx->height) &&
348         (ctx->width != avctx->width || ctx->height != avctx->height)) {
349         av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %dx%d\n",
350                avctx->width, avctx->height, ctx->width, ctx->height);
351         first_field = 1;
352     }
353
354     if (av_image_check_size(ctx->width, ctx->height, 0, avctx))
355         return -1;
356     avcodec_set_dimensions(avctx, ctx->width, ctx->height);
357
358     if (first_field) {
359         if (ctx->picture.data[0])
360             avctx->release_buffer(avctx, &ctx->picture);
361         if (ff_get_buffer(avctx, &ctx->picture) < 0) {
362             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
363             return -1;
364         }
365     }
366
367     dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
368
369     if (first_field && ctx->picture.interlaced_frame) {
370         buf      += ctx->cid_table->coding_unit_size;
371         buf_size -= ctx->cid_table->coding_unit_size;
372         first_field = 0;
373         goto decode_coding_unit;
374     }
375
376     *picture = ctx->picture;
377     *data_size = sizeof(AVPicture);
378     return buf_size;
379 }
380
381 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
382 {
383     DNXHDContext *ctx = avctx->priv_data;
384
385     if (ctx->picture.data[0])
386         avctx->release_buffer(avctx, &ctx->picture);
387     ff_free_vlc(&ctx->ac_vlc);
388     ff_free_vlc(&ctx->dc_vlc);
389     ff_free_vlc(&ctx->run_vlc);
390     return 0;
391 }
392
393 AVCodec ff_dnxhd_decoder = {
394     .name           = "dnxhd",
395     .type           = AVMEDIA_TYPE_VIDEO,
396     .id             = AV_CODEC_ID_DNXHD,
397     .priv_data_size = sizeof(DNXHDContext),
398     .init           = dnxhd_decode_init,
399     .close          = dnxhd_decode_close,
400     .decode         = dnxhd_decode_frame,
401     .capabilities   = CODEC_CAP_DR1,
402     .long_name      = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
403 };