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.

1148 lines
38KB

  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. * 50 Mbps (DVCPRO50) support
  10. * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
  11. *
  12. * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
  13. * of DV technical info.
  14. *
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2 of the License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /**
  30. * @file dv.c
  31. * DV codec.
  32. */
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "mpegvideo.h"
  36. #include "simple_idct.h"
  37. #include "dvdata.h"
  38. //#undef NDEBUG
  39. //#include <assert.h>
  40. typedef struct DVVideoContext {
  41. const DVprofile* sys;
  42. AVFrame picture;
  43. AVCodecContext *avctx;
  44. uint8_t *buf;
  45. uint8_t dv_zigzag[2][64];
  46. uint8_t dv_idct_shift[2][2][22][64];
  47. void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
  48. void (*fdct[2])(DCTELEM *block);
  49. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  50. } DVVideoContext;
  51. /* MultiThreading - dv_anchor applies to entire DV codec, not just the avcontext */
  52. /* one element is needed for each video segment in a DV frame */
  53. /* at most there are 2 DIF channels * 12 DIF sequences * 27 video segments (PAL 50Mbps) */
  54. #define DV_ANCHOR_SIZE (2*12*27)
  55. static void* dv_anchor[DV_ANCHOR_SIZE];
  56. #define TEX_VLC_BITS 9
  57. #ifdef DV_CODEC_TINY_TARGET
  58. #define DV_VLC_MAP_RUN_SIZE 15
  59. #define DV_VLC_MAP_LEV_SIZE 23
  60. #else
  61. #define DV_VLC_MAP_RUN_SIZE 64
  62. #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check
  63. #endif
  64. /* XXX: also include quantization */
  65. static RL_VLC_ELEM *dv_rl_vlc;
  66. /* VLC encoding lookup table */
  67. static struct dv_vlc_pair {
  68. uint32_t vlc;
  69. uint8_t size;
  70. } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL;
  71. static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
  72. {
  73. int i, q, j;
  74. /* NOTE: max left shift is 6 */
  75. for(q = 0; q < 22; q++) {
  76. /* 88DCT */
  77. for(i = 1; i < 64; i++) {
  78. /* 88 table */
  79. j = perm[i];
  80. s->dv_idct_shift[0][0][q][j] =
  81. dv_quant_shifts[q][dv_88_areas[i]] + 1;
  82. s->dv_idct_shift[1][0][q][j] = s->dv_idct_shift[0][0][q][j] + 1;
  83. }
  84. /* 248DCT */
  85. for(i = 1; i < 64; i++) {
  86. /* 248 table */
  87. s->dv_idct_shift[0][1][q][i] =
  88. dv_quant_shifts[q][dv_248_areas[i]] + 1;
  89. s->dv_idct_shift[1][1][q][i] = s->dv_idct_shift[0][1][q][i] + 1;
  90. }
  91. }
  92. }
  93. static int dvvideo_init(AVCodecContext *avctx)
  94. {
  95. DVVideoContext *s = avctx->priv_data;
  96. DSPContext dsp;
  97. static int done=0;
  98. int i, j;
  99. if (!done) {
  100. VLC dv_vlc;
  101. uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
  102. uint8_t new_dv_vlc_len[NB_DV_VLC*2];
  103. uint8_t new_dv_vlc_run[NB_DV_VLC*2];
  104. int16_t new_dv_vlc_level[NB_DV_VLC*2];
  105. done = 1;
  106. dv_vlc_map = av_mallocz_static(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
  107. if (!dv_vlc_map)
  108. return -ENOMEM;
  109. /* dv_anchor lets each thread know its Id */
  110. for (i=0; i<DV_ANCHOR_SIZE; i++)
  111. dv_anchor[i] = (void*)(size_t)i;
  112. /* it's faster to include sign bit in a generic VLC parsing scheme */
  113. for (i=0, j=0; i<NB_DV_VLC; i++, j++) {
  114. new_dv_vlc_bits[j] = dv_vlc_bits[i];
  115. new_dv_vlc_len[j] = dv_vlc_len[i];
  116. new_dv_vlc_run[j] = dv_vlc_run[i];
  117. new_dv_vlc_level[j] = dv_vlc_level[i];
  118. if (dv_vlc_level[i]) {
  119. new_dv_vlc_bits[j] <<= 1;
  120. new_dv_vlc_len[j]++;
  121. j++;
  122. new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
  123. new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
  124. new_dv_vlc_run[j] = dv_vlc_run[i];
  125. new_dv_vlc_level[j] = -dv_vlc_level[i];
  126. }
  127. }
  128. /* NOTE: as a trick, we use the fact the no codes are unused
  129. to accelerate the parsing of partial codes */
  130. init_vlc(&dv_vlc, TEX_VLC_BITS, j,
  131. new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
  132. dv_rl_vlc = av_mallocz_static(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  133. if (!dv_rl_vlc)
  134. return -ENOMEM;
  135. for(i = 0; i < dv_vlc.table_size; i++){
  136. int code= dv_vlc.table[i][0];
  137. int len = dv_vlc.table[i][1];
  138. int level, run;
  139. if(len<0){ //more bits needed
  140. run= 0;
  141. level= code;
  142. } else {
  143. run= new_dv_vlc_run[code] + 1;
  144. level= new_dv_vlc_level[code];
  145. }
  146. dv_rl_vlc[i].len = len;
  147. dv_rl_vlc[i].level = level;
  148. dv_rl_vlc[i].run = run;
  149. }
  150. free_vlc(&dv_vlc);
  151. for (i = 0; i < NB_DV_VLC - 1; i++) {
  152. if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE)
  153. continue;
  154. #ifdef DV_CODEC_TINY_TARGET
  155. if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
  156. continue;
  157. #endif
  158. if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
  159. continue;
  160. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
  161. (!!dv_vlc_level[i]);
  162. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
  163. (!!dv_vlc_level[i]);
  164. }
  165. for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
  166. #ifdef DV_CODEC_TINY_TARGET
  167. for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
  168. if (dv_vlc_map[i][j].size == 0) {
  169. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  170. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  171. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  172. dv_vlc_map[0][j].size;
  173. }
  174. }
  175. #else
  176. for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
  177. if (dv_vlc_map[i][j].size == 0) {
  178. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  179. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  180. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  181. dv_vlc_map[0][j].size;
  182. }
  183. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
  184. dv_vlc_map[i][j].vlc | 1;
  185. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
  186. dv_vlc_map[i][j].size;
  187. }
  188. #endif
  189. }
  190. }
  191. /* Generic DSP setup */
  192. dsputil_init(&dsp, avctx);
  193. s->get_pixels = dsp.get_pixels;
  194. /* 88DCT setup */
  195. s->fdct[0] = dsp.fdct;
  196. s->idct_put[0] = dsp.idct_put;
  197. for (i=0; i<64; i++)
  198. s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
  199. /* 248DCT setup */
  200. s->fdct[1] = dsp.fdct248;
  201. s->idct_put[1] = simple_idct248_put; // FIXME: need to add it to DSP
  202. if(avctx->lowres){
  203. for (i=0; i<64; i++){
  204. int j= ff_zigzag248_direct[i];
  205. s->dv_zigzag[1][i] = dsp.idct_permutation[(j&7) + (j&8)*4 + (j&48)/2];
  206. }
  207. }else
  208. memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
  209. /* XXX: do it only for constant case */
  210. dv_build_unquantize_tables(s, dsp.idct_permutation);
  211. avctx->coded_frame = &s->picture;
  212. s->avctx= avctx;
  213. return 0;
  214. }
  215. // #define VLC_DEBUG
  216. // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
  217. typedef struct BlockInfo {
  218. const uint8_t *shift_table;
  219. const uint8_t *scan_table;
  220. const int *iweight_table;
  221. uint8_t pos; /* position in block */
  222. uint8_t dct_mode;
  223. uint8_t partial_bit_count;
  224. uint16_t partial_bit_buffer;
  225. int shift_offset;
  226. } BlockInfo;
  227. /* block size in bits */
  228. static const uint16_t block_sizes[6] = {
  229. 112, 112, 112, 112, 80, 80
  230. };
  231. /* bit budget for AC only in 5 MBs */
  232. static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
  233. /* see dv_88_areas and dv_248_areas for details */
  234. static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
  235. #ifndef ALT_BITSTREAM_READER
  236. #warning only works with ALT_BITSTREAM_READER
  237. static int re_index; //Hack to make it compile
  238. #endif
  239. static inline int get_bits_left(GetBitContext *s)
  240. {
  241. return s->size_in_bits - get_bits_count(s);
  242. }
  243. static inline int get_bits_size(GetBitContext *s)
  244. {
  245. return s->size_in_bits;
  246. }
  247. static inline int put_bits_left(PutBitContext* s)
  248. {
  249. return (s->buf_end - s->buf) * 8 - put_bits_count(s);
  250. }
  251. /* decode ac coefs */
  252. static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
  253. {
  254. int last_index = get_bits_size(gb);
  255. const uint8_t *scan_table = mb->scan_table;
  256. const uint8_t *shift_table = mb->shift_table;
  257. const int *iweight_table = mb->iweight_table;
  258. int pos = mb->pos;
  259. int partial_bit_count = mb->partial_bit_count;
  260. int level, pos1, run, vlc_len, index;
  261. OPEN_READER(re, gb);
  262. UPDATE_CACHE(re, gb);
  263. /* if we must parse a partial vlc, we do it here */
  264. if (partial_bit_count > 0) {
  265. re_cache = ((unsigned)re_cache >> partial_bit_count) |
  266. (mb->partial_bit_buffer << (sizeof(re_cache)*8 - partial_bit_count));
  267. re_index -= partial_bit_count;
  268. mb->partial_bit_count = 0;
  269. }
  270. /* get the AC coefficients until last_index is reached */
  271. for(;;) {
  272. #ifdef VLC_DEBUG
  273. printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index);
  274. #endif
  275. /* our own optimized GET_RL_VLC */
  276. index = NEG_USR32(re_cache, TEX_VLC_BITS);
  277. vlc_len = dv_rl_vlc[index].len;
  278. if (vlc_len < 0) {
  279. index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
  280. vlc_len = TEX_VLC_BITS - vlc_len;
  281. }
  282. level = dv_rl_vlc[index].level;
  283. run = dv_rl_vlc[index].run;
  284. /* gotta check if we're still within gb boundaries */
  285. if (re_index + vlc_len > last_index) {
  286. /* should be < 16 bits otherwise a codeword could have been parsed */
  287. mb->partial_bit_count = last_index - re_index;
  288. mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
  289. re_index = last_index;
  290. break;
  291. }
  292. re_index += vlc_len;
  293. #ifdef VLC_DEBUG
  294. printf("run=%d level=%d\n", run, level);
  295. #endif
  296. pos += run;
  297. if (pos >= 64)
  298. break;
  299. pos1 = scan_table[pos];
  300. level <<= shift_table[pos1];
  301. /* unweigh, round, and shift down */
  302. level = (level*iweight_table[pos] + (1 << (dv_iweight_bits-1))) >> dv_iweight_bits;
  303. block[pos1] = level;
  304. UPDATE_CACHE(re, gb);
  305. }
  306. CLOSE_READER(re, gb);
  307. mb->pos = pos;
  308. }
  309. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
  310. {
  311. int bits_left = get_bits_left(gb);
  312. while (bits_left >= MIN_CACHE_BITS) {
  313. put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
  314. bits_left -= MIN_CACHE_BITS;
  315. }
  316. if (bits_left > 0) {
  317. put_bits(pb, bits_left, get_bits(gb, bits_left));
  318. }
  319. }
  320. /* mb_x and mb_y are in units of 8 pixels */
  321. static inline void dv_decode_video_segment(DVVideoContext *s,
  322. uint8_t *buf_ptr1,
  323. const uint16_t *mb_pos_ptr)
  324. {
  325. int quant, dc, dct_mode, class1, j;
  326. int mb_index, mb_x, mb_y, v, last_index;
  327. DCTELEM *block, *block1;
  328. int c_offset;
  329. uint8_t *y_ptr;
  330. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  331. uint8_t *buf_ptr;
  332. PutBitContext pb, vs_pb;
  333. GetBitContext gb;
  334. BlockInfo mb_data[5 * 6], *mb, *mb1;
  335. DECLARE_ALIGNED_8(DCTELEM, sblock[5*6][64]);
  336. DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */
  337. DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */
  338. const int log2_blocksize= 3-s->avctx->lowres;
  339. assert((((int)mb_bit_buffer)&7)==0);
  340. assert((((int)vs_bit_buffer)&7)==0);
  341. memset(sblock, 0, sizeof(sblock));
  342. /* pass 1 : read DC and AC coefficients in blocks */
  343. buf_ptr = buf_ptr1;
  344. block1 = &sblock[0][0];
  345. mb1 = mb_data;
  346. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
  347. for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) {
  348. /* skip header */
  349. quant = buf_ptr[3] & 0x0f;
  350. buf_ptr += 4;
  351. init_put_bits(&pb, mb_bit_buffer, 80);
  352. mb = mb1;
  353. block = block1;
  354. for(j = 0;j < 6; j++) {
  355. last_index = block_sizes[j];
  356. init_get_bits(&gb, buf_ptr, last_index);
  357. /* get the dc */
  358. dc = get_sbits(&gb, 9);
  359. dct_mode = get_bits1(&gb);
  360. mb->dct_mode = dct_mode;
  361. mb->scan_table = s->dv_zigzag[dct_mode];
  362. mb->iweight_table = dct_mode ? dv_iweight_248 : dv_iweight_88;
  363. class1 = get_bits(&gb, 2);
  364. mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
  365. [quant + dv_quant_offset[class1]];
  366. dc = dc << 2;
  367. /* convert to unsigned because 128 is not added in the
  368. standard IDCT */
  369. dc += 1024;
  370. block[0] = dc;
  371. buf_ptr += last_index >> 3;
  372. mb->pos = 0;
  373. mb->partial_bit_count = 0;
  374. #ifdef VLC_DEBUG
  375. printf("MB block: %d, %d ", mb_index, j);
  376. #endif
  377. dv_decode_ac(&gb, mb, block);
  378. /* write the remaining bits in a new buffer only if the
  379. block is finished */
  380. if (mb->pos >= 64)
  381. bit_copy(&pb, &gb);
  382. block += 64;
  383. mb++;
  384. }
  385. /* pass 2 : we can do it just after */
  386. #ifdef VLC_DEBUG
  387. printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
  388. #endif
  389. block = block1;
  390. mb = mb1;
  391. init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
  392. flush_put_bits(&pb);
  393. for(j = 0;j < 6; j++, block += 64, mb++) {
  394. if (mb->pos < 64 && get_bits_left(&gb) > 0) {
  395. dv_decode_ac(&gb, mb, block);
  396. /* if still not finished, no need to parse other blocks */
  397. if (mb->pos < 64)
  398. break;
  399. }
  400. }
  401. /* all blocks are finished, so the extra bytes can be used at
  402. the video segment level */
  403. if (j >= 6)
  404. bit_copy(&vs_pb, &gb);
  405. }
  406. /* we need a pass other the whole video segment */
  407. #ifdef VLC_DEBUG
  408. printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
  409. #endif
  410. block = &sblock[0][0];
  411. mb = mb_data;
  412. init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
  413. flush_put_bits(&vs_pb);
  414. for(mb_index = 0; mb_index < 5; mb_index++) {
  415. for(j = 0;j < 6; j++) {
  416. if (mb->pos < 64) {
  417. #ifdef VLC_DEBUG
  418. printf("start %d:%d\n", mb_index, j);
  419. #endif
  420. dv_decode_ac(&gb, mb, block);
  421. }
  422. if (mb->pos >= 64 && mb->pos < 127)
  423. av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
  424. block += 64;
  425. mb++;
  426. }
  427. }
  428. /* compute idct and place blocks */
  429. block = &sblock[0][0];
  430. mb = mb_data;
  431. for(mb_index = 0; mb_index < 5; mb_index++) {
  432. v = *mb_pos_ptr++;
  433. mb_x = v & 0xff;
  434. mb_y = v >> 8;
  435. if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
  436. y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + (mb_x>>1))<<log2_blocksize);
  437. c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
  438. } else { /* 4:1:1 or 4:2:0 */
  439. y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
  440. if (s->sys->pix_fmt == PIX_FMT_YUV411P)
  441. c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
  442. else /* 4:2:0 */
  443. c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize);
  444. }
  445. for(j = 0;j < 6; j++) {
  446. idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3];
  447. if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
  448. if (j == 0 || j == 2) {
  449. /* Y0 Y1 */
  450. idct_put(y_ptr + ((j >> 1)<<log2_blocksize),
  451. s->picture.linesize[0], block);
  452. } else if(j > 3) {
  453. /* Cr Cb */
  454. idct_put(s->picture.data[6 - j] + c_offset,
  455. s->picture.linesize[6 - j], block);
  456. }
  457. /* note: j=1 and j=3 are "dummy" blocks in 4:2:2 */
  458. } else { /* 4:1:1 or 4:2:0 */
  459. if (j < 4) {
  460. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  461. /* NOTE: at end of line, the macroblock is handled as 420 */
  462. idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block);
  463. } else {
  464. idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
  465. s->picture.linesize[0], block);
  466. }
  467. } else {
  468. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  469. uint64_t aligned_pixels[64/8];
  470. uint8_t *pixels= (uint8_t*)aligned_pixels;
  471. uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1;
  472. int x, y, linesize;
  473. /* NOTE: at end of line, the macroblock is handled as 420 */
  474. idct_put(pixels, 8, block);
  475. linesize = s->picture.linesize[6 - j];
  476. c_ptr = s->picture.data[6 - j] + c_offset;
  477. ptr = pixels;
  478. for(y = 0;y < (1<<log2_blocksize); y++) {
  479. ptr1= ptr + (1<<(log2_blocksize-1));
  480. c_ptr1 = c_ptr + (linesize<<log2_blocksize);
  481. for(x=0; x < (1<<(log2_blocksize-1)); x++){
  482. c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
  483. }
  484. c_ptr += linesize;
  485. ptr += 8;
  486. }
  487. } else {
  488. /* don't ask me why they inverted Cb and Cr ! */
  489. idct_put(s->picture.data[6 - j] + c_offset,
  490. s->picture.linesize[6 - j], block);
  491. }
  492. }
  493. }
  494. block += 64;
  495. mb++;
  496. }
  497. }
  498. }
  499. #ifdef DV_CODEC_TINY_TARGET
  500. /* Converts run and level (where level != 0) pair into vlc, returning bit size */
  501. static always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
  502. {
  503. int size;
  504. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  505. *vlc = dv_vlc_map[run][level].vlc | sign;
  506. size = dv_vlc_map[run][level].size;
  507. }
  508. else {
  509. if (level < DV_VLC_MAP_LEV_SIZE) {
  510. *vlc = dv_vlc_map[0][level].vlc | sign;
  511. size = dv_vlc_map[0][level].size;
  512. } else {
  513. *vlc = 0xfe00 | (level << 1) | sign;
  514. size = 16;
  515. }
  516. if (run) {
  517. *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
  518. (0x1f80 | (run - 1))) << size;
  519. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  520. }
  521. }
  522. return size;
  523. }
  524. static always_inline int dv_rl2vlc_size(int run, int level)
  525. {
  526. int size;
  527. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  528. size = dv_vlc_map[run][level].size;
  529. }
  530. else {
  531. size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
  532. if (run) {
  533. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  534. }
  535. }
  536. return size;
  537. }
  538. #else
  539. static always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
  540. {
  541. *vlc = dv_vlc_map[run][l].vlc | sign;
  542. return dv_vlc_map[run][l].size;
  543. }
  544. static always_inline int dv_rl2vlc_size(int run, int l)
  545. {
  546. return dv_vlc_map[run][l].size;
  547. }
  548. #endif
  549. typedef struct EncBlockInfo {
  550. int area_q[4];
  551. int bit_size[4];
  552. int prev[5];
  553. int cur_ac;
  554. int cno;
  555. int dct_mode;
  556. DCTELEM mb[64];
  557. uint8_t next[64];
  558. uint8_t sign[64];
  559. uint8_t partial_bit_count;
  560. uint32_t partial_bit_buffer; /* we can't use uint16_t here */
  561. } EncBlockInfo;
  562. static always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
  563. PutBitContext* pb_end)
  564. {
  565. int prev;
  566. int bits_left;
  567. PutBitContext* pb = pb_pool;
  568. int size = bi->partial_bit_count;
  569. uint32_t vlc = bi->partial_bit_buffer;
  570. bi->partial_bit_count = bi->partial_bit_buffer = 0;
  571. for(;;){
  572. /* Find suitable storage space */
  573. for (; size > (bits_left = put_bits_left(pb)); pb++) {
  574. if (bits_left) {
  575. size -= bits_left;
  576. put_bits(pb, bits_left, vlc >> size);
  577. vlc = vlc & ((1<<size)-1);
  578. }
  579. if (pb + 1 >= pb_end) {
  580. bi->partial_bit_count = size;
  581. bi->partial_bit_buffer = vlc;
  582. return pb;
  583. }
  584. }
  585. /* Store VLC */
  586. put_bits(pb, size, vlc);
  587. if(bi->cur_ac>=64)
  588. break;
  589. /* Construct the next VLC */
  590. prev= bi->cur_ac;
  591. bi->cur_ac = bi->next[prev];
  592. if(bi->cur_ac < 64){
  593. size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
  594. } else {
  595. size = 4; vlc = 6; /* End Of Block stamp */
  596. }
  597. }
  598. return pb;
  599. }
  600. static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
  601. const uint8_t* zigzag_scan, const int *weight, int bias)
  602. {
  603. int i, area;
  604. /* We offer two different methods for class number assignment: the
  605. method suggested in SMPTE 314M Table 22, and an improved
  606. method. The SMPTE method is very conservative; it assigns class
  607. 3 (i.e. severe quantization) to any block where the largest AC
  608. component is greater than 36. ffmpeg's DV encoder tracks AC bit
  609. consumption precisely, so there is no need to bias most blocks
  610. towards strongly lossy compression. Instead, we assign class 2
  611. to most blocks, and use class 3 only when strictly necessary
  612. (for blocks whose largest AC component exceeds 255). */
  613. #if 0 /* SMPTE spec method */
  614. static const int classes[] = {12, 24, 36, 0xffff};
  615. #else /* improved ffmpeg method */
  616. static const int classes[] = {-1, -1, 255, 0xffff};
  617. #endif
  618. int max=classes[0];
  619. int prev=0;
  620. bi->mb[0] = blk[0];
  621. for (area = 0; area < 4; area++) {
  622. bi->prev[area] = prev;
  623. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  624. for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
  625. int level = blk[zigzag_scan[i]];
  626. if (level+15 > 30U) {
  627. bi->sign[i] = (level>>31)&1;
  628. /* weigh it and and shift down into range, adding for rounding */
  629. /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
  630. AND the 2x doubling of the weights */
  631. level = (ABS(level) * weight[i] + (1<<(dv_weight_bits+3))) >> (dv_weight_bits+4);
  632. bi->mb[i] = level;
  633. if(level>max) max= level;
  634. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
  635. bi->next[prev]= i;
  636. prev= i;
  637. }
  638. }
  639. }
  640. bi->next[prev]= i;
  641. for(bi->cno = 0; max > classes[bi->cno]; bi->cno++);
  642. bi->cno += bias;
  643. if (bi->cno >= 3) {
  644. bi->cno = 3;
  645. prev=0;
  646. i= bi->next[prev];
  647. for (area = 0; area < 4; area++) {
  648. bi->prev[area] = prev;
  649. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  650. for (; i<mb_area_start[area+1]; i= bi->next[i]) {
  651. bi->mb[i] >>=1;
  652. if (bi->mb[i]) {
  653. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
  654. bi->next[prev]= i;
  655. prev= i;
  656. }
  657. }
  658. }
  659. bi->next[prev]= i;
  660. }
  661. }
  662. //FIXME replace this by dsputil
  663. #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
  664. static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
  665. DCTELEM *s;
  666. int score88 = 0;
  667. int score248 = 0;
  668. int i;
  669. /* Compute 8-8 score (small values give a better chance for 8-8 DCT) */
  670. s = blk;
  671. for(i=0; i<7; i++) {
  672. score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
  673. SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
  674. s += 8;
  675. }
  676. /* Compute 2-4-8 score (small values give a better chance for 2-4-8 DCT) */
  677. s = blk;
  678. for(i=0; i<6; i++) {
  679. score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
  680. SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
  681. s += 8;
  682. }
  683. return (score88 - score248 > -10);
  684. }
  685. static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
  686. {
  687. int size[5];
  688. int i, j, k, a, prev, a2;
  689. EncBlockInfo* b;
  690. size[0] = size[1] = size[2] = size[3] = size[4] = 1<<24;
  691. do {
  692. b = blks;
  693. for (i=0; i<5; i++) {
  694. if (!qnos[i])
  695. continue;
  696. qnos[i]--;
  697. size[i] = 0;
  698. for (j=0; j<6; j++, b++) {
  699. for (a=0; a<4; a++) {
  700. if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
  701. b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
  702. b->area_q[a]++;
  703. prev= b->prev[a];
  704. assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
  705. for (k= b->next[prev] ; k<mb_area_start[a+1]; k= b->next[k]) {
  706. b->mb[k] >>= 1;
  707. if (b->mb[k]) {
  708. b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  709. prev= k;
  710. } else {
  711. if(b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
  712. for(a2=a+1; b->next[k] >= mb_area_start[a2+1]; a2++)
  713. b->prev[a2] = prev;
  714. assert(a2<4);
  715. assert(b->mb[b->next[k]]);
  716. b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
  717. -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
  718. assert(b->prev[a2]==k && (a2+1 >= 4 || b->prev[a2+1]!=k));
  719. b->prev[a2] = prev;
  720. }
  721. b->next[prev] = b->next[k];
  722. }
  723. }
  724. b->prev[a+1]= prev;
  725. }
  726. size[i] += b->bit_size[a];
  727. }
  728. }
  729. if(vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
  730. return;
  731. }
  732. } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
  733. for(a=2; a==2 || vs_total_ac_bits < size[0]; a+=a){
  734. b = blks;
  735. size[0] = 5*6*4; //EOB
  736. for (j=0; j<6*5; j++, b++) {
  737. prev= b->prev[0];
  738. for (k= b->next[prev]; k<64; k= b->next[k]) {
  739. if(b->mb[k] < a && b->mb[k] > -a){
  740. b->next[prev] = b->next[k];
  741. }else{
  742. size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  743. prev= k;
  744. }
  745. }
  746. }
  747. }
  748. }
  749. static inline void dv_encode_video_segment(DVVideoContext *s,
  750. uint8_t *dif,
  751. const uint16_t *mb_pos_ptr)
  752. {
  753. int mb_index, i, j, v;
  754. int mb_x, mb_y, c_offset, linesize;
  755. uint8_t* y_ptr;
  756. uint8_t* data;
  757. uint8_t* ptr;
  758. int do_edge_wrap;
  759. DECLARE_ALIGNED_8(DCTELEM, block[64]);
  760. EncBlockInfo enc_blks[5*6];
  761. PutBitContext pbs[5*6];
  762. PutBitContext* pb;
  763. EncBlockInfo* enc_blk;
  764. int vs_bit_size = 0;
  765. int qnos[5];
  766. assert((((int)block) & 7) == 0);
  767. enc_blk = &enc_blks[0];
  768. pb = &pbs[0];
  769. for(mb_index = 0; mb_index < 5; mb_index++) {
  770. v = *mb_pos_ptr++;
  771. mb_x = v & 0xff;
  772. mb_y = v >> 8;
  773. if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
  774. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);
  775. } else { /* 4:1:1 */
  776. y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
  777. }
  778. if (s->sys->pix_fmt == PIX_FMT_YUV420P) {
  779. c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
  780. } else { /* 4:2:2 or 4:1:1 */
  781. c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));
  782. }
  783. do_edge_wrap = 0;
  784. qnos[mb_index] = 15; /* No quantization */
  785. ptr = dif + mb_index*80 + 4;
  786. for(j = 0;j < 6; j++) {
  787. int dummy = 0;
  788. if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
  789. if (j == 0 || j == 2) {
  790. /* Y0 Y1 */
  791. data = y_ptr + ((j>>1) * 8);
  792. linesize = s->picture.linesize[0];
  793. } else if (j > 3) {
  794. /* Cr Cb */
  795. data = s->picture.data[6 - j] + c_offset;
  796. linesize = s->picture.linesize[6 - j];
  797. } else {
  798. /* j=1 and j=3 are "dummy" blocks, used for AC data only */
  799. data = 0;
  800. linesize = 0;
  801. dummy = 1;
  802. }
  803. } else { /* 4:1:1 or 4:2:0 */
  804. if (j < 4) { /* Four Y blocks */
  805. /* NOTE: at end of line, the macroblock is handled as 420 */
  806. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
  807. data = y_ptr + (j * 8);
  808. } else {
  809. data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
  810. }
  811. linesize = s->picture.linesize[0];
  812. } else { /* Cr and Cb blocks */
  813. /* don't ask Fabrice why they inverted Cb and Cr ! */
  814. data = s->picture.data[6 - j] + c_offset;
  815. linesize = s->picture.linesize[6 - j];
  816. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
  817. do_edge_wrap = 1;
  818. }
  819. }
  820. /* Everything is set up -- now just copy data -> DCT block */
  821. if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
  822. uint8_t* d;
  823. DCTELEM *b = block;
  824. for (i=0;i<8;i++) {
  825. d = data + 8 * linesize;
  826. b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
  827. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  828. data += linesize;
  829. b += 8;
  830. }
  831. } else { /* Simple copy: 8x8 -> 8x8 */
  832. if (!dummy)
  833. s->get_pixels(block, data, linesize);
  834. }
  835. if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
  836. enc_blk->dct_mode = dv_guess_dct_mode(block);
  837. else
  838. enc_blk->dct_mode = 0;
  839. enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
  840. enc_blk->partial_bit_count = 0;
  841. enc_blk->partial_bit_buffer = 0;
  842. enc_blk->cur_ac = 0;
  843. if (dummy) {
  844. /* We rely on the fact that encoding all zeros leads to an immediate EOB,
  845. which is precisely what the spec calls for in the "dummy" blocks. */
  846. memset(block, 0, sizeof(block));
  847. } else {
  848. s->fdct[enc_blk->dct_mode](block);
  849. }
  850. dv_set_class_number(block, enc_blk,
  851. enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
  852. enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
  853. j/4);
  854. init_put_bits(pb, ptr, block_sizes[j]/8);
  855. put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
  856. put_bits(pb, 1, enc_blk->dct_mode);
  857. put_bits(pb, 2, enc_blk->cno);
  858. vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
  859. enc_blk->bit_size[2] + enc_blk->bit_size[3];
  860. ++enc_blk;
  861. ++pb;
  862. ptr += block_sizes[j]/8;
  863. }
  864. }
  865. if (vs_total_ac_bits < vs_bit_size)
  866. dv_guess_qnos(&enc_blks[0], &qnos[0]);
  867. for (i=0; i<5; i++) {
  868. dif[i*80 + 3] = qnos[i];
  869. }
  870. /* First pass over individual cells only */
  871. for (j=0; j<5*6; j++)
  872. dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
  873. /* Second pass over each MB space */
  874. for (j=0; j<5*6; j+=6) {
  875. pb= &pbs[j];
  876. for (i=0; i<6; i++) {
  877. if (enc_blks[i+j].partial_bit_count)
  878. pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);
  879. }
  880. }
  881. /* Third and final pass over the whole vides segment space */
  882. pb= &pbs[0];
  883. for (j=0; j<5*6; j++) {
  884. if (enc_blks[j].partial_bit_count)
  885. pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);
  886. if (enc_blks[j].partial_bit_count)
  887. av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
  888. }
  889. for (j=0; j<5*6; j++)
  890. flush_put_bits(&pbs[j]);
  891. }
  892. static int dv_decode_mt(AVCodecContext *avctx, void* sl)
  893. {
  894. DVVideoContext *s = avctx->priv_data;
  895. int slice = (size_t)sl;
  896. /* which DIF channel is this? */
  897. int chan = slice / (s->sys->difseg_size * 27);
  898. /* slice within the DIF channel */
  899. int chan_slice = slice % (s->sys->difseg_size * 27);
  900. /* byte offset of this channel's data */
  901. int chan_offset = chan * s->sys->difseg_size * 150 * 80;
  902. dv_decode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
  903. &s->sys->video_place[slice*5]);
  904. return 0;
  905. }
  906. static int dv_encode_mt(AVCodecContext *avctx, void* sl)
  907. {
  908. DVVideoContext *s = avctx->priv_data;
  909. int slice = (size_t)sl;
  910. /* which DIF channel is this? */
  911. int chan = slice / (s->sys->difseg_size * 27);
  912. /* slice within the DIF channel */
  913. int chan_slice = slice % (s->sys->difseg_size * 27);
  914. /* byte offset of this channel's data */
  915. int chan_offset = chan * s->sys->difseg_size * 150 * 80;
  916. dv_encode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
  917. &s->sys->video_place[slice*5]);
  918. return 0;
  919. }
  920. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  921. 144000 bytes for PAL - or twice those for 50Mbps) */
  922. static int dvvideo_decode_frame(AVCodecContext *avctx,
  923. void *data, int *data_size,
  924. uint8_t *buf, int buf_size)
  925. {
  926. DVVideoContext *s = avctx->priv_data;
  927. s->sys = dv_frame_profile(buf);
  928. if (!s->sys || buf_size < s->sys->frame_size)
  929. return -1; /* NOTE: we only accept several full frames */
  930. if(s->picture.data[0])
  931. avctx->release_buffer(avctx, &s->picture);
  932. s->picture.reference = 0;
  933. s->picture.key_frame = 1;
  934. s->picture.pict_type = FF_I_TYPE;
  935. avctx->pix_fmt = s->sys->pix_fmt;
  936. avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
  937. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  938. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  939. return -1;
  940. }
  941. s->picture.interlaced_frame = 1;
  942. s->picture.top_field_first = 0;
  943. s->buf = buf;
  944. avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
  945. s->sys->n_difchan * s->sys->difseg_size * 27);
  946. emms_c();
  947. /* return image */
  948. *data_size = sizeof(AVFrame);
  949. *(AVFrame*)data= s->picture;
  950. return s->sys->frame_size;
  951. }
  952. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  953. void *data)
  954. {
  955. DVVideoContext *s = c->priv_data;
  956. s->sys = dv_codec_profile(c);
  957. if (!s->sys)
  958. return -1;
  959. if(buf_size < s->sys->frame_size)
  960. return -1;
  961. c->pix_fmt = s->sys->pix_fmt;
  962. s->picture = *((AVFrame *)data);
  963. s->picture.key_frame = 1;
  964. s->picture.pict_type = FF_I_TYPE;
  965. s->buf = buf;
  966. c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
  967. s->sys->n_difchan * s->sys->difseg_size * 27);
  968. emms_c();
  969. /* Fill in just enough of the header for dv_frame_profile() to
  970. return the correct result, so that the frame can be decoded
  971. correctly. The rest of the metadata is filled in by the dvvideo
  972. avformat. (this should probably change so that encode_frame()
  973. fills in ALL of the metadata - e.g. for Quicktime-wrapped DV
  974. streams) */
  975. /* NTSC/PAL format */
  976. buf[3] = s->sys->dsf ? 0x80 : 0x00;
  977. /* 25Mbps or 50Mbps */
  978. buf[80*5 + 48 + 3] = (s->sys->pix_fmt == PIX_FMT_YUV422P) ? 0x4 : 0x0;
  979. return s->sys->frame_size;
  980. }
  981. static int dvvideo_close(AVCodecContext *c)
  982. {
  983. return 0;
  984. }
  985. #ifdef CONFIG_DVVIDEO_ENCODER
  986. AVCodec dvvideo_encoder = {
  987. "dvvideo",
  988. CODEC_TYPE_VIDEO,
  989. CODEC_ID_DVVIDEO,
  990. sizeof(DVVideoContext),
  991. dvvideo_init,
  992. dvvideo_encode_frame,
  993. dvvideo_close,
  994. NULL,
  995. CODEC_CAP_DR1,
  996. NULL
  997. };
  998. #endif // CONFIG_DVVIDEO_ENCODER
  999. AVCodec dvvideo_decoder = {
  1000. "dvvideo",
  1001. CODEC_TYPE_VIDEO,
  1002. CODEC_ID_DVVIDEO,
  1003. sizeof(DVVideoContext),
  1004. dvvideo_init,
  1005. NULL,
  1006. dvvideo_close,
  1007. dvvideo_decode_frame,
  1008. CODEC_CAP_DR1,
  1009. NULL
  1010. };