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.

1003 lines
30KB

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