vp3.c 77 KB

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