3 * Copyright (c) 2002 Fabrice Bellard
4 * Copyright (c) 2004 Roman Shaposhnik
6 * 50 Mbps (DVCPRO50) support
7 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
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
13 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
14 * of DV technical info.
16 * This file is part of Libav.
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.
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.
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
38 #include "libavutil/imgutils.h"
39 #include "libavutil/internal.h"
40 #include "libavutil/pixdesc.h"
43 #include "bitstream.h"
49 #include "simple_idct.h"
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;
61 static const int dv_iweight_bits = 14;
63 static av_cold int dvvideo_decode_init(AVCodecContext *avctx)
65 DVVideoContext *s = avctx->priv_data;
69 ff_idctdsp_init(&idsp, avctx);
71 for (i = 0; i < 64; i++)
72 s->dv_zigzag[0][i] = idsp.idct_permutation[ff_zigzag_direct[i]];
74 memcpy(s->dv_zigzag[1], ff_dv_zigzag248_direct, sizeof(s->dv_zigzag[1]));
76 s->idct_put[0] = idsp.idct_put;
77 s->idct_put[1] = ff_simple_idct248_put;
79 return ff_dvvideo_init(avctx);
82 /* decode AC coefficients */
83 static void dv_decode_ac(BitstreamContext *bc, BlockInfo *mb, int16_t *block)
85 const uint8_t *scan_table = mb->scan_table;
86 const uint32_t *factor_table = mb->factor_table;
88 int partial_bit_count = mb->partial_bit_count;
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;
97 /* get the AC coefficients until last_index is reached */
99 BitstreamContext tmp = *bc;
101 ff_dlog(NULL, "%2d: bits=%04x index=%d\n",
102 pos, bitstream_peek(bc, 16), bitstream_tell(bc));
104 BITSTREAM_RL_VLC(level, run, bc, ff_dv_rl_vlc, TEX_VLC_BITS, 2);
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);
111 ff_dlog(NULL, "run=%d level=%d\n", run, level);
116 level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >>
118 block[scan_table[pos]] = level;
123 static inline void bit_copy(PutBitContext *pb, BitstreamContext *bc)
125 int bits_left = bitstream_bits_left(bc);
127 while (bits_left >= 32) {
128 int read = bitstream_read(bc, 32);
129 put_bits32(pb, read);
134 put_bits(pb, bits_left, bitstream_read(bc, bits_left));
137 /* mb_x and mb_y are in units of 8 pixels */
138 static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
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;
148 const uint8_t *buf_ptr;
149 PutBitContext pb, vs_pb;
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];
159 assert((((int) mb_bit_buffer) & 7) == 0);
160 assert((((int) vs_bit_buffer) & 7) == 0);
162 memset(sblock, 0, 5 * DV_MAX_BPM * sizeof(*sblock));
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];
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) {
171 quant = buf_ptr[3] & 0x0f;
173 init_put_bits(&pb, mb_bit_buffer, 80);
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);
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 +
191 is_field_mode[mb_index] |= !j && dct_mode;
193 mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
194 mb->scan_table = s->dv_zigzag[dct_mode];
196 &s->idct_factor[(class1 == 3) * 2 * 22 * 64 +
198 (quant + ff_dv_quant_offset[class1]) * 64];
201 /* convert to unsigned because 128 is not added in the
205 buf_ptr += last_index >> 3;
207 mb->partial_bit_count = 0;
209 ff_dlog(avctx, "MB block: %d, %d ", mb_index, j);
210 dv_decode_ac(&bc, mb, block);
212 /* write the remaining bits in a new buffer only if the
213 * block is finished */
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);
226 mb_bits = put_bits_count(&pb);
227 put_bits32(&pb, 0); // padding must be zeroed
229 bitstream_init(&bc, mb_bit_buffer, mb_bits);
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 */
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);
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];
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++) {
256 ff_dlog(avctx, "start %d:%d\n", mb_index, j);
257 dv_decode_ac(&bc, mb, block);
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);
267 /* compute idct and place blocks */
268 block = &sblock[0][0];
270 for (mb_index = 0; mb_index < 5; mb_index++) {
271 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
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));
280 y_stride = (2 << log2_blocksize);
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);
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);
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;
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];
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);
323 if (s->sys->bpm == 8) {
324 (mb++)->idct_put(c_ptr + y_stride, linesize, block);
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)
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;
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 */
353 ret = ff_dv_init_dynamic_tables(s, sys);
355 av_log(avctx, AV_LOG_ERROR, "Error initializing the work tables.\n");
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);
367 ret = ff_set_dimensions(avctx, s->sys->width, s->sys->height);
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) {
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]);
380 if (ff_get_buffer(avctx, frame, 0) < 0) {
381 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
384 frame->interlaced_frame = 1;
385 frame->top_field_first = 0;
388 avctx->execute(avctx, dv_decode_video_segment, s->work_chunks, NULL,
389 dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
396 return s->sys->frame_size;
399 AVCodec ff_dvvideo_decoder = {
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,