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.

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