FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dvdec.c
Go to the documentation of this file.
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 FFmpeg.
17  *
18  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/avassert.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/internal.h"
41 #include "libavutil/pixdesc.h"
42 
43 #include "avcodec.h"
44 #include "dv.h"
45 #include "dv_profile_internal.h"
46 #include "dvdata.h"
47 #include "get_bits.h"
48 #include "idctdsp.h"
49 #include "internal.h"
50 #include "put_bits.h"
51 #include "simple_idct.h"
52 
53 typedef struct BlockInfo {
54  const uint32_t *factor_table;
56  uint8_t pos; /* position in block */
57  void (*idct_put)(uint8_t *dest, int line_size, int16_t *block);
61 } BlockInfo;
62 
63 static const int dv_iweight_bits = 14;
64 
66 {
67  DVVideoContext *s = avctx->priv_data;
68  IDCTDSPContext idsp;
69  int i;
70 
71  memset(&idsp,0, sizeof(idsp));
72  ff_idctdsp_init(&idsp, avctx);
73 
74  for (i = 0; i < 64; i++)
75  s->dv_zigzag[0][i] = idsp.idct_permutation[ff_zigzag_direct[i]];
76 
77  if (avctx->lowres){
78  for (i = 0; i < 64; i++){
79  int j = ff_dv_zigzag248_direct[i];
80  s->dv_zigzag[1][i] = idsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2];
81  }
82  }else
83  memcpy(s->dv_zigzag[1], ff_dv_zigzag248_direct, sizeof(s->dv_zigzag[1]));
84 
85  s->idct_put[0] = idsp.idct_put;
87 
88  return ff_dvvideo_init(avctx);
89 }
90 
91 /* decode AC coefficients */
92 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, int16_t *block)
93 {
94  int last_index = gb->size_in_bits;
95  const uint8_t *scan_table = mb->scan_table;
96  const uint32_t *factor_table = mb->factor_table;
97  int pos = mb->pos;
98  int partial_bit_count = mb->partial_bit_count;
99  int level, run, vlc_len, index;
100 
101  OPEN_READER_NOSIZE(re, gb);
102  UPDATE_CACHE(re, gb);
103 
104  /* if we must parse a partial VLC, we do it here */
105  if (partial_bit_count > 0) {
106  re_cache = re_cache >> partial_bit_count |
107  mb->partial_bit_buffer;
108  re_index -= partial_bit_count;
109  mb->partial_bit_count = 0;
110  }
111 
112  /* get the AC coefficients until last_index is reached */
113  for (;;) {
114  av_dlog(NULL, "%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16),
115  re_index);
116  /* our own optimized GET_RL_VLC */
117  index = NEG_USR32(re_cache, TEX_VLC_BITS);
118  vlc_len = ff_dv_rl_vlc[index].len;
119  if (vlc_len < 0) {
120  index = NEG_USR32((unsigned) re_cache << TEX_VLC_BITS, -vlc_len) +
122  vlc_len = TEX_VLC_BITS - vlc_len;
123  }
124  level = ff_dv_rl_vlc[index].level;
125  run = ff_dv_rl_vlc[index].run;
126 
127  /* gotta check if we're still within gb boundaries */
128  if (re_index + vlc_len > last_index) {
129  /* should be < 16 bits otherwise a codeword could have been parsed */
130  mb->partial_bit_count = last_index - re_index;
131  mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
132  re_index = last_index;
133  break;
134  }
135  re_index += vlc_len;
136 
137  av_dlog(NULL, "run=%d level=%d\n", run, level);
138  pos += run;
139  if (pos >= 64)
140  break;
141 
142  level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >>
144  block[scan_table[pos]] = level;
145 
146  UPDATE_CACHE(re, gb);
147  }
148  CLOSE_READER(re, gb);
149  mb->pos = pos;
150 }
151 
152 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
153 {
154  int bits_left = get_bits_left(gb);
155  while (bits_left >= MIN_CACHE_BITS) {
157  bits_left -= MIN_CACHE_BITS;
158  }
159  if (bits_left > 0)
160  put_bits(pb, bits_left, get_bits(gb, bits_left));
161 }
162 
163 /* mb_x and mb_y are in units of 8 pixels */
164 static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
165 {
166  DVVideoContext *s = avctx->priv_data;
167  DVwork_chunk *work_chunk = arg;
168  int quant, dc, dct_mode, class1, j;
169  int mb_index, mb_x, mb_y, last_index;
170  int y_stride, linesize;
171  int16_t *block, *block1;
172  int c_offset;
173  uint8_t *y_ptr;
174  const uint8_t *buf_ptr;
175  PutBitContext pb, vs_pb;
176  GetBitContext gb;
177  BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
178  LOCAL_ALIGNED_16(int16_t, sblock, [5 * DV_MAX_BPM], [64]);
179  LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [80 + FF_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
180  LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [80 * 5 + FF_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
181  const int log2_blocksize = 3-s->avctx->lowres;
182  int is_field_mode[5];
183 
184  av_assert1((((int) mb_bit_buffer) & 7) == 0);
185  av_assert1((((int) vs_bit_buffer) & 7) == 0);
186 
187  memset(sblock, 0, 5 * DV_MAX_BPM * sizeof(*sblock));
188 
189  /* pass 1: read DC and AC coefficients in blocks */
190  buf_ptr = &s->buf[work_chunk->buf_offset * 80];
191  block1 = &sblock[0][0];
192  mb1 = mb_data;
193  init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
194  for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
195  /* skip header */
196  quant = buf_ptr[3] & 0x0f;
197  buf_ptr += 4;
198  init_put_bits(&pb, mb_bit_buffer, 80);
199  mb = mb1;
200  block = block1;
201  is_field_mode[mb_index] = 0;
202  for (j = 0; j < s->sys->bpm; j++) {
203  last_index = s->sys->block_sizes[j];
204  init_get_bits(&gb, buf_ptr, last_index);
205 
206  /* get the DC */
207  dc = get_sbits(&gb, 9);
208  dct_mode = get_bits1(&gb);
209  class1 = get_bits(&gb, 2);
210  if (DV_PROFILE_IS_HD(s->sys)) {
211  mb->idct_put = s->idct_put[0];
212  mb->scan_table = s->dv_zigzag[0];
213  mb->factor_table = &s->idct_factor[(j >= 4) * 4 * 16 * 64 +
214  class1 * 16 * 64 +
215  quant * 64];
216  is_field_mode[mb_index] |= !j && dct_mode;
217  } else {
218  mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
219  mb->scan_table = s->dv_zigzag[dct_mode];
220  mb->factor_table =
221  &s->idct_factor[(class1 == 3) * 2 * 22 * 64 +
222  dct_mode * 22 * 64 +
223  (quant + ff_dv_quant_offset[class1]) * 64];
224  }
225  dc = dc << 2;
226  /* convert to unsigned because 128 is not added in the
227  * standard IDCT */
228  dc += 1024;
229  block[0] = dc;
230  buf_ptr += last_index >> 3;
231  mb->pos = 0;
232  mb->partial_bit_count = 0;
233 
234  av_dlog(avctx, "MB block: %d, %d ", mb_index, j);
235  dv_decode_ac(&gb, mb, block);
236 
237  /* write the remaining bits in a new buffer only if the
238  * block is finished */
239  if (mb->pos >= 64)
240  bit_copy(&pb, &gb);
241 
242  block += 64;
243  mb++;
244  }
245 
246  /* pass 2: we can do it just after */
247  av_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
248  block = block1;
249  mb = mb1;
250  init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
251  put_bits32(&pb, 0); // padding must be zeroed
252  flush_put_bits(&pb);
253  for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
254  if (mb->pos < 64 && get_bits_left(&gb) > 0) {
255  dv_decode_ac(&gb, mb, block);
256  /* if still not finished, no need to parse other blocks */
257  if (mb->pos < 64)
258  break;
259  }
260  }
261  /* all blocks are finished, so the extra bytes can be used at
262  * the video segment level */
263  if (j >= s->sys->bpm)
264  bit_copy(&vs_pb, &gb);
265  }
266 
267  /* we need a pass over the whole video segment */
268  av_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
269  block = &sblock[0][0];
270  mb = mb_data;
271  init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
272  put_bits32(&vs_pb, 0); // padding must be zeroed
273  flush_put_bits(&vs_pb);
274  for (mb_index = 0; mb_index < 5; mb_index++) {
275  for (j = 0; j < s->sys->bpm; j++) {
276  if (mb->pos < 64 && get_bits_left(&gb) > 0) {
277  av_dlog(avctx, "start %d:%d\n", mb_index, j);
278  dv_decode_ac(&gb, mb, block);
279  }
280  if (mb->pos >= 64 && mb->pos < 127)
281  av_log(avctx, AV_LOG_ERROR,
282  "AC EOB marker is absent pos=%d\n", mb->pos);
283  block += 64;
284  mb++;
285  }
286  }
287 
288  /* compute idct and place blocks */
289  block = &sblock[0][0];
290  mb = mb_data;
291  for (mb_index = 0; mb_index < 5; mb_index++) {
292  dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
293 
294  /* idct_put'ting luminance */
295  if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
296  (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
297  (s->sys->height >= 720 && mb_y != 134)) {
298  y_stride = (s->frame->linesize[0] <<
299  ((!is_field_mode[mb_index]) * log2_blocksize));
300  } else {
301  y_stride = (2 << log2_blocksize);
302  }
303  y_ptr = s->frame->data[0] +
304  ((mb_y * s->frame->linesize[0] + mb_x) << log2_blocksize);
305  linesize = s->frame->linesize[0] << is_field_mode[mb_index];
306  mb[0].idct_put(y_ptr, linesize, block + 0 * 64);
307  if (s->sys->video_stype == 4) { /* SD 422 */
308  mb[2].idct_put(y_ptr + (1 << log2_blocksize), linesize, block + 2 * 64);
309  } else {
310  mb[1].idct_put(y_ptr + (1 << log2_blocksize), linesize, block + 1 * 64);
311  mb[2].idct_put(y_ptr + y_stride, linesize, block + 2 * 64);
312  mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3 * 64);
313  }
314  mb += 4;
315  block += 4 * 64;
316 
317  /* idct_put'ting chrominance */
318  c_offset = (((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
319  (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
320  for (j = 2; j; j--) {
321  uint8_t *c_ptr = s->frame->data[j] + c_offset;
322  if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
323  uint64_t aligned_pixels[64 / 8];
324  uint8_t *pixels = (uint8_t *) aligned_pixels;
325  uint8_t *c_ptr1, *ptr1;
326  int x, y;
327  mb->idct_put(pixels, 8, block);
328  for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->frame->linesize[j], pixels += 8) {
329  ptr1 = pixels + ((1 << (log2_blocksize))>>1);
330  c_ptr1 = c_ptr + (s->frame->linesize[j] << log2_blocksize);
331  for (x = 0; x < (1 << FFMAX(log2_blocksize - 1, 0)); x++) {
332  c_ptr[x] = pixels[x];
333  c_ptr1[x] = ptr1[x];
334  }
335  }
336  block += 64;
337  mb++;
338  } else {
339  y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
340  s->frame->linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
341  linesize = s->frame->linesize[j] << is_field_mode[mb_index];
342  (mb++)->idct_put(c_ptr, linesize, block);
343  block += 64;
344  if (s->sys->bpm == 8) {
345  (mb++)->idct_put(c_ptr + y_stride, linesize, block);
346  block += 64;
347  }
348  }
349  }
350  }
351  return 0;
352 }
353 
354 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
355  * 144000 bytes for PAL - or twice those for 50Mbps) */
356 static int dvvideo_decode_frame(AVCodecContext *avctx, void *data,
357  int *got_frame, AVPacket *avpkt)
358 {
359  uint8_t *buf = avpkt->data;
360  int buf_size = avpkt->size;
361  DVVideoContext *s = avctx->priv_data;
362  const uint8_t *vsc_pack;
363  int apt, is16_9, ret;
364  const AVDVProfile *sys;
365 
366  sys = ff_dv_frame_profile(avctx, s->sys, buf, buf_size);
367  if (!sys || buf_size < sys->frame_size) {
368  av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
369  return -1; /* NOTE: we only accept several full frames */
370  }
371 
372  if (sys != s->sys) {
373  ret = ff_dv_init_dynamic_tables(s, sys);
374  if (ret < 0) {
375  av_log(avctx, AV_LOG_ERROR, "Error initializing the work tables.\n");
376  return ret;
377  }
378  s->sys = sys;
379  }
380 
381  s->frame = data;
382  s->frame->key_frame = 1;
384  avctx->pix_fmt = s->sys->pix_fmt;
385  avctx->framerate = av_inv_q(s->sys->time_base);
386 
387  ret = ff_set_dimensions(avctx, s->sys->width, s->sys->height);
388  if (ret < 0)
389  return ret;
390 
391  /* Determine the codec's sample_aspect ratio from the packet */
392  vsc_pack = buf + 80 * 5 + 48 + 5;
393  if (*vsc_pack == dv_video_control) {
394  apt = buf[4] & 0x07;
395  is16_9 = (vsc_pack[2] & 0x07) == 0x02 ||
396  (!apt && (vsc_pack[2] & 0x07) == 0x07);
397  ff_set_sar(avctx, s->sys->sar[is16_9]);
398  }
399 
400  if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
401  return ret;
402  s->frame->interlaced_frame = 1;
403  s->frame->top_field_first = 0;
404 
405  /* Determine the codec's field order from the packet */
406  if ( *vsc_pack == dv_video_control ) {
407  s->frame->top_field_first = !(vsc_pack[3] & 0x40);
408  }
409 
410  s->buf = buf;
411  avctx->execute(avctx, dv_decode_video_segment, s->work_chunks, NULL,
412  dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
413 
414  emms_c();
415 
416  /* return image */
417  *got_frame = 1;
418 
419  return s->sys->frame_size;
420 }
421 
423  .name = "dvvideo",
424  .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
425  .type = AVMEDIA_TYPE_VIDEO,
426  .id = AV_CODEC_ID_DVVIDEO,
427  .priv_data_size = sizeof(DVVideoContext),
430  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
431  .max_lowres = 3,
432 };