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.

975 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);
  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 || dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
  152. continue;
  153. if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
  154. continue;
  155. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
  156. (!!dv_vlc_level[i]);
  157. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
  158. (!!dv_vlc_level[i]);
  159. }
  160. for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
  161. #ifdef DV_CODEC_TINY_TARGET
  162. for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
  163. if (dv_vlc_map[i][j].size == 0) {
  164. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  165. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  166. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  167. dv_vlc_map[0][j].size;
  168. }
  169. }
  170. #else
  171. for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
  172. if (dv_vlc_map[i][j].size == 0) {
  173. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  174. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  175. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  176. dv_vlc_map[0][j].size;
  177. }
  178. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
  179. dv_vlc_map[i][j].vlc | 1;
  180. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
  181. dv_vlc_map[i][j].size;
  182. }
  183. #endif
  184. }
  185. }
  186. /* Generic DSP setup */
  187. dsputil_init(&dsp, avctx);
  188. s->get_pixels = dsp.get_pixels;
  189. /* 88DCT setup */
  190. s->fdct[0] = dsp.fdct;
  191. s->idct_put[0] = dsp.idct_put;
  192. for (i=0; i<64; i++)
  193. s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
  194. /* 248DCT setup */
  195. s->fdct[1] = dsp.fdct248;
  196. s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
  197. memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
  198. /* XXX: do it only for constant case */
  199. dv_build_unquantize_tables(s, dsp.idct_permutation);
  200. /* FIXME: I really don't think this should be here */
  201. if (dv_codec_profile(avctx))
  202. avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
  203. avctx->coded_frame = &s->picture;
  204. return 0;
  205. }
  206. static int dvvideo_end(AVCodecContext *avctx)
  207. {
  208. avcodec_default_free_buffers(avctx);
  209. return 0;
  210. }
  211. // #define VLC_DEBUG
  212. // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
  213. typedef struct BlockInfo {
  214. const uint8_t *shift_table;
  215. const uint8_t *scan_table;
  216. uint8_t pos; /* position in block */
  217. uint8_t dct_mode;
  218. uint8_t partial_bit_count;
  219. uint16_t partial_bit_buffer;
  220. int shift_offset;
  221. } BlockInfo;
  222. /* block size in bits */
  223. static const uint16_t block_sizes[6] = {
  224. 112, 112, 112, 112, 80, 80
  225. };
  226. /* bit budget for AC only in 5 MBs */
  227. static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
  228. /* see dv_88_areas and dv_248_areas for details */
  229. static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
  230. #ifndef ALT_BITSTREAM_READER
  231. #warning only works with ALT_BITSTREAM_READER
  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_bits(&gb, 9);
  348. dc = (dc << (32 - 9)) >> (32 - 9);
  349. dct_mode = get_bits1(&gb);
  350. mb->dct_mode = dct_mode;
  351. mb->scan_table = s->dv_zigzag[dct_mode];
  352. class1 = get_bits(&gb, 2);
  353. mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
  354. [quant + dv_quant_offset[class1]];
  355. dc = dc << 2;
  356. /* convert to unsigned because 128 is not added in the
  357. standard IDCT */
  358. dc += 1024;
  359. block[0] = dc;
  360. buf_ptr += last_index >> 3;
  361. mb->pos = 0;
  362. mb->partial_bit_count = 0;
  363. #ifdef VLC_DEBUG
  364. printf("MB block: %d, %d ", mb_index, j);
  365. #endif
  366. dv_decode_ac(&gb, mb, block);
  367. /* write the remaining bits in a new buffer only if the
  368. block is finished */
  369. if (mb->pos >= 64)
  370. bit_copy(&pb, &gb);
  371. block += 64;
  372. mb++;
  373. }
  374. /* pass 2 : we can do it just after */
  375. #ifdef VLC_DEBUG
  376. printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
  377. #endif
  378. block = block1;
  379. mb = mb1;
  380. init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
  381. flush_put_bits(&pb);
  382. for(j = 0;j < 6; j++, block += 64, mb++) {
  383. if (mb->pos < 64 && get_bits_left(&gb) > 0) {
  384. dv_decode_ac(&gb, mb, block);
  385. /* if still not finished, no need to parse other blocks */
  386. if (mb->pos < 64)
  387. break;
  388. }
  389. }
  390. /* all blocks are finished, so the extra bytes can be used at
  391. the video segment level */
  392. if (j >= 6)
  393. bit_copy(&vs_pb, &gb);
  394. }
  395. /* we need a pass other the whole video segment */
  396. #ifdef VLC_DEBUG
  397. printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
  398. #endif
  399. block = &sblock[0][0];
  400. mb = mb_data;
  401. init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
  402. flush_put_bits(&vs_pb);
  403. for(mb_index = 0; mb_index < 5; mb_index++) {
  404. for(j = 0;j < 6; j++) {
  405. if (mb->pos < 64) {
  406. #ifdef VLC_DEBUG
  407. printf("start %d:%d\n", mb_index, j);
  408. #endif
  409. dv_decode_ac(&gb, mb, block);
  410. }
  411. if (mb->pos >= 64 && mb->pos < 127)
  412. av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
  413. block += 64;
  414. mb++;
  415. }
  416. }
  417. /* compute idct and place blocks */
  418. block = &sblock[0][0];
  419. mb = mb_data;
  420. for(mb_index = 0; mb_index < 5; mb_index++) {
  421. v = *mb_pos_ptr++;
  422. mb_x = v & 0xff;
  423. mb_y = v >> 8;
  424. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  425. if (s->sys->pix_fmt == PIX_FMT_YUV411P)
  426. c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
  427. else
  428. c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
  429. for(j = 0;j < 6; j++) {
  430. idct_put = s->idct_put[mb->dct_mode];
  431. if (j < 4) {
  432. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  433. /* NOTE: at end of line, the macroblock is handled as 420 */
  434. idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
  435. } else {
  436. idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
  437. s->picture.linesize[0], block);
  438. }
  439. } else {
  440. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  441. uint64_t aligned_pixels[64/8];
  442. uint8_t *pixels= (uint8_t*)aligned_pixels;
  443. uint8_t *c_ptr, *c_ptr1, *ptr;
  444. int y, linesize;
  445. /* NOTE: at end of line, the macroblock is handled as 420 */
  446. idct_put(pixels, 8, block);
  447. linesize = s->picture.linesize[6 - j];
  448. c_ptr = s->picture.data[6 - j] + c_offset;
  449. ptr = pixels;
  450. for(y = 0;y < 8; y++) {
  451. /* convert to 411P */
  452. c_ptr1 = c_ptr + 8*linesize;
  453. c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
  454. c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
  455. c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
  456. c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
  457. c_ptr += linesize;
  458. ptr += 8;
  459. }
  460. } else {
  461. /* don't ask me why they inverted Cb and Cr ! */
  462. idct_put(s->picture.data[6 - j] + c_offset,
  463. s->picture.linesize[6 - j], block);
  464. }
  465. }
  466. block += 64;
  467. mb++;
  468. }
  469. }
  470. }
  471. #ifdef DV_CODEC_TINY_TARGET
  472. /* Converts run and level (where level != 0) pair into vlc, returning bit size */
  473. static always_inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
  474. {
  475. int sign = l >> 8;
  476. int level = (l ^ sign) - sign;
  477. int size;
  478. sign = (sign & 1);
  479. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  480. *vlc = dv_vlc_map[run][level].vlc | sign;
  481. size = dv_vlc_map[run][level].size;
  482. }
  483. else {
  484. if (level < DV_VLC_MAP_LEV_SIZE) {
  485. *vlc = dv_vlc_map[0][level].vlc | sign;
  486. size = dv_vlc_map[0][level].size;
  487. } else {
  488. *vlc = 0xfe00 | (level << 1) | sign;
  489. size = 16;
  490. }
  491. if (run) {
  492. *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
  493. (0x1f80 | (run - 1))) << size;
  494. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  495. }
  496. }
  497. return size;
  498. }
  499. static always_inline int dv_rl2vlc_size(int run, int l)
  500. {
  501. int level = (l ^ (l >> 8)) - (l >> 8);
  502. int size;
  503. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  504. size = dv_vlc_map[run][level].size;
  505. }
  506. else {
  507. size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
  508. if (run) {
  509. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  510. }
  511. }
  512. return size;
  513. }
  514. #else
  515. static always_inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
  516. {
  517. *vlc = dv_vlc_map[run][((uint16_t)l)&0x1ff].vlc;
  518. return dv_vlc_map[run][((uint16_t)l)&0x1ff].size;
  519. }
  520. static always_inline int dv_rl2vlc_size(int run, int l)
  521. {
  522. return dv_vlc_map[run][((uint16_t)l)&0x1ff].size;
  523. }
  524. #endif
  525. typedef struct EncBlockInfo {
  526. int area_q[4];
  527. int bit_size[4];
  528. int prev_run[4];
  529. int cur_ac;
  530. int cno;
  531. int dct_mode;
  532. DCTELEM *mb;
  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 void dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
  537. int pb_size)
  538. {
  539. int run;
  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. vlc_loop:
  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_size == 1) {
  554. bi->partial_bit_count = size;
  555. bi->partial_bit_buffer = vlc;
  556. return;
  557. }
  558. --pb_size;
  559. }
  560. /* Store VLC */
  561. put_bits(pb, size, vlc);
  562. /* Construct the next VLC */
  563. run = 0;
  564. for (; bi->cur_ac < 64; bi->cur_ac++, run++) {
  565. if (bi->mb[bi->cur_ac]) {
  566. size = dv_rl2vlc(run, bi->mb[bi->cur_ac], &vlc);
  567. bi->cur_ac++;
  568. goto vlc_loop;
  569. }
  570. }
  571. if (bi->cur_ac == 64) {
  572. size = 4; vlc = 6; /* End Of Block stamp */
  573. bi->cur_ac++;
  574. goto vlc_loop;
  575. }
  576. }
  577. static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
  578. const uint8_t* zigzag_scan, int bias)
  579. {
  580. int i, area;
  581. int run;
  582. int classes[] = {12, 24, 36, 0xffff};
  583. run = 0;
  584. bi->mb[0] = blk[0];
  585. bi->cno = 0;
  586. for (area = 0; area < 4; area++) {
  587. bi->prev_run[area] = run;
  588. bi->bit_size[area] = 0;
  589. for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
  590. bi->mb[i] = (blk[zigzag_scan[i]] / 16);
  591. while ((bi->mb[i] ^ (bi->mb[i] >> 8)) > classes[bi->cno])
  592. bi->cno++;
  593. if (bi->mb[i]) {
  594. bi->bit_size[area] += dv_rl2vlc_size(run, bi->mb[i]);
  595. run = 0;
  596. } else
  597. ++run;
  598. }
  599. }
  600. bi->bit_size[3] += 4; /* EOB marker */
  601. bi->cno += bias;
  602. if (bi->cno >= 3) { /* FIXME: we have to recreate bit_size[], prev_run[] */
  603. bi->cno = 3;
  604. for (i=1; i<64; i++)
  605. bi->mb[i] /= 2;
  606. }
  607. }
  608. #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
  609. static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
  610. DCTELEM *s;
  611. int score88 = 0;
  612. int score248 = 0;
  613. int i;
  614. /* Compute 8-8 score (small values give a better chance for 8-8 DCT) */
  615. s = blk;
  616. for(i=0; i<7; i++) {
  617. score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
  618. SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
  619. s += 8;
  620. }
  621. /* Compute 2-4-8 score (small values give a better chance for 2-4-8 DCT) */
  622. s = blk;
  623. for(i=0; i<6; i++) {
  624. score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
  625. SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
  626. s += 8;
  627. }
  628. return (score88 - score248 > -10);
  629. }
  630. static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
  631. {
  632. int size[5];
  633. int i, j, k, a, run;
  634. EncBlockInfo* b;
  635. do {
  636. b = blks;
  637. for (i=0; i<5; i++) {
  638. if (!qnos[i])
  639. continue;
  640. qnos[i]--;
  641. size[i] = 0;
  642. for (j=0; j<6; j++, b++) {
  643. for (a=0; a<4; a++) {
  644. if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
  645. b->bit_size[a] = (a==3)?4:0;
  646. b->area_q[a]++;
  647. run = b->prev_run[a];
  648. for (k=mb_area_start[a]; k<mb_area_start[a+1]; k++) {
  649. b->mb[k] /= 2;
  650. if (b->mb[k]) {
  651. b->bit_size[a] += dv_rl2vlc_size(run, b->mb[k]);
  652. run = 0;
  653. } else
  654. ++run;
  655. }
  656. }
  657. size[i] += b->bit_size[a];
  658. }
  659. }
  660. }
  661. } while ((vs_total_ac_bits < size[0] + size[1] + size[2] + size[3] + size[4]) &&
  662. (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]));
  663. }
  664. /*
  665. * This is a very rough initial implementaion. The performance is
  666. * horrible and the weighting is missing. But it's missing from the
  667. * decoding step also -- so at least we're on the same page with decoder ;-)
  668. */
  669. static inline void dv_encode_video_segment(DVVideoContext *s,
  670. uint8_t *dif,
  671. const uint16_t *mb_pos_ptr)
  672. {
  673. int mb_index, i, j, v;
  674. int mb_x, mb_y, c_offset, linesize;
  675. uint8_t* y_ptr;
  676. uint8_t* data;
  677. uint8_t* ptr;
  678. int do_edge_wrap;
  679. DCTELEM block[64] __align8;
  680. DCTELEM sblock[5*6][64] __align8;
  681. EncBlockInfo enc_blks[5*6];
  682. PutBitContext pbs[5*6];
  683. PutBitContext* pb;
  684. EncBlockInfo* enc_blk;
  685. int vs_bit_size = 0;
  686. int qnos[5];
  687. enc_blk = &enc_blks[0];
  688. pb = &pbs[0];
  689. for(mb_index = 0; mb_index < 5; mb_index++) {
  690. v = *mb_pos_ptr++;
  691. mb_x = v & 0xff;
  692. mb_y = v >> 8;
  693. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  694. c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
  695. ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
  696. (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
  697. do_edge_wrap = 0;
  698. qnos[mb_index] = 15; /* No quantization */
  699. ptr = dif + mb_index*80 + 4;
  700. for(j = 0;j < 6; j++) {
  701. if (j < 4) { /* Four Y blocks */
  702. /* NOTE: at end of line, the macroblock is handled as 420 */
  703. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  704. data = y_ptr + (j * 8);
  705. } else {
  706. data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
  707. }
  708. linesize = s->picture.linesize[0];
  709. } else { /* Cr and Cb blocks */
  710. /* don't ask Fabrice why they inverted Cb and Cr ! */
  711. data = s->picture.data[6 - j] + c_offset;
  712. linesize = s->picture.linesize[6 - j];
  713. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
  714. do_edge_wrap = 1;
  715. }
  716. /* Everything is set up -- now just copy data -> DCT block */
  717. if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
  718. uint8_t* d;
  719. DCTELEM *b = block;
  720. for (i=0;i<8;i++) {
  721. d = data + 8 * linesize;
  722. b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
  723. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  724. data += linesize;
  725. b += 8;
  726. }
  727. } else { /* Simple copy: 8x8 -> 8x8 */
  728. s->get_pixels(block, data, linesize);
  729. }
  730. enc_blk->dct_mode = dv_guess_dct_mode(block);
  731. enc_blk->mb = &sblock[mb_index*6+j][0];
  732. enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
  733. enc_blk->partial_bit_count = 0;
  734. enc_blk->partial_bit_buffer = 0;
  735. enc_blk->cur_ac = 1;
  736. s->fdct[enc_blk->dct_mode](block);
  737. dv_set_class_number(block, enc_blk,
  738. enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
  739. j/4*(j%2));
  740. init_put_bits(pb, ptr, block_sizes[j]/8);
  741. put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024) >> 2));
  742. put_bits(pb, 1, enc_blk->dct_mode);
  743. put_bits(pb, 2, enc_blk->cno);
  744. vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
  745. enc_blk->bit_size[2] + enc_blk->bit_size[3];
  746. ++enc_blk;
  747. ++pb;
  748. ptr += block_sizes[j]/8;
  749. }
  750. }
  751. if (vs_total_ac_bits < vs_bit_size)
  752. dv_guess_qnos(&enc_blks[0], &qnos[0]);
  753. for (i=0; i<5; i++) {
  754. dif[i*80 + 3] = qnos[i];
  755. }
  756. /* First pass over individual cells only */
  757. for (j=0; j<5*6; j++)
  758. dv_encode_ac(&enc_blks[j], &pbs[j], 1);
  759. /* Second pass over each MB space */
  760. for (j=0; j<5*6; j++) {
  761. if (enc_blks[j].cur_ac < 65 || enc_blks[j].partial_bit_count)
  762. dv_encode_ac(&enc_blks[j], &pbs[(j/6)*6], 6);
  763. }
  764. /* Third and final pass over the whole vides segment space */
  765. for (j=0; j<5*6; j++) {
  766. if (enc_blks[j].cur_ac < 65 || enc_blks[j].partial_bit_count)
  767. dv_encode_ac(&enc_blks[j], &pbs[0], 6*5);
  768. }
  769. for (j=0; j<5*6; j++)
  770. flush_put_bits(&pbs[j]);
  771. }
  772. static int dv_decode_mt(AVCodecContext *avctx, void* sl)
  773. {
  774. DVVideoContext *s = avctx->priv_data;
  775. int slice = (size_t)sl;
  776. dv_decode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80],
  777. &s->sys->video_place[slice*5]);
  778. return 0;
  779. }
  780. static int dv_encode_mt(AVCodecContext *avctx, void* sl)
  781. {
  782. DVVideoContext *s = avctx->priv_data;
  783. int slice = (size_t)sl;
  784. dv_encode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80],
  785. &s->sys->video_place[slice*5]);
  786. return 0;
  787. }
  788. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  789. 144000 bytes for PAL) */
  790. static int dvvideo_decode_frame(AVCodecContext *avctx,
  791. void *data, int *data_size,
  792. uint8_t *buf, int buf_size)
  793. {
  794. DVVideoContext *s = avctx->priv_data;
  795. *data_size=0;
  796. /* special case for last picture */
  797. if(buf_size==0)
  798. return 0;
  799. s->sys = dv_frame_profile(buf);
  800. if (!s->sys || buf_size < s->sys->frame_size)
  801. return -1; /* NOTE: we only accept several full frames */
  802. if(s->picture.data[0])
  803. avctx->release_buffer(avctx, &s->picture);
  804. s->picture.reference = 0;
  805. avctx->pix_fmt = s->sys->pix_fmt;
  806. avctx->width = s->sys->width;
  807. avctx->height = s->sys->height;
  808. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  809. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  810. return -1;
  811. }
  812. s->picture.interlaced_frame = 1;
  813. s->picture.top_field_first = 0;
  814. s->buf = buf;
  815. avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
  816. s->sys->difseg_size * 27);
  817. emms_c();
  818. /* return image */
  819. *data_size = sizeof(AVFrame);
  820. *(AVFrame*)data= s->picture;
  821. return s->sys->frame_size;
  822. }
  823. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  824. void *data)
  825. {
  826. DVVideoContext *s = c->priv_data;
  827. s->sys = dv_codec_profile(c);
  828. if (!s->sys)
  829. return -1;
  830. c->pix_fmt = s->sys->pix_fmt;
  831. s->picture = *((AVFrame *)data);
  832. s->buf = buf;
  833. c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
  834. s->sys->difseg_size * 27);
  835. emms_c();
  836. return s->sys->frame_size;
  837. }
  838. AVCodec dvvideo_encoder = {
  839. "dvvideo",
  840. CODEC_TYPE_VIDEO,
  841. CODEC_ID_DVVIDEO,
  842. sizeof(DVVideoContext),
  843. dvvideo_init,
  844. dvvideo_encode_frame,
  845. dvvideo_end,
  846. NULL,
  847. CODEC_CAP_DR1,
  848. NULL
  849. };
  850. AVCodec dvvideo_decoder = {
  851. "dvvideo",
  852. CODEC_TYPE_VIDEO,
  853. CODEC_ID_DVVIDEO,
  854. sizeof(DVVideoContext),
  855. dvvideo_init,
  856. NULL,
  857. dvvideo_end,
  858. dvvideo_decode_frame,
  859. CODEC_CAP_DR1,
  860. NULL
  861. };