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.

972 lines
29KB

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