mpeg12.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  1. /*
  2. * MPEG-1/2 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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * MPEG-1/2 decoder
  25. */
  26. //#define DEBUG
  27. #include "internal.h"
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "mpegvideo.h"
  31. #include "mpeg12.h"
  32. #include "mpeg12data.h"
  33. #include "mpeg12decdata.h"
  34. #include "bytestream.h"
  35. #include "vdpau_internal.h"
  36. #include "xvmc_internal.h"
  37. #include "thread.h"
  38. //#undef NDEBUG
  39. //#include <assert.h>
  40. #define MV_VLC_BITS 9
  41. #define MBINCR_VLC_BITS 9
  42. #define MB_PAT_VLC_BITS 9
  43. #define MB_PTYPE_VLC_BITS 6
  44. #define MB_BTYPE_VLC_BITS 6
  45. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  46. DCTELEM *block,
  47. int n);
  48. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  49. DCTELEM *block,
  50. int n);
  51. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
  52. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  53. DCTELEM *block,
  54. int n);
  55. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  56. DCTELEM *block,
  57. int n);
  58. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
  59. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
  60. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  61. static void exchange_uv(MpegEncContext *s);
  62. static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
  63. PIX_FMT_XVMC_MPEG2_IDCT,
  64. PIX_FMT_XVMC_MPEG2_MC,
  65. PIX_FMT_NONE};
  66. uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  67. #define INIT_2D_VLC_RL(rl, static_size)\
  68. {\
  69. static RL_VLC_ELEM rl_vlc_table[static_size];\
  70. INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
  71. &rl.table_vlc[0][1], 4, 2,\
  72. &rl.table_vlc[0][0], 4, 2, static_size);\
  73. \
  74. rl.rl_vlc[0]= rl_vlc_table;\
  75. init_2d_vlc_rl(&rl);\
  76. }
  77. static void init_2d_vlc_rl(RLTable *rl)
  78. {
  79. int i;
  80. for(i=0; i<rl->vlc.table_size; i++){
  81. int code= rl->vlc.table[i][0];
  82. int len = rl->vlc.table[i][1];
  83. int level, run;
  84. if(len==0){ // illegal code
  85. run= 65;
  86. level= MAX_LEVEL;
  87. }else if(len<0){ //more bits needed
  88. run= 0;
  89. level= code;
  90. }else{
  91. if(code==rl->n){ //esc
  92. run= 65;
  93. level= 0;
  94. }else if(code==rl->n+1){ //eob
  95. run= 0;
  96. level= 127;
  97. }else{
  98. run= rl->table_run [code] + 1;
  99. level= rl->table_level[code];
  100. }
  101. }
  102. rl->rl_vlc[0][i].len= len;
  103. rl->rl_vlc[0][i].level= level;
  104. rl->rl_vlc[0][i].run= run;
  105. }
  106. }
  107. void ff_mpeg12_common_init(MpegEncContext *s)
  108. {
  109. s->y_dc_scale_table=
  110. s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  111. }
  112. void ff_mpeg1_clean_buffers(MpegEncContext *s){
  113. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  114. s->last_dc[1] = s->last_dc[0];
  115. s->last_dc[2] = s->last_dc[0];
  116. memset(s->last_mv, 0, sizeof(s->last_mv));
  117. }
  118. /******************************************/
  119. /* decoding */
  120. VLC ff_dc_lum_vlc;
  121. VLC ff_dc_chroma_vlc;
  122. static VLC mv_vlc;
  123. static VLC mbincr_vlc;
  124. static VLC mb_ptype_vlc;
  125. static VLC mb_btype_vlc;
  126. static VLC mb_pat_vlc;
  127. av_cold void ff_mpeg12_init_vlcs(void)
  128. {
  129. static int done = 0;
  130. if (!done) {
  131. done = 1;
  132. INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
  133. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  134. ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
  135. INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
  136. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  137. ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
  138. INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
  139. &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
  140. &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
  141. INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  142. &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
  143. &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
  144. INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  145. &ff_mpeg12_mbPatTable[0][1], 2, 1,
  146. &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
  147. INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  148. &table_mb_ptype[0][1], 2, 1,
  149. &table_mb_ptype[0][0], 2, 1, 64);
  150. INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  151. &table_mb_btype[0][1], 2, 1,
  152. &table_mb_btype[0][0], 2, 1, 64);
  153. init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  154. init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  155. INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
  156. INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
  157. }
  158. }
  159. static inline int get_dmv(MpegEncContext *s)
  160. {
  161. if(get_bits1(&s->gb))
  162. return 1 - (get_bits1(&s->gb) << 1);
  163. else
  164. return 0;
  165. }
  166. static inline int get_qscale(MpegEncContext *s)
  167. {
  168. int qscale = get_bits(&s->gb, 5);
  169. if (s->q_scale_type) {
  170. return non_linear_qscale[qscale];
  171. } else {
  172. return qscale << 1;
  173. }
  174. }
  175. /* motion type (for MPEG-2) */
  176. #define MT_FIELD 1
  177. #define MT_FRAME 2
  178. #define MT_16X8 2
  179. #define MT_DMV 3
  180. static int mpeg_decode_mb(MpegEncContext *s,
  181. DCTELEM block[12][64])
  182. {
  183. int i, j, k, cbp, val, mb_type, motion_type;
  184. const int mb_block_count = 4 + (1<< s->chroma_format);
  185. av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  186. assert(s->mb_skipped==0);
  187. if (s->mb_skip_run-- != 0) {
  188. if (s->pict_type == AV_PICTURE_TYPE_P) {
  189. s->mb_skipped = 1;
  190. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  191. } else {
  192. int mb_type;
  193. if(s->mb_x)
  194. mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
  195. else
  196. 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
  197. if(IS_INTRA(mb_type))
  198. return -1;
  199. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  200. mb_type | MB_TYPE_SKIP;
  201. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  202. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  203. s->mb_skipped = 1;
  204. }
  205. return 0;
  206. }
  207. switch(s->pict_type) {
  208. default:
  209. case AV_PICTURE_TYPE_I:
  210. if (get_bits1(&s->gb) == 0) {
  211. if (get_bits1(&s->gb) == 0){
  212. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  213. return -1;
  214. }
  215. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  216. } else {
  217. mb_type = MB_TYPE_INTRA;
  218. }
  219. break;
  220. case AV_PICTURE_TYPE_P:
  221. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  222. if (mb_type < 0){
  223. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  224. return -1;
  225. }
  226. mb_type = ptype2mb_type[ mb_type ];
  227. break;
  228. case AV_PICTURE_TYPE_B:
  229. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  230. if (mb_type < 0){
  231. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  232. return -1;
  233. }
  234. mb_type = btype2mb_type[ mb_type ];
  235. break;
  236. }
  237. av_dlog(s->avctx, "mb_type=%x\n", mb_type);
  238. // motion_type = 0; /* avoid warning */
  239. if (IS_INTRA(mb_type)) {
  240. s->dsp.clear_blocks(s->block[0]);
  241. if(!s->chroma_y_shift){
  242. s->dsp.clear_blocks(s->block[6]);
  243. }
  244. /* compute DCT type */
  245. if (s->picture_structure == PICT_FRAME && //FIXME add an interlaced_dct coded var?
  246. !s->frame_pred_frame_dct) {
  247. s->interlaced_dct = get_bits1(&s->gb);
  248. }
  249. if (IS_QUANT(mb_type))
  250. s->qscale = get_qscale(s);
  251. if (s->concealment_motion_vectors) {
  252. /* just parse them */
  253. if (s->picture_structure != PICT_FRAME)
  254. skip_bits1(&s->gb); /* field select */
  255. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  256. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  257. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  258. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  259. skip_bits1(&s->gb); /* marker */
  260. }else
  261. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  262. s->mb_intra = 1;
  263. //if 1, we memcpy blocks in xvmcvideo
  264. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
  265. ff_xvmc_pack_pblocks(s,-1);//inter are always full blocks
  266. if(s->swap_uv){
  267. exchange_uv(s);
  268. }
  269. }
  270. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  271. if(s->flags2 & CODEC_FLAG2_FAST){
  272. for(i=0;i<6;i++) {
  273. mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
  274. }
  275. }else{
  276. for(i=0;i<mb_block_count;i++) {
  277. if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
  278. return -1;
  279. }
  280. }
  281. } else {
  282. for(i=0;i<6;i++) {
  283. if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
  284. return -1;
  285. }
  286. }
  287. } else {
  288. if (mb_type & MB_TYPE_ZERO_MV){
  289. assert(mb_type & MB_TYPE_CBP);
  290. s->mv_dir = MV_DIR_FORWARD;
  291. if(s->picture_structure == PICT_FRAME){
  292. if(!s->frame_pred_frame_dct)
  293. s->interlaced_dct = get_bits1(&s->gb);
  294. s->mv_type = MV_TYPE_16X16;
  295. }else{
  296. s->mv_type = MV_TYPE_FIELD;
  297. mb_type |= MB_TYPE_INTERLACED;
  298. s->field_select[0][0]= s->picture_structure - 1;
  299. }
  300. if (IS_QUANT(mb_type))
  301. s->qscale = get_qscale(s);
  302. s->last_mv[0][0][0] = 0;
  303. s->last_mv[0][0][1] = 0;
  304. s->last_mv[0][1][0] = 0;
  305. s->last_mv[0][1][1] = 0;
  306. s->mv[0][0][0] = 0;
  307. s->mv[0][0][1] = 0;
  308. }else{
  309. assert(mb_type & MB_TYPE_L0L1);
  310. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  311. /* get additional motion vector type */
  312. if (s->frame_pred_frame_dct)
  313. motion_type = MT_FRAME;
  314. else{
  315. motion_type = get_bits(&s->gb, 2);
  316. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  317. s->interlaced_dct = get_bits1(&s->gb);
  318. }
  319. if (IS_QUANT(mb_type))
  320. s->qscale = get_qscale(s);
  321. /* motion vectors */
  322. s->mv_dir= (mb_type>>13)&3;
  323. av_dlog(s->avctx, "motion_type=%d\n", motion_type);
  324. switch(motion_type) {
  325. case MT_FRAME: /* or MT_16X8 */
  326. if (s->picture_structure == PICT_FRAME) {
  327. mb_type |= MB_TYPE_16x16;
  328. s->mv_type = MV_TYPE_16X16;
  329. for(i=0;i<2;i++) {
  330. if (USES_LIST(mb_type, i)) {
  331. /* MT_FRAME */
  332. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  333. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  334. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  335. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  336. /* full_pel: only for MPEG-1 */
  337. if (s->full_pel[i]){
  338. s->mv[i][0][0] <<= 1;
  339. s->mv[i][0][1] <<= 1;
  340. }
  341. }
  342. }
  343. } else {
  344. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  345. s->mv_type = MV_TYPE_16X8;
  346. for(i=0;i<2;i++) {
  347. if (USES_LIST(mb_type, i)) {
  348. /* MT_16X8 */
  349. for(j=0;j<2;j++) {
  350. s->field_select[i][j] = get_bits1(&s->gb);
  351. for(k=0;k<2;k++) {
  352. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  353. s->last_mv[i][j][k]);
  354. s->last_mv[i][j][k] = val;
  355. s->mv[i][j][k] = val;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. break;
  362. case MT_FIELD:
  363. s->mv_type = MV_TYPE_FIELD;
  364. if (s->picture_structure == PICT_FRAME) {
  365. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  366. for(i=0;i<2;i++) {
  367. if (USES_LIST(mb_type, i)) {
  368. for(j=0;j<2;j++) {
  369. s->field_select[i][j] = get_bits1(&s->gb);
  370. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  371. s->last_mv[i][j][0]);
  372. s->last_mv[i][j][0] = val;
  373. s->mv[i][j][0] = val;
  374. av_dlog(s->avctx, "fmx=%d\n", val);
  375. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  376. s->last_mv[i][j][1] >> 1);
  377. s->last_mv[i][j][1] = val << 1;
  378. s->mv[i][j][1] = val;
  379. av_dlog(s->avctx, "fmy=%d\n", val);
  380. }
  381. }
  382. }
  383. } else {
  384. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  385. for(i=0;i<2;i++) {
  386. if (USES_LIST(mb_type, i)) {
  387. s->field_select[i][0] = get_bits1(&s->gb);
  388. for(k=0;k<2;k++) {
  389. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  390. s->last_mv[i][0][k]);
  391. s->last_mv[i][0][k] = val;
  392. s->last_mv[i][1][k] = val;
  393. s->mv[i][0][k] = val;
  394. }
  395. }
  396. }
  397. }
  398. break;
  399. case MT_DMV:
  400. s->mv_type = MV_TYPE_DMV;
  401. for(i=0;i<2;i++) {
  402. if (USES_LIST(mb_type, i)) {
  403. int dmx, dmy, mx, my, m;
  404. const int my_shift= s->picture_structure == PICT_FRAME;
  405. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  406. s->last_mv[i][0][0]);
  407. s->last_mv[i][0][0] = mx;
  408. s->last_mv[i][1][0] = mx;
  409. dmx = get_dmv(s);
  410. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  411. s->last_mv[i][0][1] >> my_shift);
  412. dmy = get_dmv(s);
  413. s->last_mv[i][0][1] = my<<my_shift;
  414. s->last_mv[i][1][1] = my<<my_shift;
  415. s->mv[i][0][0] = mx;
  416. s->mv[i][0][1] = my;
  417. s->mv[i][1][0] = mx;//not used
  418. s->mv[i][1][1] = my;//not used
  419. if (s->picture_structure == PICT_FRAME) {
  420. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  421. //m = 1 + 2 * s->top_field_first;
  422. m = s->top_field_first ? 1 : 3;
  423. /* top -> top pred */
  424. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  425. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  426. m = 4 - m;
  427. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  428. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  429. } else {
  430. mb_type |= MB_TYPE_16x16;
  431. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  432. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  433. if(s->picture_structure == PICT_TOP_FIELD)
  434. s->mv[i][2][1]--;
  435. else
  436. s->mv[i][2][1]++;
  437. }
  438. }
  439. }
  440. break;
  441. default:
  442. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  443. return -1;
  444. }
  445. }
  446. s->mb_intra = 0;
  447. if (HAS_CBP(mb_type)) {
  448. s->dsp.clear_blocks(s->block[0]);
  449. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  450. if(mb_block_count > 6){
  451. cbp<<= mb_block_count-6;
  452. cbp |= get_bits(&s->gb, mb_block_count-6);
  453. s->dsp.clear_blocks(s->block[6]);
  454. }
  455. if (cbp <= 0){
  456. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  457. return -1;
  458. }
  459. //if 1, we memcpy blocks in xvmcvideo
  460. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
  461. ff_xvmc_pack_pblocks(s,cbp);
  462. if(s->swap_uv){
  463. exchange_uv(s);
  464. }
  465. }
  466. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  467. if(s->flags2 & CODEC_FLAG2_FAST){
  468. for(i=0;i<6;i++) {
  469. if(cbp & 32) {
  470. mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
  471. } else {
  472. s->block_last_index[i] = -1;
  473. }
  474. cbp+=cbp;
  475. }
  476. }else{
  477. cbp<<= 12-mb_block_count;
  478. for(i=0;i<mb_block_count;i++) {
  479. if ( cbp & (1<<11) ) {
  480. if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
  481. return -1;
  482. } else {
  483. s->block_last_index[i] = -1;
  484. }
  485. cbp+=cbp;
  486. }
  487. }
  488. } else {
  489. if(s->flags2 & CODEC_FLAG2_FAST){
  490. for(i=0;i<6;i++) {
  491. if (cbp & 32) {
  492. mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
  493. } else {
  494. s->block_last_index[i] = -1;
  495. }
  496. cbp+=cbp;
  497. }
  498. }else{
  499. for(i=0;i<6;i++) {
  500. if (cbp & 32) {
  501. if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
  502. return -1;
  503. } else {
  504. s->block_last_index[i] = -1;
  505. }
  506. cbp+=cbp;
  507. }
  508. }
  509. }
  510. }else{
  511. for(i=0;i<12;i++)
  512. s->block_last_index[i] = -1;
  513. }
  514. }
  515. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  516. return 0;
  517. }
  518. /* as H.263, but only 17 codes */
  519. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  520. {
  521. int code, sign, val, l, shift;
  522. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  523. if (code == 0) {
  524. return pred;
  525. }
  526. if (code < 0) {
  527. return 0xffff;
  528. }
  529. sign = get_bits1(&s->gb);
  530. shift = fcode - 1;
  531. val = code;
  532. if (shift) {
  533. val = (val - 1) << shift;
  534. val |= get_bits(&s->gb, shift);
  535. val++;
  536. }
  537. if (sign)
  538. val = -val;
  539. val += pred;
  540. /* modulo decoding */
  541. l= INT_BIT - 5 - shift;
  542. val = (val<<l)>>l;
  543. return val;
  544. }
  545. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  546. DCTELEM *block,
  547. int n)
  548. {
  549. int level, dc, diff, i, j, run;
  550. int component;
  551. RLTable *rl = &ff_rl_mpeg1;
  552. uint8_t * const scantable= s->intra_scantable.permutated;
  553. const uint16_t *quant_matrix= s->intra_matrix;
  554. const int qscale= s->qscale;
  555. /* DC coefficient */
  556. component = (n <= 3 ? 0 : n - 4 + 1);
  557. diff = decode_dc(&s->gb, component);
  558. if (diff >= 0xffff)
  559. return -1;
  560. dc = s->last_dc[component];
  561. dc += diff;
  562. s->last_dc[component] = dc;
  563. block[0] = dc*quant_matrix[0];
  564. av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
  565. i = 0;
  566. {
  567. OPEN_READER(re, &s->gb);
  568. /* now quantify & encode AC coefficients */
  569. for(;;) {
  570. UPDATE_CACHE(re, &s->gb);
  571. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  572. if(level == 127){
  573. break;
  574. } else if(level != 0) {
  575. i += run;
  576. j = scantable[i];
  577. level= (level*qscale*quant_matrix[j])>>4;
  578. level= (level-1)|1;
  579. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  580. LAST_SKIP_BITS(re, &s->gb, 1);
  581. } else {
  582. /* escape */
  583. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  584. UPDATE_CACHE(re, &s->gb);
  585. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  586. if (level == -128) {
  587. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  588. } else if (level == 0) {
  589. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  590. }
  591. i += run;
  592. j = scantable[i];
  593. if(level<0){
  594. level= -level;
  595. level= (level*qscale*quant_matrix[j])>>4;
  596. level= (level-1)|1;
  597. level= -level;
  598. }else{
  599. level= (level*qscale*quant_matrix[j])>>4;
  600. level= (level-1)|1;
  601. }
  602. }
  603. if (i > 63){
  604. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  605. return -1;
  606. }
  607. block[j] = level;
  608. }
  609. CLOSE_READER(re, &s->gb);
  610. }
  611. s->block_last_index[n] = i;
  612. return 0;
  613. }
  614. int ff_mpeg1_decode_block_intra(MpegEncContext *s,
  615. DCTELEM *block,
  616. int n)
  617. {
  618. return mpeg1_decode_block_intra(s, block, n);
  619. }
  620. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  621. DCTELEM *block,
  622. int n)
  623. {
  624. int level, i, j, run;
  625. RLTable *rl = &ff_rl_mpeg1;
  626. uint8_t * const scantable= s->intra_scantable.permutated;
  627. const uint16_t *quant_matrix= s->inter_matrix;
  628. const int qscale= s->qscale;
  629. {
  630. OPEN_READER(re, &s->gb);
  631. i = -1;
  632. // special case for first coefficient, no need to add second VLC table
  633. UPDATE_CACHE(re, &s->gb);
  634. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  635. level= (3*qscale*quant_matrix[0])>>5;
  636. level= (level-1)|1;
  637. if(GET_CACHE(re, &s->gb)&0x40000000)
  638. level= -level;
  639. block[0] = level;
  640. i++;
  641. SKIP_BITS(re, &s->gb, 2);
  642. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  643. goto end;
  644. }
  645. /* now quantify & encode AC coefficients */
  646. for(;;) {
  647. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  648. if(level != 0) {
  649. i += run;
  650. j = scantable[i];
  651. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  652. level= (level-1)|1;
  653. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  654. SKIP_BITS(re, &s->gb, 1);
  655. } else {
  656. /* escape */
  657. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  658. UPDATE_CACHE(re, &s->gb);
  659. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  660. if (level == -128) {
  661. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  662. } else if (level == 0) {
  663. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  664. }
  665. i += run;
  666. j = scantable[i];
  667. if(level<0){
  668. level= -level;
  669. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  670. level= (level-1)|1;
  671. level= -level;
  672. }else{
  673. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  674. level= (level-1)|1;
  675. }
  676. }
  677. if (i > 63){
  678. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  679. return -1;
  680. }
  681. block[j] = level;
  682. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  683. break;
  684. UPDATE_CACHE(re, &s->gb);
  685. }
  686. end:
  687. LAST_SKIP_BITS(re, &s->gb, 2);
  688. CLOSE_READER(re, &s->gb);
  689. }
  690. s->block_last_index[n] = i;
  691. return 0;
  692. }
  693. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  694. {
  695. int level, i, j, run;
  696. RLTable *rl = &ff_rl_mpeg1;
  697. uint8_t * const scantable= s->intra_scantable.permutated;
  698. const int qscale= s->qscale;
  699. {
  700. OPEN_READER(re, &s->gb);
  701. i = -1;
  702. // special case for first coefficient, no need to add second VLC table
  703. UPDATE_CACHE(re, &s->gb);
  704. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  705. level= (3*qscale)>>1;
  706. level= (level-1)|1;
  707. if(GET_CACHE(re, &s->gb)&0x40000000)
  708. level= -level;
  709. block[0] = level;
  710. i++;
  711. SKIP_BITS(re, &s->gb, 2);
  712. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  713. goto end;
  714. }
  715. /* now quantify & encode AC coefficients */
  716. for(;;) {
  717. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  718. if(level != 0) {
  719. i += run;
  720. j = scantable[i];
  721. level= ((level*2+1)*qscale)>>1;
  722. level= (level-1)|1;
  723. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  724. SKIP_BITS(re, &s->gb, 1);
  725. } else {
  726. /* escape */
  727. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  728. UPDATE_CACHE(re, &s->gb);
  729. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  730. if (level == -128) {
  731. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  732. } else if (level == 0) {
  733. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  734. }
  735. i += run;
  736. j = scantable[i];
  737. if(level<0){
  738. level= -level;
  739. level= ((level*2+1)*qscale)>>1;
  740. level= (level-1)|1;
  741. level= -level;
  742. }else{
  743. level= ((level*2+1)*qscale)>>1;
  744. level= (level-1)|1;
  745. }
  746. }
  747. block[j] = level;
  748. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  749. break;
  750. UPDATE_CACHE(re, &s->gb);
  751. }
  752. end:
  753. LAST_SKIP_BITS(re, &s->gb, 2);
  754. CLOSE_READER(re, &s->gb);
  755. }
  756. s->block_last_index[n] = i;
  757. return 0;
  758. }
  759. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  760. DCTELEM *block,
  761. int n)
  762. {
  763. int level, i, j, run;
  764. RLTable *rl = &ff_rl_mpeg1;
  765. uint8_t * const scantable= s->intra_scantable.permutated;
  766. const uint16_t *quant_matrix;
  767. const int qscale= s->qscale;
  768. int mismatch;
  769. mismatch = 1;
  770. {
  771. OPEN_READER(re, &s->gb);
  772. i = -1;
  773. if (n < 4)
  774. quant_matrix = s->inter_matrix;
  775. else
  776. quant_matrix = s->chroma_inter_matrix;
  777. // special case for first coefficient, no need to add second VLC table
  778. UPDATE_CACHE(re, &s->gb);
  779. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  780. level= (3*qscale*quant_matrix[0])>>5;
  781. if(GET_CACHE(re, &s->gb)&0x40000000)
  782. level= -level;
  783. block[0] = level;
  784. mismatch ^= level;
  785. i++;
  786. SKIP_BITS(re, &s->gb, 2);
  787. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  788. goto end;
  789. }
  790. /* now quantify & encode AC coefficients */
  791. for(;;) {
  792. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  793. if(level != 0) {
  794. i += run;
  795. j = scantable[i];
  796. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  797. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  798. SKIP_BITS(re, &s->gb, 1);
  799. } else {
  800. /* escape */
  801. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  802. UPDATE_CACHE(re, &s->gb);
  803. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  804. i += run;
  805. j = scantable[i];
  806. if(level<0){
  807. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  808. level= -level;
  809. }else{
  810. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  811. }
  812. }
  813. if (i > 63){
  814. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  815. return -1;
  816. }
  817. mismatch ^= level;
  818. block[j] = level;
  819. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  820. break;
  821. UPDATE_CACHE(re, &s->gb);
  822. }
  823. end:
  824. LAST_SKIP_BITS(re, &s->gb, 2);
  825. CLOSE_READER(re, &s->gb);
  826. }
  827. block[63] ^= (mismatch & 1);
  828. s->block_last_index[n] = i;
  829. return 0;
  830. }
  831. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  832. DCTELEM *block,
  833. int n)
  834. {
  835. int level, i, j, run;
  836. RLTable *rl = &ff_rl_mpeg1;
  837. uint8_t * const scantable= s->intra_scantable.permutated;
  838. const int qscale= s->qscale;
  839. OPEN_READER(re, &s->gb);
  840. i = -1;
  841. // special case for first coefficient, no need to add second VLC table
  842. UPDATE_CACHE(re, &s->gb);
  843. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  844. level= (3*qscale)>>1;
  845. if(GET_CACHE(re, &s->gb)&0x40000000)
  846. level= -level;
  847. block[0] = level;
  848. i++;
  849. SKIP_BITS(re, &s->gb, 2);
  850. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  851. goto end;
  852. }
  853. /* now quantify & encode AC coefficients */
  854. for(;;) {
  855. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  856. if(level != 0) {
  857. i += run;
  858. j = scantable[i];
  859. level= ((level*2+1)*qscale)>>1;
  860. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  861. SKIP_BITS(re, &s->gb, 1);
  862. } else {
  863. /* escape */
  864. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  865. UPDATE_CACHE(re, &s->gb);
  866. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  867. i += run;
  868. j = scantable[i];
  869. if(level<0){
  870. level= ((-level*2+1)*qscale)>>1;
  871. level= -level;
  872. }else{
  873. level= ((level*2+1)*qscale)>>1;
  874. }
  875. }
  876. block[j] = level;
  877. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  878. break;
  879. UPDATE_CACHE(re, &s->gb);
  880. }
  881. end:
  882. LAST_SKIP_BITS(re, &s->gb, 2);
  883. CLOSE_READER(re, &s->gb);
  884. s->block_last_index[n] = i;
  885. return 0;
  886. }
  887. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  888. DCTELEM *block,
  889. int n)
  890. {
  891. int level, dc, diff, i, j, run;
  892. int component;
  893. RLTable *rl;
  894. uint8_t * const scantable= s->intra_scantable.permutated;
  895. const uint16_t *quant_matrix;
  896. const int qscale= s->qscale;
  897. int mismatch;
  898. /* DC coefficient */
  899. if (n < 4){
  900. quant_matrix = s->intra_matrix;
  901. component = 0;
  902. }else{
  903. quant_matrix = s->chroma_intra_matrix;
  904. component = (n&1) + 1;
  905. }
  906. diff = decode_dc(&s->gb, component);
  907. if (diff >= 0xffff)
  908. return -1;
  909. dc = s->last_dc[component];
  910. dc += diff;
  911. s->last_dc[component] = dc;
  912. block[0] = dc << (3 - s->intra_dc_precision);
  913. av_dlog(s->avctx, "dc=%d\n", block[0]);
  914. mismatch = block[0] ^ 1;
  915. i = 0;
  916. if (s->intra_vlc_format)
  917. rl = &ff_rl_mpeg2;
  918. else
  919. rl = &ff_rl_mpeg1;
  920. {
  921. OPEN_READER(re, &s->gb);
  922. /* now quantify & encode AC coefficients */
  923. for(;;) {
  924. UPDATE_CACHE(re, &s->gb);
  925. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  926. if(level == 127){
  927. break;
  928. } else if(level != 0) {
  929. i += run;
  930. j = scantable[i];
  931. level= (level*qscale*quant_matrix[j])>>4;
  932. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  933. LAST_SKIP_BITS(re, &s->gb, 1);
  934. } else {
  935. /* escape */
  936. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  937. UPDATE_CACHE(re, &s->gb);
  938. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  939. i += run;
  940. j = scantable[i];
  941. if(level<0){
  942. level= (-level*qscale*quant_matrix[j])>>4;
  943. level= -level;
  944. }else{
  945. level= (level*qscale*quant_matrix[j])>>4;
  946. }
  947. }
  948. if (i > 63){
  949. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  950. return -1;
  951. }
  952. mismatch^= level;
  953. block[j] = level;
  954. }
  955. CLOSE_READER(re, &s->gb);
  956. }
  957. block[63]^= mismatch&1;
  958. s->block_last_index[n] = i;
  959. return 0;
  960. }
  961. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
  962. DCTELEM *block,
  963. int n)
  964. {
  965. int level, dc, diff, j, run;
  966. int component;
  967. RLTable *rl;
  968. uint8_t * scantable= s->intra_scantable.permutated;
  969. const uint16_t *quant_matrix;
  970. const int qscale= s->qscale;
  971. /* DC coefficient */
  972. if (n < 4){
  973. quant_matrix = s->intra_matrix;
  974. component = 0;
  975. }else{
  976. quant_matrix = s->chroma_intra_matrix;
  977. component = (n&1) + 1;
  978. }
  979. diff = decode_dc(&s->gb, component);
  980. if (diff >= 0xffff)
  981. return -1;
  982. dc = s->last_dc[component];
  983. dc += diff;
  984. s->last_dc[component] = dc;
  985. block[0] = dc << (3 - s->intra_dc_precision);
  986. if (s->intra_vlc_format)
  987. rl = &ff_rl_mpeg2;
  988. else
  989. rl = &ff_rl_mpeg1;
  990. {
  991. OPEN_READER(re, &s->gb);
  992. /* now quantify & encode AC coefficients */
  993. for(;;) {
  994. UPDATE_CACHE(re, &s->gb);
  995. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  996. if(level == 127){
  997. break;
  998. } else if(level != 0) {
  999. scantable += run;
  1000. j = *scantable;
  1001. level= (level*qscale*quant_matrix[j])>>4;
  1002. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1003. LAST_SKIP_BITS(re, &s->gb, 1);
  1004. } else {
  1005. /* escape */
  1006. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1007. UPDATE_CACHE(re, &s->gb);
  1008. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1009. scantable += run;
  1010. j = *scantable;
  1011. if(level<0){
  1012. level= (-level*qscale*quant_matrix[j])>>4;
  1013. level= -level;
  1014. }else{
  1015. level= (level*qscale*quant_matrix[j])>>4;
  1016. }
  1017. }
  1018. block[j] = level;
  1019. }
  1020. CLOSE_READER(re, &s->gb);
  1021. }
  1022. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  1023. return 0;
  1024. }
  1025. typedef struct Mpeg1Context {
  1026. MpegEncContext mpeg_enc_ctx;
  1027. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1028. int repeat_field; /* true if we must repeat the field */
  1029. AVPanScan pan_scan; /**< some temporary storage for the panscan */
  1030. int slice_count;
  1031. int swap_uv;//indicate VCR2
  1032. int save_aspect_info;
  1033. int save_width, save_height, save_progressive_seq;
  1034. AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
  1035. int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
  1036. } Mpeg1Context;
  1037. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  1038. {
  1039. Mpeg1Context *s = avctx->priv_data;
  1040. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1041. int i;
  1042. /* we need some permutation to store matrices,
  1043. * until MPV_common_init() sets the real permutation. */
  1044. for(i=0;i<64;i++)
  1045. s2->dsp.idct_permutation[i]=i;
  1046. MPV_decode_defaults(s2);
  1047. s->mpeg_enc_ctx.avctx= avctx;
  1048. s->mpeg_enc_ctx.flags= avctx->flags;
  1049. s->mpeg_enc_ctx.flags2= avctx->flags2;
  1050. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1051. ff_mpeg12_init_vlcs();
  1052. s->mpeg_enc_ctx_allocated = 0;
  1053. s->mpeg_enc_ctx.picture_number = 0;
  1054. s->repeat_field = 0;
  1055. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1056. avctx->color_range= AVCOL_RANGE_MPEG;
  1057. if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
  1058. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1059. else
  1060. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1061. return 0;
  1062. }
  1063. static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
  1064. {
  1065. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1066. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1067. int err;
  1068. if(avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
  1069. return 0;
  1070. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1071. if(err) return err;
  1072. if(!ctx->mpeg_enc_ctx_allocated)
  1073. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  1074. if(!(s->pict_type == FF_B_TYPE || s->low_delay))
  1075. s->picture_number++;
  1076. return 0;
  1077. }
  1078. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1079. const uint8_t *new_perm){
  1080. uint16_t temp_matrix[64];
  1081. int i;
  1082. memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
  1083. for(i=0;i<64;i++){
  1084. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1085. }
  1086. }
  1087. static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
  1088. Mpeg1Context *s1 = avctx->priv_data;
  1089. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1090. if(avctx->xvmc_acceleration)
  1091. return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1092. else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  1093. if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
  1094. return PIX_FMT_VDPAU_MPEG1;
  1095. else
  1096. return PIX_FMT_VDPAU_MPEG2;
  1097. }else{
  1098. if(s->chroma_format < 2)
  1099. return avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
  1100. else if(s->chroma_format == 2)
  1101. return PIX_FMT_YUV422P;
  1102. else
  1103. return PIX_FMT_YUV444P;
  1104. }
  1105. }
  1106. /* Call this function when we know all parameters.
  1107. * It may be called in different places for MPEG-1 and MPEG-2. */
  1108. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1109. Mpeg1Context *s1 = avctx->priv_data;
  1110. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1111. uint8_t old_permutation[64];
  1112. if (
  1113. (s1->mpeg_enc_ctx_allocated == 0)||
  1114. avctx->coded_width != s->width ||
  1115. avctx->coded_height != s->height||
  1116. s1->save_width != s->width ||
  1117. s1->save_height != s->height ||
  1118. s1->save_aspect_info != s->aspect_ratio_info||
  1119. s1->save_progressive_seq != s->progressive_sequence ||
  1120. 0)
  1121. {
  1122. if (s1->mpeg_enc_ctx_allocated) {
  1123. ParseContext pc= s->parse_context;
  1124. s->parse_context.buffer=0;
  1125. MPV_common_end(s);
  1126. s->parse_context= pc;
  1127. }
  1128. if( (s->width == 0 )||(s->height == 0))
  1129. return -2;
  1130. avcodec_set_dimensions(avctx, s->width, s->height);
  1131. avctx->bit_rate = s->bit_rate;
  1132. s1->save_aspect_info = s->aspect_ratio_info;
  1133. s1->save_width = s->width;
  1134. s1->save_height = s->height;
  1135. s1->save_progressive_seq = s->progressive_sequence;
  1136. /* low_delay may be forced, in this case we will have B-frames
  1137. * that behave like P-frames. */
  1138. avctx->has_b_frames = !(s->low_delay);
  1139. assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
  1140. if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
  1141. //MPEG-1 fps
  1142. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1143. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1144. //MPEG-1 aspect
  1145. avctx->sample_aspect_ratio= av_d2q(
  1146. 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1147. avctx->ticks_per_frame=1;
  1148. }else{//MPEG-2
  1149. //MPEG-2 fps
  1150. av_reduce(
  1151. &s->avctx->time_base.den,
  1152. &s->avctx->time_base.num,
  1153. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
  1154. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1155. 1<<30);
  1156. avctx->ticks_per_frame=2;
  1157. //MPEG-2 aspect
  1158. if(s->aspect_ratio_info > 1){
  1159. AVRational dar =
  1160. av_mul_q(
  1161. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1162. (AVRational){s1->pan_scan.width, s1->pan_scan.height}),
  1163. (AVRational){s->width, s->height});
  1164. // we ignore the spec here and guess a bit as reality does not match the spec, see for example
  1165. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1166. // issue1613, 621, 562
  1167. if((s1->pan_scan.width == 0 ) || (s1->pan_scan.height == 0) ||
  1168. (av_cmp_q(dar,(AVRational){4,3}) && av_cmp_q(dar,(AVRational){16,9}))) {
  1169. s->avctx->sample_aspect_ratio=
  1170. av_div_q(
  1171. ff_mpeg2_aspect[s->aspect_ratio_info],
  1172. (AVRational){s->width, s->height}
  1173. );
  1174. }else{
  1175. s->avctx->sample_aspect_ratio=
  1176. av_div_q(
  1177. ff_mpeg2_aspect[s->aspect_ratio_info],
  1178. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1179. );
  1180. //issue1613 4/3 16/9 -> 16/9
  1181. //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1182. //widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1183. // s->avctx->sample_aspect_ratio= av_mul_q(s->avctx->sample_aspect_ratio, (AVRational){s->width, s->height});
  1184. //av_log(NULL, AV_LOG_ERROR, "A %d/%d\n",ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1185. //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n",s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
  1186. }
  1187. }else{
  1188. s->avctx->sample_aspect_ratio=
  1189. ff_mpeg2_aspect[s->aspect_ratio_info];
  1190. }
  1191. }//MPEG-2
  1192. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1193. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1194. //until then pix_fmt may be changed right after codec init
  1195. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
  1196. avctx->hwaccel ||
  1197. s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
  1198. if( avctx->idct_algo == FF_IDCT_AUTO )
  1199. avctx->idct_algo = FF_IDCT_SIMPLE;
  1200. /* Quantization matrices may need reordering
  1201. * if DCT permutation is changed. */
  1202. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1203. if (MPV_common_init(s) < 0)
  1204. return -2;
  1205. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1206. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1207. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1208. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1209. s1->mpeg_enc_ctx_allocated = 1;
  1210. }
  1211. return 0;
  1212. }
  1213. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1214. const uint8_t *buf, int buf_size)
  1215. {
  1216. Mpeg1Context *s1 = avctx->priv_data;
  1217. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1218. int ref, f_code, vbv_delay;
  1219. init_get_bits(&s->gb, buf, buf_size*8);
  1220. ref = get_bits(&s->gb, 10); /* temporal ref */
  1221. s->pict_type = get_bits(&s->gb, 3);
  1222. if(s->pict_type == 0 || s->pict_type > 3)
  1223. return -1;
  1224. vbv_delay= get_bits(&s->gb, 16);
  1225. if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
  1226. s->full_pel[0] = get_bits1(&s->gb);
  1227. f_code = get_bits(&s->gb, 3);
  1228. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1229. return -1;
  1230. s->mpeg_f_code[0][0] = f_code;
  1231. s->mpeg_f_code[0][1] = f_code;
  1232. }
  1233. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1234. s->full_pel[1] = get_bits1(&s->gb);
  1235. f_code = get_bits(&s->gb, 3);
  1236. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1237. return -1;
  1238. s->mpeg_f_code[1][0] = f_code;
  1239. s->mpeg_f_code[1][1] = f_code;
  1240. }
  1241. s->current_picture.pict_type= s->pict_type;
  1242. s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
  1243. if(avctx->debug & FF_DEBUG_PICT_INFO)
  1244. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1245. s->y_dc_scale = 8;
  1246. s->c_dc_scale = 8;
  1247. return 0;
  1248. }
  1249. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1250. {
  1251. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1252. int horiz_size_ext, vert_size_ext;
  1253. int bit_rate_ext;
  1254. skip_bits(&s->gb, 1); /* profile and level esc*/
  1255. s->avctx->profile= get_bits(&s->gb, 3);
  1256. s->avctx->level= get_bits(&s->gb, 4);
  1257. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1258. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1259. horiz_size_ext = get_bits(&s->gb, 2);
  1260. vert_size_ext = get_bits(&s->gb, 2);
  1261. s->width |= (horiz_size_ext << 12);
  1262. s->height |= (vert_size_ext << 12);
  1263. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1264. s->bit_rate += (bit_rate_ext << 18) * 400;
  1265. skip_bits1(&s->gb); /* marker */
  1266. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  1267. s->low_delay = get_bits1(&s->gb);
  1268. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1269. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  1270. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  1271. av_dlog(s->avctx, "sequence extension\n");
  1272. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1273. s->avctx->sub_id = 2; /* indicates MPEG-2 found */
  1274. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1275. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1276. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1277. }
  1278. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1279. {
  1280. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1281. int color_description, w, h;
  1282. skip_bits(&s->gb, 3); /* video format */
  1283. color_description= get_bits1(&s->gb);
  1284. if(color_description){
  1285. s->avctx->color_primaries= get_bits(&s->gb, 8);
  1286. s->avctx->color_trc = get_bits(&s->gb, 8);
  1287. s->avctx->colorspace = get_bits(&s->gb, 8);
  1288. }
  1289. w= get_bits(&s->gb, 14);
  1290. skip_bits(&s->gb, 1); //marker
  1291. h= get_bits(&s->gb, 14);
  1292. // remaining 3 bits are zero padding
  1293. s1->pan_scan.width= 16*w;
  1294. s1->pan_scan.height=16*h;
  1295. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1296. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1297. }
  1298. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1299. {
  1300. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1301. int i,nofco;
  1302. nofco = 1;
  1303. if(s->progressive_sequence){
  1304. if(s->repeat_first_field){
  1305. nofco++;
  1306. if(s->top_field_first)
  1307. nofco++;
  1308. }
  1309. }else{
  1310. if(s->picture_structure == PICT_FRAME){
  1311. nofco++;
  1312. if(s->repeat_first_field)
  1313. nofco++;
  1314. }
  1315. }
  1316. for(i=0; i<nofco; i++){
  1317. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1318. skip_bits(&s->gb, 1); //marker
  1319. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1320. skip_bits(&s->gb, 1); //marker
  1321. }
  1322. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1323. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1324. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1325. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1326. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1327. );
  1328. }
  1329. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
  1330. int i;
  1331. for(i=0; i<64; i++) {
  1332. int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1333. int v = get_bits(&s->gb, 8);
  1334. if(v==0){
  1335. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1336. return -1;
  1337. }
  1338. if(intra && i==0 && v!=8){
  1339. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1340. v= 8; // needed by pink.mpg / issue1046
  1341. }
  1342. matrix0[j] = v;
  1343. if(matrix1)
  1344. matrix1[j] = v;
  1345. }
  1346. return 0;
  1347. }
  1348. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1349. {
  1350. av_dlog(s->avctx, "matrix extension\n");
  1351. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1352. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1353. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
  1354. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
  1355. }
  1356. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1357. {
  1358. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1359. s->full_pel[0] = s->full_pel[1] = 0;
  1360. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1361. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1362. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1363. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1364. if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
  1365. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1366. if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
  1367. if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1368. s->pict_type= AV_PICTURE_TYPE_I;
  1369. else
  1370. s->pict_type= AV_PICTURE_TYPE_P;
  1371. }else
  1372. s->pict_type= AV_PICTURE_TYPE_B;
  1373. s->current_picture.pict_type= s->pict_type;
  1374. s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
  1375. }
  1376. s->intra_dc_precision = get_bits(&s->gb, 2);
  1377. s->picture_structure = get_bits(&s->gb, 2);
  1378. s->top_field_first = get_bits1(&s->gb);
  1379. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1380. s->concealment_motion_vectors = get_bits1(&s->gb);
  1381. s->q_scale_type = get_bits1(&s->gb);
  1382. s->intra_vlc_format = get_bits1(&s->gb);
  1383. s->alternate_scan = get_bits1(&s->gb);
  1384. s->repeat_first_field = get_bits1(&s->gb);
  1385. s->chroma_420_type = get_bits1(&s->gb);
  1386. s->progressive_frame = get_bits1(&s->gb);
  1387. if(s->progressive_sequence && !s->progressive_frame){
  1388. s->progressive_frame= 1;
  1389. av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
  1390. }
  1391. if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
  1392. av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
  1393. s->picture_structure= PICT_FRAME;
  1394. }
  1395. if(s->progressive_sequence && !s->frame_pred_frame_dct){
  1396. av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
  1397. s->frame_pred_frame_dct= 1;
  1398. }
  1399. if(s->picture_structure == PICT_FRAME){
  1400. s->first_field=0;
  1401. s->v_edge_pos= 16*s->mb_height;
  1402. }else{
  1403. s->first_field ^= 1;
  1404. s->v_edge_pos= 8*s->mb_height;
  1405. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1406. }
  1407. if(s->alternate_scan){
  1408. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1409. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1410. }else{
  1411. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1412. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1413. }
  1414. /* composite display not parsed */
  1415. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1416. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1417. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1418. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1419. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1420. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1421. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1422. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1423. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1424. }
  1425. static void exchange_uv(MpegEncContext *s){
  1426. DCTELEM (*tmp)[64];
  1427. tmp = s->pblocks[4];
  1428. s->pblocks[4] = s->pblocks[5];
  1429. s->pblocks[5] = tmp;
  1430. }
  1431. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
  1432. AVCodecContext *avctx= s->avctx;
  1433. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1434. /* start frame decoding */
  1435. if(s->first_field || s->picture_structure==PICT_FRAME){
  1436. if(MPV_frame_start(s, avctx) < 0)
  1437. return -1;
  1438. ff_er_frame_start(s);
  1439. /* first check if we must repeat the frame */
  1440. s->current_picture_ptr->repeat_pict = 0;
  1441. if (s->repeat_first_field) {
  1442. if (s->progressive_sequence) {
  1443. if (s->top_field_first)
  1444. s->current_picture_ptr->repeat_pict = 4;
  1445. else
  1446. s->current_picture_ptr->repeat_pict = 2;
  1447. } else if (s->progressive_frame) {
  1448. s->current_picture_ptr->repeat_pict = 1;
  1449. }
  1450. }
  1451. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1452. if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1453. ff_thread_finish_setup(avctx);
  1454. }else{ //second field
  1455. int i;
  1456. if(!s->current_picture_ptr){
  1457. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1458. return -1;
  1459. }
  1460. for(i=0; i<4; i++){
  1461. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1462. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1463. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1464. }
  1465. }
  1466. }
  1467. if (avctx->hwaccel) {
  1468. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1469. return -1;
  1470. }
  1471. // MPV_frame_start will call this function too,
  1472. // but we need to call it on every field
  1473. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1474. if(ff_xvmc_field_start(s,avctx) < 0)
  1475. return -1;
  1476. return 0;
  1477. }
  1478. #define DECODE_SLICE_ERROR -1
  1479. #define DECODE_SLICE_OK 0
  1480. /**
  1481. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1482. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1483. * DECODE_SLICE_OK if this slice is ok<br>
  1484. */
  1485. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1486. const uint8_t **buf, int buf_size)
  1487. {
  1488. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1489. AVCodecContext *avctx= s->avctx;
  1490. const int field_pic= s->picture_structure != PICT_FRAME;
  1491. const int lowres= s->avctx->lowres;
  1492. s->resync_mb_x=
  1493. s->resync_mb_y= -1;
  1494. assert(mb_y < s->mb_height);
  1495. init_get_bits(&s->gb, *buf, buf_size*8);
  1496. ff_mpeg1_clean_buffers(s);
  1497. s->interlaced_dct = 0;
  1498. s->qscale = get_qscale(s);
  1499. if(s->qscale == 0){
  1500. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1501. return -1;
  1502. }
  1503. /* extra slice info */
  1504. while (get_bits1(&s->gb) != 0) {
  1505. skip_bits(&s->gb, 8);
  1506. }
  1507. s->mb_x=0;
  1508. if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
  1509. skip_bits1(&s->gb);
  1510. }else{
  1511. for(;;) {
  1512. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1513. if (code < 0){
  1514. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1515. return -1;
  1516. }
  1517. if (code >= 33) {
  1518. if (code == 33) {
  1519. s->mb_x += 33;
  1520. }
  1521. /* otherwise, stuffing, nothing to do */
  1522. } else {
  1523. s->mb_x += code;
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. if(s->mb_x >= (unsigned)s->mb_width){
  1529. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1530. return -1;
  1531. }
  1532. if (avctx->hwaccel) {
  1533. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1534. int start_code = -1;
  1535. buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1536. if (buf_end < *buf + buf_size)
  1537. buf_end -= 4;
  1538. s->mb_y = mb_y;
  1539. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1540. return DECODE_SLICE_ERROR;
  1541. *buf = buf_end;
  1542. return DECODE_SLICE_OK;
  1543. }
  1544. s->resync_mb_x= s->mb_x;
  1545. s->resync_mb_y= s->mb_y= mb_y;
  1546. s->mb_skip_run= 0;
  1547. ff_init_block_index(s);
  1548. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  1549. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1550. 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",
  1551. 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],
  1552. s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1553. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1554. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1555. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1556. }
  1557. }
  1558. for(;;) {
  1559. //If 1, we memcpy blocks in xvmcvideo.
  1560. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1561. ff_xvmc_init_block(s);//set s->block
  1562. if(mpeg_decode_mb(s, s->block) < 0)
  1563. return -1;
  1564. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  1565. const int wrap = s->b8_stride;
  1566. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  1567. int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
  1568. int motion_x, motion_y, dir, i;
  1569. for(i=0; i<2; i++){
  1570. for(dir=0; dir<2; dir++){
  1571. if (s->mb_intra || (dir==1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1572. motion_x = motion_y = 0;
  1573. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  1574. motion_x = s->mv[dir][0][0];
  1575. motion_y = s->mv[dir][0][1];
  1576. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1577. motion_x = s->mv[dir][i][0];
  1578. motion_y = s->mv[dir][i][1];
  1579. }
  1580. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  1581. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  1582. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1583. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1584. s->current_picture.ref_index [dir][b8_xy ]=
  1585. s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
  1586. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  1587. }
  1588. xy += wrap;
  1589. b8_xy +=2;
  1590. }
  1591. }
  1592. s->dest[0] += 16 >> lowres;
  1593. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1594. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1595. MPV_decode_mb(s, s->block);
  1596. if (++s->mb_x >= s->mb_width) {
  1597. const int mb_size= 16>>s->avctx->lowres;
  1598. ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
  1599. MPV_report_decode_progress(s);
  1600. s->mb_x = 0;
  1601. s->mb_y += 1<<field_pic;
  1602. if(s->mb_y >= s->mb_height){
  1603. int left= get_bits_left(&s->gb);
  1604. int is_d10= s->chroma_format==2 && s->pict_type==AV_PICTURE_TYPE_I && avctx->profile==0 && avctx->level==5
  1605. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1606. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1607. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1608. || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
  1609. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1610. return -1;
  1611. }else
  1612. goto eos;
  1613. }
  1614. ff_init_block_index(s);
  1615. }
  1616. /* skip mb handling */
  1617. if (s->mb_skip_run == -1) {
  1618. /* read increment again */
  1619. s->mb_skip_run = 0;
  1620. for(;;) {
  1621. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1622. if (code < 0){
  1623. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1624. return -1;
  1625. }
  1626. if (code >= 33) {
  1627. if (code == 33) {
  1628. s->mb_skip_run += 33;
  1629. }else if(code == 35){
  1630. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  1631. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1632. return -1;
  1633. }
  1634. goto eos; /* end of slice */
  1635. }
  1636. /* otherwise, stuffing, nothing to do */
  1637. } else {
  1638. s->mb_skip_run += code;
  1639. break;
  1640. }
  1641. }
  1642. if(s->mb_skip_run){
  1643. int i;
  1644. if(s->pict_type == AV_PICTURE_TYPE_I){
  1645. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1646. return -1;
  1647. }
  1648. /* skip mb */
  1649. s->mb_intra = 0;
  1650. for(i=0;i<12;i++)
  1651. s->block_last_index[i] = -1;
  1652. if(s->picture_structure == PICT_FRAME)
  1653. s->mv_type = MV_TYPE_16X16;
  1654. else
  1655. s->mv_type = MV_TYPE_FIELD;
  1656. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1657. /* if P type, zero motion vector is implied */
  1658. s->mv_dir = MV_DIR_FORWARD;
  1659. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1660. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1661. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1662. s->field_select[0][0]= (s->picture_structure - 1) & 1;
  1663. } else {
  1664. /* if B type, reuse previous vectors and directions */
  1665. s->mv[0][0][0] = s->last_mv[0][0][0];
  1666. s->mv[0][0][1] = s->last_mv[0][0][1];
  1667. s->mv[1][0][0] = s->last_mv[1][0][0];
  1668. s->mv[1][0][1] = s->last_mv[1][0][1];
  1669. }
  1670. }
  1671. }
  1672. }
  1673. eos: // end of slice
  1674. *buf += (get_bits_count(&s->gb)-1)/8;
  1675. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1676. return 0;
  1677. }
  1678. static int slice_decode_thread(AVCodecContext *c, void *arg){
  1679. MpegEncContext *s= *(void**)arg;
  1680. const uint8_t *buf= s->gb.buffer;
  1681. int mb_y= s->start_mb_y;
  1682. const int field_pic= s->picture_structure != PICT_FRAME;
  1683. s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
  1684. for(;;){
  1685. uint32_t start_code;
  1686. int ret;
  1687. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  1688. emms_c();
  1689. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1690. //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);
  1691. if(ret < 0){
  1692. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  1693. 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);
  1694. }else{
  1695. 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);
  1696. }
  1697. if(s->mb_y == s->end_mb_y)
  1698. return 0;
  1699. start_code= -1;
  1700. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  1701. mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
  1702. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1703. mb_y++;
  1704. if(mb_y < 0 || mb_y >= s->end_mb_y)
  1705. return -1;
  1706. }
  1707. }
  1708. /**
  1709. * Handle slice ends.
  1710. * @return 1 if it seems to be the last slice
  1711. */
  1712. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1713. {
  1714. Mpeg1Context *s1 = avctx->priv_data;
  1715. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1716. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1717. return 0;
  1718. if (s->avctx->hwaccel) {
  1719. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1720. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1721. }
  1722. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1723. ff_xvmc_field_end(s);
  1724. /* end of slice reached */
  1725. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  1726. /* end of image */
  1727. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  1728. ff_er_frame_end(s);
  1729. MPV_frame_end(s);
  1730. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1731. *pict= *(AVFrame*)s->current_picture_ptr;
  1732. ff_print_debug_info(s, pict);
  1733. } else {
  1734. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1735. s->picture_number++;
  1736. /* latency of 1 frame for I- and P-frames */
  1737. /* XXX: use another variable than picture_number */
  1738. if (s->last_picture_ptr != NULL) {
  1739. *pict= *(AVFrame*)s->last_picture_ptr;
  1740. ff_print_debug_info(s, pict);
  1741. }
  1742. }
  1743. return 1;
  1744. } else {
  1745. return 0;
  1746. }
  1747. }
  1748. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1749. const uint8_t *buf, int buf_size)
  1750. {
  1751. Mpeg1Context *s1 = avctx->priv_data;
  1752. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1753. int width,height;
  1754. int i, v, j;
  1755. init_get_bits(&s->gb, buf, buf_size*8);
  1756. width = get_bits(&s->gb, 12);
  1757. height = get_bits(&s->gb, 12);
  1758. if (width <= 0 || height <= 0)
  1759. return -1;
  1760. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1761. if (s->aspect_ratio_info == 0) {
  1762. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1763. if (avctx->error_recognition >= FF_ER_COMPLIANT)
  1764. return -1;
  1765. }
  1766. s->frame_rate_index = get_bits(&s->gb, 4);
  1767. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1768. return -1;
  1769. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1770. if (get_bits1(&s->gb) == 0) /* marker */
  1771. return -1;
  1772. s->width = width;
  1773. s->height = height;
  1774. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  1775. skip_bits(&s->gb, 1);
  1776. /* get matrix */
  1777. if (get_bits1(&s->gb)) {
  1778. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1779. } else {
  1780. for(i=0;i<64;i++) {
  1781. j = s->dsp.idct_permutation[i];
  1782. v = ff_mpeg1_default_intra_matrix[i];
  1783. s->intra_matrix[j] = v;
  1784. s->chroma_intra_matrix[j] = v;
  1785. }
  1786. }
  1787. if (get_bits1(&s->gb)) {
  1788. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1789. } else {
  1790. for(i=0;i<64;i++) {
  1791. int j= s->dsp.idct_permutation[i];
  1792. v = ff_mpeg1_default_non_intra_matrix[i];
  1793. s->inter_matrix[j] = v;
  1794. s->chroma_inter_matrix[j] = v;
  1795. }
  1796. }
  1797. if(show_bits(&s->gb, 23) != 0){
  1798. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1799. return -1;
  1800. }
  1801. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1802. s->progressive_sequence = 1;
  1803. s->progressive_frame = 1;
  1804. s->picture_structure = PICT_FRAME;
  1805. s->frame_pred_frame_dct = 1;
  1806. s->chroma_format = 1;
  1807. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  1808. avctx->sub_id = 1; /* indicates MPEG-1 */
  1809. s->out_format = FMT_MPEG1;
  1810. s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
  1811. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1812. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1813. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1814. s->avctx->rc_buffer_size, s->bit_rate);
  1815. return 0;
  1816. }
  1817. static int vcr2_init_sequence(AVCodecContext *avctx)
  1818. {
  1819. Mpeg1Context *s1 = avctx->priv_data;
  1820. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1821. int i, v;
  1822. /* start new MPEG-1 context decoding */
  1823. s->out_format = FMT_MPEG1;
  1824. if (s1->mpeg_enc_ctx_allocated) {
  1825. MPV_common_end(s);
  1826. }
  1827. s->width = avctx->coded_width;
  1828. s->height = avctx->coded_height;
  1829. avctx->has_b_frames= 0; //true?
  1830. s->low_delay= 1;
  1831. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1832. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1833. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
  1834. s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
  1835. if( avctx->idct_algo == FF_IDCT_AUTO )
  1836. avctx->idct_algo = FF_IDCT_SIMPLE;
  1837. if (MPV_common_init(s) < 0)
  1838. return -1;
  1839. exchange_uv(s);//common init reset pblocks, so we swap them here
  1840. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  1841. s1->mpeg_enc_ctx_allocated = 1;
  1842. for(i=0;i<64;i++) {
  1843. int j= s->dsp.idct_permutation[i];
  1844. v = ff_mpeg1_default_intra_matrix[i];
  1845. s->intra_matrix[j] = v;
  1846. s->chroma_intra_matrix[j] = v;
  1847. v = ff_mpeg1_default_non_intra_matrix[i];
  1848. s->inter_matrix[j] = v;
  1849. s->chroma_inter_matrix[j] = v;
  1850. }
  1851. s->progressive_sequence = 1;
  1852. s->progressive_frame = 1;
  1853. s->picture_structure = PICT_FRAME;
  1854. s->frame_pred_frame_dct = 1;
  1855. s->chroma_format = 1;
  1856. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1857. avctx->sub_id = 2; /* indicates MPEG-2 */
  1858. s1->save_width = s->width;
  1859. s1->save_height = s->height;
  1860. s1->save_progressive_seq = s->progressive_sequence;
  1861. return 0;
  1862. }
  1863. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1864. const uint8_t *p, int buf_size)
  1865. {
  1866. const uint8_t *buf_end = p+buf_size;
  1867. /* we parse the DTG active format information */
  1868. if (buf_end - p >= 5 &&
  1869. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1870. int flags = p[4];
  1871. p += 5;
  1872. if (flags & 0x80) {
  1873. /* skip event id */
  1874. p += 2;
  1875. }
  1876. if (flags & 0x40) {
  1877. if (buf_end - p < 1)
  1878. return;
  1879. avctx->dtg_active_format = p[0] & 0x0f;
  1880. }
  1881. }
  1882. }
  1883. static void mpeg_decode_gop(AVCodecContext *avctx,
  1884. const uint8_t *buf, int buf_size){
  1885. Mpeg1Context *s1 = avctx->priv_data;
  1886. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1887. int time_code_hours, time_code_minutes;
  1888. int time_code_seconds, time_code_pictures;
  1889. int broken_link;
  1890. init_get_bits(&s->gb, buf, buf_size*8);
  1891. skip_bits1(&s->gb); /* drop_frame_flag */
  1892. time_code_hours=get_bits(&s->gb,5);
  1893. time_code_minutes = get_bits(&s->gb,6);
  1894. skip_bits1(&s->gb);//marker bit
  1895. time_code_seconds = get_bits(&s->gb,6);
  1896. time_code_pictures = get_bits(&s->gb,6);
  1897. s->closed_gop = get_bits1(&s->gb);
  1898. /*broken_link indicate that after editing the
  1899. reference frames of the first B-Frames after GOP I-Frame
  1900. are missing (open gop)*/
  1901. broken_link = get_bits1(&s->gb);
  1902. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1903. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1904. time_code_hours, time_code_minutes, time_code_seconds,
  1905. time_code_pictures, s->closed_gop, broken_link);
  1906. }
  1907. /**
  1908. * Find the end of the current frame in the bitstream.
  1909. * @return the position of the first byte of the next frame, or -1
  1910. */
  1911. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
  1912. {
  1913. int i;
  1914. uint32_t state= pc->state;
  1915. /* EOF considered as end of frame */
  1916. if (buf_size == 0)
  1917. return 0;
  1918. /*
  1919. 0 frame start -> 1/4
  1920. 1 first_SEQEXT -> 0/2
  1921. 2 first field start -> 3/0
  1922. 3 second_SEQEXT -> 2/0
  1923. 4 searching end
  1924. */
  1925. for(i=0; i<buf_size; i++){
  1926. assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
  1927. if(pc->frame_start_found&1){
  1928. if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
  1929. pc->frame_start_found--;
  1930. else if(state == EXT_START_CODE+2){
  1931. if((buf[i]&3) == 3) pc->frame_start_found= 0;
  1932. else pc->frame_start_found= (pc->frame_start_found+1)&3;
  1933. }
  1934. state++;
  1935. }else{
  1936. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  1937. if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  1938. i++;
  1939. pc->frame_start_found=4;
  1940. }
  1941. if(state == SEQ_END_CODE){
  1942. pc->state=-1;
  1943. return i+1;
  1944. }
  1945. if(pc->frame_start_found==2 && state == SEQ_START_CODE)
  1946. pc->frame_start_found= 0;
  1947. if(pc->frame_start_found<4 && state == EXT_START_CODE)
  1948. pc->frame_start_found++;
  1949. if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
  1950. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  1951. pc->frame_start_found=0;
  1952. pc->state=-1;
  1953. return i-3;
  1954. }
  1955. }
  1956. if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
  1957. ff_fetch_timestamp(s, i-3, 1);
  1958. }
  1959. }
  1960. }
  1961. pc->state= state;
  1962. return END_NOT_FOUND;
  1963. }
  1964. static int decode_chunks(AVCodecContext *avctx,
  1965. AVFrame *picture, int *data_size,
  1966. const uint8_t *buf, int buf_size);
  1967. /* handle buffering and image synchronisation */
  1968. static int mpeg_decode_frame(AVCodecContext *avctx,
  1969. void *data, int *data_size,
  1970. AVPacket *avpkt)
  1971. {
  1972. const uint8_t *buf = avpkt->data;
  1973. int buf_size = avpkt->size;
  1974. Mpeg1Context *s = avctx->priv_data;
  1975. AVFrame *picture = data;
  1976. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1977. av_dlog(avctx, "fill_buffer\n");
  1978. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  1979. /* special case for last picture */
  1980. if (s2->low_delay==0 && s2->next_picture_ptr) {
  1981. *picture= *(AVFrame*)s2->next_picture_ptr;
  1982. s2->next_picture_ptr= NULL;
  1983. *data_size = sizeof(AVFrame);
  1984. }
  1985. return buf_size;
  1986. }
  1987. if(s2->flags&CODEC_FLAG_TRUNCATED){
  1988. int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  1989. if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  1990. return buf_size;
  1991. }
  1992. #if 0
  1993. if (s->repeat_field % 2 == 1) {
  1994. s->repeat_field++;
  1995. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1996. // s2->picture_number, s->repeat_field);
  1997. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1998. *data_size = sizeof(AVPicture);
  1999. goto the_end;
  2000. }
  2001. }
  2002. #endif
  2003. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
  2004. vcr2_init_sequence(avctx);
  2005. s->slice_count= 0;
  2006. if(avctx->extradata && !avctx->frame_number)
  2007. decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2008. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2009. }
  2010. static int decode_chunks(AVCodecContext *avctx,
  2011. AVFrame *picture, int *data_size,
  2012. const uint8_t *buf, int buf_size)
  2013. {
  2014. Mpeg1Context *s = avctx->priv_data;
  2015. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2016. const uint8_t *buf_ptr = buf;
  2017. const uint8_t *buf_end = buf + buf_size;
  2018. int ret, input_size;
  2019. int last_code= 0;
  2020. for(;;) {
  2021. /* find next start code */
  2022. uint32_t start_code = -1;
  2023. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2024. if (start_code > 0x1ff){
  2025. if(s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2026. if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
  2027. int i;
  2028. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  2029. for(i=0; i<s->slice_count; i++)
  2030. s2->error_count += s2->thread_context[i]->error_count;
  2031. }
  2032. if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2033. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  2034. if (slice_end(avctx, picture)) {
  2035. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2036. *data_size = sizeof(AVPicture);
  2037. }
  2038. }
  2039. s2->pict_type= 0;
  2040. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2041. }
  2042. input_size = buf_end - buf_ptr;
  2043. if(avctx->debug & FF_DEBUG_STARTCODE){
  2044. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2045. }
  2046. /* prepare data for next start code */
  2047. switch(start_code) {
  2048. case SEQ_START_CODE:
  2049. if(last_code == 0){
  2050. mpeg1_decode_sequence(avctx, buf_ptr,
  2051. input_size);
  2052. s->sync=1;
  2053. }else{
  2054. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  2055. }
  2056. break;
  2057. case PICTURE_START_CODE:
  2058. if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
  2059. int i;
  2060. avctx->execute(avctx, slice_decode_thread,
  2061. s2->thread_context, NULL,
  2062. s->slice_count, sizeof(void*));
  2063. for (i = 0; i < s->slice_count; i++)
  2064. s2->error_count += s2->thread_context[i]->error_count;
  2065. s->slice_count = 0;
  2066. }
  2067. if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
  2068. if(mpeg_decode_postinit(avctx) < 0){
  2069. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  2070. return -1;
  2071. }
  2072. /* we have a complete image: we try to decompress it */
  2073. if(mpeg1_decode_picture(avctx,
  2074. buf_ptr, input_size) < 0)
  2075. s2->pict_type=0;
  2076. s2->first_slice = 1;
  2077. last_code= PICTURE_START_CODE;
  2078. }else{
  2079. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  2080. }
  2081. break;
  2082. case EXT_START_CODE:
  2083. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  2084. switch(get_bits(&s2->gb, 4)) {
  2085. case 0x1:
  2086. if(last_code == 0){
  2087. mpeg_decode_sequence_extension(s);
  2088. }else{
  2089. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  2090. }
  2091. break;
  2092. case 0x2:
  2093. mpeg_decode_sequence_display_extension(s);
  2094. break;
  2095. case 0x3:
  2096. mpeg_decode_quant_matrix_extension(s2);
  2097. break;
  2098. case 0x7:
  2099. mpeg_decode_picture_display_extension(s);
  2100. break;
  2101. case 0x8:
  2102. if(last_code == PICTURE_START_CODE){
  2103. mpeg_decode_picture_coding_extension(s);
  2104. }else{
  2105. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  2106. }
  2107. break;
  2108. }
  2109. break;
  2110. case USER_START_CODE:
  2111. mpeg_decode_user_data(avctx,
  2112. buf_ptr, input_size);
  2113. break;
  2114. case GOP_START_CODE:
  2115. if(last_code == 0){
  2116. s2->first_field=0;
  2117. mpeg_decode_gop(avctx,
  2118. buf_ptr, input_size);
  2119. s->sync=1;
  2120. }else{
  2121. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  2122. }
  2123. break;
  2124. default:
  2125. if (start_code >= SLICE_MIN_START_CODE &&
  2126. start_code <= SLICE_MAX_START_CODE && last_code!=0) {
  2127. const int field_pic= s2->picture_structure != PICT_FRAME;
  2128. int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
  2129. last_code= SLICE_MIN_START_CODE;
  2130. if(s2->picture_structure == PICT_BOTTOM_FIELD)
  2131. mb_y++;
  2132. if (mb_y >= s2->mb_height){
  2133. av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2134. return -1;
  2135. }
  2136. if(s2->last_picture_ptr==NULL){
  2137. /* Skip B-frames if we do not have reference frames and gop is not closed */
  2138. if(s2->pict_type==AV_PICTURE_TYPE_B){
  2139. if(!s2->closed_gop)
  2140. break;
  2141. }
  2142. }
  2143. if(s2->pict_type==AV_PICTURE_TYPE_I)
  2144. s->sync=1;
  2145. if(s2->next_picture_ptr==NULL){
  2146. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2147. if(s2->pict_type==AV_PICTURE_TYPE_P && !s->sync) break;
  2148. }
  2149. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==AV_PICTURE_TYPE_B)
  2150. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=AV_PICTURE_TYPE_I)
  2151. || avctx->skip_frame >= AVDISCARD_ALL)
  2152. break;
  2153. if (!s->mpeg_enc_ctx_allocated) break;
  2154. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2155. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2156. break;
  2157. }
  2158. if(!s2->pict_type){
  2159. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2160. break;
  2161. }
  2162. if(s2->first_slice){
  2163. s2->first_slice=0;
  2164. if(mpeg_field_start(s2, buf, buf_size) < 0)
  2165. return -1;
  2166. }
  2167. if(!s2->current_picture_ptr){
  2168. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2169. return -1;
  2170. }
  2171. if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
  2172. s->slice_count++;
  2173. break;
  2174. }
  2175. if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
  2176. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2177. if(threshold <= mb_y){
  2178. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2179. thread_context->start_mb_y= mb_y;
  2180. thread_context->end_mb_y = s2->mb_height;
  2181. if(s->slice_count){
  2182. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2183. ff_update_duplicate_context(thread_context, s2);
  2184. }
  2185. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2186. s->slice_count++;
  2187. }
  2188. buf_ptr += 2; //FIXME add minimum number of bytes per slice
  2189. }else{
  2190. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2191. emms_c();
  2192. if(ret < 0){
  2193. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2194. 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);
  2195. }else{
  2196. 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);
  2197. }
  2198. }
  2199. }
  2200. break;
  2201. }
  2202. }
  2203. }
  2204. static void flush(AVCodecContext *avctx){
  2205. Mpeg1Context *s = avctx->priv_data;
  2206. s->sync=0;
  2207. ff_mpeg_flush(avctx);
  2208. }
  2209. static int mpeg_decode_end(AVCodecContext *avctx)
  2210. {
  2211. Mpeg1Context *s = avctx->priv_data;
  2212. if (s->mpeg_enc_ctx_allocated)
  2213. MPV_common_end(&s->mpeg_enc_ctx);
  2214. return 0;
  2215. }
  2216. static const AVProfile mpeg2_video_profiles[] = {
  2217. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2218. { FF_PROFILE_MPEG2_HIGH, "High" },
  2219. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2220. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2221. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2222. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2223. { FF_PROFILE_RESERVED, "Reserved" },
  2224. { FF_PROFILE_RESERVED, "Reserved" },
  2225. { FF_PROFILE_UNKNOWN },
  2226. };
  2227. AVCodec ff_mpeg1video_decoder = {
  2228. "mpeg1video",
  2229. AVMEDIA_TYPE_VIDEO,
  2230. CODEC_ID_MPEG1VIDEO,
  2231. sizeof(Mpeg1Context),
  2232. mpeg_decode_init,
  2233. NULL,
  2234. mpeg_decode_end,
  2235. mpeg_decode_frame,
  2236. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2237. .flush= flush,
  2238. .max_lowres= 3,
  2239. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2240. .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2241. };
  2242. AVCodec ff_mpeg2video_decoder = {
  2243. "mpeg2video",
  2244. AVMEDIA_TYPE_VIDEO,
  2245. CODEC_ID_MPEG2VIDEO,
  2246. sizeof(Mpeg1Context),
  2247. mpeg_decode_init,
  2248. NULL,
  2249. mpeg_decode_end,
  2250. mpeg_decode_frame,
  2251. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2252. .flush= flush,
  2253. .max_lowres= 3,
  2254. .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2255. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2256. };
  2257. //legacy decoder
  2258. AVCodec ff_mpegvideo_decoder = {
  2259. "mpegvideo",
  2260. AVMEDIA_TYPE_VIDEO,
  2261. CODEC_ID_MPEG2VIDEO,
  2262. sizeof(Mpeg1Context),
  2263. mpeg_decode_init,
  2264. NULL,
  2265. mpeg_decode_end,
  2266. mpeg_decode_frame,
  2267. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2268. .flush= flush,
  2269. .max_lowres= 3,
  2270. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2271. };
  2272. #if CONFIG_MPEG_XVMC_DECODER
  2273. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
  2274. if( avctx->active_thread_type & FF_THREAD_SLICE )
  2275. return -1;
  2276. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2277. return -1;
  2278. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2279. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2280. }
  2281. mpeg_decode_init(avctx);
  2282. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2283. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2284. return 0;
  2285. }
  2286. AVCodec ff_mpeg_xvmc_decoder = {
  2287. "mpegvideo_xvmc",
  2288. AVMEDIA_TYPE_VIDEO,
  2289. CODEC_ID_MPEG2VIDEO_XVMC,
  2290. sizeof(Mpeg1Context),
  2291. mpeg_mc_decode_init,
  2292. NULL,
  2293. mpeg_decode_end,
  2294. mpeg_decode_frame,
  2295. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2296. .flush= flush,
  2297. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2298. };
  2299. #endif
  2300. #if CONFIG_MPEG_VDPAU_DECODER
  2301. AVCodec ff_mpeg_vdpau_decoder = {
  2302. "mpegvideo_vdpau",
  2303. AVMEDIA_TYPE_VIDEO,
  2304. CODEC_ID_MPEG2VIDEO,
  2305. sizeof(Mpeg1Context),
  2306. mpeg_decode_init,
  2307. NULL,
  2308. mpeg_decode_end,
  2309. mpeg_decode_frame,
  2310. CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2311. .flush= flush,
  2312. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2313. };
  2314. #endif
  2315. #if CONFIG_MPEG1_VDPAU_DECODER
  2316. AVCodec ff_mpeg1_vdpau_decoder = {
  2317. "mpeg1video_vdpau",
  2318. AVMEDIA_TYPE_VIDEO,
  2319. CODEC_ID_MPEG1VIDEO,
  2320. sizeof(Mpeg1Context),
  2321. mpeg_decode_init,
  2322. NULL,
  2323. mpeg_decode_end,
  2324. mpeg_decode_frame,
  2325. CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2326. .flush= flush,
  2327. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2328. };
  2329. #endif