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.

1747 lines
62KB

  1. /*
  2. * RV30/40 decoder common data
  3. * Copyright (c) 2007 Mike Melanson, Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RV30/40 decoder common data
  24. */
  25. #include "libavutil/internal.h"
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. #include "golomb.h"
  30. #include "internal.h"
  31. #include "mathops.h"
  32. #include "rectangle.h"
  33. #include "thread.h"
  34. #include "rv34vlc.h"
  35. #include "rv34data.h"
  36. #include "rv34.h"
  37. //#define DEBUG
  38. static inline void ZERO8x2(void* dst, int stride)
  39. {
  40. fill_rectangle(dst, 1, 2, stride, 0, 4);
  41. fill_rectangle(((uint8_t*)(dst))+4, 1, 2, stride, 0, 4);
  42. }
  43. /** translation of RV30/40 macroblock types to lavc ones */
  44. static const int rv34_mb_type_to_lavc[12] = {
  45. MB_TYPE_INTRA,
  46. MB_TYPE_INTRA16x16 | MB_TYPE_SEPARATE_DC,
  47. MB_TYPE_16x16 | MB_TYPE_L0,
  48. MB_TYPE_8x8 | MB_TYPE_L0,
  49. MB_TYPE_16x16 | MB_TYPE_L0,
  50. MB_TYPE_16x16 | MB_TYPE_L1,
  51. MB_TYPE_SKIP,
  52. MB_TYPE_DIRECT2 | MB_TYPE_16x16,
  53. MB_TYPE_16x8 | MB_TYPE_L0,
  54. MB_TYPE_8x16 | MB_TYPE_L0,
  55. MB_TYPE_16x16 | MB_TYPE_L0L1,
  56. MB_TYPE_16x16 | MB_TYPE_L0 | MB_TYPE_SEPARATE_DC
  57. };
  58. static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
  59. static int rv34_decode_mv(RV34DecContext *r, int block_type);
  60. /**
  61. * @name RV30/40 VLC generating functions
  62. * @{
  63. */
  64. static const int table_offs[] = {
  65. 0, 1818, 3622, 4144, 4698, 5234, 5804, 5868, 5900, 5932,
  66. 5996, 6252, 6316, 6348, 6380, 7674, 8944, 10274, 11668, 12250,
  67. 14060, 15846, 16372, 16962, 17512, 18148, 18180, 18212, 18244, 18308,
  68. 18564, 18628, 18660, 18692, 20036, 21314, 22648, 23968, 24614, 26384,
  69. 28190, 28736, 29366, 29938, 30608, 30640, 30672, 30704, 30768, 31024,
  70. 31088, 31120, 31184, 32570, 33898, 35236, 36644, 37286, 39020, 40802,
  71. 41368, 42052, 42692, 43348, 43380, 43412, 43444, 43476, 43604, 43668,
  72. 43700, 43732, 45100, 46430, 47778, 49160, 49802, 51550, 53340, 53972,
  73. 54648, 55348, 55994, 56122, 56154, 56186, 56218, 56346, 56410, 56442,
  74. 56474, 57878, 59290, 60636, 62036, 62682, 64460, 64524, 64588, 64716,
  75. 64844, 66076, 67466, 67978, 68542, 69064, 69648, 70296, 72010, 72074,
  76. 72138, 72202, 72330, 73572, 74936, 75454, 76030, 76566, 77176, 77822,
  77. 79582, 79646, 79678, 79742, 79870, 81180, 82536, 83064, 83672, 84242,
  78. 84934, 85576, 87384, 87448, 87480, 87544, 87672, 88982, 90340, 90902,
  79. 91598, 92182, 92846, 93488, 95246, 95278, 95310, 95374, 95502, 96878,
  80. 98266, 98848, 99542, 100234, 100884, 101524, 103320, 103352, 103384, 103416,
  81. 103480, 104874, 106222, 106910, 107584, 108258, 108902, 109544, 111366, 111398,
  82. 111430, 111462, 111494, 112878, 114320, 114988, 115660, 116310, 116950, 117592
  83. };
  84. static VLC_TYPE table_data[117592][2];
  85. /**
  86. * Generate VLC from codeword lengths.
  87. * @param bits codeword lengths (zeroes are accepted)
  88. * @param size length of input data
  89. * @param vlc output VLC
  90. * @param insyms symbols for input codes (NULL for default ones)
  91. * @param num VLC table number (for static initialization)
  92. */
  93. static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms,
  94. const int num)
  95. {
  96. int i;
  97. int counts[17] = {0}, codes[17];
  98. uint16_t cw[MAX_VLC_SIZE], syms[MAX_VLC_SIZE];
  99. uint8_t bits2[MAX_VLC_SIZE];
  100. int maxbits = 0, realsize = 0;
  101. for(i = 0; i < size; i++){
  102. if(bits[i]){
  103. bits2[realsize] = bits[i];
  104. syms[realsize] = insyms ? insyms[i] : i;
  105. realsize++;
  106. maxbits = FFMAX(maxbits, bits[i]);
  107. counts[bits[i]]++;
  108. }
  109. }
  110. codes[0] = 0;
  111. for(i = 0; i < 16; i++)
  112. codes[i+1] = (codes[i] + counts[i]) << 1;
  113. for(i = 0; i < realsize; i++)
  114. cw[i] = codes[bits2[i]]++;
  115. vlc->table = &table_data[table_offs[num]];
  116. vlc->table_allocated = table_offs[num + 1] - table_offs[num];
  117. init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
  118. bits2, 1, 1,
  119. cw, 2, 2,
  120. syms, 2, 2, INIT_VLC_USE_NEW_STATIC);
  121. }
  122. /**
  123. * Initialize all tables.
  124. */
  125. static av_cold void rv34_init_tables(void)
  126. {
  127. int i, j, k;
  128. for(i = 0; i < NUM_INTRA_TABLES; i++){
  129. for(j = 0; j < 2; j++){
  130. rv34_gen_vlc(rv34_table_intra_cbppat [i][j], CBPPAT_VLC_SIZE, &intra_vlcs[i].cbppattern[j], NULL, 19*i + 0 + j);
  131. rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].second_pattern[j], NULL, 19*i + 2 + j);
  132. rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].third_pattern[j], NULL, 19*i + 4 + j);
  133. for(k = 0; k < 4; k++){
  134. rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2], CBP_VLC_SIZE, &intra_vlcs[i].cbp[j][k], rv34_cbp_code, 19*i + 6 + j*4 + k);
  135. }
  136. }
  137. for(j = 0; j < 4; j++){
  138. rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, &intra_vlcs[i].first_pattern[j], NULL, 19*i + 14 + j);
  139. }
  140. rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, &intra_vlcs[i].coefficient, NULL, 19*i + 18);
  141. }
  142. for(i = 0; i < NUM_INTER_TABLES; i++){
  143. rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE, &inter_vlcs[i].cbppattern[0], NULL, i*12 + 95);
  144. for(j = 0; j < 4; j++){
  145. rv34_gen_vlc(rv34_inter_cbp[i][j], CBP_VLC_SIZE, &inter_vlcs[i].cbp[0][j], rv34_cbp_code, i*12 + 96 + j);
  146. }
  147. for(j = 0; j < 2; j++){
  148. rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE, &inter_vlcs[i].first_pattern[j], NULL, i*12 + 100 + j);
  149. rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].second_pattern[j], NULL, i*12 + 102 + j);
  150. rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].third_pattern[j], NULL, i*12 + 104 + j);
  151. }
  152. rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE, &inter_vlcs[i].coefficient, NULL, i*12 + 106);
  153. }
  154. }
  155. /** @} */ // vlc group
  156. /**
  157. * @name RV30/40 4x4 block decoding functions
  158. * @{
  159. */
  160. /**
  161. * Decode coded block pattern.
  162. */
  163. static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table)
  164. {
  165. int pattern, code, cbp=0;
  166. int ones;
  167. static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
  168. static const int shifts[4] = { 0, 2, 8, 10 };
  169. const int *curshift = shifts;
  170. int i, t, mask;
  171. code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
  172. pattern = code & 0xF;
  173. code >>= 4;
  174. ones = rv34_count_ones[pattern];
  175. for(mask = 8; mask; mask >>= 1, curshift++){
  176. if(pattern & mask)
  177. cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
  178. }
  179. for(i = 0; i < 4; i++){
  180. t = (modulo_three_table[code] >> (6 - 2*i)) & 3;
  181. if(t == 1)
  182. cbp |= cbp_masks[get_bits1(gb)] << i;
  183. if(t == 2)
  184. cbp |= cbp_masks[2] << i;
  185. }
  186. return cbp;
  187. }
  188. /**
  189. * Get one coefficient value from the bistream and store it.
  190. */
  191. static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc, int q)
  192. {
  193. if(coef){
  194. if(coef == esc){
  195. coef = get_vlc2(gb, vlc->table, 9, 2);
  196. if(coef > 23){
  197. coef -= 23;
  198. coef = 22 + ((1 << coef) | get_bits(gb, coef));
  199. }
  200. coef += esc;
  201. }
  202. if(get_bits1(gb))
  203. coef = -coef;
  204. *dst = (coef*q + 8) >> 4;
  205. }
  206. }
  207. /**
  208. * Decode 2x2 subblock of coefficients.
  209. */
  210. static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q)
  211. {
  212. int flags = modulo_three_table[code];
  213. decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q);
  214. if(is_block2){
  215. decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
  216. decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q);
  217. }else{
  218. decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q);
  219. decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q);
  220. }
  221. decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
  222. }
  223. /**
  224. * Decode a single coefficient.
  225. */
  226. static inline void decode_subblock1(DCTELEM *dst, int code, GetBitContext *gb, VLC *vlc, int q)
  227. {
  228. int coeff = modulo_three_table[code] >> 6;
  229. decode_coeff(dst, coeff, 3, gb, vlc, q);
  230. }
  231. static inline void decode_subblock3(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc,
  232. int q_dc, int q_ac1, int q_ac2)
  233. {
  234. int flags = modulo_three_table[code];
  235. decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q_dc);
  236. if(is_block2){
  237. decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
  238. decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
  239. }else{
  240. decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
  241. decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
  242. }
  243. decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q_ac2);
  244. }
  245. /**
  246. * Decode coefficients for 4x4 block.
  247. *
  248. * This is done by filling 2x2 subblocks with decoded coefficients
  249. * in this order (the same for subblocks and subblock coefficients):
  250. * o--o
  251. * /
  252. * /
  253. * o--o
  254. */
  255. static inline int rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2)
  256. {
  257. int code, pattern, has_ac = 1;
  258. code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
  259. pattern = code & 0x7;
  260. code >>= 3;
  261. if (modulo_three_table[code] & 0x3F) {
  262. decode_subblock3(dst, code, 0, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2);
  263. } else {
  264. decode_subblock1(dst, code, gb, &rvlc->coefficient, q_dc);
  265. if (!pattern)
  266. return 0;
  267. has_ac = 0;
  268. }
  269. if(pattern & 4){
  270. code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
  271. decode_subblock(dst + 4*0+2, code, 0, gb, &rvlc->coefficient, q_ac2);
  272. }
  273. if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
  274. code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
  275. decode_subblock(dst + 4*2+0, code, 1, gb, &rvlc->coefficient, q_ac2);
  276. }
  277. if(pattern & 1){
  278. code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
  279. decode_subblock(dst + 4*2+2, code, 0, gb, &rvlc->coefficient, q_ac2);
  280. }
  281. return has_ac || pattern;
  282. }
  283. /**
  284. * @name RV30/40 bitstream parsing
  285. * @{
  286. */
  287. /**
  288. * Decode starting slice position.
  289. * @todo Maybe replace with ff_h263_decode_mba() ?
  290. */
  291. int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
  292. {
  293. int i;
  294. for(i = 0; i < 5; i++)
  295. if(rv34_mb_max_sizes[i] >= mb_size - 1)
  296. break;
  297. return rv34_mb_bits_sizes[i];
  298. }
  299. /**
  300. * Select VLC set for decoding from current quantizer, modifier and frame type.
  301. */
  302. static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
  303. {
  304. if(mod == 2 && quant < 19) quant += 10;
  305. else if(mod && quant < 26) quant += 5;
  306. return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
  307. : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
  308. }
  309. /**
  310. * Decode intra macroblock header and return CBP in case of success, -1 otherwise.
  311. */
  312. static int rv34_decode_intra_mb_header(RV34DecContext *r, int8_t *intra_types)
  313. {
  314. MpegEncContext *s = &r->s;
  315. GetBitContext *gb = &s->gb;
  316. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  317. int t;
  318. r->is16 = get_bits1(gb);
  319. if(r->is16){
  320. s->current_picture_ptr->f.mb_type[mb_pos] = MB_TYPE_INTRA16x16;
  321. r->block_type = RV34_MB_TYPE_INTRA16x16;
  322. t = get_bits(gb, 2);
  323. fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
  324. r->luma_vlc = 2;
  325. }else{
  326. if(!r->rv30){
  327. if(!get_bits1(gb))
  328. av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
  329. }
  330. s->current_picture_ptr->f.mb_type[mb_pos] = MB_TYPE_INTRA;
  331. r->block_type = RV34_MB_TYPE_INTRA;
  332. if(r->decode_intra_types(r, gb, intra_types) < 0)
  333. return -1;
  334. r->luma_vlc = 1;
  335. }
  336. r->chroma_vlc = 0;
  337. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  338. return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
  339. }
  340. /**
  341. * Decode inter macroblock header and return CBP in case of success, -1 otherwise.
  342. */
  343. static int rv34_decode_inter_mb_header(RV34DecContext *r, int8_t *intra_types)
  344. {
  345. MpegEncContext *s = &r->s;
  346. GetBitContext *gb = &s->gb;
  347. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  348. int i, t;
  349. r->block_type = r->decode_mb_info(r);
  350. if(r->block_type == -1)
  351. return -1;
  352. s->current_picture_ptr->f.mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
  353. r->mb_type[mb_pos] = r->block_type;
  354. if(r->block_type == RV34_MB_SKIP){
  355. if(s->pict_type == AV_PICTURE_TYPE_P)
  356. r->mb_type[mb_pos] = RV34_MB_P_16x16;
  357. if(s->pict_type == AV_PICTURE_TYPE_B)
  358. r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
  359. }
  360. r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->f.mb_type[mb_pos]);
  361. rv34_decode_mv(r, r->block_type);
  362. if(r->block_type == RV34_MB_SKIP){
  363. fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
  364. return 0;
  365. }
  366. r->chroma_vlc = 1;
  367. r->luma_vlc = 0;
  368. if(IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
  369. if(r->is16){
  370. t = get_bits(gb, 2);
  371. fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
  372. r->luma_vlc = 2;
  373. }else{
  374. if(r->decode_intra_types(r, gb, intra_types) < 0)
  375. return -1;
  376. r->luma_vlc = 1;
  377. }
  378. r->chroma_vlc = 0;
  379. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  380. }else{
  381. for(i = 0; i < 16; i++)
  382. intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
  383. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  384. if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
  385. r->is16 = 1;
  386. r->chroma_vlc = 1;
  387. r->luma_vlc = 2;
  388. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  389. }
  390. }
  391. return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
  392. }
  393. /** @} */ //bitstream functions
  394. /**
  395. * @name motion vector related code (prediction, reconstruction, motion compensation)
  396. * @{
  397. */
  398. /** macroblock partition width in 8x8 blocks */
  399. static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
  400. /** macroblock partition height in 8x8 blocks */
  401. static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
  402. /** availability index for subblocks */
  403. static const uint8_t avail_indexes[4] = { 6, 7, 10, 11 };
  404. /**
  405. * motion vector prediction
  406. *
  407. * Motion prediction performed for the block by using median prediction of
  408. * motion vectors from the left, top and right top blocks but in corner cases
  409. * some other vectors may be used instead.
  410. */
  411. static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
  412. {
  413. MpegEncContext *s = &r->s;
  414. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  415. int A[2] = {0}, B[2], C[2];
  416. int i, j;
  417. int mx, my;
  418. int avail_index = avail_indexes[subblock_no];
  419. int c_off = part_sizes_w[block_type];
  420. mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
  421. if(subblock_no == 3)
  422. c_off = -1;
  423. if(r->avail_cache[avail_index - 1]){
  424. A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][0];
  425. A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][1];
  426. }
  427. if(r->avail_cache[avail_index - 4]){
  428. B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][0];
  429. B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][1];
  430. }else{
  431. B[0] = A[0];
  432. B[1] = A[1];
  433. }
  434. if(!r->avail_cache[avail_index - 4 + c_off]){
  435. if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){
  436. C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][0];
  437. C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][1];
  438. }else{
  439. C[0] = A[0];
  440. C[1] = A[1];
  441. }
  442. }else{
  443. C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][0];
  444. C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][1];
  445. }
  446. mx = mid_pred(A[0], B[0], C[0]);
  447. my = mid_pred(A[1], B[1], C[1]);
  448. mx += r->dmv[dmv_no][0];
  449. my += r->dmv[dmv_no][1];
  450. for(j = 0; j < part_sizes_h[block_type]; j++){
  451. for(i = 0; i < part_sizes_w[block_type]; i++){
  452. s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
  453. s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
  454. }
  455. }
  456. }
  457. #define GET_PTS_DIFF(a, b) ((a - b + 8192) & 0x1FFF)
  458. /**
  459. * Calculate motion vector component that should be added for direct blocks.
  460. */
  461. static int calc_add_mv(RV34DecContext *r, int dir, int val)
  462. {
  463. int mul = dir ? -r->weight2 : r->weight1;
  464. return (val * mul + 0x2000) >> 14;
  465. }
  466. /**
  467. * Predict motion vector for B-frame macroblock.
  468. */
  469. static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
  470. int A_avail, int B_avail, int C_avail,
  471. int *mx, int *my)
  472. {
  473. if(A_avail + B_avail + C_avail != 3){
  474. *mx = A[0] + B[0] + C[0];
  475. *my = A[1] + B[1] + C[1];
  476. if(A_avail + B_avail + C_avail == 2){
  477. *mx /= 2;
  478. *my /= 2;
  479. }
  480. }else{
  481. *mx = mid_pred(A[0], B[0], C[0]);
  482. *my = mid_pred(A[1], B[1], C[1]);
  483. }
  484. }
  485. /**
  486. * motion vector prediction for B-frames
  487. */
  488. static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
  489. {
  490. MpegEncContext *s = &r->s;
  491. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  492. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  493. int A[2], B[2], C[2];
  494. int has_A = 0, has_B = 0, has_C = 0;
  495. int mx, my;
  496. int i, j;
  497. Picture *cur_pic = s->current_picture_ptr;
  498. const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
  499. int type = cur_pic->f.mb_type[mb_pos];
  500. memset(A, 0, sizeof(A));
  501. memset(B, 0, sizeof(B));
  502. memset(C, 0, sizeof(C));
  503. if((r->avail_cache[6-1] & type) & mask){
  504. A[0] = cur_pic->f.motion_val[dir][mv_pos - 1][0];
  505. A[1] = cur_pic->f.motion_val[dir][mv_pos - 1][1];
  506. has_A = 1;
  507. }
  508. if((r->avail_cache[6-4] & type) & mask){
  509. B[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][0];
  510. B[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][1];
  511. has_B = 1;
  512. }
  513. if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
  514. C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][0];
  515. C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][1];
  516. has_C = 1;
  517. }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
  518. C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][0];
  519. C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][1];
  520. has_C = 1;
  521. }
  522. rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
  523. mx += r->dmv[dir][0];
  524. my += r->dmv[dir][1];
  525. for(j = 0; j < 2; j++){
  526. for(i = 0; i < 2; i++){
  527. cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
  528. cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
  529. }
  530. }
  531. if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
  532. ZERO8x2(cur_pic->f.motion_val[!dir][mv_pos], s->b8_stride);
  533. }
  534. }
  535. /**
  536. * motion vector prediction - RV3 version
  537. */
  538. static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
  539. {
  540. MpegEncContext *s = &r->s;
  541. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  542. int A[2] = {0}, B[2], C[2];
  543. int i, j, k;
  544. int mx, my;
  545. int avail_index = avail_indexes[0];
  546. if(r->avail_cache[avail_index - 1]){
  547. A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][0];
  548. A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][1];
  549. }
  550. if(r->avail_cache[avail_index - 4]){
  551. B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][0];
  552. B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][1];
  553. }else{
  554. B[0] = A[0];
  555. B[1] = A[1];
  556. }
  557. if(!r->avail_cache[avail_index - 4 + 2]){
  558. if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1])){
  559. C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][0];
  560. C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][1];
  561. }else{
  562. C[0] = A[0];
  563. C[1] = A[1];
  564. }
  565. }else{
  566. C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][0];
  567. C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][1];
  568. }
  569. mx = mid_pred(A[0], B[0], C[0]);
  570. my = mid_pred(A[1], B[1], C[1]);
  571. mx += r->dmv[0][0];
  572. my += r->dmv[0][1];
  573. for(j = 0; j < 2; j++){
  574. for(i = 0; i < 2; i++){
  575. for(k = 0; k < 2; k++){
  576. s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
  577. s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
  578. }
  579. }
  580. }
  581. }
  582. static const int chroma_coeffs[3] = { 0, 3, 5 };
  583. /**
  584. * generic motion compensation function
  585. *
  586. * @param r decoder context
  587. * @param block_type type of the current block
  588. * @param xoff horizontal offset from the start of the current block
  589. * @param yoff vertical offset from the start of the current block
  590. * @param mv_off offset to the motion vector information
  591. * @param width width of the current partition in 8x8 blocks
  592. * @param height height of the current partition in 8x8 blocks
  593. * @param dir motion compensation direction (i.e. from the last or the next reference frame)
  594. * @param thirdpel motion vectors are specified in 1/3 of pixel
  595. * @param qpel_mc a set of functions used to perform luma motion compensation
  596. * @param chroma_mc a set of functions used to perform chroma motion compensation
  597. */
  598. static inline void rv34_mc(RV34DecContext *r, const int block_type,
  599. const int xoff, const int yoff, int mv_off,
  600. const int width, const int height, int dir,
  601. const int thirdpel, int weighted,
  602. qpel_mc_func (*qpel_mc)[16],
  603. h264_chroma_mc_func (*chroma_mc))
  604. {
  605. MpegEncContext *s = &r->s;
  606. uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
  607. int dxy, mx, my, umx, umy, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  608. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
  609. int is16x16 = 1;
  610. if(thirdpel){
  611. int chroma_mx, chroma_my;
  612. mx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
  613. my = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
  614. lx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
  615. ly = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
  616. chroma_mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
  617. chroma_my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
  618. umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
  619. umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
  620. uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
  621. uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
  622. }else{
  623. int cx, cy;
  624. mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] >> 2;
  625. my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] >> 2;
  626. lx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] & 3;
  627. ly = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] & 3;
  628. cx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
  629. cy = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
  630. umx = cx >> 2;
  631. umy = cy >> 2;
  632. uvmx = (cx & 3) << 1;
  633. uvmy = (cy & 3) << 1;
  634. //due to some flaw RV40 uses the same MC compensation routine for H2V2 and H3V3
  635. if(uvmx == 6 && uvmy == 6)
  636. uvmx = uvmy = 4;
  637. }
  638. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  639. /* wait for the referenced mb row to be finished */
  640. int mb_row = FFMIN(s->mb_height - 1,
  641. s->mb_y + ((yoff + my + 5 + 8 * height) >> 4));
  642. AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f;
  643. ff_thread_await_progress(f, mb_row, 0);
  644. }
  645. dxy = ly*4 + lx;
  646. srcY = dir ? s->next_picture_ptr->f.data[0] : s->last_picture_ptr->f.data[0];
  647. srcU = dir ? s->next_picture_ptr->f.data[1] : s->last_picture_ptr->f.data[1];
  648. srcV = dir ? s->next_picture_ptr->f.data[2] : s->last_picture_ptr->f.data[2];
  649. src_x = s->mb_x * 16 + xoff + mx;
  650. src_y = s->mb_y * 16 + yoff + my;
  651. uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
  652. uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
  653. srcY += src_y * s->linesize + src_x;
  654. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  655. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  656. if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
  657. (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
  658. (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
  659. uint8_t *uvbuf = s->edge_emu_buffer + 22 * s->linesize;
  660. srcY -= 2 + 2*s->linesize;
  661. s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+6, (height<<3)+6,
  662. src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
  663. srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
  664. s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,
  665. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  666. s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,
  667. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  668. srcU = uvbuf;
  669. srcV = uvbuf + 16;
  670. }
  671. if(!weighted){
  672. Y = s->dest[0] + xoff + yoff *s->linesize;
  673. U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  674. V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  675. }else{
  676. Y = r->tmp_b_block_y [dir] + xoff + yoff *s->linesize;
  677. U = r->tmp_b_block_uv[dir*2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  678. V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  679. }
  680. if(block_type == RV34_MB_P_16x8){
  681. qpel_mc[1][dxy](Y, srcY, s->linesize);
  682. Y += 8;
  683. srcY += 8;
  684. }else if(block_type == RV34_MB_P_8x16){
  685. qpel_mc[1][dxy](Y, srcY, s->linesize);
  686. Y += 8 * s->linesize;
  687. srcY += 8 * s->linesize;
  688. }
  689. is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
  690. qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
  691. chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
  692. chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
  693. }
  694. static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
  695. const int xoff, const int yoff, int mv_off,
  696. const int width, const int height, int dir)
  697. {
  698. rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
  699. r->rdsp.put_pixels_tab,
  700. r->rdsp.put_chroma_pixels_tab);
  701. }
  702. static void rv4_weight(RV34DecContext *r)
  703. {
  704. r->rdsp.rv40_weight_pixels_tab[0](r->s.dest[0],
  705. r->tmp_b_block_y[0],
  706. r->tmp_b_block_y[1],
  707. r->weight1,
  708. r->weight2,
  709. r->s.linesize);
  710. r->rdsp.rv40_weight_pixels_tab[1](r->s.dest[1],
  711. r->tmp_b_block_uv[0],
  712. r->tmp_b_block_uv[2],
  713. r->weight1,
  714. r->weight2,
  715. r->s.uvlinesize);
  716. r->rdsp.rv40_weight_pixels_tab[1](r->s.dest[2],
  717. r->tmp_b_block_uv[1],
  718. r->tmp_b_block_uv[3],
  719. r->weight1,
  720. r->weight2,
  721. r->s.uvlinesize);
  722. }
  723. static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
  724. {
  725. int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
  726. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
  727. r->rdsp.put_pixels_tab,
  728. r->rdsp.put_chroma_pixels_tab);
  729. if(!weighted){
  730. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
  731. r->rdsp.avg_pixels_tab,
  732. r->rdsp.avg_chroma_pixels_tab);
  733. }else{
  734. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
  735. r->rdsp.put_pixels_tab,
  736. r->rdsp.put_chroma_pixels_tab);
  737. rv4_weight(r);
  738. }
  739. }
  740. static void rv34_mc_2mv_skip(RV34DecContext *r)
  741. {
  742. int i, j;
  743. int weighted = !r->rv30 && r->weight1 != 8192;
  744. for(j = 0; j < 2; j++)
  745. for(i = 0; i < 2; i++){
  746. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
  747. weighted,
  748. r->rdsp.put_pixels_tab,
  749. r->rdsp.put_chroma_pixels_tab);
  750. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
  751. weighted,
  752. weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
  753. weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
  754. }
  755. if(weighted)
  756. rv4_weight(r);
  757. }
  758. /** number of motion vectors in each macroblock type */
  759. static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
  760. /**
  761. * Decode motion vector differences
  762. * and perform motion vector reconstruction and motion compensation.
  763. */
  764. static int rv34_decode_mv(RV34DecContext *r, int block_type)
  765. {
  766. MpegEncContext *s = &r->s;
  767. GetBitContext *gb = &s->gb;
  768. int i, j, k, l;
  769. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  770. int next_bt;
  771. memset(r->dmv, 0, sizeof(r->dmv));
  772. for(i = 0; i < num_mvs[block_type]; i++){
  773. r->dmv[i][0] = svq3_get_se_golomb(gb);
  774. r->dmv[i][1] = svq3_get_se_golomb(gb);
  775. }
  776. switch(block_type){
  777. case RV34_MB_TYPE_INTRA:
  778. case RV34_MB_TYPE_INTRA16x16:
  779. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  780. return 0;
  781. case RV34_MB_SKIP:
  782. if(s->pict_type == AV_PICTURE_TYPE_P){
  783. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  784. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  785. break;
  786. }
  787. case RV34_MB_B_DIRECT:
  788. //surprisingly, it uses motion scheme from next reference frame
  789. /* wait for the current mb row to be finished */
  790. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  791. ff_thread_await_progress(&s->next_picture_ptr->f, FFMAX(0, s->mb_y-1), 0);
  792. next_bt = s->next_picture_ptr->f.mb_type[s->mb_x + s->mb_y * s->mb_stride];
  793. if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
  794. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  795. ZERO8x2(s->current_picture_ptr->f.motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  796. }else
  797. for(j = 0; j < 2; j++)
  798. for(i = 0; i < 2; i++)
  799. for(k = 0; k < 2; k++)
  800. for(l = 0; l < 2; l++)
  801. s->current_picture_ptr->f.motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][k]);
  802. if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
  803. rv34_mc_2mv(r, block_type);
  804. else
  805. rv34_mc_2mv_skip(r);
  806. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  807. break;
  808. case RV34_MB_P_16x16:
  809. case RV34_MB_P_MIX16x16:
  810. rv34_pred_mv(r, block_type, 0, 0);
  811. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  812. break;
  813. case RV34_MB_B_FORWARD:
  814. case RV34_MB_B_BACKWARD:
  815. r->dmv[1][0] = r->dmv[0][0];
  816. r->dmv[1][1] = r->dmv[0][1];
  817. if(r->rv30)
  818. rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
  819. else
  820. rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
  821. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
  822. break;
  823. case RV34_MB_P_16x8:
  824. case RV34_MB_P_8x16:
  825. rv34_pred_mv(r, block_type, 0, 0);
  826. rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
  827. if(block_type == RV34_MB_P_16x8){
  828. rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
  829. rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
  830. }
  831. if(block_type == RV34_MB_P_8x16){
  832. rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
  833. rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
  834. }
  835. break;
  836. case RV34_MB_B_BIDIR:
  837. rv34_pred_mv_b (r, block_type, 0);
  838. rv34_pred_mv_b (r, block_type, 1);
  839. rv34_mc_2mv (r, block_type);
  840. break;
  841. case RV34_MB_P_8x8:
  842. for(i=0;i< 4;i++){
  843. rv34_pred_mv(r, block_type, i, i);
  844. rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
  845. }
  846. break;
  847. }
  848. return 0;
  849. }
  850. /** @} */ // mv group
  851. /**
  852. * @name Macroblock reconstruction functions
  853. * @{
  854. */
  855. /** mapping of RV30/40 intra prediction types to standard H.264 types */
  856. static const int ittrans[9] = {
  857. DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
  858. VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
  859. };
  860. /** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
  861. static const int ittrans16[4] = {
  862. DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
  863. };
  864. /**
  865. * Perform 4x4 intra prediction.
  866. */
  867. static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
  868. {
  869. uint8_t *prev = dst - stride + 4;
  870. uint32_t topleft;
  871. if(!up && !left)
  872. itype = DC_128_PRED;
  873. else if(!up){
  874. if(itype == VERT_PRED) itype = HOR_PRED;
  875. if(itype == DC_PRED) itype = LEFT_DC_PRED;
  876. }else if(!left){
  877. if(itype == HOR_PRED) itype = VERT_PRED;
  878. if(itype == DC_PRED) itype = TOP_DC_PRED;
  879. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  880. }
  881. if(!down){
  882. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  883. if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
  884. if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
  885. }
  886. if(!right && up){
  887. topleft = dst[-stride + 3] * 0x01010101u;
  888. prev = (uint8_t*)&topleft;
  889. }
  890. r->h.pred4x4[itype](dst, prev, stride);
  891. }
  892. static inline int adjust_pred16(int itype, int up, int left)
  893. {
  894. if(!up && !left)
  895. itype = DC_128_PRED8x8;
  896. else if(!up){
  897. if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
  898. if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
  899. if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
  900. }else if(!left){
  901. if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
  902. if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
  903. if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
  904. }
  905. return itype;
  906. }
  907. static inline void rv34_process_block(RV34DecContext *r,
  908. uint8_t *pdst, int stride,
  909. int fc, int sc, int q_dc, int q_ac)
  910. {
  911. MpegEncContext *s = &r->s;
  912. DCTELEM *ptr = s->block[0];
  913. int has_ac = rv34_decode_block(ptr, &s->gb, r->cur_vlcs,
  914. fc, sc, q_dc, q_ac, q_ac);
  915. if(has_ac){
  916. r->rdsp.rv34_idct_add(pdst, stride, ptr);
  917. }else{
  918. r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
  919. ptr[0] = 0;
  920. }
  921. }
  922. static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
  923. {
  924. LOCAL_ALIGNED_16(DCTELEM, block16, [16]);
  925. MpegEncContext *s = &r->s;
  926. GetBitContext *gb = &s->gb;
  927. int q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
  928. q_ac = rv34_qscale_tab[s->qscale];
  929. uint8_t *dst = s->dest[0];
  930. DCTELEM *ptr = s->block[0];
  931. int avail[6*8] = {0};
  932. int i, j, itype, has_ac;
  933. memset(block16, 0, 16 * sizeof(*block16));
  934. // Set neighbour information.
  935. if(r->avail_cache[1])
  936. avail[0] = 1;
  937. if(r->avail_cache[2])
  938. avail[1] = avail[2] = 1;
  939. if(r->avail_cache[3])
  940. avail[3] = avail[4] = 1;
  941. if(r->avail_cache[4])
  942. avail[5] = 1;
  943. if(r->avail_cache[5])
  944. avail[8] = avail[16] = 1;
  945. if(r->avail_cache[9])
  946. avail[24] = avail[32] = 1;
  947. has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
  948. if(has_ac)
  949. r->rdsp.rv34_inv_transform(block16);
  950. else
  951. r->rdsp.rv34_inv_transform_dc(block16);
  952. itype = ittrans16[intra_types[0]];
  953. itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
  954. r->h.pred16x16[itype](dst, s->linesize);
  955. for(j = 0; j < 4; j++){
  956. for(i = 0; i < 4; i++, cbp >>= 1){
  957. int dc = block16[i + j*4];
  958. if(cbp & 1){
  959. has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
  960. }else
  961. has_ac = 0;
  962. if(has_ac){
  963. ptr[0] = dc;
  964. r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
  965. }else
  966. r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
  967. }
  968. dst += 4*s->linesize;
  969. }
  970. itype = ittrans16[intra_types[0]];
  971. if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
  972. itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
  973. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  974. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  975. for(j = 1; j < 3; j++){
  976. dst = s->dest[j];
  977. r->h.pred8x8[itype](dst, s->uvlinesize);
  978. for(i = 0; i < 4; i++, cbp >>= 1){
  979. uint8_t *pdst;
  980. if(!(cbp & 1)) continue;
  981. pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
  982. rv34_process_block(r, pdst, s->uvlinesize,
  983. r->chroma_vlc, 1, q_dc, q_ac);
  984. }
  985. }
  986. }
  987. static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
  988. {
  989. MpegEncContext *s = &r->s;
  990. uint8_t *dst = s->dest[0];
  991. int avail[6*8] = {0};
  992. int i, j, k;
  993. int idx, q_ac, q_dc;
  994. // Set neighbour information.
  995. if(r->avail_cache[1])
  996. avail[0] = 1;
  997. if(r->avail_cache[2])
  998. avail[1] = avail[2] = 1;
  999. if(r->avail_cache[3])
  1000. avail[3] = avail[4] = 1;
  1001. if(r->avail_cache[4])
  1002. avail[5] = 1;
  1003. if(r->avail_cache[5])
  1004. avail[8] = avail[16] = 1;
  1005. if(r->avail_cache[9])
  1006. avail[24] = avail[32] = 1;
  1007. q_ac = rv34_qscale_tab[s->qscale];
  1008. for(j = 0; j < 4; j++){
  1009. idx = 9 + j*8;
  1010. for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
  1011. rv34_pred_4x4_block(r, dst, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
  1012. avail[idx] = 1;
  1013. if(!(cbp & 1)) continue;
  1014. rv34_process_block(r, dst, s->linesize,
  1015. r->luma_vlc, 0, q_ac, q_ac);
  1016. }
  1017. dst += s->linesize * 4 - 4*4;
  1018. intra_types += r->intra_types_stride;
  1019. }
  1020. intra_types -= r->intra_types_stride * 4;
  1021. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  1022. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  1023. for(k = 0; k < 2; k++){
  1024. dst = s->dest[1+k];
  1025. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
  1026. for(j = 0; j < 2; j++){
  1027. int* acache = r->avail_cache + 6 + j*4;
  1028. for(i = 0; i < 2; i++, cbp >>= 1, acache++){
  1029. int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
  1030. rv34_pred_4x4_block(r, dst+4*i, s->uvlinesize, itype, acache[-4], acache[-1], !i && !j, acache[-3]);
  1031. acache[0] = 1;
  1032. if(!(cbp&1)) continue;
  1033. rv34_process_block(r, dst + 4*i, s->uvlinesize,
  1034. r->chroma_vlc, 1, q_dc, q_ac);
  1035. }
  1036. dst += 4*s->uvlinesize;
  1037. }
  1038. }
  1039. }
  1040. static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
  1041. {
  1042. int d;
  1043. d = motion_val[0][0] - motion_val[-step][0];
  1044. if(d < -3 || d > 3)
  1045. return 1;
  1046. d = motion_val[0][1] - motion_val[-step][1];
  1047. if(d < -3 || d > 3)
  1048. return 1;
  1049. return 0;
  1050. }
  1051. static int rv34_set_deblock_coef(RV34DecContext *r)
  1052. {
  1053. MpegEncContext *s = &r->s;
  1054. int hmvmask = 0, vmvmask = 0, i, j;
  1055. int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  1056. int16_t (*motion_val)[2] = &s->current_picture_ptr->f.motion_val[0][midx];
  1057. for(j = 0; j < 16; j += 8){
  1058. for(i = 0; i < 2; i++){
  1059. if(is_mv_diff_gt_3(motion_val + i, 1))
  1060. vmvmask |= 0x11 << (j + i*2);
  1061. if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
  1062. hmvmask |= 0x03 << (j + i*2);
  1063. }
  1064. motion_val += s->b8_stride;
  1065. }
  1066. if(s->first_slice_line)
  1067. hmvmask &= ~0x000F;
  1068. if(!s->mb_x)
  1069. vmvmask &= ~0x1111;
  1070. if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
  1071. vmvmask |= (vmvmask & 0x4444) >> 1;
  1072. hmvmask |= (hmvmask & 0x0F00) >> 4;
  1073. if(s->mb_x)
  1074. r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
  1075. if(!s->first_slice_line)
  1076. r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
  1077. }
  1078. return hmvmask | vmvmask;
  1079. }
  1080. static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
  1081. {
  1082. MpegEncContext *s = &r->s;
  1083. GetBitContext *gb = &s->gb;
  1084. uint8_t *dst = s->dest[0];
  1085. DCTELEM *ptr = s->block[0];
  1086. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1087. int cbp, cbp2;
  1088. int q_dc, q_ac, has_ac;
  1089. int i, j;
  1090. int dist;
  1091. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1092. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1093. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
  1094. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1095. if(s->mb_x && dist)
  1096. r->avail_cache[5] =
  1097. r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
  1098. if(dist >= s->mb_width)
  1099. r->avail_cache[2] =
  1100. r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
  1101. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1102. r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
  1103. if(s->mb_x && dist > s->mb_width)
  1104. r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
  1105. s->qscale = r->si.quant;
  1106. cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
  1107. r->cbp_luma [mb_pos] = cbp;
  1108. r->cbp_chroma[mb_pos] = cbp >> 16;
  1109. r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
  1110. s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
  1111. if(cbp == -1)
  1112. return -1;
  1113. if (IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
  1114. if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
  1115. else rv34_output_intra(r, intra_types, cbp);
  1116. return 0;
  1117. }
  1118. if(r->is16){
  1119. // Only for RV34_MB_P_MIX16x16
  1120. LOCAL_ALIGNED_16(DCTELEM, block16, [16]);
  1121. memset(block16, 0, 16 * sizeof(*block16));
  1122. q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
  1123. q_ac = rv34_qscale_tab[s->qscale];
  1124. if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac))
  1125. r->rdsp.rv34_inv_transform(block16);
  1126. else
  1127. r->rdsp.rv34_inv_transform_dc(block16);
  1128. q_ac = rv34_qscale_tab[s->qscale];
  1129. for(j = 0; j < 4; j++){
  1130. for(i = 0; i < 4; i++, cbp >>= 1){
  1131. int dc = block16[i + j*4];
  1132. if(cbp & 1){
  1133. has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
  1134. }else
  1135. has_ac = 0;
  1136. if(has_ac){
  1137. ptr[0] = dc;
  1138. r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
  1139. }else
  1140. r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
  1141. }
  1142. dst += 4*s->linesize;
  1143. }
  1144. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  1145. }else{
  1146. q_ac = rv34_qscale_tab[s->qscale];
  1147. for(j = 0; j < 4; j++){
  1148. for(i = 0; i < 4; i++, cbp >>= 1){
  1149. if(!(cbp & 1)) continue;
  1150. rv34_process_block(r, dst + 4*i, s->linesize,
  1151. r->luma_vlc, 0, q_ac, q_ac);
  1152. }
  1153. dst += 4*s->linesize;
  1154. }
  1155. }
  1156. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  1157. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  1158. for(j = 1; j < 3; j++){
  1159. dst = s->dest[j];
  1160. for(i = 0; i < 4; i++, cbp >>= 1){
  1161. uint8_t *pdst;
  1162. if(!(cbp & 1)) continue;
  1163. pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
  1164. rv34_process_block(r, pdst, s->uvlinesize,
  1165. r->chroma_vlc, 1, q_dc, q_ac);
  1166. }
  1167. }
  1168. return 0;
  1169. }
  1170. static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
  1171. {
  1172. MpegEncContext *s = &r->s;
  1173. int cbp, dist;
  1174. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1175. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1176. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1177. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
  1178. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1179. if(s->mb_x && dist)
  1180. r->avail_cache[5] =
  1181. r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
  1182. if(dist >= s->mb_width)
  1183. r->avail_cache[2] =
  1184. r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
  1185. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1186. r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
  1187. if(s->mb_x && dist > s->mb_width)
  1188. r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
  1189. s->qscale = r->si.quant;
  1190. cbp = rv34_decode_intra_mb_header(r, intra_types);
  1191. r->cbp_luma [mb_pos] = cbp;
  1192. r->cbp_chroma[mb_pos] = cbp >> 16;
  1193. r->deblock_coefs[mb_pos] = 0xFFFF;
  1194. s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
  1195. if(cbp == -1)
  1196. return -1;
  1197. if(r->is16){
  1198. rv34_output_i16x16(r, intra_types, cbp);
  1199. return 0;
  1200. }
  1201. rv34_output_intra(r, intra_types, cbp);
  1202. return 0;
  1203. }
  1204. static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
  1205. {
  1206. int bits;
  1207. if(s->mb_y >= s->mb_height)
  1208. return 1;
  1209. if(!s->mb_num_left)
  1210. return 1;
  1211. if(r->s.mb_skip_run > 1)
  1212. return 0;
  1213. bits = get_bits_left(&s->gb);
  1214. if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits)))
  1215. return 1;
  1216. return 0;
  1217. }
  1218. static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
  1219. {
  1220. MpegEncContext *s = &r->s;
  1221. GetBitContext *gb = &s->gb;
  1222. int mb_pos;
  1223. int res;
  1224. init_get_bits(&r->s.gb, buf, buf_size*8);
  1225. res = r->parse_slice_header(r, gb, &r->si);
  1226. if(res < 0){
  1227. av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
  1228. return -1;
  1229. }
  1230. if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
  1231. if(s->width != r->si.width || s->height != r->si.height){
  1232. av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
  1233. MPV_common_end(s);
  1234. s->width = r->si.width;
  1235. s->height = r->si.height;
  1236. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1237. if(MPV_common_init(s) < 0)
  1238. return -1;
  1239. r->intra_types_stride = s->mb_width*4 + 4;
  1240. r->intra_types_hist = av_realloc(r->intra_types_hist, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1241. r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
  1242. r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
  1243. r->cbp_luma = av_realloc(r->cbp_luma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
  1244. r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
  1245. r->deblock_coefs = av_realloc(r->deblock_coefs, r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
  1246. av_freep(&r->tmp_b_block_base);
  1247. }
  1248. s->pict_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
  1249. if(MPV_frame_start(s, s->avctx) < 0)
  1250. return -1;
  1251. ff_er_frame_start(s);
  1252. if (!r->tmp_b_block_base) {
  1253. int i;
  1254. r->tmp_b_block_base = av_malloc(s->linesize * 48);
  1255. for (i = 0; i < 2; i++)
  1256. r->tmp_b_block_y[i] = r->tmp_b_block_base + i * 16 * s->linesize;
  1257. for (i = 0; i < 4; i++)
  1258. r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
  1259. + (i >> 1) * 8 * s->uvlinesize + (i & 1) * 16;
  1260. }
  1261. r->cur_pts = r->si.pts;
  1262. if(s->pict_type != AV_PICTURE_TYPE_B){
  1263. r->last_pts = r->next_pts;
  1264. r->next_pts = r->cur_pts;
  1265. }else{
  1266. int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
  1267. int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
  1268. int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
  1269. if(!refdist){
  1270. r->weight1 = r->weight2 = 8192;
  1271. }else{
  1272. r->weight1 = (dist0 << 14) / refdist;
  1273. r->weight2 = (dist1 << 14) / refdist;
  1274. }
  1275. }
  1276. s->mb_x = s->mb_y = 0;
  1277. ff_thread_finish_setup(s->avctx);
  1278. } else {
  1279. int slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
  1280. if (slice_type != s->pict_type) {
  1281. av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
  1282. return AVERROR_INVALIDDATA;
  1283. }
  1284. }
  1285. r->si.end = end;
  1286. s->qscale = r->si.quant;
  1287. s->mb_num_left = r->si.end - r->si.start;
  1288. r->s.mb_skip_run = 0;
  1289. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  1290. if(r->si.start != mb_pos){
  1291. av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
  1292. s->mb_x = r->si.start % s->mb_width;
  1293. s->mb_y = r->si.start / s->mb_width;
  1294. }
  1295. memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1296. s->first_slice_line = 1;
  1297. s->resync_mb_x = s->mb_x;
  1298. s->resync_mb_y = s->mb_y;
  1299. ff_init_block_index(s);
  1300. while(!check_slice_end(r, s)) {
  1301. ff_update_block_index(s);
  1302. if(r->si.type)
  1303. res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
  1304. else
  1305. res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
  1306. if(res < 0){
  1307. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
  1308. return -1;
  1309. }
  1310. if (++s->mb_x == s->mb_width) {
  1311. s->mb_x = 0;
  1312. s->mb_y++;
  1313. ff_init_block_index(s);
  1314. memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
  1315. memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
  1316. if(r->loop_filter && s->mb_y >= 2)
  1317. r->loop_filter(r, s->mb_y - 2);
  1318. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  1319. ff_thread_report_progress(&s->current_picture_ptr->f,
  1320. s->mb_y - 2, 0);
  1321. }
  1322. if(s->mb_x == s->resync_mb_x)
  1323. s->first_slice_line=0;
  1324. s->mb_num_left--;
  1325. }
  1326. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  1327. return s->mb_y == s->mb_height;
  1328. }
  1329. /** @} */ // recons group end
  1330. /**
  1331. * Initialize decoder.
  1332. */
  1333. av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
  1334. {
  1335. RV34DecContext *r = avctx->priv_data;
  1336. MpegEncContext *s = &r->s;
  1337. MPV_decode_defaults(s);
  1338. s->avctx = avctx;
  1339. s->out_format = FMT_H263;
  1340. s->codec_id = avctx->codec_id;
  1341. s->width = avctx->width;
  1342. s->height = avctx->height;
  1343. r->s.avctx = avctx;
  1344. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  1345. r->s.flags |= CODEC_FLAG_EMU_EDGE;
  1346. avctx->pix_fmt = PIX_FMT_YUV420P;
  1347. avctx->has_b_frames = 1;
  1348. s->low_delay = 0;
  1349. if (MPV_common_init(s) < 0)
  1350. return -1;
  1351. ff_h264_pred_init(&r->h, CODEC_ID_RV40, 8, 1);
  1352. #if CONFIG_RV30_DECODER
  1353. if (avctx->codec_id == CODEC_ID_RV30)
  1354. ff_rv30dsp_init(&r->rdsp, &r->s.dsp);
  1355. #endif
  1356. #if CONFIG_RV40_DECODER
  1357. if (avctx->codec_id == CODEC_ID_RV40)
  1358. ff_rv40dsp_init(&r->rdsp, &r->s.dsp);
  1359. #endif
  1360. r->intra_types_stride = 4*s->mb_stride + 4;
  1361. r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1362. r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
  1363. r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
  1364. r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
  1365. r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
  1366. r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
  1367. if(!intra_vlcs[0].cbppattern[0].bits)
  1368. rv34_init_tables();
  1369. return 0;
  1370. }
  1371. int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
  1372. {
  1373. RV34DecContext *r = avctx->priv_data;
  1374. r->s.avctx = avctx;
  1375. if (avctx->internal->is_copy) {
  1376. r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height *
  1377. sizeof(*r->cbp_chroma));
  1378. r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height *
  1379. sizeof(*r->cbp_luma));
  1380. r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height *
  1381. sizeof(*r->deblock_coefs));
  1382. r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
  1383. sizeof(*r->intra_types_hist));
  1384. r->mb_type = av_malloc(r->s.mb_stride * r->s.mb_height *
  1385. sizeof(*r->mb_type));
  1386. if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs &&
  1387. r->intra_types_hist && r->mb_type)) {
  1388. av_freep(&r->cbp_chroma);
  1389. av_freep(&r->cbp_luma);
  1390. av_freep(&r->deblock_coefs);
  1391. av_freep(&r->intra_types_hist);
  1392. av_freep(&r->mb_type);
  1393. r->intra_types = NULL;
  1394. return AVERROR(ENOMEM);
  1395. }
  1396. r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
  1397. r->tmp_b_block_base = NULL;
  1398. memset(r->mb_type, 0, r->s.mb_stride * r->s.mb_height *
  1399. sizeof(*r->mb_type));
  1400. MPV_common_init(&r->s);
  1401. }
  1402. return 0;
  1403. }
  1404. int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1405. {
  1406. RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
  1407. MpegEncContext * const s = &r->s, * const s1 = &r1->s;
  1408. int err;
  1409. if (dst == src || !s1->context_initialized)
  1410. return 0;
  1411. if ((err = ff_mpeg_update_thread_context(dst, src)))
  1412. return err;
  1413. r->cur_pts = r1->cur_pts;
  1414. r->last_pts = r1->last_pts;
  1415. r->next_pts = r1->next_pts;
  1416. memset(&r->si, 0, sizeof(r->si));
  1417. /* necessary since it is it the condition checked for in decode_slice
  1418. * to call MPV_frame_start. cmp. comment at the end of decode_frame */
  1419. s->current_picture_ptr = NULL;
  1420. return 0;
  1421. }
  1422. static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
  1423. {
  1424. if(avctx->slice_count) return avctx->slice_offset[n];
  1425. else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
  1426. }
  1427. int ff_rv34_decode_frame(AVCodecContext *avctx,
  1428. void *data, int *data_size,
  1429. AVPacket *avpkt)
  1430. {
  1431. const uint8_t *buf = avpkt->data;
  1432. int buf_size = avpkt->size;
  1433. RV34DecContext *r = avctx->priv_data;
  1434. MpegEncContext *s = &r->s;
  1435. AVFrame *pict = data;
  1436. SliceInfo si;
  1437. int i;
  1438. int slice_count;
  1439. const uint8_t *slices_hdr = NULL;
  1440. int last = 0;
  1441. /* no supplementary picture */
  1442. if (buf_size == 0) {
  1443. /* special case for last picture */
  1444. if (s->low_delay==0 && s->next_picture_ptr) {
  1445. *pict = *(AVFrame*)s->next_picture_ptr;
  1446. s->next_picture_ptr = NULL;
  1447. *data_size = sizeof(AVFrame);
  1448. }
  1449. return 0;
  1450. }
  1451. if(!avctx->slice_count){
  1452. slice_count = (*buf++) + 1;
  1453. slices_hdr = buf + 4;
  1454. buf += 8 * slice_count;
  1455. buf_size -= 1 + 8 * slice_count;
  1456. }else
  1457. slice_count = avctx->slice_count;
  1458. //parse first slice header to check whether this frame can be decoded
  1459. if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
  1460. get_slice_offset(avctx, slices_hdr, 0) > buf_size){
  1461. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1462. return -1;
  1463. }
  1464. init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
  1465. if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
  1466. av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
  1467. return -1;
  1468. }
  1469. if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) && si.type == AV_PICTURE_TYPE_B)
  1470. return -1;
  1471. if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
  1472. || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
  1473. || avctx->skip_frame >= AVDISCARD_ALL)
  1474. return avpkt->size;
  1475. for(i = 0; i < slice_count; i++){
  1476. int offset = get_slice_offset(avctx, slices_hdr, i);
  1477. int size;
  1478. if(i+1 == slice_count)
  1479. size = buf_size - offset;
  1480. else
  1481. size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
  1482. if(offset < 0 || offset > buf_size){
  1483. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1484. break;
  1485. }
  1486. r->si.end = s->mb_width * s->mb_height;
  1487. if(i+1 < slice_count){
  1488. if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||
  1489. get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {
  1490. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1491. break;
  1492. }
  1493. init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, i+1), (buf_size-get_slice_offset(avctx, slices_hdr, i+1))*8);
  1494. if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
  1495. if(i+2 < slice_count)
  1496. size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
  1497. else
  1498. size = buf_size - offset;
  1499. }else
  1500. r->si.end = si.start;
  1501. }
  1502. if (size < 0 || size > buf_size - offset) {
  1503. av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
  1504. break;
  1505. }
  1506. last = rv34_decode_slice(r, r->si.end, buf + offset, size);
  1507. s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
  1508. if(last)
  1509. break;
  1510. }
  1511. if(last && s->current_picture_ptr){
  1512. if(r->loop_filter)
  1513. r->loop_filter(r, s->mb_height - 1);
  1514. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  1515. ff_thread_report_progress(&s->current_picture_ptr->f,
  1516. s->mb_height - 1, 0);
  1517. ff_er_frame_end(s);
  1518. MPV_frame_end(s);
  1519. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1520. *pict = *(AVFrame*)s->current_picture_ptr;
  1521. } else if (s->last_picture_ptr != NULL) {
  1522. *pict = *(AVFrame*)s->last_picture_ptr;
  1523. }
  1524. if(s->last_picture_ptr || s->low_delay){
  1525. *data_size = sizeof(AVFrame);
  1526. ff_print_debug_info(s, pict);
  1527. }
  1528. s->current_picture_ptr = NULL; //so we can detect if frame_end wasnt called (find some nicer solution...)
  1529. }
  1530. return avpkt->size;
  1531. }
  1532. av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
  1533. {
  1534. RV34DecContext *r = avctx->priv_data;
  1535. MPV_common_end(&r->s);
  1536. av_freep(&r->intra_types_hist);
  1537. r->intra_types = NULL;
  1538. av_freep(&r->tmp_b_block_base);
  1539. av_freep(&r->mb_type);
  1540. av_freep(&r->cbp_luma);
  1541. av_freep(&r->cbp_chroma);
  1542. av_freep(&r->deblock_coefs);
  1543. return 0;
  1544. }