mpeg12.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558
  1. /*
  2. * MPEG1 codec / MPEG2 decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file mpeg12.c
  24. * MPEG1/2 codec
  25. */
  26. //#define DEBUG
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "mpegvideo.h"
  30. #include "mpeg12data.h"
  31. #include "bytestream.h"
  32. //#undef NDEBUG
  33. //#include <assert.h>
  34. /* Start codes. */
  35. #define SEQ_END_CODE 0x000001b7
  36. #define SEQ_START_CODE 0x000001b3
  37. #define GOP_START_CODE 0x000001b8
  38. #define PICTURE_START_CODE 0x00000100
  39. #define SLICE_MIN_START_CODE 0x00000101
  40. #define SLICE_MAX_START_CODE 0x000001af
  41. #define EXT_START_CODE 0x000001b5
  42. #define USER_START_CODE 0x000001b2
  43. #define DC_VLC_BITS 9
  44. #define MV_VLC_BITS 9
  45. #define MBINCR_VLC_BITS 9
  46. #define MB_PAT_VLC_BITS 9
  47. #define MB_PTYPE_VLC_BITS 6
  48. #define MB_BTYPE_VLC_BITS 6
  49. #define TEX_VLC_BITS 9
  50. #ifdef CONFIG_ENCODERS
  51. static void mpeg1_encode_block(MpegEncContext *s,
  52. DCTELEM *block,
  53. int component);
  54. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
  55. #endif //CONFIG_ENCODERS
  56. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  57. DCTELEM *block,
  58. int n);
  59. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  60. DCTELEM *block,
  61. int n);
  62. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
  63. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  64. DCTELEM *block,
  65. int n);
  66. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  67. DCTELEM *block,
  68. int n);
  69. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
  70. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
  71. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  72. static void exchange_uv(MpegEncContext *s);
  73. #ifdef HAVE_XVMC
  74. extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
  75. extern int XVMC_field_end(MpegEncContext *s);
  76. extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
  77. extern void XVMC_init_block(MpegEncContext *s);//set s->block
  78. #endif
  79. static const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
  80. static const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
  81. static const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
  82. static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
  83. PIX_FMT_XVMC_MPEG2_IDCT,
  84. PIX_FMT_XVMC_MPEG2_MC,
  85. -1};
  86. #ifdef CONFIG_ENCODERS
  87. static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  88. static uint8_t fcode_tab[MAX_MV*2+1];
  89. static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
  90. static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
  91. /* simple include everything table for dc, first byte is bits number next 3 are code*/
  92. static uint32_t mpeg1_lum_dc_uni[512];
  93. static uint32_t mpeg1_chr_dc_uni[512];
  94. static uint8_t mpeg1_index_run[2][64];
  95. static int8_t mpeg1_max_level[2][64];
  96. #endif //CONFIG_ENCODERS
  97. static uint8_t static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  98. static void init_2d_vlc_rl(RLTable *rl, int use_static)
  99. {
  100. int i;
  101. init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
  102. &rl->table_vlc[0][1], 4, 2,
  103. &rl->table_vlc[0][0], 4, 2, use_static);
  104. if(use_static)
  105. rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  106. else
  107. rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  108. for(i=0; i<rl->vlc.table_size; i++){
  109. int code= rl->vlc.table[i][0];
  110. int len = rl->vlc.table[i][1];
  111. int level, run;
  112. if(len==0){ // illegal code
  113. run= 65;
  114. level= MAX_LEVEL;
  115. }else if(len<0){ //more bits needed
  116. run= 0;
  117. level= code;
  118. }else{
  119. if(code==rl->n){ //esc
  120. run= 65;
  121. level= 0;
  122. }else if(code==rl->n+1){ //eob
  123. run= 0;
  124. level= 127;
  125. }else{
  126. run= rl->table_run [code] + 1;
  127. level= rl->table_level[code];
  128. }
  129. }
  130. rl->rl_vlc[0][i].len= len;
  131. rl->rl_vlc[0][i].level= level;
  132. rl->rl_vlc[0][i].run= run;
  133. }
  134. }
  135. #ifdef CONFIG_ENCODERS
  136. static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
  137. int i;
  138. for(i=0; i<128; i++){
  139. int level= i-64;
  140. int run;
  141. for(run=0; run<64; run++){
  142. int len, bits, code;
  143. int alevel= FFABS(level);
  144. int sign= (level>>31)&1;
  145. if (alevel > rl->max_level[0][run])
  146. code= 111; /*rl->n*/
  147. else
  148. code= rl->index_run[0][run] + alevel - 1;
  149. if (code < 111 /* rl->n */) {
  150. /* store the vlc & sign at once */
  151. len= rl->table_vlc[code][1]+1;
  152. bits= (rl->table_vlc[code][0]<<1) + sign;
  153. } else {
  154. len= rl->table_vlc[111/*rl->n*/][1]+6;
  155. bits= rl->table_vlc[111/*rl->n*/][0]<<6;
  156. bits|= run;
  157. if (alevel < 128) {
  158. bits<<=8; len+=8;
  159. bits|= level & 0xff;
  160. } else {
  161. bits<<=16; len+=16;
  162. bits|= level & 0xff;
  163. if (level < 0) {
  164. bits|= 0x8001 + level + 255;
  165. } else {
  166. bits|= level & 0xffff;
  167. }
  168. }
  169. }
  170. uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
  171. }
  172. }
  173. }
  174. static int find_frame_rate_index(MpegEncContext *s){
  175. int i;
  176. int64_t dmin= INT64_MAX;
  177. int64_t d;
  178. for(i=1;i<14;i++) {
  179. int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
  180. int64_t n1= 1001LL*s->avctx->time_base.den;
  181. if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
  182. d = FFABS(n0 - n1);
  183. if(d < dmin){
  184. dmin=d;
  185. s->frame_rate_index= i;
  186. }
  187. }
  188. if(dmin)
  189. return -1;
  190. else
  191. return 0;
  192. }
  193. static int encode_init(AVCodecContext *avctx)
  194. {
  195. MpegEncContext *s = avctx->priv_data;
  196. if(MPV_encode_init(avctx) < 0)
  197. return -1;
  198. if(find_frame_rate_index(s) < 0){
  199. if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  200. av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
  201. return -1;
  202. }else{
  203. av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
  204. }
  205. }
  206. if(avctx->profile == FF_PROFILE_UNKNOWN){
  207. if(avctx->level != FF_LEVEL_UNKNOWN){
  208. av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
  209. return -1;
  210. }
  211. avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
  212. }
  213. if(avctx->level == FF_LEVEL_UNKNOWN){
  214. if(avctx->profile == 0){ /* 4:2:2 */
  215. if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
  216. else avctx->level = 2; /* High */
  217. }else{
  218. if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
  219. av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
  220. return -1;
  221. }
  222. if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
  223. else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
  224. else avctx->level = 4; /* High */
  225. }
  226. }
  227. if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
  228. av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. static void put_header(MpegEncContext *s, int header)
  234. {
  235. align_put_bits(&s->pb);
  236. put_bits(&s->pb, 16, header>>16);
  237. put_bits(&s->pb, 16, header&0xFFFF);
  238. }
  239. /* put sequence header if needed */
  240. static void mpeg1_encode_sequence_header(MpegEncContext *s)
  241. {
  242. unsigned int vbv_buffer_size;
  243. unsigned int fps, v;
  244. int i;
  245. uint64_t time_code;
  246. float best_aspect_error= 1E10;
  247. float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
  248. int constraint_parameter_flag;
  249. if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
  250. if (s->current_picture.key_frame) {
  251. AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
  252. /* mpeg1 header repeated every gop */
  253. put_header(s, SEQ_START_CODE);
  254. put_bits(&s->pb, 12, s->width);
  255. put_bits(&s->pb, 12, s->height);
  256. for(i=1; i<15; i++){
  257. float error= aspect_ratio;
  258. if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
  259. error-= 1.0/mpeg1_aspect[i];
  260. else
  261. error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
  262. error= FFABS(error);
  263. if(error < best_aspect_error){
  264. best_aspect_error= error;
  265. s->aspect_ratio_info= i;
  266. }
  267. }
  268. put_bits(&s->pb, 4, s->aspect_ratio_info);
  269. put_bits(&s->pb, 4, s->frame_rate_index);
  270. if(s->avctx->rc_max_rate){
  271. v = (s->avctx->rc_max_rate + 399) / 400;
  272. if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
  273. v = 0x3ffff;
  274. }else{
  275. v= 0x3FFFF;
  276. }
  277. if(s->avctx->rc_buffer_size)
  278. vbv_buffer_size = s->avctx->rc_buffer_size;
  279. else
  280. /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
  281. vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
  282. vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
  283. put_bits(&s->pb, 18, v & 0x3FFFF);
  284. put_bits(&s->pb, 1, 1); /* marker */
  285. put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
  286. constraint_parameter_flag=
  287. s->width <= 768 && s->height <= 576 &&
  288. s->mb_width * s->mb_height <= 396 &&
  289. s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
  290. framerate.num <= framerate.den*30 &&
  291. s->avctx->me_range && s->avctx->me_range < 128 &&
  292. vbv_buffer_size <= 20 &&
  293. v <= 1856000/400 &&
  294. s->codec_id == CODEC_ID_MPEG1VIDEO;
  295. put_bits(&s->pb, 1, constraint_parameter_flag);
  296. ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
  297. ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
  298. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  299. put_header(s, EXT_START_CODE);
  300. put_bits(&s->pb, 4, 1); //seq ext
  301. put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
  302. put_bits(&s->pb, 3, s->avctx->profile); //profile
  303. put_bits(&s->pb, 4, s->avctx->level); //level
  304. put_bits(&s->pb, 1, s->progressive_sequence);
  305. put_bits(&s->pb, 2, s->chroma_format);
  306. put_bits(&s->pb, 2, 0); //horizontal size ext
  307. put_bits(&s->pb, 2, 0); //vertical size ext
  308. put_bits(&s->pb, 12, v>>18); //bitrate ext
  309. put_bits(&s->pb, 1, 1); //marker
  310. put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
  311. put_bits(&s->pb, 1, s->low_delay);
  312. put_bits(&s->pb, 2, 0); // frame_rate_ext_n
  313. put_bits(&s->pb, 5, 0); // frame_rate_ext_d
  314. }
  315. put_header(s, GOP_START_CODE);
  316. put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
  317. /* time code : we must convert from the real frame rate to a
  318. fake mpeg frame rate in case of low frame rate */
  319. fps = (framerate.num + framerate.den/2)/ framerate.den;
  320. time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
  321. s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
  322. if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
  323. /* only works for NTSC 29.97 */
  324. int d = time_code / 17982;
  325. int m = time_code % 17982;
  326. //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
  327. time_code += 18 * d + 2 * ((m - 2) / 1798);
  328. }
  329. put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
  330. put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
  331. put_bits(&s->pb, 1, 1);
  332. put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
  333. put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
  334. put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
  335. put_bits(&s->pb, 1, 0); /* broken link */
  336. }
  337. }
  338. static inline void encode_mb_skip_run(MpegEncContext *s, int run){
  339. while (run >= 33) {
  340. put_bits(&s->pb, 11, 0x008);
  341. run -= 33;
  342. }
  343. put_bits(&s->pb, mbAddrIncrTable[run][1],
  344. mbAddrIncrTable[run][0]);
  345. }
  346. #endif //CONFIG_ENCODERS
  347. static void common_init(MpegEncContext *s)
  348. {
  349. s->y_dc_scale_table=
  350. s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
  351. }
  352. void ff_mpeg1_clean_buffers(MpegEncContext *s){
  353. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  354. s->last_dc[1] = s->last_dc[0];
  355. s->last_dc[2] = s->last_dc[0];
  356. memset(s->last_mv, 0, sizeof(s->last_mv));
  357. }
  358. #ifdef CONFIG_ENCODERS
  359. static av_always_inline void put_qscale(MpegEncContext *s)
  360. {
  361. if(s->q_scale_type){
  362. assert(s->qscale>=1 && s->qscale <=12);
  363. put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
  364. }else{
  365. put_bits(&s->pb, 5, s->qscale);
  366. }
  367. }
  368. void ff_mpeg1_encode_slice_header(MpegEncContext *s){
  369. put_header(s, SLICE_MIN_START_CODE + s->mb_y);
  370. put_qscale(s);
  371. put_bits(&s->pb, 1, 0); /* slice extra information */
  372. }
  373. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  374. {
  375. mpeg1_encode_sequence_header(s);
  376. /* mpeg1 picture header */
  377. put_header(s, PICTURE_START_CODE);
  378. /* temporal reference */
  379. // RAL: s->picture_number instead of s->fake_picture_number
  380. put_bits(&s->pb, 10, (s->picture_number -
  381. s->gop_picture_number) & 0x3ff);
  382. put_bits(&s->pb, 3, s->pict_type);
  383. s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
  384. put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
  385. // RAL: Forward f_code also needed for B frames
  386. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  387. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  388. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  389. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  390. else
  391. put_bits(&s->pb, 3, 7); /* forward_f_code */
  392. }
  393. // RAL: Backward f_code necessary for B frames
  394. if (s->pict_type == B_TYPE) {
  395. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  396. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  397. put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
  398. else
  399. put_bits(&s->pb, 3, 7); /* backward_f_code */
  400. }
  401. put_bits(&s->pb, 1, 0); /* extra bit picture */
  402. s->frame_pred_frame_dct = 1;
  403. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  404. put_header(s, EXT_START_CODE);
  405. put_bits(&s->pb, 4, 8); //pic ext
  406. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  407. put_bits(&s->pb, 4, s->f_code);
  408. put_bits(&s->pb, 4, s->f_code);
  409. }else{
  410. put_bits(&s->pb, 8, 255);
  411. }
  412. if (s->pict_type == B_TYPE) {
  413. put_bits(&s->pb, 4, s->b_code);
  414. put_bits(&s->pb, 4, s->b_code);
  415. }else{
  416. put_bits(&s->pb, 8, 255);
  417. }
  418. put_bits(&s->pb, 2, s->intra_dc_precision);
  419. assert(s->picture_structure == PICT_FRAME);
  420. put_bits(&s->pb, 2, s->picture_structure);
  421. if (s->progressive_sequence) {
  422. put_bits(&s->pb, 1, 0); /* no repeat */
  423. } else {
  424. put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
  425. }
  426. /* XXX: optimize the generation of this flag with entropy
  427. measures */
  428. s->frame_pred_frame_dct = s->progressive_sequence;
  429. put_bits(&s->pb, 1, s->frame_pred_frame_dct);
  430. put_bits(&s->pb, 1, s->concealment_motion_vectors);
  431. put_bits(&s->pb, 1, s->q_scale_type);
  432. put_bits(&s->pb, 1, s->intra_vlc_format);
  433. put_bits(&s->pb, 1, s->alternate_scan);
  434. put_bits(&s->pb, 1, s->repeat_first_field);
  435. s->progressive_frame = s->progressive_sequence;
  436. put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
  437. put_bits(&s->pb, 1, s->progressive_frame);
  438. put_bits(&s->pb, 1, 0); //composite_display_flag
  439. }
  440. if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
  441. int i;
  442. put_header(s, USER_START_CODE);
  443. for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
  444. put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
  445. }
  446. }
  447. s->mb_y=0;
  448. ff_mpeg1_encode_slice_header(s);
  449. }
  450. static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
  451. int has_mv, int field_motion)
  452. {
  453. put_bits(&s->pb, n, bits);
  454. if (!s->frame_pred_frame_dct) {
  455. if (has_mv)
  456. put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
  457. put_bits(&s->pb, 1, s->interlaced_dct);
  458. }
  459. }
  460. static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
  461. DCTELEM block[6][64],
  462. int motion_x, int motion_y,
  463. int mb_block_count)
  464. {
  465. int i, cbp;
  466. const int mb_x = s->mb_x;
  467. const int mb_y = s->mb_y;
  468. const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  469. /* compute cbp */
  470. cbp = 0;
  471. for(i=0;i<mb_block_count;i++) {
  472. if (s->block_last_index[i] >= 0)
  473. cbp |= 1 << (mb_block_count - 1 - i);
  474. }
  475. if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
  476. (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
  477. ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
  478. (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
  479. ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
  480. s->mb_skip_run++;
  481. s->qscale -= s->dquant;
  482. s->skip_count++;
  483. s->misc_bits++;
  484. s->last_bits++;
  485. if(s->pict_type == P_TYPE){
  486. s->last_mv[0][1][0]= s->last_mv[0][0][0]=
  487. s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
  488. }
  489. } else {
  490. if(first_mb){
  491. assert(s->mb_skip_run == 0);
  492. encode_mb_skip_run(s, s->mb_x);
  493. }else{
  494. encode_mb_skip_run(s, s->mb_skip_run);
  495. }
  496. if (s->pict_type == I_TYPE) {
  497. if(s->dquant && cbp){
  498. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
  499. put_qscale(s);
  500. }else{
  501. put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
  502. s->qscale -= s->dquant;
  503. }
  504. s->misc_bits+= get_bits_diff(s);
  505. s->i_count++;
  506. } else if (s->mb_intra) {
  507. if(s->dquant && cbp){
  508. put_mb_modes(s, 6, 0x01, 0, 0);
  509. put_qscale(s);
  510. }else{
  511. put_mb_modes(s, 5, 0x03, 0, 0);
  512. s->qscale -= s->dquant;
  513. }
  514. s->misc_bits+= get_bits_diff(s);
  515. s->i_count++;
  516. memset(s->last_mv, 0, sizeof(s->last_mv));
  517. } else if (s->pict_type == P_TYPE) {
  518. if(s->mv_type == MV_TYPE_16X16){
  519. if (cbp != 0) {
  520. if ((motion_x|motion_y) == 0) {
  521. if(s->dquant){
  522. put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
  523. put_qscale(s);
  524. }else{
  525. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
  526. }
  527. s->misc_bits+= get_bits_diff(s);
  528. } else {
  529. if(s->dquant){
  530. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  531. put_qscale(s);
  532. }else{
  533. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  534. }
  535. s->misc_bits+= get_bits_diff(s);
  536. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  537. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  538. s->mv_bits+= get_bits_diff(s);
  539. }
  540. } else {
  541. put_bits(&s->pb, 3, 1); /* motion only */
  542. if (!s->frame_pred_frame_dct)
  543. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  544. s->misc_bits+= get_bits_diff(s);
  545. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  546. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  547. s->qscale -= s->dquant;
  548. s->mv_bits+= get_bits_diff(s);
  549. }
  550. s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
  551. s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
  552. }else{
  553. assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  554. if (cbp) {
  555. if(s->dquant){
  556. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  557. put_qscale(s);
  558. }else{
  559. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  560. }
  561. } else {
  562. put_bits(&s->pb, 3, 1); /* motion only */
  563. put_bits(&s->pb, 2, 1); /* motion_type: field */
  564. s->qscale -= s->dquant;
  565. }
  566. s->misc_bits+= get_bits_diff(s);
  567. for(i=0; i<2; i++){
  568. put_bits(&s->pb, 1, s->field_select[0][i]);
  569. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  570. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  571. s->last_mv[0][i][0]= s->mv[0][i][0];
  572. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  573. }
  574. s->mv_bits+= get_bits_diff(s);
  575. }
  576. if(cbp) {
  577. if (s->chroma_y_shift) {
  578. put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
  579. } else {
  580. put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
  581. put_bits(&s->pb, 2, cbp & 3);
  582. }
  583. }
  584. s->f_count++;
  585. } else{
  586. static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
  587. if(s->mv_type == MV_TYPE_16X16){
  588. if (cbp){ // With coded bloc pattern
  589. if (s->dquant) {
  590. if(s->mv_dir == MV_DIR_FORWARD)
  591. put_mb_modes(s, 6, 3, 1, 0);
  592. else
  593. put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
  594. put_qscale(s);
  595. } else {
  596. put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
  597. }
  598. }else{ // No coded bloc pattern
  599. put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
  600. if (!s->frame_pred_frame_dct)
  601. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  602. s->qscale -= s->dquant;
  603. }
  604. s->misc_bits += get_bits_diff(s);
  605. if (s->mv_dir&MV_DIR_FORWARD){
  606. mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
  607. mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
  608. s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
  609. s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
  610. s->f_count++;
  611. }
  612. if (s->mv_dir&MV_DIR_BACKWARD){
  613. mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
  614. mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
  615. s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
  616. s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
  617. s->b_count++;
  618. }
  619. }else{
  620. assert(s->mv_type == MV_TYPE_FIELD);
  621. assert(!s->frame_pred_frame_dct);
  622. if (cbp){ // With coded bloc pattern
  623. if (s->dquant) {
  624. if(s->mv_dir == MV_DIR_FORWARD)
  625. put_mb_modes(s, 6, 3, 1, 1);
  626. else
  627. put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
  628. put_qscale(s);
  629. } else {
  630. put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
  631. }
  632. }else{ // No coded bloc pattern
  633. put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
  634. put_bits(&s->pb, 2, 1); /* motion_type: field */
  635. s->qscale -= s->dquant;
  636. }
  637. s->misc_bits += get_bits_diff(s);
  638. if (s->mv_dir&MV_DIR_FORWARD){
  639. for(i=0; i<2; i++){
  640. put_bits(&s->pb, 1, s->field_select[0][i]);
  641. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  642. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  643. s->last_mv[0][i][0]= s->mv[0][i][0];
  644. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  645. }
  646. s->f_count++;
  647. }
  648. if (s->mv_dir&MV_DIR_BACKWARD){
  649. for(i=0; i<2; i++){
  650. put_bits(&s->pb, 1, s->field_select[1][i]);
  651. mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
  652. mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
  653. s->last_mv[1][i][0]= s->mv[1][i][0];
  654. s->last_mv[1][i][1]= 2*s->mv[1][i][1];
  655. }
  656. s->b_count++;
  657. }
  658. }
  659. s->mv_bits += get_bits_diff(s);
  660. if(cbp) {
  661. if (s->chroma_y_shift) {
  662. put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
  663. } else {
  664. put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
  665. put_bits(&s->pb, 2, cbp & 3);
  666. }
  667. }
  668. }
  669. for(i=0;i<mb_block_count;i++) {
  670. if (cbp & (1 << (mb_block_count - 1 - i))) {
  671. mpeg1_encode_block(s, block[i], i);
  672. }
  673. }
  674. s->mb_skip_run = 0;
  675. if(s->mb_intra)
  676. s->i_tex_bits+= get_bits_diff(s);
  677. else
  678. s->p_tex_bits+= get_bits_diff(s);
  679. }
  680. }
  681. void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
  682. {
  683. if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
  684. else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
  685. }
  686. // RAL: Parameter added: f_or_b_code
  687. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
  688. {
  689. int code, bit_size, l, bits, range, sign;
  690. if (val == 0) {
  691. /* zero vector */
  692. code = 0;
  693. put_bits(&s->pb,
  694. mbMotionVectorTable[0][1],
  695. mbMotionVectorTable[0][0]);
  696. } else {
  697. bit_size = f_or_b_code - 1;
  698. range = 1 << bit_size;
  699. /* modulo encoding */
  700. l= INT_BIT - 5 - bit_size;
  701. val= (val<<l)>>l;
  702. if (val >= 0) {
  703. val--;
  704. code = (val >> bit_size) + 1;
  705. bits = val & (range - 1);
  706. sign = 0;
  707. } else {
  708. val = -val;
  709. val--;
  710. code = (val >> bit_size) + 1;
  711. bits = val & (range - 1);
  712. sign = 1;
  713. }
  714. assert(code > 0 && code <= 16);
  715. put_bits(&s->pb,
  716. mbMotionVectorTable[code][1],
  717. mbMotionVectorTable[code][0]);
  718. put_bits(&s->pb, 1, sign);
  719. if (bit_size > 0) {
  720. put_bits(&s->pb, bit_size, bits);
  721. }
  722. }
  723. }
  724. void ff_mpeg1_encode_init(MpegEncContext *s)
  725. {
  726. static int done=0;
  727. common_init(s);
  728. if(!done){
  729. int f_code;
  730. int mv;
  731. int i;
  732. done=1;
  733. init_rl(&rl_mpeg1, static_rl_table_store[0]);
  734. if(s->intra_vlc_format)
  735. init_rl(&rl_mpeg2, static_rl_table_store[1]);
  736. for(i=0; i<64; i++)
  737. {
  738. mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
  739. mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
  740. }
  741. init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
  742. if(s->intra_vlc_format)
  743. init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
  744. /* build unified dc encoding tables */
  745. for(i=-255; i<256; i++)
  746. {
  747. int adiff, index;
  748. int bits, code;
  749. int diff=i;
  750. adiff = FFABS(diff);
  751. if(diff<0) diff--;
  752. index = av_log2(2*adiff);
  753. bits= vlc_dc_lum_bits[index] + index;
  754. code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
  755. mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
  756. bits= vlc_dc_chroma_bits[index] + index;
  757. code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
  758. mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
  759. }
  760. for(f_code=1; f_code<=MAX_FCODE; f_code++){
  761. for(mv=-MAX_MV; mv<=MAX_MV; mv++){
  762. int len;
  763. if(mv==0) len= mbMotionVectorTable[0][1];
  764. else{
  765. int val, bit_size, range, code;
  766. bit_size = f_code - 1;
  767. range = 1 << bit_size;
  768. val=mv;
  769. if (val < 0)
  770. val = -val;
  771. val--;
  772. code = (val >> bit_size) + 1;
  773. if(code<17){
  774. len= mbMotionVectorTable[code][1] + 1 + bit_size;
  775. }else{
  776. len= mbMotionVectorTable[16][1] + 2 + bit_size;
  777. }
  778. }
  779. mv_penalty[f_code][mv+MAX_MV]= len;
  780. }
  781. }
  782. for(f_code=MAX_FCODE; f_code>0; f_code--){
  783. for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
  784. fcode_tab[mv+MAX_MV]= f_code;
  785. }
  786. }
  787. }
  788. s->me.mv_penalty= mv_penalty;
  789. s->fcode_tab= fcode_tab;
  790. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  791. s->min_qcoeff=-255;
  792. s->max_qcoeff= 255;
  793. }else{
  794. s->min_qcoeff=-2047;
  795. s->max_qcoeff= 2047;
  796. }
  797. if (s->intra_vlc_format) {
  798. s->intra_ac_vlc_length=
  799. s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
  800. } else {
  801. s->intra_ac_vlc_length=
  802. s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  803. }
  804. s->inter_ac_vlc_length=
  805. s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  806. }
  807. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  808. {
  809. if(((unsigned) (diff+255)) >= 511){
  810. int index;
  811. if(diff<0){
  812. index= av_log2_16bit(-2*diff);
  813. diff--;
  814. }else{
  815. index= av_log2_16bit(2*diff);
  816. }
  817. if (component == 0) {
  818. put_bits(
  819. &s->pb,
  820. vlc_dc_lum_bits[index] + index,
  821. (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
  822. }else{
  823. put_bits(
  824. &s->pb,
  825. vlc_dc_chroma_bits[index] + index,
  826. (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
  827. }
  828. }else{
  829. if (component == 0) {
  830. put_bits(
  831. &s->pb,
  832. mpeg1_lum_dc_uni[diff+255]&0xFF,
  833. mpeg1_lum_dc_uni[diff+255]>>8);
  834. } else {
  835. put_bits(
  836. &s->pb,
  837. mpeg1_chr_dc_uni[diff+255]&0xFF,
  838. mpeg1_chr_dc_uni[diff+255]>>8);
  839. }
  840. }
  841. }
  842. static void mpeg1_encode_block(MpegEncContext *s,
  843. DCTELEM *block,
  844. int n)
  845. {
  846. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  847. int code, component;
  848. const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
  849. last_index = s->block_last_index[n];
  850. /* DC coef */
  851. if (s->mb_intra) {
  852. component = (n <= 3 ? 0 : (n&1) + 1);
  853. dc = block[0]; /* overflow is impossible */
  854. diff = dc - s->last_dc[component];
  855. encode_dc(s, diff, component);
  856. s->last_dc[component] = dc;
  857. i = 1;
  858. if (s->intra_vlc_format)
  859. table_vlc = rl_mpeg2.table_vlc;
  860. } else {
  861. /* encode the first coefficient : needs to be done here because
  862. it is handled slightly differently */
  863. level = block[0];
  864. if (abs(level) == 1) {
  865. code = ((uint32_t)level >> 31); /* the sign bit */
  866. put_bits(&s->pb, 2, code | 0x02);
  867. i = 1;
  868. } else {
  869. i = 0;
  870. last_non_zero = -1;
  871. goto next_coef;
  872. }
  873. }
  874. /* now quantify & encode AC coefs */
  875. last_non_zero = i - 1;
  876. for(;i<=last_index;i++) {
  877. j = s->intra_scantable.permutated[i];
  878. level = block[j];
  879. next_coef:
  880. #if 0
  881. if (level != 0)
  882. dprintf(s->avctx, "level[%d]=%d\n", i, level);
  883. #endif
  884. /* encode using VLC */
  885. if (level != 0) {
  886. run = i - last_non_zero - 1;
  887. alevel= level;
  888. MASK_ABS(sign, alevel)
  889. sign&=1;
  890. if (alevel <= mpeg1_max_level[0][run]){
  891. code= mpeg1_index_run[0][run] + alevel - 1;
  892. /* store the vlc & sign at once */
  893. put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
  894. } else {
  895. /* escape seems to be pretty rare <5% so i dont optimize it */
  896. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  897. /* escape: only clip in this case */
  898. put_bits(&s->pb, 6, run);
  899. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  900. if (alevel < 128) {
  901. put_bits(&s->pb, 8, level & 0xff);
  902. } else {
  903. if (level < 0) {
  904. put_bits(&s->pb, 16, 0x8001 + level + 255);
  905. } else {
  906. put_bits(&s->pb, 16, level & 0xffff);
  907. }
  908. }
  909. }else{
  910. put_bits(&s->pb, 12, level & 0xfff);
  911. }
  912. }
  913. last_non_zero = i;
  914. }
  915. }
  916. /* end of block */
  917. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  918. }
  919. #endif //CONFIG_ENCODERS
  920. /******************************************/
  921. /* decoding */
  922. static VLC dc_lum_vlc;
  923. static VLC dc_chroma_vlc;
  924. static VLC mv_vlc;
  925. static VLC mbincr_vlc;
  926. static VLC mb_ptype_vlc;
  927. static VLC mb_btype_vlc;
  928. static VLC mb_pat_vlc;
  929. static void init_vlcs(void)
  930. {
  931. static int done = 0;
  932. if (!done) {
  933. done = 1;
  934. init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
  935. vlc_dc_lum_bits, 1, 1,
  936. vlc_dc_lum_code, 2, 2, 1);
  937. init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
  938. vlc_dc_chroma_bits, 1, 1,
  939. vlc_dc_chroma_code, 2, 2, 1);
  940. init_vlc(&mv_vlc, MV_VLC_BITS, 17,
  941. &mbMotionVectorTable[0][1], 2, 1,
  942. &mbMotionVectorTable[0][0], 2, 1, 1);
  943. init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  944. &mbAddrIncrTable[0][1], 2, 1,
  945. &mbAddrIncrTable[0][0], 2, 1, 1);
  946. init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  947. &mbPatTable[0][1], 2, 1,
  948. &mbPatTable[0][0], 2, 1, 1);
  949. init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  950. &table_mb_ptype[0][1], 2, 1,
  951. &table_mb_ptype[0][0], 2, 1, 1);
  952. init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  953. &table_mb_btype[0][1], 2, 1,
  954. &table_mb_btype[0][0], 2, 1, 1);
  955. init_rl(&rl_mpeg1, static_rl_table_store[0]);
  956. init_rl(&rl_mpeg2, static_rl_table_store[1]);
  957. init_2d_vlc_rl(&rl_mpeg1, 1);
  958. init_2d_vlc_rl(&rl_mpeg2, 1);
  959. }
  960. }
  961. static inline int get_dmv(MpegEncContext *s)
  962. {
  963. if(get_bits1(&s->gb))
  964. return 1 - (get_bits1(&s->gb) << 1);
  965. else
  966. return 0;
  967. }
  968. static inline int get_qscale(MpegEncContext *s)
  969. {
  970. int qscale = get_bits(&s->gb, 5);
  971. if (s->q_scale_type) {
  972. return non_linear_qscale[qscale];
  973. } else {
  974. return qscale << 1;
  975. }
  976. }
  977. /* motion type (for mpeg2) */
  978. #define MT_FIELD 1
  979. #define MT_FRAME 2
  980. #define MT_16X8 2
  981. #define MT_DMV 3
  982. static int mpeg_decode_mb(MpegEncContext *s,
  983. DCTELEM block[12][64])
  984. {
  985. int i, j, k, cbp, val, mb_type, motion_type;
  986. const int mb_block_count = 4 + (1<< s->chroma_format);
  987. dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  988. assert(s->mb_skipped==0);
  989. if (s->mb_skip_run-- != 0) {
  990. if(s->pict_type == I_TYPE){
  991. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  992. return -1;
  993. }
  994. /* skip mb */
  995. s->mb_intra = 0;
  996. for(i=0;i<12;i++)
  997. s->block_last_index[i] = -1;
  998. if(s->picture_structure == PICT_FRAME)
  999. s->mv_type = MV_TYPE_16X16;
  1000. else
  1001. s->mv_type = MV_TYPE_FIELD;
  1002. if (s->pict_type == P_TYPE) {
  1003. /* if P type, zero motion vector is implied */
  1004. s->mv_dir = MV_DIR_FORWARD;
  1005. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1006. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1007. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1008. s->field_select[0][0]= s->picture_structure - 1;
  1009. s->mb_skipped = 1;
  1010. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  1011. } else {
  1012. int mb_type;
  1013. if(s->mb_x)
  1014. mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
  1015. else
  1016. mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all,
  1017. if(IS_INTRA(mb_type))
  1018. return -1;
  1019. /* if B type, reuse previous vectors and directions */
  1020. s->mv[0][0][0] = s->last_mv[0][0][0];
  1021. s->mv[0][0][1] = s->last_mv[0][0][1];
  1022. s->mv[1][0][0] = s->last_mv[1][0][0];
  1023. s->mv[1][0][1] = s->last_mv[1][0][1];
  1024. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  1025. mb_type | MB_TYPE_SKIP;
  1026. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  1027. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  1028. s->mb_skipped = 1;
  1029. }
  1030. return 0;
  1031. }
  1032. switch(s->pict_type) {
  1033. default:
  1034. case I_TYPE:
  1035. if (get_bits1(&s->gb) == 0) {
  1036. if (get_bits1(&s->gb) == 0){
  1037. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  1038. return -1;
  1039. }
  1040. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  1041. } else {
  1042. mb_type = MB_TYPE_INTRA;
  1043. }
  1044. break;
  1045. case P_TYPE:
  1046. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  1047. if (mb_type < 0){
  1048. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  1049. return -1;
  1050. }
  1051. mb_type = ptype2mb_type[ mb_type ];
  1052. break;
  1053. case B_TYPE:
  1054. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  1055. if (mb_type < 0){
  1056. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  1057. return -1;
  1058. }
  1059. mb_type = btype2mb_type[ mb_type ];
  1060. break;
  1061. }
  1062. dprintf(s->avctx, "mb_type=%x\n", mb_type);
  1063. // motion_type = 0; /* avoid warning */
  1064. if (IS_INTRA(mb_type)) {
  1065. s->dsp.clear_blocks(s->block[0]);
  1066. if(!s->chroma_y_shift){
  1067. s->dsp.clear_blocks(s->block[6]);
  1068. }
  1069. /* compute dct type */
  1070. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1071. !s->frame_pred_frame_dct) {
  1072. s->interlaced_dct = get_bits1(&s->gb);
  1073. }
  1074. if (IS_QUANT(mb_type))
  1075. s->qscale = get_qscale(s);
  1076. if (s->concealment_motion_vectors) {
  1077. /* just parse them */
  1078. if (s->picture_structure != PICT_FRAME)
  1079. skip_bits1(&s->gb); /* field select */
  1080. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  1081. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  1082. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  1083. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  1084. skip_bits1(&s->gb); /* marker */
  1085. }else
  1086. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  1087. s->mb_intra = 1;
  1088. #ifdef HAVE_XVMC
  1089. //one 1 we memcpy blocks in xvmcvideo
  1090. if(s->avctx->xvmc_acceleration > 1){
  1091. XVMC_pack_pblocks(s,-1);//inter are always full blocks
  1092. if(s->swap_uv){
  1093. exchange_uv(s);
  1094. }
  1095. }
  1096. #endif
  1097. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1098. if(s->flags2 & CODEC_FLAG2_FAST){
  1099. for(i=0;i<6;i++) {
  1100. mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
  1101. }
  1102. }else{
  1103. for(i=0;i<mb_block_count;i++) {
  1104. if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
  1105. return -1;
  1106. }
  1107. }
  1108. } else {
  1109. for(i=0;i<6;i++) {
  1110. if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
  1111. return -1;
  1112. }
  1113. }
  1114. } else {
  1115. if (mb_type & MB_TYPE_ZERO_MV){
  1116. assert(mb_type & MB_TYPE_CBP);
  1117. /* compute dct type */
  1118. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1119. !s->frame_pred_frame_dct) {
  1120. s->interlaced_dct = get_bits1(&s->gb);
  1121. }
  1122. if (IS_QUANT(mb_type))
  1123. s->qscale = get_qscale(s);
  1124. s->mv_dir = MV_DIR_FORWARD;
  1125. if(s->picture_structure == PICT_FRAME)
  1126. s->mv_type = MV_TYPE_16X16;
  1127. else{
  1128. s->mv_type = MV_TYPE_FIELD;
  1129. mb_type |= MB_TYPE_INTERLACED;
  1130. s->field_select[0][0]= s->picture_structure - 1;
  1131. }
  1132. s->last_mv[0][0][0] = 0;
  1133. s->last_mv[0][0][1] = 0;
  1134. s->last_mv[0][1][0] = 0;
  1135. s->last_mv[0][1][1] = 0;
  1136. s->mv[0][0][0] = 0;
  1137. s->mv[0][0][1] = 0;
  1138. }else{
  1139. assert(mb_type & MB_TYPE_L0L1);
  1140. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  1141. /* get additionnal motion vector type */
  1142. if (s->frame_pred_frame_dct)
  1143. motion_type = MT_FRAME;
  1144. else{
  1145. motion_type = get_bits(&s->gb, 2);
  1146. }
  1147. /* compute dct type */
  1148. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1149. !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
  1150. s->interlaced_dct = get_bits1(&s->gb);
  1151. }
  1152. if (IS_QUANT(mb_type))
  1153. s->qscale = get_qscale(s);
  1154. /* motion vectors */
  1155. s->mv_dir = 0;
  1156. for(i=0;i<2;i++) {
  1157. if (USES_LIST(mb_type, i)) {
  1158. s->mv_dir |= (MV_DIR_FORWARD >> i);
  1159. dprintf(s->avctx, "motion_type=%d\n", motion_type);
  1160. switch(motion_type) {
  1161. case MT_FRAME: /* or MT_16X8 */
  1162. if (s->picture_structure == PICT_FRAME) {
  1163. /* MT_FRAME */
  1164. mb_type |= MB_TYPE_16x16;
  1165. s->mv_type = MV_TYPE_16X16;
  1166. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  1167. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  1168. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  1169. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  1170. /* full_pel: only for mpeg1 */
  1171. if (s->full_pel[i]){
  1172. s->mv[i][0][0] <<= 1;
  1173. s->mv[i][0][1] <<= 1;
  1174. }
  1175. } else {
  1176. /* MT_16X8 */
  1177. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1178. s->mv_type = MV_TYPE_16X8;
  1179. for(j=0;j<2;j++) {
  1180. s->field_select[i][j] = get_bits1(&s->gb);
  1181. for(k=0;k<2;k++) {
  1182. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1183. s->last_mv[i][j][k]);
  1184. s->last_mv[i][j][k] = val;
  1185. s->mv[i][j][k] = val;
  1186. }
  1187. }
  1188. }
  1189. break;
  1190. case MT_FIELD:
  1191. s->mv_type = MV_TYPE_FIELD;
  1192. if (s->picture_structure == PICT_FRAME) {
  1193. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1194. for(j=0;j<2;j++) {
  1195. s->field_select[i][j] = get_bits1(&s->gb);
  1196. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1197. s->last_mv[i][j][0]);
  1198. s->last_mv[i][j][0] = val;
  1199. s->mv[i][j][0] = val;
  1200. dprintf(s->avctx, "fmx=%d\n", val);
  1201. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1202. s->last_mv[i][j][1] >> 1);
  1203. s->last_mv[i][j][1] = val << 1;
  1204. s->mv[i][j][1] = val;
  1205. dprintf(s->avctx, "fmy=%d\n", val);
  1206. }
  1207. } else {
  1208. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1209. s->field_select[i][0] = get_bits1(&s->gb);
  1210. for(k=0;k<2;k++) {
  1211. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1212. s->last_mv[i][0][k]);
  1213. s->last_mv[i][0][k] = val;
  1214. s->last_mv[i][1][k] = val;
  1215. s->mv[i][0][k] = val;
  1216. }
  1217. }
  1218. break;
  1219. case MT_DMV:
  1220. {
  1221. int dmx, dmy, mx, my, m;
  1222. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1223. s->last_mv[i][0][0]);
  1224. s->last_mv[i][0][0] = mx;
  1225. s->last_mv[i][1][0] = mx;
  1226. dmx = get_dmv(s);
  1227. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1228. s->last_mv[i][0][1] >> 1);
  1229. dmy = get_dmv(s);
  1230. s->mv_type = MV_TYPE_DMV;
  1231. s->last_mv[i][0][1] = my<<1;
  1232. s->last_mv[i][1][1] = my<<1;
  1233. s->mv[i][0][0] = mx;
  1234. s->mv[i][0][1] = my;
  1235. s->mv[i][1][0] = mx;//not used
  1236. s->mv[i][1][1] = my;//not used
  1237. if (s->picture_structure == PICT_FRAME) {
  1238. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1239. //m = 1 + 2 * s->top_field_first;
  1240. m = s->top_field_first ? 1 : 3;
  1241. /* top -> top pred */
  1242. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1243. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  1244. m = 4 - m;
  1245. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1246. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  1247. } else {
  1248. mb_type |= MB_TYPE_16x16;
  1249. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  1250. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  1251. if(s->picture_structure == PICT_TOP_FIELD)
  1252. s->mv[i][2][1]--;
  1253. else
  1254. s->mv[i][2][1]++;
  1255. }
  1256. }
  1257. break;
  1258. default:
  1259. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  1260. return -1;
  1261. }
  1262. }
  1263. }
  1264. }
  1265. s->mb_intra = 0;
  1266. if (HAS_CBP(mb_type)) {
  1267. s->dsp.clear_blocks(s->block[0]);
  1268. if(!s->chroma_y_shift){
  1269. s->dsp.clear_blocks(s->block[6]);
  1270. }
  1271. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  1272. if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
  1273. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  1274. return -1;
  1275. }
  1276. if(mb_block_count > 6){
  1277. cbp<<= mb_block_count-6;
  1278. cbp |= get_bits(&s->gb, mb_block_count-6);
  1279. }
  1280. #ifdef HAVE_XVMC
  1281. //on 1 we memcpy blocks in xvmcvideo
  1282. if(s->avctx->xvmc_acceleration > 1){
  1283. XVMC_pack_pblocks(s,cbp);
  1284. if(s->swap_uv){
  1285. exchange_uv(s);
  1286. }
  1287. }
  1288. #endif
  1289. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1290. if(s->flags2 & CODEC_FLAG2_FAST){
  1291. for(i=0;i<6;i++) {
  1292. if(cbp & 32) {
  1293. mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
  1294. } else {
  1295. s->block_last_index[i] = -1;
  1296. }
  1297. cbp+=cbp;
  1298. }
  1299. }else{
  1300. cbp<<= 12-mb_block_count;
  1301. for(i=0;i<mb_block_count;i++) {
  1302. if ( cbp & (1<<11) ) {
  1303. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1304. return -1;
  1305. } else {
  1306. s->block_last_index[i] = -1;
  1307. }
  1308. cbp+=cbp;
  1309. }
  1310. }
  1311. } else {
  1312. if(s->flags2 & CODEC_FLAG2_FAST){
  1313. for(i=0;i<6;i++) {
  1314. if (cbp & 32) {
  1315. mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
  1316. } else {
  1317. s->block_last_index[i] = -1;
  1318. }
  1319. cbp+=cbp;
  1320. }
  1321. }else{
  1322. for(i=0;i<6;i++) {
  1323. if (cbp & 32) {
  1324. if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
  1325. return -1;
  1326. } else {
  1327. s->block_last_index[i] = -1;
  1328. }
  1329. cbp+=cbp;
  1330. }
  1331. }
  1332. }
  1333. }else{
  1334. for(i=0;i<12;i++)
  1335. s->block_last_index[i] = -1;
  1336. }
  1337. }
  1338. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  1339. return 0;
  1340. }
  1341. /* as h263, but only 17 codes */
  1342. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  1343. {
  1344. int code, sign, val, l, shift;
  1345. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  1346. if (code == 0) {
  1347. return pred;
  1348. }
  1349. if (code < 0) {
  1350. return 0xffff;
  1351. }
  1352. sign = get_bits1(&s->gb);
  1353. shift = fcode - 1;
  1354. val = code;
  1355. if (shift) {
  1356. val = (val - 1) << shift;
  1357. val |= get_bits(&s->gb, shift);
  1358. val++;
  1359. }
  1360. if (sign)
  1361. val = -val;
  1362. val += pred;
  1363. /* modulo decoding */
  1364. l= INT_BIT - 5 - shift;
  1365. val = (val<<l)>>l;
  1366. return val;
  1367. }
  1368. static inline int decode_dc(GetBitContext *gb, int component)
  1369. {
  1370. int code, diff;
  1371. if (component == 0) {
  1372. code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  1373. } else {
  1374. code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  1375. }
  1376. if (code < 0){
  1377. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  1378. return 0xffff;
  1379. }
  1380. if (code == 0) {
  1381. diff = 0;
  1382. } else {
  1383. diff = get_xbits(gb, code);
  1384. }
  1385. return diff;
  1386. }
  1387. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  1388. DCTELEM *block,
  1389. int n)
  1390. {
  1391. int level, dc, diff, i, j, run;
  1392. int component;
  1393. RLTable *rl = &rl_mpeg1;
  1394. uint8_t * const scantable= s->intra_scantable.permutated;
  1395. const uint16_t *quant_matrix= s->intra_matrix;
  1396. const int qscale= s->qscale;
  1397. /* DC coef */
  1398. component = (n <= 3 ? 0 : n - 4 + 1);
  1399. diff = decode_dc(&s->gb, component);
  1400. if (diff >= 0xffff)
  1401. return -1;
  1402. dc = s->last_dc[component];
  1403. dc += diff;
  1404. s->last_dc[component] = dc;
  1405. block[0] = dc<<3;
  1406. dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
  1407. i = 0;
  1408. {
  1409. OPEN_READER(re, &s->gb);
  1410. /* now quantify & encode AC coefs */
  1411. for(;;) {
  1412. UPDATE_CACHE(re, &s->gb);
  1413. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1414. if(level == 127){
  1415. break;
  1416. } else if(level != 0) {
  1417. i += run;
  1418. j = scantable[i];
  1419. level= (level*qscale*quant_matrix[j])>>4;
  1420. level= (level-1)|1;
  1421. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1422. LAST_SKIP_BITS(re, &s->gb, 1);
  1423. } else {
  1424. /* escape */
  1425. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1426. UPDATE_CACHE(re, &s->gb);
  1427. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1428. if (level == -128) {
  1429. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1430. } else if (level == 0) {
  1431. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1432. }
  1433. i += run;
  1434. j = scantable[i];
  1435. if(level<0){
  1436. level= -level;
  1437. level= (level*qscale*quant_matrix[j])>>4;
  1438. level= (level-1)|1;
  1439. level= -level;
  1440. }else{
  1441. level= (level*qscale*quant_matrix[j])>>4;
  1442. level= (level-1)|1;
  1443. }
  1444. }
  1445. if (i > 63){
  1446. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1447. return -1;
  1448. }
  1449. block[j] = level;
  1450. }
  1451. CLOSE_READER(re, &s->gb);
  1452. }
  1453. s->block_last_index[n] = i;
  1454. return 0;
  1455. }
  1456. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1457. DCTELEM *block,
  1458. int n)
  1459. {
  1460. int level, i, j, run;
  1461. RLTable *rl = &rl_mpeg1;
  1462. uint8_t * const scantable= s->intra_scantable.permutated;
  1463. const uint16_t *quant_matrix= s->inter_matrix;
  1464. const int qscale= s->qscale;
  1465. {
  1466. OPEN_READER(re, &s->gb);
  1467. i = -1;
  1468. /* special case for the first coef. no need to add a second vlc table */
  1469. UPDATE_CACHE(re, &s->gb);
  1470. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1471. level= (3*qscale*quant_matrix[0])>>5;
  1472. level= (level-1)|1;
  1473. if(GET_CACHE(re, &s->gb)&0x40000000)
  1474. level= -level;
  1475. block[0] = level;
  1476. i++;
  1477. SKIP_BITS(re, &s->gb, 2);
  1478. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1479. goto end;
  1480. }
  1481. /* now quantify & encode AC coefs */
  1482. for(;;) {
  1483. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1484. if(level != 0) {
  1485. i += run;
  1486. j = scantable[i];
  1487. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1488. level= (level-1)|1;
  1489. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1490. SKIP_BITS(re, &s->gb, 1);
  1491. } else {
  1492. /* escape */
  1493. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1494. UPDATE_CACHE(re, &s->gb);
  1495. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1496. if (level == -128) {
  1497. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  1498. } else if (level == 0) {
  1499. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  1500. }
  1501. i += run;
  1502. j = scantable[i];
  1503. if(level<0){
  1504. level= -level;
  1505. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1506. level= (level-1)|1;
  1507. level= -level;
  1508. }else{
  1509. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1510. level= (level-1)|1;
  1511. }
  1512. }
  1513. if (i > 63){
  1514. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1515. return -1;
  1516. }
  1517. block[j] = level;
  1518. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1519. break;
  1520. UPDATE_CACHE(re, &s->gb);
  1521. }
  1522. end:
  1523. LAST_SKIP_BITS(re, &s->gb, 2);
  1524. CLOSE_READER(re, &s->gb);
  1525. }
  1526. s->block_last_index[n] = i;
  1527. return 0;
  1528. }
  1529. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  1530. {
  1531. int level, i, j, run;
  1532. RLTable *rl = &rl_mpeg1;
  1533. uint8_t * const scantable= s->intra_scantable.permutated;
  1534. const int qscale= s->qscale;
  1535. {
  1536. OPEN_READER(re, &s->gb);
  1537. i = -1;
  1538. /* special case for the first coef. no need to add a second vlc table */
  1539. UPDATE_CACHE(re, &s->gb);
  1540. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1541. level= (3*qscale)>>1;
  1542. level= (level-1)|1;
  1543. if(GET_CACHE(re, &s->gb)&0x40000000)
  1544. level= -level;
  1545. block[0] = level;
  1546. i++;
  1547. SKIP_BITS(re, &s->gb, 2);
  1548. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1549. goto end;
  1550. }
  1551. /* now quantify & encode AC coefs */
  1552. for(;;) {
  1553. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1554. if(level != 0) {
  1555. i += run;
  1556. j = scantable[i];
  1557. level= ((level*2+1)*qscale)>>1;
  1558. level= (level-1)|1;
  1559. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1560. SKIP_BITS(re, &s->gb, 1);
  1561. } else {
  1562. /* escape */
  1563. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1564. UPDATE_CACHE(re, &s->gb);
  1565. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1566. if (level == -128) {
  1567. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  1568. } else if (level == 0) {
  1569. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  1570. }
  1571. i += run;
  1572. j = scantable[i];
  1573. if(level<0){
  1574. level= -level;
  1575. level= ((level*2+1)*qscale)>>1;
  1576. level= (level-1)|1;
  1577. level= -level;
  1578. }else{
  1579. level= ((level*2+1)*qscale)>>1;
  1580. level= (level-1)|1;
  1581. }
  1582. }
  1583. block[j] = level;
  1584. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1585. break;
  1586. UPDATE_CACHE(re, &s->gb);
  1587. }
  1588. end:
  1589. LAST_SKIP_BITS(re, &s->gb, 2);
  1590. CLOSE_READER(re, &s->gb);
  1591. }
  1592. s->block_last_index[n] = i;
  1593. return 0;
  1594. }
  1595. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1596. DCTELEM *block,
  1597. int n)
  1598. {
  1599. int level, i, j, run;
  1600. RLTable *rl = &rl_mpeg1;
  1601. uint8_t * const scantable= s->intra_scantable.permutated;
  1602. const uint16_t *quant_matrix;
  1603. const int qscale= s->qscale;
  1604. int mismatch;
  1605. mismatch = 1;
  1606. {
  1607. OPEN_READER(re, &s->gb);
  1608. i = -1;
  1609. if (n < 4)
  1610. quant_matrix = s->inter_matrix;
  1611. else
  1612. quant_matrix = s->chroma_inter_matrix;
  1613. /* special case for the first coef. no need to add a second vlc table */
  1614. UPDATE_CACHE(re, &s->gb);
  1615. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1616. level= (3*qscale*quant_matrix[0])>>5;
  1617. if(GET_CACHE(re, &s->gb)&0x40000000)
  1618. level= -level;
  1619. block[0] = level;
  1620. mismatch ^= level;
  1621. i++;
  1622. SKIP_BITS(re, &s->gb, 2);
  1623. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1624. goto end;
  1625. }
  1626. /* now quantify & encode AC coefs */
  1627. for(;;) {
  1628. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1629. if(level != 0) {
  1630. i += run;
  1631. j = scantable[i];
  1632. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1633. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1634. SKIP_BITS(re, &s->gb, 1);
  1635. } else {
  1636. /* escape */
  1637. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1638. UPDATE_CACHE(re, &s->gb);
  1639. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1640. i += run;
  1641. j = scantable[i];
  1642. if(level<0){
  1643. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1644. level= -level;
  1645. }else{
  1646. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1647. }
  1648. }
  1649. if (i > 63){
  1650. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1651. return -1;
  1652. }
  1653. mismatch ^= level;
  1654. block[j] = level;
  1655. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1656. break;
  1657. UPDATE_CACHE(re, &s->gb);
  1658. }
  1659. end:
  1660. LAST_SKIP_BITS(re, &s->gb, 2);
  1661. CLOSE_READER(re, &s->gb);
  1662. }
  1663. block[63] ^= (mismatch & 1);
  1664. s->block_last_index[n] = i;
  1665. return 0;
  1666. }
  1667. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  1668. DCTELEM *block,
  1669. int n)
  1670. {
  1671. int level, i, j, run;
  1672. RLTable *rl = &rl_mpeg1;
  1673. uint8_t * const scantable= s->intra_scantable.permutated;
  1674. const int qscale= s->qscale;
  1675. OPEN_READER(re, &s->gb);
  1676. i = -1;
  1677. /* special case for the first coef. no need to add a second vlc table */
  1678. UPDATE_CACHE(re, &s->gb);
  1679. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1680. level= (3*qscale)>>1;
  1681. if(GET_CACHE(re, &s->gb)&0x40000000)
  1682. level= -level;
  1683. block[0] = level;
  1684. i++;
  1685. SKIP_BITS(re, &s->gb, 2);
  1686. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1687. goto end;
  1688. }
  1689. /* now quantify & encode AC coefs */
  1690. for(;;) {
  1691. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1692. if(level != 0) {
  1693. i += run;
  1694. j = scantable[i];
  1695. level= ((level*2+1)*qscale)>>1;
  1696. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1697. SKIP_BITS(re, &s->gb, 1);
  1698. } else {
  1699. /* escape */
  1700. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1701. UPDATE_CACHE(re, &s->gb);
  1702. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1703. i += run;
  1704. j = scantable[i];
  1705. if(level<0){
  1706. level= ((-level*2+1)*qscale)>>1;
  1707. level= -level;
  1708. }else{
  1709. level= ((level*2+1)*qscale)>>1;
  1710. }
  1711. }
  1712. block[j] = level;
  1713. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1714. break;
  1715. UPDATE_CACHE(re, &s->gb);
  1716. }
  1717. end:
  1718. LAST_SKIP_BITS(re, &s->gb, 2);
  1719. CLOSE_READER(re, &s->gb);
  1720. s->block_last_index[n] = i;
  1721. return 0;
  1722. }
  1723. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1724. DCTELEM *block,
  1725. int n)
  1726. {
  1727. int level, dc, diff, i, j, run;
  1728. int component;
  1729. RLTable *rl;
  1730. uint8_t * const scantable= s->intra_scantable.permutated;
  1731. const uint16_t *quant_matrix;
  1732. const int qscale= s->qscale;
  1733. int mismatch;
  1734. /* DC coef */
  1735. if (n < 4){
  1736. quant_matrix = s->intra_matrix;
  1737. component = 0;
  1738. }else{
  1739. quant_matrix = s->chroma_intra_matrix;
  1740. component = (n&1) + 1;
  1741. }
  1742. diff = decode_dc(&s->gb, component);
  1743. if (diff >= 0xffff)
  1744. return -1;
  1745. dc = s->last_dc[component];
  1746. dc += diff;
  1747. s->last_dc[component] = dc;
  1748. block[0] = dc << (3 - s->intra_dc_precision);
  1749. dprintf(s->avctx, "dc=%d\n", block[0]);
  1750. mismatch = block[0] ^ 1;
  1751. i = 0;
  1752. if (s->intra_vlc_format)
  1753. rl = &rl_mpeg2;
  1754. else
  1755. rl = &rl_mpeg1;
  1756. {
  1757. OPEN_READER(re, &s->gb);
  1758. /* now quantify & encode AC coefs */
  1759. for(;;) {
  1760. UPDATE_CACHE(re, &s->gb);
  1761. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1762. if(level == 127){
  1763. break;
  1764. } else if(level != 0) {
  1765. i += run;
  1766. j = scantable[i];
  1767. level= (level*qscale*quant_matrix[j])>>4;
  1768. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1769. LAST_SKIP_BITS(re, &s->gb, 1);
  1770. } else {
  1771. /* escape */
  1772. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1773. UPDATE_CACHE(re, &s->gb);
  1774. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1775. i += run;
  1776. j = scantable[i];
  1777. if(level<0){
  1778. level= (-level*qscale*quant_matrix[j])>>4;
  1779. level= -level;
  1780. }else{
  1781. level= (level*qscale*quant_matrix[j])>>4;
  1782. }
  1783. }
  1784. if (i > 63){
  1785. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1786. return -1;
  1787. }
  1788. mismatch^= level;
  1789. block[j] = level;
  1790. }
  1791. CLOSE_READER(re, &s->gb);
  1792. }
  1793. block[63]^= mismatch&1;
  1794. s->block_last_index[n] = i;
  1795. return 0;
  1796. }
  1797. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
  1798. DCTELEM *block,
  1799. int n)
  1800. {
  1801. int level, dc, diff, j, run;
  1802. int component;
  1803. RLTable *rl;
  1804. uint8_t * scantable= s->intra_scantable.permutated;
  1805. const uint16_t *quant_matrix;
  1806. const int qscale= s->qscale;
  1807. /* DC coef */
  1808. if (n < 4){
  1809. quant_matrix = s->intra_matrix;
  1810. component = 0;
  1811. }else{
  1812. quant_matrix = s->chroma_intra_matrix;
  1813. component = (n&1) + 1;
  1814. }
  1815. diff = decode_dc(&s->gb, component);
  1816. if (diff >= 0xffff)
  1817. return -1;
  1818. dc = s->last_dc[component];
  1819. dc += diff;
  1820. s->last_dc[component] = dc;
  1821. block[0] = dc << (3 - s->intra_dc_precision);
  1822. if (s->intra_vlc_format)
  1823. rl = &rl_mpeg2;
  1824. else
  1825. rl = &rl_mpeg1;
  1826. {
  1827. OPEN_READER(re, &s->gb);
  1828. /* now quantify & encode AC coefs */
  1829. for(;;) {
  1830. UPDATE_CACHE(re, &s->gb);
  1831. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1832. if(level == 127){
  1833. break;
  1834. } else if(level != 0) {
  1835. scantable += run;
  1836. j = *scantable;
  1837. level= (level*qscale*quant_matrix[j])>>4;
  1838. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1839. LAST_SKIP_BITS(re, &s->gb, 1);
  1840. } else {
  1841. /* escape */
  1842. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1843. UPDATE_CACHE(re, &s->gb);
  1844. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1845. scantable += run;
  1846. j = *scantable;
  1847. if(level<0){
  1848. level= (-level*qscale*quant_matrix[j])>>4;
  1849. level= -level;
  1850. }else{
  1851. level= (level*qscale*quant_matrix[j])>>4;
  1852. }
  1853. }
  1854. block[j] = level;
  1855. }
  1856. CLOSE_READER(re, &s->gb);
  1857. }
  1858. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  1859. return 0;
  1860. }
  1861. typedef struct Mpeg1Context {
  1862. MpegEncContext mpeg_enc_ctx;
  1863. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1864. int repeat_field; /* true if we must repeat the field */
  1865. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1866. int slice_count;
  1867. int swap_uv;//indicate VCR2
  1868. int save_aspect_info;
  1869. AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
  1870. } Mpeg1Context;
  1871. static int mpeg_decode_init(AVCodecContext *avctx)
  1872. {
  1873. Mpeg1Context *s = avctx->priv_data;
  1874. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1875. int i;
  1876. //we need some parmutation to store
  1877. //matrixes, until MPV_common_init()
  1878. //set the real permutatuon
  1879. for(i=0;i<64;i++)
  1880. s2->dsp.idct_permutation[i]=i;
  1881. MPV_decode_defaults(s2);
  1882. s->mpeg_enc_ctx.avctx= avctx;
  1883. s->mpeg_enc_ctx.flags= avctx->flags;
  1884. s->mpeg_enc_ctx.flags2= avctx->flags2;
  1885. common_init(&s->mpeg_enc_ctx);
  1886. init_vlcs();
  1887. s->mpeg_enc_ctx_allocated = 0;
  1888. s->mpeg_enc_ctx.picture_number = 0;
  1889. s->repeat_field = 0;
  1890. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1891. return 0;
  1892. }
  1893. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1894. const uint8_t *new_perm){
  1895. uint16_t temp_matrix[64];
  1896. int i;
  1897. memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
  1898. for(i=0;i<64;i++){
  1899. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1900. }
  1901. }
  1902. //Call this function when we know all parameters
  1903. //it may be called in different places for mpeg1 and mpeg2
  1904. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1905. Mpeg1Context *s1 = avctx->priv_data;
  1906. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1907. uint8_t old_permutation[64];
  1908. if (
  1909. (s1->mpeg_enc_ctx_allocated == 0)||
  1910. avctx->coded_width != s->width ||
  1911. avctx->coded_height != s->height||
  1912. s1->save_aspect_info != s->aspect_ratio_info||
  1913. 0)
  1914. {
  1915. if (s1->mpeg_enc_ctx_allocated) {
  1916. ParseContext pc= s->parse_context;
  1917. s->parse_context.buffer=0;
  1918. MPV_common_end(s);
  1919. s->parse_context= pc;
  1920. }
  1921. if( (s->width == 0 )||(s->height == 0))
  1922. return -2;
  1923. avcodec_set_dimensions(avctx, s->width, s->height);
  1924. avctx->bit_rate = s->bit_rate;
  1925. s1->save_aspect_info = s->aspect_ratio_info;
  1926. //low_delay may be forced, in this case we will have B frames
  1927. //that behave like P frames
  1928. avctx->has_b_frames = !(s->low_delay);
  1929. if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
  1930. //mpeg1 fps
  1931. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1932. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1933. //mpeg1 aspect
  1934. avctx->sample_aspect_ratio= av_d2q(
  1935. 1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
  1936. }else{//mpeg2
  1937. //mpeg2 fps
  1938. av_reduce(
  1939. &s->avctx->time_base.den,
  1940. &s->avctx->time_base.num,
  1941. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1942. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1943. 1<<30);
  1944. //mpeg2 aspect
  1945. if(s->aspect_ratio_info > 1){
  1946. if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
  1947. s->avctx->sample_aspect_ratio=
  1948. av_div_q(
  1949. mpeg2_aspect[s->aspect_ratio_info],
  1950. (AVRational){s->width, s->height}
  1951. );
  1952. }else{
  1953. s->avctx->sample_aspect_ratio=
  1954. av_div_q(
  1955. mpeg2_aspect[s->aspect_ratio_info],
  1956. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1957. );
  1958. }
  1959. }else{
  1960. s->avctx->sample_aspect_ratio=
  1961. mpeg2_aspect[s->aspect_ratio_info];
  1962. }
  1963. }//mpeg2
  1964. if(avctx->xvmc_acceleration){
  1965. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1966. }else{
  1967. if(s->chroma_format < 2){
  1968. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  1969. }else
  1970. if(s->chroma_format == 2){
  1971. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
  1972. }else
  1973. if(s->chroma_format > 2){
  1974. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
  1975. }
  1976. }
  1977. //until then pix_fmt may be changed right after codec init
  1978. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1979. if( avctx->idct_algo == FF_IDCT_AUTO )
  1980. avctx->idct_algo = FF_IDCT_SIMPLE;
  1981. //quantization matrixes may need reordering
  1982. //if dct permutation is changed
  1983. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1984. if (MPV_common_init(s) < 0)
  1985. return -2;
  1986. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1987. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1988. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1989. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1990. s1->mpeg_enc_ctx_allocated = 1;
  1991. }
  1992. return 0;
  1993. }
  1994. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1995. const uint8_t *buf, int buf_size)
  1996. {
  1997. Mpeg1Context *s1 = avctx->priv_data;
  1998. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1999. int ref, f_code, vbv_delay;
  2000. if(mpeg_decode_postinit(s->avctx) < 0)
  2001. return -2;
  2002. init_get_bits(&s->gb, buf, buf_size*8);
  2003. ref = get_bits(&s->gb, 10); /* temporal ref */
  2004. s->pict_type = get_bits(&s->gb, 3);
  2005. if(s->pict_type == 0 || s->pict_type > 3)
  2006. return -1;
  2007. vbv_delay= get_bits(&s->gb, 16);
  2008. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  2009. s->full_pel[0] = get_bits1(&s->gb);
  2010. f_code = get_bits(&s->gb, 3);
  2011. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  2012. return -1;
  2013. s->mpeg_f_code[0][0] = f_code;
  2014. s->mpeg_f_code[0][1] = f_code;
  2015. }
  2016. if (s->pict_type == B_TYPE) {
  2017. s->full_pel[1] = get_bits1(&s->gb);
  2018. f_code = get_bits(&s->gb, 3);
  2019. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  2020. return -1;
  2021. s->mpeg_f_code[1][0] = f_code;
  2022. s->mpeg_f_code[1][1] = f_code;
  2023. }
  2024. s->current_picture.pict_type= s->pict_type;
  2025. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2026. if(avctx->debug & FF_DEBUG_PICT_INFO)
  2027. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  2028. s->y_dc_scale = 8;
  2029. s->c_dc_scale = 8;
  2030. s->first_slice = 1;
  2031. return 0;
  2032. }
  2033. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  2034. {
  2035. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2036. int horiz_size_ext, vert_size_ext;
  2037. int bit_rate_ext;
  2038. skip_bits(&s->gb, 1); /* profil and level esc*/
  2039. s->avctx->profile= get_bits(&s->gb, 3);
  2040. s->avctx->level= get_bits(&s->gb, 4);
  2041. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  2042. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  2043. horiz_size_ext = get_bits(&s->gb, 2);
  2044. vert_size_ext = get_bits(&s->gb, 2);
  2045. s->width |= (horiz_size_ext << 12);
  2046. s->height |= (vert_size_ext << 12);
  2047. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  2048. s->bit_rate += (bit_rate_ext << 18) * 400;
  2049. skip_bits1(&s->gb); /* marker */
  2050. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  2051. s->low_delay = get_bits1(&s->gb);
  2052. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2053. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  2054. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  2055. dprintf(s->avctx, "sequence extension\n");
  2056. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2057. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  2058. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2059. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  2060. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  2061. }
  2062. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  2063. {
  2064. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2065. int color_description, w, h;
  2066. skip_bits(&s->gb, 3); /* video format */
  2067. color_description= get_bits1(&s->gb);
  2068. if(color_description){
  2069. skip_bits(&s->gb, 8); /* color primaries */
  2070. skip_bits(&s->gb, 8); /* transfer_characteristics */
  2071. skip_bits(&s->gb, 8); /* matrix_coefficients */
  2072. }
  2073. w= get_bits(&s->gb, 14);
  2074. skip_bits(&s->gb, 1); //marker
  2075. h= get_bits(&s->gb, 14);
  2076. skip_bits(&s->gb, 1); //marker
  2077. s1->pan_scan.width= 16*w;
  2078. s1->pan_scan.height=16*h;
  2079. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2080. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  2081. }
  2082. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  2083. {
  2084. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2085. int i,nofco;
  2086. nofco = 1;
  2087. if(s->progressive_sequence){
  2088. if(s->repeat_first_field){
  2089. nofco++;
  2090. if(s->top_field_first)
  2091. nofco++;
  2092. }
  2093. }else{
  2094. if(s->picture_structure == PICT_FRAME){
  2095. nofco++;
  2096. if(s->repeat_first_field)
  2097. nofco++;
  2098. }
  2099. }
  2100. for(i=0; i<nofco; i++){
  2101. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  2102. skip_bits(&s->gb, 1); //marker
  2103. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  2104. skip_bits(&s->gb, 1); //marker
  2105. }
  2106. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2107. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  2108. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  2109. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  2110. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  2111. );
  2112. }
  2113. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  2114. {
  2115. int i, v, j;
  2116. dprintf(s->avctx, "matrix extension\n");
  2117. if (get_bits1(&s->gb)) {
  2118. for(i=0;i<64;i++) {
  2119. v = get_bits(&s->gb, 8);
  2120. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2121. s->intra_matrix[j] = v;
  2122. s->chroma_intra_matrix[j] = v;
  2123. }
  2124. }
  2125. if (get_bits1(&s->gb)) {
  2126. for(i=0;i<64;i++) {
  2127. v = get_bits(&s->gb, 8);
  2128. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2129. s->inter_matrix[j] = v;
  2130. s->chroma_inter_matrix[j] = v;
  2131. }
  2132. }
  2133. if (get_bits1(&s->gb)) {
  2134. for(i=0;i<64;i++) {
  2135. v = get_bits(&s->gb, 8);
  2136. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2137. s->chroma_intra_matrix[j] = v;
  2138. }
  2139. }
  2140. if (get_bits1(&s->gb)) {
  2141. for(i=0;i<64;i++) {
  2142. v = get_bits(&s->gb, 8);
  2143. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2144. s->chroma_inter_matrix[j] = v;
  2145. }
  2146. }
  2147. }
  2148. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  2149. {
  2150. s->full_pel[0] = s->full_pel[1] = 0;
  2151. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  2152. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  2153. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  2154. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  2155. s->intra_dc_precision = get_bits(&s->gb, 2);
  2156. s->picture_structure = get_bits(&s->gb, 2);
  2157. s->top_field_first = get_bits1(&s->gb);
  2158. s->frame_pred_frame_dct = get_bits1(&s->gb);
  2159. s->concealment_motion_vectors = get_bits1(&s->gb);
  2160. s->q_scale_type = get_bits1(&s->gb);
  2161. s->intra_vlc_format = get_bits1(&s->gb);
  2162. s->alternate_scan = get_bits1(&s->gb);
  2163. s->repeat_first_field = get_bits1(&s->gb);
  2164. s->chroma_420_type = get_bits1(&s->gb);
  2165. s->progressive_frame = get_bits1(&s->gb);
  2166. if(s->picture_structure == PICT_FRAME){
  2167. s->first_field=0;
  2168. s->v_edge_pos= 16*s->mb_height;
  2169. }else{
  2170. s->first_field ^= 1;
  2171. s->v_edge_pos= 8*s->mb_height;
  2172. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  2173. }
  2174. if(s->alternate_scan){
  2175. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  2176. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  2177. }else{
  2178. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  2179. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  2180. }
  2181. /* composite display not parsed */
  2182. dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  2183. dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
  2184. dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
  2185. dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  2186. dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  2187. dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  2188. dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  2189. dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  2190. dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  2191. }
  2192. static void mpeg_decode_extension(AVCodecContext *avctx,
  2193. const uint8_t *buf, int buf_size)
  2194. {
  2195. Mpeg1Context *s1 = avctx->priv_data;
  2196. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2197. int ext_type;
  2198. init_get_bits(&s->gb, buf, buf_size*8);
  2199. ext_type = get_bits(&s->gb, 4);
  2200. switch(ext_type) {
  2201. case 0x1:
  2202. mpeg_decode_sequence_extension(s1);
  2203. break;
  2204. case 0x2:
  2205. mpeg_decode_sequence_display_extension(s1);
  2206. break;
  2207. case 0x3:
  2208. mpeg_decode_quant_matrix_extension(s);
  2209. break;
  2210. case 0x7:
  2211. mpeg_decode_picture_display_extension(s1);
  2212. break;
  2213. case 0x8:
  2214. mpeg_decode_picture_coding_extension(s);
  2215. break;
  2216. }
  2217. }
  2218. static void exchange_uv(MpegEncContext *s){
  2219. short * tmp = s->pblocks[4];
  2220. s->pblocks[4] = s->pblocks[5];
  2221. s->pblocks[5] = tmp;
  2222. }
  2223. static int mpeg_field_start(MpegEncContext *s){
  2224. AVCodecContext *avctx= s->avctx;
  2225. Mpeg1Context *s1 = (Mpeg1Context*)s;
  2226. /* start frame decoding */
  2227. if(s->first_field || s->picture_structure==PICT_FRAME){
  2228. if(MPV_frame_start(s, avctx) < 0)
  2229. return -1;
  2230. ff_er_frame_start(s);
  2231. /* first check if we must repeat the frame */
  2232. s->current_picture_ptr->repeat_pict = 0;
  2233. if (s->repeat_first_field) {
  2234. if (s->progressive_sequence) {
  2235. if (s->top_field_first)
  2236. s->current_picture_ptr->repeat_pict = 4;
  2237. else
  2238. s->current_picture_ptr->repeat_pict = 2;
  2239. } else if (s->progressive_frame) {
  2240. s->current_picture_ptr->repeat_pict = 1;
  2241. }
  2242. }
  2243. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  2244. }else{ //second field
  2245. int i;
  2246. if(!s->current_picture_ptr){
  2247. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  2248. return -1;
  2249. }
  2250. for(i=0; i<4; i++){
  2251. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  2252. if(s->picture_structure == PICT_BOTTOM_FIELD){
  2253. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  2254. }
  2255. }
  2256. }
  2257. #ifdef HAVE_XVMC
  2258. // MPV_frame_start will call this function too,
  2259. // but we need to call it on every field
  2260. if(s->avctx->xvmc_acceleration)
  2261. XVMC_field_start(s,avctx);
  2262. #endif
  2263. return 0;
  2264. }
  2265. #define DECODE_SLICE_ERROR -1
  2266. #define DECODE_SLICE_OK 0
  2267. /**
  2268. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  2269. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  2270. * DECODE_SLICE_OK if this slice is ok<br>
  2271. */
  2272. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  2273. const uint8_t **buf, int buf_size)
  2274. {
  2275. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2276. AVCodecContext *avctx= s->avctx;
  2277. int ret;
  2278. const int field_pic= s->picture_structure != PICT_FRAME;
  2279. const int lowres= s->avctx->lowres;
  2280. s->resync_mb_x=
  2281. s->resync_mb_y= -1;
  2282. if (mb_y<<field_pic >= s->mb_height){
  2283. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
  2284. return -1;
  2285. }
  2286. init_get_bits(&s->gb, *buf, buf_size*8);
  2287. ff_mpeg1_clean_buffers(s);
  2288. s->interlaced_dct = 0;
  2289. s->qscale = get_qscale(s);
  2290. if(s->qscale == 0){
  2291. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  2292. return -1;
  2293. }
  2294. /* extra slice info */
  2295. while (get_bits1(&s->gb) != 0) {
  2296. skip_bits(&s->gb, 8);
  2297. }
  2298. s->mb_x=0;
  2299. for(;;) {
  2300. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2301. if (code < 0){
  2302. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  2303. return -1;
  2304. }
  2305. if (code >= 33) {
  2306. if (code == 33) {
  2307. s->mb_x += 33;
  2308. }
  2309. /* otherwise, stuffing, nothing to do */
  2310. } else {
  2311. s->mb_x += code;
  2312. break;
  2313. }
  2314. }
  2315. if(s->mb_x >= (unsigned)s->mb_width){
  2316. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  2317. return -1;
  2318. }
  2319. s->resync_mb_x= s->mb_x;
  2320. s->resync_mb_y= s->mb_y= mb_y;
  2321. s->mb_skip_run= 0;
  2322. ff_init_block_index(s);
  2323. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  2324. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2325. av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  2326. s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
  2327. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  2328. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  2329. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  2330. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  2331. }
  2332. }
  2333. for(;;) {
  2334. #ifdef HAVE_XVMC
  2335. //one 1 we memcpy blocks in xvmcvideo
  2336. if(s->avctx->xvmc_acceleration > 1)
  2337. XVMC_init_block(s);//set s->block
  2338. #endif
  2339. ret = mpeg_decode_mb(s, s->block);
  2340. s->chroma_qscale= s->qscale;
  2341. dprintf(s->avctx, "ret=%d\n", ret);
  2342. if (ret < 0)
  2343. return -1;
  2344. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  2345. const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
  2346. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  2347. int motion_x, motion_y, dir, i;
  2348. if(field_pic && !s->first_field)
  2349. xy += wrap/2;
  2350. for(i=0; i<2; i++){
  2351. for(dir=0; dir<2; dir++){
  2352. if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
  2353. motion_x = motion_y = 0;
  2354. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  2355. motion_x = s->mv[dir][0][0];
  2356. motion_y = s->mv[dir][0][1];
  2357. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  2358. motion_x = s->mv[dir][i][0];
  2359. motion_y = s->mv[dir][i][1];
  2360. }
  2361. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  2362. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  2363. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  2364. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  2365. s->current_picture.ref_index [dir][xy ]=
  2366. s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
  2367. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  2368. }
  2369. xy += wrap;
  2370. }
  2371. }
  2372. s->dest[0] += 16 >> lowres;
  2373. s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
  2374. s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
  2375. MPV_decode_mb(s, s->block);
  2376. if (++s->mb_x >= s->mb_width) {
  2377. const int mb_size= 16>>s->avctx->lowres;
  2378. ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
  2379. s->mb_x = 0;
  2380. s->mb_y++;
  2381. if(s->mb_y<<field_pic >= s->mb_height){
  2382. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  2383. int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
  2384. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  2385. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  2386. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  2387. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  2388. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  2389. return -1;
  2390. }else
  2391. goto eos;
  2392. }
  2393. ff_init_block_index(s);
  2394. }
  2395. /* skip mb handling */
  2396. if (s->mb_skip_run == -1) {
  2397. /* read again increment */
  2398. s->mb_skip_run = 0;
  2399. for(;;) {
  2400. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2401. if (code < 0){
  2402. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  2403. return -1;
  2404. }
  2405. if (code >= 33) {
  2406. if (code == 33) {
  2407. s->mb_skip_run += 33;
  2408. }else if(code == 35){
  2409. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  2410. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  2411. return -1;
  2412. }
  2413. goto eos; /* end of slice */
  2414. }
  2415. /* otherwise, stuffing, nothing to do */
  2416. } else {
  2417. s->mb_skip_run += code;
  2418. break;
  2419. }
  2420. }
  2421. }
  2422. }
  2423. eos: // end of slice
  2424. *buf += get_bits_count(&s->gb)/8 - 1;
  2425. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  2426. return 0;
  2427. }
  2428. static int slice_decode_thread(AVCodecContext *c, void *arg){
  2429. MpegEncContext *s= arg;
  2430. const uint8_t *buf= s->gb.buffer;
  2431. int mb_y= s->start_mb_y;
  2432. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  2433. for(;;){
  2434. uint32_t start_code;
  2435. int ret;
  2436. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  2437. emms_c();
  2438. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  2439. //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
  2440. if(ret < 0){
  2441. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  2442. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  2443. }else{
  2444. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
  2445. }
  2446. if(s->mb_y == s->end_mb_y)
  2447. return 0;
  2448. start_code= -1;
  2449. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  2450. mb_y= start_code - SLICE_MIN_START_CODE;
  2451. if(mb_y < 0 || mb_y >= s->end_mb_y)
  2452. return -1;
  2453. }
  2454. return 0; //not reached
  2455. }
  2456. /**
  2457. * handles slice ends.
  2458. * @return 1 if it seems to be the last slice of
  2459. */
  2460. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  2461. {
  2462. Mpeg1Context *s1 = avctx->priv_data;
  2463. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2464. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  2465. return 0;
  2466. #ifdef HAVE_XVMC
  2467. if(s->avctx->xvmc_acceleration)
  2468. XVMC_field_end(s);
  2469. #endif
  2470. /* end of slice reached */
  2471. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  2472. /* end of image */
  2473. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  2474. ff_er_frame_end(s);
  2475. MPV_frame_end(s);
  2476. if (s->pict_type == B_TYPE || s->low_delay) {
  2477. *pict= *(AVFrame*)s->current_picture_ptr;
  2478. ff_print_debug_info(s, pict);
  2479. } else {
  2480. s->picture_number++;
  2481. /* latency of 1 frame for I and P frames */
  2482. /* XXX: use another variable than picture_number */
  2483. if (s->last_picture_ptr != NULL) {
  2484. *pict= *(AVFrame*)s->last_picture_ptr;
  2485. ff_print_debug_info(s, pict);
  2486. }
  2487. }
  2488. return 1;
  2489. } else {
  2490. return 0;
  2491. }
  2492. }
  2493. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  2494. const uint8_t *buf, int buf_size)
  2495. {
  2496. Mpeg1Context *s1 = avctx->priv_data;
  2497. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2498. int width,height;
  2499. int i, v, j;
  2500. init_get_bits(&s->gb, buf, buf_size*8);
  2501. width = get_bits(&s->gb, 12);
  2502. height = get_bits(&s->gb, 12);
  2503. if (width <= 0 || height <= 0 ||
  2504. (width % 2) != 0 || (height % 2) != 0)
  2505. return -1;
  2506. s->aspect_ratio_info= get_bits(&s->gb, 4);
  2507. if (s->aspect_ratio_info == 0)
  2508. return -1;
  2509. s->frame_rate_index = get_bits(&s->gb, 4);
  2510. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  2511. return -1;
  2512. s->bit_rate = get_bits(&s->gb, 18) * 400;
  2513. if (get_bits1(&s->gb) == 0) /* marker */
  2514. return -1;
  2515. s->width = width;
  2516. s->height = height;
  2517. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  2518. skip_bits(&s->gb, 1);
  2519. /* get matrix */
  2520. if (get_bits1(&s->gb)) {
  2521. for(i=0;i<64;i++) {
  2522. v = get_bits(&s->gb, 8);
  2523. if(v==0){
  2524. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  2525. return -1;
  2526. }
  2527. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2528. s->intra_matrix[j] = v;
  2529. s->chroma_intra_matrix[j] = v;
  2530. }
  2531. #ifdef DEBUG
  2532. dprintf(s->avctx, "intra matrix present\n");
  2533. for(i=0;i<64;i++)
  2534. dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
  2535. dprintf(s->avctx, "\n");
  2536. #endif
  2537. } else {
  2538. for(i=0;i<64;i++) {
  2539. j = s->dsp.idct_permutation[i];
  2540. v = ff_mpeg1_default_intra_matrix[i];
  2541. s->intra_matrix[j] = v;
  2542. s->chroma_intra_matrix[j] = v;
  2543. }
  2544. }
  2545. if (get_bits1(&s->gb)) {
  2546. for(i=0;i<64;i++) {
  2547. v = get_bits(&s->gb, 8);
  2548. if(v==0){
  2549. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  2550. return -1;
  2551. }
  2552. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2553. s->inter_matrix[j] = v;
  2554. s->chroma_inter_matrix[j] = v;
  2555. }
  2556. #ifdef DEBUG
  2557. dprintf(s->avctx, "non intra matrix present\n");
  2558. for(i=0;i<64;i++)
  2559. dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
  2560. dprintf(s->avctx, "\n");
  2561. #endif
  2562. } else {
  2563. for(i=0;i<64;i++) {
  2564. int j= s->dsp.idct_permutation[i];
  2565. v = ff_mpeg1_default_non_intra_matrix[i];
  2566. s->inter_matrix[j] = v;
  2567. s->chroma_inter_matrix[j] = v;
  2568. }
  2569. }
  2570. if(show_bits(&s->gb, 23) != 0){
  2571. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  2572. return -1;
  2573. }
  2574. /* we set mpeg2 parameters so that it emulates mpeg1 */
  2575. s->progressive_sequence = 1;
  2576. s->progressive_frame = 1;
  2577. s->picture_structure = PICT_FRAME;
  2578. s->frame_pred_frame_dct = 1;
  2579. s->chroma_format = 1;
  2580. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  2581. avctx->sub_id = 1; /* indicates mpeg1 */
  2582. s->out_format = FMT_MPEG1;
  2583. s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
  2584. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2585. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2586. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  2587. s->avctx->rc_buffer_size, s->bit_rate);
  2588. return 0;
  2589. }
  2590. static int vcr2_init_sequence(AVCodecContext *avctx)
  2591. {
  2592. Mpeg1Context *s1 = avctx->priv_data;
  2593. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2594. int i, v;
  2595. /* start new mpeg1 context decoding */
  2596. s->out_format = FMT_MPEG1;
  2597. if (s1->mpeg_enc_ctx_allocated) {
  2598. MPV_common_end(s);
  2599. }
  2600. s->width = avctx->coded_width;
  2601. s->height = avctx->coded_height;
  2602. avctx->has_b_frames= 0; //true?
  2603. s->low_delay= 1;
  2604. if(avctx->xvmc_acceleration){
  2605. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  2606. }else{
  2607. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  2608. }
  2609. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2610. if( avctx->idct_algo == FF_IDCT_AUTO )
  2611. avctx->idct_algo = FF_IDCT_SIMPLE;
  2612. if (MPV_common_init(s) < 0)
  2613. return -1;
  2614. exchange_uv(s);//common init reset pblocks, so we swap them here
  2615. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  2616. s1->mpeg_enc_ctx_allocated = 1;
  2617. for(i=0;i<64;i++) {
  2618. int j= s->dsp.idct_permutation[i];
  2619. v = ff_mpeg1_default_intra_matrix[i];
  2620. s->intra_matrix[j] = v;
  2621. s->chroma_intra_matrix[j] = v;
  2622. v = ff_mpeg1_default_non_intra_matrix[i];
  2623. s->inter_matrix[j] = v;
  2624. s->chroma_inter_matrix[j] = v;
  2625. }
  2626. s->progressive_sequence = 1;
  2627. s->progressive_frame = 1;
  2628. s->picture_structure = PICT_FRAME;
  2629. s->frame_pred_frame_dct = 1;
  2630. s->chroma_format = 1;
  2631. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2632. avctx->sub_id = 2; /* indicates mpeg2 */
  2633. return 0;
  2634. }
  2635. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2636. const uint8_t *buf, int buf_size)
  2637. {
  2638. const uint8_t *p;
  2639. int len, flags;
  2640. p = buf;
  2641. len = buf_size;
  2642. /* we parse the DTG active format information */
  2643. if (len >= 5 &&
  2644. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2645. flags = p[4];
  2646. p += 5;
  2647. len -= 5;
  2648. if (flags & 0x80) {
  2649. /* skip event id */
  2650. if (len < 2)
  2651. return;
  2652. p += 2;
  2653. len -= 2;
  2654. }
  2655. if (flags & 0x40) {
  2656. if (len < 1)
  2657. return;
  2658. avctx->dtg_active_format = p[0] & 0x0f;
  2659. }
  2660. }
  2661. }
  2662. static void mpeg_decode_gop(AVCodecContext *avctx,
  2663. const uint8_t *buf, int buf_size){
  2664. Mpeg1Context *s1 = avctx->priv_data;
  2665. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2666. int drop_frame_flag;
  2667. int time_code_hours, time_code_minutes;
  2668. int time_code_seconds, time_code_pictures;
  2669. int broken_link;
  2670. init_get_bits(&s->gb, buf, buf_size*8);
  2671. drop_frame_flag = get_bits1(&s->gb);
  2672. time_code_hours=get_bits(&s->gb,5);
  2673. time_code_minutes = get_bits(&s->gb,6);
  2674. skip_bits1(&s->gb);//marker bit
  2675. time_code_seconds = get_bits(&s->gb,6);
  2676. time_code_pictures = get_bits(&s->gb,6);
  2677. /*broken_link indicate that after editing the
  2678. reference frames of the first B-Frames after GOP I-Frame
  2679. are missing (open gop)*/
  2680. broken_link = get_bits1(&s->gb);
  2681. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2682. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
  2683. time_code_hours, time_code_minutes, time_code_seconds,
  2684. time_code_pictures, broken_link);
  2685. }
  2686. /**
  2687. * finds the end of the current frame in the bitstream.
  2688. * @return the position of the first byte of the next frame, or -1
  2689. */
  2690. static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  2691. {
  2692. int i;
  2693. uint32_t state= pc->state;
  2694. i=0;
  2695. if(!pc->frame_start_found){
  2696. for(i=0; i<buf_size; i++){
  2697. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  2698. if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  2699. i++;
  2700. pc->frame_start_found=1;
  2701. break;
  2702. }
  2703. }
  2704. }
  2705. if(pc->frame_start_found){
  2706. /* EOF considered as end of frame */
  2707. if (buf_size == 0)
  2708. return 0;
  2709. for(; i<buf_size; i++){
  2710. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  2711. if((state&0xFFFFFF00) == 0x100){
  2712. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  2713. pc->frame_start_found=0;
  2714. pc->state=-1;
  2715. return i-3;
  2716. }
  2717. }
  2718. }
  2719. }
  2720. pc->state= state;
  2721. return END_NOT_FOUND;
  2722. }
  2723. /* handle buffering and image synchronisation */
  2724. static int mpeg_decode_frame(AVCodecContext *avctx,
  2725. void *data, int *data_size,
  2726. uint8_t *buf, int buf_size)
  2727. {
  2728. Mpeg1Context *s = avctx->priv_data;
  2729. const uint8_t *buf_end;
  2730. const uint8_t *buf_ptr;
  2731. uint32_t start_code;
  2732. int ret, input_size;
  2733. AVFrame *picture = data;
  2734. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2735. dprintf(avctx, "fill_buffer\n");
  2736. if (buf_size == 0) {
  2737. /* special case for last picture */
  2738. if (s2->low_delay==0 && s2->next_picture_ptr) {
  2739. *picture= *(AVFrame*)s2->next_picture_ptr;
  2740. s2->next_picture_ptr= NULL;
  2741. *data_size = sizeof(AVFrame);
  2742. }
  2743. return 0;
  2744. }
  2745. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2746. int next= mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
  2747. if( ff_combine_frame(&s2->parse_context, next, &buf, &buf_size) < 0 )
  2748. return buf_size;
  2749. }
  2750. buf_ptr = buf;
  2751. buf_end = buf + buf_size;
  2752. #if 0
  2753. if (s->repeat_field % 2 == 1) {
  2754. s->repeat_field++;
  2755. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2756. // s2->picture_number, s->repeat_field);
  2757. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2758. *data_size = sizeof(AVPicture);
  2759. goto the_end;
  2760. }
  2761. }
  2762. #endif
  2763. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2764. vcr2_init_sequence(avctx);
  2765. s->slice_count= 0;
  2766. for(;;) {
  2767. /* find start next code */
  2768. start_code = -1;
  2769. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2770. if (start_code > 0x1ff){
  2771. if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2772. if(avctx->thread_count > 1){
  2773. int i;
  2774. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
  2775. for(i=0; i<s->slice_count; i++)
  2776. s2->error_count += s2->thread_context[i]->error_count;
  2777. }
  2778. if (slice_end(avctx, picture)) {
  2779. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2780. *data_size = sizeof(AVPicture);
  2781. }
  2782. }
  2783. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2784. }
  2785. input_size = buf_end - buf_ptr;
  2786. if(avctx->debug & FF_DEBUG_STARTCODE){
  2787. av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
  2788. }
  2789. /* prepare data for next start code */
  2790. switch(start_code) {
  2791. case SEQ_START_CODE:
  2792. mpeg1_decode_sequence(avctx, buf_ptr,
  2793. input_size);
  2794. break;
  2795. case PICTURE_START_CODE:
  2796. /* we have a complete image : we try to decompress it */
  2797. mpeg1_decode_picture(avctx,
  2798. buf_ptr, input_size);
  2799. break;
  2800. case EXT_START_CODE:
  2801. mpeg_decode_extension(avctx,
  2802. buf_ptr, input_size);
  2803. break;
  2804. case USER_START_CODE:
  2805. mpeg_decode_user_data(avctx,
  2806. buf_ptr, input_size);
  2807. break;
  2808. case GOP_START_CODE:
  2809. s2->first_field=0;
  2810. mpeg_decode_gop(avctx,
  2811. buf_ptr, input_size);
  2812. break;
  2813. default:
  2814. if (start_code >= SLICE_MIN_START_CODE &&
  2815. start_code <= SLICE_MAX_START_CODE) {
  2816. int mb_y= start_code - SLICE_MIN_START_CODE;
  2817. if(s2->last_picture_ptr==NULL){
  2818. /* skip b frames if we dont have reference frames */
  2819. if(s2->pict_type==B_TYPE) break;
  2820. /* skip P frames if we dont have reference frame no valid header */
  2821. // if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
  2822. }
  2823. /* skip b frames if we are in a hurry */
  2824. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  2825. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
  2826. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
  2827. || avctx->skip_frame >= AVDISCARD_ALL)
  2828. break;
  2829. /* skip everything if we are in a hurry>=5 */
  2830. if(avctx->hurry_up>=5) break;
  2831. if (!s->mpeg_enc_ctx_allocated) break;
  2832. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2833. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2834. break;
  2835. }
  2836. if(s2->first_slice){
  2837. s2->first_slice=0;
  2838. if(mpeg_field_start(s2) < 0)
  2839. return -1;
  2840. }
  2841. if(!s2->current_picture_ptr){
  2842. av_log(avctx, AV_LOG_ERROR, "current_picture not initalized\n");
  2843. return -1;
  2844. }
  2845. if(avctx->thread_count > 1){
  2846. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2847. if(threshold <= mb_y){
  2848. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2849. thread_context->start_mb_y= mb_y;
  2850. thread_context->end_mb_y = s2->mb_height;
  2851. if(s->slice_count){
  2852. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2853. ff_update_duplicate_context(thread_context, s2);
  2854. }
  2855. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2856. s->slice_count++;
  2857. }
  2858. buf_ptr += 2; //FIXME add minimum num of bytes per slice
  2859. }else{
  2860. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2861. emms_c();
  2862. if(ret < 0){
  2863. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2864. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  2865. }else{
  2866. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
  2867. }
  2868. }
  2869. }
  2870. break;
  2871. }
  2872. }
  2873. }
  2874. static int mpeg_decode_end(AVCodecContext *avctx)
  2875. {
  2876. Mpeg1Context *s = avctx->priv_data;
  2877. if (s->mpeg_enc_ctx_allocated)
  2878. MPV_common_end(&s->mpeg_enc_ctx);
  2879. return 0;
  2880. }
  2881. AVCodec mpeg1video_decoder = {
  2882. "mpeg1video",
  2883. CODEC_TYPE_VIDEO,
  2884. CODEC_ID_MPEG1VIDEO,
  2885. sizeof(Mpeg1Context),
  2886. mpeg_decode_init,
  2887. NULL,
  2888. mpeg_decode_end,
  2889. mpeg_decode_frame,
  2890. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2891. .flush= ff_mpeg_flush,
  2892. };
  2893. AVCodec mpeg2video_decoder = {
  2894. "mpeg2video",
  2895. CODEC_TYPE_VIDEO,
  2896. CODEC_ID_MPEG2VIDEO,
  2897. sizeof(Mpeg1Context),
  2898. mpeg_decode_init,
  2899. NULL,
  2900. mpeg_decode_end,
  2901. mpeg_decode_frame,
  2902. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2903. .flush= ff_mpeg_flush,
  2904. };
  2905. //legacy decoder
  2906. AVCodec mpegvideo_decoder = {
  2907. "mpegvideo",
  2908. CODEC_TYPE_VIDEO,
  2909. CODEC_ID_MPEG2VIDEO,
  2910. sizeof(Mpeg1Context),
  2911. mpeg_decode_init,
  2912. NULL,
  2913. mpeg_decode_end,
  2914. mpeg_decode_frame,
  2915. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2916. .flush= ff_mpeg_flush,
  2917. };
  2918. #ifdef CONFIG_ENCODERS
  2919. AVCodec mpeg1video_encoder = {
  2920. "mpeg1video",
  2921. CODEC_TYPE_VIDEO,
  2922. CODEC_ID_MPEG1VIDEO,
  2923. sizeof(MpegEncContext),
  2924. encode_init,
  2925. MPV_encode_picture,
  2926. MPV_encode_end,
  2927. .supported_framerates= ff_frame_rate_tab+1,
  2928. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
  2929. .capabilities= CODEC_CAP_DELAY,
  2930. };
  2931. AVCodec mpeg2video_encoder = {
  2932. "mpeg2video",
  2933. CODEC_TYPE_VIDEO,
  2934. CODEC_ID_MPEG2VIDEO,
  2935. sizeof(MpegEncContext),
  2936. encode_init,
  2937. MPV_encode_picture,
  2938. MPV_encode_end,
  2939. .supported_framerates= ff_frame_rate_tab+1,
  2940. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
  2941. .capabilities= CODEC_CAP_DELAY,
  2942. };
  2943. #endif
  2944. #ifdef HAVE_XVMC
  2945. static int mpeg_mc_decode_init(AVCodecContext *avctx){
  2946. Mpeg1Context *s;
  2947. if( avctx->thread_count > 1)
  2948. return -1;
  2949. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2950. return -1;
  2951. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2952. dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2953. }
  2954. mpeg_decode_init(avctx);
  2955. s = avctx->priv_data;
  2956. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2957. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2958. return 0;
  2959. }
  2960. AVCodec mpeg_xvmc_decoder = {
  2961. "mpegvideo_xvmc",
  2962. CODEC_TYPE_VIDEO,
  2963. CODEC_ID_MPEG2VIDEO_XVMC,
  2964. sizeof(Mpeg1Context),
  2965. mpeg_mc_decode_init,
  2966. NULL,
  2967. mpeg_decode_end,
  2968. mpeg_decode_frame,
  2969. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2970. .flush= ff_mpeg_flush,
  2971. };
  2972. #endif
  2973. #ifdef CONFIG_MPEGVIDEO_PARSER
  2974. static void mpegvideo_extract_headers(AVCodecParserContext *s,
  2975. AVCodecContext *avctx,
  2976. const uint8_t *buf, int buf_size)
  2977. {
  2978. ParseContext1 *pc = s->priv_data;
  2979. const uint8_t *buf_end;
  2980. uint32_t start_code;
  2981. int frame_rate_index, ext_type, bytes_left;
  2982. int frame_rate_ext_n, frame_rate_ext_d;
  2983. int picture_structure, top_field_first, repeat_first_field, progressive_frame;
  2984. int horiz_size_ext, vert_size_ext, bit_rate_ext;
  2985. //FIXME replace the crap with get_bits()
  2986. s->repeat_pict = 0;
  2987. buf_end = buf + buf_size;
  2988. while (buf < buf_end) {
  2989. start_code= -1;
  2990. buf= ff_find_start_code(buf, buf_end, &start_code);
  2991. bytes_left = buf_end - buf;
  2992. switch(start_code) {
  2993. case PICTURE_START_CODE:
  2994. if (bytes_left >= 2) {
  2995. s->pict_type = (buf[1] >> 3) & 7;
  2996. }
  2997. break;
  2998. case SEQ_START_CODE:
  2999. if (bytes_left >= 7) {
  3000. pc->width = (buf[0] << 4) | (buf[1] >> 4);
  3001. pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
  3002. avcodec_set_dimensions(avctx, pc->width, pc->height);
  3003. frame_rate_index = buf[3] & 0xf;
  3004. pc->frame_rate.den = avctx->time_base.den = ff_frame_rate_tab[frame_rate_index].num;
  3005. pc->frame_rate.num = avctx->time_base.num = ff_frame_rate_tab[frame_rate_index].den;
  3006. avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
  3007. avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  3008. avctx->sub_id = 1;
  3009. }
  3010. break;
  3011. case EXT_START_CODE:
  3012. if (bytes_left >= 1) {
  3013. ext_type = (buf[0] >> 4);
  3014. switch(ext_type) {
  3015. case 0x1: /* sequence extension */
  3016. if (bytes_left >= 6) {
  3017. horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
  3018. vert_size_ext = (buf[2] >> 5) & 3;
  3019. bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
  3020. frame_rate_ext_n = (buf[5] >> 5) & 3;
  3021. frame_rate_ext_d = (buf[5] & 0x1f);
  3022. pc->progressive_sequence = buf[1] & (1 << 3);
  3023. avctx->has_b_frames= !(buf[5] >> 7);
  3024. pc->width |=(horiz_size_ext << 12);
  3025. pc->height |=( vert_size_ext << 12);
  3026. avctx->bit_rate += (bit_rate_ext << 18) * 400;
  3027. avcodec_set_dimensions(avctx, pc->width, pc->height);
  3028. avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1);
  3029. avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
  3030. avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  3031. avctx->sub_id = 2; /* forces MPEG2 */
  3032. }
  3033. break;
  3034. case 0x8: /* picture coding extension */
  3035. if (bytes_left >= 5) {
  3036. picture_structure = buf[2]&3;
  3037. top_field_first = buf[3] & (1 << 7);
  3038. repeat_first_field = buf[3] & (1 << 1);
  3039. progressive_frame = buf[4] & (1 << 7);
  3040. /* check if we must repeat the frame */
  3041. if (repeat_first_field) {
  3042. if (pc->progressive_sequence) {
  3043. if (top_field_first)
  3044. s->repeat_pict = 4;
  3045. else
  3046. s->repeat_pict = 2;
  3047. } else if (progressive_frame) {
  3048. s->repeat_pict = 1;
  3049. }
  3050. }
  3051. /* the packet only represents half a frame
  3052. XXX,FIXME maybe find a different solution */
  3053. if(picture_structure != 3)
  3054. s->repeat_pict = -1;
  3055. }
  3056. break;
  3057. }
  3058. }
  3059. break;
  3060. case -1:
  3061. goto the_end;
  3062. default:
  3063. /* we stop parsing when we encounter a slice. It ensures
  3064. that this function takes a negligible amount of time */
  3065. if (start_code >= SLICE_MIN_START_CODE &&
  3066. start_code <= SLICE_MAX_START_CODE)
  3067. goto the_end;
  3068. break;
  3069. }
  3070. }
  3071. the_end: ;
  3072. }
  3073. static int mpegvideo_parse(AVCodecParserContext *s,
  3074. AVCodecContext *avctx,
  3075. uint8_t **poutbuf, int *poutbuf_size,
  3076. const uint8_t *buf, int buf_size)
  3077. {
  3078. ParseContext1 *pc1 = s->priv_data;
  3079. ParseContext *pc= &pc1->pc;
  3080. int next;
  3081. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  3082. next= buf_size;
  3083. }else{
  3084. next= mpeg1_find_frame_end(pc, buf, buf_size);
  3085. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  3086. *poutbuf = NULL;
  3087. *poutbuf_size = 0;
  3088. return buf_size;
  3089. }
  3090. }
  3091. /* we have a full frame : we just parse the first few MPEG headers
  3092. to have the full timing information. The time take by this
  3093. function should be negligible for uncorrupted streams */
  3094. mpegvideo_extract_headers(s, avctx, buf, buf_size);
  3095. #if 0
  3096. printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
  3097. s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
  3098. #endif
  3099. *poutbuf = (uint8_t *)buf;
  3100. *poutbuf_size = buf_size;
  3101. return next;
  3102. }
  3103. static int mpegvideo_split(AVCodecContext *avctx,
  3104. const uint8_t *buf, int buf_size)
  3105. {
  3106. int i;
  3107. uint32_t state= -1;
  3108. for(i=0; i<buf_size; i++){
  3109. state= (state<<8) | buf[i];
  3110. if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
  3111. return i-3;
  3112. }
  3113. return 0;
  3114. }
  3115. AVCodecParser mpegvideo_parser = {
  3116. { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
  3117. sizeof(ParseContext1),
  3118. NULL,
  3119. mpegvideo_parse,
  3120. ff_parse1_close,
  3121. mpegvideo_split,
  3122. };
  3123. #endif /* !CONFIG_MPEGVIDEO_PARSER */
  3124. static int imx_dump_header(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  3125. uint8_t **poutbuf, int *poutbuf_size,
  3126. const uint8_t *buf, int buf_size, int keyframe)
  3127. {
  3128. /* MXF essence element key */
  3129. static const uint8_t imx_header[16] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x05,0x01,0x01,0x00 };
  3130. uint8_t *poutbufp;
  3131. if (avctx->codec_id != CODEC_ID_MPEG2VIDEO) {
  3132. av_log(avctx, AV_LOG_ERROR, "imx bitstream filter only applies to mpeg2video codec\n");
  3133. return 0;
  3134. }
  3135. *poutbuf = av_malloc(buf_size + 20 + FF_INPUT_BUFFER_PADDING_SIZE);
  3136. poutbufp = *poutbuf;
  3137. bytestream_put_buffer(&poutbufp, imx_header, 16);
  3138. bytestream_put_byte(&poutbufp, 0x83); /* KLV BER long form */
  3139. bytestream_put_be24(&poutbufp, buf_size);
  3140. bytestream_put_buffer(&poutbufp, buf, buf_size);
  3141. *poutbuf_size = poutbufp - *poutbuf;
  3142. return 1;
  3143. }
  3144. AVBitStreamFilter imx_dump_header_bsf = {
  3145. "imxdump",
  3146. 0,
  3147. imx_dump_header,
  3148. };
  3149. /* this is ugly i know, but the alternative is too make
  3150. hundreds of vars global and prefix them with ff_mpeg1_
  3151. which is far uglier. */
  3152. #include "mdec.c"