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.

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