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.

1613 lines
59KB

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