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