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.

1011 lines
32KB

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