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.

1524 lines
54KB

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