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.

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