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.

1025 lines
31KB

  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. void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
  40. void (*fdct[2])(DCTELEM *block);
  41. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  42. GetBitContext gb;
  43. DCTELEM block[5*6][64] __align8;
  44. } DVVideoDecodeContext;
  45. #define TEX_VLC_BITS 9
  46. #ifdef DV_CODEC_TINY_TARGET
  47. #define DV_VLC_MAP_RUN_SIZE 15
  48. #define DV_VLC_MAP_LEV_SIZE 23
  49. #else
  50. #define DV_VLC_MAP_RUN_SIZE 64
  51. #define DV_VLC_MAP_LEV_SIZE 512
  52. #endif
  53. /* XXX: also include quantization */
  54. static RL_VLC_ELEM *dv_rl_vlc[1];
  55. /* VLC encoding lookup table */
  56. static struct dv_vlc_pair {
  57. uint32_t vlc;
  58. uint8_t size;
  59. } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL;
  60. static void dv_build_unquantize_tables(DVVideoDecodeContext *s, uint8_t* perm)
  61. {
  62. int i, q, j;
  63. /* NOTE: max left shift is 6 */
  64. for(q = 0; q < 22; q++) {
  65. /* 88DCT */
  66. for(i = 1; i < 64; i++) {
  67. /* 88 table */
  68. j = perm[i];
  69. s->dv_idct_shift[0][q][j] =
  70. dv_quant_shifts[q][dv_88_areas[i]] + 1;
  71. }
  72. /* 248DCT */
  73. for(i = 1; i < 64; i++) {
  74. /* 248 table */
  75. s->dv_idct_shift[1][q][i] =
  76. dv_quant_shifts[q][dv_248_areas[i]] + 1;
  77. }
  78. }
  79. }
  80. static int dvvideo_init(AVCodecContext *avctx)
  81. {
  82. DVVideoDecodeContext *s = avctx->priv_data;
  83. DSPContext dsp;
  84. static int done=0;
  85. int i, j;
  86. if (!done) {
  87. int i;
  88. VLC dv_vlc;
  89. done = 1;
  90. dv_vlc_map = av_mallocz(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
  91. if (!dv_vlc_map)
  92. return -ENOMEM;
  93. /* NOTE: as a trick, we use the fact the no codes are unused
  94. to accelerate the parsing of partial codes */
  95. init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
  96. dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
  97. dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  98. if (!dv_rl_vlc[0]) {
  99. av_free(dv_vlc_map);
  100. return -ENOMEM;
  101. }
  102. for(i = 0; i < dv_vlc.table_size; i++){
  103. int code= dv_vlc.table[i][0];
  104. int len = dv_vlc.table[i][1];
  105. int level, run;
  106. if(len<0){ //more bits needed
  107. run= 0;
  108. level= code;
  109. } else if (code == (NB_DV_VLC - 1)) {
  110. /* EOB */
  111. run = 0;
  112. level = 256;
  113. } else {
  114. run= dv_vlc_run[code] + 1;
  115. level= dv_vlc_level[code];
  116. }
  117. dv_rl_vlc[0][i].len = len;
  118. dv_rl_vlc[0][i].level = level;
  119. dv_rl_vlc[0][i].run = run;
  120. }
  121. for (i = 0; i < NB_DV_VLC - 1; i++) {
  122. if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE || dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
  123. continue;
  124. if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
  125. continue;
  126. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
  127. (!!dv_vlc_level[i]);
  128. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
  129. (!!dv_vlc_level[i]);
  130. }
  131. for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
  132. #ifdef DV_CODEC_TINY_TARGET
  133. for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
  134. if (dv_vlc_map[i][j].size == 0) {
  135. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  136. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  137. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  138. dv_vlc_map[0][j].size;
  139. }
  140. }
  141. #else
  142. for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
  143. if (dv_vlc_map[i][j].size == 0) {
  144. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  145. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  146. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  147. dv_vlc_map[0][j].size;
  148. }
  149. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
  150. dv_vlc_map[i][j].vlc | 1;
  151. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
  152. dv_vlc_map[i][j].size;
  153. }
  154. #endif
  155. }
  156. }
  157. /* Generic DSP setup */
  158. dsputil_init(&dsp, avctx);
  159. s->get_pixels = dsp.get_pixels;
  160. /* 88DCT setup */
  161. s->fdct[0] = dsp.fdct;
  162. s->idct_put[0] = dsp.idct_put;
  163. for (i=0; i<64; i++)
  164. s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
  165. /* 248DCT setup */
  166. s->fdct[1] = dsp.fdct248;
  167. s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
  168. memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
  169. /* XXX: do it only for constant case */
  170. dv_build_unquantize_tables(s, dsp.idct_permutation);
  171. /* FIXME: I really don't think this should be here */
  172. if (dv_codec_profile(avctx))
  173. avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
  174. avctx->coded_frame = &s->picture;
  175. return 0;
  176. }
  177. // #define VLC_DEBUG
  178. typedef struct BlockInfo {
  179. const uint8_t *shift_table;
  180. const uint8_t *scan_table;
  181. uint8_t pos; /* position in block */
  182. uint8_t eob_reached; /* true if EOB has been reached */
  183. uint8_t dct_mode;
  184. uint8_t partial_bit_count;
  185. uint16_t partial_bit_buffer;
  186. int shift_offset;
  187. } BlockInfo;
  188. /* block size in bits */
  189. static const uint16_t block_sizes[6] = {
  190. 112, 112, 112, 112, 80, 80
  191. };
  192. /* bit budget for AC only in 5 MBs */
  193. static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
  194. /* see dv_88_areas and dv_248_areas for details */
  195. static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
  196. #ifndef ALT_BITSTREAM_READER
  197. #warning only works with ALT_BITSTREAM_READER
  198. #endif
  199. /* decode ac coefs */
  200. static void dv_decode_ac(DVVideoDecodeContext *s,
  201. BlockInfo *mb, DCTELEM *block, int last_index)
  202. {
  203. int last_re_index;
  204. int shift_offset = mb->shift_offset;
  205. const uint8_t *scan_table = mb->scan_table;
  206. const uint8_t *shift_table = mb->shift_table;
  207. int pos = mb->pos;
  208. int level, pos1, sign, run;
  209. int partial_bit_count;
  210. #ifndef ALT_BITSTREAM_READER //FIXME
  211. int re_index=0;
  212. int re1_index=0;
  213. #endif
  214. OPEN_READER(re, &s->gb);
  215. #ifdef VLC_DEBUG
  216. printf("start\n");
  217. #endif
  218. /* if we must parse a partial vlc, we do it here */
  219. partial_bit_count = mb->partial_bit_count;
  220. if (partial_bit_count > 0) {
  221. uint8_t buf[4];
  222. uint32_t v;
  223. int l, l1;
  224. GetBitContext gb1;
  225. /* build the dummy bit buffer */
  226. l = 16 - partial_bit_count;
  227. UPDATE_CACHE(re, &s->gb);
  228. #ifdef VLC_DEBUG
  229. printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
  230. #endif
  231. v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
  232. buf[0] = v >> 8;
  233. buf[1] = v;
  234. #ifdef VLC_DEBUG
  235. printf("v=%04x cnt=%d %04x\n",
  236. v, partial_bit_count, (mb->partial_bit_buffer << l));
  237. #endif
  238. /* try to read the codeword */
  239. init_get_bits(&gb1, buf, 4*8);
  240. {
  241. OPEN_READER(re1, &gb1);
  242. UPDATE_CACHE(re1, &gb1);
  243. GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
  244. TEX_VLC_BITS, 2);
  245. l = re1_index;
  246. CLOSE_READER(re1, &gb1);
  247. }
  248. #ifdef VLC_DEBUG
  249. printf("****run=%d level=%d size=%d\n", run, level, l);
  250. #endif
  251. /* compute codeword length */
  252. l1 = (level != 256 && level != 0);
  253. /* if too long, we cannot parse */
  254. l -= partial_bit_count;
  255. if ((re_index + l + l1) > last_index)
  256. return;
  257. /* skip read bits */
  258. last_re_index = 0; /* avoid warning */
  259. re_index += l;
  260. /* by definition, if we can read the vlc, all partial bits
  261. will be read (otherwise we could have read the vlc before) */
  262. mb->partial_bit_count = 0;
  263. UPDATE_CACHE(re, &s->gb);
  264. goto handle_vlc;
  265. }
  266. /* get the AC coefficients until last_index is reached */
  267. for(;;) {
  268. UPDATE_CACHE(re, &s->gb);
  269. #ifdef VLC_DEBUG
  270. printf("%2d: bits=%04x index=%d\n",
  271. pos, SHOW_UBITS(re, &s->gb, 16), re_index);
  272. #endif
  273. last_re_index = re_index;
  274. GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
  275. TEX_VLC_BITS, 2);
  276. handle_vlc:
  277. #ifdef VLC_DEBUG
  278. printf("run=%d level=%d\n", run, level);
  279. #endif
  280. if (level == 256) {
  281. if (re_index > last_index) {
  282. cannot_read:
  283. /* put position before read code */
  284. re_index = last_re_index;
  285. mb->eob_reached = 0;
  286. break;
  287. }
  288. /* EOB */
  289. mb->eob_reached = 1;
  290. break;
  291. } else if (level != 0) {
  292. if ((re_index + 1) > last_index)
  293. goto cannot_read;
  294. sign = SHOW_SBITS(re, &s->gb, 1);
  295. level = (level ^ sign) - sign;
  296. LAST_SKIP_BITS(re, &s->gb, 1);
  297. pos += run;
  298. /* error */
  299. if (pos >= 64) {
  300. goto read_error;
  301. }
  302. pos1 = scan_table[pos];
  303. level = level << (shift_table[pos1] + shift_offset);
  304. block[pos1] = level;
  305. // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
  306. } else {
  307. if (re_index > last_index)
  308. goto cannot_read;
  309. /* level is zero: means run without coding. No
  310. sign is coded */
  311. pos += run;
  312. /* error */
  313. if (pos >= 64) {
  314. read_error:
  315. #if defined(VLC_DEBUG) || 1
  316. av_log(NULL, AV_LOG_ERROR, "error pos=%d\n", pos);
  317. #endif
  318. /* for errors, we consider the eob is reached */
  319. mb->eob_reached = 1;
  320. break;
  321. }
  322. }
  323. }
  324. CLOSE_READER(re, &s->gb);
  325. mb->pos = pos;
  326. }
  327. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
  328. {
  329. while (bits_left >= 16) {
  330. put_bits(pb, 16, get_bits(gb, 16));
  331. bits_left -= 16;
  332. }
  333. if (bits_left > 0) {
  334. put_bits(pb, bits_left, get_bits(gb, bits_left));
  335. }
  336. }
  337. /* mb_x and mb_y are in units of 8 pixels */
  338. static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
  339. uint8_t *buf_ptr1,
  340. const uint16_t *mb_pos_ptr)
  341. {
  342. int quant, dc, dct_mode, class1, j;
  343. int mb_index, mb_x, mb_y, v, last_index;
  344. DCTELEM *block, *block1;
  345. int c_offset, bits_left;
  346. uint8_t *y_ptr;
  347. BlockInfo mb_data[5 * 6], *mb, *mb1;
  348. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  349. uint8_t *buf_ptr;
  350. PutBitContext pb, vs_pb;
  351. uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
  352. int mb_bit_count;
  353. uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
  354. int vs_bit_count;
  355. memset(s->block, 0, sizeof(s->block));
  356. /* pass 1 : read DC and AC coefficients in blocks */
  357. buf_ptr = buf_ptr1;
  358. block1 = &s->block[0][0];
  359. mb1 = mb_data;
  360. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
  361. vs_bit_count = 0;
  362. for(mb_index = 0; mb_index < 5; mb_index++) {
  363. /* skip header */
  364. quant = buf_ptr[3] & 0x0f;
  365. buf_ptr += 4;
  366. init_put_bits(&pb, mb_bit_buffer, 80);
  367. mb_bit_count = 0;
  368. mb = mb1;
  369. block = block1;
  370. for(j = 0;j < 6; j++) {
  371. /* NOTE: size is not important here */
  372. init_get_bits(&s->gb, buf_ptr, 14*8);
  373. /* get the dc */
  374. dc = get_bits(&s->gb, 9);
  375. dc = (dc << (32 - 9)) >> (32 - 9);
  376. dct_mode = get_bits1(&s->gb);
  377. mb->dct_mode = dct_mode;
  378. mb->scan_table = s->dv_zigzag[dct_mode];
  379. class1 = get_bits(&s->gb, 2);
  380. mb->shift_offset = (class1 == 3);
  381. mb->shift_table = s->dv_idct_shift[dct_mode]
  382. [quant + dv_quant_offset[class1]];
  383. dc = dc << 2;
  384. /* convert to unsigned because 128 is not added in the
  385. standard IDCT */
  386. dc += 1024;
  387. block[0] = dc;
  388. last_index = block_sizes[j];
  389. buf_ptr += last_index >> 3;
  390. mb->pos = 0;
  391. mb->partial_bit_count = 0;
  392. #ifdef VLC_DEBUG
  393. printf("MB block: %d, %d ", mb_index, j);
  394. #endif
  395. dv_decode_ac(s, mb, block, last_index);
  396. /* write the remaining bits in a new buffer only if the
  397. block is finished */
  398. bits_left = last_index - get_bits_count(&s->gb);
  399. if (mb->eob_reached) {
  400. mb->partial_bit_count = 0;
  401. mb_bit_count += bits_left;
  402. bit_copy(&pb, &s->gb, bits_left);
  403. } else {
  404. /* should be < 16 bits otherwise a codeword could have
  405. been parsed */
  406. mb->partial_bit_count = bits_left;
  407. mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
  408. }
  409. block += 64;
  410. mb++;
  411. }
  412. flush_put_bits(&pb);
  413. /* pass 2 : we can do it just after */
  414. #ifdef VLC_DEBUG
  415. printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index);
  416. #endif
  417. block = block1;
  418. mb = mb1;
  419. init_get_bits(&s->gb, mb_bit_buffer, 80*8);
  420. for(j = 0;j < 6; j++) {
  421. if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
  422. dv_decode_ac(s, mb, block, mb_bit_count);
  423. /* if still not finished, no need to parse other blocks */
  424. if (!mb->eob_reached) {
  425. /* we could not parse the current AC coefficient,
  426. so we add the remaining bytes */
  427. bits_left = mb_bit_count - get_bits_count(&s->gb);
  428. if (bits_left > 0) {
  429. mb->partial_bit_count += bits_left;
  430. mb->partial_bit_buffer =
  431. (mb->partial_bit_buffer << bits_left) |
  432. get_bits(&s->gb, bits_left);
  433. }
  434. goto next_mb;
  435. }
  436. }
  437. block += 64;
  438. mb++;
  439. }
  440. /* all blocks are finished, so the extra bytes can be used at
  441. the video segment level */
  442. bits_left = mb_bit_count - get_bits_count(&s->gb);
  443. vs_bit_count += bits_left;
  444. bit_copy(&vs_pb, &s->gb, bits_left);
  445. next_mb:
  446. mb1 += 6;
  447. block1 += 6 * 64;
  448. }
  449. /* we need a pass other the whole video segment */
  450. flush_put_bits(&vs_pb);
  451. #ifdef VLC_DEBUG
  452. printf("***pass 3 size=%d\n", vs_bit_count);
  453. #endif
  454. block = &s->block[0][0];
  455. mb = mb_data;
  456. init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
  457. for(mb_index = 0; mb_index < 5; mb_index++) {
  458. for(j = 0;j < 6; j++) {
  459. if (!mb->eob_reached) {
  460. #ifdef VLC_DEBUG
  461. printf("start %d:%d\n", mb_index, j);
  462. #endif
  463. dv_decode_ac(s, mb, block, vs_bit_count);
  464. }
  465. block += 64;
  466. mb++;
  467. }
  468. }
  469. /* compute idct and place blocks */
  470. block = &s->block[0][0];
  471. mb = mb_data;
  472. for(mb_index = 0; mb_index < 5; mb_index++) {
  473. v = *mb_pos_ptr++;
  474. mb_x = v & 0xff;
  475. mb_y = v >> 8;
  476. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  477. if (s->sys->pix_fmt == PIX_FMT_YUV411P)
  478. c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
  479. else
  480. c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
  481. for(j = 0;j < 6; j++) {
  482. idct_put = s->idct_put[mb->dct_mode];
  483. if (j < 4) {
  484. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  485. /* NOTE: at end of line, the macroblock is handled as 420 */
  486. idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
  487. } else {
  488. idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
  489. s->picture.linesize[0], block);
  490. }
  491. } else {
  492. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  493. uint64_t aligned_pixels[64/8];
  494. uint8_t *pixels= (uint8_t*)aligned_pixels;
  495. uint8_t *c_ptr, *c_ptr1, *ptr;
  496. int y, linesize;
  497. /* NOTE: at end of line, the macroblock is handled as 420 */
  498. idct_put(pixels, 8, block);
  499. linesize = s->picture.linesize[6 - j];
  500. c_ptr = s->picture.data[6 - j] + c_offset;
  501. ptr = pixels;
  502. for(y = 0;y < 8; y++) {
  503. /* convert to 411P */
  504. c_ptr1 = c_ptr + 8*linesize;
  505. c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
  506. c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
  507. c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
  508. c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
  509. c_ptr += linesize;
  510. ptr += 8;
  511. }
  512. } else {
  513. /* don't ask me why they inverted Cb and Cr ! */
  514. idct_put(s->picture.data[6 - j] + c_offset,
  515. s->picture.linesize[6 - j], block);
  516. }
  517. }
  518. block += 64;
  519. mb++;
  520. }
  521. }
  522. }
  523. #ifdef DV_CODEC_TINY_TARGET
  524. /* Converts run and level (where level != 0) pair into vlc, returning bit size */
  525. static always_inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
  526. {
  527. int sign = l >> 8;
  528. int level = (l ^ sign) - sign;
  529. int size;
  530. sign = (sign & 1);
  531. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  532. *vlc = dv_vlc_map[run][level].vlc | sign;
  533. size = dv_vlc_map[run][level].size;
  534. }
  535. else {
  536. if (level < DV_VLC_MAP_LEV_SIZE) {
  537. *vlc = dv_vlc_map[0][level].vlc | sign;
  538. size = dv_vlc_map[0][level].size;
  539. } else {
  540. *vlc = 0xfe00 | (level << 1) | sign;
  541. size = 16;
  542. }
  543. if (run) {
  544. *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
  545. (0x1f80 | (run - 1))) << size;
  546. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  547. }
  548. }
  549. return size;
  550. }
  551. static always_inline int dv_rl2vlc_size(int run, int l)
  552. {
  553. int level = (l ^ (l >> 8)) - (l >> 8);
  554. int size;
  555. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  556. size = dv_vlc_map[run][level].size;
  557. }
  558. else {
  559. size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
  560. if (run) {
  561. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  562. }
  563. }
  564. return size;
  565. }
  566. #else
  567. static always_inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
  568. {
  569. *vlc = dv_vlc_map[run][((uint16_t)l)&0x1ff].vlc;
  570. return dv_vlc_map[run][((uint16_t)l)&0x1ff].size;
  571. }
  572. static always_inline int dv_rl2vlc_size(int run, int l)
  573. {
  574. return dv_vlc_map[run][((uint16_t)l)&0x1ff].size;
  575. }
  576. #endif
  577. typedef struct EncBlockInfo {
  578. int area_q[4];
  579. int bit_size[4];
  580. int prev_run[4];
  581. int cur_ac;
  582. int cno;
  583. int dct_mode;
  584. DCTELEM *mb;
  585. uint8_t partial_bit_count;
  586. uint32_t partial_bit_buffer; /* we can't use uint16_t here */
  587. } EncBlockInfo;
  588. static always_inline int dv_bits_left(PutBitContext* s)
  589. {
  590. return (s->buf_end - s->buf) * 8 -
  591. ((s->buf_ptr - s->buf) * 8 + 32 - (int64_t)s->bit_left);
  592. }
  593. static always_inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
  594. int pb_size)
  595. {
  596. int run;
  597. int bits_left;
  598. PutBitContext* pb = pb_pool;
  599. int size = bi->partial_bit_count;
  600. uint32_t vlc = bi->partial_bit_buffer;
  601. bi->partial_bit_count = bi->partial_bit_buffer = 0;
  602. vlc_loop:
  603. /* Find suitable storage space */
  604. for (; size > (bits_left = dv_bits_left(pb)); pb++) {
  605. if (bits_left) {
  606. size -= bits_left;
  607. put_bits(pb, bits_left, vlc >> size);
  608. vlc = vlc & ((1<<size)-1);
  609. }
  610. if (pb_size == 1) {
  611. bi->partial_bit_count = size;
  612. bi->partial_bit_buffer = vlc;
  613. return;
  614. }
  615. --pb_size;
  616. }
  617. /* Store VLC */
  618. put_bits(pb, size, vlc);
  619. /* Construct the next VLC */
  620. run = 0;
  621. for (; bi->cur_ac < 64; bi->cur_ac++, run++) {
  622. if (bi->mb[bi->cur_ac]) {
  623. size = dv_rl2vlc(run, bi->mb[bi->cur_ac], &vlc);
  624. bi->cur_ac++;
  625. goto vlc_loop;
  626. }
  627. }
  628. if (bi->cur_ac == 64) {
  629. size = 4; vlc = 6; /* End Of Block stamp */
  630. bi->cur_ac++;
  631. goto vlc_loop;
  632. }
  633. }
  634. static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
  635. const uint8_t* zigzag_scan, int bias)
  636. {
  637. int i, area;
  638. int run;
  639. int classes[] = {12, 24, 36, 0xffff};
  640. run = 0;
  641. bi->mb[0] = blk[0];
  642. bi->cno = 0;
  643. for (area = 0; area < 4; area++) {
  644. bi->prev_run[area] = run;
  645. bi->bit_size[area] = 0;
  646. for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
  647. bi->mb[i] = (blk[zigzag_scan[i]] / 16);
  648. while ((bi->mb[i] ^ (bi->mb[i] >> 8)) > classes[bi->cno])
  649. bi->cno++;
  650. if (bi->mb[i]) {
  651. bi->bit_size[area] += dv_rl2vlc_size(run, bi->mb[i]);
  652. run = 0;
  653. } else
  654. ++run;
  655. }
  656. }
  657. bi->bit_size[3] += 4; /* EOB marker */
  658. bi->cno += bias;
  659. if (bi->cno >= 3) { /* FIXME: we have to recreate bit_size[], prev_run[] */
  660. bi->cno = 3;
  661. for (i=1; i<64; i++)
  662. bi->mb[i] /= 2;
  663. }
  664. }
  665. #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
  666. static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
  667. DCTELEM *s;
  668. int score88 = 0;
  669. int score248 = 0;
  670. int i;
  671. /* Compute 8-8 score (small values give a better chance for 8-8 DCT) */
  672. s = blk;
  673. for(i=0; i<7; i++) {
  674. score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
  675. SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
  676. s += 8;
  677. }
  678. /* Compute 2-4-8 score (small values give a better chance for 2-4-8 DCT) */
  679. s = blk;
  680. for(i=0; i<6; i++) {
  681. score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
  682. SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
  683. s += 8;
  684. }
  685. return (score88 - score248 > -10);
  686. }
  687. static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
  688. {
  689. int size[5];
  690. int i, j, k, a, run;
  691. EncBlockInfo* b;
  692. do {
  693. b = blks;
  694. for (i=0; i<5; i++) {
  695. if (!qnos[i])
  696. continue;
  697. qnos[i]--;
  698. size[i] = 0;
  699. for (j=0; j<6; j++, b++) {
  700. for (a=0; a<4; a++) {
  701. if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
  702. b->bit_size[a] = (a==3)?4:0;
  703. b->area_q[a]++;
  704. run = b->prev_run[a];
  705. for (k=mb_area_start[a]; k<mb_area_start[a+1]; k++) {
  706. b->mb[k] /= 2;
  707. if (b->mb[k]) {
  708. b->bit_size[a] += dv_rl2vlc_size(run, b->mb[k]);
  709. run = 0;
  710. } else
  711. ++run;
  712. }
  713. }
  714. size[i] += b->bit_size[a];
  715. }
  716. }
  717. }
  718. } while ((vs_total_ac_bits < size[0] + size[1] + size[2] + size[3] + size[4]) &&
  719. (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]));
  720. }
  721. /*
  722. * This is a very rough initial implementaion. The performance is
  723. * horrible and the weighting is missing. But it's missing from the
  724. * decoding step also -- so at least we're on the same page with decoder ;-)
  725. */
  726. static inline void dv_encode_video_segment(DVVideoDecodeContext *s,
  727. uint8_t *dif,
  728. const uint16_t *mb_pos_ptr)
  729. {
  730. int mb_index, i, j, v;
  731. int mb_x, mb_y, c_offset, linesize;
  732. uint8_t* y_ptr;
  733. uint8_t* data;
  734. uint8_t* ptr;
  735. int do_edge_wrap;
  736. DCTELEM block[64] __align8;
  737. EncBlockInfo enc_blks[5*6];
  738. PutBitContext pbs[5*6];
  739. PutBitContext* pb;
  740. EncBlockInfo* enc_blk;
  741. int vs_bit_size = 0;
  742. int qnos[5];
  743. enc_blk = &enc_blks[0];
  744. pb = &pbs[0];
  745. for(mb_index = 0; mb_index < 5; mb_index++) {
  746. v = *mb_pos_ptr++;
  747. mb_x = v & 0xff;
  748. mb_y = v >> 8;
  749. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  750. c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
  751. ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
  752. (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
  753. do_edge_wrap = 0;
  754. qnos[mb_index] = 15; /* No quantization */
  755. ptr = dif + mb_index*80 + 4;
  756. for(j = 0;j < 6; j++) {
  757. if (j < 4) { /* Four Y blocks */
  758. /* NOTE: at end of line, the macroblock is handled as 420 */
  759. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  760. data = y_ptr + (j * 8);
  761. } else {
  762. data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
  763. }
  764. linesize = s->picture.linesize[0];
  765. } else { /* Cr and Cb blocks */
  766. /* don't ask Fabrice why they inverted Cb and Cr ! */
  767. data = s->picture.data[6 - j] + c_offset;
  768. linesize = s->picture.linesize[6 - j];
  769. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
  770. do_edge_wrap = 1;
  771. }
  772. /* Everything is set up -- now just copy data -> DCT block */
  773. if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
  774. uint8_t* d;
  775. DCTELEM *b = block;
  776. for (i=0;i<8;i++) {
  777. d = data + 8 * linesize;
  778. b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
  779. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  780. data += linesize;
  781. b += 8;
  782. }
  783. } else { /* Simple copy: 8x8 -> 8x8 */
  784. s->get_pixels(block, data, linesize);
  785. }
  786. enc_blk->dct_mode = dv_guess_dct_mode(block);
  787. enc_blk->mb = &s->block[mb_index*6+j][0];
  788. enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
  789. enc_blk->partial_bit_count = 0;
  790. enc_blk->partial_bit_buffer = 0;
  791. enc_blk->cur_ac = 1;
  792. s->fdct[enc_blk->dct_mode](block);
  793. dv_set_class_number(block, enc_blk,
  794. enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
  795. j/4*(j%2));
  796. init_put_bits(pb, ptr, block_sizes[j]/8);
  797. put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024) >> 2));
  798. put_bits(pb, 1, enc_blk->dct_mode);
  799. put_bits(pb, 2, enc_blk->cno);
  800. vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
  801. enc_blk->bit_size[2] + enc_blk->bit_size[3];
  802. ++enc_blk;
  803. ++pb;
  804. ptr += block_sizes[j]/8;
  805. }
  806. }
  807. if (vs_total_ac_bits < vs_bit_size)
  808. dv_guess_qnos(&enc_blks[0], &qnos[0]);
  809. for (i=0; i<5; i++) {
  810. dif[i*80 + 3] = qnos[i];
  811. }
  812. /* First pass over individual cells only */
  813. for (j=0; j<5*6; j++)
  814. dv_encode_ac(&enc_blks[j], &pbs[j], 1);
  815. /* Second pass over each MB space */
  816. for (j=0; j<5*6; j++) {
  817. if (enc_blks[j].cur_ac < 65 || enc_blks[j].partial_bit_count)
  818. dv_encode_ac(&enc_blks[j], &pbs[(j/6)*6], 6);
  819. }
  820. /* Third and final pass over the whole vides segment space */
  821. for (j=0; j<5*6; j++) {
  822. if (enc_blks[j].cur_ac < 65 || enc_blks[j].partial_bit_count)
  823. dv_encode_ac(&enc_blks[j], &pbs[0], 6*5);
  824. }
  825. for (j=0; j<5*6; j++)
  826. flush_put_bits(&pbs[j]);
  827. }
  828. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  829. 144000 bytes for PAL) */
  830. static int dvvideo_decode_frame(AVCodecContext *avctx,
  831. void *data, int *data_size,
  832. uint8_t *buf, int buf_size)
  833. {
  834. DVVideoDecodeContext *s = avctx->priv_data;
  835. int ds, vs;
  836. const uint16_t *mb_pos_ptr;
  837. s->sys = dv_frame_profile(buf);
  838. if (!s->sys || buf_size < s->sys->frame_size)
  839. return -1; /* NOTE: we only accept several full frames */
  840. if(s->picture.data[0])
  841. avctx->release_buffer(avctx, &s->picture);
  842. s->picture.reference = 0;
  843. avctx->pix_fmt = s->sys->pix_fmt;
  844. avctx->width = s->sys->width;
  845. avctx->height = s->sys->height;
  846. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  847. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  848. return -1;
  849. }
  850. s->picture.interlaced_frame = 1;
  851. s->picture.top_field_first = 0;
  852. /* for each DIF segment */
  853. mb_pos_ptr = s->sys->video_place;
  854. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  855. buf += 6 * 80; /* skip DIF segment header */
  856. for(vs = 0; vs < 27; vs++) {
  857. if ((vs % 3) == 0)
  858. buf += 80; /* skip audio block */
  859. #ifdef VLC_DEBUG
  860. printf("********************* %d, %d **********************\n", ds, vs);
  861. #endif
  862. dv_decode_video_segment(s, buf, mb_pos_ptr);
  863. buf += 5 * 80;
  864. mb_pos_ptr += 5;
  865. }
  866. }
  867. emms_c();
  868. /* return image */
  869. *data_size = sizeof(AVFrame);
  870. *(AVFrame*)data= s->picture;
  871. return s->sys->frame_size;
  872. }
  873. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  874. void *data)
  875. {
  876. DVVideoDecodeContext *s = c->priv_data;
  877. const uint16_t *mb_pos_ptr;
  878. int ds, vs;
  879. s->sys = dv_codec_profile(c);
  880. if (!s->sys)
  881. return -1;
  882. c->pix_fmt = s->sys->pix_fmt;
  883. s->picture = *((AVFrame *)data);
  884. /* for each DIF segment */
  885. mb_pos_ptr = s->sys->video_place;
  886. for (ds = 0; ds < s->sys->difseg_size; ds++) {
  887. buf += 6 * 80; /* skip DIF segment header */
  888. for(vs = 0; vs < 27; vs++) {
  889. if ((vs % 3) == 0)
  890. buf += 80; /* skip audio block */
  891. #ifdef VLC_DEBUG
  892. printf("********************* %d, %d **********************\n", ds, vs);
  893. #endif
  894. dv_encode_video_segment(s, buf, mb_pos_ptr);
  895. buf += 5 * 80;
  896. mb_pos_ptr += 5;
  897. }
  898. }
  899. emms_c();
  900. return s->sys->frame_size;
  901. }
  902. static int dvvideo_end(AVCodecContext *avctx)
  903. {
  904. avcodec_default_free_buffers(avctx);
  905. return 0;
  906. }
  907. AVCodec dvvideo_decoder = {
  908. "dvvideo",
  909. CODEC_TYPE_VIDEO,
  910. CODEC_ID_DVVIDEO,
  911. sizeof(DVVideoDecodeContext),
  912. dvvideo_init,
  913. dvvideo_encode_frame,
  914. dvvideo_end,
  915. dvvideo_decode_frame,
  916. CODEC_CAP_DR1,
  917. NULL
  918. };