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.

1810 lines
63KB

  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 "error_resilience.h"
  28. #include "mpegutils.h"
  29. #include "mpegvideo.h"
  30. #include "golomb.h"
  31. #include "internal.h"
  32. #include "mathops.h"
  33. #include "rectangle.h"
  34. #include "thread.h"
  35. #include "rv34vlc.h"
  36. #include "rv34data.h"
  37. #include "rv34.h"
  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. ff_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(int16_t *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(int16_t *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q)
  211. {
  212. int flags = modulo_three_table[code];
  213. decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q);
  214. if(is_block2){
  215. decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
  216. decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q);
  217. }else{
  218. decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q);
  219. decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q);
  220. }
  221. decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
  222. }
  223. /**
  224. * Decode a single coefficient.
  225. */
  226. static inline void decode_subblock1(int16_t *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(int16_t *dst, int code, GetBitContext *gb, VLC *vlc,
  232. int q_dc, int q_ac1, int q_ac2)
  233. {
  234. int flags = modulo_three_table[code];
  235. decode_coeff(dst+0*4+0, (flags >> 6) , 3, gb, vlc, q_dc);
  236. decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
  237. decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
  238. decode_coeff(dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q_ac2);
  239. }
  240. /**
  241. * Decode coefficients for 4x4 block.
  242. *
  243. * This is done by filling 2x2 subblocks with decoded coefficients
  244. * in this order (the same for subblocks and subblock coefficients):
  245. * o--o
  246. * /
  247. * /
  248. * o--o
  249. */
  250. static int rv34_decode_block(int16_t *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2)
  251. {
  252. int code, pattern, has_ac = 1;
  253. code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
  254. pattern = code & 0x7;
  255. code >>= 3;
  256. if (modulo_three_table[code] & 0x3F) {
  257. decode_subblock3(dst, code, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2);
  258. } else {
  259. decode_subblock1(dst, code, gb, &rvlc->coefficient, q_dc);
  260. if (!pattern)
  261. return 0;
  262. has_ac = 0;
  263. }
  264. if(pattern & 4){
  265. code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
  266. decode_subblock(dst + 4*0+2, code, 0, gb, &rvlc->coefficient, q_ac2);
  267. }
  268. if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
  269. code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
  270. decode_subblock(dst + 4*2+0, code, 1, gb, &rvlc->coefficient, q_ac2);
  271. }
  272. if(pattern & 1){
  273. code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
  274. decode_subblock(dst + 4*2+2, code, 0, gb, &rvlc->coefficient, q_ac2);
  275. }
  276. return has_ac | pattern;
  277. }
  278. /**
  279. * @name RV30/40 bitstream parsing
  280. * @{
  281. */
  282. /**
  283. * Decode starting slice position.
  284. * @todo Maybe replace with ff_h263_decode_mba() ?
  285. */
  286. int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
  287. {
  288. int i;
  289. for(i = 0; i < 5; i++)
  290. if(rv34_mb_max_sizes[i] >= mb_size - 1)
  291. break;
  292. return rv34_mb_bits_sizes[i];
  293. }
  294. /**
  295. * Select VLC set for decoding from current quantizer, modifier and frame type.
  296. */
  297. static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
  298. {
  299. if(mod == 2 && quant < 19) quant += 10;
  300. else if(mod && quant < 26) quant += 5;
  301. return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
  302. : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
  303. }
  304. /**
  305. * Decode intra macroblock header and return CBP in case of success, -1 otherwise.
  306. */
  307. static int rv34_decode_intra_mb_header(RV34DecContext *r, int8_t *intra_types)
  308. {
  309. MpegEncContext *s = &r->s;
  310. GetBitContext *gb = &s->gb;
  311. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  312. int t;
  313. r->is16 = get_bits1(gb);
  314. if(r->is16){
  315. s->current_picture_ptr->mb_type[mb_pos] = MB_TYPE_INTRA16x16;
  316. r->block_type = RV34_MB_TYPE_INTRA16x16;
  317. t = get_bits(gb, 2);
  318. fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
  319. r->luma_vlc = 2;
  320. }else{
  321. if(!r->rv30){
  322. if(!get_bits1(gb))
  323. av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
  324. }
  325. s->current_picture_ptr->mb_type[mb_pos] = MB_TYPE_INTRA;
  326. r->block_type = RV34_MB_TYPE_INTRA;
  327. if(r->decode_intra_types(r, gb, intra_types) < 0)
  328. return -1;
  329. r->luma_vlc = 1;
  330. }
  331. r->chroma_vlc = 0;
  332. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  333. return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
  334. }
  335. /**
  336. * Decode inter macroblock header and return CBP in case of success, -1 otherwise.
  337. */
  338. static int rv34_decode_inter_mb_header(RV34DecContext *r, int8_t *intra_types)
  339. {
  340. MpegEncContext *s = &r->s;
  341. GetBitContext *gb = &s->gb;
  342. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  343. int i, t;
  344. r->block_type = r->decode_mb_info(r);
  345. if(r->block_type == -1)
  346. return -1;
  347. s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
  348. r->mb_type[mb_pos] = r->block_type;
  349. if(r->block_type == RV34_MB_SKIP){
  350. if(s->pict_type == AV_PICTURE_TYPE_P)
  351. r->mb_type[mb_pos] = RV34_MB_P_16x16;
  352. if(s->pict_type == AV_PICTURE_TYPE_B)
  353. r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
  354. }
  355. r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
  356. rv34_decode_mv(r, r->block_type);
  357. if(r->block_type == RV34_MB_SKIP){
  358. fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
  359. return 0;
  360. }
  361. r->chroma_vlc = 1;
  362. r->luma_vlc = 0;
  363. if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
  364. if(r->is16){
  365. t = get_bits(gb, 2);
  366. fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
  367. r->luma_vlc = 2;
  368. }else{
  369. if(r->decode_intra_types(r, gb, intra_types) < 0)
  370. return -1;
  371. r->luma_vlc = 1;
  372. }
  373. r->chroma_vlc = 0;
  374. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  375. }else{
  376. for(i = 0; i < 16; i++)
  377. intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
  378. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  379. if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
  380. r->is16 = 1;
  381. r->chroma_vlc = 1;
  382. r->luma_vlc = 2;
  383. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  384. }
  385. }
  386. return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
  387. }
  388. /** @} */ //bitstream functions
  389. /**
  390. * @name motion vector related code (prediction, reconstruction, motion compensation)
  391. * @{
  392. */
  393. /** macroblock partition width in 8x8 blocks */
  394. static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
  395. /** macroblock partition height in 8x8 blocks */
  396. static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
  397. /** availability index for subblocks */
  398. static const uint8_t avail_indexes[4] = { 6, 7, 10, 11 };
  399. /**
  400. * motion vector prediction
  401. *
  402. * Motion prediction performed for the block by using median prediction of
  403. * motion vectors from the left, top and right top blocks but in corner cases
  404. * some other vectors may be used instead.
  405. */
  406. static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
  407. {
  408. MpegEncContext *s = &r->s;
  409. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  410. int A[2] = {0}, B[2], C[2];
  411. int i, j;
  412. int mx, my;
  413. int* avail = r->avail_cache + avail_indexes[subblock_no];
  414. int c_off = part_sizes_w[block_type];
  415. mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
  416. if(subblock_no == 3)
  417. c_off = -1;
  418. if(avail[-1]){
  419. A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
  420. A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
  421. }
  422. if(avail[-4]){
  423. B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
  424. B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
  425. }else{
  426. B[0] = A[0];
  427. B[1] = A[1];
  428. }
  429. if(!avail[c_off-4]){
  430. if(avail[-4] && (avail[-1] || r->rv30)){
  431. C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
  432. C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
  433. }else{
  434. C[0] = A[0];
  435. C[1] = A[1];
  436. }
  437. }else{
  438. C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
  439. C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
  440. }
  441. mx = mid_pred(A[0], B[0], C[0]);
  442. my = mid_pred(A[1], B[1], C[1]);
  443. mx += r->dmv[dmv_no][0];
  444. my += r->dmv[dmv_no][1];
  445. for(j = 0; j < part_sizes_h[block_type]; j++){
  446. for(i = 0; i < part_sizes_w[block_type]; i++){
  447. s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
  448. s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
  449. }
  450. }
  451. }
  452. #define GET_PTS_DIFF(a, b) ((a - b + 8192) & 0x1FFF)
  453. /**
  454. * Calculate motion vector component that should be added for direct blocks.
  455. */
  456. static int calc_add_mv(RV34DecContext *r, int dir, int val)
  457. {
  458. int mul = dir ? -r->mv_weight2 : r->mv_weight1;
  459. return (val * mul + 0x2000) >> 14;
  460. }
  461. /**
  462. * Predict motion vector for B-frame macroblock.
  463. */
  464. static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
  465. int A_avail, int B_avail, int C_avail,
  466. int *mx, int *my)
  467. {
  468. if(A_avail + B_avail + C_avail != 3){
  469. *mx = A[0] + B[0] + C[0];
  470. *my = A[1] + B[1] + C[1];
  471. if(A_avail + B_avail + C_avail == 2){
  472. *mx /= 2;
  473. *my /= 2;
  474. }
  475. }else{
  476. *mx = mid_pred(A[0], B[0], C[0]);
  477. *my = mid_pred(A[1], B[1], C[1]);
  478. }
  479. }
  480. /**
  481. * motion vector prediction for B-frames
  482. */
  483. static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
  484. {
  485. MpegEncContext *s = &r->s;
  486. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  487. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  488. int A[2] = { 0 }, B[2] = { 0 }, C[2] = { 0 };
  489. int has_A = 0, has_B = 0, has_C = 0;
  490. int mx, my;
  491. int i, j;
  492. Picture *cur_pic = s->current_picture_ptr;
  493. const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
  494. int type = cur_pic->mb_type[mb_pos];
  495. if((r->avail_cache[6-1] & type) & mask){
  496. A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
  497. A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
  498. has_A = 1;
  499. }
  500. if((r->avail_cache[6-4] & type) & mask){
  501. B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
  502. B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
  503. has_B = 1;
  504. }
  505. if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
  506. C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
  507. C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
  508. has_C = 1;
  509. }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
  510. C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
  511. C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
  512. has_C = 1;
  513. }
  514. rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
  515. mx += r->dmv[dir][0];
  516. my += r->dmv[dir][1];
  517. for(j = 0; j < 2; j++){
  518. for(i = 0; i < 2; i++){
  519. cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
  520. cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
  521. }
  522. }
  523. if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
  524. ZERO8x2(cur_pic->motion_val[!dir][mv_pos], s->b8_stride);
  525. }
  526. }
  527. /**
  528. * motion vector prediction - RV3 version
  529. */
  530. static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
  531. {
  532. MpegEncContext *s = &r->s;
  533. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  534. int A[2] = {0}, B[2], C[2];
  535. int i, j, k;
  536. int mx, my;
  537. int* avail = r->avail_cache + avail_indexes[0];
  538. if(avail[-1]){
  539. A[0] = s->current_picture_ptr->motion_val[0][mv_pos - 1][0];
  540. A[1] = s->current_picture_ptr->motion_val[0][mv_pos - 1][1];
  541. }
  542. if(avail[-4]){
  543. B[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride][0];
  544. B[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride][1];
  545. }else{
  546. B[0] = A[0];
  547. B[1] = A[1];
  548. }
  549. if(!avail[-4 + 2]){
  550. if(avail[-4] && (avail[-1])){
  551. C[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride - 1][0];
  552. C[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride - 1][1];
  553. }else{
  554. C[0] = A[0];
  555. C[1] = A[1];
  556. }
  557. }else{
  558. C[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride + 2][0];
  559. C[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride + 2][1];
  560. }
  561. mx = mid_pred(A[0], B[0], C[0]);
  562. my = mid_pred(A[1], B[1], C[1]);
  563. mx += r->dmv[0][0];
  564. my += r->dmv[0][1];
  565. for(j = 0; j < 2; j++){
  566. for(i = 0; i < 2; i++){
  567. for(k = 0; k < 2; k++){
  568. s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
  569. s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
  570. }
  571. }
  572. }
  573. }
  574. static const int chroma_coeffs[3] = { 0, 3, 5 };
  575. /**
  576. * generic motion compensation function
  577. *
  578. * @param r decoder context
  579. * @param block_type type of the current block
  580. * @param xoff horizontal offset from the start of the current block
  581. * @param yoff vertical offset from the start of the current block
  582. * @param mv_off offset to the motion vector information
  583. * @param width width of the current partition in 8x8 blocks
  584. * @param height height of the current partition in 8x8 blocks
  585. * @param dir motion compensation direction (i.e. from the last or the next reference frame)
  586. * @param thirdpel motion vectors are specified in 1/3 of pixel
  587. * @param qpel_mc a set of functions used to perform luma motion compensation
  588. * @param chroma_mc a set of functions used to perform chroma motion compensation
  589. */
  590. static inline void rv34_mc(RV34DecContext *r, const int block_type,
  591. const int xoff, const int yoff, int mv_off,
  592. const int width, const int height, int dir,
  593. const int thirdpel, int weighted,
  594. qpel_mc_func (*qpel_mc)[16],
  595. h264_chroma_mc_func (*chroma_mc))
  596. {
  597. MpegEncContext *s = &r->s;
  598. uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
  599. int dxy, mx, my, umx, umy, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  600. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
  601. int is16x16 = 1;
  602. if(thirdpel){
  603. int chroma_mx, chroma_my;
  604. mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
  605. my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
  606. lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
  607. ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
  608. chroma_mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
  609. chroma_my = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
  610. umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
  611. umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
  612. uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
  613. uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
  614. }else{
  615. int cx, cy;
  616. mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
  617. my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
  618. lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
  619. ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
  620. cx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
  621. cy = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
  622. umx = cx >> 2;
  623. umy = cy >> 2;
  624. uvmx = (cx & 3) << 1;
  625. uvmy = (cy & 3) << 1;
  626. //due to some flaw RV40 uses the same MC compensation routine for H2V2 and H3V3
  627. if(uvmx == 6 && uvmy == 6)
  628. uvmx = uvmy = 4;
  629. }
  630. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  631. /* wait for the referenced mb row to be finished */
  632. int mb_row = s->mb_y + ((yoff + my + 5 + 8 * height) >> 4);
  633. ThreadFrame *f = dir ? &s->next_picture_ptr->tf : &s->last_picture_ptr->tf;
  634. ff_thread_await_progress(f, mb_row, 0);
  635. }
  636. dxy = ly*4 + lx;
  637. srcY = dir ? s->next_picture_ptr->f->data[0] : s->last_picture_ptr->f->data[0];
  638. srcU = dir ? s->next_picture_ptr->f->data[1] : s->last_picture_ptr->f->data[1];
  639. srcV = dir ? s->next_picture_ptr->f->data[2] : s->last_picture_ptr->f->data[2];
  640. src_x = s->mb_x * 16 + xoff + mx;
  641. src_y = s->mb_y * 16 + yoff + my;
  642. uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
  643. uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
  644. srcY += src_y * s->linesize + src_x;
  645. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  646. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  647. if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
  648. (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
  649. (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
  650. uint8_t *uvbuf = s->edge_emu_buffer + 22 * s->linesize;
  651. srcY -= 2 + 2*s->linesize;
  652. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
  653. s->linesize, s->linesize,
  654. (width << 3) + 6, (height << 3) + 6,
  655. src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
  656. srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
  657. s->vdsp.emulated_edge_mc(uvbuf, srcU,
  658. s->uvlinesize,s->uvlinesize,
  659. (width << 2) + 1, (height << 2) + 1,
  660. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  661. s->vdsp.emulated_edge_mc(uvbuf + 16, srcV,
  662. s->uvlinesize, s->uvlinesize,
  663. (width << 2) + 1, (height << 2) + 1,
  664. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  665. srcU = uvbuf;
  666. srcV = uvbuf + 16;
  667. }
  668. if(!weighted){
  669. Y = s->dest[0] + xoff + yoff *s->linesize;
  670. U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  671. V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  672. }else{
  673. Y = r->tmp_b_block_y [dir] + xoff + yoff *s->linesize;
  674. U = r->tmp_b_block_uv[dir*2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  675. V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  676. }
  677. if(block_type == RV34_MB_P_16x8){
  678. qpel_mc[1][dxy](Y, srcY, s->linesize);
  679. Y += 8;
  680. srcY += 8;
  681. }else if(block_type == RV34_MB_P_8x16){
  682. qpel_mc[1][dxy](Y, srcY, s->linesize);
  683. Y += 8 * s->linesize;
  684. srcY += 8 * s->linesize;
  685. }
  686. is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
  687. qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
  688. chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
  689. chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
  690. }
  691. static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
  692. const int xoff, const int yoff, int mv_off,
  693. const int width, const int height, int dir)
  694. {
  695. rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
  696. r->rdsp.put_pixels_tab,
  697. r->rdsp.put_chroma_pixels_tab);
  698. }
  699. static void rv4_weight(RV34DecContext *r)
  700. {
  701. r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][0](r->s.dest[0],
  702. r->tmp_b_block_y[0],
  703. r->tmp_b_block_y[1],
  704. r->weight1,
  705. r->weight2,
  706. r->s.linesize);
  707. r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[1],
  708. r->tmp_b_block_uv[0],
  709. r->tmp_b_block_uv[2],
  710. r->weight1,
  711. r->weight2,
  712. r->s.uvlinesize);
  713. r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[2],
  714. r->tmp_b_block_uv[1],
  715. r->tmp_b_block_uv[3],
  716. r->weight1,
  717. r->weight2,
  718. r->s.uvlinesize);
  719. }
  720. static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
  721. {
  722. int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
  723. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
  724. r->rdsp.put_pixels_tab,
  725. r->rdsp.put_chroma_pixels_tab);
  726. if(!weighted){
  727. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
  728. r->rdsp.avg_pixels_tab,
  729. r->rdsp.avg_chroma_pixels_tab);
  730. }else{
  731. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
  732. r->rdsp.put_pixels_tab,
  733. r->rdsp.put_chroma_pixels_tab);
  734. rv4_weight(r);
  735. }
  736. }
  737. static void rv34_mc_2mv_skip(RV34DecContext *r)
  738. {
  739. int i, j;
  740. int weighted = !r->rv30 && r->weight1 != 8192;
  741. for(j = 0; j < 2; j++)
  742. for(i = 0; i < 2; i++){
  743. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
  744. weighted,
  745. r->rdsp.put_pixels_tab,
  746. r->rdsp.put_chroma_pixels_tab);
  747. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
  748. weighted,
  749. weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
  750. weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
  751. }
  752. if(weighted)
  753. rv4_weight(r);
  754. }
  755. /** number of motion vectors in each macroblock type */
  756. static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
  757. /**
  758. * Decode motion vector differences
  759. * and perform motion vector reconstruction and motion compensation.
  760. */
  761. static int rv34_decode_mv(RV34DecContext *r, int block_type)
  762. {
  763. MpegEncContext *s = &r->s;
  764. GetBitContext *gb = &s->gb;
  765. int i, j, k, l;
  766. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  767. int next_bt;
  768. memset(r->dmv, 0, sizeof(r->dmv));
  769. for(i = 0; i < num_mvs[block_type]; i++){
  770. r->dmv[i][0] = svq3_get_se_golomb(gb);
  771. r->dmv[i][1] = svq3_get_se_golomb(gb);
  772. }
  773. switch(block_type){
  774. case RV34_MB_TYPE_INTRA:
  775. case RV34_MB_TYPE_INTRA16x16:
  776. ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  777. return 0;
  778. case RV34_MB_SKIP:
  779. if(s->pict_type == AV_PICTURE_TYPE_P){
  780. ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  781. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  782. break;
  783. }
  784. case RV34_MB_B_DIRECT:
  785. //surprisingly, it uses motion scheme from next reference frame
  786. /* wait for the current mb row to be finished */
  787. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  788. ff_thread_await_progress(&s->next_picture_ptr->tf, FFMAX(0, s->mb_y-1), 0);
  789. next_bt = s->next_picture_ptr->mb_type[s->mb_x + s->mb_y * s->mb_stride];
  790. if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
  791. ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  792. ZERO8x2(s->current_picture_ptr->motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  793. }else
  794. for(j = 0; j < 2; j++)
  795. for(i = 0; i < 2; i++)
  796. for(k = 0; k < 2; k++)
  797. for(l = 0; l < 2; l++)
  798. 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]);
  799. if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
  800. rv34_mc_2mv(r, block_type);
  801. else
  802. rv34_mc_2mv_skip(r);
  803. ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  804. break;
  805. case RV34_MB_P_16x16:
  806. case RV34_MB_P_MIX16x16:
  807. rv34_pred_mv(r, block_type, 0, 0);
  808. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  809. break;
  810. case RV34_MB_B_FORWARD:
  811. case RV34_MB_B_BACKWARD:
  812. r->dmv[1][0] = r->dmv[0][0];
  813. r->dmv[1][1] = r->dmv[0][1];
  814. if(r->rv30)
  815. rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
  816. else
  817. rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
  818. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
  819. break;
  820. case RV34_MB_P_16x8:
  821. case RV34_MB_P_8x16:
  822. rv34_pred_mv(r, block_type, 0, 0);
  823. rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
  824. if(block_type == RV34_MB_P_16x8){
  825. rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
  826. rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
  827. }
  828. if(block_type == RV34_MB_P_8x16){
  829. rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
  830. rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
  831. }
  832. break;
  833. case RV34_MB_B_BIDIR:
  834. rv34_pred_mv_b (r, block_type, 0);
  835. rv34_pred_mv_b (r, block_type, 1);
  836. rv34_mc_2mv (r, block_type);
  837. break;
  838. case RV34_MB_P_8x8:
  839. for(i=0;i< 4;i++){
  840. rv34_pred_mv(r, block_type, i, i);
  841. rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
  842. }
  843. break;
  844. }
  845. return 0;
  846. }
  847. /** @} */ // mv group
  848. /**
  849. * @name Macroblock reconstruction functions
  850. * @{
  851. */
  852. /** mapping of RV30/40 intra prediction types to standard H.264 types */
  853. static const int ittrans[9] = {
  854. DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
  855. VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
  856. };
  857. /** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
  858. static const int ittrans16[4] = {
  859. DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
  860. };
  861. /**
  862. * Perform 4x4 intra prediction.
  863. */
  864. static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
  865. {
  866. uint8_t *prev = dst - stride + 4;
  867. uint32_t topleft;
  868. if(!up && !left)
  869. itype = DC_128_PRED;
  870. else if(!up){
  871. if(itype == VERT_PRED) itype = HOR_PRED;
  872. if(itype == DC_PRED) itype = LEFT_DC_PRED;
  873. }else if(!left){
  874. if(itype == HOR_PRED) itype = VERT_PRED;
  875. if(itype == DC_PRED) itype = TOP_DC_PRED;
  876. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  877. }
  878. if(!down){
  879. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  880. if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
  881. if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
  882. }
  883. if(!right && up){
  884. topleft = dst[-stride + 3] * 0x01010101u;
  885. prev = (uint8_t*)&topleft;
  886. }
  887. r->h.pred4x4[itype](dst, prev, stride);
  888. }
  889. static inline int adjust_pred16(int itype, int up, int left)
  890. {
  891. if(!up && !left)
  892. itype = DC_128_PRED8x8;
  893. else if(!up){
  894. if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
  895. if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
  896. if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
  897. }else if(!left){
  898. if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
  899. if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
  900. if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
  901. }
  902. return itype;
  903. }
  904. static inline void rv34_process_block(RV34DecContext *r,
  905. uint8_t *pdst, int stride,
  906. int fc, int sc, int q_dc, int q_ac)
  907. {
  908. MpegEncContext *s = &r->s;
  909. int16_t *ptr = s->block[0];
  910. int has_ac = rv34_decode_block(ptr, &s->gb, r->cur_vlcs,
  911. fc, sc, q_dc, q_ac, q_ac);
  912. if(has_ac){
  913. r->rdsp.rv34_idct_add(pdst, stride, ptr);
  914. }else{
  915. r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
  916. ptr[0] = 0;
  917. }
  918. }
  919. static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
  920. {
  921. LOCAL_ALIGNED_16(int16_t, block16, [16]);
  922. MpegEncContext *s = &r->s;
  923. GetBitContext *gb = &s->gb;
  924. int q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
  925. q_ac = rv34_qscale_tab[s->qscale];
  926. uint8_t *dst = s->dest[0];
  927. int16_t *ptr = s->block[0];
  928. int i, j, itype, has_ac;
  929. memset(block16, 0, 16 * sizeof(*block16));
  930. has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
  931. if(has_ac)
  932. r->rdsp.rv34_inv_transform(block16);
  933. else
  934. r->rdsp.rv34_inv_transform_dc(block16);
  935. itype = ittrans16[intra_types[0]];
  936. itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
  937. r->h.pred16x16[itype](dst, s->linesize);
  938. for(j = 0; j < 4; j++){
  939. for(i = 0; i < 4; i++, cbp >>= 1){
  940. int dc = block16[i + j*4];
  941. if(cbp & 1){
  942. has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
  943. }else
  944. has_ac = 0;
  945. if(has_ac){
  946. ptr[0] = dc;
  947. r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
  948. }else
  949. r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
  950. }
  951. dst += 4*s->linesize;
  952. }
  953. itype = ittrans16[intra_types[0]];
  954. if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
  955. itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
  956. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  957. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  958. for(j = 1; j < 3; j++){
  959. dst = s->dest[j];
  960. r->h.pred8x8[itype](dst, s->uvlinesize);
  961. for(i = 0; i < 4; i++, cbp >>= 1){
  962. uint8_t *pdst;
  963. if(!(cbp & 1)) continue;
  964. pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
  965. rv34_process_block(r, pdst, s->uvlinesize,
  966. r->chroma_vlc, 1, q_dc, q_ac);
  967. }
  968. }
  969. }
  970. static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
  971. {
  972. MpegEncContext *s = &r->s;
  973. uint8_t *dst = s->dest[0];
  974. int avail[6*8] = {0};
  975. int i, j, k;
  976. int idx, q_ac, q_dc;
  977. // Set neighbour information.
  978. if(r->avail_cache[1])
  979. avail[0] = 1;
  980. if(r->avail_cache[2])
  981. avail[1] = avail[2] = 1;
  982. if(r->avail_cache[3])
  983. avail[3] = avail[4] = 1;
  984. if(r->avail_cache[4])
  985. avail[5] = 1;
  986. if(r->avail_cache[5])
  987. avail[8] = avail[16] = 1;
  988. if(r->avail_cache[9])
  989. avail[24] = avail[32] = 1;
  990. q_ac = rv34_qscale_tab[s->qscale];
  991. for(j = 0; j < 4; j++){
  992. idx = 9 + j*8;
  993. for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
  994. rv34_pred_4x4_block(r, dst, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
  995. avail[idx] = 1;
  996. if(!(cbp & 1)) continue;
  997. rv34_process_block(r, dst, s->linesize,
  998. r->luma_vlc, 0, q_ac, q_ac);
  999. }
  1000. dst += s->linesize * 4 - 4*4;
  1001. intra_types += r->intra_types_stride;
  1002. }
  1003. intra_types -= r->intra_types_stride * 4;
  1004. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  1005. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  1006. for(k = 0; k < 2; k++){
  1007. dst = s->dest[1+k];
  1008. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
  1009. for(j = 0; j < 2; j++){
  1010. int* acache = r->avail_cache + 6 + j*4;
  1011. for(i = 0; i < 2; i++, cbp >>= 1, acache++){
  1012. int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
  1013. rv34_pred_4x4_block(r, dst+4*i, s->uvlinesize, itype, acache[-4], acache[-1], !i && !j, acache[-3]);
  1014. acache[0] = 1;
  1015. if(!(cbp&1)) continue;
  1016. rv34_process_block(r, dst + 4*i, s->uvlinesize,
  1017. r->chroma_vlc, 1, q_dc, q_ac);
  1018. }
  1019. dst += 4*s->uvlinesize;
  1020. }
  1021. }
  1022. }
  1023. static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
  1024. {
  1025. int d;
  1026. d = motion_val[0][0] - motion_val[-step][0];
  1027. if(d < -3 || d > 3)
  1028. return 1;
  1029. d = motion_val[0][1] - motion_val[-step][1];
  1030. if(d < -3 || d > 3)
  1031. return 1;
  1032. return 0;
  1033. }
  1034. static int rv34_set_deblock_coef(RV34DecContext *r)
  1035. {
  1036. MpegEncContext *s = &r->s;
  1037. int hmvmask = 0, vmvmask = 0, i, j;
  1038. int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  1039. int16_t (*motion_val)[2] = &s->current_picture_ptr->motion_val[0][midx];
  1040. for(j = 0; j < 16; j += 8){
  1041. for(i = 0; i < 2; i++){
  1042. if(is_mv_diff_gt_3(motion_val + i, 1))
  1043. vmvmask |= 0x11 << (j + i*2);
  1044. if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
  1045. hmvmask |= 0x03 << (j + i*2);
  1046. }
  1047. motion_val += s->b8_stride;
  1048. }
  1049. if(s->first_slice_line)
  1050. hmvmask &= ~0x000F;
  1051. if(!s->mb_x)
  1052. vmvmask &= ~0x1111;
  1053. if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
  1054. vmvmask |= (vmvmask & 0x4444) >> 1;
  1055. hmvmask |= (hmvmask & 0x0F00) >> 4;
  1056. if(s->mb_x)
  1057. r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
  1058. if(!s->first_slice_line)
  1059. r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
  1060. }
  1061. return hmvmask | vmvmask;
  1062. }
  1063. static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
  1064. {
  1065. MpegEncContext *s = &r->s;
  1066. GetBitContext *gb = &s->gb;
  1067. uint8_t *dst = s->dest[0];
  1068. int16_t *ptr = s->block[0];
  1069. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1070. int cbp, cbp2;
  1071. int q_dc, q_ac, has_ac;
  1072. int i, j;
  1073. int dist;
  1074. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1075. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1076. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
  1077. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1078. if(s->mb_x && dist)
  1079. r->avail_cache[5] =
  1080. r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
  1081. if(dist >= s->mb_width)
  1082. r->avail_cache[2] =
  1083. r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
  1084. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1085. r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
  1086. if(s->mb_x && dist > s->mb_width)
  1087. r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
  1088. s->qscale = r->si.quant;
  1089. cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
  1090. r->cbp_luma [mb_pos] = cbp;
  1091. r->cbp_chroma[mb_pos] = cbp >> 16;
  1092. r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
  1093. s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
  1094. if(cbp == -1)
  1095. return -1;
  1096. if (IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
  1097. if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
  1098. else rv34_output_intra(r, intra_types, cbp);
  1099. return 0;
  1100. }
  1101. if(r->is16){
  1102. // Only for RV34_MB_P_MIX16x16
  1103. LOCAL_ALIGNED_16(int16_t, block16, [16]);
  1104. memset(block16, 0, 16 * sizeof(*block16));
  1105. q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
  1106. q_ac = rv34_qscale_tab[s->qscale];
  1107. if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac))
  1108. r->rdsp.rv34_inv_transform(block16);
  1109. else
  1110. r->rdsp.rv34_inv_transform_dc(block16);
  1111. q_ac = rv34_qscale_tab[s->qscale];
  1112. for(j = 0; j < 4; j++){
  1113. for(i = 0; i < 4; i++, cbp >>= 1){
  1114. int dc = block16[i + j*4];
  1115. if(cbp & 1){
  1116. has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
  1117. }else
  1118. has_ac = 0;
  1119. if(has_ac){
  1120. ptr[0] = dc;
  1121. r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
  1122. }else
  1123. r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
  1124. }
  1125. dst += 4*s->linesize;
  1126. }
  1127. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  1128. }else{
  1129. q_ac = rv34_qscale_tab[s->qscale];
  1130. for(j = 0; j < 4; j++){
  1131. for(i = 0; i < 4; i++, cbp >>= 1){
  1132. if(!(cbp & 1)) continue;
  1133. rv34_process_block(r, dst + 4*i, s->linesize,
  1134. r->luma_vlc, 0, q_ac, q_ac);
  1135. }
  1136. dst += 4*s->linesize;
  1137. }
  1138. }
  1139. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  1140. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  1141. for(j = 1; j < 3; j++){
  1142. dst = s->dest[j];
  1143. for(i = 0; i < 4; i++, cbp >>= 1){
  1144. uint8_t *pdst;
  1145. if(!(cbp & 1)) continue;
  1146. pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
  1147. rv34_process_block(r, pdst, s->uvlinesize,
  1148. r->chroma_vlc, 1, q_dc, q_ac);
  1149. }
  1150. }
  1151. return 0;
  1152. }
  1153. static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
  1154. {
  1155. MpegEncContext *s = &r->s;
  1156. int cbp, dist;
  1157. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1158. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1159. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1160. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
  1161. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1162. if(s->mb_x && dist)
  1163. r->avail_cache[5] =
  1164. r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
  1165. if(dist >= s->mb_width)
  1166. r->avail_cache[2] =
  1167. r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
  1168. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1169. r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
  1170. if(s->mb_x && dist > s->mb_width)
  1171. r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
  1172. s->qscale = r->si.quant;
  1173. cbp = rv34_decode_intra_mb_header(r, intra_types);
  1174. r->cbp_luma [mb_pos] = cbp;
  1175. r->cbp_chroma[mb_pos] = cbp >> 16;
  1176. r->deblock_coefs[mb_pos] = 0xFFFF;
  1177. s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
  1178. if(cbp == -1)
  1179. return -1;
  1180. if(r->is16){
  1181. rv34_output_i16x16(r, intra_types, cbp);
  1182. return 0;
  1183. }
  1184. rv34_output_intra(r, intra_types, cbp);
  1185. return 0;
  1186. }
  1187. static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
  1188. {
  1189. int bits;
  1190. if(s->mb_y >= s->mb_height)
  1191. return 1;
  1192. if(!s->mb_num_left)
  1193. return 1;
  1194. if(r->s.mb_skip_run > 1)
  1195. return 0;
  1196. bits = get_bits_left(&s->gb);
  1197. if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits)))
  1198. return 1;
  1199. return 0;
  1200. }
  1201. static void rv34_decoder_free(RV34DecContext *r)
  1202. {
  1203. av_freep(&r->intra_types_hist);
  1204. r->intra_types = NULL;
  1205. av_freep(&r->tmp_b_block_base);
  1206. av_freep(&r->mb_type);
  1207. av_freep(&r->cbp_luma);
  1208. av_freep(&r->cbp_chroma);
  1209. av_freep(&r->deblock_coefs);
  1210. }
  1211. static int rv34_decoder_alloc(RV34DecContext *r)
  1212. {
  1213. r->intra_types_stride = r->s.mb_width * 4 + 4;
  1214. r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height *
  1215. sizeof(*r->cbp_chroma));
  1216. r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height *
  1217. sizeof(*r->cbp_luma));
  1218. r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height *
  1219. sizeof(*r->deblock_coefs));
  1220. r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
  1221. sizeof(*r->intra_types_hist));
  1222. r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height *
  1223. sizeof(*r->mb_type));
  1224. if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs &&
  1225. r->intra_types_hist && r->mb_type)) {
  1226. rv34_decoder_free(r);
  1227. return AVERROR(ENOMEM);
  1228. }
  1229. r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
  1230. return 0;
  1231. }
  1232. static int rv34_decoder_realloc(RV34DecContext *r)
  1233. {
  1234. rv34_decoder_free(r);
  1235. return rv34_decoder_alloc(r);
  1236. }
  1237. static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
  1238. {
  1239. MpegEncContext *s = &r->s;
  1240. GetBitContext *gb = &s->gb;
  1241. int mb_pos, slice_type;
  1242. int res;
  1243. init_get_bits(&r->s.gb, buf, buf_size*8);
  1244. res = r->parse_slice_header(r, gb, &r->si);
  1245. if(res < 0){
  1246. av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
  1247. return -1;
  1248. }
  1249. slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
  1250. if (slice_type != s->pict_type) {
  1251. av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
  1252. return AVERROR_INVALIDDATA;
  1253. }
  1254. r->si.end = end;
  1255. s->qscale = r->si.quant;
  1256. s->mb_num_left = r->si.end - r->si.start;
  1257. r->s.mb_skip_run = 0;
  1258. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  1259. if(r->si.start != mb_pos){
  1260. av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
  1261. s->mb_x = r->si.start % s->mb_width;
  1262. s->mb_y = r->si.start / s->mb_width;
  1263. }
  1264. memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1265. s->first_slice_line = 1;
  1266. s->resync_mb_x = s->mb_x;
  1267. s->resync_mb_y = s->mb_y;
  1268. ff_init_block_index(s);
  1269. while(!check_slice_end(r, s)) {
  1270. ff_update_block_index(s);
  1271. if(r->si.type)
  1272. res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
  1273. else
  1274. res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
  1275. if(res < 0){
  1276. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
  1277. return -1;
  1278. }
  1279. if (++s->mb_x == s->mb_width) {
  1280. s->mb_x = 0;
  1281. s->mb_y++;
  1282. ff_init_block_index(s);
  1283. memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
  1284. memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
  1285. if(r->loop_filter && s->mb_y >= 2)
  1286. r->loop_filter(r, s->mb_y - 2);
  1287. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  1288. ff_thread_report_progress(&s->current_picture_ptr->tf,
  1289. s->mb_y - 2, 0);
  1290. }
  1291. if(s->mb_x == s->resync_mb_x)
  1292. s->first_slice_line=0;
  1293. s->mb_num_left--;
  1294. }
  1295. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  1296. return s->mb_y == s->mb_height;
  1297. }
  1298. /** @} */ // recons group end
  1299. /**
  1300. * Initialize decoder.
  1301. */
  1302. av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
  1303. {
  1304. RV34DecContext *r = avctx->priv_data;
  1305. MpegEncContext *s = &r->s;
  1306. int ret;
  1307. ff_MPV_decode_defaults(s);
  1308. s->avctx = avctx;
  1309. s->out_format = FMT_H263;
  1310. s->codec_id = avctx->codec_id;
  1311. s->width = avctx->width;
  1312. s->height = avctx->height;
  1313. r->s.avctx = avctx;
  1314. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  1315. avctx->has_b_frames = 1;
  1316. s->low_delay = 0;
  1317. if ((ret = ff_MPV_common_init(s)) < 0)
  1318. return ret;
  1319. ff_h264_pred_init(&r->h, AV_CODEC_ID_RV40, 8, 1);
  1320. #if CONFIG_RV30_DECODER
  1321. if (avctx->codec_id == AV_CODEC_ID_RV30)
  1322. ff_rv30dsp_init(&r->rdsp);
  1323. #endif
  1324. #if CONFIG_RV40_DECODER
  1325. if (avctx->codec_id == AV_CODEC_ID_RV40)
  1326. ff_rv40dsp_init(&r->rdsp);
  1327. #endif
  1328. if ((ret = rv34_decoder_alloc(r)) < 0) {
  1329. ff_MPV_common_end(&r->s);
  1330. return ret;
  1331. }
  1332. if(!intra_vlcs[0].cbppattern[0].bits)
  1333. rv34_init_tables();
  1334. avctx->internal->allocate_progress = 1;
  1335. return 0;
  1336. }
  1337. int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
  1338. {
  1339. int err;
  1340. RV34DecContext *r = avctx->priv_data;
  1341. r->s.avctx = avctx;
  1342. if (avctx->internal->is_copy) {
  1343. r->tmp_b_block_base = NULL;
  1344. if ((err = ff_MPV_common_init(&r->s)) < 0)
  1345. return err;
  1346. if ((err = rv34_decoder_alloc(r)) < 0) {
  1347. ff_MPV_common_end(&r->s);
  1348. return err;
  1349. }
  1350. }
  1351. return 0;
  1352. }
  1353. int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1354. {
  1355. RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
  1356. MpegEncContext * const s = &r->s, * const s1 = &r1->s;
  1357. int err;
  1358. if (dst == src || !s1->context_initialized)
  1359. return 0;
  1360. if (s->height != s1->height || s->width != s1->width) {
  1361. s->height = s1->height;
  1362. s->width = s1->width;
  1363. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  1364. return err;
  1365. if ((err = rv34_decoder_realloc(r)) < 0)
  1366. return err;
  1367. }
  1368. if ((err = ff_mpeg_update_thread_context(dst, src)))
  1369. return err;
  1370. r->cur_pts = r1->cur_pts;
  1371. r->last_pts = r1->last_pts;
  1372. r->next_pts = r1->next_pts;
  1373. memset(&r->si, 0, sizeof(r->si));
  1374. return 0;
  1375. }
  1376. static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
  1377. {
  1378. if(avctx->slice_count) return avctx->slice_offset[n];
  1379. else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
  1380. }
  1381. static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
  1382. {
  1383. RV34DecContext *r = avctx->priv_data;
  1384. MpegEncContext *s = &r->s;
  1385. int got_picture = 0, ret;
  1386. ff_er_frame_end(&s->er);
  1387. ff_MPV_frame_end(s);
  1388. s->mb_num_left = 0;
  1389. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  1390. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1391. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1392. if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
  1393. return ret;
  1394. ff_print_debug_info(s, s->current_picture_ptr);
  1395. got_picture = 1;
  1396. } else if (s->last_picture_ptr != NULL) {
  1397. if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
  1398. return ret;
  1399. ff_print_debug_info(s, s->last_picture_ptr);
  1400. got_picture = 1;
  1401. }
  1402. return got_picture;
  1403. }
  1404. int ff_rv34_decode_frame(AVCodecContext *avctx,
  1405. void *data, int *got_picture_ptr,
  1406. AVPacket *avpkt)
  1407. {
  1408. const uint8_t *buf = avpkt->data;
  1409. int buf_size = avpkt->size;
  1410. RV34DecContext *r = avctx->priv_data;
  1411. MpegEncContext *s = &r->s;
  1412. AVFrame *pict = data;
  1413. SliceInfo si;
  1414. int i, ret;
  1415. int slice_count;
  1416. const uint8_t *slices_hdr = NULL;
  1417. int last = 0;
  1418. /* no supplementary picture */
  1419. if (buf_size == 0) {
  1420. /* special case for last picture */
  1421. if (s->low_delay==0 && s->next_picture_ptr) {
  1422. if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
  1423. return ret;
  1424. s->next_picture_ptr = NULL;
  1425. *got_picture_ptr = 1;
  1426. }
  1427. return 0;
  1428. }
  1429. if(!avctx->slice_count){
  1430. slice_count = (*buf++) + 1;
  1431. slices_hdr = buf + 4;
  1432. buf += 8 * slice_count;
  1433. buf_size -= 1 + 8 * slice_count;
  1434. }else
  1435. slice_count = avctx->slice_count;
  1436. //parse first slice header to check whether this frame can be decoded
  1437. if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
  1438. get_slice_offset(avctx, slices_hdr, 0) > buf_size){
  1439. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1440. return AVERROR_INVALIDDATA;
  1441. }
  1442. init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
  1443. if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
  1444. av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
  1445. return AVERROR_INVALIDDATA;
  1446. }
  1447. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->data[0]) &&
  1448. si.type == AV_PICTURE_TYPE_B) {
  1449. av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
  1450. "reference data.\n");
  1451. return AVERROR_INVALIDDATA;
  1452. }
  1453. if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
  1454. || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
  1455. || avctx->skip_frame >= AVDISCARD_ALL)
  1456. return avpkt->size;
  1457. /* first slice */
  1458. if (si.start == 0) {
  1459. if (s->mb_num_left > 0) {
  1460. av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.",
  1461. s->mb_num_left);
  1462. ff_er_frame_end(&s->er);
  1463. ff_MPV_frame_end(s);
  1464. }
  1465. if (s->width != si.width || s->height != si.height) {
  1466. int err;
  1467. av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
  1468. si.width, si.height);
  1469. s->width = si.width;
  1470. s->height = si.height;
  1471. err = ff_set_dimensions(s->avctx, s->width, s->height);
  1472. if (err < 0)
  1473. return err;
  1474. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  1475. return err;
  1476. if ((err = rv34_decoder_realloc(r)) < 0)
  1477. return err;
  1478. }
  1479. s->pict_type = si.type ? si.type : AV_PICTURE_TYPE_I;
  1480. if (ff_MPV_frame_start(s, s->avctx) < 0)
  1481. return -1;
  1482. ff_mpeg_er_frame_start(s);
  1483. if (!r->tmp_b_block_base) {
  1484. int i;
  1485. r->tmp_b_block_base = av_malloc(s->linesize * 48);
  1486. for (i = 0; i < 2; i++)
  1487. r->tmp_b_block_y[i] = r->tmp_b_block_base
  1488. + i * 16 * s->linesize;
  1489. for (i = 0; i < 4; i++)
  1490. r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
  1491. + (i >> 1) * 8 * s->uvlinesize
  1492. + (i & 1) * 16;
  1493. }
  1494. r->cur_pts = si.pts;
  1495. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1496. r->last_pts = r->next_pts;
  1497. r->next_pts = r->cur_pts;
  1498. } else {
  1499. int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
  1500. int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
  1501. int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
  1502. if(!refdist){
  1503. r->mv_weight1 = r->mv_weight2 = r->weight1 = r->weight2 = 8192;
  1504. r->scaled_weight = 0;
  1505. }else{
  1506. r->mv_weight1 = (dist0 << 14) / refdist;
  1507. r->mv_weight2 = (dist1 << 14) / refdist;
  1508. if((r->mv_weight1|r->mv_weight2) & 511){
  1509. r->weight1 = r->mv_weight1;
  1510. r->weight2 = r->mv_weight2;
  1511. r->scaled_weight = 0;
  1512. }else{
  1513. r->weight1 = r->mv_weight1 >> 9;
  1514. r->weight2 = r->mv_weight2 >> 9;
  1515. r->scaled_weight = 1;
  1516. }
  1517. }
  1518. }
  1519. s->mb_x = s->mb_y = 0;
  1520. ff_thread_finish_setup(s->avctx);
  1521. } else if (HAVE_THREADS &&
  1522. (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  1523. av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames in frame "
  1524. "multithreading mode (start MB is %d).\n", si.start);
  1525. return AVERROR_INVALIDDATA;
  1526. }
  1527. for(i = 0; i < slice_count; i++){
  1528. int offset = get_slice_offset(avctx, slices_hdr, i);
  1529. int size;
  1530. if(i+1 == slice_count)
  1531. size = buf_size - offset;
  1532. else
  1533. size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
  1534. if(offset < 0 || offset > buf_size){
  1535. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1536. break;
  1537. }
  1538. r->si.end = s->mb_width * s->mb_height;
  1539. s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
  1540. if(i+1 < slice_count){
  1541. if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||
  1542. get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {
  1543. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1544. break;
  1545. }
  1546. 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);
  1547. if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
  1548. if(i+2 < slice_count)
  1549. size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
  1550. else
  1551. size = buf_size - offset;
  1552. }else
  1553. r->si.end = si.start;
  1554. }
  1555. if (size < 0 || size > buf_size - offset) {
  1556. av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
  1557. break;
  1558. }
  1559. last = rv34_decode_slice(r, r->si.end, buf + offset, size);
  1560. if(last)
  1561. break;
  1562. }
  1563. if (s->current_picture_ptr) {
  1564. if (last) {
  1565. if(r->loop_filter)
  1566. r->loop_filter(r, s->mb_height - 1);
  1567. ret = finish_frame(avctx, pict);
  1568. if (ret < 0)
  1569. return ret;
  1570. *got_picture_ptr = ret;
  1571. } else if (HAVE_THREADS &&
  1572. (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  1573. av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n");
  1574. /* always mark the current frame as finished, frame-mt supports
  1575. * only complete frames */
  1576. ff_er_frame_end(&s->er);
  1577. ff_MPV_frame_end(s);
  1578. s->mb_num_left = 0;
  1579. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1580. return AVERROR_INVALIDDATA;
  1581. }
  1582. }
  1583. return avpkt->size;
  1584. }
  1585. av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
  1586. {
  1587. RV34DecContext *r = avctx->priv_data;
  1588. ff_MPV_common_end(&r->s);
  1589. rv34_decoder_free(r);
  1590. return 0;
  1591. }