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.

1532 lines
53KB

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