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.

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