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.

633 lines
19KB

  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. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. #include "simple_idct.h"
  23. #define NTSC_FRAME_SIZE 120000
  24. #define PAL_FRAME_SIZE 144000
  25. #define TEX_VLC_BITS 9
  26. typedef struct DVVideoDecodeContext {
  27. AVCodecContext *avctx;
  28. GetBitContext gb;
  29. VLC *vlc;
  30. int sampling_411; /* 0 = 420, 1 = 411 */
  31. int width, height;
  32. UINT8 *current_picture[3]; /* picture structure */
  33. int linesize[3];
  34. DCTELEM block[5*6][64] __align8;
  35. UINT8 dv_zigzag[2][64];
  36. /* XXX: move it to static storage ? */
  37. UINT8 dv_shift[2][2][22][64];
  38. void (*idct_put[2])(UINT8 *dest, int line_size, DCTELEM *block);
  39. } DVVideoDecodeContext;
  40. #include "dvdata.h"
  41. static VLC dv_vlc;
  42. /* XXX: also include quantization */
  43. static RL_VLC_ELEM *dv_rl_vlc[1];
  44. static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
  45. {
  46. int i, e, q;
  47. /* NOTE: max left shift is 6 */
  48. for(e = 0; e < 2; e++) {
  49. for(q = 0; q < 22; q++) {
  50. /* 88 unquant */
  51. for(i = 1; i < 64; i++) {
  52. /* 88 table */
  53. s->dv_shift[0][e][q][i] =
  54. dv_quant_shifts[q][dv_88_areas[i]] + e + 1;
  55. }
  56. /* 248 unquant */
  57. for(i = 1; i < 64; i++) {
  58. /* 248 table */
  59. s->dv_shift[1][e][q][i] =
  60. dv_quant_shifts[q][dv_248_areas[i]] + e + 1;
  61. }
  62. }
  63. }
  64. }
  65. static int dvvideo_decode_init(AVCodecContext *avctx)
  66. {
  67. DVVideoDecodeContext *s = avctx->priv_data;
  68. static int done;
  69. if (!done) {
  70. int i;
  71. done = 1;
  72. /* NOTE: as a trick, we use the fact the no codes are unused
  73. to accelerate the parsing of partial codes */
  74. init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
  75. dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
  76. dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  77. for(i = 0; i < dv_vlc.table_size; i++){
  78. int code= dv_vlc.table[i][0];
  79. int len = dv_vlc.table[i][1];
  80. int level, run;
  81. if(len<0){ //more bits needed
  82. run= 0;
  83. level= code;
  84. } else if (code == (NB_DV_VLC - 1)) {
  85. /* EOB */
  86. run = 0;
  87. level = 256;
  88. } else {
  89. run= dv_vlc_run[code] + 1;
  90. level= dv_vlc_level[code];
  91. }
  92. dv_rl_vlc[0][i].len = len;
  93. dv_rl_vlc[0][i].level = level;
  94. dv_rl_vlc[0][i].run = run;
  95. }
  96. }
  97. /* XXX: do it only for constant case */
  98. dv_build_unquantize_tables(s);
  99. {
  100. /* XXX: fix that : use mmx when possible */
  101. s->idct_put[0] = simple_idct_put;
  102. s->idct_put[1] = simple_idct248_put;
  103. memcpy(s->dv_zigzag[0], ff_zigzag_direct, 64);
  104. memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
  105. }
  106. return 0;
  107. }
  108. //#define VLC_DEBUG
  109. typedef struct MBInfo {
  110. const UINT8 *shift_table[6];
  111. const UINT8 *scan_table[6];
  112. UINT8 pos[6]; /* position in block */
  113. UINT8 eob_reached[6]; /* true if EOB has been reached */
  114. UINT8 dct_mode[6];
  115. UINT8 partial_bit_count[6];
  116. UINT16 partial_bit_buffer[6];
  117. UINT8 bit_buffer[80 + 4]; /* allow some slack */
  118. int bits_left;
  119. } MBInfo;
  120. /* block size in bits */
  121. const static UINT16 block_sizes[6] = {
  122. 112, 112, 112, 112, 80, 80
  123. };
  124. #ifndef ALT_BITSTREAM_READER
  125. #error only works with ALT_BITSTREAM_READER
  126. #endif
  127. /* decode ac coefs */
  128. static void dv_decode_ac(DVVideoDecodeContext *s,
  129. MBInfo *mb, INT16 *block, int j, int last_index)
  130. {
  131. int last_re_index;
  132. const UINT8 *scan_table = mb->scan_table[j];
  133. const UINT8 *shift_table = mb->shift_table[j];
  134. int pos = mb->pos[j];
  135. int level, pos1, sign, run;
  136. int partial_bit_count;
  137. OPEN_READER(re, &s->gb);
  138. #ifdef VLC_DEBUG
  139. printf("start %d\n", j);
  140. #endif
  141. /* if we must parse a partial vlc, we do it here */
  142. partial_bit_count = mb->partial_bit_count[j];
  143. if (partial_bit_count > 0) {
  144. UINT8 buf[4];
  145. UINT32 v;
  146. int l, l1;
  147. GetBitContext gb1;
  148. /* build the dummy bit buffer */
  149. l = 16 - partial_bit_count;
  150. UPDATE_CACHE(re, &s->gb);
  151. #ifdef VLC_DEBUG
  152. printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
  153. #endif
  154. v = (mb->partial_bit_buffer[j] << l) | SHOW_UBITS(re, &s->gb, l);
  155. buf[0] = v >> 8;
  156. buf[1] = v;
  157. #ifdef VLC_DEBUG
  158. printf("v=%04x cnt=%d %04x\n",
  159. v, partial_bit_count, (mb->partial_bit_buffer[j] << l));
  160. #endif
  161. /* try to read the codeword */
  162. init_get_bits(&gb1, buf, 4);
  163. {
  164. OPEN_READER(re1, &gb1);
  165. UPDATE_CACHE(re1, &gb1);
  166. GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
  167. TEX_VLC_BITS, 2);
  168. l = re1_index;
  169. CLOSE_READER(re1, &gb1);
  170. }
  171. #ifdef VLC_DEBUG
  172. printf("****run=%d level=%d size=%d\n", run, level, l);
  173. #endif
  174. /* compute codeword length */
  175. l1 = (level != 256 && level != 0);
  176. /* if too long, we cannot parse */
  177. l -= partial_bit_count;
  178. if ((re_index + l + l1) > last_index)
  179. return;
  180. /* skip read bits */
  181. last_re_index = 0; /* avoid warning */
  182. re_index += l;
  183. /* by redefinition, if we can read the vlc, all partial bits
  184. will be read (otherwise we could have read the vlc before) */
  185. mb->partial_bit_count[j] = 0;
  186. UPDATE_CACHE(re, &s->gb);
  187. goto handle_vlc;
  188. }
  189. /* get the AC coefficients until last_index is reached */
  190. for(;;) {
  191. UPDATE_CACHE(re, &s->gb);
  192. #ifdef VLC_DEBUG
  193. printf("%2d: bits=%04x index=%d\n",
  194. pos, SHOW_UBITS(re, &s->gb, 16), re_index);
  195. #endif
  196. last_re_index = re_index;
  197. GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
  198. TEX_VLC_BITS, 2);
  199. handle_vlc:
  200. #ifdef VLC_DEBUG
  201. printf("run=%d level=%d\n", run, level);
  202. #endif
  203. if (level == 256) {
  204. if (re_index > last_index) {
  205. cannot_read:
  206. /* put position before read code */
  207. re_index = last_re_index;
  208. mb->eob_reached[j] = 0;
  209. break;
  210. }
  211. /* EOB */
  212. mb->eob_reached[j] = 1;
  213. break;
  214. } else if (level != 0) {
  215. if ((re_index + 1) > last_index)
  216. goto cannot_read;
  217. sign = SHOW_SBITS(re, &s->gb, 1);
  218. level = (level ^ sign) - sign;
  219. LAST_SKIP_BITS(re, &s->gb, 1);
  220. pos += run;
  221. /* error */
  222. if (pos >= 64) {
  223. goto read_error;
  224. }
  225. pos1 = scan_table[pos];
  226. level = level << shift_table[pos1];
  227. block[pos1] = level;
  228. // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
  229. } else {
  230. if (re_index > last_index)
  231. goto cannot_read;
  232. /* level is zero: means run without coding. No
  233. sign is coded */
  234. pos += run;
  235. /* error */
  236. if (pos >= 64) {
  237. read_error:
  238. #if defined(VLC_DEBUG) || 1
  239. printf("error pos=%d\n", pos);
  240. #endif
  241. /* for errors, we consider the eob is reached */
  242. mb->eob_reached[j] = 1;
  243. break;
  244. }
  245. }
  246. }
  247. CLOSE_READER(re, &s->gb);
  248. mb->pos[j] = pos;
  249. }
  250. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
  251. {
  252. while (bits_left >= 16) {
  253. put_bits(pb, 16, get_bits(gb, 16));
  254. bits_left -= 16;
  255. }
  256. if (bits_left > 0) {
  257. put_bits(pb, bits_left, get_bits(gb, bits_left));
  258. }
  259. }
  260. /* mb_x and mb_y are in units of 8 pixels */
  261. static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
  262. UINT8 *buf_ptr1,
  263. const UINT16 *mb_pos_ptr)
  264. {
  265. int quant, dc, dct_mode, class1, j;
  266. int mb_index, mb_x, mb_y, v, last_index;
  267. DCTELEM *block, *block1;
  268. int c_offset, bits_left;
  269. UINT8 *y_ptr;
  270. MBInfo mb_data[5], *mb;
  271. void (*idct_put)(UINT8 *dest, int line_size, DCTELEM *block);
  272. UINT8 *buf_ptr;
  273. PutBitContext pb, vs_pb;
  274. UINT8 vs_bit_buffer[5 * 80 + 4];
  275. int vs_bit_count;
  276. memset(s->block, 0, sizeof(s->block));
  277. /* pass 1 : read DC and AC coefficients in blocks */
  278. buf_ptr = buf_ptr1;
  279. block1 = &s->block[0][0];
  280. mb = mb_data;
  281. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
  282. vs_bit_count = 0;
  283. for(mb_index = 0; mb_index < 5; mb_index++) {
  284. /* skip header */
  285. quant = buf_ptr[3] & 0x0f;
  286. buf_ptr += 4;
  287. init_put_bits(&pb, mb->bit_buffer, 80, NULL, NULL);
  288. mb->bits_left = 0;
  289. block = block1;
  290. for(j = 0;j < 6; j++) {
  291. /* NOTE: size is not important here */
  292. init_get_bits(&s->gb, buf_ptr, 14);
  293. /* get the dc */
  294. dc = get_bits(&s->gb, 9);
  295. dc = (dc << (32 - 9)) >> (32 - 9);
  296. dct_mode = get_bits1(&s->gb);
  297. mb->dct_mode[j] = dct_mode;
  298. mb->scan_table[j] = s->dv_zigzag[dct_mode];
  299. class1 = get_bits(&s->gb, 2);
  300. mb->shift_table[j] = s->dv_shift[dct_mode][class1 == 3]
  301. [quant + dv_quant_offset[class1]];
  302. dc = dc << 2;
  303. /* convert to unsigned because 128 is not added in the
  304. standard IDCT */
  305. dc += 1024;
  306. block[0] = dc;
  307. last_index = block_sizes[j];
  308. buf_ptr += last_index >> 3;
  309. mb->pos[j] = 0;
  310. mb->partial_bit_count[j] = 0;
  311. dv_decode_ac(s, mb, block, j, last_index);
  312. /* write the remaining bits in a new buffer only if the
  313. block is finished */
  314. bits_left = last_index - s->gb.index;
  315. if (mb->eob_reached[j]) {
  316. mb->partial_bit_count[j] = 0;
  317. mb->bits_left += bits_left;
  318. bit_copy(&pb, &s->gb, bits_left);
  319. } else {
  320. /* should be < 16 bits otherwise a codeword could have
  321. been parsed */
  322. mb->partial_bit_count[j] = bits_left;
  323. mb->partial_bit_buffer[j] = get_bits(&s->gb, bits_left);
  324. }
  325. block += 64;
  326. }
  327. flush_put_bits(&pb);
  328. /* pass 2 : we can do it just after */
  329. #ifdef VLC_DEBUG
  330. printf("***pass 2 size=%d\n", mb->bits_left);
  331. #endif
  332. block = block1;
  333. init_get_bits(&s->gb, mb->bit_buffer, 80);
  334. for(j = 0;j < 6; j++) {
  335. if (!mb->eob_reached[j] && s->gb.index < mb->bits_left) {
  336. dv_decode_ac(s, mb, block, j, mb->bits_left);
  337. /* if still not finished, no need to parse other blocks */
  338. if (!mb->eob_reached[j]) {
  339. /* we could not parse the current AC coefficient,
  340. so we add the remaining bytes */
  341. bits_left = mb->bits_left - s->gb.index;
  342. if (bits_left > 0) {
  343. mb->partial_bit_count[j] += bits_left;
  344. mb->partial_bit_buffer[j] =
  345. (mb->partial_bit_buffer[j] << bits_left) |
  346. get_bits(&s->gb, bits_left);
  347. }
  348. goto next_mb;
  349. }
  350. }
  351. block += 64;
  352. }
  353. /* all blocks are finished, so the extra bytes can be used at
  354. the video segment level */
  355. bits_left = mb->bits_left - s->gb.index;
  356. vs_bit_count += bits_left;
  357. bit_copy(&vs_pb, &s->gb, bits_left);
  358. next_mb:
  359. mb++;
  360. block1 += 6 * 64;
  361. }
  362. /* we need a pass other the whole video segment */
  363. flush_put_bits(&vs_pb);
  364. #ifdef VLC_DEBUG
  365. printf("***pass 3 size=%d\n", vs_bit_count);
  366. #endif
  367. block = &s->block[0][0];
  368. mb = mb_data;
  369. init_get_bits(&s->gb, vs_bit_buffer, 5 * 80);
  370. for(mb_index = 0; mb_index < 5; mb_index++) {
  371. for(j = 0;j < 6; j++) {
  372. if (!mb->eob_reached[j]) {
  373. #ifdef VLC_DEBUG
  374. printf("start %d:%d\n", mb_index, j);
  375. #endif
  376. dv_decode_ac(s, mb, block, j, vs_bit_count);
  377. }
  378. block += 64;
  379. }
  380. mb++;
  381. }
  382. /* compute idct and place blocks */
  383. block = &s->block[0][0];
  384. mb = mb_data;
  385. for(mb_index = 0; mb_index < 5; mb_index++) {
  386. v = *mb_pos_ptr++;
  387. mb_x = v & 0xff;
  388. mb_y = v >> 8;
  389. y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8);
  390. if (s->sampling_411)
  391. c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8);
  392. else
  393. c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8);
  394. for(j = 0;j < 6; j++) {
  395. idct_put = s->idct_put[mb->dct_mode[j]];
  396. if (j < 4) {
  397. if (s->sampling_411) {
  398. idct_put(y_ptr + (j * 8), s->linesize[0], block);
  399. } else {
  400. idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]),
  401. s->linesize[0], block);
  402. }
  403. } else {
  404. /* don't ask me why they inverted Cb and Cr ! */
  405. idct_put(s->current_picture[6 - j] + c_offset,
  406. s->linesize[6 - j], block);
  407. }
  408. block += 64;
  409. }
  410. mb++;
  411. }
  412. }
  413. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  414. 144000 bytes for PAL) */
  415. static int dvvideo_decode_frame(AVCodecContext *avctx,
  416. void *data, int *data_size,
  417. UINT8 *buf, int buf_size)
  418. {
  419. DVVideoDecodeContext *s = avctx->priv_data;
  420. int sct, dsf, apt, ds, nb_dif_segs, vs, size, width, height, i;
  421. UINT8 *buf_ptr;
  422. const UINT16 *mb_pos_ptr;
  423. AVPicture *picture;
  424. /* parse id */
  425. init_get_bits(&s->gb, buf, buf_size);
  426. sct = get_bits(&s->gb, 3);
  427. if (sct != 0)
  428. return -1;
  429. skip_bits(&s->gb, 5);
  430. get_bits(&s->gb, 4); /* dsn (sequence number */
  431. get_bits(&s->gb, 1); /* fsc (channel number) */
  432. skip_bits(&s->gb, 3);
  433. get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */
  434. dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */
  435. if (get_bits(&s->gb, 1) != 0)
  436. return -1;
  437. skip_bits(&s->gb, 11);
  438. apt = get_bits(&s->gb, 3); /* apt */
  439. get_bits(&s->gb, 1); /* tf1 */
  440. skip_bits(&s->gb, 4);
  441. get_bits(&s->gb, 3); /* ap1 */
  442. get_bits(&s->gb, 1); /* tf2 */
  443. skip_bits(&s->gb, 4);
  444. get_bits(&s->gb, 3); /* ap2 */
  445. get_bits(&s->gb, 1); /* tf3 */
  446. skip_bits(&s->gb, 4);
  447. get_bits(&s->gb, 3); /* ap3 */
  448. /* init size */
  449. width = 720;
  450. if (dsf) {
  451. if (buf_size != PAL_FRAME_SIZE)
  452. return -1;
  453. height = 576;
  454. nb_dif_segs = 12;
  455. } else {
  456. if (buf_size != NTSC_FRAME_SIZE)
  457. return -1;
  458. height = 480;
  459. nb_dif_segs = 10;
  460. }
  461. /* (re)alloc picture if needed */
  462. if (s->width != width || s->height != height) {
  463. for(i=0;i<3;i++)
  464. av_freep(&s->current_picture[i]);
  465. for(i=0;i<3;i++) {
  466. size = width * height;
  467. s->linesize[i] = width;
  468. if (i >= 1) {
  469. size >>= 2;
  470. s->linesize[i] >>= 1;
  471. }
  472. s->current_picture[i] = av_malloc(size);
  473. if (!s->current_picture[i])
  474. return -1;
  475. }
  476. s->width = width;
  477. s->height = height;
  478. }
  479. /* XXX: is it correct to assume that 420 is always used in PAL
  480. mode ? */
  481. s->sampling_411 = !dsf;
  482. if (s->sampling_411)
  483. mb_pos_ptr = dv_place_411;
  484. else
  485. mb_pos_ptr = dv_place_420;
  486. /* for each DIF segment */
  487. buf_ptr = buf;
  488. for (ds = 0; ds < nb_dif_segs; ds++) {
  489. buf_ptr += 6 * 80; /* skip DIF segment header */
  490. for(vs = 0; vs < 27; vs++) {
  491. if ((vs % 3) == 0) {
  492. /* skip audio block */
  493. buf_ptr += 80;
  494. }
  495. dv_decode_video_segment(s, buf_ptr, mb_pos_ptr);
  496. buf_ptr += 5 * 80;
  497. mb_pos_ptr += 5;
  498. }
  499. }
  500. /* return image */
  501. avctx->width = width;
  502. avctx->height = height;
  503. if (s->sampling_411)
  504. avctx->pix_fmt = PIX_FMT_YUV420P; /* XXX: incorrect, add PIX_FMT_YUV411P */
  505. else
  506. avctx->pix_fmt = PIX_FMT_YUV420P;
  507. if (dsf)
  508. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  509. else
  510. avctx->frame_rate = 30 * FRAME_RATE_BASE;
  511. *data_size = sizeof(AVPicture);
  512. picture = data;
  513. for(i=0;i<3;i++) {
  514. picture->data[i] = s->current_picture[i];
  515. picture->linesize[i] = s->linesize[i];
  516. }
  517. return buf_size;
  518. }
  519. static int dvvideo_decode_end(AVCodecContext *avctx)
  520. {
  521. DVVideoDecodeContext *s = avctx->priv_data;
  522. int i;
  523. for(i=0;i<3;i++)
  524. av_freep(&s->current_picture[i]);
  525. return 0;
  526. }
  527. AVCodec dvvideo_decoder = {
  528. "dvvideo",
  529. CODEC_TYPE_VIDEO,
  530. CODEC_ID_DVVIDEO,
  531. sizeof(DVVideoDecodeContext),
  532. dvvideo_decode_init,
  533. NULL,
  534. dvvideo_decode_end,
  535. dvvideo_decode_frame,
  536. 0,
  537. NULL
  538. };
  539. typedef struct DVAudioDecodeContext {
  540. AVCodecContext *avctx;
  541. GetBitContext gb;
  542. } DVAudioDecodeContext;
  543. static int dvaudio_decode_init(AVCodecContext *avctx)
  544. {
  545. // DVAudioDecodeContext *s = avctx->priv_data;
  546. return 0;
  547. }
  548. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  549. 144000 bytes for PAL) */
  550. static int dvaudio_decode_frame(AVCodecContext *avctx,
  551. void *data, int *data_size,
  552. UINT8 *buf, int buf_size)
  553. {
  554. // DVAudioDecodeContext *s = avctx->priv_data;
  555. return buf_size;
  556. }
  557. static int dvaudio_decode_end(AVCodecContext *avctx)
  558. {
  559. // DVAudioDecodeContext *s = avctx->priv_data;
  560. return 0;
  561. }
  562. AVCodec dvaudio_decoder = {
  563. "dvaudio",
  564. CODEC_TYPE_AUDIO,
  565. CODEC_ID_DVAUDIO,
  566. sizeof(DVAudioDecodeContext),
  567. dvaudio_decode_init,
  568. NULL,
  569. dvaudio_decode_end,
  570. dvaudio_decode_frame,
  571. 0,
  572. NULL
  573. };