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.

1802 lines
63KB

  1. /*
  2. * RV30/40 decoder common data
  3. * Copyright (c) 2007 Mike Melanson, Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RV30/40 decoder common data
  24. */
  25. #include "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. 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(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc, int q)
  192. {
  193. if(coef){
  194. if(coef == esc){
  195. coef = get_vlc2(gb, vlc->table, 9, 2);
  196. if(coef > 23){
  197. coef -= 23;
  198. coef = 22 + ((1 << coef) | get_bits(gb, coef));
  199. }
  200. coef += esc;
  201. }
  202. if(get_bits1(gb))
  203. coef = -coef;
  204. *dst = (coef*q + 8) >> 4;
  205. }
  206. }
  207. /**
  208. * Decode 2x2 subblock of coefficients.
  209. */
  210. static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q)
  211. {
  212. int flags = modulo_three_table[code];
  213. decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q);
  214. if(is_block2){
  215. decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
  216. decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q);
  217. }else{
  218. decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q);
  219. decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q);
  220. }
  221. decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
  222. }
  223. /**
  224. * Decode a single coefficient.
  225. */
  226. static inline void decode_subblock1(DCTELEM *dst, int code, GetBitContext *gb, VLC *vlc, int q)
  227. {
  228. int coeff = modulo_three_table[code] >> 6;
  229. decode_coeff(dst, coeff, 3, gb, vlc, q);
  230. }
  231. static inline void decode_subblock3(DCTELEM *dst, int code, 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(DCTELEM *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->f.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->f.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->f.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->f.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->f.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->f.motion_val[0][mv_pos-1][0];
  420. A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][1];
  421. }
  422. if(avail[-4]){
  423. B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][0];
  424. B[1] = s->current_picture_ptr->f.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->f.motion_val[0][mv_pos-s->b8_stride-1][0];
  432. C[1] = s->current_picture_ptr->f.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->f.motion_val[0][mv_pos-s->b8_stride+c_off][0];
  439. C[1] = s->current_picture_ptr->f.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->f.motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
  448. s->current_picture_ptr->f.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->f.mb_type[mb_pos];
  495. if((r->avail_cache[6-1] & type) & mask){
  496. A[0] = cur_pic->f.motion_val[dir][mv_pos - 1][0];
  497. A[1] = cur_pic->f.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->f.motion_val[dir][mv_pos - s->b8_stride][0];
  502. B[1] = cur_pic->f.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->f.motion_val[dir][mv_pos - s->b8_stride + 2][0];
  507. C[1] = cur_pic->f.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->f.motion_val[dir][mv_pos - s->b8_stride - 1][0];
  511. C[1] = cur_pic->f.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->f.motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
  520. cur_pic->f.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->f.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->f.motion_val[0][mv_pos - 1][0];
  540. A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][1];
  541. }
  542. if(avail[-4]){
  543. B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][0];
  544. B[1] = s->current_picture_ptr->f.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->f.motion_val[0][mv_pos - s->b8_stride - 1][0];
  552. C[1] = s->current_picture_ptr->f.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->f.motion_val[0][mv_pos - s->b8_stride + 2][0];
  559. C[1] = s->current_picture_ptr->f.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->f.motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
  569. s->current_picture_ptr->f.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->f.motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
  605. my = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
  606. lx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
  607. ly = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
  608. chroma_mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
  609. chroma_my = s->current_picture_ptr->f.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->f.motion_val[dir][mv_pos][0] >> 2;
  617. my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] >> 2;
  618. lx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] & 3;
  619. ly = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] & 3;
  620. cx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
  621. cy = s->current_picture_ptr->f.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. AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f;
  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->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+6, (height<<3)+6,
  653. src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
  654. srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
  655. s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,
  656. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  657. s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,
  658. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  659. srcU = uvbuf;
  660. srcV = uvbuf + 16;
  661. }
  662. if(!weighted){
  663. Y = s->dest[0] + xoff + yoff *s->linesize;
  664. U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  665. V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  666. }else{
  667. Y = r->tmp_b_block_y [dir] + xoff + yoff *s->linesize;
  668. U = r->tmp_b_block_uv[dir*2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  669. V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  670. }
  671. if(block_type == RV34_MB_P_16x8){
  672. qpel_mc[1][dxy](Y, srcY, s->linesize);
  673. Y += 8;
  674. srcY += 8;
  675. }else if(block_type == RV34_MB_P_8x16){
  676. qpel_mc[1][dxy](Y, srcY, s->linesize);
  677. Y += 8 * s->linesize;
  678. srcY += 8 * s->linesize;
  679. }
  680. is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
  681. qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
  682. chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
  683. chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
  684. }
  685. static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
  686. const int xoff, const int yoff, int mv_off,
  687. const int width, const int height, int dir)
  688. {
  689. rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
  690. r->rdsp.put_pixels_tab,
  691. r->rdsp.put_chroma_pixels_tab);
  692. }
  693. static void rv4_weight(RV34DecContext *r)
  694. {
  695. r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][0](r->s.dest[0],
  696. r->tmp_b_block_y[0],
  697. r->tmp_b_block_y[1],
  698. r->weight1,
  699. r->weight2,
  700. r->s.linesize);
  701. r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[1],
  702. r->tmp_b_block_uv[0],
  703. r->tmp_b_block_uv[2],
  704. r->weight1,
  705. r->weight2,
  706. r->s.uvlinesize);
  707. r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[2],
  708. r->tmp_b_block_uv[1],
  709. r->tmp_b_block_uv[3],
  710. r->weight1,
  711. r->weight2,
  712. r->s.uvlinesize);
  713. }
  714. static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
  715. {
  716. int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
  717. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
  718. r->rdsp.put_pixels_tab,
  719. r->rdsp.put_chroma_pixels_tab);
  720. if(!weighted){
  721. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
  722. r->rdsp.avg_pixels_tab,
  723. r->rdsp.avg_chroma_pixels_tab);
  724. }else{
  725. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
  726. r->rdsp.put_pixels_tab,
  727. r->rdsp.put_chroma_pixels_tab);
  728. rv4_weight(r);
  729. }
  730. }
  731. static void rv34_mc_2mv_skip(RV34DecContext *r)
  732. {
  733. int i, j;
  734. int weighted = !r->rv30 && r->weight1 != 8192;
  735. for(j = 0; j < 2; j++)
  736. for(i = 0; i < 2; i++){
  737. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
  738. weighted,
  739. r->rdsp.put_pixels_tab,
  740. r->rdsp.put_chroma_pixels_tab);
  741. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
  742. weighted,
  743. weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
  744. weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
  745. }
  746. if(weighted)
  747. rv4_weight(r);
  748. }
  749. /** number of motion vectors in each macroblock type */
  750. static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
  751. /**
  752. * Decode motion vector differences
  753. * and perform motion vector reconstruction and motion compensation.
  754. */
  755. static int rv34_decode_mv(RV34DecContext *r, int block_type)
  756. {
  757. MpegEncContext *s = &r->s;
  758. GetBitContext *gb = &s->gb;
  759. int i, j, k, l;
  760. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  761. int next_bt;
  762. memset(r->dmv, 0, sizeof(r->dmv));
  763. for(i = 0; i < num_mvs[block_type]; i++){
  764. r->dmv[i][0] = svq3_get_se_golomb(gb);
  765. r->dmv[i][1] = svq3_get_se_golomb(gb);
  766. }
  767. switch(block_type){
  768. case RV34_MB_TYPE_INTRA:
  769. case RV34_MB_TYPE_INTRA16x16:
  770. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  771. return 0;
  772. case RV34_MB_SKIP:
  773. if(s->pict_type == AV_PICTURE_TYPE_P){
  774. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  775. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  776. break;
  777. }
  778. case RV34_MB_B_DIRECT:
  779. //surprisingly, it uses motion scheme from next reference frame
  780. /* wait for the current mb row to be finished */
  781. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  782. ff_thread_await_progress(&s->next_picture_ptr->f, FFMAX(0, s->mb_y-1), 0);
  783. next_bt = s->next_picture_ptr->f.mb_type[s->mb_x + s->mb_y * s->mb_stride];
  784. if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
  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. ZERO8x2(s->current_picture_ptr->f.motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  787. }else
  788. for(j = 0; j < 2; j++)
  789. for(i = 0; i < 2; i++)
  790. for(k = 0; k < 2; k++)
  791. for(l = 0; l < 2; l++)
  792. 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]);
  793. if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
  794. rv34_mc_2mv(r, block_type);
  795. else
  796. rv34_mc_2mv_skip(r);
  797. ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
  798. break;
  799. case RV34_MB_P_16x16:
  800. case RV34_MB_P_MIX16x16:
  801. rv34_pred_mv(r, block_type, 0, 0);
  802. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  803. break;
  804. case RV34_MB_B_FORWARD:
  805. case RV34_MB_B_BACKWARD:
  806. r->dmv[1][0] = r->dmv[0][0];
  807. r->dmv[1][1] = r->dmv[0][1];
  808. if(r->rv30)
  809. rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
  810. else
  811. rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
  812. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
  813. break;
  814. case RV34_MB_P_16x8:
  815. case RV34_MB_P_8x16:
  816. rv34_pred_mv(r, block_type, 0, 0);
  817. rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
  818. if(block_type == RV34_MB_P_16x8){
  819. rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
  820. rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
  821. }
  822. if(block_type == RV34_MB_P_8x16){
  823. rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
  824. rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
  825. }
  826. break;
  827. case RV34_MB_B_BIDIR:
  828. rv34_pred_mv_b (r, block_type, 0);
  829. rv34_pred_mv_b (r, block_type, 1);
  830. rv34_mc_2mv (r, block_type);
  831. break;
  832. case RV34_MB_P_8x8:
  833. for(i=0;i< 4;i++){
  834. rv34_pred_mv(r, block_type, i, i);
  835. rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
  836. }
  837. break;
  838. }
  839. return 0;
  840. }
  841. /** @} */ // mv group
  842. /**
  843. * @name Macroblock reconstruction functions
  844. * @{
  845. */
  846. /** mapping of RV30/40 intra prediction types to standard H.264 types */
  847. static const int ittrans[9] = {
  848. DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
  849. VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
  850. };
  851. /** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
  852. static const int ittrans16[4] = {
  853. DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
  854. };
  855. /**
  856. * Perform 4x4 intra prediction.
  857. */
  858. static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
  859. {
  860. uint8_t *prev = dst - stride + 4;
  861. uint32_t topleft;
  862. if(!up && !left)
  863. itype = DC_128_PRED;
  864. else if(!up){
  865. if(itype == VERT_PRED) itype = HOR_PRED;
  866. if(itype == DC_PRED) itype = LEFT_DC_PRED;
  867. }else if(!left){
  868. if(itype == HOR_PRED) itype = VERT_PRED;
  869. if(itype == DC_PRED) itype = TOP_DC_PRED;
  870. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  871. }
  872. if(!down){
  873. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  874. if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
  875. if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
  876. }
  877. if(!right && up){
  878. topleft = dst[-stride + 3] * 0x01010101u;
  879. prev = (uint8_t*)&topleft;
  880. }
  881. r->h.pred4x4[itype](dst, prev, stride);
  882. }
  883. static inline int adjust_pred16(int itype, int up, int left)
  884. {
  885. if(!up && !left)
  886. itype = DC_128_PRED8x8;
  887. else if(!up){
  888. if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
  889. if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
  890. if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
  891. }else if(!left){
  892. if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
  893. if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
  894. if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
  895. }
  896. return itype;
  897. }
  898. static inline void rv34_process_block(RV34DecContext *r,
  899. uint8_t *pdst, int stride,
  900. int fc, int sc, int q_dc, int q_ac)
  901. {
  902. MpegEncContext *s = &r->s;
  903. DCTELEM *ptr = s->block[0];
  904. int has_ac = rv34_decode_block(ptr, &s->gb, r->cur_vlcs,
  905. fc, sc, q_dc, q_ac, q_ac);
  906. if(has_ac){
  907. r->rdsp.rv34_idct_add(pdst, stride, ptr);
  908. }else{
  909. r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
  910. ptr[0] = 0;
  911. }
  912. }
  913. static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
  914. {
  915. LOCAL_ALIGNED_16(DCTELEM, block16, [16]);
  916. MpegEncContext *s = &r->s;
  917. GetBitContext *gb = &s->gb;
  918. int q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
  919. q_ac = rv34_qscale_tab[s->qscale];
  920. uint8_t *dst = s->dest[0];
  921. DCTELEM *ptr = s->block[0];
  922. int i, j, itype, has_ac;
  923. memset(block16, 0, 16 * sizeof(*block16));
  924. has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
  925. if(has_ac)
  926. r->rdsp.rv34_inv_transform(block16);
  927. else
  928. r->rdsp.rv34_inv_transform_dc(block16);
  929. itype = ittrans16[intra_types[0]];
  930. itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
  931. r->h.pred16x16[itype](dst, s->linesize);
  932. for(j = 0; j < 4; j++){
  933. for(i = 0; i < 4; i++, cbp >>= 1){
  934. int dc = block16[i + j*4];
  935. if(cbp & 1){
  936. has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
  937. }else
  938. has_ac = 0;
  939. if(has_ac){
  940. ptr[0] = dc;
  941. r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
  942. }else
  943. r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
  944. }
  945. dst += 4*s->linesize;
  946. }
  947. itype = ittrans16[intra_types[0]];
  948. if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
  949. itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
  950. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  951. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  952. for(j = 1; j < 3; j++){
  953. dst = s->dest[j];
  954. r->h.pred8x8[itype](dst, s->uvlinesize);
  955. for(i = 0; i < 4; i++, cbp >>= 1){
  956. uint8_t *pdst;
  957. if(!(cbp & 1)) continue;
  958. pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
  959. rv34_process_block(r, pdst, s->uvlinesize,
  960. r->chroma_vlc, 1, q_dc, q_ac);
  961. }
  962. }
  963. }
  964. static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
  965. {
  966. MpegEncContext *s = &r->s;
  967. uint8_t *dst = s->dest[0];
  968. int avail[6*8] = {0};
  969. int i, j, k;
  970. int idx, q_ac, q_dc;
  971. // Set neighbour information.
  972. if(r->avail_cache[1])
  973. avail[0] = 1;
  974. if(r->avail_cache[2])
  975. avail[1] = avail[2] = 1;
  976. if(r->avail_cache[3])
  977. avail[3] = avail[4] = 1;
  978. if(r->avail_cache[4])
  979. avail[5] = 1;
  980. if(r->avail_cache[5])
  981. avail[8] = avail[16] = 1;
  982. if(r->avail_cache[9])
  983. avail[24] = avail[32] = 1;
  984. q_ac = rv34_qscale_tab[s->qscale];
  985. for(j = 0; j < 4; j++){
  986. idx = 9 + j*8;
  987. for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
  988. rv34_pred_4x4_block(r, dst, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
  989. avail[idx] = 1;
  990. if(!(cbp & 1)) continue;
  991. rv34_process_block(r, dst, s->linesize,
  992. r->luma_vlc, 0, q_ac, q_ac);
  993. }
  994. dst += s->linesize * 4 - 4*4;
  995. intra_types += r->intra_types_stride;
  996. }
  997. intra_types -= r->intra_types_stride * 4;
  998. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  999. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  1000. for(k = 0; k < 2; k++){
  1001. dst = s->dest[1+k];
  1002. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
  1003. for(j = 0; j < 2; j++){
  1004. int* acache = r->avail_cache + 6 + j*4;
  1005. for(i = 0; i < 2; i++, cbp >>= 1, acache++){
  1006. int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
  1007. rv34_pred_4x4_block(r, dst+4*i, s->uvlinesize, itype, acache[-4], acache[-1], !i && !j, acache[-3]);
  1008. acache[0] = 1;
  1009. if(!(cbp&1)) continue;
  1010. rv34_process_block(r, dst + 4*i, s->uvlinesize,
  1011. r->chroma_vlc, 1, q_dc, q_ac);
  1012. }
  1013. dst += 4*s->uvlinesize;
  1014. }
  1015. }
  1016. }
  1017. static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
  1018. {
  1019. int d;
  1020. d = motion_val[0][0] - motion_val[-step][0];
  1021. if(d < -3 || d > 3)
  1022. return 1;
  1023. d = motion_val[0][1] - motion_val[-step][1];
  1024. if(d < -3 || d > 3)
  1025. return 1;
  1026. return 0;
  1027. }
  1028. static int rv34_set_deblock_coef(RV34DecContext *r)
  1029. {
  1030. MpegEncContext *s = &r->s;
  1031. int hmvmask = 0, vmvmask = 0, i, j;
  1032. int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  1033. int16_t (*motion_val)[2] = &s->current_picture_ptr->f.motion_val[0][midx];
  1034. for(j = 0; j < 16; j += 8){
  1035. for(i = 0; i < 2; i++){
  1036. if(is_mv_diff_gt_3(motion_val + i, 1))
  1037. vmvmask |= 0x11 << (j + i*2);
  1038. if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
  1039. hmvmask |= 0x03 << (j + i*2);
  1040. }
  1041. motion_val += s->b8_stride;
  1042. }
  1043. if(s->first_slice_line)
  1044. hmvmask &= ~0x000F;
  1045. if(!s->mb_x)
  1046. vmvmask &= ~0x1111;
  1047. if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
  1048. vmvmask |= (vmvmask & 0x4444) >> 1;
  1049. hmvmask |= (hmvmask & 0x0F00) >> 4;
  1050. if(s->mb_x)
  1051. r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
  1052. if(!s->first_slice_line)
  1053. r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
  1054. }
  1055. return hmvmask | vmvmask;
  1056. }
  1057. static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
  1058. {
  1059. MpegEncContext *s = &r->s;
  1060. GetBitContext *gb = &s->gb;
  1061. uint8_t *dst = s->dest[0];
  1062. DCTELEM *ptr = s->block[0];
  1063. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1064. int cbp, cbp2;
  1065. int q_dc, q_ac, has_ac;
  1066. int i, j;
  1067. int dist;
  1068. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1069. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1070. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
  1071. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1072. if(s->mb_x && dist)
  1073. r->avail_cache[5] =
  1074. r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
  1075. if(dist >= s->mb_width)
  1076. r->avail_cache[2] =
  1077. r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
  1078. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1079. r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
  1080. if(s->mb_x && dist > s->mb_width)
  1081. r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
  1082. s->qscale = r->si.quant;
  1083. cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
  1084. r->cbp_luma [mb_pos] = cbp;
  1085. r->cbp_chroma[mb_pos] = cbp >> 16;
  1086. r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
  1087. s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
  1088. if(cbp == -1)
  1089. return -1;
  1090. if (IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
  1091. if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
  1092. else rv34_output_intra(r, intra_types, cbp);
  1093. return 0;
  1094. }
  1095. if(r->is16){
  1096. // Only for RV34_MB_P_MIX16x16
  1097. LOCAL_ALIGNED_16(DCTELEM, block16, [16]);
  1098. memset(block16, 0, 16 * sizeof(*block16));
  1099. q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
  1100. q_ac = rv34_qscale_tab[s->qscale];
  1101. if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac))
  1102. r->rdsp.rv34_inv_transform(block16);
  1103. else
  1104. r->rdsp.rv34_inv_transform_dc(block16);
  1105. q_ac = rv34_qscale_tab[s->qscale];
  1106. for(j = 0; j < 4; j++){
  1107. for(i = 0; i < 4; i++, cbp >>= 1){
  1108. int dc = block16[i + j*4];
  1109. if(cbp & 1){
  1110. has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
  1111. }else
  1112. has_ac = 0;
  1113. if(has_ac){
  1114. ptr[0] = dc;
  1115. r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
  1116. }else
  1117. r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
  1118. }
  1119. dst += 4*s->linesize;
  1120. }
  1121. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  1122. }else{
  1123. q_ac = rv34_qscale_tab[s->qscale];
  1124. for(j = 0; j < 4; j++){
  1125. for(i = 0; i < 4; i++, cbp >>= 1){
  1126. if(!(cbp & 1)) continue;
  1127. rv34_process_block(r, dst + 4*i, s->linesize,
  1128. r->luma_vlc, 0, q_ac, q_ac);
  1129. }
  1130. dst += 4*s->linesize;
  1131. }
  1132. }
  1133. q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
  1134. q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
  1135. for(j = 1; j < 3; j++){
  1136. dst = s->dest[j];
  1137. for(i = 0; i < 4; i++, cbp >>= 1){
  1138. uint8_t *pdst;
  1139. if(!(cbp & 1)) continue;
  1140. pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
  1141. rv34_process_block(r, pdst, s->uvlinesize,
  1142. r->chroma_vlc, 1, q_dc, q_ac);
  1143. }
  1144. }
  1145. return 0;
  1146. }
  1147. static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
  1148. {
  1149. MpegEncContext *s = &r->s;
  1150. int cbp, dist;
  1151. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1152. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1153. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1154. fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
  1155. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1156. if(s->mb_x && dist)
  1157. r->avail_cache[5] =
  1158. r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
  1159. if(dist >= s->mb_width)
  1160. r->avail_cache[2] =
  1161. r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
  1162. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1163. r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
  1164. if(s->mb_x && dist > s->mb_width)
  1165. r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
  1166. s->qscale = r->si.quant;
  1167. cbp = rv34_decode_intra_mb_header(r, intra_types);
  1168. r->cbp_luma [mb_pos] = cbp;
  1169. r->cbp_chroma[mb_pos] = cbp >> 16;
  1170. r->deblock_coefs[mb_pos] = 0xFFFF;
  1171. s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
  1172. if(cbp == -1)
  1173. return -1;
  1174. if(r->is16){
  1175. rv34_output_i16x16(r, intra_types, cbp);
  1176. return 0;
  1177. }
  1178. rv34_output_intra(r, intra_types, cbp);
  1179. return 0;
  1180. }
  1181. static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
  1182. {
  1183. int bits;
  1184. if(s->mb_y >= s->mb_height)
  1185. return 1;
  1186. if(!s->mb_num_left)
  1187. return 1;
  1188. if(r->s.mb_skip_run > 1)
  1189. return 0;
  1190. bits = get_bits_left(&s->gb);
  1191. if(bits <= 0 || (bits < 8 && !show_bits(&s->gb, bits)))
  1192. return 1;
  1193. return 0;
  1194. }
  1195. static void rv34_decoder_free(RV34DecContext *r)
  1196. {
  1197. av_freep(&r->intra_types_hist);
  1198. r->intra_types = NULL;
  1199. av_freep(&r->tmp_b_block_base);
  1200. av_freep(&r->mb_type);
  1201. av_freep(&r->cbp_luma);
  1202. av_freep(&r->cbp_chroma);
  1203. av_freep(&r->deblock_coefs);
  1204. }
  1205. static int rv34_decoder_alloc(RV34DecContext *r)
  1206. {
  1207. r->intra_types_stride = r->s.mb_width * 4 + 4;
  1208. r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height *
  1209. sizeof(*r->cbp_chroma));
  1210. r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height *
  1211. sizeof(*r->cbp_luma));
  1212. r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height *
  1213. sizeof(*r->deblock_coefs));
  1214. r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
  1215. sizeof(*r->intra_types_hist));
  1216. r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height *
  1217. sizeof(*r->mb_type));
  1218. if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs &&
  1219. r->intra_types_hist && r->mb_type)) {
  1220. rv34_decoder_free(r);
  1221. return AVERROR(ENOMEM);
  1222. }
  1223. r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
  1224. return 0;
  1225. }
  1226. static int rv34_decoder_realloc(RV34DecContext *r)
  1227. {
  1228. rv34_decoder_free(r);
  1229. return rv34_decoder_alloc(r);
  1230. }
  1231. static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
  1232. {
  1233. MpegEncContext *s = &r->s;
  1234. GetBitContext *gb = &s->gb;
  1235. int mb_pos, slice_type;
  1236. int res;
  1237. init_get_bits(&r->s.gb, buf, buf_size*8);
  1238. res = r->parse_slice_header(r, gb, &r->si);
  1239. if(res < 0){
  1240. av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
  1241. return -1;
  1242. }
  1243. slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
  1244. if (slice_type != s->pict_type) {
  1245. av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
  1246. return AVERROR_INVALIDDATA;
  1247. }
  1248. if (s->width != r->si.width || s->height != r->si.height) {
  1249. av_log(s->avctx, AV_LOG_ERROR, "Size mismatch\n");
  1250. return AVERROR_INVALIDDATA;
  1251. }
  1252. r->si.end = end;
  1253. s->qscale = r->si.quant;
  1254. s->mb_num_left = r->si.end - r->si.start;
  1255. r->s.mb_skip_run = 0;
  1256. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  1257. if(r->si.start != mb_pos){
  1258. av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
  1259. s->mb_x = r->si.start % s->mb_width;
  1260. s->mb_y = r->si.start / s->mb_width;
  1261. }
  1262. memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1263. s->first_slice_line = 1;
  1264. s->resync_mb_x = s->mb_x;
  1265. s->resync_mb_y = s->mb_y;
  1266. ff_init_block_index(s);
  1267. while(!check_slice_end(r, s)) {
  1268. ff_update_block_index(s);
  1269. if(r->si.type)
  1270. res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
  1271. else
  1272. res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
  1273. if(res < 0){
  1274. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
  1275. return -1;
  1276. }
  1277. if (++s->mb_x == s->mb_width) {
  1278. s->mb_x = 0;
  1279. s->mb_y++;
  1280. ff_init_block_index(s);
  1281. memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
  1282. memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
  1283. if(r->loop_filter && s->mb_y >= 2)
  1284. r->loop_filter(r, s->mb_y - 2);
  1285. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  1286. ff_thread_report_progress(&s->current_picture_ptr->f,
  1287. s->mb_y - 2, 0);
  1288. }
  1289. if(s->mb_x == s->resync_mb_x)
  1290. s->first_slice_line=0;
  1291. s->mb_num_left--;
  1292. }
  1293. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  1294. return s->mb_y == s->mb_height;
  1295. }
  1296. /** @} */ // recons group end
  1297. /**
  1298. * Initialize decoder.
  1299. */
  1300. av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
  1301. {
  1302. RV34DecContext *r = avctx->priv_data;
  1303. MpegEncContext *s = &r->s;
  1304. int ret;
  1305. ff_MPV_decode_defaults(s);
  1306. s->avctx = avctx;
  1307. s->out_format = FMT_H263;
  1308. s->codec_id = avctx->codec_id;
  1309. s->width = avctx->width;
  1310. s->height = avctx->height;
  1311. r->s.avctx = avctx;
  1312. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  1313. r->s.flags |= CODEC_FLAG_EMU_EDGE;
  1314. avctx->pix_fmt = 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, &r->s.dsp);
  1323. #endif
  1324. #if CONFIG_RV40_DECODER
  1325. if (avctx->codec_id == AV_CODEC_ID_RV40)
  1326. ff_rv40dsp_init(&r->rdsp, &r->s.dsp);
  1327. #endif
  1328. if ((ret = rv34_decoder_alloc(r)) < 0)
  1329. return ret;
  1330. if(!intra_vlcs[0].cbppattern[0].bits)
  1331. rv34_init_tables();
  1332. return 0;
  1333. }
  1334. int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
  1335. {
  1336. int err;
  1337. RV34DecContext *r = avctx->priv_data;
  1338. r->s.avctx = avctx;
  1339. if (avctx->internal->is_copy) {
  1340. r->tmp_b_block_base = NULL;
  1341. if ((err = ff_MPV_common_init(&r->s)) < 0)
  1342. return err;
  1343. if ((err = rv34_decoder_alloc(r)) < 0)
  1344. return err;
  1345. }
  1346. return 0;
  1347. }
  1348. int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1349. {
  1350. RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
  1351. MpegEncContext * const s = &r->s, * const s1 = &r1->s;
  1352. int err;
  1353. if (dst == src || !s1->context_initialized)
  1354. return 0;
  1355. if (s->height != s1->height || s->width != s1->width) {
  1356. ff_MPV_common_end(s);
  1357. s->height = s1->height;
  1358. s->width = s1->width;
  1359. if ((err = ff_MPV_common_init(s)) < 0)
  1360. return err;
  1361. if ((err = rv34_decoder_realloc(r)) < 0)
  1362. return err;
  1363. }
  1364. if ((err = ff_mpeg_update_thread_context(dst, src)))
  1365. return err;
  1366. r->cur_pts = r1->cur_pts;
  1367. r->last_pts = r1->last_pts;
  1368. r->next_pts = r1->next_pts;
  1369. memset(&r->si, 0, sizeof(r->si));
  1370. return 0;
  1371. }
  1372. static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
  1373. {
  1374. if(avctx->slice_count) return avctx->slice_offset[n];
  1375. else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
  1376. }
  1377. static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
  1378. {
  1379. RV34DecContext *r = avctx->priv_data;
  1380. MpegEncContext *s = &r->s;
  1381. int got_picture = 0;
  1382. ff_er_frame_end(s);
  1383. ff_MPV_frame_end(s);
  1384. s->mb_num_left = 0;
  1385. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  1386. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  1387. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1388. *pict = s->current_picture_ptr->f;
  1389. got_picture = 1;
  1390. } else if (s->last_picture_ptr != NULL) {
  1391. *pict = s->last_picture_ptr->f;
  1392. got_picture = 1;
  1393. }
  1394. if (got_picture)
  1395. ff_print_debug_info(s, pict);
  1396. return got_picture;
  1397. }
  1398. int ff_rv34_decode_frame(AVCodecContext *avctx,
  1399. void *data, int *got_picture_ptr,
  1400. AVPacket *avpkt)
  1401. {
  1402. const uint8_t *buf = avpkt->data;
  1403. int buf_size = avpkt->size;
  1404. RV34DecContext *r = avctx->priv_data;
  1405. MpegEncContext *s = &r->s;
  1406. AVFrame *pict = data;
  1407. SliceInfo si;
  1408. int i;
  1409. int slice_count;
  1410. const uint8_t *slices_hdr = NULL;
  1411. int last = 0;
  1412. /* no supplementary picture */
  1413. if (buf_size == 0) {
  1414. /* special case for last picture */
  1415. if (s->low_delay==0 && s->next_picture_ptr) {
  1416. *pict = s->next_picture_ptr->f;
  1417. s->next_picture_ptr = NULL;
  1418. *got_picture_ptr = 1;
  1419. }
  1420. return 0;
  1421. }
  1422. if(!avctx->slice_count){
  1423. slice_count = (*buf++) + 1;
  1424. slices_hdr = buf + 4;
  1425. buf += 8 * slice_count;
  1426. buf_size -= 1 + 8 * slice_count;
  1427. }else
  1428. slice_count = avctx->slice_count;
  1429. //parse first slice header to check whether this frame can be decoded
  1430. if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
  1431. get_slice_offset(avctx, slices_hdr, 0) > buf_size){
  1432. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1433. return AVERROR_INVALIDDATA;
  1434. }
  1435. init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
  1436. if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
  1437. av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
  1438. return AVERROR_INVALIDDATA;
  1439. }
  1440. if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) &&
  1441. si.type == AV_PICTURE_TYPE_B) {
  1442. av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
  1443. "reference data.\n");
  1444. return AVERROR_INVALIDDATA;
  1445. }
  1446. if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
  1447. || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
  1448. || avctx->skip_frame >= AVDISCARD_ALL)
  1449. return avpkt->size;
  1450. /* first slice */
  1451. if (si.start == 0) {
  1452. if (s->mb_num_left > 0) {
  1453. av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.",
  1454. s->mb_num_left);
  1455. ff_er_frame_end(s);
  1456. ff_MPV_frame_end(s);
  1457. }
  1458. if (s->width != si.width || s->height != si.height) {
  1459. int err;
  1460. if (HAVE_THREADS &&
  1461. (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  1462. av_log_missing_feature(s->avctx, "Width/height changing with "
  1463. "frame threading is", 0);
  1464. return AVERROR_PATCHWELCOME;
  1465. }
  1466. av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
  1467. si.width, si.height);
  1468. ff_MPV_common_end(s);
  1469. s->width = si.width;
  1470. s->height = si.height;
  1471. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1472. if ((err = ff_MPV_common_init(s)) < 0)
  1473. return err;
  1474. if ((err = rv34_decoder_realloc(r)) < 0)
  1475. return err;
  1476. }
  1477. s->pict_type = si.type ? si.type : AV_PICTURE_TYPE_I;
  1478. if (ff_MPV_frame_start(s, s->avctx) < 0)
  1479. return -1;
  1480. ff_er_frame_start(s);
  1481. if (!r->tmp_b_block_base) {
  1482. int i;
  1483. r->tmp_b_block_base = av_malloc(s->linesize * 48);
  1484. for (i = 0; i < 2; i++)
  1485. r->tmp_b_block_y[i] = r->tmp_b_block_base
  1486. + i * 16 * s->linesize;
  1487. for (i = 0; i < 4; i++)
  1488. r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
  1489. + (i >> 1) * 8 * s->uvlinesize
  1490. + (i & 1) * 16;
  1491. }
  1492. r->cur_pts = si.pts;
  1493. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1494. r->last_pts = r->next_pts;
  1495. r->next_pts = r->cur_pts;
  1496. } else {
  1497. int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
  1498. int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
  1499. int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
  1500. if(!refdist){
  1501. r->mv_weight1 = r->mv_weight2 = r->weight1 = r->weight2 = 8192;
  1502. r->scaled_weight = 0;
  1503. }else{
  1504. r->mv_weight1 = (dist0 << 14) / refdist;
  1505. r->mv_weight2 = (dist1 << 14) / refdist;
  1506. if((r->mv_weight1|r->mv_weight2) & 511){
  1507. r->weight1 = r->mv_weight1;
  1508. r->weight2 = r->mv_weight2;
  1509. r->scaled_weight = 0;
  1510. }else{
  1511. r->weight1 = r->mv_weight1 >> 9;
  1512. r->weight2 = r->mv_weight2 >> 9;
  1513. r->scaled_weight = 1;
  1514. }
  1515. }
  1516. }
  1517. s->mb_x = s->mb_y = 0;
  1518. ff_thread_finish_setup(s->avctx);
  1519. } else if (HAVE_THREADS &&
  1520. (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  1521. av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames in frame "
  1522. "multithreading mode (start MB is %d).\n", si.start);
  1523. return AVERROR_INVALIDDATA;
  1524. }
  1525. for(i = 0; i < slice_count; i++){
  1526. int offset = get_slice_offset(avctx, slices_hdr, i);
  1527. int size;
  1528. if(i+1 == slice_count)
  1529. size = buf_size - offset;
  1530. else
  1531. size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
  1532. if(offset < 0 || offset > buf_size){
  1533. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1534. break;
  1535. }
  1536. r->si.end = s->mb_width * s->mb_height;
  1537. s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
  1538. if(i+1 < slice_count){
  1539. if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||
  1540. get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {
  1541. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1542. break;
  1543. }
  1544. 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);
  1545. if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
  1546. if(i+2 < slice_count)
  1547. size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
  1548. else
  1549. size = buf_size - offset;
  1550. }else
  1551. r->si.end = si.start;
  1552. }
  1553. if (size < 0 || size > buf_size - offset) {
  1554. av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
  1555. break;
  1556. }
  1557. last = rv34_decode_slice(r, r->si.end, buf + offset, size);
  1558. if(last)
  1559. break;
  1560. }
  1561. if (s->current_picture_ptr) {
  1562. if (last) {
  1563. if(r->loop_filter)
  1564. r->loop_filter(r, s->mb_height - 1);
  1565. *got_picture_ptr = finish_frame(avctx, pict);
  1566. } else if (HAVE_THREADS &&
  1567. (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  1568. av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n");
  1569. /* always mark the current frame as finished, frame-mt supports
  1570. * only complete frames */
  1571. ff_er_frame_end(s);
  1572. ff_MPV_frame_end(s);
  1573. s->mb_num_left = 0;
  1574. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  1575. return AVERROR_INVALIDDATA;
  1576. }
  1577. }
  1578. return avpkt->size;
  1579. }
  1580. av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
  1581. {
  1582. RV34DecContext *r = avctx->priv_data;
  1583. ff_MPV_common_end(&r->s);
  1584. rv34_decoder_free(r);
  1585. return 0;
  1586. }