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.

1843 lines
64KB

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