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.

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