mpeg12.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557
  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. init_rl(&rl_mpeg2, static_rl_table_store[1]);
  735. for(i=0; i<64; i++)
  736. {
  737. mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
  738. mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
  739. }
  740. init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
  741. if(s->intra_vlc_format)
  742. init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
  743. /* build unified dc encoding tables */
  744. for(i=-255; i<256; i++)
  745. {
  746. int adiff, index;
  747. int bits, code;
  748. int diff=i;
  749. adiff = FFABS(diff);
  750. if(diff<0) diff--;
  751. index = av_log2(2*adiff);
  752. bits= vlc_dc_lum_bits[index] + index;
  753. code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
  754. mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
  755. bits= vlc_dc_chroma_bits[index] + index;
  756. code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
  757. mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
  758. }
  759. for(f_code=1; f_code<=MAX_FCODE; f_code++){
  760. for(mv=-MAX_MV; mv<=MAX_MV; mv++){
  761. int len;
  762. if(mv==0) len= mbMotionVectorTable[0][1];
  763. else{
  764. int val, bit_size, range, code;
  765. bit_size = f_code - 1;
  766. range = 1 << bit_size;
  767. val=mv;
  768. if (val < 0)
  769. val = -val;
  770. val--;
  771. code = (val >> bit_size) + 1;
  772. if(code<17){
  773. len= mbMotionVectorTable[code][1] + 1 + bit_size;
  774. }else{
  775. len= mbMotionVectorTable[16][1] + 2 + bit_size;
  776. }
  777. }
  778. mv_penalty[f_code][mv+MAX_MV]= len;
  779. }
  780. }
  781. for(f_code=MAX_FCODE; f_code>0; f_code--){
  782. for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
  783. fcode_tab[mv+MAX_MV]= f_code;
  784. }
  785. }
  786. }
  787. s->me.mv_penalty= mv_penalty;
  788. s->fcode_tab= fcode_tab;
  789. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  790. s->min_qcoeff=-255;
  791. s->max_qcoeff= 255;
  792. }else{
  793. s->min_qcoeff=-2047;
  794. s->max_qcoeff= 2047;
  795. }
  796. if (s->intra_vlc_format) {
  797. s->intra_ac_vlc_length=
  798. s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
  799. } else {
  800. s->intra_ac_vlc_length=
  801. s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  802. }
  803. s->inter_ac_vlc_length=
  804. s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  805. }
  806. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  807. {
  808. if(((unsigned) (diff+255)) >= 511){
  809. int index;
  810. if(diff<0){
  811. index= av_log2_16bit(-2*diff);
  812. diff--;
  813. }else{
  814. index= av_log2_16bit(2*diff);
  815. }
  816. if (component == 0) {
  817. put_bits(
  818. &s->pb,
  819. vlc_dc_lum_bits[index] + index,
  820. (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
  821. }else{
  822. put_bits(
  823. &s->pb,
  824. vlc_dc_chroma_bits[index] + index,
  825. (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
  826. }
  827. }else{
  828. if (component == 0) {
  829. put_bits(
  830. &s->pb,
  831. mpeg1_lum_dc_uni[diff+255]&0xFF,
  832. mpeg1_lum_dc_uni[diff+255]>>8);
  833. } else {
  834. put_bits(
  835. &s->pb,
  836. mpeg1_chr_dc_uni[diff+255]&0xFF,
  837. mpeg1_chr_dc_uni[diff+255]>>8);
  838. }
  839. }
  840. }
  841. static void mpeg1_encode_block(MpegEncContext *s,
  842. DCTELEM *block,
  843. int n)
  844. {
  845. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  846. int code, component;
  847. const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
  848. last_index = s->block_last_index[n];
  849. /* DC coef */
  850. if (s->mb_intra) {
  851. component = (n <= 3 ? 0 : (n&1) + 1);
  852. dc = block[0]; /* overflow is impossible */
  853. diff = dc - s->last_dc[component];
  854. encode_dc(s, diff, component);
  855. s->last_dc[component] = dc;
  856. i = 1;
  857. if (s->intra_vlc_format)
  858. table_vlc = rl_mpeg2.table_vlc;
  859. } else {
  860. /* encode the first coefficient : needs to be done here because
  861. it is handled slightly differently */
  862. level = block[0];
  863. if (abs(level) == 1) {
  864. code = ((uint32_t)level >> 31); /* the sign bit */
  865. put_bits(&s->pb, 2, code | 0x02);
  866. i = 1;
  867. } else {
  868. i = 0;
  869. last_non_zero = -1;
  870. goto next_coef;
  871. }
  872. }
  873. /* now quantify & encode AC coefs */
  874. last_non_zero = i - 1;
  875. for(;i<=last_index;i++) {
  876. j = s->intra_scantable.permutated[i];
  877. level = block[j];
  878. next_coef:
  879. #if 0
  880. if (level != 0)
  881. dprintf(s->avctx, "level[%d]=%d\n", i, level);
  882. #endif
  883. /* encode using VLC */
  884. if (level != 0) {
  885. run = i - last_non_zero - 1;
  886. alevel= level;
  887. MASK_ABS(sign, alevel)
  888. sign&=1;
  889. if (alevel <= mpeg1_max_level[0][run]){
  890. code= mpeg1_index_run[0][run] + alevel - 1;
  891. /* store the vlc & sign at once */
  892. put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
  893. } else {
  894. /* escape seems to be pretty rare <5% so i dont optimize it */
  895. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  896. /* escape: only clip in this case */
  897. put_bits(&s->pb, 6, run);
  898. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  899. if (alevel < 128) {
  900. put_bits(&s->pb, 8, level & 0xff);
  901. } else {
  902. if (level < 0) {
  903. put_bits(&s->pb, 16, 0x8001 + level + 255);
  904. } else {
  905. put_bits(&s->pb, 16, level & 0xffff);
  906. }
  907. }
  908. }else{
  909. put_bits(&s->pb, 12, level & 0xfff);
  910. }
  911. }
  912. last_non_zero = i;
  913. }
  914. }
  915. /* end of block */
  916. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  917. }
  918. #endif //CONFIG_ENCODERS
  919. /******************************************/
  920. /* decoding */
  921. static VLC dc_lum_vlc;
  922. static VLC dc_chroma_vlc;
  923. static VLC mv_vlc;
  924. static VLC mbincr_vlc;
  925. static VLC mb_ptype_vlc;
  926. static VLC mb_btype_vlc;
  927. static VLC mb_pat_vlc;
  928. static void init_vlcs(void)
  929. {
  930. static int done = 0;
  931. if (!done) {
  932. done = 1;
  933. init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
  934. vlc_dc_lum_bits, 1, 1,
  935. vlc_dc_lum_code, 2, 2, 1);
  936. init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
  937. vlc_dc_chroma_bits, 1, 1,
  938. vlc_dc_chroma_code, 2, 2, 1);
  939. init_vlc(&mv_vlc, MV_VLC_BITS, 17,
  940. &mbMotionVectorTable[0][1], 2, 1,
  941. &mbMotionVectorTable[0][0], 2, 1, 1);
  942. init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  943. &mbAddrIncrTable[0][1], 2, 1,
  944. &mbAddrIncrTable[0][0], 2, 1, 1);
  945. init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  946. &mbPatTable[0][1], 2, 1,
  947. &mbPatTable[0][0], 2, 1, 1);
  948. init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  949. &table_mb_ptype[0][1], 2, 1,
  950. &table_mb_ptype[0][0], 2, 1, 1);
  951. init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  952. &table_mb_btype[0][1], 2, 1,
  953. &table_mb_btype[0][0], 2, 1, 1);
  954. init_rl(&rl_mpeg1, static_rl_table_store[0]);
  955. init_rl(&rl_mpeg2, static_rl_table_store[1]);
  956. init_2d_vlc_rl(&rl_mpeg1, 1);
  957. init_2d_vlc_rl(&rl_mpeg2, 1);
  958. }
  959. }
  960. static inline int get_dmv(MpegEncContext *s)
  961. {
  962. if(get_bits1(&s->gb))
  963. return 1 - (get_bits1(&s->gb) << 1);
  964. else
  965. return 0;
  966. }
  967. static inline int get_qscale(MpegEncContext *s)
  968. {
  969. int qscale = get_bits(&s->gb, 5);
  970. if (s->q_scale_type) {
  971. return non_linear_qscale[qscale];
  972. } else {
  973. return qscale << 1;
  974. }
  975. }
  976. /* motion type (for mpeg2) */
  977. #define MT_FIELD 1
  978. #define MT_FRAME 2
  979. #define MT_16X8 2
  980. #define MT_DMV 3
  981. static int mpeg_decode_mb(MpegEncContext *s,
  982. DCTELEM block[12][64])
  983. {
  984. int i, j, k, cbp, val, mb_type, motion_type;
  985. const int mb_block_count = 4 + (1<< s->chroma_format);
  986. dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  987. assert(s->mb_skipped==0);
  988. if (s->mb_skip_run-- != 0) {
  989. if(s->pict_type == I_TYPE){
  990. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  991. return -1;
  992. }
  993. /* skip mb */
  994. s->mb_intra = 0;
  995. for(i=0;i<12;i++)
  996. s->block_last_index[i] = -1;
  997. if(s->picture_structure == PICT_FRAME)
  998. s->mv_type = MV_TYPE_16X16;
  999. else
  1000. s->mv_type = MV_TYPE_FIELD;
  1001. if (s->pict_type == P_TYPE) {
  1002. /* if P type, zero motion vector is implied */
  1003. s->mv_dir = MV_DIR_FORWARD;
  1004. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1005. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1006. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1007. s->field_select[0][0]= s->picture_structure - 1;
  1008. s->mb_skipped = 1;
  1009. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  1010. } else {
  1011. int mb_type;
  1012. if(s->mb_x)
  1013. mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
  1014. else
  1015. 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,
  1016. if(IS_INTRA(mb_type))
  1017. return -1;
  1018. /* if B type, reuse previous vectors and directions */
  1019. s->mv[0][0][0] = s->last_mv[0][0][0];
  1020. s->mv[0][0][1] = s->last_mv[0][0][1];
  1021. s->mv[1][0][0] = s->last_mv[1][0][0];
  1022. s->mv[1][0][1] = s->last_mv[1][0][1];
  1023. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  1024. mb_type | MB_TYPE_SKIP;
  1025. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  1026. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  1027. s->mb_skipped = 1;
  1028. }
  1029. return 0;
  1030. }
  1031. switch(s->pict_type) {
  1032. default:
  1033. case I_TYPE:
  1034. if (get_bits1(&s->gb) == 0) {
  1035. if (get_bits1(&s->gb) == 0){
  1036. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  1037. return -1;
  1038. }
  1039. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  1040. } else {
  1041. mb_type = MB_TYPE_INTRA;
  1042. }
  1043. break;
  1044. case P_TYPE:
  1045. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  1046. if (mb_type < 0){
  1047. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  1048. return -1;
  1049. }
  1050. mb_type = ptype2mb_type[ mb_type ];
  1051. break;
  1052. case B_TYPE:
  1053. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  1054. if (mb_type < 0){
  1055. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  1056. return -1;
  1057. }
  1058. mb_type = btype2mb_type[ mb_type ];
  1059. break;
  1060. }
  1061. dprintf(s->avctx, "mb_type=%x\n", mb_type);
  1062. // motion_type = 0; /* avoid warning */
  1063. if (IS_INTRA(mb_type)) {
  1064. s->dsp.clear_blocks(s->block[0]);
  1065. if(!s->chroma_y_shift){
  1066. s->dsp.clear_blocks(s->block[6]);
  1067. }
  1068. /* compute dct type */
  1069. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1070. !s->frame_pred_frame_dct) {
  1071. s->interlaced_dct = get_bits1(&s->gb);
  1072. }
  1073. if (IS_QUANT(mb_type))
  1074. s->qscale = get_qscale(s);
  1075. if (s->concealment_motion_vectors) {
  1076. /* just parse them */
  1077. if (s->picture_structure != PICT_FRAME)
  1078. skip_bits1(&s->gb); /* field select */
  1079. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  1080. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  1081. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  1082. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  1083. skip_bits1(&s->gb); /* marker */
  1084. }else
  1085. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  1086. s->mb_intra = 1;
  1087. #ifdef HAVE_XVMC
  1088. //one 1 we memcpy blocks in xvmcvideo
  1089. if(s->avctx->xvmc_acceleration > 1){
  1090. XVMC_pack_pblocks(s,-1);//inter are always full blocks
  1091. if(s->swap_uv){
  1092. exchange_uv(s);
  1093. }
  1094. }
  1095. #endif
  1096. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1097. if(s->flags2 & CODEC_FLAG2_FAST){
  1098. for(i=0;i<6;i++) {
  1099. mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
  1100. }
  1101. }else{
  1102. for(i=0;i<mb_block_count;i++) {
  1103. if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
  1104. return -1;
  1105. }
  1106. }
  1107. } else {
  1108. for(i=0;i<6;i++) {
  1109. if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
  1110. return -1;
  1111. }
  1112. }
  1113. } else {
  1114. if (mb_type & MB_TYPE_ZERO_MV){
  1115. assert(mb_type & MB_TYPE_CBP);
  1116. /* compute dct type */
  1117. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1118. !s->frame_pred_frame_dct) {
  1119. s->interlaced_dct = get_bits1(&s->gb);
  1120. }
  1121. if (IS_QUANT(mb_type))
  1122. s->qscale = get_qscale(s);
  1123. s->mv_dir = MV_DIR_FORWARD;
  1124. if(s->picture_structure == PICT_FRAME)
  1125. s->mv_type = MV_TYPE_16X16;
  1126. else{
  1127. s->mv_type = MV_TYPE_FIELD;
  1128. mb_type |= MB_TYPE_INTERLACED;
  1129. s->field_select[0][0]= s->picture_structure - 1;
  1130. }
  1131. s->last_mv[0][0][0] = 0;
  1132. s->last_mv[0][0][1] = 0;
  1133. s->last_mv[0][1][0] = 0;
  1134. s->last_mv[0][1][1] = 0;
  1135. s->mv[0][0][0] = 0;
  1136. s->mv[0][0][1] = 0;
  1137. }else{
  1138. assert(mb_type & MB_TYPE_L0L1);
  1139. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  1140. /* get additional motion vector type */
  1141. if (s->frame_pred_frame_dct)
  1142. motion_type = MT_FRAME;
  1143. else{
  1144. motion_type = get_bits(&s->gb, 2);
  1145. }
  1146. /* compute dct type */
  1147. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1148. !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
  1149. s->interlaced_dct = get_bits1(&s->gb);
  1150. }
  1151. if (IS_QUANT(mb_type))
  1152. s->qscale = get_qscale(s);
  1153. /* motion vectors */
  1154. s->mv_dir = 0;
  1155. for(i=0;i<2;i++) {
  1156. if (USES_LIST(mb_type, i)) {
  1157. s->mv_dir |= (MV_DIR_FORWARD >> i);
  1158. dprintf(s->avctx, "motion_type=%d\n", motion_type);
  1159. switch(motion_type) {
  1160. case MT_FRAME: /* or MT_16X8 */
  1161. if (s->picture_structure == PICT_FRAME) {
  1162. /* MT_FRAME */
  1163. mb_type |= MB_TYPE_16x16;
  1164. s->mv_type = MV_TYPE_16X16;
  1165. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  1166. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  1167. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  1168. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  1169. /* full_pel: only for mpeg1 */
  1170. if (s->full_pel[i]){
  1171. s->mv[i][0][0] <<= 1;
  1172. s->mv[i][0][1] <<= 1;
  1173. }
  1174. } else {
  1175. /* MT_16X8 */
  1176. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1177. s->mv_type = MV_TYPE_16X8;
  1178. for(j=0;j<2;j++) {
  1179. s->field_select[i][j] = get_bits1(&s->gb);
  1180. for(k=0;k<2;k++) {
  1181. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1182. s->last_mv[i][j][k]);
  1183. s->last_mv[i][j][k] = val;
  1184. s->mv[i][j][k] = val;
  1185. }
  1186. }
  1187. }
  1188. break;
  1189. case MT_FIELD:
  1190. s->mv_type = MV_TYPE_FIELD;
  1191. if (s->picture_structure == PICT_FRAME) {
  1192. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1193. for(j=0;j<2;j++) {
  1194. s->field_select[i][j] = get_bits1(&s->gb);
  1195. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1196. s->last_mv[i][j][0]);
  1197. s->last_mv[i][j][0] = val;
  1198. s->mv[i][j][0] = val;
  1199. dprintf(s->avctx, "fmx=%d\n", val);
  1200. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1201. s->last_mv[i][j][1] >> 1);
  1202. s->last_mv[i][j][1] = val << 1;
  1203. s->mv[i][j][1] = val;
  1204. dprintf(s->avctx, "fmy=%d\n", val);
  1205. }
  1206. } else {
  1207. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1208. s->field_select[i][0] = get_bits1(&s->gb);
  1209. for(k=0;k<2;k++) {
  1210. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1211. s->last_mv[i][0][k]);
  1212. s->last_mv[i][0][k] = val;
  1213. s->last_mv[i][1][k] = val;
  1214. s->mv[i][0][k] = val;
  1215. }
  1216. }
  1217. break;
  1218. case MT_DMV:
  1219. {
  1220. int dmx, dmy, mx, my, m;
  1221. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1222. s->last_mv[i][0][0]);
  1223. s->last_mv[i][0][0] = mx;
  1224. s->last_mv[i][1][0] = mx;
  1225. dmx = get_dmv(s);
  1226. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1227. s->last_mv[i][0][1] >> 1);
  1228. dmy = get_dmv(s);
  1229. s->mv_type = MV_TYPE_DMV;
  1230. s->last_mv[i][0][1] = my<<1;
  1231. s->last_mv[i][1][1] = my<<1;
  1232. s->mv[i][0][0] = mx;
  1233. s->mv[i][0][1] = my;
  1234. s->mv[i][1][0] = mx;//not used
  1235. s->mv[i][1][1] = my;//not used
  1236. if (s->picture_structure == PICT_FRAME) {
  1237. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1238. //m = 1 + 2 * s->top_field_first;
  1239. m = s->top_field_first ? 1 : 3;
  1240. /* top -> top pred */
  1241. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1242. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  1243. m = 4 - m;
  1244. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1245. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  1246. } else {
  1247. mb_type |= MB_TYPE_16x16;
  1248. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  1249. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  1250. if(s->picture_structure == PICT_TOP_FIELD)
  1251. s->mv[i][2][1]--;
  1252. else
  1253. s->mv[i][2][1]++;
  1254. }
  1255. }
  1256. break;
  1257. default:
  1258. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  1259. return -1;
  1260. }
  1261. }
  1262. }
  1263. }
  1264. s->mb_intra = 0;
  1265. if (HAS_CBP(mb_type)) {
  1266. s->dsp.clear_blocks(s->block[0]);
  1267. if(!s->chroma_y_shift){
  1268. s->dsp.clear_blocks(s->block[6]);
  1269. }
  1270. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  1271. if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
  1272. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  1273. return -1;
  1274. }
  1275. if(mb_block_count > 6){
  1276. cbp<<= mb_block_count-6;
  1277. cbp |= get_bits(&s->gb, mb_block_count-6);
  1278. }
  1279. #ifdef HAVE_XVMC
  1280. //on 1 we memcpy blocks in xvmcvideo
  1281. if(s->avctx->xvmc_acceleration > 1){
  1282. XVMC_pack_pblocks(s,cbp);
  1283. if(s->swap_uv){
  1284. exchange_uv(s);
  1285. }
  1286. }
  1287. #endif
  1288. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1289. if(s->flags2 & CODEC_FLAG2_FAST){
  1290. for(i=0;i<6;i++) {
  1291. if(cbp & 32) {
  1292. mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
  1293. } else {
  1294. s->block_last_index[i] = -1;
  1295. }
  1296. cbp+=cbp;
  1297. }
  1298. }else{
  1299. cbp<<= 12-mb_block_count;
  1300. for(i=0;i<mb_block_count;i++) {
  1301. if ( cbp & (1<<11) ) {
  1302. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1303. return -1;
  1304. } else {
  1305. s->block_last_index[i] = -1;
  1306. }
  1307. cbp+=cbp;
  1308. }
  1309. }
  1310. } else {
  1311. if(s->flags2 & CODEC_FLAG2_FAST){
  1312. for(i=0;i<6;i++) {
  1313. if (cbp & 32) {
  1314. mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
  1315. } else {
  1316. s->block_last_index[i] = -1;
  1317. }
  1318. cbp+=cbp;
  1319. }
  1320. }else{
  1321. for(i=0;i<6;i++) {
  1322. if (cbp & 32) {
  1323. if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
  1324. return -1;
  1325. } else {
  1326. s->block_last_index[i] = -1;
  1327. }
  1328. cbp+=cbp;
  1329. }
  1330. }
  1331. }
  1332. }else{
  1333. for(i=0;i<12;i++)
  1334. s->block_last_index[i] = -1;
  1335. }
  1336. }
  1337. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  1338. return 0;
  1339. }
  1340. /* as h263, but only 17 codes */
  1341. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  1342. {
  1343. int code, sign, val, l, shift;
  1344. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  1345. if (code == 0) {
  1346. return pred;
  1347. }
  1348. if (code < 0) {
  1349. return 0xffff;
  1350. }
  1351. sign = get_bits1(&s->gb);
  1352. shift = fcode - 1;
  1353. val = code;
  1354. if (shift) {
  1355. val = (val - 1) << shift;
  1356. val |= get_bits(&s->gb, shift);
  1357. val++;
  1358. }
  1359. if (sign)
  1360. val = -val;
  1361. val += pred;
  1362. /* modulo decoding */
  1363. l= INT_BIT - 5 - shift;
  1364. val = (val<<l)>>l;
  1365. return val;
  1366. }
  1367. static inline int decode_dc(GetBitContext *gb, int component)
  1368. {
  1369. int code, diff;
  1370. if (component == 0) {
  1371. code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  1372. } else {
  1373. code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  1374. }
  1375. if (code < 0){
  1376. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  1377. return 0xffff;
  1378. }
  1379. if (code == 0) {
  1380. diff = 0;
  1381. } else {
  1382. diff = get_xbits(gb, code);
  1383. }
  1384. return diff;
  1385. }
  1386. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  1387. DCTELEM *block,
  1388. int n)
  1389. {
  1390. int level, dc, diff, i, j, run;
  1391. int component;
  1392. RLTable *rl = &rl_mpeg1;
  1393. uint8_t * const scantable= s->intra_scantable.permutated;
  1394. const uint16_t *quant_matrix= s->intra_matrix;
  1395. const int qscale= s->qscale;
  1396. /* DC coef */
  1397. component = (n <= 3 ? 0 : n - 4 + 1);
  1398. diff = decode_dc(&s->gb, component);
  1399. if (diff >= 0xffff)
  1400. return -1;
  1401. dc = s->last_dc[component];
  1402. dc += diff;
  1403. s->last_dc[component] = dc;
  1404. block[0] = dc<<3;
  1405. dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
  1406. i = 0;
  1407. {
  1408. OPEN_READER(re, &s->gb);
  1409. /* now quantify & encode AC coefs */
  1410. for(;;) {
  1411. UPDATE_CACHE(re, &s->gb);
  1412. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1413. if(level == 127){
  1414. break;
  1415. } else if(level != 0) {
  1416. i += run;
  1417. j = scantable[i];
  1418. level= (level*qscale*quant_matrix[j])>>4;
  1419. level= (level-1)|1;
  1420. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1421. LAST_SKIP_BITS(re, &s->gb, 1);
  1422. } else {
  1423. /* escape */
  1424. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1425. UPDATE_CACHE(re, &s->gb);
  1426. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1427. if (level == -128) {
  1428. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1429. } else if (level == 0) {
  1430. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1431. }
  1432. i += run;
  1433. j = scantable[i];
  1434. if(level<0){
  1435. level= -level;
  1436. level= (level*qscale*quant_matrix[j])>>4;
  1437. level= (level-1)|1;
  1438. level= -level;
  1439. }else{
  1440. level= (level*qscale*quant_matrix[j])>>4;
  1441. level= (level-1)|1;
  1442. }
  1443. }
  1444. if (i > 63){
  1445. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1446. return -1;
  1447. }
  1448. block[j] = level;
  1449. }
  1450. CLOSE_READER(re, &s->gb);
  1451. }
  1452. s->block_last_index[n] = i;
  1453. return 0;
  1454. }
  1455. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1456. DCTELEM *block,
  1457. int n)
  1458. {
  1459. int level, i, j, run;
  1460. RLTable *rl = &rl_mpeg1;
  1461. uint8_t * const scantable= s->intra_scantable.permutated;
  1462. const uint16_t *quant_matrix= s->inter_matrix;
  1463. const int qscale= s->qscale;
  1464. {
  1465. OPEN_READER(re, &s->gb);
  1466. i = -1;
  1467. /* special case for the first coef. no need to add a second vlc table */
  1468. UPDATE_CACHE(re, &s->gb);
  1469. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1470. level= (3*qscale*quant_matrix[0])>>5;
  1471. level= (level-1)|1;
  1472. if(GET_CACHE(re, &s->gb)&0x40000000)
  1473. level= -level;
  1474. block[0] = level;
  1475. i++;
  1476. SKIP_BITS(re, &s->gb, 2);
  1477. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1478. goto end;
  1479. }
  1480. /* now quantify & encode AC coefs */
  1481. for(;;) {
  1482. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1483. if(level != 0) {
  1484. i += run;
  1485. j = scantable[i];
  1486. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1487. level= (level-1)|1;
  1488. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1489. SKIP_BITS(re, &s->gb, 1);
  1490. } else {
  1491. /* escape */
  1492. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1493. UPDATE_CACHE(re, &s->gb);
  1494. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1495. if (level == -128) {
  1496. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  1497. } else if (level == 0) {
  1498. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  1499. }
  1500. i += run;
  1501. j = scantable[i];
  1502. if(level<0){
  1503. level= -level;
  1504. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1505. level= (level-1)|1;
  1506. level= -level;
  1507. }else{
  1508. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1509. level= (level-1)|1;
  1510. }
  1511. }
  1512. if (i > 63){
  1513. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1514. return -1;
  1515. }
  1516. block[j] = level;
  1517. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1518. break;
  1519. UPDATE_CACHE(re, &s->gb);
  1520. }
  1521. end:
  1522. LAST_SKIP_BITS(re, &s->gb, 2);
  1523. CLOSE_READER(re, &s->gb);
  1524. }
  1525. s->block_last_index[n] = i;
  1526. return 0;
  1527. }
  1528. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  1529. {
  1530. int level, i, j, run;
  1531. RLTable *rl = &rl_mpeg1;
  1532. uint8_t * const scantable= s->intra_scantable.permutated;
  1533. const int qscale= s->qscale;
  1534. {
  1535. OPEN_READER(re, &s->gb);
  1536. i = -1;
  1537. /* special case for the first coef. no need to add a second vlc table */
  1538. UPDATE_CACHE(re, &s->gb);
  1539. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1540. level= (3*qscale)>>1;
  1541. level= (level-1)|1;
  1542. if(GET_CACHE(re, &s->gb)&0x40000000)
  1543. level= -level;
  1544. block[0] = level;
  1545. i++;
  1546. SKIP_BITS(re, &s->gb, 2);
  1547. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1548. goto end;
  1549. }
  1550. /* now quantify & encode AC coefs */
  1551. for(;;) {
  1552. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1553. if(level != 0) {
  1554. i += run;
  1555. j = scantable[i];
  1556. level= ((level*2+1)*qscale)>>1;
  1557. level= (level-1)|1;
  1558. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1559. SKIP_BITS(re, &s->gb, 1);
  1560. } else {
  1561. /* escape */
  1562. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1563. UPDATE_CACHE(re, &s->gb);
  1564. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1565. if (level == -128) {
  1566. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  1567. } else if (level == 0) {
  1568. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  1569. }
  1570. i += run;
  1571. j = scantable[i];
  1572. if(level<0){
  1573. level= -level;
  1574. level= ((level*2+1)*qscale)>>1;
  1575. level= (level-1)|1;
  1576. level= -level;
  1577. }else{
  1578. level= ((level*2+1)*qscale)>>1;
  1579. level= (level-1)|1;
  1580. }
  1581. }
  1582. block[j] = level;
  1583. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1584. break;
  1585. UPDATE_CACHE(re, &s->gb);
  1586. }
  1587. end:
  1588. LAST_SKIP_BITS(re, &s->gb, 2);
  1589. CLOSE_READER(re, &s->gb);
  1590. }
  1591. s->block_last_index[n] = i;
  1592. return 0;
  1593. }
  1594. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1595. DCTELEM *block,
  1596. int n)
  1597. {
  1598. int level, i, j, run;
  1599. RLTable *rl = &rl_mpeg1;
  1600. uint8_t * const scantable= s->intra_scantable.permutated;
  1601. const uint16_t *quant_matrix;
  1602. const int qscale= s->qscale;
  1603. int mismatch;
  1604. mismatch = 1;
  1605. {
  1606. OPEN_READER(re, &s->gb);
  1607. i = -1;
  1608. if (n < 4)
  1609. quant_matrix = s->inter_matrix;
  1610. else
  1611. quant_matrix = s->chroma_inter_matrix;
  1612. /* special case for the first coef. no need to add a second vlc table */
  1613. UPDATE_CACHE(re, &s->gb);
  1614. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1615. level= (3*qscale*quant_matrix[0])>>5;
  1616. if(GET_CACHE(re, &s->gb)&0x40000000)
  1617. level= -level;
  1618. block[0] = level;
  1619. mismatch ^= level;
  1620. i++;
  1621. SKIP_BITS(re, &s->gb, 2);
  1622. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1623. goto end;
  1624. }
  1625. /* now quantify & encode AC coefs */
  1626. for(;;) {
  1627. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1628. if(level != 0) {
  1629. i += run;
  1630. j = scantable[i];
  1631. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1632. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1633. SKIP_BITS(re, &s->gb, 1);
  1634. } else {
  1635. /* escape */
  1636. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1637. UPDATE_CACHE(re, &s->gb);
  1638. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1639. i += run;
  1640. j = scantable[i];
  1641. if(level<0){
  1642. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1643. level= -level;
  1644. }else{
  1645. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1646. }
  1647. }
  1648. if (i > 63){
  1649. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1650. return -1;
  1651. }
  1652. mismatch ^= level;
  1653. block[j] = level;
  1654. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1655. break;
  1656. UPDATE_CACHE(re, &s->gb);
  1657. }
  1658. end:
  1659. LAST_SKIP_BITS(re, &s->gb, 2);
  1660. CLOSE_READER(re, &s->gb);
  1661. }
  1662. block[63] ^= (mismatch & 1);
  1663. s->block_last_index[n] = i;
  1664. return 0;
  1665. }
  1666. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  1667. DCTELEM *block,
  1668. int n)
  1669. {
  1670. int level, i, j, run;
  1671. RLTable *rl = &rl_mpeg1;
  1672. uint8_t * const scantable= s->intra_scantable.permutated;
  1673. const int qscale= s->qscale;
  1674. OPEN_READER(re, &s->gb);
  1675. i = -1;
  1676. /* special case for the first coef. no need to add a second vlc table */
  1677. UPDATE_CACHE(re, &s->gb);
  1678. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1679. level= (3*qscale)>>1;
  1680. if(GET_CACHE(re, &s->gb)&0x40000000)
  1681. level= -level;
  1682. block[0] = level;
  1683. i++;
  1684. SKIP_BITS(re, &s->gb, 2);
  1685. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1686. goto end;
  1687. }
  1688. /* now quantify & encode AC coefs */
  1689. for(;;) {
  1690. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1691. if(level != 0) {
  1692. i += run;
  1693. j = scantable[i];
  1694. level= ((level*2+1)*qscale)>>1;
  1695. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1696. SKIP_BITS(re, &s->gb, 1);
  1697. } else {
  1698. /* escape */
  1699. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1700. UPDATE_CACHE(re, &s->gb);
  1701. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1702. i += run;
  1703. j = scantable[i];
  1704. if(level<0){
  1705. level= ((-level*2+1)*qscale)>>1;
  1706. level= -level;
  1707. }else{
  1708. level= ((level*2+1)*qscale)>>1;
  1709. }
  1710. }
  1711. block[j] = level;
  1712. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1713. break;
  1714. UPDATE_CACHE(re, &s->gb);
  1715. }
  1716. end:
  1717. LAST_SKIP_BITS(re, &s->gb, 2);
  1718. CLOSE_READER(re, &s->gb);
  1719. s->block_last_index[n] = i;
  1720. return 0;
  1721. }
  1722. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1723. DCTELEM *block,
  1724. int n)
  1725. {
  1726. int level, dc, diff, i, j, run;
  1727. int component;
  1728. RLTable *rl;
  1729. uint8_t * const scantable= s->intra_scantable.permutated;
  1730. const uint16_t *quant_matrix;
  1731. const int qscale= s->qscale;
  1732. int mismatch;
  1733. /* DC coef */
  1734. if (n < 4){
  1735. quant_matrix = s->intra_matrix;
  1736. component = 0;
  1737. }else{
  1738. quant_matrix = s->chroma_intra_matrix;
  1739. component = (n&1) + 1;
  1740. }
  1741. diff = decode_dc(&s->gb, component);
  1742. if (diff >= 0xffff)
  1743. return -1;
  1744. dc = s->last_dc[component];
  1745. dc += diff;
  1746. s->last_dc[component] = dc;
  1747. block[0] = dc << (3 - s->intra_dc_precision);
  1748. dprintf(s->avctx, "dc=%d\n", block[0]);
  1749. mismatch = block[0] ^ 1;
  1750. i = 0;
  1751. if (s->intra_vlc_format)
  1752. rl = &rl_mpeg2;
  1753. else
  1754. rl = &rl_mpeg1;
  1755. {
  1756. OPEN_READER(re, &s->gb);
  1757. /* now quantify & encode AC coefs */
  1758. for(;;) {
  1759. UPDATE_CACHE(re, &s->gb);
  1760. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1761. if(level == 127){
  1762. break;
  1763. } else if(level != 0) {
  1764. i += run;
  1765. j = scantable[i];
  1766. level= (level*qscale*quant_matrix[j])>>4;
  1767. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1768. LAST_SKIP_BITS(re, &s->gb, 1);
  1769. } else {
  1770. /* escape */
  1771. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1772. UPDATE_CACHE(re, &s->gb);
  1773. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1774. i += run;
  1775. j = scantable[i];
  1776. if(level<0){
  1777. level= (-level*qscale*quant_matrix[j])>>4;
  1778. level= -level;
  1779. }else{
  1780. level= (level*qscale*quant_matrix[j])>>4;
  1781. }
  1782. }
  1783. if (i > 63){
  1784. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1785. return -1;
  1786. }
  1787. mismatch^= level;
  1788. block[j] = level;
  1789. }
  1790. CLOSE_READER(re, &s->gb);
  1791. }
  1792. block[63]^= mismatch&1;
  1793. s->block_last_index[n] = i;
  1794. return 0;
  1795. }
  1796. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
  1797. DCTELEM *block,
  1798. int n)
  1799. {
  1800. int level, dc, diff, j, run;
  1801. int component;
  1802. RLTable *rl;
  1803. uint8_t * scantable= s->intra_scantable.permutated;
  1804. const uint16_t *quant_matrix;
  1805. const int qscale= s->qscale;
  1806. /* DC coef */
  1807. if (n < 4){
  1808. quant_matrix = s->intra_matrix;
  1809. component = 0;
  1810. }else{
  1811. quant_matrix = s->chroma_intra_matrix;
  1812. component = (n&1) + 1;
  1813. }
  1814. diff = decode_dc(&s->gb, component);
  1815. if (diff >= 0xffff)
  1816. return -1;
  1817. dc = s->last_dc[component];
  1818. dc += diff;
  1819. s->last_dc[component] = dc;
  1820. block[0] = dc << (3 - s->intra_dc_precision);
  1821. if (s->intra_vlc_format)
  1822. rl = &rl_mpeg2;
  1823. else
  1824. rl = &rl_mpeg1;
  1825. {
  1826. OPEN_READER(re, &s->gb);
  1827. /* now quantify & encode AC coefs */
  1828. for(;;) {
  1829. UPDATE_CACHE(re, &s->gb);
  1830. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1831. if(level == 127){
  1832. break;
  1833. } else if(level != 0) {
  1834. scantable += run;
  1835. j = *scantable;
  1836. level= (level*qscale*quant_matrix[j])>>4;
  1837. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1838. LAST_SKIP_BITS(re, &s->gb, 1);
  1839. } else {
  1840. /* escape */
  1841. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1842. UPDATE_CACHE(re, &s->gb);
  1843. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1844. scantable += run;
  1845. j = *scantable;
  1846. if(level<0){
  1847. level= (-level*qscale*quant_matrix[j])>>4;
  1848. level= -level;
  1849. }else{
  1850. level= (level*qscale*quant_matrix[j])>>4;
  1851. }
  1852. }
  1853. block[j] = level;
  1854. }
  1855. CLOSE_READER(re, &s->gb);
  1856. }
  1857. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  1858. return 0;
  1859. }
  1860. typedef struct Mpeg1Context {
  1861. MpegEncContext mpeg_enc_ctx;
  1862. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1863. int repeat_field; /* true if we must repeat the field */
  1864. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1865. int slice_count;
  1866. int swap_uv;//indicate VCR2
  1867. int save_aspect_info;
  1868. AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
  1869. } Mpeg1Context;
  1870. static int mpeg_decode_init(AVCodecContext *avctx)
  1871. {
  1872. Mpeg1Context *s = avctx->priv_data;
  1873. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1874. int i;
  1875. //we need some parmutation to store
  1876. //matrixes, until MPV_common_init()
  1877. //set the real permutatuon
  1878. for(i=0;i<64;i++)
  1879. s2->dsp.idct_permutation[i]=i;
  1880. MPV_decode_defaults(s2);
  1881. s->mpeg_enc_ctx.avctx= avctx;
  1882. s->mpeg_enc_ctx.flags= avctx->flags;
  1883. s->mpeg_enc_ctx.flags2= avctx->flags2;
  1884. common_init(&s->mpeg_enc_ctx);
  1885. init_vlcs();
  1886. s->mpeg_enc_ctx_allocated = 0;
  1887. s->mpeg_enc_ctx.picture_number = 0;
  1888. s->repeat_field = 0;
  1889. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1890. return 0;
  1891. }
  1892. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1893. const uint8_t *new_perm){
  1894. uint16_t temp_matrix[64];
  1895. int i;
  1896. memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
  1897. for(i=0;i<64;i++){
  1898. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1899. }
  1900. }
  1901. //Call this function when we know all parameters
  1902. //it may be called in different places for mpeg1 and mpeg2
  1903. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1904. Mpeg1Context *s1 = avctx->priv_data;
  1905. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1906. uint8_t old_permutation[64];
  1907. if (
  1908. (s1->mpeg_enc_ctx_allocated == 0)||
  1909. avctx->coded_width != s->width ||
  1910. avctx->coded_height != s->height||
  1911. s1->save_aspect_info != s->aspect_ratio_info||
  1912. 0)
  1913. {
  1914. if (s1->mpeg_enc_ctx_allocated) {
  1915. ParseContext pc= s->parse_context;
  1916. s->parse_context.buffer=0;
  1917. MPV_common_end(s);
  1918. s->parse_context= pc;
  1919. }
  1920. if( (s->width == 0 )||(s->height == 0))
  1921. return -2;
  1922. avcodec_set_dimensions(avctx, s->width, s->height);
  1923. avctx->bit_rate = s->bit_rate;
  1924. s1->save_aspect_info = s->aspect_ratio_info;
  1925. //low_delay may be forced, in this case we will have B frames
  1926. //that behave like P frames
  1927. avctx->has_b_frames = !(s->low_delay);
  1928. if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
  1929. //mpeg1 fps
  1930. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1931. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1932. //mpeg1 aspect
  1933. avctx->sample_aspect_ratio= av_d2q(
  1934. 1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
  1935. }else{//mpeg2
  1936. //mpeg2 fps
  1937. av_reduce(
  1938. &s->avctx->time_base.den,
  1939. &s->avctx->time_base.num,
  1940. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1941. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1942. 1<<30);
  1943. //mpeg2 aspect
  1944. if(s->aspect_ratio_info > 1){
  1945. if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
  1946. s->avctx->sample_aspect_ratio=
  1947. av_div_q(
  1948. mpeg2_aspect[s->aspect_ratio_info],
  1949. (AVRational){s->width, s->height}
  1950. );
  1951. }else{
  1952. s->avctx->sample_aspect_ratio=
  1953. av_div_q(
  1954. mpeg2_aspect[s->aspect_ratio_info],
  1955. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1956. );
  1957. }
  1958. }else{
  1959. s->avctx->sample_aspect_ratio=
  1960. mpeg2_aspect[s->aspect_ratio_info];
  1961. }
  1962. }//mpeg2
  1963. if(avctx->xvmc_acceleration){
  1964. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1965. }else{
  1966. if(s->chroma_format < 2){
  1967. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  1968. }else
  1969. if(s->chroma_format == 2){
  1970. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
  1971. }else
  1972. if(s->chroma_format > 2){
  1973. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
  1974. }
  1975. }
  1976. //until then pix_fmt may be changed right after codec init
  1977. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1978. if( avctx->idct_algo == FF_IDCT_AUTO )
  1979. avctx->idct_algo = FF_IDCT_SIMPLE;
  1980. //quantization matrixes may need reordering
  1981. //if dct permutation is changed
  1982. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1983. if (MPV_common_init(s) < 0)
  1984. return -2;
  1985. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1986. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1987. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1988. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1989. s1->mpeg_enc_ctx_allocated = 1;
  1990. }
  1991. return 0;
  1992. }
  1993. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1994. const uint8_t *buf, int buf_size)
  1995. {
  1996. Mpeg1Context *s1 = avctx->priv_data;
  1997. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1998. int ref, f_code, vbv_delay;
  1999. if(mpeg_decode_postinit(s->avctx) < 0)
  2000. return -2;
  2001. init_get_bits(&s->gb, buf, buf_size*8);
  2002. ref = get_bits(&s->gb, 10); /* temporal ref */
  2003. s->pict_type = get_bits(&s->gb, 3);
  2004. if(s->pict_type == 0 || s->pict_type > 3)
  2005. return -1;
  2006. vbv_delay= get_bits(&s->gb, 16);
  2007. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  2008. s->full_pel[0] = get_bits1(&s->gb);
  2009. f_code = get_bits(&s->gb, 3);
  2010. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  2011. return -1;
  2012. s->mpeg_f_code[0][0] = f_code;
  2013. s->mpeg_f_code[0][1] = f_code;
  2014. }
  2015. if (s->pict_type == B_TYPE) {
  2016. s->full_pel[1] = get_bits1(&s->gb);
  2017. f_code = get_bits(&s->gb, 3);
  2018. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  2019. return -1;
  2020. s->mpeg_f_code[1][0] = f_code;
  2021. s->mpeg_f_code[1][1] = f_code;
  2022. }
  2023. s->current_picture.pict_type= s->pict_type;
  2024. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2025. if(avctx->debug & FF_DEBUG_PICT_INFO)
  2026. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  2027. s->y_dc_scale = 8;
  2028. s->c_dc_scale = 8;
  2029. s->first_slice = 1;
  2030. return 0;
  2031. }
  2032. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  2033. {
  2034. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2035. int horiz_size_ext, vert_size_ext;
  2036. int bit_rate_ext;
  2037. skip_bits(&s->gb, 1); /* profil and level esc*/
  2038. s->avctx->profile= get_bits(&s->gb, 3);
  2039. s->avctx->level= get_bits(&s->gb, 4);
  2040. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  2041. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  2042. horiz_size_ext = get_bits(&s->gb, 2);
  2043. vert_size_ext = get_bits(&s->gb, 2);
  2044. s->width |= (horiz_size_ext << 12);
  2045. s->height |= (vert_size_ext << 12);
  2046. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  2047. s->bit_rate += (bit_rate_ext << 18) * 400;
  2048. skip_bits1(&s->gb); /* marker */
  2049. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  2050. s->low_delay = get_bits1(&s->gb);
  2051. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2052. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  2053. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  2054. dprintf(s->avctx, "sequence extension\n");
  2055. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2056. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  2057. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2058. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  2059. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  2060. }
  2061. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  2062. {
  2063. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2064. int color_description, w, h;
  2065. skip_bits(&s->gb, 3); /* video format */
  2066. color_description= get_bits1(&s->gb);
  2067. if(color_description){
  2068. skip_bits(&s->gb, 8); /* color primaries */
  2069. skip_bits(&s->gb, 8); /* transfer_characteristics */
  2070. skip_bits(&s->gb, 8); /* matrix_coefficients */
  2071. }
  2072. w= get_bits(&s->gb, 14);
  2073. skip_bits(&s->gb, 1); //marker
  2074. h= get_bits(&s->gb, 14);
  2075. skip_bits(&s->gb, 1); //marker
  2076. s1->pan_scan.width= 16*w;
  2077. s1->pan_scan.height=16*h;
  2078. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2079. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  2080. }
  2081. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  2082. {
  2083. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2084. int i,nofco;
  2085. nofco = 1;
  2086. if(s->progressive_sequence){
  2087. if(s->repeat_first_field){
  2088. nofco++;
  2089. if(s->top_field_first)
  2090. nofco++;
  2091. }
  2092. }else{
  2093. if(s->picture_structure == PICT_FRAME){
  2094. nofco++;
  2095. if(s->repeat_first_field)
  2096. nofco++;
  2097. }
  2098. }
  2099. for(i=0; i<nofco; i++){
  2100. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  2101. skip_bits(&s->gb, 1); //marker
  2102. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  2103. skip_bits(&s->gb, 1); //marker
  2104. }
  2105. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2106. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  2107. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  2108. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  2109. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  2110. );
  2111. }
  2112. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  2113. {
  2114. int i, v, j;
  2115. dprintf(s->avctx, "matrix extension\n");
  2116. if (get_bits1(&s->gb)) {
  2117. for(i=0;i<64;i++) {
  2118. v = get_bits(&s->gb, 8);
  2119. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2120. s->intra_matrix[j] = v;
  2121. s->chroma_intra_matrix[j] = v;
  2122. }
  2123. }
  2124. if (get_bits1(&s->gb)) {
  2125. for(i=0;i<64;i++) {
  2126. v = get_bits(&s->gb, 8);
  2127. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2128. s->inter_matrix[j] = v;
  2129. s->chroma_inter_matrix[j] = v;
  2130. }
  2131. }
  2132. if (get_bits1(&s->gb)) {
  2133. for(i=0;i<64;i++) {
  2134. v = get_bits(&s->gb, 8);
  2135. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2136. s->chroma_intra_matrix[j] = v;
  2137. }
  2138. }
  2139. if (get_bits1(&s->gb)) {
  2140. for(i=0;i<64;i++) {
  2141. v = get_bits(&s->gb, 8);
  2142. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2143. s->chroma_inter_matrix[j] = v;
  2144. }
  2145. }
  2146. }
  2147. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  2148. {
  2149. s->full_pel[0] = s->full_pel[1] = 0;
  2150. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  2151. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  2152. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  2153. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  2154. s->intra_dc_precision = get_bits(&s->gb, 2);
  2155. s->picture_structure = get_bits(&s->gb, 2);
  2156. s->top_field_first = get_bits1(&s->gb);
  2157. s->frame_pred_frame_dct = get_bits1(&s->gb);
  2158. s->concealment_motion_vectors = get_bits1(&s->gb);
  2159. s->q_scale_type = get_bits1(&s->gb);
  2160. s->intra_vlc_format = get_bits1(&s->gb);
  2161. s->alternate_scan = get_bits1(&s->gb);
  2162. s->repeat_first_field = get_bits1(&s->gb);
  2163. s->chroma_420_type = get_bits1(&s->gb);
  2164. s->progressive_frame = get_bits1(&s->gb);
  2165. if(s->picture_structure == PICT_FRAME){
  2166. s->first_field=0;
  2167. s->v_edge_pos= 16*s->mb_height;
  2168. }else{
  2169. s->first_field ^= 1;
  2170. s->v_edge_pos= 8*s->mb_height;
  2171. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  2172. }
  2173. if(s->alternate_scan){
  2174. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  2175. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  2176. }else{
  2177. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  2178. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  2179. }
  2180. /* composite display not parsed */
  2181. dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  2182. dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
  2183. dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
  2184. dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  2185. dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  2186. dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  2187. dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  2188. dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  2189. dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  2190. }
  2191. static void mpeg_decode_extension(AVCodecContext *avctx,
  2192. const uint8_t *buf, int buf_size)
  2193. {
  2194. Mpeg1Context *s1 = avctx->priv_data;
  2195. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2196. int ext_type;
  2197. init_get_bits(&s->gb, buf, buf_size*8);
  2198. ext_type = get_bits(&s->gb, 4);
  2199. switch(ext_type) {
  2200. case 0x1:
  2201. mpeg_decode_sequence_extension(s1);
  2202. break;
  2203. case 0x2:
  2204. mpeg_decode_sequence_display_extension(s1);
  2205. break;
  2206. case 0x3:
  2207. mpeg_decode_quant_matrix_extension(s);
  2208. break;
  2209. case 0x7:
  2210. mpeg_decode_picture_display_extension(s1);
  2211. break;
  2212. case 0x8:
  2213. mpeg_decode_picture_coding_extension(s);
  2214. break;
  2215. }
  2216. }
  2217. static void exchange_uv(MpegEncContext *s){
  2218. short * tmp = s->pblocks[4];
  2219. s->pblocks[4] = s->pblocks[5];
  2220. s->pblocks[5] = tmp;
  2221. }
  2222. static int mpeg_field_start(MpegEncContext *s){
  2223. AVCodecContext *avctx= s->avctx;
  2224. Mpeg1Context *s1 = (Mpeg1Context*)s;
  2225. /* start frame decoding */
  2226. if(s->first_field || s->picture_structure==PICT_FRAME){
  2227. if(MPV_frame_start(s, avctx) < 0)
  2228. return -1;
  2229. ff_er_frame_start(s);
  2230. /* first check if we must repeat the frame */
  2231. s->current_picture_ptr->repeat_pict = 0;
  2232. if (s->repeat_first_field) {
  2233. if (s->progressive_sequence) {
  2234. if (s->top_field_first)
  2235. s->current_picture_ptr->repeat_pict = 4;
  2236. else
  2237. s->current_picture_ptr->repeat_pict = 2;
  2238. } else if (s->progressive_frame) {
  2239. s->current_picture_ptr->repeat_pict = 1;
  2240. }
  2241. }
  2242. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  2243. }else{ //second field
  2244. int i;
  2245. if(!s->current_picture_ptr){
  2246. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  2247. return -1;
  2248. }
  2249. for(i=0; i<4; i++){
  2250. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  2251. if(s->picture_structure == PICT_BOTTOM_FIELD){
  2252. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  2253. }
  2254. }
  2255. }
  2256. #ifdef HAVE_XVMC
  2257. // MPV_frame_start will call this function too,
  2258. // but we need to call it on every field
  2259. if(s->avctx->xvmc_acceleration)
  2260. XVMC_field_start(s,avctx);
  2261. #endif
  2262. return 0;
  2263. }
  2264. #define DECODE_SLICE_ERROR -1
  2265. #define DECODE_SLICE_OK 0
  2266. /**
  2267. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  2268. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  2269. * DECODE_SLICE_OK if this slice is ok<br>
  2270. */
  2271. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  2272. const uint8_t **buf, int buf_size)
  2273. {
  2274. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2275. AVCodecContext *avctx= s->avctx;
  2276. int ret;
  2277. const int field_pic= s->picture_structure != PICT_FRAME;
  2278. const int lowres= s->avctx->lowres;
  2279. s->resync_mb_x=
  2280. s->resync_mb_y= -1;
  2281. if (mb_y<<field_pic >= s->mb_height){
  2282. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
  2283. return -1;
  2284. }
  2285. init_get_bits(&s->gb, *buf, buf_size*8);
  2286. ff_mpeg1_clean_buffers(s);
  2287. s->interlaced_dct = 0;
  2288. s->qscale = get_qscale(s);
  2289. if(s->qscale == 0){
  2290. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  2291. return -1;
  2292. }
  2293. /* extra slice info */
  2294. while (get_bits1(&s->gb) != 0) {
  2295. skip_bits(&s->gb, 8);
  2296. }
  2297. s->mb_x=0;
  2298. for(;;) {
  2299. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2300. if (code < 0){
  2301. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  2302. return -1;
  2303. }
  2304. if (code >= 33) {
  2305. if (code == 33) {
  2306. s->mb_x += 33;
  2307. }
  2308. /* otherwise, stuffing, nothing to do */
  2309. } else {
  2310. s->mb_x += code;
  2311. break;
  2312. }
  2313. }
  2314. if(s->mb_x >= (unsigned)s->mb_width){
  2315. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  2316. return -1;
  2317. }
  2318. s->resync_mb_x= s->mb_x;
  2319. s->resync_mb_y= s->mb_y= mb_y;
  2320. s->mb_skip_run= 0;
  2321. ff_init_block_index(s);
  2322. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  2323. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2324. 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",
  2325. 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],
  2326. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  2327. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  2328. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  2329. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  2330. }
  2331. }
  2332. for(;;) {
  2333. #ifdef HAVE_XVMC
  2334. //one 1 we memcpy blocks in xvmcvideo
  2335. if(s->avctx->xvmc_acceleration > 1)
  2336. XVMC_init_block(s);//set s->block
  2337. #endif
  2338. ret = mpeg_decode_mb(s, s->block);
  2339. s->chroma_qscale= s->qscale;
  2340. dprintf(s->avctx, "ret=%d\n", ret);
  2341. if (ret < 0)
  2342. return -1;
  2343. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  2344. const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
  2345. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  2346. int motion_x, motion_y, dir, i;
  2347. if(field_pic && !s->first_field)
  2348. xy += wrap/2;
  2349. for(i=0; i<2; i++){
  2350. for(dir=0; dir<2; dir++){
  2351. if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
  2352. motion_x = motion_y = 0;
  2353. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  2354. motion_x = s->mv[dir][0][0];
  2355. motion_y = s->mv[dir][0][1];
  2356. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  2357. motion_x = s->mv[dir][i][0];
  2358. motion_y = s->mv[dir][i][1];
  2359. }
  2360. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  2361. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  2362. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  2363. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  2364. s->current_picture.ref_index [dir][xy ]=
  2365. s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
  2366. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  2367. }
  2368. xy += wrap;
  2369. }
  2370. }
  2371. s->dest[0] += 16 >> lowres;
  2372. s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
  2373. s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
  2374. MPV_decode_mb(s, s->block);
  2375. if (++s->mb_x >= s->mb_width) {
  2376. const int mb_size= 16>>s->avctx->lowres;
  2377. ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
  2378. s->mb_x = 0;
  2379. s->mb_y++;
  2380. if(s->mb_y<<field_pic >= s->mb_height){
  2381. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  2382. int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
  2383. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  2384. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  2385. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  2386. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  2387. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  2388. return -1;
  2389. }else
  2390. goto eos;
  2391. }
  2392. ff_init_block_index(s);
  2393. }
  2394. /* skip mb handling */
  2395. if (s->mb_skip_run == -1) {
  2396. /* read again increment */
  2397. s->mb_skip_run = 0;
  2398. for(;;) {
  2399. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2400. if (code < 0){
  2401. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  2402. return -1;
  2403. }
  2404. if (code >= 33) {
  2405. if (code == 33) {
  2406. s->mb_skip_run += 33;
  2407. }else if(code == 35){
  2408. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  2409. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  2410. return -1;
  2411. }
  2412. goto eos; /* end of slice */
  2413. }
  2414. /* otherwise, stuffing, nothing to do */
  2415. } else {
  2416. s->mb_skip_run += code;
  2417. break;
  2418. }
  2419. }
  2420. }
  2421. }
  2422. eos: // end of slice
  2423. *buf += get_bits_count(&s->gb)/8 - 1;
  2424. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  2425. return 0;
  2426. }
  2427. static int slice_decode_thread(AVCodecContext *c, void *arg){
  2428. MpegEncContext *s= arg;
  2429. const uint8_t *buf= s->gb.buffer;
  2430. int mb_y= s->start_mb_y;
  2431. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  2432. for(;;){
  2433. uint32_t start_code;
  2434. int ret;
  2435. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  2436. emms_c();
  2437. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  2438. //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);
  2439. if(ret < 0){
  2440. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  2441. 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);
  2442. }else{
  2443. 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);
  2444. }
  2445. if(s->mb_y == s->end_mb_y)
  2446. return 0;
  2447. start_code= -1;
  2448. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  2449. mb_y= start_code - SLICE_MIN_START_CODE;
  2450. if(mb_y < 0 || mb_y >= s->end_mb_y)
  2451. return -1;
  2452. }
  2453. return 0; //not reached
  2454. }
  2455. /**
  2456. * handles slice ends.
  2457. * @return 1 if it seems to be the last slice of
  2458. */
  2459. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  2460. {
  2461. Mpeg1Context *s1 = avctx->priv_data;
  2462. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2463. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  2464. return 0;
  2465. #ifdef HAVE_XVMC
  2466. if(s->avctx->xvmc_acceleration)
  2467. XVMC_field_end(s);
  2468. #endif
  2469. /* end of slice reached */
  2470. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  2471. /* end of image */
  2472. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  2473. ff_er_frame_end(s);
  2474. MPV_frame_end(s);
  2475. if (s->pict_type == B_TYPE || s->low_delay) {
  2476. *pict= *(AVFrame*)s->current_picture_ptr;
  2477. ff_print_debug_info(s, pict);
  2478. } else {
  2479. s->picture_number++;
  2480. /* latency of 1 frame for I and P frames */
  2481. /* XXX: use another variable than picture_number */
  2482. if (s->last_picture_ptr != NULL) {
  2483. *pict= *(AVFrame*)s->last_picture_ptr;
  2484. ff_print_debug_info(s, pict);
  2485. }
  2486. }
  2487. return 1;
  2488. } else {
  2489. return 0;
  2490. }
  2491. }
  2492. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  2493. const uint8_t *buf, int buf_size)
  2494. {
  2495. Mpeg1Context *s1 = avctx->priv_data;
  2496. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2497. int width,height;
  2498. int i, v, j;
  2499. init_get_bits(&s->gb, buf, buf_size*8);
  2500. width = get_bits(&s->gb, 12);
  2501. height = get_bits(&s->gb, 12);
  2502. if (width <= 0 || height <= 0 ||
  2503. (width % 2) != 0 || (height % 2) != 0)
  2504. return -1;
  2505. s->aspect_ratio_info= get_bits(&s->gb, 4);
  2506. if (s->aspect_ratio_info == 0)
  2507. return -1;
  2508. s->frame_rate_index = get_bits(&s->gb, 4);
  2509. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  2510. return -1;
  2511. s->bit_rate = get_bits(&s->gb, 18) * 400;
  2512. if (get_bits1(&s->gb) == 0) /* marker */
  2513. return -1;
  2514. s->width = width;
  2515. s->height = height;
  2516. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  2517. skip_bits(&s->gb, 1);
  2518. /* get matrix */
  2519. if (get_bits1(&s->gb)) {
  2520. for(i=0;i<64;i++) {
  2521. v = get_bits(&s->gb, 8);
  2522. if(v==0){
  2523. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  2524. return -1;
  2525. }
  2526. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2527. s->intra_matrix[j] = v;
  2528. s->chroma_intra_matrix[j] = v;
  2529. }
  2530. #ifdef DEBUG
  2531. dprintf(s->avctx, "intra matrix present\n");
  2532. for(i=0;i<64;i++)
  2533. dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
  2534. dprintf(s->avctx, "\n");
  2535. #endif
  2536. } else {
  2537. for(i=0;i<64;i++) {
  2538. j = s->dsp.idct_permutation[i];
  2539. v = ff_mpeg1_default_intra_matrix[i];
  2540. s->intra_matrix[j] = v;
  2541. s->chroma_intra_matrix[j] = v;
  2542. }
  2543. }
  2544. if (get_bits1(&s->gb)) {
  2545. for(i=0;i<64;i++) {
  2546. v = get_bits(&s->gb, 8);
  2547. if(v==0){
  2548. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  2549. return -1;
  2550. }
  2551. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2552. s->inter_matrix[j] = v;
  2553. s->chroma_inter_matrix[j] = v;
  2554. }
  2555. #ifdef DEBUG
  2556. dprintf(s->avctx, "non intra matrix present\n");
  2557. for(i=0;i<64;i++)
  2558. dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
  2559. dprintf(s->avctx, "\n");
  2560. #endif
  2561. } else {
  2562. for(i=0;i<64;i++) {
  2563. int j= s->dsp.idct_permutation[i];
  2564. v = ff_mpeg1_default_non_intra_matrix[i];
  2565. s->inter_matrix[j] = v;
  2566. s->chroma_inter_matrix[j] = v;
  2567. }
  2568. }
  2569. if(show_bits(&s->gb, 23) != 0){
  2570. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  2571. return -1;
  2572. }
  2573. /* we set mpeg2 parameters so that it emulates mpeg1 */
  2574. s->progressive_sequence = 1;
  2575. s->progressive_frame = 1;
  2576. s->picture_structure = PICT_FRAME;
  2577. s->frame_pred_frame_dct = 1;
  2578. s->chroma_format = 1;
  2579. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  2580. avctx->sub_id = 1; /* indicates mpeg1 */
  2581. s->out_format = FMT_MPEG1;
  2582. s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
  2583. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2584. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2585. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  2586. s->avctx->rc_buffer_size, s->bit_rate);
  2587. return 0;
  2588. }
  2589. static int vcr2_init_sequence(AVCodecContext *avctx)
  2590. {
  2591. Mpeg1Context *s1 = avctx->priv_data;
  2592. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2593. int i, v;
  2594. /* start new mpeg1 context decoding */
  2595. s->out_format = FMT_MPEG1;
  2596. if (s1->mpeg_enc_ctx_allocated) {
  2597. MPV_common_end(s);
  2598. }
  2599. s->width = avctx->coded_width;
  2600. s->height = avctx->coded_height;
  2601. avctx->has_b_frames= 0; //true?
  2602. s->low_delay= 1;
  2603. if(avctx->xvmc_acceleration){
  2604. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  2605. }else{
  2606. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  2607. }
  2608. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2609. if( avctx->idct_algo == FF_IDCT_AUTO )
  2610. avctx->idct_algo = FF_IDCT_SIMPLE;
  2611. if (MPV_common_init(s) < 0)
  2612. return -1;
  2613. exchange_uv(s);//common init reset pblocks, so we swap them here
  2614. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  2615. s1->mpeg_enc_ctx_allocated = 1;
  2616. for(i=0;i<64;i++) {
  2617. int j= s->dsp.idct_permutation[i];
  2618. v = ff_mpeg1_default_intra_matrix[i];
  2619. s->intra_matrix[j] = v;
  2620. s->chroma_intra_matrix[j] = v;
  2621. v = ff_mpeg1_default_non_intra_matrix[i];
  2622. s->inter_matrix[j] = v;
  2623. s->chroma_inter_matrix[j] = v;
  2624. }
  2625. s->progressive_sequence = 1;
  2626. s->progressive_frame = 1;
  2627. s->picture_structure = PICT_FRAME;
  2628. s->frame_pred_frame_dct = 1;
  2629. s->chroma_format = 1;
  2630. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2631. avctx->sub_id = 2; /* indicates mpeg2 */
  2632. return 0;
  2633. }
  2634. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2635. const uint8_t *buf, int buf_size)
  2636. {
  2637. const uint8_t *p;
  2638. int len, flags;
  2639. p = buf;
  2640. len = buf_size;
  2641. /* we parse the DTG active format information */
  2642. if (len >= 5 &&
  2643. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2644. flags = p[4];
  2645. p += 5;
  2646. len -= 5;
  2647. if (flags & 0x80) {
  2648. /* skip event id */
  2649. if (len < 2)
  2650. return;
  2651. p += 2;
  2652. len -= 2;
  2653. }
  2654. if (flags & 0x40) {
  2655. if (len < 1)
  2656. return;
  2657. avctx->dtg_active_format = p[0] & 0x0f;
  2658. }
  2659. }
  2660. }
  2661. static void mpeg_decode_gop(AVCodecContext *avctx,
  2662. const uint8_t *buf, int buf_size){
  2663. Mpeg1Context *s1 = avctx->priv_data;
  2664. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2665. int drop_frame_flag;
  2666. int time_code_hours, time_code_minutes;
  2667. int time_code_seconds, time_code_pictures;
  2668. int broken_link;
  2669. init_get_bits(&s->gb, buf, buf_size*8);
  2670. drop_frame_flag = get_bits1(&s->gb);
  2671. time_code_hours=get_bits(&s->gb,5);
  2672. time_code_minutes = get_bits(&s->gb,6);
  2673. skip_bits1(&s->gb);//marker bit
  2674. time_code_seconds = get_bits(&s->gb,6);
  2675. time_code_pictures = get_bits(&s->gb,6);
  2676. /*broken_link indicate that after editing the
  2677. reference frames of the first B-Frames after GOP I-Frame
  2678. are missing (open gop)*/
  2679. broken_link = get_bits1(&s->gb);
  2680. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2681. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
  2682. time_code_hours, time_code_minutes, time_code_seconds,
  2683. time_code_pictures, broken_link);
  2684. }
  2685. /**
  2686. * finds the end of the current frame in the bitstream.
  2687. * @return the position of the first byte of the next frame, or -1
  2688. */
  2689. static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  2690. {
  2691. int i;
  2692. uint32_t state= pc->state;
  2693. i=0;
  2694. if(!pc->frame_start_found){
  2695. for(i=0; i<buf_size; i++){
  2696. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  2697. if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  2698. i++;
  2699. pc->frame_start_found=1;
  2700. break;
  2701. }
  2702. }
  2703. }
  2704. if(pc->frame_start_found){
  2705. /* EOF considered as end of frame */
  2706. if (buf_size == 0)
  2707. return 0;
  2708. for(; i<buf_size; i++){
  2709. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  2710. if((state&0xFFFFFF00) == 0x100){
  2711. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  2712. pc->frame_start_found=0;
  2713. pc->state=-1;
  2714. return i-3;
  2715. }
  2716. }
  2717. }
  2718. }
  2719. pc->state= state;
  2720. return END_NOT_FOUND;
  2721. }
  2722. /* handle buffering and image synchronisation */
  2723. static int mpeg_decode_frame(AVCodecContext *avctx,
  2724. void *data, int *data_size,
  2725. uint8_t *buf, int buf_size)
  2726. {
  2727. Mpeg1Context *s = avctx->priv_data;
  2728. const uint8_t *buf_end;
  2729. const uint8_t *buf_ptr;
  2730. uint32_t start_code;
  2731. int ret, input_size;
  2732. AVFrame *picture = data;
  2733. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2734. dprintf(avctx, "fill_buffer\n");
  2735. if (buf_size == 0) {
  2736. /* special case for last picture */
  2737. if (s2->low_delay==0 && s2->next_picture_ptr) {
  2738. *picture= *(AVFrame*)s2->next_picture_ptr;
  2739. s2->next_picture_ptr= NULL;
  2740. *data_size = sizeof(AVFrame);
  2741. }
  2742. return 0;
  2743. }
  2744. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2745. int next= mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
  2746. if( ff_combine_frame(&s2->parse_context, next, &buf, &buf_size) < 0 )
  2747. return buf_size;
  2748. }
  2749. buf_ptr = buf;
  2750. buf_end = buf + buf_size;
  2751. #if 0
  2752. if (s->repeat_field % 2 == 1) {
  2753. s->repeat_field++;
  2754. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2755. // s2->picture_number, s->repeat_field);
  2756. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2757. *data_size = sizeof(AVPicture);
  2758. goto the_end;
  2759. }
  2760. }
  2761. #endif
  2762. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2763. vcr2_init_sequence(avctx);
  2764. s->slice_count= 0;
  2765. for(;;) {
  2766. /* find start next code */
  2767. start_code = -1;
  2768. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2769. if (start_code > 0x1ff){
  2770. if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2771. if(avctx->thread_count > 1){
  2772. int i;
  2773. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
  2774. for(i=0; i<s->slice_count; i++)
  2775. s2->error_count += s2->thread_context[i]->error_count;
  2776. }
  2777. if (slice_end(avctx, picture)) {
  2778. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2779. *data_size = sizeof(AVPicture);
  2780. }
  2781. }
  2782. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2783. }
  2784. input_size = buf_end - buf_ptr;
  2785. if(avctx->debug & FF_DEBUG_STARTCODE){
  2786. av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
  2787. }
  2788. /* prepare data for next start code */
  2789. switch(start_code) {
  2790. case SEQ_START_CODE:
  2791. mpeg1_decode_sequence(avctx, buf_ptr,
  2792. input_size);
  2793. break;
  2794. case PICTURE_START_CODE:
  2795. /* we have a complete image : we try to decompress it */
  2796. mpeg1_decode_picture(avctx,
  2797. buf_ptr, input_size);
  2798. break;
  2799. case EXT_START_CODE:
  2800. mpeg_decode_extension(avctx,
  2801. buf_ptr, input_size);
  2802. break;
  2803. case USER_START_CODE:
  2804. mpeg_decode_user_data(avctx,
  2805. buf_ptr, input_size);
  2806. break;
  2807. case GOP_START_CODE:
  2808. s2->first_field=0;
  2809. mpeg_decode_gop(avctx,
  2810. buf_ptr, input_size);
  2811. break;
  2812. default:
  2813. if (start_code >= SLICE_MIN_START_CODE &&
  2814. start_code <= SLICE_MAX_START_CODE) {
  2815. int mb_y= start_code - SLICE_MIN_START_CODE;
  2816. if(s2->last_picture_ptr==NULL){
  2817. /* skip b frames if we dont have reference frames */
  2818. if(s2->pict_type==B_TYPE) break;
  2819. /* skip P frames if we dont have reference frame no valid header */
  2820. // if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
  2821. }
  2822. /* skip b frames if we are in a hurry */
  2823. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  2824. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
  2825. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
  2826. || avctx->skip_frame >= AVDISCARD_ALL)
  2827. break;
  2828. /* skip everything if we are in a hurry>=5 */
  2829. if(avctx->hurry_up>=5) break;
  2830. if (!s->mpeg_enc_ctx_allocated) break;
  2831. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2832. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2833. break;
  2834. }
  2835. if(s2->first_slice){
  2836. s2->first_slice=0;
  2837. if(mpeg_field_start(s2) < 0)
  2838. return -1;
  2839. }
  2840. if(!s2->current_picture_ptr){
  2841. av_log(avctx, AV_LOG_ERROR, "current_picture not initalized\n");
  2842. return -1;
  2843. }
  2844. if(avctx->thread_count > 1){
  2845. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2846. if(threshold <= mb_y){
  2847. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2848. thread_context->start_mb_y= mb_y;
  2849. thread_context->end_mb_y = s2->mb_height;
  2850. if(s->slice_count){
  2851. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2852. ff_update_duplicate_context(thread_context, s2);
  2853. }
  2854. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2855. s->slice_count++;
  2856. }
  2857. buf_ptr += 2; //FIXME add minimum num of bytes per slice
  2858. }else{
  2859. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2860. emms_c();
  2861. if(ret < 0){
  2862. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2863. 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);
  2864. }else{
  2865. 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);
  2866. }
  2867. }
  2868. }
  2869. break;
  2870. }
  2871. }
  2872. }
  2873. static int mpeg_decode_end(AVCodecContext *avctx)
  2874. {
  2875. Mpeg1Context *s = avctx->priv_data;
  2876. if (s->mpeg_enc_ctx_allocated)
  2877. MPV_common_end(&s->mpeg_enc_ctx);
  2878. return 0;
  2879. }
  2880. AVCodec mpeg1video_decoder = {
  2881. "mpeg1video",
  2882. CODEC_TYPE_VIDEO,
  2883. CODEC_ID_MPEG1VIDEO,
  2884. sizeof(Mpeg1Context),
  2885. mpeg_decode_init,
  2886. NULL,
  2887. mpeg_decode_end,
  2888. mpeg_decode_frame,
  2889. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2890. .flush= ff_mpeg_flush,
  2891. };
  2892. AVCodec mpeg2video_decoder = {
  2893. "mpeg2video",
  2894. CODEC_TYPE_VIDEO,
  2895. CODEC_ID_MPEG2VIDEO,
  2896. sizeof(Mpeg1Context),
  2897. mpeg_decode_init,
  2898. NULL,
  2899. mpeg_decode_end,
  2900. mpeg_decode_frame,
  2901. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2902. .flush= ff_mpeg_flush,
  2903. };
  2904. //legacy decoder
  2905. AVCodec mpegvideo_decoder = {
  2906. "mpegvideo",
  2907. CODEC_TYPE_VIDEO,
  2908. CODEC_ID_MPEG2VIDEO,
  2909. sizeof(Mpeg1Context),
  2910. mpeg_decode_init,
  2911. NULL,
  2912. mpeg_decode_end,
  2913. mpeg_decode_frame,
  2914. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2915. .flush= ff_mpeg_flush,
  2916. };
  2917. #ifdef CONFIG_ENCODERS
  2918. AVCodec mpeg1video_encoder = {
  2919. "mpeg1video",
  2920. CODEC_TYPE_VIDEO,
  2921. CODEC_ID_MPEG1VIDEO,
  2922. sizeof(MpegEncContext),
  2923. encode_init,
  2924. MPV_encode_picture,
  2925. MPV_encode_end,
  2926. .supported_framerates= ff_frame_rate_tab+1,
  2927. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
  2928. .capabilities= CODEC_CAP_DELAY,
  2929. };
  2930. AVCodec mpeg2video_encoder = {
  2931. "mpeg2video",
  2932. CODEC_TYPE_VIDEO,
  2933. CODEC_ID_MPEG2VIDEO,
  2934. sizeof(MpegEncContext),
  2935. encode_init,
  2936. MPV_encode_picture,
  2937. MPV_encode_end,
  2938. .supported_framerates= ff_frame_rate_tab+1,
  2939. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
  2940. .capabilities= CODEC_CAP_DELAY,
  2941. };
  2942. #endif
  2943. #ifdef HAVE_XVMC
  2944. static int mpeg_mc_decode_init(AVCodecContext *avctx){
  2945. Mpeg1Context *s;
  2946. if( avctx->thread_count > 1)
  2947. return -1;
  2948. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2949. return -1;
  2950. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2951. dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2952. }
  2953. mpeg_decode_init(avctx);
  2954. s = avctx->priv_data;
  2955. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2956. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2957. return 0;
  2958. }
  2959. AVCodec mpeg_xvmc_decoder = {
  2960. "mpegvideo_xvmc",
  2961. CODEC_TYPE_VIDEO,
  2962. CODEC_ID_MPEG2VIDEO_XVMC,
  2963. sizeof(Mpeg1Context),
  2964. mpeg_mc_decode_init,
  2965. NULL,
  2966. mpeg_decode_end,
  2967. mpeg_decode_frame,
  2968. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2969. .flush= ff_mpeg_flush,
  2970. };
  2971. #endif
  2972. #ifdef CONFIG_MPEGVIDEO_PARSER
  2973. static void mpegvideo_extract_headers(AVCodecParserContext *s,
  2974. AVCodecContext *avctx,
  2975. const uint8_t *buf, int buf_size)
  2976. {
  2977. ParseContext1 *pc = s->priv_data;
  2978. const uint8_t *buf_end;
  2979. uint32_t start_code;
  2980. int frame_rate_index, ext_type, bytes_left;
  2981. int frame_rate_ext_n, frame_rate_ext_d;
  2982. int picture_structure, top_field_first, repeat_first_field, progressive_frame;
  2983. int horiz_size_ext, vert_size_ext, bit_rate_ext;
  2984. //FIXME replace the crap with get_bits()
  2985. s->repeat_pict = 0;
  2986. buf_end = buf + buf_size;
  2987. while (buf < buf_end) {
  2988. start_code= -1;
  2989. buf= ff_find_start_code(buf, buf_end, &start_code);
  2990. bytes_left = buf_end - buf;
  2991. switch(start_code) {
  2992. case PICTURE_START_CODE:
  2993. if (bytes_left >= 2) {
  2994. s->pict_type = (buf[1] >> 3) & 7;
  2995. }
  2996. break;
  2997. case SEQ_START_CODE:
  2998. if (bytes_left >= 7) {
  2999. pc->width = (buf[0] << 4) | (buf[1] >> 4);
  3000. pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
  3001. avcodec_set_dimensions(avctx, pc->width, pc->height);
  3002. frame_rate_index = buf[3] & 0xf;
  3003. pc->frame_rate.den = avctx->time_base.den = ff_frame_rate_tab[frame_rate_index].num;
  3004. pc->frame_rate.num = avctx->time_base.num = ff_frame_rate_tab[frame_rate_index].den;
  3005. avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
  3006. avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  3007. avctx->sub_id = 1;
  3008. }
  3009. break;
  3010. case EXT_START_CODE:
  3011. if (bytes_left >= 1) {
  3012. ext_type = (buf[0] >> 4);
  3013. switch(ext_type) {
  3014. case 0x1: /* sequence extension */
  3015. if (bytes_left >= 6) {
  3016. horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
  3017. vert_size_ext = (buf[2] >> 5) & 3;
  3018. bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
  3019. frame_rate_ext_n = (buf[5] >> 5) & 3;
  3020. frame_rate_ext_d = (buf[5] & 0x1f);
  3021. pc->progressive_sequence = buf[1] & (1 << 3);
  3022. avctx->has_b_frames= !(buf[5] >> 7);
  3023. pc->width |=(horiz_size_ext << 12);
  3024. pc->height |=( vert_size_ext << 12);
  3025. avctx->bit_rate += (bit_rate_ext << 18) * 400;
  3026. avcodec_set_dimensions(avctx, pc->width, pc->height);
  3027. avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1);
  3028. avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
  3029. avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  3030. avctx->sub_id = 2; /* forces MPEG2 */
  3031. }
  3032. break;
  3033. case 0x8: /* picture coding extension */
  3034. if (bytes_left >= 5) {
  3035. picture_structure = buf[2]&3;
  3036. top_field_first = buf[3] & (1 << 7);
  3037. repeat_first_field = buf[3] & (1 << 1);
  3038. progressive_frame = buf[4] & (1 << 7);
  3039. /* check if we must repeat the frame */
  3040. if (repeat_first_field) {
  3041. if (pc->progressive_sequence) {
  3042. if (top_field_first)
  3043. s->repeat_pict = 4;
  3044. else
  3045. s->repeat_pict = 2;
  3046. } else if (progressive_frame) {
  3047. s->repeat_pict = 1;
  3048. }
  3049. }
  3050. /* the packet only represents half a frame
  3051. XXX,FIXME maybe find a different solution */
  3052. if(picture_structure != 3)
  3053. s->repeat_pict = -1;
  3054. }
  3055. break;
  3056. }
  3057. }
  3058. break;
  3059. case -1:
  3060. goto the_end;
  3061. default:
  3062. /* we stop parsing when we encounter a slice. It ensures
  3063. that this function takes a negligible amount of time */
  3064. if (start_code >= SLICE_MIN_START_CODE &&
  3065. start_code <= SLICE_MAX_START_CODE)
  3066. goto the_end;
  3067. break;
  3068. }
  3069. }
  3070. the_end: ;
  3071. }
  3072. static int mpegvideo_parse(AVCodecParserContext *s,
  3073. AVCodecContext *avctx,
  3074. uint8_t **poutbuf, int *poutbuf_size,
  3075. const uint8_t *buf, int buf_size)
  3076. {
  3077. ParseContext1 *pc1 = s->priv_data;
  3078. ParseContext *pc= &pc1->pc;
  3079. int next;
  3080. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  3081. next= buf_size;
  3082. }else{
  3083. next= mpeg1_find_frame_end(pc, buf, buf_size);
  3084. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  3085. *poutbuf = NULL;
  3086. *poutbuf_size = 0;
  3087. return buf_size;
  3088. }
  3089. }
  3090. /* we have a full frame : we just parse the first few MPEG headers
  3091. to have the full timing information. The time take by this
  3092. function should be negligible for uncorrupted streams */
  3093. mpegvideo_extract_headers(s, avctx, buf, buf_size);
  3094. #if 0
  3095. printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
  3096. s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
  3097. #endif
  3098. *poutbuf = (uint8_t *)buf;
  3099. *poutbuf_size = buf_size;
  3100. return next;
  3101. }
  3102. static int mpegvideo_split(AVCodecContext *avctx,
  3103. const uint8_t *buf, int buf_size)
  3104. {
  3105. int i;
  3106. uint32_t state= -1;
  3107. for(i=0; i<buf_size; i++){
  3108. state= (state<<8) | buf[i];
  3109. if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
  3110. return i-3;
  3111. }
  3112. return 0;
  3113. }
  3114. AVCodecParser mpegvideo_parser = {
  3115. { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
  3116. sizeof(ParseContext1),
  3117. NULL,
  3118. mpegvideo_parse,
  3119. ff_parse1_close,
  3120. mpegvideo_split,
  3121. };
  3122. #endif /* !CONFIG_MPEGVIDEO_PARSER */
  3123. static int imx_dump_header(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  3124. uint8_t **poutbuf, int *poutbuf_size,
  3125. const uint8_t *buf, int buf_size, int keyframe)
  3126. {
  3127. /* MXF essence element key */
  3128. static const uint8_t imx_header[16] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x05,0x01,0x01,0x00 };
  3129. uint8_t *poutbufp;
  3130. if (avctx->codec_id != CODEC_ID_MPEG2VIDEO) {
  3131. av_log(avctx, AV_LOG_ERROR, "imx bitstream filter only applies to mpeg2video codec\n");
  3132. return 0;
  3133. }
  3134. *poutbuf = av_malloc(buf_size + 20 + FF_INPUT_BUFFER_PADDING_SIZE);
  3135. poutbufp = *poutbuf;
  3136. bytestream_put_buffer(&poutbufp, imx_header, 16);
  3137. bytestream_put_byte(&poutbufp, 0x83); /* KLV BER long form */
  3138. bytestream_put_be24(&poutbufp, buf_size);
  3139. bytestream_put_buffer(&poutbufp, buf, buf_size);
  3140. *poutbuf_size = poutbufp - *poutbuf;
  3141. return 1;
  3142. }
  3143. AVBitStreamFilter imx_dump_header_bsf = {
  3144. "imxdump",
  3145. 0,
  3146. imx_dump_header,
  3147. };
  3148. /* this is ugly i know, but the alternative is too make
  3149. hundreds of vars global and prefix them with ff_mpeg1_
  3150. which is far uglier. */
  3151. #include "mdec.c"