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.

1051 lines
34KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. /* MultiThreading - applies to entire DV codec, not just the avcontext */
  49. uint8_t** dv_anchor;
  50. #define TEX_VLC_BITS 9
  51. #ifdef DV_CODEC_TINY_TARGET
  52. #define DV_VLC_MAP_RUN_SIZE 15
  53. #define DV_VLC_MAP_LEV_SIZE 23
  54. #else
  55. #define DV_VLC_MAP_RUN_SIZE 64
  56. #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check
  57. #endif
  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_mallocz_static(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  131. if (!dv_rl_vlc)
  132. return -ENOMEM;
  133. for(i = 0; i < dv_vlc.table_size; i++){
  134. int code= dv_vlc.table[i][0];
  135. int len = dv_vlc.table[i][1];
  136. int level, run;
  137. if(len<0){ //more bits needed
  138. run= 0;
  139. level= code;
  140. } else {
  141. run= new_dv_vlc_run[code] + 1;
  142. level= new_dv_vlc_level[code];
  143. }
  144. dv_rl_vlc[i].len = len;
  145. dv_rl_vlc[i].level = level;
  146. dv_rl_vlc[i].run = run;
  147. }
  148. free_vlc(&dv_vlc);
  149. for (i = 0; i < NB_DV_VLC - 1; i++) {
  150. if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE)
  151. continue;
  152. #ifdef DV_CODEC_TINY_TARGET
  153. if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
  154. continue;
  155. #endif
  156. if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
  157. continue;
  158. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
  159. (!!dv_vlc_level[i]);
  160. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
  161. (!!dv_vlc_level[i]);
  162. }
  163. for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
  164. #ifdef DV_CODEC_TINY_TARGET
  165. for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
  166. if (dv_vlc_map[i][j].size == 0) {
  167. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  168. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  169. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  170. dv_vlc_map[0][j].size;
  171. }
  172. }
  173. #else
  174. for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
  175. if (dv_vlc_map[i][j].size == 0) {
  176. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  177. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  178. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  179. dv_vlc_map[0][j].size;
  180. }
  181. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
  182. dv_vlc_map[i][j].vlc | 1;
  183. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
  184. dv_vlc_map[i][j].size;
  185. }
  186. #endif
  187. }
  188. }
  189. /* Generic DSP setup */
  190. dsputil_init(&dsp, avctx);
  191. s->get_pixels = dsp.get_pixels;
  192. /* 88DCT setup */
  193. s->fdct[0] = dsp.fdct;
  194. s->idct_put[0] = dsp.idct_put;
  195. for (i=0; i<64; i++)
  196. s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
  197. /* 248DCT setup */
  198. s->fdct[1] = dsp.fdct248;
  199. s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
  200. if(avctx->lowres){
  201. for (i=0; i<64; i++){
  202. int j= ff_zigzag248_direct[i];
  203. s->dv_zigzag[1][i] = dsp.idct_permutation[(j&7) + (j&8)*4 + (j&48)/2];
  204. }
  205. }else
  206. memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
  207. /* XXX: do it only for constant case */
  208. dv_build_unquantize_tables(s, dsp.idct_permutation);
  209. /* FIXME: I really don't think this should be here */
  210. if (dv_codec_profile(avctx))
  211. avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
  212. avctx->coded_frame = &s->picture;
  213. s->avctx= avctx;
  214. return 0;
  215. }
  216. // #define VLC_DEBUG
  217. // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
  218. typedef struct BlockInfo {
  219. const uint8_t *shift_table;
  220. const uint8_t *scan_table;
  221. const int *iweight_table;
  222. uint8_t pos; /* position in block */
  223. uint8_t dct_mode;
  224. uint8_t partial_bit_count;
  225. uint16_t partial_bit_buffer;
  226. int shift_offset;
  227. } BlockInfo;
  228. /* block size in bits */
  229. static const uint16_t block_sizes[6] = {
  230. 112, 112, 112, 112, 80, 80
  231. };
  232. /* bit budget for AC only in 5 MBs */
  233. static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
  234. /* see dv_88_areas and dv_248_areas for details */
  235. static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
  236. #ifndef ALT_BITSTREAM_READER
  237. #warning only works with ALT_BITSTREAM_READER
  238. static int re_index; //Hack to make it compile
  239. #endif
  240. static inline int get_bits_left(GetBitContext *s)
  241. {
  242. return s->size_in_bits - get_bits_count(s);
  243. }
  244. static inline int get_bits_size(GetBitContext *s)
  245. {
  246. return s->size_in_bits;
  247. }
  248. static inline int put_bits_left(PutBitContext* s)
  249. {
  250. return (s->buf_end - s->buf) * 8 - put_bits_count(s);
  251. }
  252. /* decode ac coefs */
  253. static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
  254. {
  255. int last_index = get_bits_size(gb);
  256. const uint8_t *scan_table = mb->scan_table;
  257. const uint8_t *shift_table = mb->shift_table;
  258. const int *iweight_table = mb->iweight_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. pos1 = scan_table[pos];
  301. level <<= shift_table[pos1];
  302. /* unweigh, round, and shift down */
  303. level = (level*iweight_table[pos] + (1 << (dv_iweight_bits-1))) >> dv_iweight_bits;
  304. block[pos1] = level;
  305. UPDATE_CACHE(re, gb);
  306. }
  307. CLOSE_READER(re, gb);
  308. mb->pos = pos;
  309. }
  310. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
  311. {
  312. int bits_left = get_bits_left(gb);
  313. while (bits_left >= MIN_CACHE_BITS) {
  314. put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
  315. bits_left -= MIN_CACHE_BITS;
  316. }
  317. if (bits_left > 0) {
  318. put_bits(pb, bits_left, get_bits(gb, bits_left));
  319. }
  320. }
  321. /* mb_x and mb_y are in units of 8 pixels */
  322. static inline void dv_decode_video_segment(DVVideoContext *s,
  323. uint8_t *buf_ptr1,
  324. const uint16_t *mb_pos_ptr)
  325. {
  326. int quant, dc, dct_mode, class1, j;
  327. int mb_index, mb_x, mb_y, v, last_index;
  328. DCTELEM *block, *block1;
  329. int c_offset;
  330. uint8_t *y_ptr;
  331. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  332. uint8_t *buf_ptr;
  333. PutBitContext pb, vs_pb;
  334. GetBitContext gb;
  335. BlockInfo mb_data[5 * 6], *mb, *mb1;
  336. DECLARE_ALIGNED_8(DCTELEM, sblock[5*6][64]);
  337. DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */
  338. DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */
  339. const int log2_blocksize= 3-s->avctx->lowres;
  340. assert((((int)mb_bit_buffer)&7)==0);
  341. assert((((int)vs_bit_buffer)&7)==0);
  342. memset(sblock, 0, sizeof(sblock));
  343. /* pass 1 : read DC and AC coefficients in blocks */
  344. buf_ptr = buf_ptr1;
  345. block1 = &sblock[0][0];
  346. mb1 = mb_data;
  347. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
  348. for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) {
  349. /* skip header */
  350. quant = buf_ptr[3] & 0x0f;
  351. buf_ptr += 4;
  352. init_put_bits(&pb, mb_bit_buffer, 80);
  353. mb = mb1;
  354. block = block1;
  355. for(j = 0;j < 6; j++) {
  356. last_index = block_sizes[j];
  357. init_get_bits(&gb, buf_ptr, last_index);
  358. /* get the dc */
  359. dc = get_sbits(&gb, 9);
  360. dct_mode = get_bits1(&gb);
  361. mb->dct_mode = dct_mode;
  362. mb->scan_table = s->dv_zigzag[dct_mode];
  363. mb->iweight_table = dct_mode ? dv_iweight_248 : dv_iweight_88;
  364. class1 = get_bits(&gb, 2);
  365. mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
  366. [quant + dv_quant_offset[class1]];
  367. dc = dc << 2;
  368. /* convert to unsigned because 128 is not added in the
  369. standard IDCT */
  370. dc += 1024;
  371. block[0] = dc;
  372. buf_ptr += last_index >> 3;
  373. mb->pos = 0;
  374. mb->partial_bit_count = 0;
  375. #ifdef VLC_DEBUG
  376. printf("MB block: %d, %d ", mb_index, j);
  377. #endif
  378. dv_decode_ac(&gb, mb, block);
  379. /* write the remaining bits in a new buffer only if the
  380. block is finished */
  381. if (mb->pos >= 64)
  382. bit_copy(&pb, &gb);
  383. block += 64;
  384. mb++;
  385. }
  386. /* pass 2 : we can do it just after */
  387. #ifdef VLC_DEBUG
  388. printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
  389. #endif
  390. block = block1;
  391. mb = mb1;
  392. init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
  393. flush_put_bits(&pb);
  394. for(j = 0;j < 6; j++, block += 64, mb++) {
  395. if (mb->pos < 64 && get_bits_left(&gb) > 0) {
  396. dv_decode_ac(&gb, mb, block);
  397. /* if still not finished, no need to parse other blocks */
  398. if (mb->pos < 64)
  399. break;
  400. }
  401. }
  402. /* all blocks are finished, so the extra bytes can be used at
  403. the video segment level */
  404. if (j >= 6)
  405. bit_copy(&vs_pb, &gb);
  406. }
  407. /* we need a pass other the whole video segment */
  408. #ifdef VLC_DEBUG
  409. printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
  410. #endif
  411. block = &sblock[0][0];
  412. mb = mb_data;
  413. init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
  414. flush_put_bits(&vs_pb);
  415. for(mb_index = 0; mb_index < 5; mb_index++) {
  416. for(j = 0;j < 6; j++) {
  417. if (mb->pos < 64) {
  418. #ifdef VLC_DEBUG
  419. printf("start %d:%d\n", mb_index, j);
  420. #endif
  421. dv_decode_ac(&gb, mb, block);
  422. }
  423. if (mb->pos >= 64 && mb->pos < 127)
  424. av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
  425. block += 64;
  426. mb++;
  427. }
  428. }
  429. /* compute idct and place blocks */
  430. block = &sblock[0][0];
  431. mb = mb_data;
  432. for(mb_index = 0; mb_index < 5; mb_index++) {
  433. v = *mb_pos_ptr++;
  434. mb_x = v & 0xff;
  435. mb_y = v >> 8;
  436. y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
  437. if (s->sys->pix_fmt == PIX_FMT_YUV411P)
  438. c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
  439. else
  440. c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize);
  441. for(j = 0;j < 6; j++) {
  442. idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3];
  443. if (j < 4) {
  444. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  445. /* NOTE: at end of line, the macroblock is handled as 420 */
  446. idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block);
  447. } else {
  448. idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
  449. s->picture.linesize[0], block);
  450. }
  451. } else {
  452. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  453. uint64_t aligned_pixels[64/8];
  454. uint8_t *pixels= (uint8_t*)aligned_pixels;
  455. uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1;
  456. int x, y, linesize;
  457. /* NOTE: at end of line, the macroblock is handled as 420 */
  458. idct_put(pixels, 8, block);
  459. linesize = s->picture.linesize[6 - j];
  460. c_ptr = s->picture.data[6 - j] + c_offset;
  461. ptr = pixels;
  462. for(y = 0;y < (1<<log2_blocksize); y++) {
  463. ptr1= ptr + (1<<(log2_blocksize-1));
  464. c_ptr1 = c_ptr + (linesize<<log2_blocksize);
  465. for(x=0; x < (1<<(log2_blocksize-1)); x++){
  466. c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
  467. }
  468. c_ptr += linesize;
  469. ptr += 8;
  470. }
  471. } else {
  472. /* don't ask me why they inverted Cb and Cr ! */
  473. idct_put(s->picture.data[6 - j] + c_offset,
  474. s->picture.linesize[6 - j], block);
  475. }
  476. }
  477. block += 64;
  478. mb++;
  479. }
  480. }
  481. }
  482. #ifdef DV_CODEC_TINY_TARGET
  483. /* Converts run and level (where level != 0) pair into vlc, returning bit size */
  484. static always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
  485. {
  486. int size;
  487. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  488. *vlc = dv_vlc_map[run][level].vlc | sign;
  489. size = dv_vlc_map[run][level].size;
  490. }
  491. else {
  492. if (level < DV_VLC_MAP_LEV_SIZE) {
  493. *vlc = dv_vlc_map[0][level].vlc | sign;
  494. size = dv_vlc_map[0][level].size;
  495. } else {
  496. *vlc = 0xfe00 | (level << 1) | sign;
  497. size = 16;
  498. }
  499. if (run) {
  500. *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
  501. (0x1f80 | (run - 1))) << size;
  502. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  503. }
  504. }
  505. return size;
  506. }
  507. static always_inline int dv_rl2vlc_size(int run, int level)
  508. {
  509. int size;
  510. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  511. size = dv_vlc_map[run][level].size;
  512. }
  513. else {
  514. size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
  515. if (run) {
  516. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  517. }
  518. }
  519. return size;
  520. }
  521. #else
  522. static always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
  523. {
  524. *vlc = dv_vlc_map[run][l].vlc | sign;
  525. return dv_vlc_map[run][l].size;
  526. }
  527. static always_inline int dv_rl2vlc_size(int run, int l)
  528. {
  529. return dv_vlc_map[run][l].size;
  530. }
  531. #endif
  532. typedef struct EncBlockInfo {
  533. int area_q[4];
  534. int bit_size[4];
  535. int prev[5];
  536. int cur_ac;
  537. int cno;
  538. int dct_mode;
  539. DCTELEM mb[64];
  540. uint8_t next[64];
  541. uint8_t sign[64];
  542. uint8_t partial_bit_count;
  543. uint32_t partial_bit_buffer; /* we can't use uint16_t here */
  544. } EncBlockInfo;
  545. static always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
  546. PutBitContext* pb_end)
  547. {
  548. int prev;
  549. int bits_left;
  550. PutBitContext* pb = pb_pool;
  551. int size = bi->partial_bit_count;
  552. uint32_t vlc = bi->partial_bit_buffer;
  553. bi->partial_bit_count = bi->partial_bit_buffer = 0;
  554. for(;;){
  555. /* Find suitable storage space */
  556. for (; size > (bits_left = put_bits_left(pb)); pb++) {
  557. if (bits_left) {
  558. size -= bits_left;
  559. put_bits(pb, bits_left, vlc >> size);
  560. vlc = vlc & ((1<<size)-1);
  561. }
  562. if (pb + 1 >= pb_end) {
  563. bi->partial_bit_count = size;
  564. bi->partial_bit_buffer = vlc;
  565. return pb;
  566. }
  567. }
  568. /* Store VLC */
  569. put_bits(pb, size, vlc);
  570. if(bi->cur_ac>=64)
  571. break;
  572. /* Construct the next VLC */
  573. prev= bi->cur_ac;
  574. bi->cur_ac = bi->next[prev];
  575. if(bi->cur_ac < 64){
  576. size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
  577. } else {
  578. size = 4; vlc = 6; /* End Of Block stamp */
  579. }
  580. }
  581. return pb;
  582. }
  583. static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
  584. const uint8_t* zigzag_scan, const int *weight, int bias)
  585. {
  586. int i, area;
  587. static const int classes[] = {12, 24, 36, 0xffff};
  588. int max=12;
  589. int prev=0;
  590. bi->mb[0] = blk[0];
  591. for (area = 0; area < 4; area++) {
  592. bi->prev[area] = prev;
  593. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  594. for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
  595. int level = blk[zigzag_scan[i]];
  596. if (level+15 > 30U) {
  597. bi->sign[i] = (level>>31)&1;
  598. /* weigh it and and shift down into range, adding for rounding */
  599. /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
  600. AND the 2x doubling of the weights */
  601. level = (ABS(level) * weight[i] + (1<<(dv_weight_bits+3))) >> (dv_weight_bits+4);
  602. bi->mb[i] = level;
  603. if(level>max) max= level;
  604. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
  605. bi->next[prev]= i;
  606. prev= i;
  607. }
  608. }
  609. }
  610. bi->next[prev]= i;
  611. for(bi->cno = 0; max > classes[bi->cno]; bi->cno++);
  612. bi->cno += bias;
  613. if (bi->cno >= 3) {
  614. bi->cno = 3;
  615. prev=0;
  616. i= bi->next[prev];
  617. for (area = 0; area < 4; area++) {
  618. bi->prev[area] = prev;
  619. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  620. for (; i<mb_area_start[area+1]; i= bi->next[i]) {
  621. bi->mb[i] >>=1;
  622. if (bi->mb[i]) {
  623. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
  624. bi->next[prev]= i;
  625. prev= i;
  626. }
  627. }
  628. }
  629. bi->next[prev]= i;
  630. }
  631. }
  632. //FIXME replace this by dsputil
  633. #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
  634. static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
  635. DCTELEM *s;
  636. int score88 = 0;
  637. int score248 = 0;
  638. int i;
  639. /* Compute 8-8 score (small values give a better chance for 8-8 DCT) */
  640. s = blk;
  641. for(i=0; i<7; i++) {
  642. score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
  643. SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
  644. s += 8;
  645. }
  646. /* Compute 2-4-8 score (small values give a better chance for 2-4-8 DCT) */
  647. s = blk;
  648. for(i=0; i<6; i++) {
  649. score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
  650. SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
  651. s += 8;
  652. }
  653. return (score88 - score248 > -10);
  654. }
  655. static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
  656. {
  657. int size[5];
  658. int i, j, k, a, prev, a2;
  659. EncBlockInfo* b;
  660. size[0] = size[1] = size[2] = size[3] = size[4] = 1<<24;
  661. do {
  662. b = blks;
  663. for (i=0; i<5; i++) {
  664. if (!qnos[i])
  665. continue;
  666. qnos[i]--;
  667. size[i] = 0;
  668. for (j=0; j<6; j++, b++) {
  669. for (a=0; a<4; a++) {
  670. if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
  671. b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
  672. b->area_q[a]++;
  673. prev= b->prev[a];
  674. assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
  675. for (k= b->next[prev] ; k<mb_area_start[a+1]; k= b->next[k]) {
  676. b->mb[k] >>= 1;
  677. if (b->mb[k]) {
  678. b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  679. prev= k;
  680. } else {
  681. if(b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
  682. for(a2=a+1; b->next[k] >= mb_area_start[a2+1]; a2++)
  683. b->prev[a2] = prev;
  684. assert(a2<4);
  685. assert(b->mb[b->next[k]]);
  686. b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
  687. -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
  688. assert(b->prev[a2]==k && (a2+1 >= 4 || b->prev[a2+1]!=k));
  689. b->prev[a2] = prev;
  690. }
  691. b->next[prev] = b->next[k];
  692. }
  693. }
  694. b->prev[a+1]= prev;
  695. }
  696. size[i] += b->bit_size[a];
  697. }
  698. }
  699. if(vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
  700. return;
  701. }
  702. } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
  703. for(a=2; a==2 || vs_total_ac_bits < size[0]; a+=a){
  704. b = blks;
  705. size[0] = 5*6*4; //EOB
  706. for (j=0; j<6*5; j++, b++) {
  707. prev= b->prev[0];
  708. for (k= b->next[prev]; k<64; k= b->next[k]) {
  709. if(b->mb[k] < a && b->mb[k] > -a){
  710. b->next[prev] = b->next[k];
  711. }else{
  712. size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  713. prev= k;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. static inline void dv_encode_video_segment(DVVideoContext *s,
  720. uint8_t *dif,
  721. const uint16_t *mb_pos_ptr)
  722. {
  723. int mb_index, i, j, v;
  724. int mb_x, mb_y, c_offset, linesize;
  725. uint8_t* y_ptr;
  726. uint8_t* data;
  727. uint8_t* ptr;
  728. int do_edge_wrap;
  729. DECLARE_ALIGNED_8(DCTELEM, block[64]);
  730. EncBlockInfo enc_blks[5*6];
  731. PutBitContext pbs[5*6];
  732. PutBitContext* pb;
  733. EncBlockInfo* enc_blk;
  734. int vs_bit_size = 0;
  735. int qnos[5];
  736. assert((((int)block) & 7) == 0);
  737. enc_blk = &enc_blks[0];
  738. pb = &pbs[0];
  739. for(mb_index = 0; mb_index < 5; mb_index++) {
  740. v = *mb_pos_ptr++;
  741. mb_x = v & 0xff;
  742. mb_y = v >> 8;
  743. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  744. c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
  745. ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
  746. (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
  747. do_edge_wrap = 0;
  748. qnos[mb_index] = 15; /* No quantization */
  749. ptr = dif + mb_index*80 + 4;
  750. for(j = 0;j < 6; j++) {
  751. if (j < 4) { /* Four Y blocks */
  752. /* NOTE: at end of line, the macroblock is handled as 420 */
  753. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  754. data = y_ptr + (j * 8);
  755. } else {
  756. data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
  757. }
  758. linesize = s->picture.linesize[0];
  759. } else { /* Cr and Cb blocks */
  760. /* don't ask Fabrice why they inverted Cb and Cr ! */
  761. data = s->picture.data[6 - j] + c_offset;
  762. linesize = s->picture.linesize[6 - j];
  763. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
  764. do_edge_wrap = 1;
  765. }
  766. /* Everything is set up -- now just copy data -> DCT block */
  767. if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
  768. uint8_t* d;
  769. DCTELEM *b = block;
  770. for (i=0;i<8;i++) {
  771. d = data + 8 * linesize;
  772. b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
  773. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  774. data += linesize;
  775. b += 8;
  776. }
  777. } else { /* Simple copy: 8x8 -> 8x8 */
  778. s->get_pixels(block, data, linesize);
  779. }
  780. if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
  781. enc_blk->dct_mode = dv_guess_dct_mode(block);
  782. else
  783. enc_blk->dct_mode = 0;
  784. enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
  785. enc_blk->partial_bit_count = 0;
  786. enc_blk->partial_bit_buffer = 0;
  787. enc_blk->cur_ac = 0;
  788. s->fdct[enc_blk->dct_mode](block);
  789. dv_set_class_number(block, enc_blk,
  790. enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
  791. enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
  792. j/4);
  793. init_put_bits(pb, ptr, block_sizes[j]/8);
  794. put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
  795. put_bits(pb, 1, enc_blk->dct_mode);
  796. put_bits(pb, 2, enc_blk->cno);
  797. vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
  798. enc_blk->bit_size[2] + enc_blk->bit_size[3];
  799. ++enc_blk;
  800. ++pb;
  801. ptr += block_sizes[j]/8;
  802. }
  803. }
  804. if (vs_total_ac_bits < vs_bit_size)
  805. dv_guess_qnos(&enc_blks[0], &qnos[0]);
  806. for (i=0; i<5; i++) {
  807. dif[i*80 + 3] = qnos[i];
  808. }
  809. /* First pass over individual cells only */
  810. for (j=0; j<5*6; j++)
  811. dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
  812. /* Second pass over each MB space */
  813. for (j=0; j<5*6; j+=6) {
  814. pb= &pbs[j];
  815. for (i=0; i<6; i++) {
  816. if (enc_blks[i+j].partial_bit_count)
  817. pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);
  818. }
  819. }
  820. /* Third and final pass over the whole vides segment space */
  821. pb= &pbs[0];
  822. for (j=0; j<5*6; j++) {
  823. if (enc_blks[j].partial_bit_count)
  824. pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);
  825. if (enc_blks[j].partial_bit_count)
  826. av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
  827. }
  828. for (j=0; j<5*6; j++)
  829. flush_put_bits(&pbs[j]);
  830. }
  831. static int dv_decode_mt(AVCodecContext *avctx, void* sl)
  832. {
  833. DVVideoContext *s = avctx->priv_data;
  834. int slice = (size_t)sl;
  835. dv_decode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80],
  836. &s->sys->video_place[slice*5]);
  837. return 0;
  838. }
  839. static int dv_encode_mt(AVCodecContext *avctx, void* sl)
  840. {
  841. DVVideoContext *s = avctx->priv_data;
  842. int slice = (size_t)sl;
  843. dv_encode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80],
  844. &s->sys->video_place[slice*5]);
  845. return 0;
  846. }
  847. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  848. 144000 bytes for PAL) */
  849. static int dvvideo_decode_frame(AVCodecContext *avctx,
  850. void *data, int *data_size,
  851. uint8_t *buf, int buf_size)
  852. {
  853. DVVideoContext *s = avctx->priv_data;
  854. s->sys = dv_frame_profile(buf);
  855. if (!s->sys || buf_size < s->sys->frame_size)
  856. return -1; /* NOTE: we only accept several full frames */
  857. if(s->picture.data[0])
  858. avctx->release_buffer(avctx, &s->picture);
  859. s->picture.reference = 0;
  860. s->picture.key_frame = 1;
  861. s->picture.pict_type = FF_I_TYPE;
  862. avctx->pix_fmt = s->sys->pix_fmt;
  863. avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
  864. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  865. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  866. return -1;
  867. }
  868. s->picture.interlaced_frame = 1;
  869. s->picture.top_field_first = 0;
  870. s->buf = buf;
  871. avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
  872. s->sys->difseg_size * 27);
  873. emms_c();
  874. /* return image */
  875. *data_size = sizeof(AVFrame);
  876. *(AVFrame*)data= s->picture;
  877. return s->sys->frame_size;
  878. }
  879. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  880. void *data)
  881. {
  882. DVVideoContext *s = c->priv_data;
  883. s->sys = dv_codec_profile(c);
  884. if (!s->sys)
  885. return -1;
  886. if(buf_size < s->sys->frame_size)
  887. return -1;
  888. c->pix_fmt = s->sys->pix_fmt;
  889. s->picture = *((AVFrame *)data);
  890. s->picture.key_frame = 1;
  891. s->picture.pict_type = FF_I_TYPE;
  892. s->buf = buf;
  893. c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
  894. s->sys->difseg_size * 27);
  895. emms_c();
  896. return s->sys->frame_size;
  897. }
  898. static int dvvideo_close(AVCodecContext *c)
  899. {
  900. return 0;
  901. }
  902. #ifdef CONFIG_DVVIDEO_ENCODER
  903. AVCodec dvvideo_encoder = {
  904. "dvvideo",
  905. CODEC_TYPE_VIDEO,
  906. CODEC_ID_DVVIDEO,
  907. sizeof(DVVideoContext),
  908. dvvideo_init,
  909. dvvideo_encode_frame,
  910. dvvideo_close,
  911. NULL,
  912. CODEC_CAP_DR1,
  913. NULL
  914. };
  915. #endif // CONFIG_DVVIDEO_ENCODER
  916. AVCodec dvvideo_decoder = {
  917. "dvvideo",
  918. CODEC_TYPE_VIDEO,
  919. CODEC_ID_DVVIDEO,
  920. sizeof(DVVideoContext),
  921. dvvideo_init,
  922. NULL,
  923. dvvideo_close,
  924. dvvideo_decode_frame,
  925. CODEC_CAP_DR1,
  926. NULL
  927. };