rv34.c 54 KB

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