mpeg12.c 87 KB

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