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.

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