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.

1000 lines
30KB

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