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.

1769 lines
61KB

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