rv34.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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 libavcodec/rv34.c
  23. * RV30/40 decoder common data
  24. */
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. #include "golomb.h"
  29. #include "mathops.h"
  30. #include "rectangle.h"
  31. #include "rv34vlc.h"
  32. #include "rv34data.h"
  33. #include "rv34.h"
  34. //#define DEBUG
  35. /** translation of RV30/40 macroblock types to lavc ones */
  36. static const int rv34_mb_type_to_lavc[12] = {
  37. MB_TYPE_INTRA,
  38. MB_TYPE_INTRA16x16 | MB_TYPE_SEPARATE_DC,
  39. MB_TYPE_16x16 | MB_TYPE_L0,
  40. MB_TYPE_8x8 | MB_TYPE_L0,
  41. MB_TYPE_16x16 | MB_TYPE_L0,
  42. MB_TYPE_16x16 | MB_TYPE_L1,
  43. MB_TYPE_SKIP,
  44. MB_TYPE_DIRECT2 | MB_TYPE_16x16,
  45. MB_TYPE_16x8 | MB_TYPE_L0,
  46. MB_TYPE_8x16 | MB_TYPE_L0,
  47. MB_TYPE_16x16 | MB_TYPE_L0L1,
  48. MB_TYPE_16x16 | MB_TYPE_L0 | MB_TYPE_SEPARATE_DC
  49. };
  50. static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
  51. /**
  52. * @defgroup vlc RV30/40 VLC generating functions
  53. * @{
  54. */
  55. /**
  56. * Generate VLC from codeword lengths.
  57. * @param bits codeword lengths (zeroes are accepted)
  58. * @param size length of input data
  59. * @param vlc output VLC
  60. * @param insyms symbols for input codes (NULL for default ones)
  61. */
  62. static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms)
  63. {
  64. int i;
  65. int counts[17] = {0}, codes[17];
  66. uint16_t cw[size], syms[size];
  67. uint8_t bits2[size];
  68. int maxbits = 0, realsize = 0;
  69. for(i = 0; i < size; i++){
  70. if(bits[i]){
  71. bits2[realsize] = bits[i];
  72. syms[realsize] = insyms ? insyms[i] : i;
  73. realsize++;
  74. maxbits = FFMAX(maxbits, bits[i]);
  75. counts[bits[i]]++;
  76. }
  77. }
  78. codes[0] = 0;
  79. for(i = 0; i < 16; i++)
  80. codes[i+1] = (codes[i] + counts[i]) << 1;
  81. for(i = 0; i < realsize; i++)
  82. cw[i] = codes[bits2[i]]++;
  83. init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
  84. bits2, 1, 1,
  85. cw, 2, 2,
  86. syms, 2, 2, INIT_VLC_USE_STATIC);
  87. }
  88. /**
  89. * Initialize all tables.
  90. */
  91. static av_cold void rv34_init_tables(void)
  92. {
  93. int i, j, k;
  94. for(i = 0; i < NUM_INTRA_TABLES; i++){
  95. for(j = 0; j < 2; j++){
  96. rv34_gen_vlc(rv34_table_intra_cbppat [i][j], CBPPAT_VLC_SIZE, &intra_vlcs[i].cbppattern[j], NULL);
  97. rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].second_pattern[j], NULL);
  98. rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].third_pattern[j], NULL);
  99. for(k = 0; k < 4; k++)
  100. rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2], CBP_VLC_SIZE, &intra_vlcs[i].cbp[j][k], rv34_cbp_code);
  101. }
  102. for(j = 0; j < 4; j++)
  103. rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, &intra_vlcs[i].first_pattern[j], NULL);
  104. rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, &intra_vlcs[i].coefficient, NULL);
  105. }
  106. for(i = 0; i < NUM_INTER_TABLES; i++){
  107. rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE, &inter_vlcs[i].cbppattern[0], NULL);
  108. for(j = 0; j < 4; j++)
  109. rv34_gen_vlc(rv34_inter_cbp[i][j], CBP_VLC_SIZE, &inter_vlcs[i].cbp[0][j], rv34_cbp_code);
  110. for(j = 0; j < 2; j++){
  111. rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE, &inter_vlcs[i].first_pattern[j], NULL);
  112. rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].second_pattern[j], NULL);
  113. rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].third_pattern[j], NULL);
  114. }
  115. rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE, &inter_vlcs[i].coefficient, NULL);
  116. }
  117. }
  118. /** @} */ // vlc group
  119. /**
  120. * @defgroup transform RV30/40 inverse transform functions
  121. * @{
  122. */
  123. static av_always_inline void rv34_row_transform(int temp[16], DCTELEM *block)
  124. {
  125. int i;
  126. for(i=0; i<4; i++){
  127. const int z0= 13*(block[i+8*0] + block[i+8*2]);
  128. const int z1= 13*(block[i+8*0] - block[i+8*2]);
  129. const int z2= 7* block[i+8*1] - 17*block[i+8*3];
  130. const int z3= 17* block[i+8*1] + 7*block[i+8*3];
  131. temp[4*i+0]= z0+z3;
  132. temp[4*i+1]= z1+z2;
  133. temp[4*i+2]= z1-z2;
  134. temp[4*i+3]= z0-z3;
  135. }
  136. }
  137. /**
  138. * Real Video 3.0/4.0 inverse transform
  139. * Code is almost the same as in SVQ3, only scaling is different.
  140. */
  141. static void rv34_inv_transform(DCTELEM *block){
  142. int temp[16];
  143. int i;
  144. rv34_row_transform(temp, block);
  145. for(i=0; i<4; i++){
  146. const int z0= 13*(temp[4*0+i] + temp[4*2+i]) + 0x200;
  147. const int z1= 13*(temp[4*0+i] - temp[4*2+i]) + 0x200;
  148. const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
  149. const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
  150. block[i*8+0]= (z0 + z3)>>10;
  151. block[i*8+1]= (z1 + z2)>>10;
  152. block[i*8+2]= (z1 - z2)>>10;
  153. block[i*8+3]= (z0 - z3)>>10;
  154. }
  155. }
  156. /**
  157. * RealVideo 3.0/4.0 inverse transform for DC block
  158. *
  159. * Code is almost the same as rv34_inv_transform()
  160. * but final coefficients are multiplied by 1.5 and have no rounding.
  161. */
  162. static void rv34_inv_transform_noround(DCTELEM *block){
  163. int temp[16];
  164. int i;
  165. rv34_row_transform(temp, block);
  166. for(i=0; i<4; i++){
  167. const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
  168. const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
  169. const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
  170. const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
  171. block[i*8+0]= ((z0 + z3)*3)>>11;
  172. block[i*8+1]= ((z1 + z2)*3)>>11;
  173. block[i*8+2]= ((z1 - z2)*3)>>11;
  174. block[i*8+3]= ((z0 - z3)*3)>>11;
  175. }
  176. }
  177. /** @} */ // transform
  178. /**
  179. * @defgroup block RV30/40 4x4 block decoding functions
  180. * @{
  181. */
  182. /**
  183. * Decode coded block pattern.
  184. */
  185. static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table)
  186. {
  187. int pattern, code, cbp=0;
  188. int ones;
  189. static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
  190. static const int shifts[4] = { 0, 2, 8, 10 };
  191. const int *curshift = shifts;
  192. int i, t, mask;
  193. code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
  194. pattern = code & 0xF;
  195. code >>= 4;
  196. ones = rv34_count_ones[pattern];
  197. for(mask = 8; mask; mask >>= 1, curshift++){
  198. if(pattern & mask)
  199. cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
  200. }
  201. for(i = 0; i < 4; i++){
  202. t = modulo_three_table[code][i];
  203. if(t == 1)
  204. cbp |= cbp_masks[get_bits1(gb)] << i;
  205. if(t == 2)
  206. cbp |= cbp_masks[2] << i;
  207. }
  208. return cbp;
  209. }
  210. /**
  211. * Get one coefficient value from the bistream and store it.
  212. */
  213. static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc)
  214. {
  215. if(coef){
  216. if(coef == esc){
  217. coef = get_vlc2(gb, vlc->table, 9, 2);
  218. if(coef > 23){
  219. coef -= 23;
  220. coef = 22 + ((1 << coef) | get_bits(gb, coef));
  221. }
  222. coef += esc;
  223. }
  224. if(get_bits1(gb))
  225. coef = -coef;
  226. *dst = coef;
  227. }
  228. }
  229. /**
  230. * Decode 2x2 subblock of coefficients.
  231. */
  232. static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc)
  233. {
  234. int coeffs[4];
  235. coeffs[0] = modulo_three_table[code][0];
  236. coeffs[1] = modulo_three_table[code][1];
  237. coeffs[2] = modulo_three_table[code][2];
  238. coeffs[3] = modulo_three_table[code][3];
  239. decode_coeff(dst , coeffs[0], 3, gb, vlc);
  240. if(is_block2){
  241. decode_coeff(dst+8, coeffs[1], 2, gb, vlc);
  242. decode_coeff(dst+1, coeffs[2], 2, gb, vlc);
  243. }else{
  244. decode_coeff(dst+1, coeffs[1], 2, gb, vlc);
  245. decode_coeff(dst+8, coeffs[2], 2, gb, vlc);
  246. }
  247. decode_coeff(dst+9, coeffs[3], 2, gb, vlc);
  248. }
  249. /**
  250. * Decode coefficients for 4x4 block.
  251. *
  252. * This is done by filling 2x2 subblocks with decoded coefficients
  253. * in this order (the same for subblocks and subblock coefficients):
  254. * o--o
  255. * /
  256. * /
  257. * o--o
  258. */
  259. static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc)
  260. {
  261. int code, pattern;
  262. code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
  263. pattern = code & 0x7;
  264. code >>= 3;
  265. decode_subblock(dst, code, 0, gb, &rvlc->coefficient);
  266. if(pattern & 4){
  267. code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
  268. decode_subblock(dst + 2, code, 0, gb, &rvlc->coefficient);
  269. }
  270. if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
  271. code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
  272. decode_subblock(dst + 8*2, code, 1, gb, &rvlc->coefficient);
  273. }
  274. if(pattern & 1){
  275. code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
  276. decode_subblock(dst + 8*2+2, code, 0, gb, &rvlc->coefficient);
  277. }
  278. }
  279. /**
  280. * Dequantize ordinary 4x4 block.
  281. * @todo optimize
  282. */
  283. static inline void rv34_dequant4x4(DCTELEM *block, int Qdc, int Q)
  284. {
  285. int i, j;
  286. block[0] = (block[0] * Qdc + 8) >> 4;
  287. for(i = 0; i < 4; i++)
  288. for(j = !i; j < 4; j++)
  289. block[j + i*8] = (block[j + i*8] * Q + 8) >> 4;
  290. }
  291. /**
  292. * Dequantize 4x4 block of DC values for 16x16 macroblock.
  293. * @todo optimize
  294. */
  295. static inline void rv34_dequant4x4_16x16(DCTELEM *block, int Qdc, int Q)
  296. {
  297. int i;
  298. for(i = 0; i < 3; i++)
  299. block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Qdc + 8) >> 4;
  300. for(; i < 16; i++)
  301. block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Q + 8) >> 4;
  302. }
  303. /** @} */ //block functions
  304. /**
  305. * @defgroup bitstream RV30/40 bitstream parsing
  306. * @{
  307. */
  308. /**
  309. * Decode starting slice position.
  310. * @todo Maybe replace with ff_h263_decode_mba() ?
  311. */
  312. int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
  313. {
  314. int i;
  315. for(i = 0; i < 5; i++)
  316. if(rv34_mb_max_sizes[i] >= mb_size - 1)
  317. break;
  318. return rv34_mb_bits_sizes[i];
  319. }
  320. /**
  321. * Select VLC set for decoding from current quantizer, modifier and frame type.
  322. */
  323. static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
  324. {
  325. if(mod == 2 && quant < 19) quant += 10;
  326. else if(mod && quant < 26) quant += 5;
  327. return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
  328. : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
  329. }
  330. /**
  331. * Decode quantizer difference and return modified quantizer.
  332. */
  333. static inline int rv34_decode_dquant(GetBitContext *gb, int quant)
  334. {
  335. if(get_bits1(gb))
  336. return rv34_dquant_tab[get_bits1(gb)][quant];
  337. else
  338. return get_bits(gb, 5);
  339. }
  340. /** @} */ //bitstream functions
  341. /**
  342. * @defgroup mv motion vector related code (prediction, reconstruction, motion compensation)
  343. * @{
  344. */
  345. /** macroblock partition width in 8x8 blocks */
  346. static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
  347. /** macroblock partition height in 8x8 blocks */
  348. static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
  349. /** availability index for subblocks */
  350. static const uint8_t avail_indexes[4] = { 5, 6, 9, 10 };
  351. /**
  352. * motion vector prediction
  353. *
  354. * Motion prediction performed for the block by using median prediction of
  355. * motion vectors from the left, top and right top blocks but in corner cases
  356. * some other vectors may be used instead.
  357. */
  358. static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
  359. {
  360. MpegEncContext *s = &r->s;
  361. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  362. int A[2] = {0}, B[2], C[2];
  363. int i, j;
  364. int mx, my;
  365. int avail_index = avail_indexes[subblock_no];
  366. int c_off = part_sizes_w[block_type];
  367. mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
  368. if(subblock_no == 3)
  369. c_off = -1;
  370. if(r->avail_cache[avail_index - 1]){
  371. A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
  372. A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
  373. }
  374. if(r->avail_cache[avail_index - 4]){
  375. B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
  376. B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
  377. }else{
  378. B[0] = A[0];
  379. B[1] = A[1];
  380. }
  381. if(!r->avail_cache[avail_index - 4 + c_off]){
  382. if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){
  383. C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
  384. C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
  385. }else{
  386. C[0] = A[0];
  387. C[1] = A[1];
  388. }
  389. }else{
  390. C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
  391. C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
  392. }
  393. mx = mid_pred(A[0], B[0], C[0]);
  394. my = mid_pred(A[1], B[1], C[1]);
  395. mx += r->dmv[dmv_no][0];
  396. my += r->dmv[dmv_no][1];
  397. for(j = 0; j < part_sizes_h[block_type]; j++){
  398. for(i = 0; i < part_sizes_w[block_type]; i++){
  399. s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
  400. s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
  401. }
  402. }
  403. }
  404. #define GET_PTS_DIFF(a, b) ((a - b + 8192) & 0x1FFF)
  405. /**
  406. * Calculate motion vector component that should be added for direct blocks.
  407. */
  408. static int calc_add_mv(RV34DecContext *r, int dir, int val)
  409. {
  410. int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
  411. int dist = dir ? -GET_PTS_DIFF(r->next_pts, r->cur_pts) : GET_PTS_DIFF(r->cur_pts, r->last_pts);
  412. int mul;
  413. if(!refdist) return 0;
  414. mul = (dist << 14) / refdist;
  415. return (val * mul + 0x2000) >> 14;
  416. }
  417. /**
  418. * Predict motion vector for B-frame macroblock.
  419. */
  420. static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
  421. int A_avail, int B_avail, int C_avail,
  422. int *mx, int *my)
  423. {
  424. if(A_avail + B_avail + C_avail != 3){
  425. *mx = A[0] + B[0] + C[0];
  426. *my = A[1] + B[1] + C[1];
  427. if(A_avail + B_avail + C_avail == 2){
  428. *mx /= 2;
  429. *my /= 2;
  430. }
  431. }else{
  432. *mx = mid_pred(A[0], B[0], C[0]);
  433. *my = mid_pred(A[1], B[1], C[1]);
  434. }
  435. }
  436. /**
  437. * motion vector prediction for B-frames
  438. */
  439. static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
  440. {
  441. MpegEncContext *s = &r->s;
  442. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  443. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  444. int A[2], B[2], C[2];
  445. int has_A = 0, has_B = 0, has_C = 0;
  446. int mx, my;
  447. int i, j;
  448. Picture *cur_pic = s->current_picture_ptr;
  449. const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
  450. int type = cur_pic->mb_type[mb_pos];
  451. memset(A, 0, sizeof(A));
  452. memset(B, 0, sizeof(B));
  453. memset(C, 0, sizeof(C));
  454. if((r->avail_cache[5-1] & type) & mask){
  455. A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
  456. A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
  457. has_A = 1;
  458. }
  459. if((r->avail_cache[5-4] & type) & mask){
  460. B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
  461. B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
  462. has_B = 1;
  463. }
  464. if((r->avail_cache[5-2] & type) & mask){
  465. C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
  466. C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
  467. has_C = 1;
  468. }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[5-5] & type) & mask){
  469. C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
  470. C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
  471. has_C = 1;
  472. }
  473. rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
  474. mx += r->dmv[dir][0];
  475. my += r->dmv[dir][1];
  476. for(j = 0; j < 2; j++){
  477. for(i = 0; i < 2; i++){
  478. cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
  479. cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
  480. }
  481. }
  482. if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD)
  483. fill_rectangle(cur_pic->motion_val[!dir][mv_pos], 2, 2, s->b8_stride, 0, 4);
  484. }
  485. /**
  486. * motion vector prediction - RV3 version
  487. */
  488. static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
  489. {
  490. MpegEncContext *s = &r->s;
  491. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  492. int A[2] = {0}, B[2], C[2];
  493. int i, j, k;
  494. int mx, my;
  495. int avail_index = avail_indexes[0];
  496. if(r->avail_cache[avail_index - 1]){
  497. A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
  498. A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
  499. }
  500. if(r->avail_cache[avail_index - 4]){
  501. B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
  502. B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
  503. }else{
  504. B[0] = A[0];
  505. B[1] = A[1];
  506. }
  507. if(!r->avail_cache[avail_index - 4 + 2]){
  508. if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1])){
  509. C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
  510. C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
  511. }else{
  512. C[0] = A[0];
  513. C[1] = A[1];
  514. }
  515. }else{
  516. C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+2][0];
  517. C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+2][1];
  518. }
  519. mx = mid_pred(A[0], B[0], C[0]);
  520. my = mid_pred(A[1], B[1], C[1]);
  521. mx += r->dmv[0][0];
  522. my += r->dmv[0][1];
  523. for(j = 0; j < 2; j++){
  524. for(i = 0; i < 2; i++){
  525. for(k = 0; k < 2; k++){
  526. s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
  527. s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
  528. }
  529. }
  530. }
  531. }
  532. static const int chroma_coeffs[3] = { 0, 3, 5 };
  533. /**
  534. * generic motion compensation function
  535. *
  536. * @param r decoder context
  537. * @param block_type type of the current block
  538. * @param xoff horizontal offset from the start of the current block
  539. * @param yoff vertical offset from the start of the current block
  540. * @param mv_off offset to the motion vector information
  541. * @param width width of the current partition in 8x8 blocks
  542. * @param height height of the current partition in 8x8 blocks
  543. * @param dir motion compensation direction (i.e. from the last or the next reference frame)
  544. * @param thirdpel motion vectors are specified in 1/3 of pixel
  545. * @param qpel_mc a set of functions used to perform luma motion compensation
  546. * @param chroma_mc a set of functions used to perform chroma motion compensation
  547. */
  548. static inline void rv34_mc(RV34DecContext *r, const int block_type,
  549. const int xoff, const int yoff, int mv_off,
  550. const int width, const int height, int dir,
  551. const int thirdpel,
  552. qpel_mc_func (*qpel_mc)[16],
  553. h264_chroma_mc_func (*chroma_mc))
  554. {
  555. MpegEncContext *s = &r->s;
  556. uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
  557. int dxy, mx, my, umx, umy, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
  558. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
  559. int is16x16 = 1;
  560. if(thirdpel){
  561. int chroma_mx, chroma_my;
  562. mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
  563. my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
  564. lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
  565. ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
  566. chroma_mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + 1) >> 1;
  567. chroma_my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + 1) >> 1;
  568. umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
  569. umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
  570. uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
  571. uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
  572. }else{
  573. int cx, cy;
  574. mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
  575. my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
  576. lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
  577. ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
  578. cx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
  579. cy = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
  580. umx = cx >> 2;
  581. umy = cy >> 2;
  582. uvmx = (cx & 3) << 1;
  583. uvmy = (cy & 3) << 1;
  584. //due to some flaw RV40 uses the same MC compensation routine for H2V2 and H3V3
  585. if(uvmx == 6 && uvmy == 6)
  586. uvmx = uvmy = 4;
  587. }
  588. dxy = ly*4 + lx;
  589. srcY = dir ? s->next_picture_ptr->data[0] : s->last_picture_ptr->data[0];
  590. srcU = dir ? s->next_picture_ptr->data[1] : s->last_picture_ptr->data[1];
  591. srcV = dir ? s->next_picture_ptr->data[2] : s->last_picture_ptr->data[2];
  592. src_x = s->mb_x * 16 + xoff + mx;
  593. src_y = s->mb_y * 16 + yoff + my;
  594. uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
  595. uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
  596. srcY += src_y * s->linesize + src_x;
  597. srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
  598. srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
  599. if( (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4
  600. || (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4){
  601. uint8_t *uvbuf= s->edge_emu_buffer + 22 * s->linesize;
  602. srcY -= 2 + 2*s->linesize;
  603. ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+6, (height<<3)+6,
  604. src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
  605. srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
  606. ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,
  607. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  608. ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,
  609. uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  610. srcU = uvbuf;
  611. srcV = uvbuf + 16;
  612. }
  613. Y = s->dest[0] + xoff + yoff *s->linesize;
  614. U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  615. V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
  616. if(block_type == RV34_MB_P_16x8){
  617. qpel_mc[1][dxy](Y, srcY, s->linesize);
  618. Y += 8;
  619. srcY += 8;
  620. }else if(block_type == RV34_MB_P_8x16){
  621. qpel_mc[1][dxy](Y, srcY, s->linesize);
  622. Y += 8 * s->linesize;
  623. srcY += 8 * s->linesize;
  624. }
  625. is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
  626. qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
  627. chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
  628. chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
  629. }
  630. static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
  631. const int xoff, const int yoff, int mv_off,
  632. const int width, const int height, int dir)
  633. {
  634. rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30,
  635. r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
  636. : r->s.dsp.put_rv40_qpel_pixels_tab,
  637. r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
  638. : r->s.dsp.put_rv40_chroma_pixels_tab);
  639. }
  640. static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
  641. {
  642. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,
  643. r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
  644. : r->s.dsp.put_rv40_qpel_pixels_tab,
  645. r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
  646. : r->s.dsp.put_rv40_chroma_pixels_tab);
  647. rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,
  648. r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
  649. : r->s.dsp.avg_rv40_qpel_pixels_tab,
  650. r->rv30 ? r->s.dsp.avg_h264_chroma_pixels_tab
  651. : r->s.dsp.avg_rv40_chroma_pixels_tab);
  652. }
  653. static void rv34_mc_2mv_skip(RV34DecContext *r)
  654. {
  655. int i, j;
  656. for(j = 0; j < 2; j++)
  657. for(i = 0; i < 2; i++){
  658. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
  659. r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
  660. : r->s.dsp.put_rv40_qpel_pixels_tab,
  661. r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
  662. : r->s.dsp.put_rv40_chroma_pixels_tab);
  663. rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
  664. r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
  665. : r->s.dsp.avg_rv40_qpel_pixels_tab,
  666. r->rv30 ? r->s.dsp.avg_h264_chroma_pixels_tab
  667. : r->s.dsp.avg_rv40_chroma_pixels_tab);
  668. }
  669. }
  670. /** number of motion vectors in each macroblock type */
  671. static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
  672. /**
  673. * Decode motion vector differences
  674. * and perform motion vector reconstruction and motion compensation.
  675. */
  676. static int rv34_decode_mv(RV34DecContext *r, int block_type)
  677. {
  678. MpegEncContext *s = &r->s;
  679. GetBitContext *gb = &s->gb;
  680. int i, j, k, l;
  681. int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  682. int next_bt;
  683. memset(r->dmv, 0, sizeof(r->dmv));
  684. for(i = 0; i < num_mvs[block_type]; i++){
  685. r->dmv[i][0] = svq3_get_se_golomb(gb);
  686. r->dmv[i][1] = svq3_get_se_golomb(gb);
  687. }
  688. switch(block_type){
  689. case RV34_MB_TYPE_INTRA:
  690. case RV34_MB_TYPE_INTRA16x16:
  691. fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
  692. return 0;
  693. case RV34_MB_SKIP:
  694. if(s->pict_type == FF_P_TYPE){
  695. fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
  696. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  697. break;
  698. }
  699. case RV34_MB_B_DIRECT:
  700. //surprisingly, it uses motion scheme from next reference frame
  701. next_bt = s->next_picture_ptr->mb_type[s->mb_x + s->mb_y * s->mb_stride];
  702. if(IS_INTRA(next_bt))
  703. fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
  704. else
  705. for(j = 0; j < 2; j++)
  706. for(i = 0; i < 2; i++)
  707. for(k = 0; k < 2; k++)
  708. for(l = 0; l < 2; l++)
  709. 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]);
  710. if(IS_16X16(next_bt)) //we can use whole macroblock MC
  711. rv34_mc_2mv(r, block_type);
  712. else
  713. rv34_mc_2mv_skip(r);
  714. fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
  715. break;
  716. case RV34_MB_P_16x16:
  717. case RV34_MB_P_MIX16x16:
  718. rv34_pred_mv(r, block_type, 0, 0);
  719. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
  720. break;
  721. case RV34_MB_B_FORWARD:
  722. case RV34_MB_B_BACKWARD:
  723. r->dmv[1][0] = r->dmv[0][0];
  724. r->dmv[1][1] = r->dmv[0][1];
  725. if(r->rv30)
  726. rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
  727. else
  728. rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
  729. rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
  730. break;
  731. case RV34_MB_P_16x8:
  732. case RV34_MB_P_8x16:
  733. rv34_pred_mv(r, block_type, 0, 0);
  734. rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
  735. if(block_type == RV34_MB_P_16x8){
  736. rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
  737. rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
  738. }
  739. if(block_type == RV34_MB_P_8x16){
  740. rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
  741. rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
  742. }
  743. break;
  744. case RV34_MB_B_BIDIR:
  745. rv34_pred_mv_b (r, block_type, 0);
  746. rv34_pred_mv_b (r, block_type, 1);
  747. rv34_mc_2mv (r, block_type);
  748. break;
  749. case RV34_MB_P_8x8:
  750. for(i=0;i< 4;i++){
  751. rv34_pred_mv(r, block_type, i, i);
  752. rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
  753. }
  754. break;
  755. }
  756. return 0;
  757. }
  758. /** @} */ // mv group
  759. /**
  760. * @defgroup recons Macroblock reconstruction functions
  761. * @{
  762. */
  763. /** mapping of RV30/40 intra prediction types to standard H.264 types */
  764. static const int ittrans[9] = {
  765. DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
  766. VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
  767. };
  768. /** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
  769. static const int ittrans16[4] = {
  770. DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
  771. };
  772. /**
  773. * Perform 4x4 intra prediction.
  774. */
  775. static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
  776. {
  777. uint8_t *prev = dst - stride + 4;
  778. uint32_t topleft;
  779. if(!up && !left)
  780. itype = DC_128_PRED;
  781. else if(!up){
  782. if(itype == VERT_PRED) itype = HOR_PRED;
  783. if(itype == DC_PRED) itype = LEFT_DC_PRED;
  784. }else if(!left){
  785. if(itype == HOR_PRED) itype = VERT_PRED;
  786. if(itype == DC_PRED) itype = TOP_DC_PRED;
  787. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  788. }
  789. if(!down){
  790. if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
  791. if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
  792. if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
  793. }
  794. if(!right && up){
  795. topleft = dst[-stride + 3] * 0x01010101;
  796. prev = (uint8_t*)&topleft;
  797. }
  798. r->h.pred4x4[itype](dst, prev, stride);
  799. }
  800. /** add_pixels_clamped for 4x4 block */
  801. static void rv34_add_4x4_block(uint8_t *dst, int stride, DCTELEM block[64], int off)
  802. {
  803. int x, y;
  804. for(y = 0; y < 4; y++)
  805. for(x = 0; x < 4; x++)
  806. dst[x + y*stride] = av_clip_uint8(dst[x + y*stride] + block[off + x+y*8]);
  807. }
  808. static inline int adjust_pred16(int itype, int up, int left)
  809. {
  810. if(!up && !left)
  811. itype = DC_128_PRED8x8;
  812. else if(!up){
  813. if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
  814. if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
  815. if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
  816. }else if(!left){
  817. if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
  818. if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
  819. if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
  820. }
  821. return itype;
  822. }
  823. static void rv34_output_macroblock(RV34DecContext *r, int8_t *intra_types, int cbp, int is16)
  824. {
  825. MpegEncContext *s = &r->s;
  826. DSPContext *dsp = &s->dsp;
  827. int i, j;
  828. uint8_t *Y, *U, *V;
  829. int itype;
  830. int avail[6*8] = {0};
  831. int idx;
  832. // Set neighbour information.
  833. if(r->avail_cache[0])
  834. avail[0] = 1;
  835. if(r->avail_cache[1])
  836. avail[1] = avail[2] = 1;
  837. if(r->avail_cache[2])
  838. avail[3] = avail[4] = 1;
  839. if(r->avail_cache[3])
  840. avail[5] = 1;
  841. if(r->avail_cache[4])
  842. avail[8] = avail[16] = 1;
  843. if(r->avail_cache[8])
  844. avail[24] = avail[32] = 1;
  845. Y = s->dest[0];
  846. U = s->dest[1];
  847. V = s->dest[2];
  848. if(!is16){
  849. for(j = 0; j < 4; j++){
  850. idx = 9 + j*8;
  851. for(i = 0; i < 4; i++, cbp >>= 1, Y += 4, idx++){
  852. rv34_pred_4x4_block(r, Y, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
  853. avail[idx] = 1;
  854. if(cbp & 1)
  855. rv34_add_4x4_block(Y, s->linesize, s->block[(i>>1)+(j&2)], (i&1)*4+(j&1)*32);
  856. }
  857. Y += s->linesize * 4 - 4*4;
  858. intra_types += s->b4_stride;
  859. }
  860. intra_types -= s->b4_stride * 4;
  861. fill_rectangle(r->avail_cache + 5, 2, 2, 4, 0, 4);
  862. for(j = 0; j < 2; j++){
  863. idx = 5 + j*4;
  864. for(i = 0; i < 2; i++, cbp >>= 1, idx++){
  865. rv34_pred_4x4_block(r, U + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);
  866. rv34_pred_4x4_block(r, V + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);
  867. r->avail_cache[idx] = 1;
  868. if(cbp & 0x01)
  869. rv34_add_4x4_block(U + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[4], i*4+j*32);
  870. if(cbp & 0x10)
  871. rv34_add_4x4_block(V + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[5], i*4+j*32);
  872. }
  873. }
  874. }else{
  875. itype = ittrans16[intra_types[0]];
  876. itype = adjust_pred16(itype, r->avail_cache[5-4], r->avail_cache[5-1]);
  877. r->h.pred16x16[itype](Y, s->linesize);
  878. dsp->add_pixels_clamped(s->block[0], Y, s->linesize);
  879. dsp->add_pixels_clamped(s->block[1], Y + 8, s->linesize);
  880. Y += s->linesize * 8;
  881. dsp->add_pixels_clamped(s->block[2], Y, s->linesize);
  882. dsp->add_pixels_clamped(s->block[3], Y + 8, s->linesize);
  883. itype = ittrans16[intra_types[0]];
  884. if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
  885. itype = adjust_pred16(itype, r->avail_cache[5-4], r->avail_cache[5-1]);
  886. r->h.pred8x8[itype](U, s->uvlinesize);
  887. dsp->add_pixels_clamped(s->block[4], U, s->uvlinesize);
  888. r->h.pred8x8[itype](V, s->uvlinesize);
  889. dsp->add_pixels_clamped(s->block[5], V, s->uvlinesize);
  890. }
  891. }
  892. /** @} */ // recons group
  893. /**
  894. * @addtogroup bitstream
  895. * Decode macroblock header and return CBP in case of success, -1 otherwise.
  896. */
  897. static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
  898. {
  899. MpegEncContext *s = &r->s;
  900. GetBitContext *gb = &s->gb;
  901. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  902. int i, t;
  903. if(!r->si.type){
  904. r->is16 = get_bits1(gb);
  905. if(!r->is16 && !r->rv30){
  906. if(!get_bits1(gb))
  907. av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
  908. }
  909. s->current_picture_ptr->mb_type[mb_pos] = r->is16 ? MB_TYPE_INTRA16x16 : MB_TYPE_INTRA;
  910. r->block_type = r->is16 ? RV34_MB_TYPE_INTRA16x16 : RV34_MB_TYPE_INTRA;
  911. }else{
  912. r->block_type = r->decode_mb_info(r);
  913. if(r->block_type == -1)
  914. return -1;
  915. s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
  916. r->mb_type[mb_pos] = r->block_type;
  917. if(r->block_type == RV34_MB_SKIP){
  918. if(s->pict_type == FF_P_TYPE)
  919. r->mb_type[mb_pos] = RV34_MB_P_16x16;
  920. if(s->pict_type == FF_B_TYPE)
  921. r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
  922. }
  923. r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
  924. rv34_decode_mv(r, r->block_type);
  925. if(r->block_type == RV34_MB_SKIP){
  926. fill_rectangle(intra_types, 4, 4, s->b4_stride, 0, sizeof(intra_types[0]));
  927. return 0;
  928. }
  929. r->chroma_vlc = 1;
  930. r->luma_vlc = 0;
  931. }
  932. if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
  933. if(r->is16){
  934. t = get_bits(gb, 2);
  935. fill_rectangle(intra_types, 4, 4, s->b4_stride, t, sizeof(intra_types[0]));
  936. r->luma_vlc = 2;
  937. }else{
  938. if(r->decode_intra_types(r, gb, intra_types) < 0)
  939. return -1;
  940. r->luma_vlc = 1;
  941. }
  942. r->chroma_vlc = 0;
  943. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  944. }else{
  945. for(i = 0; i < 16; i++)
  946. intra_types[(i & 3) + (i>>2) * s->b4_stride] = 0;
  947. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  948. if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
  949. r->is16 = 1;
  950. r->chroma_vlc = 1;
  951. r->luma_vlc = 2;
  952. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
  953. }
  954. }
  955. return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
  956. }
  957. /**
  958. * @addtogroup recons
  959. * @{
  960. */
  961. /**
  962. * mask for retrieving all bits in coded block pattern
  963. * corresponding to one 8x8 block
  964. */
  965. #define LUMA_CBP_BLOCK_MASK 0x33
  966. #define U_CBP_MASK 0x0F0000
  967. #define V_CBP_MASK 0xF00000
  968. static void rv34_apply_differences(RV34DecContext *r, int cbp)
  969. {
  970. static const int shifts[4] = { 0, 2, 8, 10 };
  971. MpegEncContext *s = &r->s;
  972. int i;
  973. for(i = 0; i < 4; i++)
  974. if((cbp & (LUMA_CBP_BLOCK_MASK << shifts[i])) || r->block_type == RV34_MB_P_MIX16x16)
  975. s->dsp.add_pixels_clamped(s->block[i], s->dest[0] + (i & 1)*8 + (i&2)*4*s->linesize, s->linesize);
  976. if(cbp & U_CBP_MASK)
  977. s->dsp.add_pixels_clamped(s->block[4], s->dest[1], s->uvlinesize);
  978. if(cbp & V_CBP_MASK)
  979. s->dsp.add_pixels_clamped(s->block[5], s->dest[2], s->uvlinesize);
  980. }
  981. static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
  982. {
  983. int d;
  984. d = motion_val[0][0] - motion_val[-step][0];
  985. if(d < -3 || d > 3)
  986. return 1;
  987. d = motion_val[0][1] - motion_val[-step][1];
  988. if(d < -3 || d > 3)
  989. return 1;
  990. return 0;
  991. }
  992. static int rv34_set_deblock_coef(RV34DecContext *r)
  993. {
  994. MpegEncContext *s = &r->s;
  995. int hmvmask = 0, vmvmask = 0, i, j;
  996. int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
  997. int16_t (*motion_val)[2] = s->current_picture_ptr->motion_val[0][midx];
  998. for(j = 0; j < 16; j += 8){
  999. for(i = 0; i < 2; i++){
  1000. if(is_mv_diff_gt_3(motion_val + i, 1))
  1001. vmvmask |= 0x11 << (j + i*2);
  1002. if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
  1003. hmvmask |= 0x03 << (j + i*2);
  1004. }
  1005. motion_val += s->b8_stride;
  1006. }
  1007. if(s->first_slice_line)
  1008. hmvmask &= ~0x000F;
  1009. if(!s->mb_x)
  1010. vmvmask &= ~0x1111;
  1011. if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
  1012. vmvmask |= (vmvmask & 0x4444) >> 1;
  1013. hmvmask |= (hmvmask & 0x0F00) >> 4;
  1014. if(s->mb_x)
  1015. r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
  1016. if(!s->first_slice_line)
  1017. r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
  1018. }
  1019. return hmvmask | vmvmask;
  1020. }
  1021. static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types)
  1022. {
  1023. MpegEncContext *s = &r->s;
  1024. GetBitContext *gb = &s->gb;
  1025. int cbp, cbp2;
  1026. int i, blknum, blkoff;
  1027. DCTELEM block16[64];
  1028. int luma_dc_quant;
  1029. int dist;
  1030. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1031. // Calculate which neighbours are available. Maybe it's worth optimizing too.
  1032. memset(r->avail_cache, 0, sizeof(r->avail_cache));
  1033. fill_rectangle(r->avail_cache + 5, 2, 2, 4, 1, 4);
  1034. dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
  1035. if(s->mb_x && dist)
  1036. r->avail_cache[4] =
  1037. r->avail_cache[8] = s->current_picture_ptr->mb_type[mb_pos - 1];
  1038. if(dist >= s->mb_width)
  1039. r->avail_cache[1] =
  1040. r->avail_cache[2] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
  1041. if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
  1042. r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
  1043. if(s->mb_x && dist > s->mb_width)
  1044. r->avail_cache[0] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
  1045. s->qscale = r->si.quant;
  1046. cbp = cbp2 = rv34_decode_mb_header(r, intra_types);
  1047. r->cbp_luma [mb_pos] = cbp;
  1048. r->cbp_chroma[mb_pos] = cbp >> 16;
  1049. if(s->pict_type == FF_I_TYPE)
  1050. r->deblock_coefs[mb_pos] = 0xFFFF;
  1051. else
  1052. r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
  1053. s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
  1054. if(cbp == -1)
  1055. return -1;
  1056. luma_dc_quant = r->block_type == RV34_MB_P_MIX16x16 ? r->luma_dc_quant_p[s->qscale] : r->luma_dc_quant_i[s->qscale];
  1057. if(r->is16){
  1058. memset(block16, 0, sizeof(block16));
  1059. rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0);
  1060. rv34_dequant4x4_16x16(block16, rv34_qscale_tab[luma_dc_quant],rv34_qscale_tab[s->qscale]);
  1061. rv34_inv_transform_noround(block16);
  1062. }
  1063. for(i = 0; i < 16; i++, cbp >>= 1){
  1064. if(!r->is16 && !(cbp & 1)) continue;
  1065. blknum = ((i & 2) >> 1) + ((i & 8) >> 2);
  1066. blkoff = ((i & 1) << 2) + ((i & 4) << 3);
  1067. if(cbp & 1)
  1068. rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->luma_vlc, 0);
  1069. rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[s->qscale],rv34_qscale_tab[s->qscale]);
  1070. if(r->is16) //FIXME: optimize
  1071. s->block[blknum][blkoff] = block16[(i & 3) | ((i & 0xC) << 1)];
  1072. rv34_inv_transform(s->block[blknum] + blkoff);
  1073. }
  1074. if(r->block_type == RV34_MB_P_MIX16x16)
  1075. r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
  1076. for(; i < 24; i++, cbp >>= 1){
  1077. if(!(cbp & 1)) continue;
  1078. blknum = ((i & 4) >> 2) + 4;
  1079. blkoff = ((i & 1) << 2) + ((i & 2) << 4);
  1080. rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->chroma_vlc, 1);
  1081. rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]],rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]]);
  1082. rv34_inv_transform(s->block[blknum] + blkoff);
  1083. }
  1084. if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos]))
  1085. rv34_output_macroblock(r, intra_types, cbp2, r->is16);
  1086. else
  1087. rv34_apply_differences(r, cbp2);
  1088. return 0;
  1089. }
  1090. static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
  1091. {
  1092. int bits;
  1093. if(s->mb_y >= s->mb_height)
  1094. return 1;
  1095. if(!s->mb_num_left)
  1096. return 1;
  1097. if(r->s.mb_skip_run > 1)
  1098. return 0;
  1099. bits = r->bits - get_bits_count(&s->gb);
  1100. if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits)))
  1101. return 1;
  1102. return 0;
  1103. }
  1104. static inline int slice_compare(SliceInfo *si1, SliceInfo *si2)
  1105. {
  1106. return si1->type != si2->type ||
  1107. si1->start >= si2->start ||
  1108. si1->width != si2->width ||
  1109. si1->height != si2->height||
  1110. si1->pts != si2->pts;
  1111. }
  1112. static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
  1113. {
  1114. MpegEncContext *s = &r->s;
  1115. GetBitContext *gb = &s->gb;
  1116. int mb_pos;
  1117. int res;
  1118. init_get_bits(&r->s.gb, buf, buf_size*8);
  1119. res = r->parse_slice_header(r, gb, &r->si);
  1120. if(res < 0){
  1121. av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
  1122. return -1;
  1123. }
  1124. if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
  1125. if(s->width != r->si.width || s->height != r->si.height){
  1126. av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
  1127. MPV_common_end(s);
  1128. s->width = r->si.width;
  1129. s->height = r->si.height;
  1130. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1131. if(MPV_common_init(s) < 0)
  1132. return -1;
  1133. r->intra_types_hist = av_realloc(r->intra_types_hist, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1134. r->intra_types = r->intra_types_hist + s->b4_stride * 4;
  1135. r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
  1136. r->cbp_luma = av_realloc(r->cbp_luma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
  1137. r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
  1138. r->deblock_coefs = av_realloc(r->deblock_coefs, r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
  1139. }
  1140. s->pict_type = r->si.type ? r->si.type : FF_I_TYPE;
  1141. if(MPV_frame_start(s, s->avctx) < 0)
  1142. return -1;
  1143. ff_er_frame_start(s);
  1144. r->cur_pts = r->si.pts;
  1145. if(s->pict_type != FF_B_TYPE){
  1146. r->last_pts = r->next_pts;
  1147. r->next_pts = r->cur_pts;
  1148. }
  1149. s->mb_x = s->mb_y = 0;
  1150. } else {
  1151. int slice_type = r->si.type ? r->si.type : FF_I_TYPE;
  1152. if (slice_type != s->pict_type) {
  1153. av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
  1154. return AVERROR_INVALIDDATA;
  1155. }
  1156. if (s->width != r->si.width || s->height != r->si.height) {
  1157. av_log(s->avctx, AV_LOG_ERROR, "Size mismatch\n");
  1158. return AVERROR_INVALIDDATA;
  1159. }
  1160. }
  1161. r->si.end = end;
  1162. s->qscale = r->si.quant;
  1163. r->bits = buf_size*8;
  1164. s->mb_num_left = r->si.end - r->si.start;
  1165. r->s.mb_skip_run = 0;
  1166. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  1167. if(r->si.start != mb_pos){
  1168. av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
  1169. s->mb_x = r->si.start % s->mb_width;
  1170. s->mb_y = r->si.start / s->mb_width;
  1171. }
  1172. memset(r->intra_types_hist, -1, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1173. s->first_slice_line = 1;
  1174. s->resync_mb_x= s->mb_x;
  1175. s->resync_mb_y= s->mb_y;
  1176. ff_init_block_index(s);
  1177. while(!check_slice_end(r, s)) {
  1178. ff_update_block_index(s);
  1179. s->dsp.clear_blocks(s->block[0]);
  1180. if(rv34_decode_macroblock(r, r->intra_types + s->mb_x * 4 + 1) < 0){
  1181. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  1182. return -1;
  1183. }
  1184. if (++s->mb_x == s->mb_width) {
  1185. s->mb_x = 0;
  1186. s->mb_y++;
  1187. ff_init_block_index(s);
  1188. memmove(r->intra_types_hist, r->intra_types, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
  1189. memset(r->intra_types, -1, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
  1190. if(r->loop_filter && s->mb_y >= 2)
  1191. r->loop_filter(r, s->mb_y - 2);
  1192. }
  1193. if(s->mb_x == s->resync_mb_x)
  1194. s->first_slice_line=0;
  1195. s->mb_num_left--;
  1196. }
  1197. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
  1198. return s->mb_y == s->mb_height;
  1199. }
  1200. /** @} */ // recons group end
  1201. /**
  1202. * Initialize decoder.
  1203. */
  1204. av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
  1205. {
  1206. RV34DecContext *r = avctx->priv_data;
  1207. MpegEncContext *s = &r->s;
  1208. MPV_decode_defaults(s);
  1209. s->avctx= avctx;
  1210. s->out_format = FMT_H263;
  1211. s->codec_id= avctx->codec_id;
  1212. s->width = avctx->width;
  1213. s->height = avctx->height;
  1214. r->s.avctx = avctx;
  1215. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  1216. r->s.flags |= CODEC_FLAG_EMU_EDGE;
  1217. avctx->pix_fmt = PIX_FMT_YUV420P;
  1218. avctx->has_b_frames = 1;
  1219. s->low_delay = 0;
  1220. if (MPV_common_init(s) < 0)
  1221. return -1;
  1222. ff_h264_pred_init(&r->h, CODEC_ID_RV40);
  1223. r->intra_types_hist = av_malloc(s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
  1224. r->intra_types = r->intra_types_hist + s->b4_stride * 4;
  1225. r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
  1226. r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
  1227. r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
  1228. r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
  1229. if(!intra_vlcs[0].cbppattern[0].bits)
  1230. rv34_init_tables();
  1231. return 0;
  1232. }
  1233. static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
  1234. {
  1235. if(avctx->slice_count) return avctx->slice_offset[n];
  1236. else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
  1237. }
  1238. int ff_rv34_decode_frame(AVCodecContext *avctx,
  1239. void *data, int *data_size,
  1240. const uint8_t *buf, int buf_size)
  1241. {
  1242. RV34DecContext *r = avctx->priv_data;
  1243. MpegEncContext *s = &r->s;
  1244. AVFrame *pict = data;
  1245. SliceInfo si;
  1246. int i;
  1247. int slice_count;
  1248. const uint8_t *slices_hdr = NULL;
  1249. int last = 0;
  1250. /* no supplementary picture */
  1251. if (buf_size == 0) {
  1252. /* special case for last picture */
  1253. if (s->low_delay==0 && s->next_picture_ptr) {
  1254. *pict= *(AVFrame*)s->next_picture_ptr;
  1255. s->next_picture_ptr= NULL;
  1256. *data_size = sizeof(AVFrame);
  1257. }
  1258. return 0;
  1259. }
  1260. if(!avctx->slice_count){
  1261. slice_count = (*buf++) + 1;
  1262. slices_hdr = buf + 4;
  1263. buf += 8 * slice_count;
  1264. }else
  1265. slice_count = avctx->slice_count;
  1266. //parse first slice header to check whether this frame can be decoded
  1267. if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
  1268. get_slice_offset(avctx, slices_hdr, 0) > buf_size){
  1269. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1270. return -1;
  1271. }
  1272. init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), buf_size-get_slice_offset(avctx, slices_hdr, 0));
  1273. if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
  1274. av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
  1275. return -1;
  1276. }
  1277. if((!s->last_picture_ptr || !s->last_picture_ptr->data[0]) && si.type == FF_B_TYPE)
  1278. return -1;
  1279. /* skip b frames if we are in a hurry */
  1280. if(avctx->hurry_up && si.type==FF_B_TYPE) return buf_size;
  1281. if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==FF_B_TYPE)
  1282. || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=FF_I_TYPE)
  1283. || avctx->skip_frame >= AVDISCARD_ALL)
  1284. return buf_size;
  1285. /* skip everything if we are in a hurry>=5 */
  1286. if(avctx->hurry_up>=5)
  1287. return buf_size;
  1288. for(i=0; i<slice_count; i++){
  1289. int offset= get_slice_offset(avctx, slices_hdr, i);
  1290. int size;
  1291. if(i+1 == slice_count)
  1292. size= buf_size - offset;
  1293. else
  1294. size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
  1295. if(offset < 0 || offset > buf_size || size < 0){
  1296. av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
  1297. break;
  1298. }
  1299. r->si.end = s->mb_width * s->mb_height;
  1300. if(i+1 < slice_count){
  1301. 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);
  1302. if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
  1303. if(i+2 < slice_count)
  1304. size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
  1305. else
  1306. size = buf_size - offset;
  1307. }else
  1308. r->si.end = si.start;
  1309. }
  1310. last = rv34_decode_slice(r, r->si.end, buf + offset, size);
  1311. s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
  1312. if(last)
  1313. break;
  1314. }
  1315. if(last && s->current_picture_ptr){
  1316. if(r->loop_filter)
  1317. r->loop_filter(r, s->mb_height - 1);
  1318. ff_er_frame_end(s);
  1319. MPV_frame_end(s);
  1320. if (s->pict_type == FF_B_TYPE || s->low_delay) {
  1321. *pict= *(AVFrame*)s->current_picture_ptr;
  1322. } else if (s->last_picture_ptr != NULL) {
  1323. *pict= *(AVFrame*)s->last_picture_ptr;
  1324. }
  1325. if(s->last_picture_ptr || s->low_delay){
  1326. *data_size = sizeof(AVFrame);
  1327. ff_print_debug_info(s, pict);
  1328. }
  1329. s->current_picture_ptr= NULL; //so we can detect if frame_end wasnt called (find some nicer solution...)
  1330. }
  1331. return buf_size;
  1332. }
  1333. av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
  1334. {
  1335. RV34DecContext *r = avctx->priv_data;
  1336. MPV_common_end(&r->s);
  1337. av_freep(&r->intra_types_hist);
  1338. r->intra_types = NULL;
  1339. av_freep(&r->mb_type);
  1340. av_freep(&r->cbp_luma);
  1341. av_freep(&r->cbp_chroma);
  1342. av_freep(&r->deblock_coefs);
  1343. return 0;
  1344. }