vp3.c 94 KB

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