vp3.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. /*
  2. * Copyright (C) 2003-2004 the ffmpeg project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file libavcodec/vp3.c
  22. * On2 VP3 Video Decoder
  23. *
  24. * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
  25. * For more information about the VP3 coding process, visit:
  26. * http://wiki.multimedia.cx/index.php?title=On2_VP3
  27. *
  28. * Theora decoder by Alex Beregszaszi
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "get_bits.h"
  36. #include "vp3data.h"
  37. #include "xiph.h"
  38. #define FRAGMENT_PIXELS 8
  39. static av_cold int vp3_decode_end(AVCodecContext *avctx);
  40. typedef struct Coeff {
  41. struct Coeff *next;
  42. DCTELEM coeff;
  43. uint8_t index;
  44. } Coeff;
  45. //FIXME split things out into their own arrays
  46. typedef struct Vp3Fragment {
  47. Coeff *next_coeff;
  48. uint8_t coding_method;
  49. int8_t motion_x;
  50. int8_t motion_y;
  51. uint8_t qpi;
  52. } Vp3Fragment;
  53. #define SB_NOT_CODED 0
  54. #define SB_PARTIALLY_CODED 1
  55. #define SB_FULLY_CODED 2
  56. #define MODE_INTER_NO_MV 0
  57. #define MODE_INTRA 1
  58. #define MODE_INTER_PLUS_MV 2
  59. #define MODE_INTER_LAST_MV 3
  60. #define MODE_INTER_PRIOR_LAST 4
  61. #define MODE_USING_GOLDEN 5
  62. #define MODE_GOLDEN_MV 6
  63. #define MODE_INTER_FOURMV 7
  64. #define CODING_MODE_COUNT 8
  65. /* special internal mode */
  66. #define MODE_COPY 8
  67. /* There are 6 preset schemes, plus a free-form scheme */
  68. static const int ModeAlphabet[6][CODING_MODE_COUNT] =
  69. {
  70. /* scheme 1: Last motion vector dominates */
  71. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  72. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  73. MODE_INTRA, MODE_USING_GOLDEN,
  74. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  75. /* scheme 2 */
  76. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  77. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  78. MODE_INTRA, MODE_USING_GOLDEN,
  79. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  80. /* scheme 3 */
  81. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  82. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  83. MODE_INTRA, MODE_USING_GOLDEN,
  84. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  85. /* scheme 4 */
  86. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  87. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  88. MODE_INTRA, MODE_USING_GOLDEN,
  89. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  90. /* scheme 5: No motion vector dominates */
  91. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  92. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  93. MODE_INTRA, MODE_USING_GOLDEN,
  94. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  95. /* scheme 6 */
  96. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  97. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  98. MODE_INTER_PLUS_MV, MODE_INTRA,
  99. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  100. };
  101. #define MIN_DEQUANT_VAL 2
  102. typedef struct Vp3DecodeContext {
  103. AVCodecContext *avctx;
  104. int theora, theora_tables;
  105. int version;
  106. int width, height;
  107. AVFrame golden_frame;
  108. AVFrame last_frame;
  109. AVFrame current_frame;
  110. int keyframe;
  111. DSPContext dsp;
  112. int flipped_image;
  113. int last_slice_end;
  114. int qps[3];
  115. int nqps;
  116. int last_qps[3];
  117. int superblock_count;
  118. int y_superblock_width;
  119. int y_superblock_height;
  120. int c_superblock_width;
  121. int c_superblock_height;
  122. int u_superblock_start;
  123. int v_superblock_start;
  124. unsigned char *superblock_coding;
  125. int macroblock_count;
  126. int macroblock_width;
  127. int macroblock_height;
  128. int fragment_count;
  129. int fragment_width;
  130. int fragment_height;
  131. Vp3Fragment *all_fragments;
  132. uint8_t *coeff_counts;
  133. Coeff *coeffs;
  134. Coeff *next_coeff;
  135. int fragment_start[3];
  136. int data_offset[3];
  137. ScanTable scantable;
  138. /* tables */
  139. uint16_t coded_dc_scale_factor[64];
  140. uint32_t coded_ac_scale_factor[64];
  141. uint8_t base_matrix[384][64];
  142. uint8_t qr_count[2][3];
  143. uint8_t qr_size [2][3][64];
  144. uint16_t qr_base[2][3][64];
  145. /* this is a list of indexes into the all_fragments array indicating
  146. * which of the fragments are coded */
  147. int *coded_fragment_list;
  148. int coded_fragment_list_index;
  149. /* track which fragments have already been decoded; called 'fast'
  150. * because this data structure avoids having to iterate through every
  151. * fragment in coded_fragment_list; once a fragment has been fully
  152. * decoded, it is removed from this list */
  153. int *fast_fragment_list;
  154. int fragment_list_y_head;
  155. int fragment_list_c_head;
  156. VLC dc_vlc[16];
  157. VLC ac_vlc_1[16];
  158. VLC ac_vlc_2[16];
  159. VLC ac_vlc_3[16];
  160. VLC ac_vlc_4[16];
  161. VLC superblock_run_length_vlc;
  162. VLC fragment_run_length_vlc;
  163. VLC mode_code_vlc;
  164. VLC motion_vector_vlc;
  165. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  166. * index into them */
  167. DECLARE_ALIGNED_16(int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane]
  168. /* This table contains superblock_count * 16 entries. Each set of 16
  169. * numbers corresponds to the fragment indexes 0..15 of the superblock.
  170. * An entry will be -1 to indicate that no entry corresponds to that
  171. * index. */
  172. int *superblock_fragments;
  173. /* This is an array that indicates how a particular macroblock
  174. * is coded. */
  175. unsigned char *macroblock_coding;
  176. int first_coded_y_fragment;
  177. int first_coded_c_fragment;
  178. int last_coded_y_fragment;
  179. int last_coded_c_fragment;
  180. uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
  181. int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
  182. /* Huffman decode */
  183. int hti;
  184. unsigned int hbits;
  185. int entries;
  186. int huff_code_size;
  187. uint16_t huffman_table[80][32][2];
  188. uint8_t filter_limit_values[64];
  189. DECLARE_ALIGNED_8(int, bounding_values_array)[256+2];
  190. } Vp3DecodeContext;
  191. /************************************************************************
  192. * VP3 specific functions
  193. ************************************************************************/
  194. /*
  195. * This function sets up all of the various blocks mappings:
  196. * superblocks <-> fragments, macroblocks <-> fragments,
  197. * superblocks <-> macroblocks
  198. *
  199. * Returns 0 is successful; returns 1 if *anything* went wrong.
  200. */
  201. static int init_block_mapping(Vp3DecodeContext *s)
  202. {
  203. int i, j;
  204. signed int hilbert_walk_mb[4];
  205. int current_fragment = 0;
  206. int current_width = 0;
  207. int current_height = 0;
  208. int right_edge = 0;
  209. int bottom_edge = 0;
  210. int superblock_row_inc = 0;
  211. int mapping_index = 0;
  212. int current_macroblock;
  213. int c_fragment;
  214. static const signed char travel_width[16] = {
  215. 1, 1, 0, -1,
  216. 0, 0, 1, 0,
  217. 1, 0, 1, 0,
  218. 0, -1, 0, 1
  219. };
  220. static const signed char travel_height[16] = {
  221. 0, 0, 1, 0,
  222. 1, 1, 0, -1,
  223. 0, 1, 0, -1,
  224. -1, 0, -1, 0
  225. };
  226. hilbert_walk_mb[0] = 1;
  227. hilbert_walk_mb[1] = s->macroblock_width;
  228. hilbert_walk_mb[2] = 1;
  229. hilbert_walk_mb[3] = -s->macroblock_width;
  230. /* iterate through each superblock (all planes) and map the fragments */
  231. for (i = 0; i < s->superblock_count; i++) {
  232. /* time to re-assign the limits? */
  233. if (i == 0) {
  234. /* start of Y superblocks */
  235. right_edge = s->fragment_width;
  236. bottom_edge = s->fragment_height;
  237. current_width = -1;
  238. current_height = 0;
  239. superblock_row_inc = 3 * s->fragment_width -
  240. (s->y_superblock_width * 4 - s->fragment_width);
  241. /* the first operation for this variable is to advance by 1 */
  242. current_fragment = -1;
  243. } else if (i == s->u_superblock_start) {
  244. /* start of U superblocks */
  245. right_edge = s->fragment_width / 2;
  246. bottom_edge = s->fragment_height / 2;
  247. current_width = -1;
  248. current_height = 0;
  249. superblock_row_inc = 3 * (s->fragment_width / 2) -
  250. (s->c_superblock_width * 4 - s->fragment_width / 2);
  251. /* the first operation for this variable is to advance by 1 */
  252. current_fragment = s->fragment_start[1] - 1;
  253. } else if (i == s->v_superblock_start) {
  254. /* start of V superblocks */
  255. right_edge = s->fragment_width / 2;
  256. bottom_edge = s->fragment_height / 2;
  257. current_width = -1;
  258. current_height = 0;
  259. superblock_row_inc = 3 * (s->fragment_width / 2) -
  260. (s->c_superblock_width * 4 - s->fragment_width / 2);
  261. /* the first operation for this variable is to advance by 1 */
  262. current_fragment = s->fragment_start[2] - 1;
  263. }
  264. if (current_width >= right_edge - 1) {
  265. /* reset width and move to next superblock row */
  266. current_width = -1;
  267. current_height += 4;
  268. /* fragment is now at the start of a new superblock row */
  269. current_fragment += superblock_row_inc;
  270. }
  271. /* iterate through all 16 fragments in a superblock */
  272. for (j = 0; j < 16; j++) {
  273. current_fragment += travel_width[j] + right_edge * travel_height[j];
  274. current_width += travel_width[j];
  275. current_height += travel_height[j];
  276. /* check if the fragment is in bounds */
  277. if ((current_width < right_edge) &&
  278. (current_height < bottom_edge)) {
  279. s->superblock_fragments[mapping_index] = current_fragment;
  280. } else {
  281. s->superblock_fragments[mapping_index] = -1;
  282. }
  283. mapping_index++;
  284. }
  285. }
  286. return 0; /* successful path out */
  287. }
  288. /*
  289. * This function wipes out all of the fragment data.
  290. */
  291. static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
  292. {
  293. int i;
  294. /* zero out all of the fragment information */
  295. s->coded_fragment_list_index = 0;
  296. for (i = 0; i < s->fragment_count; i++) {
  297. s->coeff_counts[i] = 0;
  298. s->all_fragments[i].motion_x = 127;
  299. s->all_fragments[i].motion_y = 127;
  300. s->all_fragments[i].next_coeff= NULL;
  301. s->all_fragments[i].qpi = 0;
  302. s->coeffs[i].index=
  303. s->coeffs[i].coeff=0;
  304. s->coeffs[i].next= NULL;
  305. }
  306. }
  307. /*
  308. * This function sets up the dequantization tables used for a particular
  309. * frame.
  310. */
  311. static void init_dequantizer(Vp3DecodeContext *s, int qpi)
  312. {
  313. int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
  314. int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
  315. int i, plane, inter, qri, bmi, bmj, qistart;
  316. for(inter=0; inter<2; inter++){
  317. for(plane=0; plane<3; plane++){
  318. int sum=0;
  319. for(qri=0; qri<s->qr_count[inter][plane]; qri++){
  320. sum+= s->qr_size[inter][plane][qri];
  321. if(s->qps[qpi] <= sum)
  322. break;
  323. }
  324. qistart= sum - s->qr_size[inter][plane][qri];
  325. bmi= s->qr_base[inter][plane][qri ];
  326. bmj= s->qr_base[inter][plane][qri+1];
  327. for(i=0; i<64; i++){
  328. int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
  329. - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
  330. + s->qr_size[inter][plane][qri])
  331. / (2*s->qr_size[inter][plane][qri]);
  332. int qmin= 8<<(inter + !i);
  333. int qscale= i ? ac_scale_factor : dc_scale_factor;
  334. s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
  335. }
  336. // all DC coefficients use the same quant so as not to interfere with DC prediction
  337. s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
  338. }
  339. }
  340. memset(s->qscale_table, (FFMAX(s->qmat[0][0][0][1], s->qmat[0][0][1][1])+8)/16, 512); //FIXME finetune
  341. }
  342. /*
  343. * This function initializes the loop filter boundary limits if the frame's
  344. * quality index is different from the previous frame's.
  345. *
  346. * The filter_limit_values may not be larger than 127.
  347. */
  348. static void init_loop_filter(Vp3DecodeContext *s)
  349. {
  350. int *bounding_values= s->bounding_values_array+127;
  351. int filter_limit;
  352. int x;
  353. int value;
  354. filter_limit = s->filter_limit_values[s->qps[0]];
  355. /* set up the bounding values */
  356. memset(s->bounding_values_array, 0, 256 * sizeof(int));
  357. for (x = 0; x < filter_limit; x++) {
  358. bounding_values[-x] = -x;
  359. bounding_values[x] = x;
  360. }
  361. for (x = value = filter_limit; x < 128 && value; x++, value--) {
  362. bounding_values[ x] = value;
  363. bounding_values[-x] = -value;
  364. }
  365. if (value)
  366. bounding_values[128] = value;
  367. bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
  368. }
  369. /*
  370. * This function unpacks all of the superblock/macroblock/fragment coding
  371. * information from the bitstream.
  372. */
  373. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  374. {
  375. int bit = 0;
  376. int current_superblock = 0;
  377. int current_run = 0;
  378. int decode_fully_flags = 0;
  379. int decode_partial_blocks = 0;
  380. int first_c_fragment_seen;
  381. int i, j;
  382. int current_fragment;
  383. if (s->keyframe) {
  384. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  385. } else {
  386. /* unpack the list of partially-coded superblocks */
  387. bit = get_bits1(gb);
  388. while (current_superblock < s->superblock_count) {
  389. current_run = get_vlc2(gb,
  390. s->superblock_run_length_vlc.table, 6, 2) + 1;
  391. if (current_run == 34)
  392. current_run += get_bits(gb, 12);
  393. if (current_superblock + current_run > s->superblock_count) {
  394. av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
  395. return -1;
  396. }
  397. memset(s->superblock_coding + current_superblock, bit, current_run);
  398. current_superblock += current_run;
  399. /* if any of the superblocks are not partially coded, flag
  400. * a boolean to decode the list of fully-coded superblocks */
  401. if (bit == 0) {
  402. decode_fully_flags = 1;
  403. } else {
  404. /* make a note of the fact that there are partially coded
  405. * superblocks */
  406. decode_partial_blocks = 1;
  407. }
  408. bit ^= 1;
  409. }
  410. /* unpack the list of fully coded superblocks if any of the blocks were
  411. * not marked as partially coded in the previous step */
  412. if (decode_fully_flags) {
  413. current_superblock = 0;
  414. current_run = 0;
  415. bit = get_bits1(gb);
  416. /* toggle the bit because as soon as the first run length is
  417. * fetched the bit will be toggled again */
  418. bit ^= 1;
  419. while (current_superblock < s->superblock_count) {
  420. /* skip any superblocks already marked as partially coded */
  421. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  422. if (current_run-- == 0) {
  423. bit ^= 1;
  424. current_run = get_vlc2(gb,
  425. s->superblock_run_length_vlc.table, 6, 2);
  426. if (current_run == 33)
  427. current_run += get_bits(gb, 12);
  428. }
  429. s->superblock_coding[current_superblock] = 2*bit;
  430. }
  431. current_superblock++;
  432. }
  433. }
  434. /* if there were partial blocks, initialize bitstream for
  435. * unpacking fragment codings */
  436. if (decode_partial_blocks) {
  437. current_run = 0;
  438. bit = get_bits1(gb);
  439. /* toggle the bit because as soon as the first run length is
  440. * fetched the bit will be toggled again */
  441. bit ^= 1;
  442. }
  443. }
  444. /* figure out which fragments are coded; iterate through each
  445. * superblock (all planes) */
  446. s->coded_fragment_list_index = 0;
  447. s->next_coeff= s->coeffs + s->fragment_count;
  448. s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
  449. s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
  450. first_c_fragment_seen = 0;
  451. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  452. for (i = 0; i < s->superblock_count; i++) {
  453. /* iterate through all 16 fragments in a superblock */
  454. for (j = 0; j < 16; j++) {
  455. /* if the fragment is in bounds, check its coding status */
  456. current_fragment = s->superblock_fragments[i * 16 + j];
  457. if (current_fragment >= s->fragment_count) {
  458. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
  459. current_fragment, s->fragment_count);
  460. return 1;
  461. }
  462. if (current_fragment != -1) {
  463. if (s->superblock_coding[i] == SB_NOT_CODED) {
  464. /* copy all the fragments from the prior frame */
  465. s->all_fragments[current_fragment].coding_method =
  466. MODE_COPY;
  467. } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  468. /* fragment may or may not be coded; this is the case
  469. * that cares about the fragment coding runs */
  470. if (current_run-- == 0) {
  471. bit ^= 1;
  472. current_run = get_vlc2(gb,
  473. s->fragment_run_length_vlc.table, 5, 2);
  474. }
  475. if (bit) {
  476. /* default mode; actual mode will be decoded in
  477. * the next phase */
  478. s->all_fragments[current_fragment].coding_method =
  479. MODE_INTER_NO_MV;
  480. s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
  481. s->coded_fragment_list[s->coded_fragment_list_index] =
  482. current_fragment;
  483. if ((current_fragment >= s->fragment_start[1]) &&
  484. (s->last_coded_y_fragment == -1) &&
  485. (!first_c_fragment_seen)) {
  486. s->first_coded_c_fragment = s->coded_fragment_list_index;
  487. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  488. first_c_fragment_seen = 1;
  489. }
  490. s->coded_fragment_list_index++;
  491. } else {
  492. /* not coded; copy this fragment from the prior frame */
  493. s->all_fragments[current_fragment].coding_method =
  494. MODE_COPY;
  495. }
  496. } else {
  497. /* fragments are fully coded in this superblock; actual
  498. * coding will be determined in next step */
  499. s->all_fragments[current_fragment].coding_method =
  500. MODE_INTER_NO_MV;
  501. s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
  502. s->coded_fragment_list[s->coded_fragment_list_index] =
  503. current_fragment;
  504. if ((current_fragment >= s->fragment_start[1]) &&
  505. (s->last_coded_y_fragment == -1) &&
  506. (!first_c_fragment_seen)) {
  507. s->first_coded_c_fragment = s->coded_fragment_list_index;
  508. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  509. first_c_fragment_seen = 1;
  510. }
  511. s->coded_fragment_list_index++;
  512. }
  513. }
  514. }
  515. }
  516. if (!first_c_fragment_seen)
  517. /* only Y fragments coded in this frame */
  518. s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
  519. else
  520. /* end the list of coded C fragments */
  521. s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
  522. for (i = 0; i < s->fragment_count - 1; i++) {
  523. s->fast_fragment_list[i] = i + 1;
  524. }
  525. s->fast_fragment_list[s->fragment_count - 1] = -1;
  526. if (s->last_coded_y_fragment == -1)
  527. s->fragment_list_y_head = -1;
  528. else {
  529. s->fragment_list_y_head = s->first_coded_y_fragment;
  530. s->fast_fragment_list[s->last_coded_y_fragment] = -1;
  531. }
  532. if (s->last_coded_c_fragment == -1)
  533. s->fragment_list_c_head = -1;
  534. else {
  535. s->fragment_list_c_head = s->first_coded_c_fragment;
  536. s->fast_fragment_list[s->last_coded_c_fragment] = -1;
  537. }
  538. return 0;
  539. }
  540. /*
  541. * This function unpacks all the coding mode data for individual macroblocks
  542. * from the bitstream.
  543. */
  544. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  545. {
  546. int i, j, k, sb_x, sb_y;
  547. int scheme;
  548. int current_macroblock;
  549. int current_fragment;
  550. int coding_mode;
  551. int custom_mode_alphabet[CODING_MODE_COUNT];
  552. const int *alphabet;
  553. if (s->keyframe) {
  554. for (i = 0; i < s->fragment_count; i++)
  555. s->all_fragments[i].coding_method = MODE_INTRA;
  556. } else {
  557. /* fetch the mode coding scheme for this frame */
  558. scheme = get_bits(gb, 3);
  559. /* is it a custom coding scheme? */
  560. if (scheme == 0) {
  561. for (i = 0; i < 8; i++)
  562. custom_mode_alphabet[i] = MODE_INTER_NO_MV;
  563. for (i = 0; i < 8; i++)
  564. custom_mode_alphabet[get_bits(gb, 3)] = i;
  565. alphabet = custom_mode_alphabet;
  566. } else
  567. alphabet = ModeAlphabet[scheme-1];
  568. /* iterate through all of the macroblocks that contain 1 or more
  569. * coded fragments */
  570. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  571. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  572. for (j = 0; j < 4; j++) {
  573. int mb_x = 2*sb_x + (j>>1);
  574. int mb_y = 2*sb_y + (((j>>1)+j)&1);
  575. int frags_coded = 0;
  576. current_macroblock = mb_y * s->macroblock_width + mb_x;
  577. if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
  578. continue;
  579. #define BLOCK_X (2*mb_x + (k&1))
  580. #define BLOCK_Y (2*mb_y + (k>>1))
  581. /* coding modes are only stored if the macroblock has at least one
  582. * luma block coded, otherwise it must be INTER_NO_MV */
  583. for (k = 0; k < 4; k++) {
  584. current_fragment = BLOCK_Y*s->fragment_width + BLOCK_X;
  585. if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
  586. break;
  587. }
  588. if (k == 4) {
  589. s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
  590. continue;
  591. }
  592. /* mode 7 means get 3 bits for each coding mode */
  593. if (scheme == 7)
  594. coding_mode = get_bits(gb, 3);
  595. else
  596. coding_mode = alphabet
  597. [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  598. s->macroblock_coding[current_macroblock] = coding_mode;
  599. for (k = 0; k < 4; k++) {
  600. current_fragment =
  601. BLOCK_Y*s->fragment_width + BLOCK_X;
  602. if (s->all_fragments[current_fragment].coding_method !=
  603. MODE_COPY)
  604. s->all_fragments[current_fragment].coding_method =
  605. coding_mode;
  606. }
  607. for (k = 0; k < 2; k++) {
  608. current_fragment = s->fragment_start[k+1] +
  609. mb_y*(s->fragment_width>>1) + mb_x;
  610. if (s->all_fragments[current_fragment].coding_method !=
  611. MODE_COPY)
  612. s->all_fragments[current_fragment].coding_method =
  613. coding_mode;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. return 0;
  620. }
  621. /*
  622. * This function unpacks all the motion vectors for the individual
  623. * macroblocks from the bitstream.
  624. */
  625. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  626. {
  627. int j, k, sb_x, sb_y;
  628. int coding_mode;
  629. int motion_x[6];
  630. int motion_y[6];
  631. int last_motion_x = 0;
  632. int last_motion_y = 0;
  633. int prior_last_motion_x = 0;
  634. int prior_last_motion_y = 0;
  635. int current_macroblock;
  636. int current_fragment;
  637. if (s->keyframe)
  638. return 0;
  639. memset(motion_x, 0, 6 * sizeof(int));
  640. memset(motion_y, 0, 6 * sizeof(int));
  641. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  642. coding_mode = get_bits1(gb);
  643. /* iterate through all of the macroblocks that contain 1 or more
  644. * coded fragments */
  645. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  646. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  647. for (j = 0; j < 4; j++) {
  648. int mb_x = 2*sb_x + (j>>1);
  649. int mb_y = 2*sb_y + (((j>>1)+j)&1);
  650. current_macroblock = mb_y * s->macroblock_width + mb_x;
  651. if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
  652. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  653. continue;
  654. switch (s->macroblock_coding[current_macroblock]) {
  655. case MODE_INTER_PLUS_MV:
  656. case MODE_GOLDEN_MV:
  657. /* all 6 fragments use the same motion vector */
  658. if (coding_mode == 0) {
  659. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  660. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  661. } else {
  662. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  663. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  664. }
  665. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  666. if (s->macroblock_coding[current_macroblock] ==
  667. MODE_INTER_PLUS_MV) {
  668. prior_last_motion_x = last_motion_x;
  669. prior_last_motion_y = last_motion_y;
  670. last_motion_x = motion_x[0];
  671. last_motion_y = motion_y[0];
  672. }
  673. break;
  674. case MODE_INTER_FOURMV:
  675. /* vector maintenance */
  676. prior_last_motion_x = last_motion_x;
  677. prior_last_motion_y = last_motion_y;
  678. /* fetch 4 vectors from the bitstream, one for each
  679. * Y fragment, then average for the C fragment vectors */
  680. motion_x[4] = motion_y[4] = 0;
  681. for (k = 0; k < 4; k++) {
  682. current_fragment = BLOCK_Y*s->fragment_width + BLOCK_X;
  683. if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
  684. if (coding_mode == 0) {
  685. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  686. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  687. } else {
  688. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  689. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  690. }
  691. last_motion_x = motion_x[k];
  692. last_motion_y = motion_y[k];
  693. } else {
  694. motion_x[k] = 0;
  695. motion_y[k] = 0;
  696. }
  697. motion_x[4] += motion_x[k];
  698. motion_y[4] += motion_y[k];
  699. }
  700. motion_x[5]=
  701. motion_x[4]= RSHIFT(motion_x[4], 2);
  702. motion_y[5]=
  703. motion_y[4]= RSHIFT(motion_y[4], 2);
  704. break;
  705. case MODE_INTER_LAST_MV:
  706. /* all 6 fragments use the last motion vector */
  707. motion_x[0] = last_motion_x;
  708. motion_y[0] = last_motion_y;
  709. /* no vector maintenance (last vector remains the
  710. * last vector) */
  711. break;
  712. case MODE_INTER_PRIOR_LAST:
  713. /* all 6 fragments use the motion vector prior to the
  714. * last motion vector */
  715. motion_x[0] = prior_last_motion_x;
  716. motion_y[0] = prior_last_motion_y;
  717. /* vector maintenance */
  718. prior_last_motion_x = last_motion_x;
  719. prior_last_motion_y = last_motion_y;
  720. last_motion_x = motion_x[0];
  721. last_motion_y = motion_y[0];
  722. break;
  723. default:
  724. /* covers intra, inter without MV, golden without MV */
  725. motion_x[0] = 0;
  726. motion_y[0] = 0;
  727. /* no vector maintenance */
  728. break;
  729. }
  730. /* assign the motion vectors to the correct fragments */
  731. for (k = 0; k < 4; k++) {
  732. current_fragment =
  733. BLOCK_Y*s->fragment_width + BLOCK_X;
  734. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  735. s->all_fragments[current_fragment].motion_x = motion_x[k];
  736. s->all_fragments[current_fragment].motion_y = motion_y[k];
  737. } else {
  738. s->all_fragments[current_fragment].motion_x = motion_x[0];
  739. s->all_fragments[current_fragment].motion_y = motion_y[0];
  740. }
  741. }
  742. for (k = 0; k < 2; k++) {
  743. current_fragment = s->fragment_start[k+1] +
  744. mb_y*(s->fragment_width>>1) + mb_x;
  745. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  746. s->all_fragments[current_fragment].motion_x = motion_x[k+4];
  747. s->all_fragments[current_fragment].motion_y = motion_y[k+4];
  748. } else {
  749. s->all_fragments[current_fragment].motion_x = motion_x[0];
  750. s->all_fragments[current_fragment].motion_y = motion_y[0];
  751. }
  752. }
  753. }
  754. }
  755. }
  756. return 0;
  757. }
  758. static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
  759. {
  760. int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
  761. int num_blocks = s->coded_fragment_list_index;
  762. for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
  763. i = blocks_decoded = num_blocks_at_qpi = 0;
  764. bit = get_bits1(gb);
  765. do {
  766. run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
  767. if (run_length == 34)
  768. run_length += get_bits(gb, 12);
  769. blocks_decoded += run_length;
  770. if (!bit)
  771. num_blocks_at_qpi += run_length;
  772. for (j = 0; j < run_length; i++) {
  773. if (i >= s->coded_fragment_list_index)
  774. return -1;
  775. if (s->all_fragments[s->coded_fragment_list[i]].qpi == qpi) {
  776. s->all_fragments[s->coded_fragment_list[i]].qpi += bit;
  777. j++;
  778. }
  779. }
  780. if (run_length == 4129)
  781. bit = get_bits1(gb);
  782. else
  783. bit ^= 1;
  784. } while (blocks_decoded < num_blocks);
  785. num_blocks -= num_blocks_at_qpi;
  786. }
  787. return 0;
  788. }
  789. /*
  790. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  791. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  792. * data. This function unpacks all the VLCs for either the Y plane or both
  793. * C planes, and is called for DC coefficients or different AC coefficient
  794. * levels (since different coefficient types require different VLC tables.
  795. *
  796. * This function returns a residual eob run. E.g, if a particular token gave
  797. * instructions to EOB the next 5 fragments and there were only 2 fragments
  798. * left in the current fragment range, 3 would be returned so that it could
  799. * be passed into the next call to this same function.
  800. */
  801. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  802. VLC *table, int coeff_index,
  803. int y_plane,
  804. int eob_run)
  805. {
  806. int i;
  807. int token;
  808. int zero_run = 0;
  809. DCTELEM coeff = 0;
  810. Vp3Fragment *fragment;
  811. int bits_to_get;
  812. int next_fragment;
  813. int previous_fragment;
  814. int fragment_num;
  815. int *list_head;
  816. /* local references to structure members to avoid repeated deferences */
  817. uint8_t *perm= s->scantable.permutated;
  818. int *coded_fragment_list = s->coded_fragment_list;
  819. Vp3Fragment *all_fragments = s->all_fragments;
  820. uint8_t *coeff_counts = s->coeff_counts;
  821. VLC_TYPE (*vlc_table)[2] = table->table;
  822. int *fast_fragment_list = s->fast_fragment_list;
  823. if (y_plane) {
  824. next_fragment = s->fragment_list_y_head;
  825. list_head = &s->fragment_list_y_head;
  826. } else {
  827. next_fragment = s->fragment_list_c_head;
  828. list_head = &s->fragment_list_c_head;
  829. }
  830. i = next_fragment;
  831. previous_fragment = -1; /* this indicates that the previous fragment is actually the list head */
  832. while (i != -1) {
  833. fragment_num = coded_fragment_list[i];
  834. if (coeff_counts[fragment_num] > coeff_index) {
  835. previous_fragment = i;
  836. i = fast_fragment_list[i];
  837. continue;
  838. }
  839. fragment = &all_fragments[fragment_num];
  840. if (!eob_run) {
  841. /* decode a VLC into a token */
  842. token = get_vlc2(gb, vlc_table, 5, 3);
  843. /* use the token to get a zero run, a coefficient, and an eob run */
  844. if (token <= 6) {
  845. eob_run = eob_run_base[token];
  846. if (eob_run_get_bits[token])
  847. eob_run += get_bits(gb, eob_run_get_bits[token]);
  848. coeff = zero_run = 0;
  849. } else {
  850. bits_to_get = coeff_get_bits[token];
  851. if (bits_to_get)
  852. bits_to_get = get_bits(gb, bits_to_get);
  853. coeff = coeff_tables[token][bits_to_get];
  854. zero_run = zero_run_base[token];
  855. if (zero_run_get_bits[token])
  856. zero_run += get_bits(gb, zero_run_get_bits[token]);
  857. }
  858. }
  859. if (!eob_run) {
  860. coeff_counts[fragment_num] += zero_run;
  861. if (coeff_counts[fragment_num] < 64){
  862. fragment->next_coeff->coeff= coeff;
  863. fragment->next_coeff->index= perm[coeff_counts[fragment_num]++]; //FIXME perm here already?
  864. fragment->next_coeff->next= s->next_coeff;
  865. s->next_coeff->next=NULL;
  866. fragment->next_coeff= s->next_coeff++;
  867. }
  868. /* previous fragment is now this fragment */
  869. previous_fragment = i;
  870. } else {
  871. coeff_counts[fragment_num] |= 128;
  872. eob_run--;
  873. /* remove this fragment from the list */
  874. if (previous_fragment != -1)
  875. fast_fragment_list[previous_fragment] = fast_fragment_list[i];
  876. else
  877. *list_head = fast_fragment_list[i];
  878. /* previous fragment remains unchanged */
  879. }
  880. i = fast_fragment_list[i];
  881. }
  882. return eob_run;
  883. }
  884. static void reverse_dc_prediction(Vp3DecodeContext *s,
  885. int first_fragment,
  886. int fragment_width,
  887. int fragment_height);
  888. /*
  889. * This function unpacks all of the DCT coefficient data from the
  890. * bitstream.
  891. */
  892. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  893. {
  894. int i;
  895. int dc_y_table;
  896. int dc_c_table;
  897. int ac_y_table;
  898. int ac_c_table;
  899. int residual_eob_run = 0;
  900. VLC *y_tables[64];
  901. VLC *c_tables[64];
  902. /* fetch the DC table indexes */
  903. dc_y_table = get_bits(gb, 4);
  904. dc_c_table = get_bits(gb, 4);
  905. /* unpack the Y plane DC coefficients */
  906. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  907. 1, residual_eob_run);
  908. /* reverse prediction of the Y-plane DC coefficients */
  909. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  910. /* unpack the C plane DC coefficients */
  911. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  912. 0, residual_eob_run);
  913. /* reverse prediction of the C-plane DC coefficients */
  914. if (!(s->avctx->flags & CODEC_FLAG_GRAY))
  915. {
  916. reverse_dc_prediction(s, s->fragment_start[1],
  917. s->fragment_width / 2, s->fragment_height / 2);
  918. reverse_dc_prediction(s, s->fragment_start[2],
  919. s->fragment_width / 2, s->fragment_height / 2);
  920. }
  921. /* fetch the AC table indexes */
  922. ac_y_table = get_bits(gb, 4);
  923. ac_c_table = get_bits(gb, 4);
  924. /* build tables of AC VLC tables */
  925. for (i = 1; i <= 5; i++) {
  926. y_tables[i] = &s->ac_vlc_1[ac_y_table];
  927. c_tables[i] = &s->ac_vlc_1[ac_c_table];
  928. }
  929. for (i = 6; i <= 14; i++) {
  930. y_tables[i] = &s->ac_vlc_2[ac_y_table];
  931. c_tables[i] = &s->ac_vlc_2[ac_c_table];
  932. }
  933. for (i = 15; i <= 27; i++) {
  934. y_tables[i] = &s->ac_vlc_3[ac_y_table];
  935. c_tables[i] = &s->ac_vlc_3[ac_c_table];
  936. }
  937. for (i = 28; i <= 63; i++) {
  938. y_tables[i] = &s->ac_vlc_4[ac_y_table];
  939. c_tables[i] = &s->ac_vlc_4[ac_c_table];
  940. }
  941. /* decode all AC coefficents */
  942. for (i = 1; i <= 63; i++) {
  943. if (s->fragment_list_y_head != -1)
  944. residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
  945. 1, residual_eob_run);
  946. if (s->fragment_list_c_head != -1)
  947. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  948. 0, residual_eob_run);
  949. }
  950. return 0;
  951. }
  952. /*
  953. * This function reverses the DC prediction for each coded fragment in
  954. * the frame. Much of this function is adapted directly from the original
  955. * VP3 source code.
  956. */
  957. #define COMPATIBLE_FRAME(x) \
  958. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  959. #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
  960. static void reverse_dc_prediction(Vp3DecodeContext *s,
  961. int first_fragment,
  962. int fragment_width,
  963. int fragment_height)
  964. {
  965. #define PUL 8
  966. #define PU 4
  967. #define PUR 2
  968. #define PL 1
  969. int x, y;
  970. int i = first_fragment;
  971. int predicted_dc;
  972. /* DC values for the left, up-left, up, and up-right fragments */
  973. int vl, vul, vu, vur;
  974. /* indexes for the left, up-left, up, and up-right fragments */
  975. int l, ul, u, ur;
  976. /*
  977. * The 6 fields mean:
  978. * 0: up-left multiplier
  979. * 1: up multiplier
  980. * 2: up-right multiplier
  981. * 3: left multiplier
  982. */
  983. static const int predictor_transform[16][4] = {
  984. { 0, 0, 0, 0},
  985. { 0, 0, 0,128}, // PL
  986. { 0, 0,128, 0}, // PUR
  987. { 0, 0, 53, 75}, // PUR|PL
  988. { 0,128, 0, 0}, // PU
  989. { 0, 64, 0, 64}, // PU|PL
  990. { 0,128, 0, 0}, // PU|PUR
  991. { 0, 0, 53, 75}, // PU|PUR|PL
  992. {128, 0, 0, 0}, // PUL
  993. { 0, 0, 0,128}, // PUL|PL
  994. { 64, 0, 64, 0}, // PUL|PUR
  995. { 0, 0, 53, 75}, // PUL|PUR|PL
  996. { 0,128, 0, 0}, // PUL|PU
  997. {-104,116, 0,116}, // PUL|PU|PL
  998. { 24, 80, 24, 0}, // PUL|PU|PUR
  999. {-104,116, 0,116} // PUL|PU|PUR|PL
  1000. };
  1001. /* This table shows which types of blocks can use other blocks for
  1002. * prediction. For example, INTRA is the only mode in this table to
  1003. * have a frame number of 0. That means INTRA blocks can only predict
  1004. * from other INTRA blocks. There are 2 golden frame coding types;
  1005. * blocks encoding in these modes can only predict from other blocks
  1006. * that were encoded with these 1 of these 2 modes. */
  1007. static const unsigned char compatible_frame[9] = {
  1008. 1, /* MODE_INTER_NO_MV */
  1009. 0, /* MODE_INTRA */
  1010. 1, /* MODE_INTER_PLUS_MV */
  1011. 1, /* MODE_INTER_LAST_MV */
  1012. 1, /* MODE_INTER_PRIOR_MV */
  1013. 2, /* MODE_USING_GOLDEN */
  1014. 2, /* MODE_GOLDEN_MV */
  1015. 1, /* MODE_INTER_FOUR_MV */
  1016. 3 /* MODE_COPY */
  1017. };
  1018. int current_frame_type;
  1019. /* there is a last DC predictor for each of the 3 frame types */
  1020. short last_dc[3];
  1021. int transform = 0;
  1022. vul = vu = vur = vl = 0;
  1023. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1024. /* for each fragment row... */
  1025. for (y = 0; y < fragment_height; y++) {
  1026. /* for each fragment in a row... */
  1027. for (x = 0; x < fragment_width; x++, i++) {
  1028. /* reverse prediction if this block was coded */
  1029. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1030. current_frame_type =
  1031. compatible_frame[s->all_fragments[i].coding_method];
  1032. transform= 0;
  1033. if(x){
  1034. l= i-1;
  1035. vl = DC_COEFF(l);
  1036. if(COMPATIBLE_FRAME(l))
  1037. transform |= PL;
  1038. }
  1039. if(y){
  1040. u= i-fragment_width;
  1041. vu = DC_COEFF(u);
  1042. if(COMPATIBLE_FRAME(u))
  1043. transform |= PU;
  1044. if(x){
  1045. ul= i-fragment_width-1;
  1046. vul = DC_COEFF(ul);
  1047. if(COMPATIBLE_FRAME(ul))
  1048. transform |= PUL;
  1049. }
  1050. if(x + 1 < fragment_width){
  1051. ur= i-fragment_width+1;
  1052. vur = DC_COEFF(ur);
  1053. if(COMPATIBLE_FRAME(ur))
  1054. transform |= PUR;
  1055. }
  1056. }
  1057. if (transform == 0) {
  1058. /* if there were no fragments to predict from, use last
  1059. * DC saved */
  1060. predicted_dc = last_dc[current_frame_type];
  1061. } else {
  1062. /* apply the appropriate predictor transform */
  1063. predicted_dc =
  1064. (predictor_transform[transform][0] * vul) +
  1065. (predictor_transform[transform][1] * vu) +
  1066. (predictor_transform[transform][2] * vur) +
  1067. (predictor_transform[transform][3] * vl);
  1068. predicted_dc /= 128;
  1069. /* check for outranging on the [ul u l] and
  1070. * [ul u ur l] predictors */
  1071. if ((transform == 15) || (transform == 13)) {
  1072. if (FFABS(predicted_dc - vu) > 128)
  1073. predicted_dc = vu;
  1074. else if (FFABS(predicted_dc - vl) > 128)
  1075. predicted_dc = vl;
  1076. else if (FFABS(predicted_dc - vul) > 128)
  1077. predicted_dc = vul;
  1078. }
  1079. }
  1080. /* at long last, apply the predictor */
  1081. if(s->coeffs[i].index){
  1082. *s->next_coeff= s->coeffs[i];
  1083. s->coeffs[i].index=0;
  1084. s->coeffs[i].coeff=0;
  1085. s->coeffs[i].next= s->next_coeff++;
  1086. }
  1087. s->coeffs[i].coeff += predicted_dc;
  1088. /* save the DC */
  1089. last_dc[current_frame_type] = DC_COEFF(i);
  1090. if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
  1091. s->coeff_counts[i]= 129;
  1092. // s->all_fragments[i].next_coeff= s->next_coeff;
  1093. s->coeffs[i].next= s->next_coeff;
  1094. (s->next_coeff++)->next=NULL;
  1095. }
  1096. }
  1097. }
  1098. }
  1099. }
  1100. static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
  1101. {
  1102. int x, y;
  1103. int *bounding_values= s->bounding_values_array+127;
  1104. int width = s->fragment_width >> !!plane;
  1105. int height = s->fragment_height >> !!plane;
  1106. int fragment = s->fragment_start [plane] + ystart * width;
  1107. int stride = s->current_frame.linesize[plane];
  1108. uint8_t *plane_data = s->current_frame.data [plane];
  1109. if (!s->flipped_image) stride = -stride;
  1110. plane_data += s->data_offset[plane] + 8*ystart*stride;
  1111. for (y = ystart; y < yend; y++) {
  1112. for (x = 0; x < width; x++) {
  1113. /* This code basically just deblocks on the edges of coded blocks.
  1114. * However, it has to be much more complicated because of the
  1115. * braindamaged deblock ordering used in VP3/Theora. Order matters
  1116. * because some pixels get filtered twice. */
  1117. if( s->all_fragments[fragment].coding_method != MODE_COPY )
  1118. {
  1119. /* do not perform left edge filter for left columns frags */
  1120. if (x > 0) {
  1121. s->dsp.vp3_h_loop_filter(
  1122. plane_data + 8*x,
  1123. stride, bounding_values);
  1124. }
  1125. /* do not perform top edge filter for top row fragments */
  1126. if (y > 0) {
  1127. s->dsp.vp3_v_loop_filter(
  1128. plane_data + 8*x,
  1129. stride, bounding_values);
  1130. }
  1131. /* do not perform right edge filter for right column
  1132. * fragments or if right fragment neighbor is also coded
  1133. * in this frame (it will be filtered in next iteration) */
  1134. if ((x < width - 1) &&
  1135. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1136. s->dsp.vp3_h_loop_filter(
  1137. plane_data + 8*x + 8,
  1138. stride, bounding_values);
  1139. }
  1140. /* do not perform bottom edge filter for bottom row
  1141. * fragments or if bottom fragment neighbor is also coded
  1142. * in this frame (it will be filtered in the next row) */
  1143. if ((y < height - 1) &&
  1144. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1145. s->dsp.vp3_v_loop_filter(
  1146. plane_data + 8*x + 8*stride,
  1147. stride, bounding_values);
  1148. }
  1149. }
  1150. fragment++;
  1151. }
  1152. plane_data += 8*stride;
  1153. }
  1154. }
  1155. /**
  1156. * called when all pixels up to row y are complete
  1157. */
  1158. static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
  1159. {
  1160. int h, cy;
  1161. int offset[4];
  1162. if(s->avctx->draw_horiz_band==NULL)
  1163. return;
  1164. h= y - s->last_slice_end;
  1165. y -= h;
  1166. if (!s->flipped_image) {
  1167. if (y == 0)
  1168. h -= s->height - s->avctx->height; // account for non-mod16
  1169. y = s->height - y - h;
  1170. }
  1171. cy = y >> 1;
  1172. offset[0] = s->current_frame.linesize[0]*y;
  1173. offset[1] = s->current_frame.linesize[1]*cy;
  1174. offset[2] = s->current_frame.linesize[2]*cy;
  1175. offset[3] = 0;
  1176. emms_c();
  1177. s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
  1178. s->last_slice_end= y + h;
  1179. }
  1180. /*
  1181. * Perform the final rendering for a particular slice of data.
  1182. * The slice number ranges from 0..(macroblock_height - 1).
  1183. */
  1184. static void render_slice(Vp3DecodeContext *s, int slice)
  1185. {
  1186. int x;
  1187. int16_t *dequantizer;
  1188. LOCAL_ALIGNED_16(DCTELEM, block, [64]);
  1189. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1190. int motion_halfpel_index;
  1191. uint8_t *motion_source;
  1192. int plane;
  1193. if (slice >= s->macroblock_height)
  1194. return;
  1195. for (plane = 0; plane < 3; plane++) {
  1196. uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
  1197. uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
  1198. uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
  1199. int stride = s->current_frame.linesize[plane];
  1200. int plane_width = s->width >> !!plane;
  1201. int plane_height = s->height >> !!plane;
  1202. int y = slice * FRAGMENT_PIXELS << !plane ;
  1203. int slice_height = y + (FRAGMENT_PIXELS << !plane);
  1204. int i = s->fragment_start[plane] + (y>>3)*(s->fragment_width>>!!plane);
  1205. if (!s->flipped_image) stride = -stride;
  1206. if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
  1207. continue;
  1208. if(FFABS(stride) > 2048)
  1209. return; //various tables are fixed size
  1210. /* for each fragment row in the slice (both of them)... */
  1211. for (; y < slice_height; y += 8) {
  1212. /* for each fragment in a row... */
  1213. for (x = 0; x < plane_width; x += 8, i++) {
  1214. int first_pixel = y*stride + x;
  1215. if ((i < 0) || (i >= s->fragment_count)) {
  1216. av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
  1217. return;
  1218. }
  1219. /* transform if this block was coded */
  1220. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1221. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1222. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1223. motion_source= golden_plane;
  1224. else
  1225. motion_source= last_plane;
  1226. motion_source += first_pixel;
  1227. motion_halfpel_index = 0;
  1228. /* sort out the motion vector if this fragment is coded
  1229. * using a motion vector method */
  1230. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1231. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1232. int src_x, src_y;
  1233. motion_x = s->all_fragments[i].motion_x;
  1234. motion_y = s->all_fragments[i].motion_y;
  1235. if(plane){
  1236. motion_x= (motion_x>>1) | (motion_x&1);
  1237. motion_y= (motion_y>>1) | (motion_y&1);
  1238. }
  1239. src_x= (motion_x>>1) + x;
  1240. src_y= (motion_y>>1) + y;
  1241. if ((motion_x == 127) || (motion_y == 127))
  1242. av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
  1243. motion_halfpel_index = motion_x & 0x01;
  1244. motion_source += (motion_x >> 1);
  1245. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1246. motion_source += ((motion_y >> 1) * stride);
  1247. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1248. uint8_t *temp= s->edge_emu_buffer;
  1249. if(stride<0) temp -= 9*stride;
  1250. else temp += 9*stride;
  1251. ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1252. motion_source= temp;
  1253. }
  1254. }
  1255. /* first, take care of copying a block from either the
  1256. * previous or the golden frame */
  1257. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1258. /* Note, it is possible to implement all MC cases with
  1259. put_no_rnd_pixels_l2 which would look more like the
  1260. VP3 source but this would be slower as
  1261. put_no_rnd_pixels_tab is better optimzed */
  1262. if(motion_halfpel_index != 3){
  1263. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1264. output_plane + first_pixel,
  1265. motion_source, stride, 8);
  1266. }else{
  1267. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1268. s->dsp.put_no_rnd_pixels_l2[1](
  1269. output_plane + first_pixel,
  1270. motion_source - d,
  1271. motion_source + stride + 1 + d,
  1272. stride, 8);
  1273. }
  1274. dequantizer = s->qmat[s->all_fragments[i].qpi][1][plane];
  1275. }else{
  1276. dequantizer = s->qmat[s->all_fragments[i].qpi][0][plane];
  1277. }
  1278. /* dequantize the DCT coefficients */
  1279. if(s->avctx->idct_algo==FF_IDCT_VP3){
  1280. Coeff *coeff= s->coeffs + i;
  1281. s->dsp.clear_block(block);
  1282. while(coeff->next){
  1283. block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
  1284. coeff= coeff->next;
  1285. }
  1286. }else{
  1287. Coeff *coeff= s->coeffs + i;
  1288. s->dsp.clear_block(block);
  1289. while(coeff->next){
  1290. block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
  1291. coeff= coeff->next;
  1292. }
  1293. }
  1294. /* invert DCT and place (or add) in final output */
  1295. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1296. if(s->avctx->idct_algo!=FF_IDCT_VP3)
  1297. block[0] += 128<<3;
  1298. s->dsp.idct_put(
  1299. output_plane + first_pixel,
  1300. stride,
  1301. block);
  1302. } else {
  1303. s->dsp.idct_add(
  1304. output_plane + first_pixel,
  1305. stride,
  1306. block);
  1307. }
  1308. } else {
  1309. /* copy directly from the previous frame */
  1310. s->dsp.put_pixels_tab[1][0](
  1311. output_plane + first_pixel,
  1312. last_plane + first_pixel,
  1313. stride, 8);
  1314. }
  1315. }
  1316. // Filter the previous block row. We can't filter the current row yet
  1317. // since it needs pixels from the next row
  1318. if (y > 0)
  1319. apply_loop_filter(s, plane, (y>>3)-1, (y>>3));
  1320. }
  1321. }
  1322. /* this looks like a good place for slice dispatch... */
  1323. /* algorithm:
  1324. * if (slice == s->macroblock_height - 1)
  1325. * dispatch (both last slice & 2nd-to-last slice);
  1326. * else if (slice > 0)
  1327. * dispatch (slice - 1);
  1328. */
  1329. // now that we've filtered the last rows, they're safe to display
  1330. if (slice)
  1331. vp3_draw_horiz_band(s, 16*slice);
  1332. }
  1333. /*
  1334. * This is the ffmpeg/libavcodec API init function.
  1335. */
  1336. static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1337. {
  1338. Vp3DecodeContext *s = avctx->priv_data;
  1339. int i, inter, plane;
  1340. int c_width;
  1341. int c_height;
  1342. int y_superblock_count;
  1343. int c_superblock_count;
  1344. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1345. s->version = 0;
  1346. else
  1347. s->version = 1;
  1348. s->avctx = avctx;
  1349. s->width = FFALIGN(avctx->width, 16);
  1350. s->height = FFALIGN(avctx->height, 16);
  1351. avctx->pix_fmt = PIX_FMT_YUV420P;
  1352. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1353. if(avctx->idct_algo==FF_IDCT_AUTO)
  1354. avctx->idct_algo=FF_IDCT_VP3;
  1355. dsputil_init(&s->dsp, avctx);
  1356. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1357. /* initialize to an impossible value which will force a recalculation
  1358. * in the first frame decode */
  1359. for (i = 0; i < 3; i++)
  1360. s->qps[i] = -1;
  1361. s->y_superblock_width = (s->width + 31) / 32;
  1362. s->y_superblock_height = (s->height + 31) / 32;
  1363. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1364. /* work out the dimensions for the C planes */
  1365. c_width = s->width / 2;
  1366. c_height = s->height / 2;
  1367. s->c_superblock_width = (c_width + 31) / 32;
  1368. s->c_superblock_height = (c_height + 31) / 32;
  1369. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1370. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1371. s->u_superblock_start = y_superblock_count;
  1372. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1373. s->superblock_coding = av_malloc(s->superblock_count);
  1374. s->macroblock_width = (s->width + 15) / 16;
  1375. s->macroblock_height = (s->height + 15) / 16;
  1376. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1377. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1378. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1379. /* fragment count covers all 8x8 blocks for all 3 planes */
  1380. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1381. s->fragment_start[1] = s->fragment_width * s->fragment_height;
  1382. s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
  1383. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1384. s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
  1385. s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
  1386. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1387. s->fast_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1388. if (!s->superblock_coding || !s->all_fragments || !s->coeff_counts ||
  1389. !s->coeffs || !s->coded_fragment_list || !s->fast_fragment_list) {
  1390. vp3_decode_end(avctx);
  1391. return -1;
  1392. }
  1393. if (!s->theora_tables)
  1394. {
  1395. for (i = 0; i < 64; i++) {
  1396. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1397. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1398. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1399. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1400. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1401. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1402. }
  1403. for(inter=0; inter<2; inter++){
  1404. for(plane=0; plane<3; plane++){
  1405. s->qr_count[inter][plane]= 1;
  1406. s->qr_size [inter][plane][0]= 63;
  1407. s->qr_base [inter][plane][0]=
  1408. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1409. }
  1410. }
  1411. /* init VLC tables */
  1412. for (i = 0; i < 16; i++) {
  1413. /* DC histograms */
  1414. init_vlc(&s->dc_vlc[i], 5, 32,
  1415. &dc_bias[i][0][1], 4, 2,
  1416. &dc_bias[i][0][0], 4, 2, 0);
  1417. /* group 1 AC histograms */
  1418. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1419. &ac_bias_0[i][0][1], 4, 2,
  1420. &ac_bias_0[i][0][0], 4, 2, 0);
  1421. /* group 2 AC histograms */
  1422. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1423. &ac_bias_1[i][0][1], 4, 2,
  1424. &ac_bias_1[i][0][0], 4, 2, 0);
  1425. /* group 3 AC histograms */
  1426. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1427. &ac_bias_2[i][0][1], 4, 2,
  1428. &ac_bias_2[i][0][0], 4, 2, 0);
  1429. /* group 4 AC histograms */
  1430. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1431. &ac_bias_3[i][0][1], 4, 2,
  1432. &ac_bias_3[i][0][0], 4, 2, 0);
  1433. }
  1434. } else {
  1435. for (i = 0; i < 16; i++) {
  1436. /* DC histograms */
  1437. if (init_vlc(&s->dc_vlc[i], 5, 32,
  1438. &s->huffman_table[i][0][1], 4, 2,
  1439. &s->huffman_table[i][0][0], 4, 2, 0) < 0)
  1440. goto vlc_fail;
  1441. /* group 1 AC histograms */
  1442. if (init_vlc(&s->ac_vlc_1[i], 5, 32,
  1443. &s->huffman_table[i+16][0][1], 4, 2,
  1444. &s->huffman_table[i+16][0][0], 4, 2, 0) < 0)
  1445. goto vlc_fail;
  1446. /* group 2 AC histograms */
  1447. if (init_vlc(&s->ac_vlc_2[i], 5, 32,
  1448. &s->huffman_table[i+16*2][0][1], 4, 2,
  1449. &s->huffman_table[i+16*2][0][0], 4, 2, 0) < 0)
  1450. goto vlc_fail;
  1451. /* group 3 AC histograms */
  1452. if (init_vlc(&s->ac_vlc_3[i], 5, 32,
  1453. &s->huffman_table[i+16*3][0][1], 4, 2,
  1454. &s->huffman_table[i+16*3][0][0], 4, 2, 0) < 0)
  1455. goto vlc_fail;
  1456. /* group 4 AC histograms */
  1457. if (init_vlc(&s->ac_vlc_4[i], 5, 32,
  1458. &s->huffman_table[i+16*4][0][1], 4, 2,
  1459. &s->huffman_table[i+16*4][0][0], 4, 2, 0) < 0)
  1460. goto vlc_fail;
  1461. }
  1462. }
  1463. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1464. &superblock_run_length_vlc_table[0][1], 4, 2,
  1465. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1466. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1467. &fragment_run_length_vlc_table[0][1], 4, 2,
  1468. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1469. init_vlc(&s->mode_code_vlc, 3, 8,
  1470. &mode_code_vlc_table[0][1], 2, 1,
  1471. &mode_code_vlc_table[0][0], 2, 1, 0);
  1472. init_vlc(&s->motion_vector_vlc, 6, 63,
  1473. &motion_vector_vlc_table[0][1], 2, 1,
  1474. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1475. /* work out the block mapping tables */
  1476. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1477. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1478. if (!s->superblock_fragments || !s->macroblock_coding) {
  1479. vp3_decode_end(avctx);
  1480. return -1;
  1481. }
  1482. init_block_mapping(s);
  1483. for (i = 0; i < 3; i++) {
  1484. s->current_frame.data[i] = NULL;
  1485. s->last_frame.data[i] = NULL;
  1486. s->golden_frame.data[i] = NULL;
  1487. }
  1488. return 0;
  1489. vlc_fail:
  1490. av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
  1491. return -1;
  1492. }
  1493. /*
  1494. * This is the ffmpeg/libavcodec API frame decode function.
  1495. */
  1496. static int vp3_decode_frame(AVCodecContext *avctx,
  1497. void *data, int *data_size,
  1498. AVPacket *avpkt)
  1499. {
  1500. const uint8_t *buf = avpkt->data;
  1501. int buf_size = avpkt->size;
  1502. Vp3DecodeContext *s = avctx->priv_data;
  1503. GetBitContext gb;
  1504. static int counter = 0;
  1505. int i;
  1506. init_get_bits(&gb, buf, buf_size * 8);
  1507. if (s->theora && get_bits1(&gb))
  1508. {
  1509. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1510. return -1;
  1511. }
  1512. s->keyframe = !get_bits1(&gb);
  1513. if (!s->theora)
  1514. skip_bits(&gb, 1);
  1515. for (i = 0; i < 3; i++)
  1516. s->last_qps[i] = s->qps[i];
  1517. s->nqps=0;
  1518. do{
  1519. s->qps[s->nqps++]= get_bits(&gb, 6);
  1520. } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
  1521. for (i = s->nqps; i < 3; i++)
  1522. s->qps[i] = -1;
  1523. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1524. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1525. s->keyframe?"key":"", counter, s->qps[0]);
  1526. counter++;
  1527. if (s->qps[0] != s->last_qps[0])
  1528. init_loop_filter(s);
  1529. for (i = 0; i < s->nqps; i++)
  1530. // reinit all dequantizers if the first one changed, because
  1531. // the DC of the first quantizer must be used for all matrices
  1532. if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
  1533. init_dequantizer(s, i);
  1534. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  1535. return buf_size;
  1536. if (s->keyframe) {
  1537. if (!s->theora)
  1538. {
  1539. skip_bits(&gb, 4); /* width code */
  1540. skip_bits(&gb, 4); /* height code */
  1541. if (s->version)
  1542. {
  1543. s->version = get_bits(&gb, 5);
  1544. if (counter == 1)
  1545. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1546. }
  1547. }
  1548. if (s->version || s->theora)
  1549. {
  1550. if (get_bits1(&gb))
  1551. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1552. skip_bits(&gb, 2); /* reserved? */
  1553. }
  1554. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  1555. if (s->golden_frame.data[0])
  1556. avctx->release_buffer(avctx, &s->golden_frame);
  1557. s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
  1558. } else {
  1559. if (s->golden_frame.data[0])
  1560. avctx->release_buffer(avctx, &s->golden_frame);
  1561. if (s->last_frame.data[0])
  1562. avctx->release_buffer(avctx, &s->last_frame);
  1563. }
  1564. s->golden_frame.reference = 3;
  1565. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  1566. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1567. return -1;
  1568. }
  1569. /* golden frame is also the current frame */
  1570. s->current_frame= s->golden_frame;
  1571. } else {
  1572. /* allocate a new current frame */
  1573. s->current_frame.reference = 3;
  1574. if (!s->golden_frame.data[0]) {
  1575. av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
  1576. return -1;
  1577. }
  1578. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  1579. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1580. return -1;
  1581. }
  1582. }
  1583. s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
  1584. s->current_frame.qstride= 0;
  1585. init_frame(s, &gb);
  1586. if (unpack_superblocks(s, &gb)){
  1587. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1588. return -1;
  1589. }
  1590. if (unpack_modes(s, &gb)){
  1591. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1592. return -1;
  1593. }
  1594. if (unpack_vectors(s, &gb)){
  1595. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1596. return -1;
  1597. }
  1598. if (unpack_block_qpis(s, &gb)){
  1599. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
  1600. return -1;
  1601. }
  1602. if (unpack_dct_coeffs(s, &gb)){
  1603. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1604. return -1;
  1605. }
  1606. for (i = 0; i < 3; i++) {
  1607. if (s->flipped_image)
  1608. s->data_offset[i] = 0;
  1609. else
  1610. s->data_offset[i] = ((s->height>>!!i)-1) * s->current_frame.linesize[i];
  1611. }
  1612. s->last_slice_end = 0;
  1613. for (i = 0; i < s->macroblock_height; i++)
  1614. render_slice(s, i);
  1615. // filter the last row
  1616. for (i = 0; i < 3; i++) {
  1617. int row = (s->height >> (3+!!i)) - 1;
  1618. apply_loop_filter(s, i, row, row+1);
  1619. }
  1620. vp3_draw_horiz_band(s, s->height);
  1621. *data_size=sizeof(AVFrame);
  1622. *(AVFrame*)data= s->current_frame;
  1623. /* release the last frame, if it is allocated and if it is not the
  1624. * golden frame */
  1625. if ((s->last_frame.data[0]) &&
  1626. (s->last_frame.data[0] != s->golden_frame.data[0]))
  1627. avctx->release_buffer(avctx, &s->last_frame);
  1628. /* shuffle frames (last = current) */
  1629. s->last_frame= s->current_frame;
  1630. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1631. return buf_size;
  1632. }
  1633. /*
  1634. * This is the ffmpeg/libavcodec API module cleanup function.
  1635. */
  1636. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1637. {
  1638. Vp3DecodeContext *s = avctx->priv_data;
  1639. int i;
  1640. av_free(s->superblock_coding);
  1641. av_free(s->all_fragments);
  1642. av_free(s->coeff_counts);
  1643. av_free(s->coeffs);
  1644. av_free(s->coded_fragment_list);
  1645. av_free(s->fast_fragment_list);
  1646. av_free(s->superblock_fragments);
  1647. av_free(s->macroblock_coding);
  1648. for (i = 0; i < 16; i++) {
  1649. free_vlc(&s->dc_vlc[i]);
  1650. free_vlc(&s->ac_vlc_1[i]);
  1651. free_vlc(&s->ac_vlc_2[i]);
  1652. free_vlc(&s->ac_vlc_3[i]);
  1653. free_vlc(&s->ac_vlc_4[i]);
  1654. }
  1655. free_vlc(&s->superblock_run_length_vlc);
  1656. free_vlc(&s->fragment_run_length_vlc);
  1657. free_vlc(&s->mode_code_vlc);
  1658. free_vlc(&s->motion_vector_vlc);
  1659. /* release all frames */
  1660. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  1661. avctx->release_buffer(avctx, &s->golden_frame);
  1662. if (s->last_frame.data[0])
  1663. avctx->release_buffer(avctx, &s->last_frame);
  1664. /* no need to release the current_frame since it will always be pointing
  1665. * to the same frame as either the golden or last frame */
  1666. return 0;
  1667. }
  1668. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  1669. {
  1670. Vp3DecodeContext *s = avctx->priv_data;
  1671. if (get_bits1(gb)) {
  1672. int token;
  1673. if (s->entries >= 32) { /* overflow */
  1674. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1675. return -1;
  1676. }
  1677. token = get_bits(gb, 5);
  1678. //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size);
  1679. s->huffman_table[s->hti][token][0] = s->hbits;
  1680. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  1681. s->entries++;
  1682. }
  1683. else {
  1684. if (s->huff_code_size >= 32) {/* overflow */
  1685. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1686. return -1;
  1687. }
  1688. s->huff_code_size++;
  1689. s->hbits <<= 1;
  1690. if (read_huffman_tree(avctx, gb))
  1691. return -1;
  1692. s->hbits |= 1;
  1693. if (read_huffman_tree(avctx, gb))
  1694. return -1;
  1695. s->hbits >>= 1;
  1696. s->huff_code_size--;
  1697. }
  1698. return 0;
  1699. }
  1700. #if CONFIG_THEORA_DECODER
  1701. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  1702. {
  1703. Vp3DecodeContext *s = avctx->priv_data;
  1704. int visible_width, visible_height, colorspace;
  1705. s->theora = get_bits_long(gb, 24);
  1706. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  1707. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  1708. /* but previous versions have the image flipped relative to vp3 */
  1709. if (s->theora < 0x030200)
  1710. {
  1711. s->flipped_image = 1;
  1712. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  1713. }
  1714. visible_width = s->width = get_bits(gb, 16) << 4;
  1715. visible_height = s->height = get_bits(gb, 16) << 4;
  1716. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  1717. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  1718. s->width= s->height= 0;
  1719. return -1;
  1720. }
  1721. if (s->theora >= 0x030200) {
  1722. visible_width = get_bits_long(gb, 24);
  1723. visible_height = get_bits_long(gb, 24);
  1724. skip_bits(gb, 8); /* offset x */
  1725. skip_bits(gb, 8); /* offset y */
  1726. }
  1727. skip_bits(gb, 32); /* fps numerator */
  1728. skip_bits(gb, 32); /* fps denumerator */
  1729. skip_bits(gb, 24); /* aspect numerator */
  1730. skip_bits(gb, 24); /* aspect denumerator */
  1731. if (s->theora < 0x030200)
  1732. skip_bits(gb, 5); /* keyframe frequency force */
  1733. colorspace = get_bits(gb, 8);
  1734. skip_bits(gb, 24); /* bitrate */
  1735. skip_bits(gb, 6); /* quality hint */
  1736. if (s->theora >= 0x030200)
  1737. {
  1738. skip_bits(gb, 5); /* keyframe frequency force */
  1739. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  1740. skip_bits(gb, 3); /* reserved */
  1741. }
  1742. // align_get_bits(gb);
  1743. if ( visible_width <= s->width && visible_width > s->width-16
  1744. && visible_height <= s->height && visible_height > s->height-16)
  1745. avcodec_set_dimensions(avctx, visible_width, visible_height);
  1746. else
  1747. avcodec_set_dimensions(avctx, s->width, s->height);
  1748. if (colorspace == 1) {
  1749. avctx->color_primaries = AVCOL_PRI_BT470M;
  1750. } else if (colorspace == 2) {
  1751. avctx->color_primaries = AVCOL_PRI_BT470BG;
  1752. }
  1753. if (colorspace == 1 || colorspace == 2) {
  1754. avctx->colorspace = AVCOL_SPC_BT470BG;
  1755. avctx->color_trc = AVCOL_TRC_BT709;
  1756. }
  1757. return 0;
  1758. }
  1759. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  1760. {
  1761. Vp3DecodeContext *s = avctx->priv_data;
  1762. int i, n, matrices, inter, plane;
  1763. if (s->theora >= 0x030200) {
  1764. n = get_bits(gb, 3);
  1765. /* loop filter limit values table */
  1766. for (i = 0; i < 64; i++) {
  1767. s->filter_limit_values[i] = get_bits(gb, n);
  1768. if (s->filter_limit_values[i] > 127) {
  1769. av_log(avctx, AV_LOG_ERROR, "filter limit value too large (%i > 127), clamping\n", s->filter_limit_values[i]);
  1770. s->filter_limit_values[i] = 127;
  1771. }
  1772. }
  1773. }
  1774. if (s->theora >= 0x030200)
  1775. n = get_bits(gb, 4) + 1;
  1776. else
  1777. n = 16;
  1778. /* quality threshold table */
  1779. for (i = 0; i < 64; i++)
  1780. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  1781. if (s->theora >= 0x030200)
  1782. n = get_bits(gb, 4) + 1;
  1783. else
  1784. n = 16;
  1785. /* dc scale factor table */
  1786. for (i = 0; i < 64; i++)
  1787. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  1788. if (s->theora >= 0x030200)
  1789. matrices = get_bits(gb, 9) + 1;
  1790. else
  1791. matrices = 3;
  1792. if(matrices > 384){
  1793. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  1794. return -1;
  1795. }
  1796. for(n=0; n<matrices; n++){
  1797. for (i = 0; i < 64; i++)
  1798. s->base_matrix[n][i]= get_bits(gb, 8);
  1799. }
  1800. for (inter = 0; inter <= 1; inter++) {
  1801. for (plane = 0; plane <= 2; plane++) {
  1802. int newqr= 1;
  1803. if (inter || plane > 0)
  1804. newqr = get_bits1(gb);
  1805. if (!newqr) {
  1806. int qtj, plj;
  1807. if(inter && get_bits1(gb)){
  1808. qtj = 0;
  1809. plj = plane;
  1810. }else{
  1811. qtj= (3*inter + plane - 1) / 3;
  1812. plj= (plane + 2) % 3;
  1813. }
  1814. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  1815. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  1816. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  1817. } else {
  1818. int qri= 0;
  1819. int qi = 0;
  1820. for(;;){
  1821. i= get_bits(gb, av_log2(matrices-1)+1);
  1822. if(i>= matrices){
  1823. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  1824. return -1;
  1825. }
  1826. s->qr_base[inter][plane][qri]= i;
  1827. if(qi >= 63)
  1828. break;
  1829. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  1830. s->qr_size[inter][plane][qri++]= i;
  1831. qi += i;
  1832. }
  1833. if (qi > 63) {
  1834. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  1835. return -1;
  1836. }
  1837. s->qr_count[inter][plane]= qri;
  1838. }
  1839. }
  1840. }
  1841. /* Huffman tables */
  1842. for (s->hti = 0; s->hti < 80; s->hti++) {
  1843. s->entries = 0;
  1844. s->huff_code_size = 1;
  1845. if (!get_bits1(gb)) {
  1846. s->hbits = 0;
  1847. if(read_huffman_tree(avctx, gb))
  1848. return -1;
  1849. s->hbits = 1;
  1850. if(read_huffman_tree(avctx, gb))
  1851. return -1;
  1852. }
  1853. }
  1854. s->theora_tables = 1;
  1855. return 0;
  1856. }
  1857. static av_cold int theora_decode_init(AVCodecContext *avctx)
  1858. {
  1859. Vp3DecodeContext *s = avctx->priv_data;
  1860. GetBitContext gb;
  1861. int ptype;
  1862. uint8_t *header_start[3];
  1863. int header_len[3];
  1864. int i;
  1865. s->theora = 1;
  1866. if (!avctx->extradata_size)
  1867. {
  1868. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  1869. return -1;
  1870. }
  1871. if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  1872. 42, header_start, header_len) < 0) {
  1873. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  1874. return -1;
  1875. }
  1876. for(i=0;i<3;i++) {
  1877. init_get_bits(&gb, header_start[i], header_len[i] * 8);
  1878. ptype = get_bits(&gb, 8);
  1879. if (!(ptype & 0x80))
  1880. {
  1881. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  1882. // return -1;
  1883. }
  1884. // FIXME: Check for this as well.
  1885. skip_bits_long(&gb, 6*8); /* "theora" */
  1886. switch(ptype)
  1887. {
  1888. case 0x80:
  1889. theora_decode_header(avctx, &gb);
  1890. break;
  1891. case 0x81:
  1892. // FIXME: is this needed? it breaks sometimes
  1893. // theora_decode_comments(avctx, gb);
  1894. break;
  1895. case 0x82:
  1896. if (theora_decode_tables(avctx, &gb))
  1897. return -1;
  1898. break;
  1899. default:
  1900. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  1901. break;
  1902. }
  1903. if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
  1904. av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  1905. if (s->theora < 0x030200)
  1906. break;
  1907. }
  1908. return vp3_decode_init(avctx);
  1909. }
  1910. AVCodec theora_decoder = {
  1911. "theora",
  1912. CODEC_TYPE_VIDEO,
  1913. CODEC_ID_THEORA,
  1914. sizeof(Vp3DecodeContext),
  1915. theora_decode_init,
  1916. NULL,
  1917. vp3_decode_end,
  1918. vp3_decode_frame,
  1919. CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
  1920. NULL,
  1921. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  1922. };
  1923. #endif
  1924. AVCodec vp3_decoder = {
  1925. "vp3",
  1926. CODEC_TYPE_VIDEO,
  1927. CODEC_ID_VP3,
  1928. sizeof(Vp3DecodeContext),
  1929. vp3_decode_init,
  1930. NULL,
  1931. vp3_decode_end,
  1932. vp3_decode_frame,
  1933. CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
  1934. NULL,
  1935. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  1936. };