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.

958 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. AVFrame picture;
  37. uint8_t dv_zigzag[2][64];
  38. uint8_t dv_idct_shift[2][22][64];
  39. uint8_t dv_dct_shift[2][22][64];
  40. void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
  41. void (*fdct[2])(DCTELEM *block);
  42. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  43. GetBitContext gb;
  44. DCTELEM block[5*6][64] __align8;
  45. } DVVideoDecodeContext;
  46. #define TEX_VLC_BITS 9
  47. /* XXX: also include quantization */
  48. static RL_VLC_ELEM *dv_rl_vlc[1];
  49. static VLC_TYPE dv_vlc_codes[15][23];
  50. static void dv_build_unquantize_tables(DVVideoDecodeContext *s, uint8_t* perm)
  51. {
  52. int i, q, j;
  53. /* NOTE: max left shift is 6 */
  54. for(q = 0; q < 22; q++) {
  55. /* 88DCT */
  56. for(i = 1; i < 64; i++) {
  57. /* 88 table */
  58. j = perm[i];
  59. s->dv_idct_shift[0][q][j] =
  60. dv_quant_shifts[q][dv_88_areas[i]] + 1;
  61. s->dv_dct_shift[0][q][i] =
  62. dv_quant_shifts[q][dv_88_areas[ff_zigzag_direct[i]]] + 4;
  63. }
  64. /* 248DCT */
  65. for(i = 1; i < 64; i++) {
  66. /* 248 table */
  67. s->dv_idct_shift[1][q][i] =
  68. dv_quant_shifts[q][dv_248_areas[i]] + 1;
  69. s->dv_dct_shift[1][q][i] =
  70. dv_quant_shifts[q][dv_248_areas[ff_zigzag248_direct[i]]] + 4;
  71. }
  72. }
  73. }
  74. static int dvvideo_init(AVCodecContext *avctx)
  75. {
  76. DVVideoDecodeContext *s = avctx->priv_data;
  77. DSPContext dsp;
  78. static int done=0;
  79. int i;
  80. if (!done) {
  81. int i;
  82. VLC dv_vlc;
  83. done = 1;
  84. /* NOTE: as a trick, we use the fact the no codes are unused
  85. to accelerate the parsing of partial codes */
  86. init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
  87. dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
  88. dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  89. for(i = 0; i < dv_vlc.table_size; i++){
  90. int code= dv_vlc.table[i][0];
  91. int len = dv_vlc.table[i][1];
  92. int level, run;
  93. if(len<0){ //more bits needed
  94. run= 0;
  95. level= code;
  96. } else if (code == (NB_DV_VLC - 1)) {
  97. /* EOB */
  98. run = 0;
  99. level = 256;
  100. } else {
  101. run= dv_vlc_run[code] + 1;
  102. level= dv_vlc_level[code];
  103. }
  104. dv_rl_vlc[0][i].len = len;
  105. dv_rl_vlc[0][i].level = level;
  106. dv_rl_vlc[0][i].run = run;
  107. }
  108. memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
  109. for (i = 0; i < NB_DV_VLC - 1; i++) {
  110. if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
  111. dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
  112. }
  113. }
  114. /* Generic DSP setup */
  115. dsputil_init(&dsp, avctx);
  116. s->get_pixels = dsp.get_pixels;
  117. /* 88DCT setup */
  118. s->fdct[0] = dsp.fdct;
  119. s->idct_put[0] = dsp.idct_put;
  120. for (i=0; i<64; i++)
  121. s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
  122. /* 248DCT setup */
  123. s->fdct[1] = dsp.fdct248;
  124. s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
  125. memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
  126. /* XXX: do it only for constant case */
  127. dv_build_unquantize_tables(s, dsp.idct_permutation);
  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. av_log(NULL, AV_LOG_ERROR, "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_idct_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. const uint8_t* zigzag_scan;
  526. uint8_t *dv_shift;
  527. } EncBlockInfo;
  528. static inline int dv_bits_left(EncBlockInfo* bi)
  529. {
  530. return (bi->block_size - get_bit_count(&bi->pb));
  531. }
  532. static inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* heap)
  533. {
  534. int i, level, size, run = 0;
  535. uint32_t vlc;
  536. PutBitContext* cpb = &bi->pb;
  537. int bias = (bi->cno == 3);
  538. for (i=1; i<64; i++) {
  539. level = bi->mb[bi->zigzag_scan[i]] / (1<<(bi->dv_shift[i] + bias));
  540. if (level != 0) {
  541. size = dv_rl2vlc(run, level, &vlc);
  542. put_vlc:
  543. #ifdef VLC_DEBUG
  544. printf(" %3d:%3d", run, level);
  545. #endif
  546. if (cpb == &bi->pb && size > dv_bits_left(bi)) {
  547. size -= dv_bits_left(bi);
  548. put_bits(cpb, dv_bits_left(bi), vlc >> size);
  549. vlc = vlc & ((1<<size)-1);
  550. cpb = heap;
  551. }
  552. put_bits(cpb, size, vlc);
  553. run = 0;
  554. } else
  555. run++;
  556. }
  557. if (i == 64) {
  558. size = 4; vlc = 6; /* End Of Block stamp */
  559. goto put_vlc;
  560. }
  561. }
  562. static inline void dv_redistr_bits(EncBlockInfo* bi, int count, uint8_t* extra_data, int extra_bits, PutBitContext* heap)
  563. {
  564. int i;
  565. GetBitContext gb;
  566. init_get_bits(&gb, extra_data, extra_bits);
  567. for (i=0; i<count; i++) {
  568. int bits_left = dv_bits_left(bi);
  569. #ifdef VLC_DEBUG
  570. if (bits_left)
  571. printf("------------> inserting %d bytes in %d:%d\n", bits_left, i/6, i%6);
  572. #endif
  573. if (bits_left > extra_bits) {
  574. bit_copy(&bi->pb, &gb, extra_bits);
  575. extra_bits = 0;
  576. break;
  577. } else
  578. bit_copy(&bi->pb, &gb, bits_left);
  579. extra_bits -= bits_left;
  580. bi++;
  581. }
  582. if (extra_bits > 0 && heap)
  583. bit_copy(heap, &gb, extra_bits);
  584. }
  585. static inline void dv_set_class_number(EncBlockInfo* bi, int j)
  586. {
  587. int i, max_ac = 0;
  588. for (i=1; i<64; i++) {
  589. int ac = abs(bi->mb[ff_zigzag_direct[i]]) / 4;
  590. if (max_ac < ac)
  591. max_ac = ac;
  592. }
  593. if (max_ac < 12)
  594. bi->cno = j;
  595. else if (max_ac < 24)
  596. bi->cno = j + 1;
  597. else if (max_ac < 36)
  598. bi->cno = j + 2;
  599. else
  600. bi->cno = j + 3;
  601. if (bi->cno > 3)
  602. bi->cno = 3;
  603. }
  604. #define SQ(a) ((a)*(a))
  605. static int dv_score_lines(DCTELEM *s, int stride) {
  606. int score=0;
  607. int x, y;
  608. for(y=0; y<4; y++) {
  609. for(x=0; x<8; x+=4){
  610. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  611. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  612. }
  613. s+= stride;
  614. }
  615. return score;
  616. }
  617. /*
  618. * This is a very rough initial implementaion. The performance is
  619. * horrible and the weighting is missing. But it's missing from the
  620. * decoding step also -- so at least we're on the same page with decoder ;-)
  621. */
  622. static inline void dv_encode_video_segment(DVVideoDecodeContext *s,
  623. uint8_t *dif,
  624. const uint16_t *mb_pos_ptr)
  625. {
  626. int mb_index, i, j, v;
  627. int mb_x, mb_y, c_offset, linesize;
  628. uint8_t* y_ptr;
  629. uint8_t* data;
  630. int do_edge_wrap;
  631. DCTELEM *block;
  632. EncBlockInfo enc_blks[5*6];
  633. EncBlockInfo* enc_blk;
  634. int free_vs_bits;
  635. int extra_bits;
  636. PutBitContext extra_vs;
  637. uint8_t extra_vs_data[5*6*128];
  638. uint8_t extra_mb_data[6*128];
  639. int QNO = 15;
  640. /* Stage 1 -- doing DCT on 5 MBs */
  641. block = &s->block[0][0];
  642. enc_blk = &enc_blks[0];
  643. for(mb_index = 0; mb_index < 5; mb_index++) {
  644. v = *mb_pos_ptr++;
  645. mb_x = v & 0xff;
  646. mb_y = v >> 8;
  647. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  648. c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
  649. ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
  650. (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
  651. do_edge_wrap = 0;
  652. for(j = 0;j < 6; j++) {
  653. if (j < 4) { /* Four Y blocks */
  654. /* NOTE: at end of line, the macroblock is handled as 420 */
  655. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  656. data = y_ptr + (j * 8);
  657. } else {
  658. data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
  659. }
  660. linesize = s->picture.linesize[0];
  661. } else { /* Cr and Cb blocks */
  662. /* don't ask Fabrice why they inverted Cb and Cr ! */
  663. data = s->picture.data[6 - j] + c_offset;
  664. linesize = s->picture.linesize[6 - j];
  665. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
  666. do_edge_wrap = 1;
  667. }
  668. /* Everything is set up -- now just copy data -> DCT block */
  669. if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
  670. uint8_t* d;
  671. DCTELEM *b = block;
  672. for (i=0;i<8;i++) {
  673. d = data + 8 * linesize;
  674. b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
  675. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  676. data += linesize;
  677. b += 8;
  678. }
  679. } else { /* Simple copy: 8x8 -> 8x8 */
  680. s->get_pixels(block, data, linesize);
  681. }
  682. if (dv_score_lines(block, 8) + dv_score_lines(block+8*4, 8) - 100 >
  683. dv_score_lines(block, 16) + dv_score_lines(block+8, 16)) {
  684. enc_blk->dct_mode = 1;
  685. enc_blk->zigzag_scan = ff_zigzag248_direct;
  686. } else {
  687. enc_blk->dct_mode = 0;
  688. enc_blk->zigzag_scan = ff_zigzag_direct;
  689. }
  690. enc_blk->mb = block;
  691. enc_blk->block_size = block_sizes[j];
  692. s->fdct[enc_blk->dct_mode](block);
  693. dv_set_class_number(enc_blk, j/4*(j%2));
  694. block += 64;
  695. enc_blk++;
  696. }
  697. }
  698. /* Stage 2 -- encoding by trial-and-error */
  699. encode_vs:
  700. enc_blk = &enc_blks[0];
  701. for (i=0; i<5; i++) {
  702. uint8_t* p = dif + i*80 + 4;
  703. for (j=0; j<6; j++) {
  704. enc_blk->qno = QNO;
  705. enc_blk->dv_shift = &(s->dv_dct_shift[0]
  706. [QNO + dv_quant_offset[enc_blk->cno]][0]);
  707. init_put_bits(&enc_blk->pb, p, block_sizes[j]/8);
  708. enc_blk++;
  709. p += block_sizes[j]/8;
  710. }
  711. }
  712. init_put_bits(&extra_vs, extra_vs_data, sizeof(extra_vs_data));
  713. free_vs_bits = 0;
  714. enc_blk = &enc_blks[0];
  715. for (i=0; i<5; i++) {
  716. PutBitContext extra_mb;
  717. EncBlockInfo* enc_blk2 = enc_blk;
  718. int free_mb_bits = 0;
  719. init_put_bits(&extra_mb, extra_mb_data, sizeof(extra_mb_data));
  720. dif[i*80 + 3] = enc_blk->qno;
  721. for (j=0; j<6; j++) {
  722. uint16_t dc = ((enc_blk->mb[0] >> 3) - 1024) >> 2;
  723. put_bits(&enc_blk->pb, 9, dc);
  724. put_bits(&enc_blk->pb, 1, enc_blk->dct_mode);
  725. put_bits(&enc_blk->pb, 2, enc_blk->cno);
  726. #ifdef VLC_DEBUG
  727. printf("[%d, %d]: ", i, j);
  728. #endif
  729. dv_encode_ac(enc_blk, &extra_mb);
  730. #ifdef VLC_DEBUG
  731. printf("\n");
  732. #endif
  733. free_mb_bits += dv_bits_left(enc_blk);
  734. enc_blk++;
  735. }
  736. /* We can't flush extra_mb just yet -- since it'll round up bit number */
  737. extra_bits = get_bit_count(&extra_mb);
  738. if (free_mb_bits > extra_bits)
  739. free_vs_bits += free_mb_bits - extra_bits;
  740. if (extra_bits) { /* FIXME: speed up things when free_mb_bits == 0 */
  741. flush_put_bits(&extra_mb);
  742. dv_redistr_bits(enc_blk2, 6, extra_mb_data, extra_bits, &extra_vs);
  743. }
  744. }
  745. /* We can't flush extra_mb just yet -- since it'll round up bit number */
  746. extra_bits = get_bit_count(&extra_vs);
  747. if (extra_bits > free_vs_bits && QNO) { /* FIXME: very crude trial-and-error */
  748. QNO--;
  749. goto encode_vs;
  750. }
  751. if (extra_bits) {
  752. flush_put_bits(&extra_vs);
  753. dv_redistr_bits(&enc_blks[0], 5*6, extra_vs_data, extra_bits, NULL);
  754. }
  755. for (i=0; i<6*5; i++) {
  756. flush_put_bits(&enc_blks[i].pb);
  757. #ifdef VLC_DEBUG
  758. printf("[%d:%d] qno=%d cno=%d\n", i/6, i%6, enc_blks[i].qno, enc_blks[i].cno);
  759. #endif
  760. }
  761. }
  762. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  763. 144000 bytes for PAL) */
  764. static int dvvideo_decode_frame(AVCodecContext *avctx,
  765. void *data, int *data_size,
  766. uint8_t *buf, int buf_size)
  767. {
  768. DVVideoDecodeContext *s = avctx->priv_data;
  769. int ds, vs;
  770. const uint16_t *mb_pos_ptr;
  771. s->sys = dv_frame_profile(buf);
  772. if (!s->sys || buf_size < s->sys->frame_size)
  773. return -1; /* NOTE: we only accept several full frames */
  774. if(s->picture.data[0])
  775. avctx->release_buffer(avctx, &s->picture);
  776. s->picture.reference = 0;
  777. avctx->pix_fmt = s->sys->pix_fmt;
  778. avctx->width = s->sys->width;
  779. avctx->height = s->sys->height;
  780. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  781. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  782. return -1;
  783. }
  784. s->picture.interlaced_frame = 1;
  785. s->picture.top_field_first = 0;
  786. /* for each DIF segment */
  787. mb_pos_ptr = s->sys->video_place;
  788. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  789. buf += 6 * 80; /* skip DIF segment header */
  790. for(vs = 0; vs < 27; vs++) {
  791. if ((vs % 3) == 0)
  792. buf += 80; /* skip audio block */
  793. #ifdef VLC_DEBUG
  794. printf("********************* %d, %d **********************\n", ds, vs);
  795. #endif
  796. dv_decode_video_segment(s, buf, mb_pos_ptr);
  797. buf += 5 * 80;
  798. mb_pos_ptr += 5;
  799. }
  800. }
  801. emms_c();
  802. /* return image */
  803. *data_size = sizeof(AVFrame);
  804. *(AVFrame*)data= s->picture;
  805. return s->sys->frame_size;
  806. }
  807. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  808. void *data)
  809. {
  810. DVVideoDecodeContext *s = c->priv_data;
  811. const uint16_t *mb_pos_ptr;
  812. int ds, vs;
  813. s->sys = dv_codec_profile(c);
  814. if (!s->sys)
  815. return -1;
  816. c->pix_fmt = s->sys->pix_fmt;
  817. s->picture = *((AVFrame *)data);
  818. /* for each DIF segment */
  819. mb_pos_ptr = s->sys->video_place;
  820. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  821. buf += 6 * 80; /* skip DIF segment header */
  822. for(vs = 0; vs < 27; vs++) {
  823. if ((vs % 3) == 0)
  824. buf += 80; /* skip audio block */
  825. #ifdef VLC_DEBUG
  826. printf("********************* %d, %d **********************\n", ds, vs);
  827. #endif
  828. dv_encode_video_segment(s, buf, mb_pos_ptr);
  829. buf += 5 * 80;
  830. mb_pos_ptr += 5;
  831. }
  832. }
  833. emms_c();
  834. return s->sys->frame_size;
  835. }
  836. static int dvvideo_end(AVCodecContext *avctx)
  837. {
  838. avcodec_default_free_buffers(avctx);
  839. return 0;
  840. }
  841. AVCodec dvvideo_decoder = {
  842. "dvvideo",
  843. CODEC_TYPE_VIDEO,
  844. CODEC_ID_DVVIDEO,
  845. sizeof(DVVideoDecodeContext),
  846. dvvideo_init,
  847. dvvideo_encode_frame,
  848. dvvideo_end,
  849. dvvideo_decode_frame,
  850. CODEC_CAP_DR1,
  851. NULL
  852. };