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.

572 lines
18KB

  1. /*
  2. * DV decoder
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file dv.c
  21. * DV decoder.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. #include "simple_idct.h"
  27. #include "dvdata.h"
  28. typedef struct DVVideoDecodeContext {
  29. const DVprofile* sys;
  30. GetBitContext gb;
  31. AVFrame picture;
  32. DCTELEM block[5*6][64] __align8;
  33. /* FIXME: the following is extracted from DSP */
  34. uint8_t dv_zigzag[2][64];
  35. uint8_t idct_permutation[64];
  36. void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
  37. void (*fdct)(DCTELEM *block);
  38. /* XXX: move it to static storage ? */
  39. uint8_t dv_shift[2][22][64];
  40. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  41. } DVVideoDecodeContext;
  42. #define TEX_VLC_BITS 9
  43. /* XXX: also include quantization */
  44. static RL_VLC_ELEM *dv_rl_vlc[1];
  45. static VLC_TYPE dv_vlc_codes[15][23];
  46. static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
  47. {
  48. int i, q, j;
  49. /* NOTE: max left shift is 6 */
  50. for(q = 0; q < 22; q++) {
  51. /* 88 unquant */
  52. for(i = 1; i < 64; i++) {
  53. /* 88 table */
  54. j = s->idct_permutation[i];
  55. s->dv_shift[0][q][j] =
  56. dv_quant_shifts[q][dv_88_areas[i]] + 1;
  57. }
  58. /* 248 unquant */
  59. for(i = 1; i < 64; i++) {
  60. /* 248 table */
  61. s->dv_shift[1][q][i] =
  62. dv_quant_shifts[q][dv_248_areas[i]] + 1;
  63. }
  64. }
  65. }
  66. static int dvvideo_decode_init(AVCodecContext *avctx)
  67. {
  68. DVVideoDecodeContext *s = avctx->priv_data;
  69. MpegEncContext s2;
  70. static int done=0;
  71. if (!done) {
  72. int i;
  73. VLC dv_vlc;
  74. done = 1;
  75. /* NOTE: as a trick, we use the fact the no codes are unused
  76. to accelerate the parsing of partial codes */
  77. init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
  78. dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
  79. dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  80. for(i = 0; i < dv_vlc.table_size; i++){
  81. int code= dv_vlc.table[i][0];
  82. int len = dv_vlc.table[i][1];
  83. int level, run;
  84. if(len<0){ //more bits needed
  85. run= 0;
  86. level= code;
  87. } else if (code == (NB_DV_VLC - 1)) {
  88. /* EOB */
  89. run = 0;
  90. level = 256;
  91. } else {
  92. run= dv_vlc_run[code] + 1;
  93. level= dv_vlc_level[code];
  94. }
  95. dv_rl_vlc[0][i].len = len;
  96. dv_rl_vlc[0][i].level = level;
  97. dv_rl_vlc[0][i].run = run;
  98. }
  99. memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
  100. for (i = 0; i < NB_DV_VLC - 1; i++) {
  101. if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
  102. dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
  103. }
  104. }
  105. /* ugly way to get the idct & scantable */
  106. /* XXX: fix it */
  107. memset(&s2, 0, sizeof(MpegEncContext));
  108. s2.avctx = avctx;
  109. dsputil_init(&s2.dsp, avctx);
  110. if (DCT_common_init(&s2) < 0)
  111. return -1;
  112. s->get_pixels = s2.dsp.get_pixels;
  113. s->fdct = s2.dsp.fdct;
  114. s->idct_put[0] = s2.dsp.idct_put;
  115. memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
  116. memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
  117. /* XXX: use MMX also for idct248 */
  118. s->idct_put[1] = simple_idct248_put;
  119. memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
  120. /* XXX: do it only for constant case */
  121. dv_build_unquantize_tables(s);
  122. return 0;
  123. }
  124. // #define VLC_DEBUG
  125. typedef struct BlockInfo {
  126. const uint8_t *shift_table;
  127. const uint8_t *scan_table;
  128. uint8_t pos; /* position in block */
  129. uint8_t eob_reached; /* true if EOB has been reached */
  130. uint8_t dct_mode;
  131. uint8_t partial_bit_count;
  132. uint16_t partial_bit_buffer;
  133. int shift_offset;
  134. } BlockInfo;
  135. /* block size in bits */
  136. static const uint16_t block_sizes[6] = {
  137. 112, 112, 112, 112, 80, 80
  138. };
  139. #ifndef ALT_BITSTREAM_READER
  140. #warning only works with ALT_BITSTREAM_READER
  141. #endif
  142. /* decode ac coefs */
  143. static void dv_decode_ac(DVVideoDecodeContext *s,
  144. BlockInfo *mb, DCTELEM *block, int last_index)
  145. {
  146. int last_re_index;
  147. int shift_offset = mb->shift_offset;
  148. const uint8_t *scan_table = mb->scan_table;
  149. const uint8_t *shift_table = mb->shift_table;
  150. int pos = mb->pos;
  151. int level, pos1, sign, run;
  152. int partial_bit_count;
  153. #ifndef ALT_BITSTREAM_READER //FIXME
  154. int re_index=0;
  155. int re1_index=0;
  156. #endif
  157. OPEN_READER(re, &s->gb);
  158. #ifdef VLC_DEBUG
  159. printf("start\n");
  160. #endif
  161. /* if we must parse a partial vlc, we do it here */
  162. partial_bit_count = mb->partial_bit_count;
  163. if (partial_bit_count > 0) {
  164. uint8_t buf[4];
  165. uint32_t v;
  166. int l, l1;
  167. GetBitContext gb1;
  168. /* build the dummy bit buffer */
  169. l = 16 - partial_bit_count;
  170. UPDATE_CACHE(re, &s->gb);
  171. #ifdef VLC_DEBUG
  172. printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
  173. #endif
  174. v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
  175. buf[0] = v >> 8;
  176. buf[1] = v;
  177. #ifdef VLC_DEBUG
  178. printf("v=%04x cnt=%d %04x\n",
  179. v, partial_bit_count, (mb->partial_bit_buffer << l));
  180. #endif
  181. /* try to read the codeword */
  182. init_get_bits(&gb1, buf, 4*8);
  183. {
  184. OPEN_READER(re1, &gb1);
  185. UPDATE_CACHE(re1, &gb1);
  186. GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
  187. TEX_VLC_BITS, 2);
  188. l = re1_index;
  189. CLOSE_READER(re1, &gb1);
  190. }
  191. #ifdef VLC_DEBUG
  192. printf("****run=%d level=%d size=%d\n", run, level, l);
  193. #endif
  194. /* compute codeword length */
  195. l1 = (level != 256 && level != 0);
  196. /* if too long, we cannot parse */
  197. l -= partial_bit_count;
  198. if ((re_index + l + l1) > last_index)
  199. return;
  200. /* skip read bits */
  201. last_re_index = 0; /* avoid warning */
  202. re_index += l;
  203. /* by definition, if we can read the vlc, all partial bits
  204. will be read (otherwise we could have read the vlc before) */
  205. mb->partial_bit_count = 0;
  206. UPDATE_CACHE(re, &s->gb);
  207. goto handle_vlc;
  208. }
  209. /* get the AC coefficients until last_index is reached */
  210. for(;;) {
  211. UPDATE_CACHE(re, &s->gb);
  212. #ifdef VLC_DEBUG
  213. printf("%2d: bits=%04x index=%d\n",
  214. pos, SHOW_UBITS(re, &s->gb, 16), re_index);
  215. #endif
  216. last_re_index = re_index;
  217. GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
  218. TEX_VLC_BITS, 2);
  219. handle_vlc:
  220. #ifdef VLC_DEBUG
  221. printf("run=%d level=%d\n", run, level);
  222. #endif
  223. if (level == 256) {
  224. if (re_index > last_index) {
  225. cannot_read:
  226. /* put position before read code */
  227. re_index = last_re_index;
  228. mb->eob_reached = 0;
  229. break;
  230. }
  231. /* EOB */
  232. mb->eob_reached = 1;
  233. break;
  234. } else if (level != 0) {
  235. if ((re_index + 1) > last_index)
  236. goto cannot_read;
  237. sign = SHOW_SBITS(re, &s->gb, 1);
  238. level = (level ^ sign) - sign;
  239. LAST_SKIP_BITS(re, &s->gb, 1);
  240. pos += run;
  241. /* error */
  242. if (pos >= 64) {
  243. goto read_error;
  244. }
  245. pos1 = scan_table[pos];
  246. level = level << (shift_table[pos1] + shift_offset);
  247. block[pos1] = level;
  248. // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
  249. } else {
  250. if (re_index > last_index)
  251. goto cannot_read;
  252. /* level is zero: means run without coding. No
  253. sign is coded */
  254. pos += run;
  255. /* error */
  256. if (pos >= 64) {
  257. read_error:
  258. #if defined(VLC_DEBUG) || 1
  259. fprintf(stderr, "error pos=%d\n", pos);
  260. #endif
  261. /* for errors, we consider the eob is reached */
  262. mb->eob_reached = 1;
  263. break;
  264. }
  265. }
  266. }
  267. CLOSE_READER(re, &s->gb);
  268. mb->pos = pos;
  269. }
  270. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
  271. {
  272. while (bits_left >= 16) {
  273. put_bits(pb, 16, get_bits(gb, 16));
  274. bits_left -= 16;
  275. }
  276. if (bits_left > 0) {
  277. put_bits(pb, bits_left, get_bits(gb, bits_left));
  278. }
  279. }
  280. /* mb_x and mb_y are in units of 8 pixels */
  281. static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
  282. uint8_t *buf_ptr1,
  283. const uint16_t *mb_pos_ptr)
  284. {
  285. int quant, dc, dct_mode, class1, j;
  286. int mb_index, mb_x, mb_y, v, last_index;
  287. DCTELEM *block, *block1;
  288. int c_offset, bits_left;
  289. uint8_t *y_ptr;
  290. BlockInfo mb_data[5 * 6], *mb, *mb1;
  291. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  292. uint8_t *buf_ptr;
  293. PutBitContext pb, vs_pb;
  294. uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
  295. int mb_bit_count;
  296. uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
  297. int vs_bit_count;
  298. memset(s->block, 0, sizeof(s->block));
  299. /* pass 1 : read DC and AC coefficients in blocks */
  300. buf_ptr = buf_ptr1;
  301. block1 = &s->block[0][0];
  302. mb1 = mb_data;
  303. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
  304. vs_bit_count = 0;
  305. for(mb_index = 0; mb_index < 5; mb_index++) {
  306. /* skip header */
  307. quant = buf_ptr[3] & 0x0f;
  308. buf_ptr += 4;
  309. init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
  310. mb_bit_count = 0;
  311. mb = mb1;
  312. block = block1;
  313. for(j = 0;j < 6; j++) {
  314. /* NOTE: size is not important here */
  315. init_get_bits(&s->gb, buf_ptr, 14*8);
  316. /* get the dc */
  317. dc = get_bits(&s->gb, 9);
  318. dc = (dc << (32 - 9)) >> (32 - 9);
  319. dct_mode = get_bits1(&s->gb);
  320. mb->dct_mode = dct_mode;
  321. mb->scan_table = s->dv_zigzag[dct_mode];
  322. class1 = get_bits(&s->gb, 2);
  323. mb->shift_offset = (class1 == 3);
  324. mb->shift_table = s->dv_shift[dct_mode]
  325. [quant + dv_quant_offset[class1]];
  326. dc = dc << 2;
  327. /* convert to unsigned because 128 is not added in the
  328. standard IDCT */
  329. dc += 1024;
  330. block[0] = dc;
  331. last_index = block_sizes[j];
  332. buf_ptr += last_index >> 3;
  333. mb->pos = 0;
  334. mb->partial_bit_count = 0;
  335. dv_decode_ac(s, mb, block, last_index);
  336. /* write the remaining bits in a new buffer only if the
  337. block is finished */
  338. bits_left = last_index - get_bits_count(&s->gb);
  339. if (mb->eob_reached) {
  340. mb->partial_bit_count = 0;
  341. mb_bit_count += bits_left;
  342. bit_copy(&pb, &s->gb, bits_left);
  343. } else {
  344. /* should be < 16 bits otherwise a codeword could have
  345. been parsed */
  346. mb->partial_bit_count = bits_left;
  347. mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
  348. }
  349. block += 64;
  350. mb++;
  351. }
  352. flush_put_bits(&pb);
  353. /* pass 2 : we can do it just after */
  354. #ifdef VLC_DEBUG
  355. printf("***pass 2 size=%d\n", mb_bit_count);
  356. #endif
  357. block = block1;
  358. mb = mb1;
  359. init_get_bits(&s->gb, mb_bit_buffer, 80*8);
  360. for(j = 0;j < 6; j++) {
  361. if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
  362. dv_decode_ac(s, mb, block, mb_bit_count);
  363. /* if still not finished, no need to parse other blocks */
  364. if (!mb->eob_reached) {
  365. /* we could not parse the current AC coefficient,
  366. so we add the remaining bytes */
  367. bits_left = mb_bit_count - get_bits_count(&s->gb);
  368. if (bits_left > 0) {
  369. mb->partial_bit_count += bits_left;
  370. mb->partial_bit_buffer =
  371. (mb->partial_bit_buffer << bits_left) |
  372. get_bits(&s->gb, bits_left);
  373. }
  374. goto next_mb;
  375. }
  376. }
  377. block += 64;
  378. mb++;
  379. }
  380. /* all blocks are finished, so the extra bytes can be used at
  381. the video segment level */
  382. bits_left = mb_bit_count - get_bits_count(&s->gb);
  383. vs_bit_count += bits_left;
  384. bit_copy(&vs_pb, &s->gb, bits_left);
  385. next_mb:
  386. mb1 += 6;
  387. block1 += 6 * 64;
  388. }
  389. /* we need a pass other the whole video segment */
  390. flush_put_bits(&vs_pb);
  391. #ifdef VLC_DEBUG
  392. printf("***pass 3 size=%d\n", vs_bit_count);
  393. #endif
  394. block = &s->block[0][0];
  395. mb = mb_data;
  396. init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
  397. for(mb_index = 0; mb_index < 5; mb_index++) {
  398. for(j = 0;j < 6; j++) {
  399. if (!mb->eob_reached) {
  400. #ifdef VLC_DEBUG
  401. printf("start %d:%d\n", mb_index, j);
  402. #endif
  403. dv_decode_ac(s, mb, block, vs_bit_count);
  404. }
  405. block += 64;
  406. mb++;
  407. }
  408. }
  409. /* compute idct and place blocks */
  410. block = &s->block[0][0];
  411. mb = mb_data;
  412. for(mb_index = 0; mb_index < 5; mb_index++) {
  413. v = *mb_pos_ptr++;
  414. mb_x = v & 0xff;
  415. mb_y = v >> 8;
  416. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  417. if (s->sys->pix_fmt == PIX_FMT_YUV411P)
  418. c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
  419. else
  420. c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
  421. for(j = 0;j < 6; j++) {
  422. idct_put = s->idct_put[mb->dct_mode];
  423. if (j < 4) {
  424. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  425. /* NOTE: at end of line, the macroblock is handled as 420 */
  426. idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
  427. } else {
  428. idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
  429. s->picture.linesize[0], block);
  430. }
  431. } else {
  432. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  433. uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
  434. int y, linesize;
  435. /* NOTE: at end of line, the macroblock is handled as 420 */
  436. idct_put(pixels, 8, block);
  437. linesize = s->picture.linesize[6 - j];
  438. c_ptr = s->picture.data[6 - j] + c_offset;
  439. ptr = pixels;
  440. for(y = 0;y < 8; y++) {
  441. /* convert to 411P */
  442. c_ptr1 = c_ptr + 8*linesize;
  443. c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
  444. c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
  445. c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
  446. c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
  447. c_ptr += linesize;
  448. ptr += 8;
  449. }
  450. } else {
  451. /* don't ask me why they inverted Cb and Cr ! */
  452. idct_put(s->picture.data[6 - j] + c_offset,
  453. s->picture.linesize[6 - j], block);
  454. }
  455. }
  456. block += 64;
  457. mb++;
  458. }
  459. }
  460. }
  461. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  462. 144000 bytes for PAL) */
  463. static int dvvideo_decode_frame(AVCodecContext *avctx,
  464. void *data, int *data_size,
  465. uint8_t *buf, int buf_size)
  466. {
  467. DVVideoDecodeContext *s = avctx->priv_data;
  468. int ds, vs;
  469. const uint16_t *mb_pos_ptr;
  470. s->sys = dv_frame_profile(buf);
  471. if (!s->sys || buf_size < s->sys->frame_size)
  472. return -1; /* NOTE: we only accept several full frames */
  473. if(s->picture.data[0])
  474. avctx->release_buffer(avctx, &s->picture);
  475. s->picture.reference = 0;
  476. avctx->pix_fmt = s->sys->pix_fmt;
  477. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  478. fprintf(stderr, "get_buffer() failed\n");
  479. return -1;
  480. }
  481. /* for each DIF segment */
  482. mb_pos_ptr = s->sys->video_place;
  483. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  484. buf += 6 * 80; /* skip DIF segment header */
  485. for(vs = 0; vs < 27; vs++) {
  486. if ((vs % 3) == 0)
  487. buf += 80; /* skip audio block */
  488. dv_decode_video_segment(s, buf, mb_pos_ptr);
  489. buf += 5 * 80;
  490. mb_pos_ptr += 5;
  491. }
  492. }
  493. emms_c();
  494. /* return image */
  495. *data_size = sizeof(AVFrame);
  496. *(AVFrame*)data= s->picture;
  497. return s->sys->frame_size;
  498. }
  499. static int dvvideo_decode_end(AVCodecContext *avctx)
  500. {
  501. avcodec_default_free_buffers(avctx);
  502. return 0;
  503. }
  504. AVCodec dvvideo_decoder = {
  505. "dvvideo",
  506. CODEC_TYPE_VIDEO,
  507. CODEC_ID_DVVIDEO,
  508. sizeof(DVVideoDecodeContext),
  509. dvvideo_decode_init,
  510. NULL,
  511. dvvideo_decode_end,
  512. dvvideo_decode_frame,
  513. CODEC_CAP_DR1,
  514. NULL
  515. };