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.

988 lines
29KB

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