vc9.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  1. /*
  2. * VC-9 and WMV3 decoder
  3. * Copyright (c) 2005 Anonymous
  4. * Copyright (c) 2005 Alex Beregszaszi
  5. * Copyright (c) 2005 Michael Niedermayer
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file vc9.c
  24. * VC-9 and WMV3 decoder
  25. *
  26. * TODO: most AP stuff, optimize, most of MB layer, transform, filtering and motion compensation, etc
  27. * TODO: use MPV_ !!
  28. */
  29. #include "common.h"
  30. #include "dsputil.h"
  31. #include "avcodec.h"
  32. #include "mpegvideo.h"
  33. #include "vc9data.h"
  34. #undef NDEBUG
  35. #include <assert.h>
  36. extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
  37. extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
  38. extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
  39. #define MB_INTRA_VLC_BITS 9
  40. extern VLC ff_msmp4_mb_i_vlc;
  41. #define DC_VLC_BITS 9
  42. static const uint16_t table_mb_intra[64][2];
  43. /* Some inhibiting stuff */
  44. #define HAS_ADVANCED_PROFILE 0
  45. #define TRACE 1
  46. #if TRACE
  47. # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  48. codes, codes_wrap, codes_size, use_static) \
  49. if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  50. codes, codes_wrap, codes_size, use_static) < 0) \
  51. { \
  52. av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i); \
  53. return -1; \
  54. }
  55. #else
  56. # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  57. codes, codes_wrap, codes_size, use_static) \
  58. init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  59. codes, codes_wrap, codes_size, use_static)
  60. #endif
  61. /** Available Profiles */
  62. //@{
  63. #define PROFILE_SIMPLE 0
  64. #define PROFILE_MAIN 1
  65. #define PROFILE_COMPLEX 2 ///< TODO: WMV9 specific
  66. #define PROFILE_ADVANCED 3
  67. //@}
  68. /** Sequence quantizer mode */
  69. //@{
  70. #define QUANT_FRAME_IMPLICIT 0 ///< Implicitly specified at frame level
  71. #define QUANT_FRAME_EXPLICIT 1 ///< Explicitly specified at frame level
  72. #define QUANT_NON_UNIFORM 2 ///< Non-uniform quant used for all frames
  73. #define QUANT_UNIFORM 3 ///< Uniform quant used for all frames
  74. //@}
  75. /** Where quant can be changed */
  76. //@{
  77. #define DQPROFILE_FOUR_EDGES 0
  78. #define DQPROFILE_DOUBLE_EDGES 1
  79. #define DQPROFILE_SINGLE_EDGE 2
  80. #define DQPROFILE_ALL_MBS 3
  81. //@}
  82. /** @name Where quant can be changed
  83. */
  84. //@{
  85. #define DQPROFILE_FOUR_EDGES 0
  86. #define DQSINGLE_BEDGE_LEFT 0
  87. #define DQSINGLE_BEDGE_TOP 1
  88. #define DQSINGLE_BEDGE_RIGHT 2
  89. #define DQSINGLE_BEDGE_BOTTOM 3
  90. //@}
  91. /** Which pair of edges is quantized with ALTPQUANT */
  92. //@{
  93. #define DQDOUBLE_BEDGE_TOPLEFT 0
  94. #define DQDOUBLE_BEDGE_TOPRIGHT 1
  95. #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
  96. #define DQDOUBLE_BEDGE_BOTTOMLEFT 3
  97. //@}
  98. /** MV modes for P frames */
  99. //@{
  100. #define MV_PMODE_1MV_HPEL_BILIN 0
  101. #define MV_PMODE_1MV 1
  102. #define MV_PMODE_1MV_HPEL 2
  103. #define MV_PMODE_MIXED_MV 3
  104. #define MV_PMODE_INTENSITY_COMP 4
  105. //@}
  106. /** @name MV types for B frames */
  107. //@{
  108. #define BMV_TYPE_BACKWARD 0
  109. #define BMV_TYPE_BACKWARD 0
  110. #define BMV_TYPE_FORWARD 1
  111. #define BMV_TYPE_INTERPOLATED 3
  112. //@}
  113. /** MV P mode - the 5th element is only used for mode 1 */
  114. static const uint8_t mv_pmode_table[2][5] = {
  115. { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
  116. { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
  117. };
  118. /** One more frame type */
  119. #define BI_TYPE 7
  120. static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
  121. fps_dr[2] = { 1000, 1001 };
  122. static const uint8_t pquant_table[3][32] = {
  123. { /* Implicit quantizer */
  124. 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
  125. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
  126. },
  127. { /* Explicit quantizer, pquantizer uniform */
  128. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  129. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  130. },
  131. { /* Explicit quantizer, pquantizer non-uniform */
  132. 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  133. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
  134. }
  135. };
  136. /** @name VC-9 VLC tables and defines
  137. * @todo TODO move this into the context
  138. */
  139. //@{
  140. #define VC9_BFRACTION_VLC_BITS 7
  141. static VLC vc9_bfraction_vlc;
  142. #define VC9_IMODE_VLC_BITS 4
  143. static VLC vc9_imode_vlc;
  144. #define VC9_NORM2_VLC_BITS 3
  145. static VLC vc9_norm2_vlc;
  146. #define VC9_NORM6_VLC_BITS 9
  147. static VLC vc9_norm6_vlc;
  148. /* Could be optimized, one table only needs 8 bits */
  149. #define VC9_TTMB_VLC_BITS 9 //12
  150. static VLC vc9_ttmb_vlc[3];
  151. #define VC9_MV_DIFF_VLC_BITS 9 //15
  152. static VLC vc9_mv_diff_vlc[4];
  153. #define VC9_CBPCY_P_VLC_BITS 9 //14
  154. static VLC vc9_cbpcy_p_vlc[4];
  155. #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
  156. static VLC vc9_4mv_block_pattern_vlc[4];
  157. #define VC9_TTBLK_VLC_BITS 5
  158. static VLC vc9_ttblk_vlc[3];
  159. #define VC9_SUBBLKPAT_VLC_BITS 6
  160. static VLC vc9_subblkpat_vlc[3];
  161. //@}
  162. /** Bitplane struct
  163. * We mainly need data and is_raw, so this struct could be avoided
  164. * to save a level of indirection; feel free to modify
  165. * @fixme For now, stride=width
  166. * @warning Data are bits, either 1 or 0
  167. */
  168. typedef struct BitPlane {
  169. uint8_t *data; ///< Data buffer
  170. int width; ///< Width of the buffer
  171. int stride; ///< Stride of the buffer
  172. int height; ///< Plane height
  173. uint8_t is_raw; ///< Bit values must be read at MB level
  174. } BitPlane;
  175. /** The VC9 Context
  176. * @fixme Change size wherever another size is more efficient
  177. * Many members are only used for Advanced Profile
  178. */
  179. typedef struct VC9Context{
  180. MpegEncContext s;
  181. /** Simple/Main Profile sequence header */
  182. //@{
  183. int res_sm; ///< reserved, 2b
  184. int res_x8; ///< reserved
  185. int multires; ///< frame-level RESPIC syntax element present
  186. int res_fasttx; ///< reserved, always 1
  187. int res_transtab; ///< reserved, always 0
  188. int rangered; ///< RANGEREDFRM (range reduction) syntax element present
  189. ///< at frame level
  190. int res_rtm_flag; ///< reserved, set to 1
  191. int reserved; ///< reserved
  192. //@}
  193. #if HAS_ADVANCED_PROFILE
  194. /** Advanced Profile */
  195. //@{
  196. int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
  197. int chromaformat; ///< 2bits, 2=4:2:0, only defined
  198. int postprocflag; ///< Per-frame processing suggestion flag present
  199. int broadcast; ///< TFF/RFF present
  200. int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
  201. int tfcntrflag; ///< TFCNTR present
  202. int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
  203. int extended_dmv; ///< Additional extended dmv range at P/B frame-level
  204. int color_prim; ///< 8bits, chroma coordinates of the color primaries
  205. int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
  206. int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
  207. int hrd_param_flag; ///< Presence of Hypothetical Reference
  208. ///< Decoder parameters
  209. //@}
  210. #endif
  211. /** Sequence header data for all Profiles
  212. * TODO: choose between ints, uint8_ts and monobit flags
  213. */
  214. //@{
  215. int profile; ///< 2bits, Profile
  216. int frmrtq_postproc; ///< 3bits,
  217. int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
  218. int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
  219. int extended_mv; ///< Ext MV in P/B (not in Simple)
  220. int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
  221. int vstransform; ///< variable-size [48]x[48] transform type + info
  222. int overlap; ///< overlapped transforms in use
  223. int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
  224. int finterpflag; ///< INTERPFRM present
  225. //@}
  226. /** Frame decoding info for all profiles */
  227. //@{
  228. uint8_t mv_mode; ///< MV coding monde
  229. uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
  230. int k_x; ///< Number of bits for MVs (depends on MV range)
  231. int k_y; ///< Number of bits for MVs (depends on MV range)
  232. uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
  233. /** pquant parameters */
  234. //@{
  235. uint8_t dquantfrm;
  236. uint8_t dqprofile;
  237. uint8_t dqsbedge;
  238. uint8_t dqbilevel;
  239. //@}
  240. /** AC coding set indexes
  241. * @see 8.1.1.10, p(1)10
  242. */
  243. //@{
  244. int c_ac_table_index; ///< Chroma index from ACFRM element
  245. int y_ac_table_index; ///< Luma index from AC2FRM element
  246. //@}
  247. int ttfrm; ///< Transform type info present at frame level
  248. uint8_t ttmbf; ///< Transform type flag
  249. int ttmb; ///< Transform type
  250. uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
  251. /** Luma compensation parameters */
  252. //@{
  253. uint8_t lumscale;
  254. uint8_t lumshift;
  255. //@}
  256. int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
  257. uint8_t halfpq; ///< Uniform quant over image and qp+.5
  258. uint8_t respic; ///< Frame-level flag for resized images
  259. int buffer_fullness; ///< HRD info
  260. /** Ranges:
  261. * -# 0 -> [-64n 63.f] x [-32, 31.f]
  262. * -# 1 -> [-128, 127.f] x [-64, 63.f]
  263. * -# 2 -> [-512, 511.f] x [-128, 127.f]
  264. * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
  265. */
  266. uint8_t mvrange;
  267. uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
  268. uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
  269. VLC *cbpcy_vlc; ///< CBPCY VLC table
  270. int tt_index; ///< Index for Transform Type tables
  271. BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
  272. BitPlane skip_mb_plane; ///< bitplane for skipped MBs
  273. BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
  274. /** Frame decoding info for S/M profiles only */
  275. //@{
  276. uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
  277. uint8_t interpfrm;
  278. //@}
  279. #if HAS_ADVANCED_PROFILE
  280. /** Frame decoding info for Advanced profile */
  281. //@{
  282. uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
  283. uint8_t numpanscanwin;
  284. uint8_t tfcntr;
  285. uint8_t rptfrm, tff, rff;
  286. uint16_t topleftx;
  287. uint16_t toplefty;
  288. uint16_t bottomrightx;
  289. uint16_t bottomrighty;
  290. uint8_t uvsamp;
  291. uint8_t postproc;
  292. int hrd_num_leaky_buckets;
  293. uint8_t bit_rate_exponent;
  294. uint8_t buffer_size_exponent;
  295. BitPlane ac_pred_plane; ///< AC prediction flags bitplane
  296. BitPlane over_flags_plane; ///< Overflags bitplane
  297. uint8_t condover;
  298. uint16_t *hrd_rate, *hrd_buffer;
  299. uint8_t *hrd_fullness;
  300. uint8_t range_mapy_flag;
  301. uint8_t range_mapuv_flag;
  302. uint8_t range_mapy;
  303. uint8_t range_mapuv;
  304. //@}
  305. #endif
  306. } VC9Context;
  307. /**
  308. * Get unary code of limited length
  309. * @fixme FIXME Slow and ugly
  310. * @param gb GetBitContext
  311. * @param[in] stop The bitstop value (unary code of 1's or 0's)
  312. * @param[in] len Maximum length
  313. * @return Unary length/index
  314. */
  315. static int get_prefix(GetBitContext *gb, int stop, int len)
  316. {
  317. #if 1
  318. int i = 0, tmp = !stop;
  319. while (i != len && tmp != stop)
  320. {
  321. tmp = get_bits(gb, 1);
  322. i++;
  323. }
  324. if (i == len && tmp != stop) return len+1;
  325. return i;
  326. #else
  327. unsigned int buf;
  328. int log;
  329. OPEN_READER(re, gb);
  330. UPDATE_CACHE(re, gb);
  331. buf=GET_CACHE(re, gb); //Still not sure
  332. if (stop) buf = ~buf;
  333. log= av_log2(-buf); //FIXME: -?
  334. if (log < limit){
  335. LAST_SKIP_BITS(re, gb, log+1);
  336. CLOSE_READER(re, gb);
  337. return log;
  338. }
  339. LAST_SKIP_BITS(re, gb, limit);
  340. CLOSE_READER(re, gb);
  341. return limit;
  342. #endif
  343. }
  344. /**
  345. * Init VC-9 specific tables and VC9Context members
  346. * @param v The VC9Context to initialize
  347. * @return Status
  348. */
  349. static int vc9_init_common(VC9Context *v)
  350. {
  351. static int done = 0;
  352. int i = 0;
  353. /* Set the bit planes */
  354. v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  355. v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  356. v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  357. #if HAS_ADVANCED_PROFILE
  358. v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  359. v->hrd_rate = v->hrd_buffer = NULL;
  360. #endif
  361. /* VLC tables */
  362. #if 0 // spec -> actual tables converter
  363. for(i=0; i<64; i++){
  364. int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
  365. av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
  366. if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  367. }
  368. for(i=0; i<64; i++){
  369. int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4];
  370. av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
  371. if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  372. }
  373. #endif
  374. if(!done)
  375. {
  376. done = 1;
  377. INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23,
  378. vc9_bfraction_bits, 1, 1,
  379. vc9_bfraction_codes, 1, 1, 1);
  380. INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
  381. vc9_norm2_bits, 1, 1,
  382. vc9_norm2_codes, 1, 1, 1);
  383. INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
  384. vc9_norm6_bits, 1, 1,
  385. vc9_norm6_codes, 2, 2, 1);
  386. INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
  387. vc9_imode_bits, 1, 1,
  388. vc9_imode_codes, 1, 1, 1);
  389. for (i=0; i<3; i++)
  390. {
  391. INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
  392. vc9_ttmb_bits[i], 1, 1,
  393. vc9_ttmb_codes[i], 2, 2, 1);
  394. INIT_VLC(&vc9_ttblk_vlc[i], VC9_TTBLK_VLC_BITS, 8,
  395. vc9_ttblk_bits[i], 1, 1,
  396. vc9_ttblk_codes[i], 1, 1, 1);
  397. INIT_VLC(&vc9_subblkpat_vlc[i], VC9_SUBBLKPAT_VLC_BITS, 15,
  398. vc9_subblkpat_bits[i], 1, 1,
  399. vc9_subblkpat_codes[i], 1, 1, 1);
  400. }
  401. for(i=0; i<4; i++)
  402. {
  403. INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
  404. vc9_4mv_block_pattern_bits[i], 1, 1,
  405. vc9_4mv_block_pattern_codes[i], 1, 1, 1);
  406. INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64,
  407. vc9_cbpcy_p_bits[i], 1, 1,
  408. vc9_cbpcy_p_codes[i], 2, 2, 1);
  409. INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73,
  410. vc9_mv_diff_bits[i], 1, 1,
  411. vc9_mv_diff_codes[i], 2, 2, 1);
  412. }
  413. }
  414. /* Other defaults */
  415. v->pq = -1;
  416. v->mvrange = 0; /* 7.1.1.18, p80 */
  417. return 0;
  418. }
  419. #if HAS_ADVANCED_PROFILE
  420. /**
  421. * Decode sequence header's Hypothetic Reference Decoder data
  422. * @see 6.2.1, p32
  423. * @param v The VC9Context to initialize
  424. * @param gb A GetBitContext initialized from AVCodecContext extra_data
  425. * @return Status
  426. */
  427. static int decode_hrd(VC9Context *v, GetBitContext *gb)
  428. {
  429. int i, num;
  430. num = 1 + get_bits(gb, 5);
  431. /*hrd rate*/
  432. if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
  433. {
  434. av_freep(&v->hrd_rate);
  435. }
  436. if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
  437. if (!v->hrd_rate) return -1;
  438. /*hrd buffer*/
  439. if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
  440. {
  441. av_freep(&v->hrd_buffer);
  442. }
  443. if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
  444. if (!v->hrd_buffer)
  445. {
  446. av_freep(&v->hrd_rate);
  447. return -1;
  448. }
  449. /*hrd fullness*/
  450. if (v->hrd_fullness || num != v->hrd_num_leaky_buckets)
  451. {
  452. av_freep(&v->hrd_buffer);
  453. }
  454. if (!v->hrd_fullness) v->hrd_fullness = av_malloc(num*sizeof(uint8_t));
  455. if (!v->hrd_fullness)
  456. {
  457. av_freep(&v->hrd_rate);
  458. av_freep(&v->hrd_buffer);
  459. return -1;
  460. }
  461. v->hrd_num_leaky_buckets = num;
  462. //exponent in base-2 for rate
  463. v->bit_rate_exponent = 6 + get_bits(gb, 4);
  464. //exponent in base-2 for buffer_size
  465. v->buffer_size_exponent = 4 + get_bits(gb, 4);
  466. for (i=0; i<num; i++)
  467. {
  468. //mantissae, ordered (if not, use a function ?
  469. v->hrd_rate[i] = 1 + get_bits(gb, 16);
  470. if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
  471. {
  472. av_log(v->s.avctx, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
  473. "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
  474. return -1;
  475. }
  476. v->hrd_buffer[i] = 1 + get_bits(gb, 16);
  477. if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
  478. {
  479. av_log(v->s.avctx, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
  480. "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
  481. return -1;
  482. }
  483. }
  484. return 0;
  485. }
  486. /**
  487. * Decode sequence header for Advanced Profile
  488. * @see Table 2, p18
  489. * @see 6.1.7, pp21-27
  490. * @param v The VC9Context to initialize
  491. * @param gb A GetBitContext initialized from AVCodecContext extra_data
  492. * @return Status
  493. */
  494. static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  495. {
  496. VC9Context *v = avctx->priv_data;
  497. int nr, dr, aspect_ratio;
  498. v->postprocflag = get_bits(gb, 1);
  499. v->broadcast = get_bits(gb, 1);
  500. v->interlace = get_bits(gb, 1);
  501. v->tfcntrflag = get_bits(gb, 1);
  502. v->finterpflag = get_bits(gb, 1); //common
  503. v->panscanflag = get_bits(gb, 1);
  504. v->reserved = get_bits(gb, 1);
  505. if (v->reserved)
  506. {
  507. av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
  508. v->reserved);
  509. return -1;
  510. }
  511. if (v->extended_mv)
  512. v->extended_dmv = get_bits(gb, 1);
  513. /* 6.1.7, p21 */
  514. if (get_bits(gb, 1) /* pic_size_flag */)
  515. {
  516. avctx->coded_width = get_bits(gb, 12) << 1;
  517. avctx->coded_height = get_bits(gb, 12) << 1;
  518. if ( get_bits(gb, 1) /* disp_size_flag */)
  519. {
  520. avctx->width = get_bits(gb, 14);
  521. avctx->height = get_bits(gb, 14);
  522. }
  523. /* 6.1.7.4, p23 */
  524. if ( get_bits(gb, 1) /* aspect_ratio_flag */)
  525. {
  526. aspect_ratio = get_bits(gb, 4); //SAR
  527. if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
  528. {
  529. avctx->sample_aspect_ratio.num = 1 + get_bits(gb, 8);
  530. avctx->sample_aspect_ratio.den = 1 + get_bits(gb, 8);
  531. }
  532. else if (aspect_ratio == 0x0E)
  533. {
  534. av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
  535. }
  536. else
  537. {
  538. avctx->sample_aspect_ratio = vc9_pixel_aspect[aspect_ratio];
  539. }
  540. }
  541. }
  542. else
  543. {
  544. avctx->coded_width = avctx->width;
  545. avctx->coded_height = avctx->height;
  546. }
  547. /* 6.1.8, p23 */
  548. if ( get_bits(gb, 1) /* framerateflag */)
  549. {
  550. if ( !get_bits(gb, 1) /* framerateind */)
  551. {
  552. nr = get_bits(gb, 8);
  553. dr = get_bits(gb, 4);
  554. if (nr<1)
  555. {
  556. av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
  557. return -1;
  558. }
  559. if (nr>5)
  560. {
  561. av_log(avctx, AV_LOG_ERROR,
  562. "Reserved FRAMERATENR %i not handled\n", nr);
  563. nr = 5; /* overflow protection */
  564. }
  565. if (dr<1)
  566. {
  567. av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
  568. return -1;
  569. }
  570. if (dr>2)
  571. {
  572. av_log(avctx, AV_LOG_ERROR,
  573. "Reserved FRAMERATEDR %i not handled\n", dr);
  574. dr = 2; /* overflow protection */
  575. }
  576. avctx->time_base.num = fps_nr[dr - 1];
  577. avctx->time_base.den = fps_nr[nr - 1];
  578. }
  579. else
  580. {
  581. nr = get_bits(gb, 16);
  582. // 0.03125->2048Hz / 0.03125Hz
  583. avctx->time_base.den = 1000000;
  584. avctx->time_base.num = 31250*(1+nr);
  585. }
  586. }
  587. /* 6.1.9, p25 */
  588. if ( get_bits(gb, 1) /* color_format_flag */)
  589. {
  590. //Chromacity coordinates of color primaries
  591. //like ITU-R BT.709-2, BT.470-2, ...
  592. v->color_prim = get_bits(gb, 8);
  593. if (v->color_prim<1)
  594. {
  595. av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is forbidden\n");
  596. return -1;
  597. }
  598. if (v->color_prim == 3 || v->color_prim>6)
  599. {
  600. av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
  601. v->color_prim);
  602. return -1;
  603. }
  604. //Opto-electronic transfer characteristics
  605. v->transfer_char = get_bits(gb, 8);
  606. if (v->transfer_char < 1)
  607. {
  608. av_log(avctx, AV_LOG_ERROR, "0 for TRAMSFER_CHAR is forbidden\n");
  609. return -1;
  610. }
  611. if (v->transfer_char == 3 || v->transfer_char>8)
  612. {
  613. av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
  614. v->color_prim);
  615. return -1;
  616. }
  617. //Matrix coefficient for primariev->YCbCr
  618. v->matrix_coef = get_bits(gb, 8);
  619. if (v->matrix_coef < 1)
  620. {
  621. av_log(avctx, AV_LOG_ERROR, "0 for MATRIX_COEF is forbidden\n");
  622. return -1;
  623. }
  624. if ((v->matrix_coef > 2 && v->matrix_coef < 6) || v->matrix_coef > 7)
  625. {
  626. av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
  627. v->color_prim);
  628. return -1;
  629. }
  630. }
  631. //Hypothetical reference decoder indicator flag
  632. v->hrd_param_flag = get_bits(gb, 1);
  633. if (v->hrd_param_flag)
  634. {
  635. if (decode_hrd(v, gb) < 0) return -1;
  636. }
  637. /*reset scaling ranges, 6.2.2 & 6.2.3, p33*/
  638. v->range_mapy_flag = 0;
  639. v->range_mapuv_flag = 0;
  640. av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
  641. return -1;
  642. }
  643. #endif
  644. /**
  645. * Decode Simple/Main Profiles sequence header
  646. * @see Figure 7-8, p16-17
  647. * @param avctx Codec context
  648. * @param gb GetBit context initialized from Codec context extra_data
  649. * @return Status
  650. */
  651. static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  652. {
  653. VC9Context *v = avctx->priv_data;
  654. av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
  655. v->profile = get_bits(gb, 2);
  656. if (v->profile == 2)
  657. {
  658. av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden\n");
  659. return -1;
  660. }
  661. #if HAS_ADVANCED_PROFILE
  662. if (v->profile == PROFILE_ADVANCED)
  663. {
  664. v->level = get_bits(gb, 3);
  665. if(v->level >= 5)
  666. {
  667. av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
  668. }
  669. v->chromaformat = get_bits(gb, 2);
  670. if (v->chromaformat != 1)
  671. {
  672. av_log(avctx, AV_LOG_ERROR,
  673. "Only 4:2:0 chroma format supported\n");
  674. return -1;
  675. }
  676. }
  677. else
  678. #endif
  679. {
  680. v->res_sm = get_bits(gb, 2); //reserved
  681. if (v->res_sm)
  682. {
  683. av_log(avctx, AV_LOG_ERROR,
  684. "Reserved RES_SM=%i is forbidden\n", v->res_sm);
  685. return -1;
  686. }
  687. }
  688. // (fps-2)/4 (->30)
  689. v->frmrtq_postproc = get_bits(gb, 3); //common
  690. // (bitrate-32kbps)/64kbps
  691. v->bitrtq_postproc = get_bits(gb, 5); //common
  692. v->s.loop_filter = get_bits(gb, 1); //common
  693. if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
  694. {
  695. av_log(avctx, AV_LOG_ERROR,
  696. "LOOPFILTER shell not be enabled in simple profile\n");
  697. }
  698. #if HAS_ADVANCED_PROFILE
  699. if (v->profile < PROFILE_ADVANCED)
  700. #endif
  701. {
  702. v->res_x8 = get_bits(gb, 1); //reserved
  703. if (v->res_x8)
  704. {
  705. av_log(avctx, AV_LOG_ERROR,
  706. "1 for reserved RES_X8 is forbidden\n");
  707. //return -1;
  708. }
  709. v->multires = get_bits(gb, 1);
  710. v->res_fasttx = get_bits(gb, 1);
  711. if (!v->res_fasttx)
  712. {
  713. av_log(avctx, AV_LOG_ERROR,
  714. "0 for reserved RES_FASTTX is forbidden\n");
  715. //return -1;
  716. }
  717. }
  718. v->fastuvmc = get_bits(gb, 1); //common
  719. if (!v->profile && !v->fastuvmc)
  720. {
  721. av_log(avctx, AV_LOG_ERROR,
  722. "FASTUVMC unavailable in Simple Profile\n");
  723. return -1;
  724. }
  725. v->extended_mv = get_bits(gb, 1); //common
  726. if (!v->profile && v->extended_mv)
  727. {
  728. av_log(avctx, AV_LOG_ERROR,
  729. "Extended MVs unavailable in Simple Profile\n");
  730. return -1;
  731. }
  732. v->dquant = get_bits(gb, 2); //common
  733. v->vstransform = get_bits(gb, 1); //common
  734. #if HAS_ADVANCED_PROFILE
  735. if (v->profile < PROFILE_ADVANCED)
  736. #endif
  737. {
  738. v->res_transtab = get_bits(gb, 1);
  739. if (v->res_transtab)
  740. {
  741. av_log(avctx, AV_LOG_ERROR,
  742. "1 for reserved RES_TRANSTAB is forbidden\n");
  743. return -1;
  744. }
  745. }
  746. v->overlap = get_bits(gb, 1); //common
  747. #if HAS_ADVANCED_PROFILE
  748. if (v->profile < PROFILE_ADVANCED)
  749. #endif
  750. {
  751. v->s.resync_marker = get_bits(gb, 1);
  752. v->rangered = get_bits(gb, 1);
  753. if (v->rangered && v->profile == PROFILE_SIMPLE)
  754. {
  755. av_log(avctx, AV_LOG_DEBUG,
  756. "RANGERED should be set to 0 in simple profile\n");
  757. }
  758. }
  759. v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
  760. v->quantizer_mode = get_bits(gb, 2); //common
  761. #if HAS_ADVANCED_PROFILE
  762. if (v->profile < PROFILE_ADVANCED)
  763. #endif
  764. {
  765. v->finterpflag = get_bits(gb, 1); //common
  766. v->res_rtm_flag = get_bits(gb, 1); //reserved
  767. if (!v->res_rtm_flag)
  768. {
  769. av_log(avctx, AV_LOG_ERROR,
  770. "0 for reserved RES_RTM_FLAG is forbidden\n");
  771. //return -1;
  772. }
  773. #if TRACE
  774. av_log(avctx, AV_LOG_INFO,
  775. "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  776. "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
  777. "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
  778. "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
  779. v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
  780. v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
  781. v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
  782. v->dquant, v->quantizer_mode, avctx->max_b_frames
  783. );
  784. return 0;
  785. #endif
  786. }
  787. #if HAS_ADVANCED_PROFILE
  788. else return decode_advanced_sequence_header(avctx, gb);
  789. #endif
  790. }
  791. #if HAS_ADVANCED_PROFILE
  792. /** Entry point decoding (Advanced Profile)
  793. * @param avctx Codec context
  794. * @param gb GetBit context initialized from avctx->extra_data
  795. * @return Status
  796. */
  797. static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
  798. {
  799. VC9Context *v = avctx->priv_data;
  800. int i;
  801. if (v->profile != PROFILE_ADVANCED)
  802. {
  803. av_log(avctx, AV_LOG_ERROR,
  804. "Entry point are only defined in Advanced Profile!\n");
  805. return -1; //Only for advanced profile!
  806. }
  807. if (v->hrd_param_flag)
  808. {
  809. //Update buffer fullness
  810. av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
  811. assert(v->hrd_num_leaky_buckets > 0);
  812. for (i=0; i<v->hrd_num_leaky_buckets; i++)
  813. v->hrd_fullness[i] = get_bits(gb, 8);
  814. }
  815. if ((v->range_mapy_flag = get_bits(gb, 1)))
  816. {
  817. //RANGE_MAPY
  818. av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
  819. v->range_mapy = get_bits(gb, 3);
  820. }
  821. if ((v->range_mapuv_flag = get_bits(gb, 1)))
  822. {
  823. //RANGE_MAPUV
  824. av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
  825. v->range_mapuv = get_bits(gb, 3);
  826. }
  827. if (v->panscanflag)
  828. {
  829. //NUMPANSCANWIN
  830. v->numpanscanwin = get_bits(gb, 3);
  831. av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
  832. }
  833. return 0;
  834. }
  835. #endif
  836. /***********************************************************************/
  837. /**
  838. * @defgroup bitplane VC9 Bitplane decoding
  839. * @see 8.7, p56
  840. * @{
  841. */
  842. /** @addtogroup bitplane
  843. * Imode types
  844. * @{
  845. */
  846. #define IMODE_RAW 0
  847. #define IMODE_NORM2 1
  848. #define IMODE_DIFF2 2
  849. #define IMODE_NORM6 3
  850. #define IMODE_DIFF6 4
  851. #define IMODE_ROWSKIP 5
  852. #define IMODE_COLSKIP 6
  853. /** @} */ //imode defines
  854. /** Allocate the buffer from a bitplane, given its dimensions
  855. * @param bp Bitplane which buffer is to allocate
  856. * @param[in] width Width of the buffer
  857. * @param[in] height Height of the buffer
  858. * @return Status
  859. * @todo TODO: Take into account stride
  860. * @todo TODO: Allow use of external buffers ?
  861. */
  862. static int alloc_bitplane(BitPlane *bp, int width, int height)
  863. {
  864. if (!bp || bp->width<0 || bp->height<0) return -1;
  865. bp->data = (uint8_t*)av_malloc(width*height);
  866. if (!bp->data) return -1;
  867. bp->width = bp->stride = width;
  868. bp->height = height;
  869. return 0;
  870. }
  871. /** Free the bitplane's buffer
  872. * @param bp Bitplane which buffer is to free
  873. */
  874. static void free_bitplane(BitPlane *bp)
  875. {
  876. bp->width = bp->stride = bp->height = 0;
  877. if (bp->data) av_freep(&bp->data);
  878. }
  879. /** Decode rows by checking if they are skipped
  880. * @param plane Buffer to store decoded bits
  881. * @param[in] width Width of this buffer
  882. * @param[in] height Height of this buffer
  883. * @param[in] stride of this buffer
  884. */
  885. static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  886. int x, y;
  887. for (y=0; y<height; y++){
  888. if (!get_bits(gb, 1)) //rowskip
  889. memset(plane, 0, width);
  890. else
  891. for (x=0; x<width; x++)
  892. plane[x] = get_bits(gb, 1);
  893. plane += stride;
  894. }
  895. }
  896. /** Decode columns by checking if they are skipped
  897. * @param plane Buffer to store decoded bits
  898. * @param[in] width Width of this buffer
  899. * @param[in] height Height of this buffer
  900. * @param[in] stride of this buffer
  901. * @fixme FIXME: Optimize
  902. */
  903. static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  904. int x, y;
  905. for (x=0; x<width; x++){
  906. if (!get_bits(gb, 1)) //colskip
  907. for (y=0; y<height; y++)
  908. plane[y*stride] = 0;
  909. else
  910. for (y=0; y<height; y++)
  911. plane[y*stride] = get_bits(gb, 1);
  912. plane ++;
  913. }
  914. }
  915. /** Decode a bitplane's bits
  916. * @param bp Bitplane where to store the decode bits
  917. * @param v VC9 context for bit reading and logging
  918. * @return Status
  919. * @fixme FIXME: Optimize
  920. * @todo TODO: Decide if a struct is needed
  921. */
  922. static int bitplane_decoding(BitPlane *bp, VC9Context *v)
  923. {
  924. GetBitContext *gb = &v->s.gb;
  925. int imode, x, y, code, use_vertical_tile, tile_w, tile_h, offset;
  926. uint8_t invert, *planep = bp->data;
  927. invert = get_bits(gb, 1);
  928. imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
  929. bp->is_raw = 0;
  930. switch (imode)
  931. {
  932. case IMODE_RAW:
  933. //Data is actually read in the MB layer (same for all tests == "raw")
  934. bp->is_raw = 1; //invert ignored
  935. return invert;
  936. case IMODE_DIFF2:
  937. case IMODE_NORM2:
  938. if ((bp->height*bp->width) & 1)
  939. {
  940. *(++planep) = get_bits(gb, 1);
  941. offset = x = 1;
  942. }
  943. else offset = x = 0;
  944. for (y=0; y<bp->height; y++)
  945. {
  946. for(; x<bp->width; x+=2)
  947. {
  948. code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
  949. *(++planep) = code&1; //lsb => left
  950. *(++planep) = (code>>1)&1; //msb => right
  951. }
  952. planep += bp->stride-bp->width;
  953. if ((bp->width-offset)&1) //Odd number previously processed
  954. {
  955. code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
  956. *planep = code&1;
  957. planep += bp->stride-bp->width;
  958. *planep = (code>>1)&1; //msb => right
  959. offset = x = 1;
  960. }
  961. else
  962. {
  963. offset = x = 0;
  964. planep += bp->stride-bp->width;
  965. }
  966. }
  967. break;
  968. case IMODE_DIFF6:
  969. case IMODE_NORM6:
  970. use_vertical_tile= bp->height%3==0 && bp->width%3!=0;
  971. tile_w= use_vertical_tile ? 2 : 3;
  972. tile_h= use_vertical_tile ? 3 : 2;
  973. for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
  974. for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
  975. code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
  976. if(code<0){
  977. av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
  978. return -1;
  979. }
  980. //FIXME following is a pure guess and probably wrong
  981. //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
  982. planep[x + 0*bp->stride]= (code>>0)&1;
  983. planep[x + 1 + 0*bp->stride]= (code>>1)&1;
  984. //FIXME Does branch prediction help here?
  985. if(use_vertical_tile){
  986. planep[x + 0 + 1*bp->stride]= (code>>2)&1;
  987. planep[x + 1 + 1*bp->stride]= (code>>3)&1;
  988. planep[x + 0 + 2*bp->stride]= (code>>4)&1;
  989. planep[x + 1 + 2*bp->stride]= (code>>5)&1;
  990. }else{
  991. planep[x + 2 + 0*bp->stride]= (code>>2)&1;
  992. planep[x + 0 + 1*bp->stride]= (code>>3)&1;
  993. planep[x + 1 + 1*bp->stride]= (code>>4)&1;
  994. planep[x + 2 + 1*bp->stride]= (code>>5)&1;
  995. }
  996. }
  997. }
  998. x= bp->width % tile_w;
  999. decode_colskip(bp->data , x, bp->height , bp->stride, &v->s.gb);
  1000. decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, &v->s.gb);
  1001. break;
  1002. case IMODE_ROWSKIP:
  1003. decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
  1004. break;
  1005. case IMODE_COLSKIP:
  1006. decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
  1007. break;
  1008. default: break;
  1009. }
  1010. /* Applying diff operator */
  1011. if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
  1012. {
  1013. planep = bp->data;
  1014. planep[0] ^= invert;
  1015. for (x=1; x<bp->width; x++)
  1016. planep[x] ^= planep[x-1];
  1017. for (y=1; y<bp->height; y++)
  1018. {
  1019. planep += bp->stride;
  1020. planep[0] ^= planep[-bp->stride];
  1021. for (x=1; x<bp->width; x++)
  1022. {
  1023. if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
  1024. else planep[x] ^= planep[x-1];
  1025. }
  1026. }
  1027. }
  1028. else if (invert)
  1029. {
  1030. planep = bp->data;
  1031. for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
  1032. }
  1033. return (imode<<1) + invert;
  1034. }
  1035. /** @} */ //Bitplane group
  1036. /***********************************************************************/
  1037. /** VOP Dquant decoding
  1038. * @param v VC9 Context
  1039. */
  1040. static int vop_dquant_decoding(VC9Context *v)
  1041. {
  1042. GetBitContext *gb = &v->s.gb;
  1043. int pqdiff;
  1044. //variable size
  1045. if (v->dquant == 2)
  1046. {
  1047. pqdiff = get_bits(gb, 3);
  1048. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  1049. else v->altpq = v->pq + pqdiff + 1;
  1050. }
  1051. else
  1052. {
  1053. v->dquantfrm = get_bits(gb, 1);
  1054. if ( v->dquantfrm )
  1055. {
  1056. v->dqprofile = get_bits(gb, 2);
  1057. switch (v->dqprofile)
  1058. {
  1059. case DQPROFILE_SINGLE_EDGE:
  1060. case DQPROFILE_DOUBLE_EDGES:
  1061. v->dqsbedge = get_bits(gb, 2);
  1062. break;
  1063. case DQPROFILE_ALL_MBS:
  1064. v->dqbilevel = get_bits(gb, 1);
  1065. default: break; //Forbidden ?
  1066. }
  1067. if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
  1068. {
  1069. pqdiff = get_bits(gb, 3);
  1070. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  1071. else v->altpq = v->pq + pqdiff + 1;
  1072. }
  1073. }
  1074. }
  1075. return 0;
  1076. }
  1077. /***********************************************************************/
  1078. /**
  1079. * @defgroup all_frame_hdr All VC9 profiles frame header
  1080. * @brief Part of the frame header decoding from all profiles
  1081. * @warning Only pro/epilog differs between Simple/Main and Advanced => check caller
  1082. * @{
  1083. */
  1084. /** B and BI frame header decoding, primary part
  1085. * @see Tables 11+12, p62-65
  1086. * @param v VC9 context
  1087. * @return Status
  1088. * @warning Also handles BI frames
  1089. */
  1090. static int decode_b_picture_primary_header(VC9Context *v)
  1091. {
  1092. GetBitContext *gb = &v->s.gb;
  1093. int pqindex;
  1094. /* Prolog common to all frametypes should be done in caller */
  1095. if (v->profile == PROFILE_SIMPLE)
  1096. {
  1097. av_log(v->s.avctx, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n");
  1098. return FRAME_SKIPPED;
  1099. }
  1100. v->bfraction = vc9_bfraction_lut[get_vlc2(gb, vc9_bfraction_vlc.table,
  1101. VC9_BFRACTION_VLC_BITS, 2)];
  1102. if (v->bfraction < -1)
  1103. {
  1104. av_log(v->s.avctx, AV_LOG_ERROR, "Invalid BFRaction\n");
  1105. return FRAME_SKIPPED;
  1106. }
  1107. else if (!v->bfraction)
  1108. {
  1109. /* We actually have a BI frame */
  1110. v->s.pict_type = BI_TYPE;
  1111. v->buffer_fullness = get_bits(gb, 7);
  1112. }
  1113. /* Read the quantization stuff */
  1114. pqindex = get_bits(gb, 5);
  1115. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1116. v->pq = pquant_table[0][pqindex];
  1117. else
  1118. {
  1119. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1120. }
  1121. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1122. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1123. v->pquantizer = get_bits(gb, 1);
  1124. #if HAS_ADVANCED_PROFILE
  1125. if (v->profile == PROFILE_ADVANCED)
  1126. {
  1127. if (v->postprocflag) v->postproc = get_bits(gb, 2);
  1128. if (v->extended_mv == 1 && v->s.pict_type != BI_TYPE)
  1129. v->mvrange = get_prefix(gb, 0, 3);
  1130. }
  1131. #endif
  1132. else
  1133. {
  1134. if (v->extended_mv == 1)
  1135. v->mvrange = get_prefix(gb, 0, 3);
  1136. }
  1137. /* Read the MV mode */
  1138. if (v->s.pict_type != BI_TYPE)
  1139. {
  1140. v->mv_mode = get_bits(gb, 1);
  1141. if (v->pq < 13)
  1142. {
  1143. if (!v->mv_mode)
  1144. {
  1145. v->mv_mode = get_bits(gb, 2);
  1146. if (v->mv_mode)
  1147. av_log(v->s.avctx, AV_LOG_ERROR,
  1148. "mv_mode for lowquant B frame was %i\n", v->mv_mode);
  1149. }
  1150. }
  1151. else
  1152. {
  1153. if (!v->mv_mode)
  1154. {
  1155. if (get_bits(gb, 1))
  1156. av_log(v->s.avctx, AV_LOG_ERROR,
  1157. "mv_mode for highquant B frame was %i\n", v->mv_mode);
  1158. }
  1159. v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping
  1160. }
  1161. }
  1162. return 0;
  1163. }
  1164. /** B and BI frame header decoding, secondary part
  1165. * @see Tables 11+12, p62-65
  1166. * @param v VC9 context
  1167. * @return Status
  1168. * @warning Also handles BI frames
  1169. * @warning To call once all MB arrays are allocated
  1170. * @todo Support Advanced Profile headers
  1171. */
  1172. static int decode_b_picture_secondary_header(VC9Context *v)
  1173. {
  1174. GetBitContext *gb = &v->s.gb;
  1175. int status;
  1176. status = bitplane_decoding(&v->skip_mb_plane, v);
  1177. if (status < 0) return -1;
  1178. #if TRACE
  1179. if (v->mv_mode == MV_PMODE_MIXED_MV)
  1180. {
  1181. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  1182. if (status < 0)
  1183. return -1;
  1184. #if TRACE
  1185. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1186. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1187. #endif
  1188. }
  1189. //bitplane
  1190. status = bitplane_decoding(&v->direct_mb_plane, v);
  1191. if (status < 0) return -1;
  1192. #if TRACE
  1193. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct plane encoding: "
  1194. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1195. #endif
  1196. av_log(v->s.avctx, AV_LOG_DEBUG, "Skip MB plane encoding: "
  1197. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1198. #endif
  1199. /* FIXME: what is actually chosen for B frames ? */
  1200. v->s.mv_table_index = get_bits(gb, 2); //but using vc9_ tables
  1201. v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)];
  1202. if (v->dquant)
  1203. {
  1204. vop_dquant_decoding(v);
  1205. }
  1206. if (v->vstransform)
  1207. {
  1208. v->ttmbf = get_bits(gb, 1);
  1209. if (v->ttmbf)
  1210. {
  1211. v->ttfrm = get_bits(gb, 2);
  1212. av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
  1213. (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
  1214. }
  1215. }
  1216. /* Epilog (AC/DC syntax) should be done in caller */
  1217. return 0;
  1218. }
  1219. /** I frame header decoding, primary part
  1220. * @see Tables 5+7, p53-54 and 55-57
  1221. * @param v VC9 context
  1222. * @return Status
  1223. * @todo Support Advanced Profile headers
  1224. */
  1225. static int decode_i_picture_primary_header(VC9Context *v)
  1226. {
  1227. GetBitContext *gb = &v->s.gb;
  1228. int pqindex;
  1229. /* Prolog common to all frametypes should be done in caller */
  1230. //BF = Buffer Fullness
  1231. if (v->profile < PROFILE_ADVANCED && get_bits(gb, 7))
  1232. {
  1233. av_log(v->s.avctx, AV_LOG_DEBUG, "I BufferFullness not 0\n");
  1234. }
  1235. /* Quantizer stuff */
  1236. pqindex = get_bits(gb, 5);
  1237. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1238. v->pq = pquant_table[0][pqindex];
  1239. else
  1240. {
  1241. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1242. }
  1243. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1244. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1245. v->pquantizer = get_bits(gb, 1);
  1246. av_log(v->s.avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n",
  1247. v->pq, v->halfpq);
  1248. return 0;
  1249. }
  1250. /** I frame header decoding, secondary part
  1251. * @param v VC9 context
  1252. * @return Status
  1253. * @warning Not called in A/S/C profiles, it seems
  1254. * @todo Support Advanced Profile headers
  1255. */
  1256. static int decode_i_picture_secondary_header(VC9Context *v)
  1257. {
  1258. #if HAS_ADVANCED_PROFILE
  1259. int status;
  1260. if (v->profile == PROFILE_ADVANCED)
  1261. {
  1262. v->s.ac_pred = get_bits(&v->s.gb, 1);
  1263. if (v->postprocflag) v->postproc = get_bits(&v->s.gb, 1);
  1264. /* 7.1.1.34 + 8.5.2 */
  1265. if (v->overlap && v->pq<9)
  1266. {
  1267. v->condover = get_bits(&v->s.gb, 1);
  1268. if (v->condover)
  1269. {
  1270. v->condover = 2+get_bits(&v->s.gb, 1);
  1271. if (v->condover == 3)
  1272. {
  1273. status = bitplane_decoding(&v->over_flags_plane, v);
  1274. if (status < 0) return -1;
  1275. # if TRACE
  1276. av_log(v->s.avctx, AV_LOG_DEBUG, "Overflags plane encoding: "
  1277. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1278. # endif
  1279. }
  1280. }
  1281. }
  1282. }
  1283. #endif
  1284. /* Epilog (AC/DC syntax) should be done in caller */
  1285. return 0;
  1286. }
  1287. /** P frame header decoding, primary part
  1288. * @see Tables 5+7, p53-54 and 55-57
  1289. * @param v VC9 context
  1290. * @todo Support Advanced Profile headers
  1291. * @return Status
  1292. */
  1293. static int decode_p_picture_primary_header(VC9Context *v)
  1294. {
  1295. /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
  1296. GetBitContext *gb = &v->s.gb;
  1297. int lowquant, pqindex;
  1298. pqindex = get_bits(gb, 5);
  1299. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1300. v->pq = pquant_table[0][pqindex];
  1301. else
  1302. {
  1303. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1304. }
  1305. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1306. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1307. v->pquantizer = get_bits(gb, 1);
  1308. av_log(v->s.avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n",
  1309. v->pq, v->halfpq);
  1310. if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
  1311. #if HAS_ADVANCED_PROFILE
  1312. if (v->profile == PROFILE_ADVANCED)
  1313. {
  1314. if (v->postprocflag) v->postproc = get_bits(gb, 1);
  1315. }
  1316. else
  1317. #endif
  1318. if (v->multires) v->respic = get_bits(gb, 2);
  1319. lowquant = (v->pquantizer>12) ? 0 : 1;
  1320. v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
  1321. if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  1322. {
  1323. v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
  1324. v->lumscale = get_bits(gb, 6);
  1325. v->lumshift = get_bits(gb, 6);
  1326. }
  1327. return 0;
  1328. }
  1329. /** P frame header decoding, secondary part
  1330. * @see Tables 5+7, p53-54 and 55-57
  1331. * @param v VC9 context
  1332. * @warning To call once all MB arrays are allocated
  1333. * @return Status
  1334. */
  1335. static int decode_p_picture_secondary_header(VC9Context *v)
  1336. {
  1337. GetBitContext *gb = &v->s.gb;
  1338. int status = 0;
  1339. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  1340. v->mv_mode2 == MV_PMODE_MIXED_MV)
  1341. || v->mv_mode == MV_PMODE_MIXED_MV)
  1342. {
  1343. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  1344. if (status < 0) return -1;
  1345. #if TRACE
  1346. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1347. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1348. #endif
  1349. }
  1350. status = bitplane_decoding(&v->skip_mb_plane, v);
  1351. if (status < 0) return -1;
  1352. #if TRACE
  1353. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  1354. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1355. #endif
  1356. /* Hopefully this is correct for P frames */
  1357. v->s.mv_table_index =get_bits(gb, 2); //but using vc9_ tables
  1358. v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)];
  1359. if (v->dquant)
  1360. {
  1361. av_log(v->s.avctx, AV_LOG_INFO, "VOP DQuant info\n");
  1362. vop_dquant_decoding(v);
  1363. }
  1364. v->ttfrm = 0; //FIXME Is that so ?
  1365. if (v->vstransform)
  1366. {
  1367. v->ttmbf = get_bits(gb, 1);
  1368. if (v->ttmbf)
  1369. {
  1370. v->ttfrm = get_bits(gb, 2);
  1371. av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
  1372. (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
  1373. }
  1374. }
  1375. /* Epilog (AC/DC syntax) should be done in caller */
  1376. return 0;
  1377. }
  1378. /** @} */ //End of group all_frm_hdr
  1379. /***********************************************************************/
  1380. /**
  1381. * @defgroup std_frame_hdr VC9 Simple/Main Profiles header decoding
  1382. * @brief Part of the frame header decoding belonging to Simple/Main Profiles
  1383. * @warning Only pro/epilog differs between Simple/Main and Advanced =>
  1384. * check caller
  1385. * @{
  1386. */
  1387. /** Frame header decoding, first part, in Simple and Main profiles
  1388. * @see Tables 5+7, p53-54 and 55-57
  1389. * @param v VC9 context
  1390. * @todo FIXME: RANGEREDFRM element not read if BI frame from Table6, P54
  1391. * However, 7.1.1.8 says "all frame types, for main profiles"
  1392. * @return Status
  1393. */
  1394. static int standard_decode_picture_primary_header(VC9Context *v)
  1395. {
  1396. GetBitContext *gb = &v->s.gb;
  1397. int status = 0;
  1398. if (v->finterpflag) v->interpfrm = get_bits(gb, 1);
  1399. skip_bits(gb, 2); //framecnt unused
  1400. if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
  1401. v->s.pict_type = get_bits(gb, 1);
  1402. if (v->s.avctx->max_b_frames)
  1403. {
  1404. if (!v->s.pict_type)
  1405. {
  1406. if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
  1407. else v->s.pict_type = B_TYPE;
  1408. }
  1409. else v->s.pict_type = P_TYPE;
  1410. }
  1411. else v->s.pict_type++;
  1412. switch (v->s.pict_type)
  1413. {
  1414. case I_TYPE: status = decode_i_picture_primary_header(v); break;
  1415. case P_TYPE: status = decode_p_picture_primary_header(v); break;
  1416. case BI_TYPE: //Same as B
  1417. case B_TYPE: status = decode_b_picture_primary_header(v); break;
  1418. }
  1419. if (status == FRAME_SKIPPED)
  1420. {
  1421. av_log(v->s.avctx, AV_LOG_INFO, "Skipping frame...\n");
  1422. return status;
  1423. }
  1424. return 0;
  1425. }
  1426. /** Frame header decoding, secondary part
  1427. * @param v VC9 context
  1428. * @warning To call once all MB arrays are allocated
  1429. * @return Status
  1430. */
  1431. static int standard_decode_picture_secondary_header(VC9Context *v)
  1432. {
  1433. GetBitContext *gb = &v->s.gb;
  1434. int status = 0;
  1435. switch (v->s.pict_type)
  1436. {
  1437. case P_TYPE: status = decode_p_picture_secondary_header(v); break;
  1438. case B_TYPE: status = decode_b_picture_secondary_header(v); break;
  1439. case BI_TYPE:
  1440. case I_TYPE: break; //Nothing needed as it's done in the epilog
  1441. }
  1442. if (status < 0) return FRAME_SKIPPED;
  1443. /* AC Syntax */
  1444. v->c_ac_table_index = decode012(gb);
  1445. if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
  1446. {
  1447. v->y_ac_table_index = decode012(gb);
  1448. }
  1449. /* DC Syntax */
  1450. v->s.dc_table_index = decode012(gb);
  1451. return 0;
  1452. }
  1453. /** @} */ //End for group std_frame_hdr
  1454. #if HAS_ADVANCED_PROFILE
  1455. /***********************************************************************/
  1456. /**
  1457. * @defgroup adv_frame_hdr VC9 Advanced Profile header decoding
  1458. * @brief Part of the frame header decoding belonging to Advanced Profiles
  1459. * @warning Only pro/epilog differs between Simple/Main and Advanced =>
  1460. * check caller
  1461. * @{
  1462. */
  1463. /** Frame header decoding, primary part
  1464. * @param v VC9 context
  1465. * @return Status
  1466. */
  1467. static int advanced_decode_picture_primary_header(VC9Context *v)
  1468. {
  1469. GetBitContext *gb = &v->s.gb;
  1470. static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE };
  1471. int type;
  1472. if (v->interlace)
  1473. {
  1474. v->fcm = get_bits(gb, 1);
  1475. if (v->fcm) v->fcm = 2+get_bits(gb, 1);
  1476. }
  1477. type = get_prefix(gb, 0, 4);
  1478. if (type > 4 || type < 0) return FRAME_SKIPPED;
  1479. v->s.pict_type = type_table[type];
  1480. av_log(v->s.avctx, AV_LOG_INFO, "AP Frame Type: %i\n", v->s.pict_type);
  1481. if (v->tfcntrflag) v->tfcntr = get_bits(gb, 8);
  1482. if (v->broadcast)
  1483. {
  1484. if (!v->interlace) v->rptfrm = get_bits(gb, 2);
  1485. else
  1486. {
  1487. v->tff = get_bits(gb, 1);
  1488. v->rff = get_bits(gb, 1);
  1489. }
  1490. }
  1491. if (v->panscanflag)
  1492. {
  1493. #if 0
  1494. for (i=0; i<v->numpanscanwin; i++)
  1495. {
  1496. v->topleftx[i] = get_bits(gb, 16);
  1497. v->toplefty[i] = get_bits(gb, 16);
  1498. v->bottomrightx[i] = get_bits(gb, 16);
  1499. v->bottomrighty[i] = get_bits(gb, 16);
  1500. }
  1501. #else
  1502. skip_bits(gb, 16*4*v->numpanscanwin);
  1503. #endif
  1504. }
  1505. v->s.no_rounding = !get_bits(gb, 1);
  1506. v->uvsamp = get_bits(gb, 1);
  1507. if (v->finterpflag == 1) v->interpfrm = get_bits(gb, 1);
  1508. switch(v->s.pict_type)
  1509. {
  1510. case I_TYPE: if (decode_i_picture_primary_header(v) < 0) return -1;
  1511. case P_TYPE: if (decode_p_picture_primary_header(v) < 0) return -1;
  1512. case BI_TYPE:
  1513. case B_TYPE: if (decode_b_picture_primary_header(v) < 0) return FRAME_SKIPPED;
  1514. default: return -1;
  1515. }
  1516. }
  1517. /** Frame header decoding, secondary part
  1518. * @param v VC9 context
  1519. * @return Status
  1520. */
  1521. static int advanced_decode_picture_secondary_header(VC9Context *v)
  1522. {
  1523. GetBitContext *gb = &v->s.gb;
  1524. int status = 0;
  1525. switch(v->s.pict_type)
  1526. {
  1527. case P_TYPE: status = decode_p_picture_secondary_header(v); break;
  1528. case B_TYPE: status = decode_b_picture_secondary_header(v); break;
  1529. case BI_TYPE:
  1530. case I_TYPE: status = decode_i_picture_secondary_header(v); break;
  1531. }
  1532. if (status<0) return FRAME_SKIPPED;
  1533. /* AC Syntax */
  1534. v->c_ac_table_index = decode012(gb);
  1535. if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
  1536. {
  1537. v->y_ac_table_index = decode012(gb);
  1538. }
  1539. /* DC Syntax */
  1540. v->s.dc_table_index = decode012(gb);
  1541. return 0;
  1542. }
  1543. #endif
  1544. /** @} */ //End for adv_frame_hdr
  1545. /***********************************************************************/
  1546. /**
  1547. * @defgroup block VC9 Block-level functions
  1548. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1549. * @todo TODO: Integrate to MpegEncContext facilities
  1550. * @{
  1551. */
  1552. /**
  1553. * @def GET_MQUANT
  1554. * @brief Get macroblock-level quantizer scale
  1555. * @warning XXX: qdiff to the frame quant, not previous quant ?
  1556. * @fixme XXX: Don't know how to initialize mquant otherwise in last case
  1557. */
  1558. #define GET_MQUANT() \
  1559. if (v->dquantfrm) \
  1560. { \
  1561. if (v->dqprofile == DQPROFILE_ALL_MBS) \
  1562. { \
  1563. if (v->dqbilevel) \
  1564. { \
  1565. mquant = (get_bits(gb, 1)) ? v->pq : v->altpq; \
  1566. } \
  1567. else \
  1568. { \
  1569. mqdiff = get_bits(gb, 3); \
  1570. if (mqdiff != 7) mquant = v->pq + mqdiff; \
  1571. else mquant = get_bits(gb, 5); \
  1572. } \
  1573. } \
  1574. else mquant = v->pq; \
  1575. }
  1576. /**
  1577. * @def GET_MVDATA(_dmv_x, _dmv_y)
  1578. * @brief Get MV differentials
  1579. * @see MVDATA decoding from 8.3.5.2, p(1)20
  1580. * @param _dmv_x Horizontal differential for decoded MV
  1581. * @param _dmv_y Vertical differential for decoded MV
  1582. * @todo TODO: Use MpegEncContext arrays to store them
  1583. */
  1584. #define GET_MVDATA(_dmv_x, _dmv_y) \
  1585. index = 1 + get_vlc2(gb, vc9_mv_diff_vlc[s->mv_table_index].table,\
  1586. VC9_MV_DIFF_VLC_BITS, 2); \
  1587. if (index > 36) \
  1588. { \
  1589. mb_has_coeffs = 1; \
  1590. index -= 37; \
  1591. } \
  1592. else mb_has_coeffs = 0; \
  1593. s->mb_intra = 0; \
  1594. if (!index) { _dmv_x = _dmv_y = 0; } \
  1595. else if (index == 35) \
  1596. { \
  1597. _dmv_x = get_bits(gb, v->k_x); \
  1598. _dmv_y = get_bits(gb, v->k_y); \
  1599. s->mb_intra = 1; \
  1600. } \
  1601. else \
  1602. { \
  1603. index1 = index%6; \
  1604. if (s->mspel && index1 == 5) val = 1; \
  1605. else val = 0; \
  1606. val = get_bits(gb, size_table[index1] - val); \
  1607. sign = 0 - (val&1); \
  1608. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1609. \
  1610. index1 = index/6; \
  1611. if (s->mspel && index1 == 5) val = 1; \
  1612. else val = 0; \
  1613. val = get_bits(gb, size_table[index1] - val); \
  1614. sign = 0 - (val&1); \
  1615. _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1616. }
  1617. /** Get predicted DC value
  1618. * prediction dir: left=0, top=1
  1619. * @param s MpegEncContext
  1620. * @param[in] n block index in the current MB
  1621. * @param dc_val_ptr Pointer to DC predictor
  1622. * @param dir_ptr Prediction direction for use in AC prediction
  1623. * @todo TODO: Actually do it the VC9 way
  1624. * @todo TODO: Handle properly edges
  1625. */
  1626. static inline int vc9_pred_dc(MpegEncContext *s, int n,
  1627. uint16_t **dc_val_ptr, int *dir_ptr)
  1628. {
  1629. int a, b, c, wrap, pred, scale;
  1630. int16_t *dc_val;
  1631. static const uint16_t dcpred[31] = {
  1632. 1024, 512, 341, 256, 205, 171, 146, 128,
  1633. 114, 102, 93, 85, 79, 73, 68, 64,
  1634. 60, 57, 54, 51, 49, 47, 45, 43,
  1635. 41, 39, 38, 37, 35, 34, 33
  1636. };
  1637. /* find prediction - wmv3_dc_scale always used here in fact */
  1638. if (n < 4) scale = s->y_dc_scale;
  1639. else scale = s->c_dc_scale;
  1640. wrap = s->block_wrap[n];
  1641. dc_val= s->dc_val[0] + s->block_index[n];
  1642. /* B C
  1643. * A X
  1644. */
  1645. a = dc_val[ - 1];
  1646. b = dc_val[ - 1 - wrap];
  1647. c = dc_val[ - wrap];
  1648. /* XXX: Rule B is used only for I and BI frames in S/M/C profile
  1649. * with overlap filtering off
  1650. */
  1651. if ((s->pict_type == I_TYPE || s->pict_type == BI_TYPE) &&
  1652. 1 /* XXX: overlap filtering off */)
  1653. {
  1654. /* Set outer values */
  1655. if (s->first_slice_line && n!=2) b=c=dcpred[scale];
  1656. if (s->mb_x == 0) b=a=dcpred[scale];
  1657. }
  1658. else
  1659. {
  1660. /* Set outer values */
  1661. if (s->first_slice_line && n!=2) b=c=0;
  1662. if (s->mb_x == 0) b=a=0;
  1663. /* XXX: Rule A needs to know if blocks are inter or intra :/ */
  1664. if (0)
  1665. {
  1666. /* update predictor */
  1667. *dc_val_ptr = &dc_val[0];
  1668. dir_ptr = 0;
  1669. return a;
  1670. }
  1671. }
  1672. if (abs(a - b) <= abs(b - c)) {
  1673. pred = c;
  1674. *dir_ptr = 1;
  1675. } else {
  1676. pred = a;
  1677. *dir_ptr = 0;
  1678. }
  1679. /* update predictor */
  1680. *dc_val_ptr = &dc_val[0];
  1681. return pred;
  1682. }
  1683. /** Decode one block, inter or intra
  1684. * @param v The VC9 context
  1685. * @param block 8x8 DCT block
  1686. * @param n Block index in the current MB (<4=>luma)
  1687. * @param coded If the block is coded
  1688. * @param mquant Quantizer step for the current block
  1689. * @see Inter TT: Table 21, p73 + p91-85
  1690. * @see Intra TT: Table 20, p72 + p(1)05-(1)07
  1691. * @todo TODO: Process the blocks
  1692. * @todo TODO: Use M$ MPEG-4 cbp prediction
  1693. */
  1694. static int vc9_decode_block(VC9Context *v, DCTELEM block[64], int n, int coded, int mquant)
  1695. {
  1696. GetBitContext *gb = &v->s.gb;
  1697. MpegEncContext *s = &v->s;
  1698. int ttblk; /* Transform Type per Block */
  1699. int subblkpat; /* Sub-block Transform Type Pattern */
  1700. int dc_pred_dir; /* Direction of the DC prediction used */
  1701. int run_diff, i;
  1702. /* XXX: Guard against dumb values of mquant */
  1703. mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
  1704. /* Set DC scale - y and c use the same */
  1705. s->y_dc_scale = s->y_dc_scale_table[mquant];
  1706. s->c_dc_scale = s->c_dc_scale_table[mquant];
  1707. if (s->mb_intra)
  1708. {
  1709. int dcdiff;
  1710. uint16_t *dc_val;
  1711. /* Get DC differential */
  1712. if (n < 4) {
  1713. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1714. } else {
  1715. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1716. }
  1717. if (dcdiff < 0){
  1718. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  1719. return -1;
  1720. }
  1721. if (dcdiff)
  1722. {
  1723. if (dcdiff == 119 /* ESC index value */)
  1724. {
  1725. /* TODO: Optimize */
  1726. if (mquant == 1) dcdiff = get_bits(gb, 10);
  1727. else if (mquant == 2) dcdiff = get_bits(gb, 9);
  1728. else dcdiff = get_bits(gb, 8);
  1729. }
  1730. else
  1731. {
  1732. if (mquant == 1)
  1733. dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
  1734. else if (mquant == 2)
  1735. dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
  1736. }
  1737. if (get_bits(gb, 1))
  1738. dcdiff = -dcdiff;
  1739. }
  1740. /* Prediction */
  1741. dcdiff += vc9_pred_dc(s, n, &dc_val, &dc_pred_dir);
  1742. *dc_val = dcdiff;
  1743. /* Store the quantized DC coeff, used for prediction */
  1744. if (n < 4) {
  1745. block[0] = dcdiff * s->y_dc_scale;
  1746. } else {
  1747. block[0] = dcdiff * s->c_dc_scale;
  1748. }
  1749. if (block[0] < 0) {
  1750. #if TRACE
  1751. //av_log(s->avctx, AV_LOG_ERROR, "DC=%i<0\n", dcdiff);
  1752. #endif
  1753. //return -1;
  1754. }
  1755. /* Skip ? */
  1756. run_diff = 0;
  1757. i = 0;
  1758. if (!coded) {
  1759. goto not_coded;
  1760. }
  1761. }
  1762. else
  1763. {
  1764. mquant = v->pq;
  1765. /* Get TTBLK */
  1766. if (v->ttmb < 8) /* per block */
  1767. ttblk = get_vlc2(gb, vc9_ttblk_vlc[v->tt_index].table, VC9_TTBLK_VLC_BITS, 2);
  1768. else /* Per frame */
  1769. ttblk = 0; //FIXME, depends on ttfrm
  1770. /* Get SUBBLKPAT */
  1771. if (ttblk == v->ttblk4x4) /* 4x4 transform for that qp value */
  1772. subblkpat = 1+get_vlc2(gb, vc9_subblkpat_vlc[v->tt_index].table,
  1773. VC9_SUBBLKPAT_VLC_BITS, 2);
  1774. else /* All others: 8x8, 4x8, 8x4 */
  1775. subblkpat = decode012(gb);
  1776. }
  1777. //TODO AC Decoding
  1778. i = 63; //XXX: nothing done yet
  1779. not_coded:
  1780. if (s->mb_intra) {
  1781. mpeg4_pred_ac(s, block, n, dc_pred_dir);
  1782. if (s->ac_pred) {
  1783. i = 63; /* XXX: not optimal */
  1784. }
  1785. }
  1786. if(i>0) i=63; //FIXME/XXX optimize
  1787. s->block_last_index[n] = i;
  1788. return 0;
  1789. }
  1790. /** @} */ //End for group block
  1791. /***********************************************************************/
  1792. /**
  1793. * @defgroup std_mb VC9 Macroblock-level functions in Simple/Main Profiles
  1794. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1795. * @todo TODO: Integrate to MpegEncContext facilities
  1796. * @{
  1797. */
  1798. static inline int vc9_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
  1799. {
  1800. int xy, wrap, pred, a, b, c;
  1801. xy = s->block_index[n];
  1802. wrap = s->b8_stride;
  1803. /* B C
  1804. * A X
  1805. */
  1806. a = s->coded_block[xy - 1 ];
  1807. b = s->coded_block[xy - 1 - wrap];
  1808. c = s->coded_block[xy - wrap];
  1809. if (b == c) {
  1810. pred = a;
  1811. } else {
  1812. pred = c;
  1813. }
  1814. /* store value */
  1815. *coded_block_ptr = &s->coded_block[xy];
  1816. return pred;
  1817. }
  1818. /** Decode one I-frame MB (in Simple/Main profile)
  1819. * @todo TODO: Extend to AP
  1820. */
  1821. static int vc9_decode_i_mb(VC9Context *v, DCTELEM block[6][64])
  1822. {
  1823. int i, cbp, val;
  1824. uint8_t *coded_val;
  1825. // uint32_t * const mb_type_ptr= &v->s.current_picture.mb_type[ v->s.mb_x + v->s.mb_y*v->s.mb_stride ];
  1826. v->s.mb_intra = 1;
  1827. cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
  1828. if (cbp < 0) return -1;
  1829. v->s.ac_pred = get_bits(&v->s.gb, 1);
  1830. for (i=0; i<6; i++)
  1831. {
  1832. val = ((cbp >> (5 - i)) & 1);
  1833. if (i < 4) {
  1834. int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
  1835. val = val ^ pred;
  1836. *coded_val = val;
  1837. }
  1838. cbp |= val << (5 - i);
  1839. if (vc9_decode_block(v, block[i], i, val, v->pq) < 0) //FIXME Should be mquant
  1840. {
  1841. av_log(v->s.avctx, AV_LOG_ERROR,
  1842. "\nerror while decoding block: %d x %d (%d)\n", v->s.mb_x, v->s.mb_y, i);
  1843. return -1;
  1844. }
  1845. }
  1846. return 0;
  1847. }
  1848. /** Decode one P-frame MB (in Simple/Main profile)
  1849. * @todo TODO: Extend to AP
  1850. * @fixme FIXME: DC value for inter blocks not set
  1851. */
  1852. static int vc9_decode_p_mb(VC9Context *v, DCTELEM block[6][64])
  1853. {
  1854. MpegEncContext *s = &v->s;
  1855. GetBitContext *gb = &s->gb;
  1856. int i, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
  1857. int cbp; /* cbp decoding stuff */
  1858. int hybrid_pred; /* Prediction types */
  1859. int mv_mode_bit = 0;
  1860. int mqdiff, mquant; /* MB quantization */
  1861. int ttmb; /* MB Transform type */
  1862. int status;
  1863. uint8_t *coded_val;
  1864. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1865. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1866. int mb_has_coeffs = 1; /* last_flag */
  1867. int dmv_x, dmv_y; /* Differential MV components */
  1868. int index, index1; /* LUT indices */
  1869. int val, sign; /* temp values */
  1870. mquant = v->pq; /* Loosy initialization */
  1871. if (v->mv_type_mb_plane.is_raw)
  1872. v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1873. if (v->skip_mb_plane.is_raw)
  1874. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1875. if (!mv_mode_bit) /* 1MV mode */
  1876. {
  1877. if (!v->skip_mb_plane.data[mb_offset])
  1878. {
  1879. GET_MVDATA(dmv_x, dmv_y);
  1880. /* hybrid mv pred, 8.3.5.3.4 */
  1881. if (v->mv_mode == MV_PMODE_1MV ||
  1882. v->mv_mode == MV_PMODE_MIXED_MV)
  1883. hybrid_pred = get_bits(gb, 1);
  1884. /* FIXME Set DC val for inter block ? */
  1885. if (s->mb_intra && !mb_has_coeffs)
  1886. {
  1887. GET_MQUANT();
  1888. s->ac_pred = get_bits(gb, 1);
  1889. /* XXX: how to handle cbp ? */
  1890. cbp = 0;
  1891. for (i=0; i<6; i++)
  1892. {
  1893. s->coded_block[s->block_index[i]] = 0;
  1894. vc9_decode_block(v, block[i], i, 0, mquant);
  1895. }
  1896. return 0;
  1897. }
  1898. else if (mb_has_coeffs)
  1899. {
  1900. if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
  1901. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
  1902. GET_MQUANT();
  1903. }
  1904. else
  1905. {
  1906. mquant = v->pq;
  1907. /* XXX: how to handle cbp ? */
  1908. /* XXX: how to set values for following predictions ? */
  1909. cbp = 0;
  1910. }
  1911. if (!v->ttmbf)
  1912. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  1913. VC9_TTMB_VLC_BITS, 12);
  1914. for (i=0; i<6; i++)
  1915. {
  1916. val = ((cbp >> (5 - i)) & 1);
  1917. if (i < 4) {
  1918. int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
  1919. val = val ^ pred;
  1920. *coded_val = val;
  1921. }
  1922. vc9_decode_block(v, block[i], i, val, mquant); //FIXME
  1923. }
  1924. }
  1925. else //Skipped
  1926. {
  1927. /* hybrid mv pred, 8.3.5.3.4 */
  1928. if (v->mv_mode == MV_PMODE_1MV ||
  1929. v->mv_mode == MV_PMODE_MIXED_MV)
  1930. hybrid_pred = get_bits(gb, 1);
  1931. /* TODO: blah */
  1932. return 0;
  1933. }
  1934. } //1MV mode
  1935. else //4MV mode
  1936. {
  1937. if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
  1938. {
  1939. /* Get CBPCY */
  1940. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
  1941. for (i=0; i<6; i++)
  1942. {
  1943. val = ((cbp >> (5 - i)) & 1);
  1944. if (i < 4) {
  1945. int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
  1946. val = val ^ pred;
  1947. *coded_val = val;
  1948. }
  1949. if (i<4 && val)
  1950. {
  1951. GET_MVDATA(dmv_x, dmv_y);
  1952. }
  1953. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  1954. hybrid_pred = get_bits(gb, 1);
  1955. GET_MQUANT();
  1956. if (s->mb_intra /* One of the 4 blocks is intra */ &&
  1957. index /* non-zero pred for that block */)
  1958. s->ac_pred = get_bits(gb, 1);
  1959. if (!v->ttmbf)
  1960. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  1961. VC9_TTMB_VLC_BITS, 12);
  1962. status = vc9_decode_block(v, block[i], i, val, mquant);
  1963. }
  1964. return status;
  1965. }
  1966. else //Skipped MB
  1967. {
  1968. /* XXX: Skipped => cbp=0 and mquant doesn't matter ? */
  1969. for (i=0; i<4; i++)
  1970. {
  1971. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  1972. hybrid_pred = get_bits(gb, 1);
  1973. vc9_decode_block(v, block[i], i, 0, v->pq); //FIXME
  1974. }
  1975. vc9_decode_block(v, block[4], 4, 0, v->pq); //FIXME
  1976. vc9_decode_block(v, block[5], 5, 0, v->pq); //FIXME
  1977. /* TODO: blah */
  1978. return 0;
  1979. }
  1980. }
  1981. /* Should never happen */
  1982. return -1;
  1983. }
  1984. /** Decode one B-frame MB (in Simple/Main profile)
  1985. * @todo TODO: Extend to AP
  1986. * @warning XXX: Used for decoding BI MBs
  1987. * @fixme FIXME: DC value for inter blocks not set
  1988. */
  1989. static int vc9_decode_b_mb(VC9Context *v, DCTELEM block[6][64])
  1990. {
  1991. MpegEncContext *s = &v->s;
  1992. GetBitContext *gb = &v->s.gb;
  1993. int mb_offset, i /* MB / B postion information */;
  1994. int b_mv_type = BMV_TYPE_BACKWARD;
  1995. int mquant, mqdiff; /* MB quant stuff */
  1996. int ttmb; /* MacroBlock transform type */
  1997. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1998. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1999. int mb_has_coeffs = 1; /* last_flag */
  2000. int dmv1_x, dmv1_y, dmv2_x, dmv2_y; /* Differential MV components */
  2001. int index, index1; /* LUT indices */
  2002. int val, sign; /* MVDATA temp values */
  2003. mb_offset = s->mb_width*s->mb_y + s->mb_x; //FIXME: arrays aren't using stride
  2004. if (v->direct_mb_plane.is_raw)
  2005. v->direct_mb_plane.data[mb_offset] = get_bits(gb, 1);
  2006. if (v->skip_mb_plane.is_raw)
  2007. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  2008. if (!v->direct_mb_plane.data[mb_offset])
  2009. {
  2010. if (v->skip_mb_plane.data[mb_offset])
  2011. {
  2012. b_mv_type = decode012(gb);
  2013. if (v->bfraction > 420 /*1/2*/ &&
  2014. b_mv_type < 3) b_mv_type = 1-b_mv_type;
  2015. }
  2016. else
  2017. {
  2018. GET_MVDATA(dmv1_x, dmv1_y);
  2019. if (!s->mb_intra /* b_mv1 tells not intra */)
  2020. {
  2021. b_mv_type = decode012(gb);
  2022. if (v->bfraction > 420 /*1/2*/ &&
  2023. b_mv_type < 3) b_mv_type = 1-b_mv_type;
  2024. }
  2025. }
  2026. }
  2027. if (!v->skip_mb_plane.data[mb_offset])
  2028. {
  2029. if (mb_has_coeffs /* BMV1 == "last" */)
  2030. {
  2031. GET_MQUANT();
  2032. if (s->mb_intra /* intra mb */)
  2033. s->ac_pred = get_bits(gb, 1);
  2034. }
  2035. else
  2036. {
  2037. /* if bmv1 tells MVs are interpolated */
  2038. if (b_mv_type == BMV_TYPE_INTERPOLATED)
  2039. {
  2040. GET_MVDATA(dmv2_x, dmv2_y);
  2041. mquant = v->pq; //FIXME: initialization not necessary ?
  2042. }
  2043. /* GET_MVDATA has reset some stuff */
  2044. if (mb_has_coeffs /* b_mv2 == "last" */)
  2045. {
  2046. if (s->mb_intra /* intra_mb */)
  2047. s->ac_pred = get_bits(gb, 1);
  2048. GET_MQUANT();
  2049. }
  2050. }
  2051. }
  2052. //End1
  2053. if (v->ttmbf)
  2054. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  2055. VC9_TTMB_VLC_BITS, 12);
  2056. //End2
  2057. for (i=0; i<6; i++)
  2058. {
  2059. vc9_decode_block(v, block[i], i, 0 /*cbp[i]*/, mquant); //FIXME
  2060. }
  2061. return 0;
  2062. }
  2063. /** Decode all MBs for an I frame in Simple/Main profile
  2064. * @todo TODO: Move out of the loop the picture type case?
  2065. (branch prediction should help there though)
  2066. */
  2067. static int standard_decode_mbs(VC9Context *v)
  2068. {
  2069. MpegEncContext *s = &v->s;
  2070. /* Set transform type info depending on pq */
  2071. if (v->pq < 5)
  2072. {
  2073. v->tt_index = 0;
  2074. v->ttblk4x4 = 3;
  2075. }
  2076. else if (v->pq < 13)
  2077. {
  2078. v->tt_index = 1;
  2079. v->ttblk4x4 = 3;
  2080. }
  2081. else
  2082. {
  2083. v->tt_index = 2;
  2084. v->ttblk4x4 = 2;
  2085. }
  2086. if (s->pict_type != I_TYPE)
  2087. {
  2088. /* Select proper long MV range */
  2089. switch (v->mvrange)
  2090. {
  2091. case 1: v->k_x = 10; v->k_y = 9; break;
  2092. case 2: v->k_x = 12; v->k_y = 10; break;
  2093. case 3: v->k_x = 13; v->k_y = 11; break;
  2094. default: /*case 0 too */ v->k_x = 9; v->k_y = 8; break;
  2095. }
  2096. s->mspel = v->mv_mode & 1; //MV_PMODE is HPEL
  2097. v->k_x -= s->mspel;
  2098. v->k_y -= s->mspel;
  2099. }
  2100. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  2101. {
  2102. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
  2103. {
  2104. //FIXME Get proper MB DCTELEM
  2105. //TODO Move out of the loop
  2106. switch (s->pict_type)
  2107. {
  2108. case I_TYPE: vc9_decode_i_mb(v, s->block); break;
  2109. case P_TYPE: vc9_decode_p_mb(v, s->block); break;
  2110. case BI_TYPE:
  2111. case B_TYPE: vc9_decode_b_mb(v, s->block); break;
  2112. }
  2113. }
  2114. //Add a check for overconsumption ?
  2115. }
  2116. return 0;
  2117. }
  2118. /** @} */ //End for group std_mb
  2119. #if HAS_ADVANCED_PROFILE
  2120. /***********************************************************************/
  2121. /**
  2122. * @defgroup adv_mb VC9 Macroblock-level functions in Advanced Profile
  2123. * @todo TODO: Integrate to MpegEncContext facilities
  2124. * @todo TODO: Code P, B and BI
  2125. * @{
  2126. */
  2127. static int advanced_decode_i_mbs(VC9Context *v)
  2128. {
  2129. MpegEncContext *s = &v->s;
  2130. GetBitContext *gb = &v->s.gb;
  2131. int mqdiff, mquant, mb_offset = 0, over_flags_mb = 0;
  2132. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  2133. {
  2134. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
  2135. {
  2136. if (v->ac_pred_plane.is_raw)
  2137. s->ac_pred = get_bits(gb, 1);
  2138. else
  2139. s->ac_pred = v->ac_pred_plane.data[mb_offset];
  2140. if (v->condover == 3 && v->over_flags_plane.is_raw)
  2141. over_flags_mb = get_bits(gb, 1);
  2142. GET_MQUANT();
  2143. /* TODO: lots */
  2144. }
  2145. mb_offset++;
  2146. }
  2147. return 0;
  2148. }
  2149. /** @} */ //End for group adv_mb
  2150. #endif
  2151. /** Initialize a VC9/WMV3 decoder
  2152. * @todo TODO: Handle VC-9 IDUs (Transport level?)
  2153. * @todo TODO: Decypher remaining bits in extra_data
  2154. */
  2155. static int vc9_decode_init(AVCodecContext *avctx)
  2156. {
  2157. VC9Context *v = avctx->priv_data;
  2158. MpegEncContext *s = &v->s;
  2159. GetBitContext gb;
  2160. if (!avctx->extradata_size || !avctx->extradata) return -1;
  2161. avctx->pix_fmt = PIX_FMT_YUV420P;
  2162. v->s.avctx = avctx;
  2163. if(ff_h263_decode_init(avctx) < 0)
  2164. return -1;
  2165. if (vc9_init_common(v) < 0) return -1;
  2166. av_log(avctx, AV_LOG_INFO, "This decoder is not supposed to produce picture. Dont report this as a bug!\n");
  2167. avctx->coded_width = avctx->width;
  2168. avctx->coded_height = avctx->height;
  2169. if (avctx->codec_id == CODEC_ID_WMV3)
  2170. {
  2171. int count = 0;
  2172. // looks like WMV3 has a sequence header stored in the extradata
  2173. // advanced sequence header may be before the first frame
  2174. // the last byte of the extradata is a version number, 1 for the
  2175. // samples we can decode
  2176. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  2177. if (decode_sequence_header(avctx, &gb) < 0)
  2178. return -1;
  2179. count = avctx->extradata_size*8 - get_bits_count(&gb);
  2180. if (count>0)
  2181. {
  2182. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  2183. count, get_bits(&gb, count));
  2184. }
  2185. else if (count < 0)
  2186. {
  2187. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  2188. }
  2189. }
  2190. avctx->has_b_frames= !!(avctx->max_b_frames);
  2191. s->mb_width = (avctx->coded_width+15)>>4;
  2192. s->mb_height = (avctx->coded_height+15)>>4;
  2193. /* Allocate mb bitplanes */
  2194. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2195. return -1;
  2196. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2197. return -1;
  2198. if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0)
  2199. return -1;
  2200. if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0)
  2201. return -1;
  2202. /* For predictors */
  2203. v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
  2204. if (!v->previous_line_cbpcy) return -1;
  2205. #if HAS_ADVANCED_PROFILE
  2206. if (v->profile == PROFILE_ADVANCED)
  2207. {
  2208. if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
  2209. return -1;
  2210. if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
  2211. return -1;
  2212. }
  2213. #endif
  2214. return 0;
  2215. }
  2216. /** Decode a VC9/WMV3 frame
  2217. * @todo TODO: Handle VC-9 IDUs (Transport level?)
  2218. * @warning Initial try at using MpegEncContext stuff
  2219. */
  2220. static int vc9_decode_frame(AVCodecContext *avctx,
  2221. void *data, int *data_size,
  2222. uint8_t *buf, int buf_size)
  2223. {
  2224. VC9Context *v = avctx->priv_data;
  2225. MpegEncContext *s = &v->s;
  2226. int ret = FRAME_SKIPPED, len;
  2227. AVFrame *pict = data;
  2228. uint8_t *tmp_buf;
  2229. v->s.avctx = avctx;
  2230. //buf_size = 0 -> last frame
  2231. if (!buf_size) return 0;
  2232. len = avpicture_get_size(avctx->pix_fmt, avctx->width,
  2233. avctx->height);
  2234. tmp_buf = (uint8_t *)av_mallocz(len);
  2235. avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt,
  2236. avctx->width, avctx->height);
  2237. if (avctx->codec_id == CODEC_ID_VC9)
  2238. {
  2239. #if 0
  2240. // search for IDU's
  2241. // FIXME
  2242. uint32_t scp = 0;
  2243. int scs = 0, i = 0;
  2244. while (i < buf_size)
  2245. {
  2246. for (; i < buf_size && scp != 0x000001; i++)
  2247. scp = ((scp<<8)|buf[i])&0xffffff;
  2248. if (scp != 0x000001)
  2249. break; // eof ?
  2250. scs = buf[i++];
  2251. init_get_bits(gb, buf+i, (buf_size-i)*8);
  2252. switch(scs)
  2253. {
  2254. case 0x0A: //Sequence End Code
  2255. return 0;
  2256. case 0x0B: //Slice Start Code
  2257. av_log(avctx, AV_LOG_ERROR, "Slice coding not supported\n");
  2258. return -1;
  2259. case 0x0C: //Field start code
  2260. av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n");
  2261. return -1;
  2262. case 0x0D: //Frame start code
  2263. break;
  2264. case 0x0E: //Entry point Start Code
  2265. if (v->profile < PROFILE_ADVANCED)
  2266. av_log(avctx, AV_LOG_ERROR,
  2267. "Found an entry point in profile %i\n", v->profile);
  2268. advanced_entry_point_process(avctx, gb);
  2269. break;
  2270. case 0x0F: //Sequence header Start Code
  2271. decode_sequence_header(avctx, gb);
  2272. break;
  2273. default:
  2274. av_log(avctx, AV_LOG_ERROR,
  2275. "Unsupported IDU suffix %lX\n", scs);
  2276. }
  2277. i += get_bits_count(gb)*8;
  2278. }
  2279. #else
  2280. av_abort();
  2281. #endif
  2282. }
  2283. else
  2284. init_get_bits(&v->s.gb, buf, buf_size*8);
  2285. s->flags= avctx->flags;
  2286. s->flags2= avctx->flags2;
  2287. /* no supplementary picture */
  2288. if (buf_size == 0) {
  2289. /* special case for last picture */
  2290. if (s->low_delay==0 && s->next_picture_ptr) {
  2291. *pict= *(AVFrame*)s->next_picture_ptr;
  2292. s->next_picture_ptr= NULL;
  2293. *data_size = sizeof(AVFrame);
  2294. }
  2295. return 0;
  2296. }
  2297. //No IDU - we mimic ff_h263_decode_frame
  2298. s->bitstream_buffer_size=0;
  2299. if (!s->context_initialized) {
  2300. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  2301. return -1;
  2302. }
  2303. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  2304. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  2305. s->current_picture_ptr= &s->picture[ff_find_unused_picture(s, 0)];
  2306. }
  2307. #if HAS_ADVANCED_PROFILE
  2308. if (v->profile == PROFILE_ADVANCED)
  2309. ret= advanced_decode_picture_primary_header(v);
  2310. else
  2311. #endif
  2312. ret= standard_decode_picture_primary_header(v);
  2313. if (ret == FRAME_SKIPPED) return buf_size;
  2314. /* skip if the header was thrashed */
  2315. if (ret < 0){
  2316. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  2317. return -1;
  2318. }
  2319. //No bug workaround yet, no DCT conformance
  2320. //WMV9 does have resized images
  2321. if (v->profile < PROFILE_ADVANCED && v->multires){
  2322. //Parse context stuff in here, don't know how appliable it is
  2323. }
  2324. //Not sure about context initialization
  2325. // for hurry_up==5
  2326. s->current_picture.pict_type= s->pict_type;
  2327. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2328. /* skip b frames if we dont have reference frames */
  2329. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable))
  2330. return buf_size; //FIXME simulating all buffer consumed
  2331. /* skip b frames if we are in a hurry */
  2332. if(avctx->hurry_up && s->pict_type==B_TYPE)
  2333. return buf_size; //FIXME simulating all buffer consumed
  2334. /* skip everything if we are in a hurry>=5 */
  2335. if(avctx->hurry_up>=5)
  2336. return buf_size; //FIXME simulating all buffer consumed
  2337. if(s->next_p_frame_damaged){
  2338. if(s->pict_type==B_TYPE)
  2339. return buf_size; //FIXME simulating all buffer consumed
  2340. else
  2341. s->next_p_frame_damaged=0;
  2342. }
  2343. if(MPV_frame_start(s, avctx) < 0)
  2344. return -1;
  2345. ff_er_frame_start(s);
  2346. //wmv9 may or may not have skip bits
  2347. #if HAS_ADVANCED_PROFILE
  2348. if (v->profile == PROFILE_ADVANCED)
  2349. ret= advanced_decode_picture_secondary_header(v);
  2350. else
  2351. #endif
  2352. ret = standard_decode_picture_secondary_header(v);
  2353. if (ret<0) return FRAME_SKIPPED; //FIXME Non fatal for now
  2354. //We consider the image coded in only one slice
  2355. #if HAS_ADVANCED_PROFILE
  2356. if (v->profile == PROFILE_ADVANCED)
  2357. {
  2358. switch(s->pict_type)
  2359. {
  2360. case I_TYPE: ret = advanced_decode_i_mbs(v); break;
  2361. case P_TYPE: ret = decode_p_mbs(v); break;
  2362. case B_TYPE:
  2363. case BI_TYPE: ret = decode_b_mbs(v); break;
  2364. default: ret = FRAME_SKIPPED;
  2365. }
  2366. if (ret == FRAME_SKIPPED) return buf_size; //We ignore for now failures
  2367. }
  2368. else
  2369. #endif
  2370. {
  2371. ret = standard_decode_mbs(v);
  2372. if (ret == FRAME_SKIPPED) return buf_size;
  2373. }
  2374. ff_er_frame_end(s);
  2375. MPV_frame_end(s);
  2376. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  2377. assert(s->current_picture.pict_type == s->pict_type);
  2378. if(s->pict_type==B_TYPE || s->low_delay){
  2379. *pict= *(AVFrame*)&s->current_picture;
  2380. ff_print_debug_info(s, pict);
  2381. } else {
  2382. *pict= *(AVFrame*)&s->last_picture;
  2383. if(pict)
  2384. ff_print_debug_info(s, pict);
  2385. }
  2386. /* Return the Picture timestamp as the frame number */
  2387. /* we substract 1 because it is added on utils.c */
  2388. avctx->frame_number = s->picture_number - 1;
  2389. /* dont output the last pic after seeking */
  2390. if(s->last_picture_ptr || s->low_delay)
  2391. *data_size = sizeof(AVFrame);
  2392. av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n",
  2393. get_bits_count(&s->gb), buf_size*8);
  2394. /* Fake consumption of all data */
  2395. *data_size = len;
  2396. return buf_size; //Number of bytes consumed
  2397. }
  2398. /** Close a VC9/WMV3 decoder
  2399. * @warning Initial try at using MpegEncContext stuff
  2400. */
  2401. static int vc9_decode_end(AVCodecContext *avctx)
  2402. {
  2403. VC9Context *v = avctx->priv_data;
  2404. #if HAS_ADVANCED_PROFILE
  2405. av_freep(&v->hrd_rate);
  2406. av_freep(&v->hrd_buffer);
  2407. #endif
  2408. MPV_common_end(&v->s);
  2409. free_bitplane(&v->mv_type_mb_plane);
  2410. free_bitplane(&v->skip_mb_plane);
  2411. free_bitplane(&v->direct_mb_plane);
  2412. return 0;
  2413. }
  2414. AVCodec vc9_decoder = {
  2415. "vc9",
  2416. CODEC_TYPE_VIDEO,
  2417. CODEC_ID_VC9,
  2418. sizeof(VC9Context),
  2419. vc9_decode_init,
  2420. NULL,
  2421. vc9_decode_end,
  2422. vc9_decode_frame,
  2423. CODEC_CAP_DELAY,
  2424. NULL
  2425. };
  2426. AVCodec wmv3_decoder = {
  2427. "wmv3",
  2428. CODEC_TYPE_VIDEO,
  2429. CODEC_ID_WMV3,
  2430. sizeof(VC9Context),
  2431. vc9_decode_init,
  2432. NULL,
  2433. vc9_decode_end,
  2434. vc9_decode_frame,
  2435. CODEC_CAP_DELAY,
  2436. NULL
  2437. };