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