]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdec.c
libopenh264dec: Export the decoded profile and level in AVCodecContext
[ffmpeg] / libavcodec / dvdec.c
1 /*
2  * DV decoder
3  * Copyright (c) 2002 Fabrice Bellard
4  * Copyright (c) 2004 Roman Shaposhnik
5  *
6  * 50 Mbps (DVCPRO50) support
7  * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
8  *
9  * 100 Mbps (DVCPRO HD) support
10  * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D)
11  * Final code by Roman Shaposhnik
12  *
13  * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
14  * of DV technical info.
15  *
16  * This file is part of Libav.
17  *
18  * Libav is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * Libav is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public
29  * License along with Libav; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31  */
32
33 /**
34  * @file
35  * DV decoder
36  */
37
38 #include "libavutil/imgutils.h"
39 #include "libavutil/internal.h"
40 #include "libavutil/pixdesc.h"
41
42 #include "avcodec.h"
43 #include "bitstream.h"
44 #include "dv.h"
45 #include "dvdata.h"
46 #include "idctdsp.h"
47 #include "internal.h"
48 #include "put_bits.h"
49 #include "simple_idct.h"
50
51 typedef struct BlockInfo {
52     const uint32_t *factor_table;
53     const uint8_t *scan_table;
54     uint8_t pos; /* position in block */
55     void (*idct_put)(uint8_t *dest, ptrdiff_t stride, int16_t *block);
56     uint8_t partial_bit_count;
57     uint32_t partial_bit_buffer;
58     int shift_offset;
59 } BlockInfo;
60
61 static const int dv_iweight_bits = 14;
62
63 static av_cold int dvvideo_decode_init(AVCodecContext *avctx)
64 {
65     DVVideoContext *s = avctx->priv_data;
66     IDCTDSPContext idsp;
67     int i;
68
69     ff_idctdsp_init(&idsp, avctx);
70
71     for (i = 0; i < 64; i++)
72         s->dv_zigzag[0][i] = idsp.idct_permutation[ff_zigzag_direct[i]];
73
74     memcpy(s->dv_zigzag[1], ff_dv_zigzag248_direct, sizeof(s->dv_zigzag[1]));
75
76     s->idct_put[0] = idsp.idct_put;
77     s->idct_put[1] = ff_simple_idct248_put;
78
79     return ff_dvvideo_init(avctx);
80 }
81
82 /* decode AC coefficients */
83 static void dv_decode_ac(BitstreamContext *bc, BlockInfo *mb, int16_t *block)
84 {
85     const uint8_t  *scan_table   = mb->scan_table;
86     const uint32_t *factor_table = mb->factor_table;
87     int pos                      = mb->pos;
88     int partial_bit_count        = mb->partial_bit_count;
89     int level, run;
90
91     /* if we must parse a partial VLC, we do it here */
92     if (partial_bit_count > 0) {
93         bitstream_unget(bc, mb->partial_bit_buffer, partial_bit_count);
94         mb->partial_bit_count = 0;
95     }
96
97     /* get the AC coefficients until last_index is reached */
98     for (;;) {
99         BitstreamContext tmp = *bc;
100
101         ff_dlog(NULL, "%2d: bits=%04x index=%d\n",
102                 pos, bitstream_peek(bc, 16), bitstream_tell(bc));
103
104         BITSTREAM_RL_VLC(level, run, bc, ff_dv_rl_vlc, TEX_VLC_BITS, 2);
105
106         if (bitstream_bits_left(bc) < 0) {
107             mb->partial_bit_count  = bitstream_bits_left(&tmp);
108             mb->partial_bit_buffer = bitstream_peek(&tmp, mb->partial_bit_count);
109             break;
110         }
111         ff_dlog(NULL, "run=%d level=%d\n", run, level);
112         pos += run;
113         if (pos >= 64)
114             break;
115
116         level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >>
117                 dv_iweight_bits;
118         block[scan_table[pos]] = level;
119     }
120     mb->pos = pos;
121 }
122
123 static inline void bit_copy(PutBitContext *pb, BitstreamContext *bc)
124 {
125     int bits_left = bitstream_bits_left(bc);
126
127     while (bits_left >= 32) {
128         int read = bitstream_read(bc, 32);
129         put_bits32(pb, read);
130         bits_left -= 32;
131     }
132
133     if (bits_left > 0)
134         put_bits(pb, bits_left, bitstream_read(bc, bits_left));
135 }
136
137 /* mb_x and mb_y are in units of 8 pixels */
138 static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
139 {
140     DVVideoContext *s = avctx->priv_data;
141     DVwork_chunk *work_chunk = arg;
142     int quant, dc, dct_mode, class1, j;
143     int mb_index, mb_x, mb_y, last_index;
144     int y_stride, linesize;
145     int16_t *block, *block1;
146     int c_offset;
147     uint8_t *y_ptr;
148     const uint8_t *buf_ptr;
149     PutBitContext pb, vs_pb;
150     BitstreamContext bc;
151     BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
152     LOCAL_ALIGNED_16(int16_t, sblock, [5 * DV_MAX_BPM], [64]);
153     LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [80     + AV_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
154     LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [80 * 5 + AV_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
155     const int log2_blocksize = 3;
156     int is_field_mode[5];
157     int mb_bits;
158
159     assert((((int) mb_bit_buffer) & 7) == 0);
160     assert((((int) vs_bit_buffer) & 7) == 0);
161
162     memset(sblock, 0, 5 * DV_MAX_BPM * sizeof(*sblock));
163
164     /* pass 1: read DC and AC coefficients in blocks */
165     buf_ptr = &s->buf[work_chunk->buf_offset * 80];
166     block1  = &sblock[0][0];
167     mb1     = mb_data;
168     init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
169     for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
170         /* skip header */
171         quant    = buf_ptr[3] & 0x0f;
172         buf_ptr += 4;
173         init_put_bits(&pb, mb_bit_buffer, 80);
174         mb    = mb1;
175         block = block1;
176         is_field_mode[mb_index] = 0;
177         for (j = 0; j < s->sys->bpm; j++) {
178             last_index = s->sys->block_sizes[j];
179             bitstream_init(&bc, buf_ptr, last_index);
180
181             /* get the DC */
182             dc       = bitstream_read_signed(&bc, 9);
183             dct_mode = bitstream_read_bit(&bc);
184             class1   = bitstream_read(&bc, 2);
185             if (DV_PROFILE_IS_HD(s->sys)) {
186                 mb->idct_put     = s->idct_put[0];
187                 mb->scan_table   = s->dv_zigzag[0];
188                 mb->factor_table = &s->idct_factor[(j >= 4) * 4 * 16 * 64 +
189                                                    class1       * 16 * 64 +
190                                                    quant             * 64];
191                 is_field_mode[mb_index] |= !j && dct_mode;
192             } else {
193                 mb->idct_put     = s->idct_put[dct_mode && log2_blocksize == 3];
194                 mb->scan_table   = s->dv_zigzag[dct_mode];
195                 mb->factor_table =
196                     &s->idct_factor[(class1 == 3)               * 2 * 22 * 64 +
197                                     dct_mode                        * 22 * 64 +
198                                     (quant + ff_dv_quant_offset[class1]) * 64];
199             }
200             dc = dc << 2;
201             /* convert to unsigned because 128 is not added in the
202              * standard IDCT */
203             dc                   += 1024;
204             block[0]              = dc;
205             buf_ptr              += last_index >> 3;
206             mb->pos               = 0;
207             mb->partial_bit_count = 0;
208
209             ff_dlog(avctx, "MB block: %d, %d ", mb_index, j);
210             dv_decode_ac(&bc, mb, block);
211
212             /* write the remaining bits in a new buffer only if the
213              * block is finished */
214             if (mb->pos >= 64)
215                 bit_copy(&pb, &bc);
216
217             block += 64;
218             mb++;
219         }
220
221         /* pass 2: we can do it just after */
222         ff_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
223         block = block1;
224         mb    = mb1;
225
226         mb_bits = put_bits_count(&pb);
227         put_bits32(&pb, 0); // padding must be zeroed
228         flush_put_bits(&pb);
229         bitstream_init(&bc, mb_bit_buffer, mb_bits);
230
231         for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
232             if (mb->pos < 64 && bitstream_bits_left(&bc) > 0) {
233                 dv_decode_ac(&bc, mb, block);
234                 /* if still not finished, no need to parse other blocks */
235                 if (mb->pos < 64)
236                     break;
237             }
238         }
239         /* all blocks are finished, so the extra bytes can be used at
240          * the video segment level */
241         if (j >= s->sys->bpm)
242             bit_copy(&vs_pb, &bc);
243     }
244
245     /* we need a pass over the whole video segment */
246     ff_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
247     block = &sblock[0][0];
248     mb    = mb_data;
249     mb_bits = put_bits_count(&vs_pb);
250     put_bits32(&vs_pb, 0); // padding must be zeroed
251     flush_put_bits(&vs_pb);
252     bitstream_init(&bc, vs_bit_buffer, mb_bits);
253     for (mb_index = 0; mb_index < 5; mb_index++) {
254         for (j = 0; j < s->sys->bpm; j++) {
255             if (mb->pos < 64) {
256                 ff_dlog(avctx, "start %d:%d\n", mb_index, j);
257                 dv_decode_ac(&bc, mb, block);
258             }
259             if (mb->pos >= 64 && mb->pos < 127)
260                 av_log(avctx, AV_LOG_ERROR,
261                        "AC EOB marker is absent pos=%"PRIu8"\n", mb->pos);
262             block += 64;
263             mb++;
264         }
265     }
266
267     /* compute idct and place blocks */
268     block = &sblock[0][0];
269     mb    = mb_data;
270     for (mb_index = 0; mb_index < 5; mb_index++) {
271         dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
272
273         /* idct_put'ting luminance */
274         if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P)                      ||
275             (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
276             (s->sys->height >= 720 && mb_y != 134)) {
277             y_stride = (s->frame->linesize[0] <<
278                         ((!is_field_mode[mb_index]) * log2_blocksize));
279         } else {
280             y_stride = (2 << log2_blocksize);
281         }
282         y_ptr    = s->frame->data[0] +
283                    ((mb_y * s->frame->linesize[0] + mb_x) << log2_blocksize);
284         linesize = s->frame->linesize[0] << is_field_mode[mb_index];
285         mb[0].idct_put(y_ptr, linesize, block + 0 * 64);
286         if (s->sys->video_stype == 4) { /* SD 422 */
287             mb[2].idct_put(y_ptr + (1 << log2_blocksize),            linesize, block + 2 * 64);
288         } else {
289             mb[1].idct_put(y_ptr + (1 << log2_blocksize),            linesize, block + 1 * 64);
290             mb[2].idct_put(y_ptr                         + y_stride, linesize, block + 2 * 64);
291             mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3 * 64);
292         }
293         mb    += 4;
294         block += 4 * 64;
295
296         /* idct_put'ting chrominance */
297         c_offset = (((mb_y >>  (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
298                      (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
299         for (j = 2; j; j--) {
300             uint8_t *c_ptr = s->frame->data[j] + c_offset;
301             if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
302                 uint64_t aligned_pixels[64 / 8];
303                 uint8_t *pixels = (uint8_t *) aligned_pixels;
304                 uint8_t *c_ptr1, *ptr1;
305                 int x, y;
306                 mb->idct_put(pixels, 8, block);
307                 for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->frame->linesize[j], pixels += 8) {
308                     ptr1   = pixels + (1 << (log2_blocksize - 1));
309                     c_ptr1 = c_ptr + (s->frame->linesize[j] << log2_blocksize);
310                     for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
311                         c_ptr[x]  = pixels[x];
312                         c_ptr1[x] = ptr1[x];
313                     }
314                 }
315                 block += 64;
316                 mb++;
317             } else {
318                 y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
319                            s->frame->linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
320                 linesize = s->frame->linesize[j] << is_field_mode[mb_index];
321                 (mb++)->idct_put(c_ptr, linesize, block);
322                 block += 64;
323                 if (s->sys->bpm == 8) {
324                     (mb++)->idct_put(c_ptr + y_stride, linesize, block);
325                     block += 64;
326                 }
327             }
328         }
329     }
330     return 0;
331 }
332
333 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
334  * 144000 bytes for PAL - or twice those for 50Mbps) */
335 static int dvvideo_decode_frame(AVCodecContext *avctx, void *data,
336                                 int *got_frame, AVPacket *avpkt)
337 {
338     uint8_t *buf = avpkt->data;
339     int buf_size = avpkt->size;
340     DVVideoContext *s = avctx->priv_data;
341     AVFrame *frame = data;
342     const uint8_t *vsc_pack;
343     int apt, is16_9, ret;
344     const AVDVProfile *sys;
345
346     sys = av_dv_frame_profile(s->sys, buf, buf_size);
347     if (!sys || buf_size < sys->frame_size) {
348         av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
349         return -1; /* NOTE: we only accept several full frames */
350     }
351
352     if (sys != s->sys) {
353         ret = ff_dv_init_dynamic_tables(s, sys);
354         if (ret < 0) {
355             av_log(avctx, AV_LOG_ERROR, "Error initializing the work tables.\n");
356             return ret;
357         }
358         s->sys = sys;
359     }
360
361     s->frame            = frame;
362     frame->key_frame    = 1;
363     frame->pict_type    = AV_PICTURE_TYPE_I;
364     avctx->pix_fmt      = s->sys->pix_fmt;
365     avctx->framerate    = av_inv_q(s->sys->time_base);
366
367     ret = ff_set_dimensions(avctx, s->sys->width, s->sys->height);
368     if (ret < 0)
369         return ret;
370
371     /* Determine the codec's sample_aspect ratio from the packet */
372     vsc_pack = buf + 80 * 5 + 48 + 5;
373     if (*vsc_pack == dv_video_control) {
374         apt    = buf[4] & 0x07;
375         is16_9 = (vsc_pack[2] & 0x07) == 0x02 ||
376                  (!apt && (vsc_pack[2] & 0x07) == 0x07);
377         ff_set_sar(avctx, s->sys->sar[is16_9]);
378     }
379
380     if (ff_get_buffer(avctx, frame, 0) < 0) {
381         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
382         return -1;
383     }
384     frame->interlaced_frame = 1;
385     frame->top_field_first  = 0;
386
387     s->buf = buf;
388     avctx->execute(avctx, dv_decode_video_segment, s->work_chunks, NULL,
389                    dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
390
391     emms_c();
392
393     /* return image */
394     *got_frame = 1;
395
396     return s->sys->frame_size;
397 }
398
399 AVCodec ff_dvvideo_decoder = {
400     .name           = "dvvideo",
401     .long_name      = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
402     .type           = AVMEDIA_TYPE_VIDEO,
403     .id             = AV_CODEC_ID_DVVIDEO,
404     .priv_data_size = sizeof(DVVideoContext),
405     .init           = dvvideo_decode_init,
406     .decode         = dvvideo_decode_frame,
407     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
408 };