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.

1005 lines
31KB

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