You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
14KB

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