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.

934 lines
28KB

  1. /*
  2. * DV decoder
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * DV encoder
  6. * Copyright (c) 2003 Roman Shaposhnik.
  7. *
  8. * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
  9. * of DV technical info.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /**
  26. * @file dv.c
  27. * DV codec.
  28. */
  29. #include "avcodec.h"
  30. #include "dsputil.h"
  31. #include "mpegvideo.h"
  32. #include "simple_idct.h"
  33. #include "dvdata.h"
  34. typedef struct DVVideoDecodeContext {
  35. const DVprofile* sys;
  36. GetBitContext gb;
  37. AVFrame picture;
  38. DCTELEM block[5*6][64] __align8;
  39. /* FIXME: the following is extracted from DSP */
  40. uint8_t dv_zigzag[2][64];
  41. uint8_t idct_permutation[64];
  42. void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
  43. void (*fdct)(DCTELEM *block);
  44. /* XXX: move it to static storage ? */
  45. uint8_t dv_shift[2][22][64];
  46. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  47. } DVVideoDecodeContext;
  48. #define TEX_VLC_BITS 9
  49. /* XXX: also include quantization */
  50. static RL_VLC_ELEM *dv_rl_vlc[1];
  51. static VLC_TYPE dv_vlc_codes[15][23];
  52. static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
  53. {
  54. int i, q, j;
  55. /* NOTE: max left shift is 6 */
  56. for(q = 0; q < 22; q++) {
  57. /* 88 unquant */
  58. for(i = 1; i < 64; i++) {
  59. /* 88 table */
  60. j = s->idct_permutation[i];
  61. s->dv_shift[0][q][j] =
  62. dv_quant_shifts[q][dv_88_areas[i]] + 1;
  63. }
  64. /* 248 unquant */
  65. for(i = 1; i < 64; i++) {
  66. /* 248 table */
  67. s->dv_shift[1][q][i] =
  68. dv_quant_shifts[q][dv_248_areas[i]] + 1;
  69. }
  70. }
  71. }
  72. static int dvvideo_init(AVCodecContext *avctx)
  73. {
  74. DVVideoDecodeContext *s = avctx->priv_data;
  75. MpegEncContext s2;
  76. static int done=0;
  77. if (!done) {
  78. int i;
  79. VLC dv_vlc;
  80. done = 1;
  81. /* NOTE: as a trick, we use the fact the no codes are unused
  82. to accelerate the parsing of partial codes */
  83. init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
  84. dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
  85. dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  86. for(i = 0; i < dv_vlc.table_size; i++){
  87. int code= dv_vlc.table[i][0];
  88. int len = dv_vlc.table[i][1];
  89. int level, run;
  90. if(len<0){ //more bits needed
  91. run= 0;
  92. level= code;
  93. } else if (code == (NB_DV_VLC - 1)) {
  94. /* EOB */
  95. run = 0;
  96. level = 256;
  97. } else {
  98. run= dv_vlc_run[code] + 1;
  99. level= dv_vlc_level[code];
  100. }
  101. dv_rl_vlc[0][i].len = len;
  102. dv_rl_vlc[0][i].level = level;
  103. dv_rl_vlc[0][i].run = run;
  104. }
  105. memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
  106. for (i = 0; i < NB_DV_VLC - 1; i++) {
  107. if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
  108. dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
  109. }
  110. }
  111. /* ugly way to get the idct & scantable */
  112. /* XXX: fix it */
  113. memset(&s2, 0, sizeof(MpegEncContext));
  114. s2.avctx = avctx;
  115. dsputil_init(&s2.dsp, avctx);
  116. if (DCT_common_init(&s2) < 0)
  117. return -1;
  118. s->get_pixels = s2.dsp.get_pixels;
  119. s->fdct = s2.dsp.fdct;
  120. s->idct_put[0] = s2.dsp.idct_put;
  121. memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
  122. memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
  123. /* XXX: use MMX also for idct248 */
  124. s->idct_put[1] = simple_idct248_put;
  125. memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
  126. /* XXX: do it only for constant case */
  127. dv_build_unquantize_tables(s);
  128. /* FIXME: I really don't think this should be here */
  129. if (dv_codec_profile(avctx))
  130. avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
  131. return 0;
  132. }
  133. // #define VLC_DEBUG
  134. typedef struct BlockInfo {
  135. const uint8_t *shift_table;
  136. const uint8_t *scan_table;
  137. uint8_t pos; /* position in block */
  138. uint8_t eob_reached; /* true if EOB has been reached */
  139. uint8_t dct_mode;
  140. uint8_t partial_bit_count;
  141. uint16_t partial_bit_buffer;
  142. int shift_offset;
  143. } BlockInfo;
  144. /* block size in bits */
  145. static const uint16_t block_sizes[6] = {
  146. 112, 112, 112, 112, 80, 80
  147. };
  148. #ifndef ALT_BITSTREAM_READER
  149. #warning only works with ALT_BITSTREAM_READER
  150. #endif
  151. /* decode ac coefs */
  152. static void dv_decode_ac(DVVideoDecodeContext *s,
  153. BlockInfo *mb, DCTELEM *block, int last_index)
  154. {
  155. int last_re_index;
  156. int shift_offset = mb->shift_offset;
  157. const uint8_t *scan_table = mb->scan_table;
  158. const uint8_t *shift_table = mb->shift_table;
  159. int pos = mb->pos;
  160. int level, pos1, sign, run;
  161. int partial_bit_count;
  162. #ifndef ALT_BITSTREAM_READER //FIXME
  163. int re_index=0;
  164. int re1_index=0;
  165. #endif
  166. OPEN_READER(re, &s->gb);
  167. #ifdef VLC_DEBUG
  168. printf("start\n");
  169. #endif
  170. /* if we must parse a partial vlc, we do it here */
  171. partial_bit_count = mb->partial_bit_count;
  172. if (partial_bit_count > 0) {
  173. uint8_t buf[4];
  174. uint32_t v;
  175. int l, l1;
  176. GetBitContext gb1;
  177. /* build the dummy bit buffer */
  178. l = 16 - partial_bit_count;
  179. UPDATE_CACHE(re, &s->gb);
  180. #ifdef VLC_DEBUG
  181. printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
  182. #endif
  183. v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
  184. buf[0] = v >> 8;
  185. buf[1] = v;
  186. #ifdef VLC_DEBUG
  187. printf("v=%04x cnt=%d %04x\n",
  188. v, partial_bit_count, (mb->partial_bit_buffer << l));
  189. #endif
  190. /* try to read the codeword */
  191. init_get_bits(&gb1, buf, 4*8);
  192. {
  193. OPEN_READER(re1, &gb1);
  194. UPDATE_CACHE(re1, &gb1);
  195. GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
  196. TEX_VLC_BITS, 2);
  197. l = re1_index;
  198. CLOSE_READER(re1, &gb1);
  199. }
  200. #ifdef VLC_DEBUG
  201. printf("****run=%d level=%d size=%d\n", run, level, l);
  202. #endif
  203. /* compute codeword length */
  204. l1 = (level != 256 && level != 0);
  205. /* if too long, we cannot parse */
  206. l -= partial_bit_count;
  207. if ((re_index + l + l1) > last_index)
  208. return;
  209. /* skip read bits */
  210. last_re_index = 0; /* avoid warning */
  211. re_index += l;
  212. /* by definition, if we can read the vlc, all partial bits
  213. will be read (otherwise we could have read the vlc before) */
  214. mb->partial_bit_count = 0;
  215. UPDATE_CACHE(re, &s->gb);
  216. goto handle_vlc;
  217. }
  218. /* get the AC coefficients until last_index is reached */
  219. for(;;) {
  220. UPDATE_CACHE(re, &s->gb);
  221. #ifdef VLC_DEBUG
  222. printf("%2d: bits=%04x index=%d\n",
  223. pos, SHOW_UBITS(re, &s->gb, 16), re_index);
  224. #endif
  225. last_re_index = re_index;
  226. GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
  227. TEX_VLC_BITS, 2);
  228. handle_vlc:
  229. #ifdef VLC_DEBUG
  230. printf("run=%d level=%d\n", run, level);
  231. #endif
  232. if (level == 256) {
  233. if (re_index > last_index) {
  234. cannot_read:
  235. /* put position before read code */
  236. re_index = last_re_index;
  237. mb->eob_reached = 0;
  238. break;
  239. }
  240. /* EOB */
  241. mb->eob_reached = 1;
  242. break;
  243. } else if (level != 0) {
  244. if ((re_index + 1) > last_index)
  245. goto cannot_read;
  246. sign = SHOW_SBITS(re, &s->gb, 1);
  247. level = (level ^ sign) - sign;
  248. LAST_SKIP_BITS(re, &s->gb, 1);
  249. pos += run;
  250. /* error */
  251. if (pos >= 64) {
  252. goto read_error;
  253. }
  254. pos1 = scan_table[pos];
  255. level = level << (shift_table[pos1] + shift_offset);
  256. block[pos1] = level;
  257. // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
  258. } else {
  259. if (re_index > last_index)
  260. goto cannot_read;
  261. /* level is zero: means run without coding. No
  262. sign is coded */
  263. pos += run;
  264. /* error */
  265. if (pos >= 64) {
  266. read_error:
  267. #if defined(VLC_DEBUG) || 1
  268. fprintf(stderr, "error pos=%d\n", pos);
  269. #endif
  270. /* for errors, we consider the eob is reached */
  271. mb->eob_reached = 1;
  272. break;
  273. }
  274. }
  275. }
  276. CLOSE_READER(re, &s->gb);
  277. mb->pos = pos;
  278. }
  279. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
  280. {
  281. while (bits_left >= 16) {
  282. put_bits(pb, 16, get_bits(gb, 16));
  283. bits_left -= 16;
  284. }
  285. if (bits_left > 0) {
  286. put_bits(pb, bits_left, get_bits(gb, bits_left));
  287. }
  288. }
  289. /* mb_x and mb_y are in units of 8 pixels */
  290. static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
  291. uint8_t *buf_ptr1,
  292. const uint16_t *mb_pos_ptr)
  293. {
  294. int quant, dc, dct_mode, class1, j;
  295. int mb_index, mb_x, mb_y, v, last_index;
  296. DCTELEM *block, *block1;
  297. int c_offset, bits_left;
  298. uint8_t *y_ptr;
  299. BlockInfo mb_data[5 * 6], *mb, *mb1;
  300. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  301. uint8_t *buf_ptr;
  302. PutBitContext pb, vs_pb;
  303. uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
  304. int mb_bit_count;
  305. uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
  306. int vs_bit_count;
  307. memset(s->block, 0, sizeof(s->block));
  308. /* pass 1 : read DC and AC coefficients in blocks */
  309. buf_ptr = buf_ptr1;
  310. block1 = &s->block[0][0];
  311. mb1 = mb_data;
  312. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
  313. vs_bit_count = 0;
  314. for(mb_index = 0; mb_index < 5; mb_index++) {
  315. /* skip header */
  316. quant = buf_ptr[3] & 0x0f;
  317. buf_ptr += 4;
  318. init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
  319. mb_bit_count = 0;
  320. mb = mb1;
  321. block = block1;
  322. for(j = 0;j < 6; j++) {
  323. /* NOTE: size is not important here */
  324. init_get_bits(&s->gb, buf_ptr, 14*8);
  325. /* get the dc */
  326. dc = get_bits(&s->gb, 9);
  327. dc = (dc << (32 - 9)) >> (32 - 9);
  328. dct_mode = get_bits1(&s->gb);
  329. mb->dct_mode = dct_mode;
  330. mb->scan_table = s->dv_zigzag[dct_mode];
  331. class1 = get_bits(&s->gb, 2);
  332. mb->shift_offset = (class1 == 3);
  333. mb->shift_table = s->dv_shift[dct_mode]
  334. [quant + dv_quant_offset[class1]];
  335. dc = dc << 2;
  336. /* convert to unsigned because 128 is not added in the
  337. standard IDCT */
  338. dc += 1024;
  339. block[0] = dc;
  340. last_index = block_sizes[j];
  341. buf_ptr += last_index >> 3;
  342. mb->pos = 0;
  343. mb->partial_bit_count = 0;
  344. #ifdef VLC_DEBUG
  345. printf("MB block: %d, %d ", mb_index, j);
  346. #endif
  347. dv_decode_ac(s, mb, block, last_index);
  348. /* write the remaining bits in a new buffer only if the
  349. block is finished */
  350. bits_left = last_index - get_bits_count(&s->gb);
  351. if (mb->eob_reached) {
  352. mb->partial_bit_count = 0;
  353. mb_bit_count += bits_left;
  354. bit_copy(&pb, &s->gb, bits_left);
  355. } else {
  356. /* should be < 16 bits otherwise a codeword could have
  357. been parsed */
  358. mb->partial_bit_count = bits_left;
  359. mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
  360. }
  361. block += 64;
  362. mb++;
  363. }
  364. flush_put_bits(&pb);
  365. /* pass 2 : we can do it just after */
  366. #ifdef VLC_DEBUG
  367. printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index);
  368. #endif
  369. block = block1;
  370. mb = mb1;
  371. init_get_bits(&s->gb, mb_bit_buffer, 80*8);
  372. for(j = 0;j < 6; j++) {
  373. if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
  374. dv_decode_ac(s, mb, block, mb_bit_count);
  375. /* if still not finished, no need to parse other blocks */
  376. if (!mb->eob_reached) {
  377. /* we could not parse the current AC coefficient,
  378. so we add the remaining bytes */
  379. bits_left = mb_bit_count - get_bits_count(&s->gb);
  380. if (bits_left > 0) {
  381. mb->partial_bit_count += bits_left;
  382. mb->partial_bit_buffer =
  383. (mb->partial_bit_buffer << bits_left) |
  384. get_bits(&s->gb, bits_left);
  385. }
  386. goto next_mb;
  387. }
  388. }
  389. block += 64;
  390. mb++;
  391. }
  392. /* all blocks are finished, so the extra bytes can be used at
  393. the video segment level */
  394. bits_left = mb_bit_count - get_bits_count(&s->gb);
  395. vs_bit_count += bits_left;
  396. bit_copy(&vs_pb, &s->gb, bits_left);
  397. next_mb:
  398. mb1 += 6;
  399. block1 += 6 * 64;
  400. }
  401. /* we need a pass other the whole video segment */
  402. flush_put_bits(&vs_pb);
  403. #ifdef VLC_DEBUG
  404. printf("***pass 3 size=%d\n", vs_bit_count);
  405. #endif
  406. block = &s->block[0][0];
  407. mb = mb_data;
  408. init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
  409. for(mb_index = 0; mb_index < 5; mb_index++) {
  410. for(j = 0;j < 6; j++) {
  411. if (!mb->eob_reached) {
  412. #ifdef VLC_DEBUG
  413. printf("start %d:%d\n", mb_index, j);
  414. #endif
  415. dv_decode_ac(s, mb, block, vs_bit_count);
  416. }
  417. block += 64;
  418. mb++;
  419. }
  420. }
  421. /* compute idct and place blocks */
  422. block = &s->block[0][0];
  423. mb = mb_data;
  424. for(mb_index = 0; mb_index < 5; mb_index++) {
  425. v = *mb_pos_ptr++;
  426. mb_x = v & 0xff;
  427. mb_y = v >> 8;
  428. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  429. if (s->sys->pix_fmt == PIX_FMT_YUV411P)
  430. c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
  431. else
  432. c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
  433. for(j = 0;j < 6; j++) {
  434. idct_put = s->idct_put[mb->dct_mode];
  435. if (j < 4) {
  436. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  437. /* NOTE: at end of line, the macroblock is handled as 420 */
  438. idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
  439. } else {
  440. idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
  441. s->picture.linesize[0], block);
  442. }
  443. } else {
  444. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  445. uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
  446. int y, linesize;
  447. /* NOTE: at end of line, the macroblock is handled as 420 */
  448. idct_put(pixels, 8, block);
  449. linesize = s->picture.linesize[6 - j];
  450. c_ptr = s->picture.data[6 - j] + c_offset;
  451. ptr = pixels;
  452. for(y = 0;y < 8; y++) {
  453. /* convert to 411P */
  454. c_ptr1 = c_ptr + 8*linesize;
  455. c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
  456. c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
  457. c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
  458. c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
  459. c_ptr += linesize;
  460. ptr += 8;
  461. }
  462. } else {
  463. /* don't ask me why they inverted Cb and Cr ! */
  464. idct_put(s->picture.data[6 - j] + c_offset,
  465. s->picture.linesize[6 - j], block);
  466. }
  467. }
  468. block += 64;
  469. mb++;
  470. }
  471. }
  472. }
  473. /* Converts run and level (where level != 0) pair into vlc, returning bit size */
  474. static inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
  475. {
  476. int sign = l >> 8;
  477. int level = (l ^ sign) - sign;
  478. int size;
  479. sign = (sign & 1);
  480. if (run < 15 && level < 23 && dv_vlc_codes[run][level] != -1) {
  481. *vlc = (dv_vlc_bits[dv_vlc_codes[run][level]] << 1) | sign;
  482. size = dv_vlc_len[dv_vlc_codes[run][level]] + 1;
  483. }
  484. else {
  485. if (level < 23) {
  486. *vlc = (dv_vlc_bits[dv_vlc_codes[0][level]] << 1) | sign;
  487. size = dv_vlc_len[dv_vlc_codes[0][level]] + 1;
  488. } else {
  489. *vlc = 0xfe00 | (level << 1) | sign;
  490. size = 16;
  491. }
  492. switch(run) {
  493. case 0:
  494. break;
  495. case 1:
  496. case 2:
  497. *vlc |= ((0x7ce | (run - 1)) << size);
  498. size += 11;
  499. break;
  500. case 3:
  501. case 4:
  502. case 5:
  503. case 6:
  504. *vlc |= ((0xfac | (run - 3)) << size);
  505. size += 12;
  506. break;
  507. default:
  508. *vlc |= ((0x1f80 | (run - 1)) << size);
  509. size += 13;
  510. break;
  511. }
  512. }
  513. return size;
  514. }
  515. typedef struct EncBlockInfo {
  516. int qno;
  517. int cno;
  518. int dct_mode;
  519. int block_size;
  520. DCTELEM *mb;
  521. PutBitContext pb;
  522. } EncBlockInfo;
  523. static inline int dv_bits_left(EncBlockInfo* bi)
  524. {
  525. return (bi->block_size - get_bit_count(&bi->pb));
  526. }
  527. static inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* heap)
  528. {
  529. int i, level, size, run = 0;
  530. uint32_t vlc;
  531. PutBitContext* cpb = &bi->pb;
  532. for (i=1; i<64; i++) {
  533. level = bi->mb[ff_zigzag_direct[i]] /
  534. (1<<(dv_quant_shifts[bi->qno + dv_quant_offset[bi->cno]]
  535. [dv_88_areas[ff_zigzag_direct[i]]] + 4 + (bi->cno == 3)));
  536. if (level != 0) {
  537. size = dv_rl2vlc(run, level, &vlc);
  538. put_vlc:
  539. #ifdef VLC_DEBUG
  540. printf(" %3d:%3d", run, level);
  541. #endif
  542. if (cpb == &bi->pb && size > dv_bits_left(bi)) {
  543. size -= dv_bits_left(bi);
  544. put_bits(cpb, dv_bits_left(bi), vlc >> size);
  545. vlc = vlc & ((1<<size)-1);
  546. cpb = heap;
  547. }
  548. put_bits(cpb, size, vlc);
  549. run = 0;
  550. } else
  551. run++;
  552. }
  553. if (i == 64) {
  554. size = 4; vlc = 6; /* End Of Block stamp */
  555. goto put_vlc;
  556. }
  557. }
  558. static inline void dv_redistr_bits(EncBlockInfo* bi, int count, uint8_t* extra_data, int extra_bits, PutBitContext* heap)
  559. {
  560. int i;
  561. GetBitContext gb;
  562. init_get_bits(&gb, extra_data, extra_bits);
  563. for (i=0; i<count; i++) {
  564. int bits_left = dv_bits_left(bi);
  565. #ifdef VLC_DEBUG
  566. if (bits_left)
  567. printf("------------> inserting %d bytes in %d:%d\n", bits_left, i/6, i%6);
  568. #endif
  569. if (bits_left > extra_bits) {
  570. bit_copy(&bi->pb, &gb, extra_bits);
  571. extra_bits = 0;
  572. break;
  573. } else
  574. bit_copy(&bi->pb, &gb, bits_left);
  575. extra_bits -= bits_left;
  576. bi++;
  577. }
  578. if (extra_bits > 0 && heap)
  579. bit_copy(heap, &gb, extra_bits);
  580. }
  581. static inline void dv_set_class_number(EncBlockInfo* bi, int j)
  582. {
  583. int i, max_ac = 0;
  584. for (i=1; i<64; i++) {
  585. int ac = abs(bi->mb[ff_zigzag_direct[i]]) / 4;
  586. if (max_ac < ac)
  587. max_ac = ac;
  588. }
  589. if (max_ac < 12)
  590. bi->cno = j;
  591. else if (max_ac < 24)
  592. bi->cno = j + 1;
  593. else if (max_ac < 36)
  594. bi->cno = j + 2;
  595. else
  596. bi->cno = j + 3;
  597. if (bi->cno > 3)
  598. bi->cno = 3;
  599. }
  600. /*
  601. * This is a very rough initial implementaion. The performance is
  602. * horrible and some features are missing, mainly 2-4-8 DCT encoding.
  603. * The weighting is missing as well, but it's missing from the decoding
  604. * step also -- so at least we're on the same page with decoder ;-)
  605. */
  606. static inline void dv_encode_video_segment(DVVideoDecodeContext *s,
  607. uint8_t *dif,
  608. const uint16_t *mb_pos_ptr)
  609. {
  610. int mb_index, i, j, v;
  611. int mb_x, mb_y, c_offset, linesize;
  612. uint8_t* y_ptr;
  613. uint8_t* data;
  614. int do_edge_wrap;
  615. DCTELEM *block;
  616. EncBlockInfo enc_blks[5*6];
  617. EncBlockInfo* enc_blk;
  618. int free_vs_bits;
  619. int extra_bits;
  620. PutBitContext extra_vs;
  621. uint8_t extra_vs_data[5*6*128];
  622. uint8_t extra_mb_data[6*128];
  623. int QNO = 15;
  624. /* Stage 1 -- doing DCT on 5 MBs */
  625. block = &s->block[0][0];
  626. for(mb_index = 0; mb_index < 5; mb_index++) {
  627. v = *mb_pos_ptr++;
  628. mb_x = v & 0xff;
  629. mb_y = v >> 8;
  630. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  631. c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
  632. ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
  633. (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
  634. do_edge_wrap = 0;
  635. for(j = 0;j < 6; j++) {
  636. if (j < 4) { /* Four Y blocks */
  637. /* NOTE: at end of line, the macroblock is handled as 420 */
  638. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  639. data = y_ptr + (j * 8);
  640. } else {
  641. data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
  642. }
  643. linesize = s->picture.linesize[0];
  644. } else { /* Cr and Cb blocks */
  645. /* don't ask Fabrice why they inverted Cb and Cr ! */
  646. data = s->picture.data[6 - j] + c_offset;
  647. linesize = s->picture.linesize[6 - j];
  648. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
  649. do_edge_wrap = 1;
  650. }
  651. /* Everything is set up -- now just copy data -> DCT block */
  652. if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
  653. uint8_t* d;
  654. DCTELEM *b = block;
  655. for (i=0;i<8;i++) {
  656. d = data + 8 * linesize;
  657. b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
  658. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  659. data += linesize;
  660. b += 8;
  661. }
  662. } else { /* Simple copy: 8x8 -> 8x8 */
  663. s->get_pixels(block, data, linesize);
  664. }
  665. s->fdct(block);
  666. block += 64;
  667. }
  668. }
  669. /* Stage 2 -- setup for encoding phase */
  670. enc_blk = &enc_blks[0];
  671. block = &s->block[0][0];
  672. for (i=0; i<5; i++) {
  673. for (j=0; j<6; j++) {
  674. enc_blk->mb = block;
  675. enc_blk->dct_mode = 0;
  676. enc_blk->block_size = block_sizes[j];
  677. dv_set_class_number(enc_blk, j/4*(j%2));
  678. block += 64;
  679. enc_blk++;
  680. }
  681. }
  682. /* Stage 3 -- encoding by trial-and-error */
  683. encode_vs:
  684. enc_blk = &enc_blks[0];
  685. for (i=0; i<5; i++) {
  686. uint8_t* p = dif + i*80 + 4;
  687. for (j=0; j<6; j++) {
  688. enc_blk->qno = QNO;
  689. init_put_bits(&enc_blk->pb, p, block_sizes[j]/8, NULL, NULL);
  690. enc_blk++;
  691. p += block_sizes[j]/8;
  692. }
  693. }
  694. init_put_bits(&extra_vs, extra_vs_data, sizeof(extra_vs_data), NULL, NULL);
  695. free_vs_bits = 0;
  696. enc_blk = &enc_blks[0];
  697. for (i=0; i<5; i++) {
  698. PutBitContext extra_mb;
  699. EncBlockInfo* enc_blk2 = enc_blk;
  700. int free_mb_bits = 0;
  701. init_put_bits(&extra_mb, extra_mb_data, sizeof(extra_mb_data), NULL, NULL);
  702. dif[i*80 + 3] = enc_blk->qno;
  703. for (j=0; j<6; j++) {
  704. uint16_t dc = ((enc_blk->mb[0] >> 3) - 1024) >> 2;
  705. put_bits(&enc_blk->pb, 9, dc);
  706. put_bits(&enc_blk->pb, 1, enc_blk->dct_mode);
  707. put_bits(&enc_blk->pb, 2, enc_blk->cno);
  708. #ifdef VLC_DEBUG
  709. printf("[%d, %d]: ", i, j);
  710. #endif
  711. dv_encode_ac(enc_blk, &extra_mb);
  712. #ifdef VLC_DEBUG
  713. printf("\n");
  714. #endif
  715. free_mb_bits += dv_bits_left(enc_blk);
  716. enc_blk++;
  717. }
  718. /* We can't flush extra_mb just yet -- since it'll round up bit number */
  719. extra_bits = get_bit_count(&extra_mb);
  720. if (free_mb_bits > extra_bits)
  721. free_vs_bits += free_mb_bits - extra_bits;
  722. if (extra_bits) { /* FIXME: speed up things when free_mb_bits == 0 */
  723. flush_put_bits(&extra_mb);
  724. dv_redistr_bits(enc_blk2, 6, extra_mb_data, extra_bits, &extra_vs);
  725. }
  726. }
  727. /* We can't flush extra_mb just yet -- since it'll round up bit number */
  728. extra_bits = get_bit_count(&extra_vs);
  729. if (extra_bits > free_vs_bits && QNO) { /* FIXME: very crude trial-and-error */
  730. QNO--;
  731. goto encode_vs;
  732. }
  733. if (extra_bits) {
  734. flush_put_bits(&extra_vs);
  735. dv_redistr_bits(&enc_blks[0], 5*6, extra_vs_data, extra_bits, NULL);
  736. }
  737. for (i=0; i<6*5; i++) {
  738. flush_put_bits(&enc_blks[i].pb);
  739. #ifdef VLC_DEBUG
  740. printf("[%d:%d] qno=%d cno=%d\n", i/6, i%6, enc_blks[i].qno, enc_blks[i].cno);
  741. #endif
  742. }
  743. }
  744. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  745. 144000 bytes for PAL) */
  746. static int dvvideo_decode_frame(AVCodecContext *avctx,
  747. void *data, int *data_size,
  748. uint8_t *buf, int buf_size)
  749. {
  750. DVVideoDecodeContext *s = avctx->priv_data;
  751. int ds, vs;
  752. const uint16_t *mb_pos_ptr;
  753. s->sys = dv_frame_profile(buf);
  754. if (!s->sys || buf_size < s->sys->frame_size)
  755. return -1; /* NOTE: we only accept several full frames */
  756. if(s->picture.data[0])
  757. avctx->release_buffer(avctx, &s->picture);
  758. s->picture.reference = 0;
  759. avctx->pix_fmt = s->sys->pix_fmt;
  760. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  761. fprintf(stderr, "get_buffer() failed\n");
  762. return -1;
  763. }
  764. /* for each DIF segment */
  765. mb_pos_ptr = s->sys->video_place;
  766. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  767. buf += 6 * 80; /* skip DIF segment header */
  768. for(vs = 0; vs < 27; vs++) {
  769. if ((vs % 3) == 0)
  770. buf += 80; /* skip audio block */
  771. #ifdef VLC_DEBUG
  772. printf("********************* %d, %d **********************\n", ds, vs);
  773. #endif
  774. dv_decode_video_segment(s, buf, mb_pos_ptr);
  775. buf += 5 * 80;
  776. mb_pos_ptr += 5;
  777. }
  778. }
  779. emms_c();
  780. /* return image */
  781. *data_size = sizeof(AVFrame);
  782. *(AVFrame*)data= s->picture;
  783. return s->sys->frame_size;
  784. }
  785. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  786. void *data)
  787. {
  788. DVVideoDecodeContext *s = c->priv_data;
  789. const uint16_t *mb_pos_ptr;
  790. int ds, vs;
  791. s->sys = dv_codec_profile(c);
  792. if (!s->sys)
  793. return -1;
  794. c->pix_fmt = s->sys->pix_fmt;
  795. s->picture = *((AVFrame *)data);
  796. /* for each DIF segment */
  797. mb_pos_ptr = s->sys->video_place;
  798. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  799. buf += 6 * 80; /* skip DIF segment header */
  800. for(vs = 0; vs < 27; vs++) {
  801. if ((vs % 3) == 0)
  802. buf += 80; /* skip audio block */
  803. #ifdef VLC_DEBUG
  804. printf("********************* %d, %d **********************\n", ds, vs);
  805. #endif
  806. dv_encode_video_segment(s, buf, mb_pos_ptr);
  807. buf += 5 * 80;
  808. mb_pos_ptr += 5;
  809. }
  810. }
  811. emms_c();
  812. return s->sys->frame_size;
  813. }
  814. static int dvvideo_end(AVCodecContext *avctx)
  815. {
  816. avcodec_default_free_buffers(avctx);
  817. return 0;
  818. }
  819. AVCodec dvvideo_decoder = {
  820. "dvvideo",
  821. CODEC_TYPE_VIDEO,
  822. CODEC_ID_DVVIDEO,
  823. sizeof(DVVideoDecodeContext),
  824. dvvideo_init,
  825. dvvideo_encode_frame,
  826. dvvideo_end,
  827. dvvideo_decode_frame,
  828. CODEC_CAP_DR1,
  829. NULL
  830. };