mpegvideo.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file libavcodec/mpegvideo.c
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "mpegvideo.h"
  31. #include "mpegvideo_common.h"
  32. #include "mjpegenc.h"
  33. #include "msmpeg4.h"
  34. #include "faandct.h"
  35. #include "xvmc_internal.h"
  36. #include <limits.h>
  37. //#undef NDEBUG
  38. //#include <assert.h>
  39. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  40. DCTELEM *block, int n, int qscale);
  41. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  42. DCTELEM *block, int n, int qscale);
  43. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. /* enable all paranoid tests for rounding, overflows, etc... */
  54. //#define PARANOID
  55. //#define DEBUG
  56. static const uint8_t ff_default_chroma_qscale_table[32]={
  57. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  58. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
  59. };
  60. const uint8_t ff_mpeg1_dc_scale_table[128]={
  61. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  62. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  63. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  64. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  66. };
  67. const enum PixelFormat ff_pixfmt_list_420[] = {
  68. PIX_FMT_YUV420P,
  69. PIX_FMT_NONE
  70. };
  71. const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
  72. PIX_FMT_YUV420P,
  73. PIX_FMT_NONE
  74. };
  75. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  76. int i;
  77. assert(p<=end);
  78. if(p>=end)
  79. return end;
  80. for(i=0; i<3; i++){
  81. uint32_t tmp= *state << 8;
  82. *state= tmp + *(p++);
  83. if(tmp == 0x100 || p==end)
  84. return p;
  85. }
  86. while(p<end){
  87. if (p[-1] > 1 ) p+= 3;
  88. else if(p[-2] ) p+= 2;
  89. else if(p[-3]|(p[-1]-1)) p++;
  90. else{
  91. p++;
  92. break;
  93. }
  94. }
  95. p= FFMIN(p, end)-4;
  96. *state= AV_RB32(p);
  97. return p+4;
  98. }
  99. /* init common dct for both encoder and decoder */
  100. av_cold int ff_dct_common_init(MpegEncContext *s)
  101. {
  102. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  103. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  104. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  105. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  106. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  107. if(s->flags & CODEC_FLAG_BITEXACT)
  108. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  109. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  110. #if HAVE_MMX
  111. MPV_common_init_mmx(s);
  112. #elif ARCH_ALPHA
  113. MPV_common_init_axp(s);
  114. #elif CONFIG_MLIB
  115. MPV_common_init_mlib(s);
  116. #elif HAVE_MMI
  117. MPV_common_init_mmi(s);
  118. #elif ARCH_ARM
  119. MPV_common_init_arm(s);
  120. #elif HAVE_ALTIVEC
  121. MPV_common_init_altivec(s);
  122. #elif ARCH_BFIN
  123. MPV_common_init_bfin(s);
  124. #endif
  125. /* load & permutate scantables
  126. note: only wmv uses different ones
  127. */
  128. if(s->alternate_scan){
  129. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  130. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  131. }else{
  132. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  133. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  134. }
  135. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  136. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  137. return 0;
  138. }
  139. void ff_copy_picture(Picture *dst, Picture *src){
  140. *dst = *src;
  141. dst->type= FF_BUFFER_TYPE_COPY;
  142. }
  143. /**
  144. * allocates a Picture
  145. * The pixels are allocated/set by calling get_buffer() if shared=0
  146. */
  147. int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  148. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  149. const int mb_array_size= s->mb_stride*s->mb_height;
  150. const int b8_array_size= s->b8_stride*s->mb_height*2;
  151. const int b4_array_size= s->b4_stride*s->mb_height*4;
  152. int i;
  153. int r= -1;
  154. if(shared){
  155. assert(pic->data[0]);
  156. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  157. pic->type= FF_BUFFER_TYPE_SHARED;
  158. }else{
  159. assert(!pic->data[0]);
  160. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  161. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  162. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  163. return -1;
  164. }
  165. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  166. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  167. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  168. return -1;
  169. }
  170. if(pic->linesize[1] != pic->linesize[2]){
  171. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  172. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  173. return -1;
  174. }
  175. s->linesize = pic->linesize[0];
  176. s->uvlinesize= pic->linesize[1];
  177. }
  178. if(pic->qscale_table==NULL){
  179. if (s->encoding) {
  180. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  181. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  182. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  183. }
  184. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  185. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  186. CHECKED_ALLOCZ(pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t))
  187. pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
  188. if(s->out_format == FMT_H264){
  189. for(i=0; i<2; i++){
  190. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
  191. pic->motion_val[i]= pic->motion_val_base[i]+4;
  192. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  193. }
  194. pic->motion_subsample_log2= 2;
  195. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  196. for(i=0; i<2; i++){
  197. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
  198. pic->motion_val[i]= pic->motion_val_base[i]+4;
  199. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  200. }
  201. pic->motion_subsample_log2= 3;
  202. }
  203. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  204. CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
  205. }
  206. pic->qstride= s->mb_stride;
  207. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  208. }
  209. /* It might be nicer if the application would keep track of these
  210. * but it would require an API change. */
  211. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  212. s->prev_pict_types[0]= s->dropable ? FF_B_TYPE : s->pict_type;
  213. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == FF_B_TYPE)
  214. pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  215. return 0;
  216. fail: //for the CHECKED_ALLOCZ macro
  217. if(r>=0)
  218. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  219. return -1;
  220. }
  221. /**
  222. * deallocates a picture
  223. */
  224. static void free_picture(MpegEncContext *s, Picture *pic){
  225. int i;
  226. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  227. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  228. }
  229. av_freep(&pic->mb_var);
  230. av_freep(&pic->mc_mb_var);
  231. av_freep(&pic->mb_mean);
  232. av_freep(&pic->mbskip_table);
  233. av_freep(&pic->qscale_table);
  234. av_freep(&pic->mb_type_base);
  235. av_freep(&pic->dct_coeff);
  236. av_freep(&pic->pan_scan);
  237. pic->mb_type= NULL;
  238. for(i=0; i<2; i++){
  239. av_freep(&pic->motion_val_base[i]);
  240. av_freep(&pic->ref_index[i]);
  241. }
  242. if(pic->type == FF_BUFFER_TYPE_SHARED){
  243. for(i=0; i<4; i++){
  244. pic->base[i]=
  245. pic->data[i]= NULL;
  246. }
  247. pic->type= 0;
  248. }
  249. }
  250. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  251. int i;
  252. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  253. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2); //(width + edge + align)*interlaced*MBsize*tolerance
  254. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
  255. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  256. CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
  257. s->me.temp= s->me.scratchpad;
  258. s->rd_scratchpad= s->me.scratchpad;
  259. s->b_scratchpad= s->me.scratchpad;
  260. s->obmc_scratchpad= s->me.scratchpad + 16;
  261. if (s->encoding) {
  262. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  263. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  264. if(s->avctx->noise_reduction){
  265. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  266. }
  267. }
  268. CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
  269. s->block= s->blocks[0];
  270. for(i=0;i<12;i++){
  271. s->pblocks[i] = &s->block[i];
  272. }
  273. return 0;
  274. fail:
  275. return -1; //free() through MPV_common_end()
  276. }
  277. static void free_duplicate_context(MpegEncContext *s){
  278. if(s==NULL) return;
  279. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  280. av_freep(&s->me.scratchpad);
  281. s->me.temp=
  282. s->rd_scratchpad=
  283. s->b_scratchpad=
  284. s->obmc_scratchpad= NULL;
  285. av_freep(&s->dct_error_sum);
  286. av_freep(&s->me.map);
  287. av_freep(&s->me.score_map);
  288. av_freep(&s->blocks);
  289. s->block= NULL;
  290. }
  291. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  292. #define COPY(a) bak->a= src->a
  293. COPY(allocated_edge_emu_buffer);
  294. COPY(edge_emu_buffer);
  295. COPY(me.scratchpad);
  296. COPY(me.temp);
  297. COPY(rd_scratchpad);
  298. COPY(b_scratchpad);
  299. COPY(obmc_scratchpad);
  300. COPY(me.map);
  301. COPY(me.score_map);
  302. COPY(blocks);
  303. COPY(block);
  304. COPY(start_mb_y);
  305. COPY(end_mb_y);
  306. COPY(me.map_generation);
  307. COPY(pb);
  308. COPY(dct_error_sum);
  309. COPY(dct_count[0]);
  310. COPY(dct_count[1]);
  311. #undef COPY
  312. }
  313. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  314. MpegEncContext bak;
  315. int i;
  316. //FIXME copy only needed parts
  317. //START_TIMER
  318. backup_duplicate_context(&bak, dst);
  319. memcpy(dst, src, sizeof(MpegEncContext));
  320. backup_duplicate_context(dst, &bak);
  321. for(i=0;i<12;i++){
  322. dst->pblocks[i] = &dst->block[i];
  323. }
  324. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  325. }
  326. /**
  327. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  328. * the changed fields will not depend upon the prior state of the MpegEncContext.
  329. */
  330. void MPV_common_defaults(MpegEncContext *s){
  331. s->y_dc_scale_table=
  332. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  333. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  334. s->progressive_frame= 1;
  335. s->progressive_sequence= 1;
  336. s->picture_structure= PICT_FRAME;
  337. s->coded_picture_number = 0;
  338. s->picture_number = 0;
  339. s->input_picture_number = 0;
  340. s->picture_in_gop_number = 0;
  341. s->f_code = 1;
  342. s->b_code = 1;
  343. }
  344. /**
  345. * sets the given MpegEncContext to defaults for decoding.
  346. * the changed fields will not depend upon the prior state of the MpegEncContext.
  347. */
  348. void MPV_decode_defaults(MpegEncContext *s){
  349. MPV_common_defaults(s);
  350. }
  351. /**
  352. * init common structure for both encoder and decoder.
  353. * this assumes that some variables like width/height are already set
  354. */
  355. av_cold int MPV_common_init(MpegEncContext *s)
  356. {
  357. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
  358. s->mb_height = (s->height + 15) / 16;
  359. if(s->avctx->pix_fmt == PIX_FMT_NONE){
  360. av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
  361. return -1;
  362. }
  363. if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
  364. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  365. return -1;
  366. }
  367. if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
  368. return -1;
  369. dsputil_init(&s->dsp, s->avctx);
  370. ff_dct_common_init(s);
  371. s->flags= s->avctx->flags;
  372. s->flags2= s->avctx->flags2;
  373. s->mb_width = (s->width + 15) / 16;
  374. s->mb_stride = s->mb_width + 1;
  375. s->b8_stride = s->mb_width*2 + 1;
  376. s->b4_stride = s->mb_width*4 + 1;
  377. mb_array_size= s->mb_height * s->mb_stride;
  378. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  379. /* set chroma shifts */
  380. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  381. &(s->chroma_y_shift) );
  382. /* set default edge pos, will be overriden in decode_header if needed */
  383. s->h_edge_pos= s->mb_width*16;
  384. s->v_edge_pos= s->mb_height*16;
  385. s->mb_num = s->mb_width * s->mb_height;
  386. s->block_wrap[0]=
  387. s->block_wrap[1]=
  388. s->block_wrap[2]=
  389. s->block_wrap[3]= s->b8_stride;
  390. s->block_wrap[4]=
  391. s->block_wrap[5]= s->mb_stride;
  392. y_size = s->b8_stride * (2 * s->mb_height + 1);
  393. c_size = s->mb_stride * (s->mb_height + 1);
  394. yc_size = y_size + 2 * c_size;
  395. /* convert fourcc to upper case */
  396. s->codec_tag= toupper( s->avctx->codec_tag &0xFF)
  397. + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
  398. + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
  399. + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
  400. s->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
  401. + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
  402. + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
  403. + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
  404. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  405. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
  406. for(y=0; y<s->mb_height; y++){
  407. for(x=0; x<s->mb_width; x++){
  408. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  409. }
  410. }
  411. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  412. if (s->encoding) {
  413. /* Allocate MV tables */
  414. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  415. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  416. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  417. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  418. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  419. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  420. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  421. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  422. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  423. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  424. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  425. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  426. if(s->msmpeg4_version){
  427. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  428. }
  429. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  430. /* Allocate MB type table */
  431. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
  432. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  433. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  434. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  435. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  436. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  437. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  438. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  439. if(s->avctx->noise_reduction){
  440. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  441. }
  442. }
  443. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  444. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  445. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  446. /* interlaced direct mode decoding tables */
  447. for(i=0; i<2; i++){
  448. int j, k;
  449. for(j=0; j<2; j++){
  450. for(k=0; k<2; k++){
  451. CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
  452. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  453. }
  454. CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
  455. CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
  456. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  457. }
  458. CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
  459. }
  460. }
  461. if (s->out_format == FMT_H263) {
  462. /* ac values */
  463. CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
  464. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  465. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  466. s->ac_val[2] = s->ac_val[1] + c_size;
  467. /* cbp values */
  468. CHECKED_ALLOCZ(s->coded_block_base, y_size);
  469. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  470. /* cbp, ac_pred, pred_dir */
  471. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  472. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  473. }
  474. if (s->h263_pred || s->h263_plus || !s->encoding) {
  475. /* dc values */
  476. //MN: we need these for error resilience of intra-frames
  477. CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
  478. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  479. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  480. s->dc_val[2] = s->dc_val[1] + c_size;
  481. for(i=0;i<yc_size;i++)
  482. s->dc_val_base[i] = 1024;
  483. }
  484. /* which mb is a intra block */
  485. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  486. memset(s->mbintra_table, 1, mb_array_size);
  487. /* init macroblock skip table */
  488. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  489. //Note the +1 is for a quicker mpeg4 slice_end detection
  490. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  491. s->parse_context.state= -1;
  492. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  493. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  494. s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  495. s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  496. }
  497. s->context_initialized = 1;
  498. s->thread_context[0]= s;
  499. threads = s->avctx->thread_count;
  500. for(i=1; i<threads; i++){
  501. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  502. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  503. }
  504. for(i=0; i<threads; i++){
  505. if(init_duplicate_context(s->thread_context[i], s) < 0)
  506. goto fail;
  507. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  508. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  509. }
  510. return 0;
  511. fail:
  512. MPV_common_end(s);
  513. return -1;
  514. }
  515. /* init common structure for both encoder and decoder */
  516. void MPV_common_end(MpegEncContext *s)
  517. {
  518. int i, j, k;
  519. for(i=0; i<s->avctx->thread_count; i++){
  520. free_duplicate_context(s->thread_context[i]);
  521. }
  522. for(i=1; i<s->avctx->thread_count; i++){
  523. av_freep(&s->thread_context[i]);
  524. }
  525. av_freep(&s->parse_context.buffer);
  526. s->parse_context.buffer_size=0;
  527. av_freep(&s->mb_type);
  528. av_freep(&s->p_mv_table_base);
  529. av_freep(&s->b_forw_mv_table_base);
  530. av_freep(&s->b_back_mv_table_base);
  531. av_freep(&s->b_bidir_forw_mv_table_base);
  532. av_freep(&s->b_bidir_back_mv_table_base);
  533. av_freep(&s->b_direct_mv_table_base);
  534. s->p_mv_table= NULL;
  535. s->b_forw_mv_table= NULL;
  536. s->b_back_mv_table= NULL;
  537. s->b_bidir_forw_mv_table= NULL;
  538. s->b_bidir_back_mv_table= NULL;
  539. s->b_direct_mv_table= NULL;
  540. for(i=0; i<2; i++){
  541. for(j=0; j<2; j++){
  542. for(k=0; k<2; k++){
  543. av_freep(&s->b_field_mv_table_base[i][j][k]);
  544. s->b_field_mv_table[i][j][k]=NULL;
  545. }
  546. av_freep(&s->b_field_select_table[i][j]);
  547. av_freep(&s->p_field_mv_table_base[i][j]);
  548. s->p_field_mv_table[i][j]=NULL;
  549. }
  550. av_freep(&s->p_field_select_table[i]);
  551. }
  552. av_freep(&s->dc_val_base);
  553. av_freep(&s->ac_val_base);
  554. av_freep(&s->coded_block_base);
  555. av_freep(&s->mbintra_table);
  556. av_freep(&s->cbp_table);
  557. av_freep(&s->pred_dir_table);
  558. av_freep(&s->mbskip_table);
  559. av_freep(&s->prev_pict_types);
  560. av_freep(&s->bitstream_buffer);
  561. s->allocated_bitstream_buffer_size=0;
  562. av_freep(&s->avctx->stats_out);
  563. av_freep(&s->ac_stats);
  564. av_freep(&s->error_status_table);
  565. av_freep(&s->mb_index2xy);
  566. av_freep(&s->lambda_table);
  567. av_freep(&s->q_intra_matrix);
  568. av_freep(&s->q_inter_matrix);
  569. av_freep(&s->q_intra_matrix16);
  570. av_freep(&s->q_inter_matrix16);
  571. av_freep(&s->input_picture);
  572. av_freep(&s->reordered_input_picture);
  573. av_freep(&s->dct_offset);
  574. if(s->picture){
  575. for(i=0; i<MAX_PICTURE_COUNT; i++){
  576. free_picture(s, &s->picture[i]);
  577. }
  578. }
  579. av_freep(&s->picture);
  580. s->context_initialized = 0;
  581. s->last_picture_ptr=
  582. s->next_picture_ptr=
  583. s->current_picture_ptr= NULL;
  584. s->linesize= s->uvlinesize= 0;
  585. for(i=0; i<3; i++)
  586. av_freep(&s->visualization_buffer[i]);
  587. avcodec_default_free_buffers(s->avctx);
  588. }
  589. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  590. {
  591. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  592. uint8_t index_run[MAX_RUN+1];
  593. int last, run, level, start, end, i;
  594. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  595. if(static_store && rl->max_level[0])
  596. return;
  597. /* compute max_level[], max_run[] and index_run[] */
  598. for(last=0;last<2;last++) {
  599. if (last == 0) {
  600. start = 0;
  601. end = rl->last;
  602. } else {
  603. start = rl->last;
  604. end = rl->n;
  605. }
  606. memset(max_level, 0, MAX_RUN + 1);
  607. memset(max_run, 0, MAX_LEVEL + 1);
  608. memset(index_run, rl->n, MAX_RUN + 1);
  609. for(i=start;i<end;i++) {
  610. run = rl->table_run[i];
  611. level = rl->table_level[i];
  612. if (index_run[run] == rl->n)
  613. index_run[run] = i;
  614. if (level > max_level[run])
  615. max_level[run] = level;
  616. if (run > max_run[level])
  617. max_run[level] = run;
  618. }
  619. if(static_store)
  620. rl->max_level[last] = static_store[last];
  621. else
  622. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  623. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  624. if(static_store)
  625. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  626. else
  627. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  628. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  629. if(static_store)
  630. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  631. else
  632. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  633. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  634. }
  635. }
  636. void init_vlc_rl(RLTable *rl)
  637. {
  638. int i, q;
  639. for(q=0; q<32; q++){
  640. int qmul= q*2;
  641. int qadd= (q-1)|1;
  642. if(q==0){
  643. qmul=1;
  644. qadd=0;
  645. }
  646. for(i=0; i<rl->vlc.table_size; i++){
  647. int code= rl->vlc.table[i][0];
  648. int len = rl->vlc.table[i][1];
  649. int level, run;
  650. if(len==0){ // illegal code
  651. run= 66;
  652. level= MAX_LEVEL;
  653. }else if(len<0){ //more bits needed
  654. run= 0;
  655. level= code;
  656. }else{
  657. if(code==rl->n){ //esc
  658. run= 66;
  659. level= 0;
  660. }else{
  661. run= rl->table_run [code] + 1;
  662. level= rl->table_level[code] * qmul + qadd;
  663. if(code >= rl->last) run+=192;
  664. }
  665. }
  666. rl->rl_vlc[q][i].len= len;
  667. rl->rl_vlc[q][i].level= level;
  668. rl->rl_vlc[q][i].run= run;
  669. }
  670. }
  671. }
  672. int ff_find_unused_picture(MpegEncContext *s, int shared){
  673. int i;
  674. if(shared){
  675. for(i=0; i<MAX_PICTURE_COUNT; i++){
  676. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  677. }
  678. }else{
  679. for(i=0; i<MAX_PICTURE_COUNT; i++){
  680. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  681. }
  682. for(i=0; i<MAX_PICTURE_COUNT; i++){
  683. if(s->picture[i].data[0]==NULL) return i;
  684. }
  685. }
  686. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  687. /* We could return -1, but the codec would crash trying to draw into a
  688. * non-existing frame anyway. This is safer than waiting for a random crash.
  689. * Also the return of this is never useful, an encoder must only allocate
  690. * as much as allowed in the specification. This has no relationship to how
  691. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  692. * enough for such valid streams).
  693. * Plus, a decoder has to check stream validity and remove frames if too
  694. * many reference frames are around. Waiting for "OOM" is not correct at
  695. * all. Similarly, missing reference frames have to be replaced by
  696. * interpolated/MC frames, anything else is a bug in the codec ...
  697. */
  698. abort();
  699. return -1;
  700. }
  701. static void update_noise_reduction(MpegEncContext *s){
  702. int intra, i;
  703. for(intra=0; intra<2; intra++){
  704. if(s->dct_count[intra] > (1<<16)){
  705. for(i=0; i<64; i++){
  706. s->dct_error_sum[intra][i] >>=1;
  707. }
  708. s->dct_count[intra] >>= 1;
  709. }
  710. for(i=0; i<64; i++){
  711. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  712. }
  713. }
  714. }
  715. /**
  716. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  717. */
  718. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  719. {
  720. int i;
  721. AVFrame *pic;
  722. s->mb_skipped = 0;
  723. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  724. /* mark&release old frames */
  725. if (s->pict_type != FF_B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  726. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  727. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  728. /* release forgotten pictures */
  729. /* if(mpeg124/h263) */
  730. if(!s->encoding){
  731. for(i=0; i<MAX_PICTURE_COUNT; i++){
  732. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  733. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  734. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  735. }
  736. }
  737. }
  738. }
  739. }
  740. alloc:
  741. if(!s->encoding){
  742. /* release non reference frames */
  743. for(i=0; i<MAX_PICTURE_COUNT; i++){
  744. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  745. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  746. }
  747. }
  748. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  749. pic= (AVFrame*)s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  750. else{
  751. i= ff_find_unused_picture(s, 0);
  752. pic= (AVFrame*)&s->picture[i];
  753. }
  754. pic->reference= 0;
  755. if (!s->dropable){
  756. if (s->codec_id == CODEC_ID_H264)
  757. pic->reference = s->picture_structure;
  758. else if (s->pict_type != FF_B_TYPE)
  759. pic->reference = 3;
  760. }
  761. pic->coded_picture_number= s->coded_picture_number++;
  762. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  763. return -1;
  764. s->current_picture_ptr= (Picture*)pic;
  765. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  766. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  767. }
  768. s->current_picture_ptr->pict_type= s->pict_type;
  769. // if(s->flags && CODEC_FLAG_QSCALE)
  770. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  771. s->current_picture_ptr->key_frame= s->pict_type == FF_I_TYPE;
  772. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  773. if (s->pict_type != FF_B_TYPE) {
  774. s->last_picture_ptr= s->next_picture_ptr;
  775. if(!s->dropable)
  776. s->next_picture_ptr= s->current_picture_ptr;
  777. }
  778. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  779. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  780. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  781. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  782. s->pict_type, s->dropable);*/
  783. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  784. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  785. if(s->pict_type != FF_I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) && !s->dropable && s->codec_id != CODEC_ID_H264){
  786. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  787. assert(s->pict_type != FF_B_TYPE); //these should have been dropped if we don't have a reference
  788. goto alloc;
  789. }
  790. assert(s->pict_type == FF_I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  791. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  792. int i;
  793. for(i=0; i<4; i++){
  794. if(s->picture_structure == PICT_BOTTOM_FIELD){
  795. s->current_picture.data[i] += s->current_picture.linesize[i];
  796. }
  797. s->current_picture.linesize[i] *= 2;
  798. s->last_picture.linesize[i] *=2;
  799. s->next_picture.linesize[i] *=2;
  800. }
  801. }
  802. s->hurry_up= s->avctx->hurry_up;
  803. s->error_recognition= avctx->error_recognition;
  804. /* set dequantizer, we can't do it during init as it might change for mpeg4
  805. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  806. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  807. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  808. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  809. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  810. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  811. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  812. }else{
  813. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  814. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  815. }
  816. if(s->dct_error_sum){
  817. assert(s->avctx->noise_reduction && s->encoding);
  818. update_noise_reduction(s);
  819. }
  820. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  821. return ff_xvmc_field_start(s, avctx);
  822. return 0;
  823. }
  824. /* generic function for encode/decode called after a frame has been coded/decoded */
  825. void MPV_frame_end(MpegEncContext *s)
  826. {
  827. int i;
  828. /* draw edge for correct motion prediction if outside */
  829. //just to make sure that all data is rendered.
  830. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  831. ff_xvmc_field_end(s);
  832. }else if(!s->avctx->hwaccel
  833. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  834. && s->unrestricted_mv
  835. && s->current_picture.reference
  836. && !s->intra_only
  837. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  838. s->dsp.draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  839. s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  840. s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  841. }
  842. emms_c();
  843. s->last_pict_type = s->pict_type;
  844. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  845. if(s->pict_type!=FF_B_TYPE){
  846. s->last_non_b_pict_type= s->pict_type;
  847. }
  848. #if 0
  849. /* copy back current_picture variables */
  850. for(i=0; i<MAX_PICTURE_COUNT; i++){
  851. if(s->picture[i].data[0] == s->current_picture.data[0]){
  852. s->picture[i]= s->current_picture;
  853. break;
  854. }
  855. }
  856. assert(i<MAX_PICTURE_COUNT);
  857. #endif
  858. if(s->encoding){
  859. /* release non-reference frames */
  860. for(i=0; i<MAX_PICTURE_COUNT; i++){
  861. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  862. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  863. }
  864. }
  865. }
  866. // clear copies, to avoid confusion
  867. #if 0
  868. memset(&s->last_picture, 0, sizeof(Picture));
  869. memset(&s->next_picture, 0, sizeof(Picture));
  870. memset(&s->current_picture, 0, sizeof(Picture));
  871. #endif
  872. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  873. }
  874. /**
  875. * draws an line from (ex, ey) -> (sx, sy).
  876. * @param w width of the image
  877. * @param h height of the image
  878. * @param stride stride/linesize of the image
  879. * @param color color of the arrow
  880. */
  881. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  882. int x, y, fr, f;
  883. sx= av_clip(sx, 0, w-1);
  884. sy= av_clip(sy, 0, h-1);
  885. ex= av_clip(ex, 0, w-1);
  886. ey= av_clip(ey, 0, h-1);
  887. buf[sy*stride + sx]+= color;
  888. if(FFABS(ex - sx) > FFABS(ey - sy)){
  889. if(sx > ex){
  890. FFSWAP(int, sx, ex);
  891. FFSWAP(int, sy, ey);
  892. }
  893. buf+= sx + sy*stride;
  894. ex-= sx;
  895. f= ((ey-sy)<<16)/ex;
  896. for(x= 0; x <= ex; x++){
  897. y = (x*f)>>16;
  898. fr= (x*f)&0xFFFF;
  899. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  900. buf[(y+1)*stride + x]+= (color* fr )>>16;
  901. }
  902. }else{
  903. if(sy > ey){
  904. FFSWAP(int, sx, ex);
  905. FFSWAP(int, sy, ey);
  906. }
  907. buf+= sx + sy*stride;
  908. ey-= sy;
  909. if(ey) f= ((ex-sx)<<16)/ey;
  910. else f= 0;
  911. for(y= 0; y <= ey; y++){
  912. x = (y*f)>>16;
  913. fr= (y*f)&0xFFFF;
  914. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  915. buf[y*stride + x+1]+= (color* fr )>>16;
  916. }
  917. }
  918. }
  919. /**
  920. * draws an arrow from (ex, ey) -> (sx, sy).
  921. * @param w width of the image
  922. * @param h height of the image
  923. * @param stride stride/linesize of the image
  924. * @param color color of the arrow
  925. */
  926. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  927. int dx,dy;
  928. sx= av_clip(sx, -100, w+100);
  929. sy= av_clip(sy, -100, h+100);
  930. ex= av_clip(ex, -100, w+100);
  931. ey= av_clip(ey, -100, h+100);
  932. dx= ex - sx;
  933. dy= ey - sy;
  934. if(dx*dx + dy*dy > 3*3){
  935. int rx= dx + dy;
  936. int ry= -dx + dy;
  937. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  938. //FIXME subpixel accuracy
  939. rx= ROUNDED_DIV(rx*3<<4, length);
  940. ry= ROUNDED_DIV(ry*3<<4, length);
  941. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  942. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  943. }
  944. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  945. }
  946. /**
  947. * prints debuging info for the given picture.
  948. */
  949. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  950. if(s->avctx->hwaccel) return;
  951. if(!pict || !pict->mb_type) return;
  952. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  953. int x,y;
  954. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  955. switch (pict->pict_type) {
  956. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  957. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  958. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  959. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  960. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  961. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  962. }
  963. for(y=0; y<s->mb_height; y++){
  964. for(x=0; x<s->mb_width; x++){
  965. if(s->avctx->debug&FF_DEBUG_SKIP){
  966. int count= s->mbskip_table[x + y*s->mb_stride];
  967. if(count>9) count=9;
  968. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  969. }
  970. if(s->avctx->debug&FF_DEBUG_QP){
  971. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  972. }
  973. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  974. int mb_type= pict->mb_type[x + y*s->mb_stride];
  975. //Type & MV direction
  976. if(IS_PCM(mb_type))
  977. av_log(s->avctx, AV_LOG_DEBUG, "P");
  978. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  979. av_log(s->avctx, AV_LOG_DEBUG, "A");
  980. else if(IS_INTRA4x4(mb_type))
  981. av_log(s->avctx, AV_LOG_DEBUG, "i");
  982. else if(IS_INTRA16x16(mb_type))
  983. av_log(s->avctx, AV_LOG_DEBUG, "I");
  984. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  985. av_log(s->avctx, AV_LOG_DEBUG, "d");
  986. else if(IS_DIRECT(mb_type))
  987. av_log(s->avctx, AV_LOG_DEBUG, "D");
  988. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  989. av_log(s->avctx, AV_LOG_DEBUG, "g");
  990. else if(IS_GMC(mb_type))
  991. av_log(s->avctx, AV_LOG_DEBUG, "G");
  992. else if(IS_SKIP(mb_type))
  993. av_log(s->avctx, AV_LOG_DEBUG, "S");
  994. else if(!USES_LIST(mb_type, 1))
  995. av_log(s->avctx, AV_LOG_DEBUG, ">");
  996. else if(!USES_LIST(mb_type, 0))
  997. av_log(s->avctx, AV_LOG_DEBUG, "<");
  998. else{
  999. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1000. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1001. }
  1002. //segmentation
  1003. if(IS_8X8(mb_type))
  1004. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1005. else if(IS_16X8(mb_type))
  1006. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1007. else if(IS_8X16(mb_type))
  1008. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1009. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1010. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1011. else
  1012. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1013. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1014. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1015. else
  1016. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1017. }
  1018. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1019. }
  1020. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1021. }
  1022. }
  1023. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1024. const int shift= 1 + s->quarter_sample;
  1025. int mb_y;
  1026. uint8_t *ptr;
  1027. int i;
  1028. int h_chroma_shift, v_chroma_shift, block_height;
  1029. const int width = s->avctx->width;
  1030. const int height= s->avctx->height;
  1031. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1032. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1033. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1034. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1035. for(i=0; i<3; i++){
  1036. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1037. pict->data[i]= s->visualization_buffer[i];
  1038. }
  1039. pict->type= FF_BUFFER_TYPE_COPY;
  1040. ptr= pict->data[0];
  1041. block_height = 16>>v_chroma_shift;
  1042. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1043. int mb_x;
  1044. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1045. const int mb_index= mb_x + mb_y*s->mb_stride;
  1046. if((s->avctx->debug_mv) && pict->motion_val){
  1047. int type;
  1048. for(type=0; type<3; type++){
  1049. int direction = 0;
  1050. switch (type) {
  1051. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1052. continue;
  1053. direction = 0;
  1054. break;
  1055. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1056. continue;
  1057. direction = 0;
  1058. break;
  1059. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1060. continue;
  1061. direction = 1;
  1062. break;
  1063. }
  1064. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1065. continue;
  1066. if(IS_8X8(pict->mb_type[mb_index])){
  1067. int i;
  1068. for(i=0; i<4; i++){
  1069. int sx= mb_x*16 + 4 + 8*(i&1);
  1070. int sy= mb_y*16 + 4 + 8*(i>>1);
  1071. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1072. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1073. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1074. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1075. }
  1076. }else if(IS_16X8(pict->mb_type[mb_index])){
  1077. int i;
  1078. for(i=0; i<2; i++){
  1079. int sx=mb_x*16 + 8;
  1080. int sy=mb_y*16 + 4 + 8*i;
  1081. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1082. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1083. int my=(pict->motion_val[direction][xy][1]>>shift);
  1084. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1085. my*=2;
  1086. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1087. }
  1088. }else if(IS_8X16(pict->mb_type[mb_index])){
  1089. int i;
  1090. for(i=0; i<2; i++){
  1091. int sx=mb_x*16 + 4 + 8*i;
  1092. int sy=mb_y*16 + 8;
  1093. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1094. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1095. int my=(pict->motion_val[direction][xy][1]>>shift);
  1096. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1097. my*=2;
  1098. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1099. }
  1100. }else{
  1101. int sx= mb_x*16 + 8;
  1102. int sy= mb_y*16 + 8;
  1103. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1104. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1105. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1106. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1107. }
  1108. }
  1109. }
  1110. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1111. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1112. int y;
  1113. for(y=0; y<block_height; y++){
  1114. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1115. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1116. }
  1117. }
  1118. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1119. int mb_type= pict->mb_type[mb_index];
  1120. uint64_t u,v;
  1121. int y;
  1122. #define COLOR(theta, r)\
  1123. u= (int)(128 + r*cos(theta*3.141592/180));\
  1124. v= (int)(128 + r*sin(theta*3.141592/180));
  1125. u=v=128;
  1126. if(IS_PCM(mb_type)){
  1127. COLOR(120,48)
  1128. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1129. COLOR(30,48)
  1130. }else if(IS_INTRA4x4(mb_type)){
  1131. COLOR(90,48)
  1132. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1133. // COLOR(120,48)
  1134. }else if(IS_DIRECT(mb_type)){
  1135. COLOR(150,48)
  1136. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1137. COLOR(170,48)
  1138. }else if(IS_GMC(mb_type)){
  1139. COLOR(190,48)
  1140. }else if(IS_SKIP(mb_type)){
  1141. // COLOR(180,48)
  1142. }else if(!USES_LIST(mb_type, 1)){
  1143. COLOR(240,48)
  1144. }else if(!USES_LIST(mb_type, 0)){
  1145. COLOR(0,48)
  1146. }else{
  1147. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1148. COLOR(300,48)
  1149. }
  1150. u*= 0x0101010101010101ULL;
  1151. v*= 0x0101010101010101ULL;
  1152. for(y=0; y<block_height; y++){
  1153. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1154. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1155. }
  1156. //segmentation
  1157. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1158. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1159. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1160. }
  1161. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1162. for(y=0; y<16; y++)
  1163. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1164. }
  1165. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1166. int dm= 1 << (mv_sample_log2-2);
  1167. for(i=0; i<4; i++){
  1168. int sx= mb_x*16 + 8*(i&1);
  1169. int sy= mb_y*16 + 8*(i>>1);
  1170. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1171. //FIXME bidir
  1172. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1173. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1174. for(y=0; y<8; y++)
  1175. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1176. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1177. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1178. }
  1179. }
  1180. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1181. // hmm
  1182. }
  1183. }
  1184. s->mbskip_table[mb_index]=0;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. static inline int hpel_motion_lowres(MpegEncContext *s,
  1190. uint8_t *dest, uint8_t *src,
  1191. int field_based, int field_select,
  1192. int src_x, int src_y,
  1193. int width, int height, int stride,
  1194. int h_edge_pos, int v_edge_pos,
  1195. int w, int h, h264_chroma_mc_func *pix_op,
  1196. int motion_x, int motion_y)
  1197. {
  1198. const int lowres= s->avctx->lowres;
  1199. const int s_mask= (2<<lowres)-1;
  1200. int emu=0;
  1201. int sx, sy;
  1202. if(s->quarter_sample){
  1203. motion_x/=2;
  1204. motion_y/=2;
  1205. }
  1206. sx= motion_x & s_mask;
  1207. sy= motion_y & s_mask;
  1208. src_x += motion_x >> (lowres+1);
  1209. src_y += motion_y >> (lowres+1);
  1210. src += src_y * stride + src_x;
  1211. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1212. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1213. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1214. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1215. src= s->edge_emu_buffer;
  1216. emu=1;
  1217. }
  1218. sx <<= 2 - lowres;
  1219. sy <<= 2 - lowres;
  1220. if(field_select)
  1221. src += s->linesize;
  1222. pix_op[lowres](dest, src, stride, h, sx, sy);
  1223. return emu;
  1224. }
  1225. /* apply one mpeg motion vector to the three components */
  1226. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1227. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1228. int field_based, int bottom_field, int field_select,
  1229. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1230. int motion_x, int motion_y, int h)
  1231. {
  1232. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1233. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1234. const int lowres= s->avctx->lowres;
  1235. const int block_s= 8>>lowres;
  1236. const int s_mask= (2<<lowres)-1;
  1237. const int h_edge_pos = s->h_edge_pos >> lowres;
  1238. const int v_edge_pos = s->v_edge_pos >> lowres;
  1239. linesize = s->current_picture.linesize[0] << field_based;
  1240. uvlinesize = s->current_picture.linesize[1] << field_based;
  1241. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1242. motion_x/=2;
  1243. motion_y/=2;
  1244. }
  1245. if(field_based){
  1246. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1247. }
  1248. sx= motion_x & s_mask;
  1249. sy= motion_y & s_mask;
  1250. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1251. src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1252. if (s->out_format == FMT_H263) {
  1253. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1254. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1255. uvsrc_x = src_x>>1;
  1256. uvsrc_y = src_y>>1;
  1257. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1258. mx = motion_x / 4;
  1259. my = motion_y / 4;
  1260. uvsx = (2*mx) & s_mask;
  1261. uvsy = (2*my) & s_mask;
  1262. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1263. uvsrc_y = s->mb_y*block_s + (my >> lowres);
  1264. } else {
  1265. mx = motion_x / 2;
  1266. my = motion_y / 2;
  1267. uvsx = mx & s_mask;
  1268. uvsy = my & s_mask;
  1269. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1270. uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
  1271. }
  1272. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1273. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1274. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1275. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1276. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1277. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1278. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1279. ptr_y = s->edge_emu_buffer;
  1280. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1281. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1282. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1283. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1284. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1285. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1286. ptr_cb= uvbuf;
  1287. ptr_cr= uvbuf+16;
  1288. }
  1289. }
  1290. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1291. dest_y += s->linesize;
  1292. dest_cb+= s->uvlinesize;
  1293. dest_cr+= s->uvlinesize;
  1294. }
  1295. if(field_select){
  1296. ptr_y += s->linesize;
  1297. ptr_cb+= s->uvlinesize;
  1298. ptr_cr+= s->uvlinesize;
  1299. }
  1300. sx <<= 2 - lowres;
  1301. sy <<= 2 - lowres;
  1302. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1303. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1304. uvsx <<= 2 - lowres;
  1305. uvsy <<= 2 - lowres;
  1306. pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1307. pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1308. }
  1309. //FIXME h261 lowres loop filter
  1310. }
  1311. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1312. uint8_t *dest_cb, uint8_t *dest_cr,
  1313. uint8_t **ref_picture,
  1314. h264_chroma_mc_func *pix_op,
  1315. int mx, int my){
  1316. const int lowres= s->avctx->lowres;
  1317. const int block_s= 8>>lowres;
  1318. const int s_mask= (2<<lowres)-1;
  1319. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1320. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1321. int emu=0, src_x, src_y, offset, sx, sy;
  1322. uint8_t *ptr;
  1323. if(s->quarter_sample){
  1324. mx/=2;
  1325. my/=2;
  1326. }
  1327. /* In case of 8X8, we construct a single chroma motion vector
  1328. with a special rounding */
  1329. mx= ff_h263_round_chroma(mx);
  1330. my= ff_h263_round_chroma(my);
  1331. sx= mx & s_mask;
  1332. sy= my & s_mask;
  1333. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1334. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1335. offset = src_y * s->uvlinesize + src_x;
  1336. ptr = ref_picture[1] + offset;
  1337. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1338. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1339. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1340. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1341. ptr= s->edge_emu_buffer;
  1342. emu=1;
  1343. }
  1344. }
  1345. sx <<= 2 - lowres;
  1346. sy <<= 2 - lowres;
  1347. pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1348. ptr = ref_picture[2] + offset;
  1349. if(emu){
  1350. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1351. ptr= s->edge_emu_buffer;
  1352. }
  1353. pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1354. }
  1355. /**
  1356. * motion compensation of a single macroblock
  1357. * @param s context
  1358. * @param dest_y luma destination pointer
  1359. * @param dest_cb chroma cb/u destination pointer
  1360. * @param dest_cr chroma cr/v destination pointer
  1361. * @param dir direction (0->forward, 1->backward)
  1362. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1363. * @param pic_op halfpel motion compensation function (average or put normally)
  1364. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1365. */
  1366. static inline void MPV_motion_lowres(MpegEncContext *s,
  1367. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1368. int dir, uint8_t **ref_picture,
  1369. h264_chroma_mc_func *pix_op)
  1370. {
  1371. int mx, my;
  1372. int mb_x, mb_y, i;
  1373. const int lowres= s->avctx->lowres;
  1374. const int block_s= 8>>lowres;
  1375. mb_x = s->mb_x;
  1376. mb_y = s->mb_y;
  1377. switch(s->mv_type) {
  1378. case MV_TYPE_16X16:
  1379. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1380. 0, 0, 0,
  1381. ref_picture, pix_op,
  1382. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1383. break;
  1384. case MV_TYPE_8X8:
  1385. mx = 0;
  1386. my = 0;
  1387. for(i=0;i<4;i++) {
  1388. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1389. ref_picture[0], 0, 0,
  1390. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1391. s->width, s->height, s->linesize,
  1392. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1393. block_s, block_s, pix_op,
  1394. s->mv[dir][i][0], s->mv[dir][i][1]);
  1395. mx += s->mv[dir][i][0];
  1396. my += s->mv[dir][i][1];
  1397. }
  1398. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1399. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1400. break;
  1401. case MV_TYPE_FIELD:
  1402. if (s->picture_structure == PICT_FRAME) {
  1403. /* top field */
  1404. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1405. 1, 0, s->field_select[dir][0],
  1406. ref_picture, pix_op,
  1407. s->mv[dir][0][0], s->mv[dir][0][1], block_s);
  1408. /* bottom field */
  1409. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1410. 1, 1, s->field_select[dir][1],
  1411. ref_picture, pix_op,
  1412. s->mv[dir][1][0], s->mv[dir][1][1], block_s);
  1413. } else {
  1414. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != FF_B_TYPE && !s->first_field){
  1415. ref_picture= s->current_picture_ptr->data;
  1416. }
  1417. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1418. 0, 0, s->field_select[dir][0],
  1419. ref_picture, pix_op,
  1420. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1421. }
  1422. break;
  1423. case MV_TYPE_16X8:
  1424. for(i=0; i<2; i++){
  1425. uint8_t ** ref2picture;
  1426. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == FF_B_TYPE || s->first_field){
  1427. ref2picture= ref_picture;
  1428. }else{
  1429. ref2picture= s->current_picture_ptr->data;
  1430. }
  1431. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1432. 0, 0, s->field_select[dir][i],
  1433. ref2picture, pix_op,
  1434. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
  1435. dest_y += 2*block_s*s->linesize;
  1436. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1437. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1438. }
  1439. break;
  1440. case MV_TYPE_DMV:
  1441. if(s->picture_structure == PICT_FRAME){
  1442. for(i=0; i<2; i++){
  1443. int j;
  1444. for(j=0; j<2; j++){
  1445. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1446. 1, j, j^i,
  1447. ref_picture, pix_op,
  1448. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
  1449. }
  1450. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1451. }
  1452. }else{
  1453. for(i=0; i<2; i++){
  1454. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1455. 0, 0, s->picture_structure != i+1,
  1456. ref_picture, pix_op,
  1457. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
  1458. // after put we make avg of the same block
  1459. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1460. //opposite parity is always in the same frame if this is second field
  1461. if(!s->first_field){
  1462. ref_picture = s->current_picture_ptr->data;
  1463. }
  1464. }
  1465. }
  1466. break;
  1467. default: assert(0);
  1468. }
  1469. }
  1470. /* put block[] to dest[] */
  1471. static inline void put_dct(MpegEncContext *s,
  1472. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1473. {
  1474. s->dct_unquantize_intra(s, block, i, qscale);
  1475. s->dsp.idct_put (dest, line_size, block);
  1476. }
  1477. /* add block[] to dest[] */
  1478. static inline void add_dct(MpegEncContext *s,
  1479. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1480. {
  1481. if (s->block_last_index[i] >= 0) {
  1482. s->dsp.idct_add (dest, line_size, block);
  1483. }
  1484. }
  1485. static inline void add_dequant_dct(MpegEncContext *s,
  1486. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1487. {
  1488. if (s->block_last_index[i] >= 0) {
  1489. s->dct_unquantize_inter(s, block, i, qscale);
  1490. s->dsp.idct_add (dest, line_size, block);
  1491. }
  1492. }
  1493. /**
  1494. * cleans dc, ac, coded_block for the current non intra MB
  1495. */
  1496. void ff_clean_intra_table_entries(MpegEncContext *s)
  1497. {
  1498. int wrap = s->b8_stride;
  1499. int xy = s->block_index[0];
  1500. s->dc_val[0][xy ] =
  1501. s->dc_val[0][xy + 1 ] =
  1502. s->dc_val[0][xy + wrap] =
  1503. s->dc_val[0][xy + 1 + wrap] = 1024;
  1504. /* ac pred */
  1505. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1506. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1507. if (s->msmpeg4_version>=3) {
  1508. s->coded_block[xy ] =
  1509. s->coded_block[xy + 1 ] =
  1510. s->coded_block[xy + wrap] =
  1511. s->coded_block[xy + 1 + wrap] = 0;
  1512. }
  1513. /* chroma */
  1514. wrap = s->mb_stride;
  1515. xy = s->mb_x + s->mb_y * wrap;
  1516. s->dc_val[1][xy] =
  1517. s->dc_val[2][xy] = 1024;
  1518. /* ac pred */
  1519. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1520. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1521. s->mbintra_table[xy]= 0;
  1522. }
  1523. /* generic function called after a macroblock has been parsed by the
  1524. decoder or after it has been encoded by the encoder.
  1525. Important variables used:
  1526. s->mb_intra : true if intra macroblock
  1527. s->mv_dir : motion vector direction
  1528. s->mv_type : motion vector type
  1529. s->mv : motion vector
  1530. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1531. */
  1532. static av_always_inline
  1533. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1534. int lowres_flag, int is_mpeg12)
  1535. {
  1536. int mb_x, mb_y;
  1537. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1538. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1539. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1540. return;
  1541. }
  1542. mb_x = s->mb_x;
  1543. mb_y = s->mb_y;
  1544. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1545. /* save DCT coefficients */
  1546. int i,j;
  1547. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1548. for(i=0; i<6; i++)
  1549. for(j=0; j<64; j++)
  1550. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1551. }
  1552. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1553. /* update DC predictors for P macroblocks */
  1554. if (!s->mb_intra) {
  1555. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1556. if(s->mbintra_table[mb_xy])
  1557. ff_clean_intra_table_entries(s);
  1558. } else {
  1559. s->last_dc[0] =
  1560. s->last_dc[1] =
  1561. s->last_dc[2] = 128 << s->intra_dc_precision;
  1562. }
  1563. }
  1564. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1565. s->mbintra_table[mb_xy]=1;
  1566. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==FF_B_TYPE) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1567. uint8_t *dest_y, *dest_cb, *dest_cr;
  1568. int dct_linesize, dct_offset;
  1569. op_pixels_func (*op_pix)[4];
  1570. qpel_mc_func (*op_qpix)[16];
  1571. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1572. const int uvlinesize= s->current_picture.linesize[1];
  1573. const int readable= s->pict_type != FF_B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1574. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1575. /* avoid copy if macroblock skipped in last frame too */
  1576. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1577. if(!s->encoding){
  1578. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1579. const int age= s->current_picture.age;
  1580. assert(age);
  1581. if (s->mb_skipped) {
  1582. s->mb_skipped= 0;
  1583. assert(s->pict_type!=FF_I_TYPE);
  1584. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1585. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1586. /* if previous was skipped too, then nothing to do ! */
  1587. if (*mbskip_ptr >= age && s->current_picture.reference){
  1588. return;
  1589. }
  1590. } else if(!s->current_picture.reference){
  1591. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1592. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1593. } else{
  1594. *mbskip_ptr = 0; /* not skipped */
  1595. }
  1596. }
  1597. dct_linesize = linesize << s->interlaced_dct;
  1598. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1599. if(readable){
  1600. dest_y= s->dest[0];
  1601. dest_cb= s->dest[1];
  1602. dest_cr= s->dest[2];
  1603. }else{
  1604. dest_y = s->b_scratchpad;
  1605. dest_cb= s->b_scratchpad+16*linesize;
  1606. dest_cr= s->b_scratchpad+32*linesize;
  1607. }
  1608. if (!s->mb_intra) {
  1609. /* motion handling */
  1610. /* decoding or more than one mb_type (MC was already done otherwise) */
  1611. if(!s->encoding){
  1612. if(lowres_flag){
  1613. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1614. if (s->mv_dir & MV_DIR_FORWARD) {
  1615. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1616. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1617. }
  1618. if (s->mv_dir & MV_DIR_BACKWARD) {
  1619. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1620. }
  1621. }else{
  1622. op_qpix= s->me.qpel_put;
  1623. if ((!s->no_rounding) || s->pict_type==FF_B_TYPE){
  1624. op_pix = s->dsp.put_pixels_tab;
  1625. }else{
  1626. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1627. }
  1628. if (s->mv_dir & MV_DIR_FORWARD) {
  1629. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1630. op_pix = s->dsp.avg_pixels_tab;
  1631. op_qpix= s->me.qpel_avg;
  1632. }
  1633. if (s->mv_dir & MV_DIR_BACKWARD) {
  1634. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1635. }
  1636. }
  1637. }
  1638. /* skip dequant / idct if we are really late ;) */
  1639. if(s->hurry_up>1) goto skip_idct;
  1640. if(s->avctx->skip_idct){
  1641. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == FF_B_TYPE)
  1642. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != FF_I_TYPE)
  1643. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1644. goto skip_idct;
  1645. }
  1646. /* add dct residue */
  1647. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1648. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1649. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1650. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1651. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1652. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1653. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1654. if (s->chroma_y_shift){
  1655. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1656. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1657. }else{
  1658. dct_linesize >>= 1;
  1659. dct_offset >>=1;
  1660. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1661. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1662. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1663. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1664. }
  1665. }
  1666. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1667. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1668. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1669. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1670. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1671. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1672. if(s->chroma_y_shift){//Chroma420
  1673. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1674. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1675. }else{
  1676. //chroma422
  1677. dct_linesize = uvlinesize << s->interlaced_dct;
  1678. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1679. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1680. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1681. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1682. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1683. if(!s->chroma_x_shift){//Chroma444
  1684. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1685. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1686. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1687. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1688. }
  1689. }
  1690. }//fi gray
  1691. }
  1692. else if (CONFIG_WMV2) {
  1693. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1694. }
  1695. } else {
  1696. /* dct only in intra block */
  1697. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1698. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1699. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1700. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1701. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1702. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1703. if(s->chroma_y_shift){
  1704. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1705. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1706. }else{
  1707. dct_offset >>=1;
  1708. dct_linesize >>=1;
  1709. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1710. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1711. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1712. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1713. }
  1714. }
  1715. }else{
  1716. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1717. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1718. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1719. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1720. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1721. if(s->chroma_y_shift){
  1722. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1723. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1724. }else{
  1725. dct_linesize = uvlinesize << s->interlaced_dct;
  1726. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1727. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1728. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1729. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1730. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1731. if(!s->chroma_x_shift){//Chroma444
  1732. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1733. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1734. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1735. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1736. }
  1737. }
  1738. }//gray
  1739. }
  1740. }
  1741. skip_idct:
  1742. if(!readable){
  1743. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1744. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1745. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1746. }
  1747. }
  1748. }
  1749. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1750. #if !CONFIG_SMALL
  1751. if(s->out_format == FMT_MPEG1) {
  1752. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  1753. else MPV_decode_mb_internal(s, block, 0, 1);
  1754. } else
  1755. #endif
  1756. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  1757. else MPV_decode_mb_internal(s, block, 0, 0);
  1758. }
  1759. /**
  1760. *
  1761. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1762. */
  1763. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1764. if (s->avctx->draw_horiz_band) {
  1765. AVFrame *src;
  1766. int offset[4];
  1767. if(s->picture_structure != PICT_FRAME){
  1768. h <<= 1;
  1769. y <<= 1;
  1770. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1771. }
  1772. h= FFMIN(h, s->avctx->height - y);
  1773. if(s->pict_type==FF_B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  1774. src= (AVFrame*)s->current_picture_ptr;
  1775. else if(s->last_picture_ptr)
  1776. src= (AVFrame*)s->last_picture_ptr;
  1777. else
  1778. return;
  1779. if(s->pict_type==FF_B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  1780. offset[0]=
  1781. offset[1]=
  1782. offset[2]=
  1783. offset[3]= 0;
  1784. }else{
  1785. offset[0]= y * s->linesize;
  1786. offset[1]=
  1787. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  1788. offset[3]= 0;
  1789. }
  1790. emms_c();
  1791. s->avctx->draw_horiz_band(s->avctx, src, offset,
  1792. y, s->picture_structure, h);
  1793. }
  1794. }
  1795. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1796. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1797. const int uvlinesize= s->current_picture.linesize[1];
  1798. const int mb_size= 4 - s->avctx->lowres;
  1799. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1800. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1801. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1802. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1803. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1804. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1805. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1806. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  1807. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1808. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1809. if(!(s->pict_type==FF_B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1810. {
  1811. s->dest[0] += s->mb_y * linesize << mb_size;
  1812. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1813. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1814. }
  1815. }
  1816. void ff_mpeg_flush(AVCodecContext *avctx){
  1817. int i;
  1818. MpegEncContext *s = avctx->priv_data;
  1819. if(s==NULL || s->picture==NULL)
  1820. return;
  1821. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1822. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  1823. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  1824. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  1825. }
  1826. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1827. s->mb_x= s->mb_y= 0;
  1828. s->parse_context.state= -1;
  1829. s->parse_context.frame_start_found= 0;
  1830. s->parse_context.overread= 0;
  1831. s->parse_context.overread_index= 0;
  1832. s->parse_context.index= 0;
  1833. s->parse_context.last_index= 0;
  1834. s->bitstream_buffer_size=0;
  1835. s->pp_time=0;
  1836. }
  1837. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  1838. DCTELEM *block, int n, int qscale)
  1839. {
  1840. int i, level, nCoeffs;
  1841. const uint16_t *quant_matrix;
  1842. nCoeffs= s->block_last_index[n];
  1843. if (n < 4)
  1844. block[0] = block[0] * s->y_dc_scale;
  1845. else
  1846. block[0] = block[0] * s->c_dc_scale;
  1847. /* XXX: only mpeg1 */
  1848. quant_matrix = s->intra_matrix;
  1849. for(i=1;i<=nCoeffs;i++) {
  1850. int j= s->intra_scantable.permutated[i];
  1851. level = block[j];
  1852. if (level) {
  1853. if (level < 0) {
  1854. level = -level;
  1855. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1856. level = (level - 1) | 1;
  1857. level = -level;
  1858. } else {
  1859. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1860. level = (level - 1) | 1;
  1861. }
  1862. block[j] = level;
  1863. }
  1864. }
  1865. }
  1866. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  1867. DCTELEM *block, int n, int qscale)
  1868. {
  1869. int i, level, nCoeffs;
  1870. const uint16_t *quant_matrix;
  1871. nCoeffs= s->block_last_index[n];
  1872. quant_matrix = s->inter_matrix;
  1873. for(i=0; i<=nCoeffs; i++) {
  1874. int j= s->intra_scantable.permutated[i];
  1875. level = block[j];
  1876. if (level) {
  1877. if (level < 0) {
  1878. level = -level;
  1879. level = (((level << 1) + 1) * qscale *
  1880. ((int) (quant_matrix[j]))) >> 4;
  1881. level = (level - 1) | 1;
  1882. level = -level;
  1883. } else {
  1884. level = (((level << 1) + 1) * qscale *
  1885. ((int) (quant_matrix[j]))) >> 4;
  1886. level = (level - 1) | 1;
  1887. }
  1888. block[j] = level;
  1889. }
  1890. }
  1891. }
  1892. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  1893. DCTELEM *block, int n, int qscale)
  1894. {
  1895. int i, level, nCoeffs;
  1896. const uint16_t *quant_matrix;
  1897. if(s->alternate_scan) nCoeffs= 63;
  1898. else nCoeffs= s->block_last_index[n];
  1899. if (n < 4)
  1900. block[0] = block[0] * s->y_dc_scale;
  1901. else
  1902. block[0] = block[0] * s->c_dc_scale;
  1903. quant_matrix = s->intra_matrix;
  1904. for(i=1;i<=nCoeffs;i++) {
  1905. int j= s->intra_scantable.permutated[i];
  1906. level = block[j];
  1907. if (level) {
  1908. if (level < 0) {
  1909. level = -level;
  1910. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1911. level = -level;
  1912. } else {
  1913. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1914. }
  1915. block[j] = level;
  1916. }
  1917. }
  1918. }
  1919. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  1920. DCTELEM *block, int n, int qscale)
  1921. {
  1922. int i, level, nCoeffs;
  1923. const uint16_t *quant_matrix;
  1924. int sum=-1;
  1925. if(s->alternate_scan) nCoeffs= 63;
  1926. else nCoeffs= s->block_last_index[n];
  1927. if (n < 4)
  1928. block[0] = block[0] * s->y_dc_scale;
  1929. else
  1930. block[0] = block[0] * s->c_dc_scale;
  1931. quant_matrix = s->intra_matrix;
  1932. for(i=1;i<=nCoeffs;i++) {
  1933. int j= s->intra_scantable.permutated[i];
  1934. level = block[j];
  1935. if (level) {
  1936. if (level < 0) {
  1937. level = -level;
  1938. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1939. level = -level;
  1940. } else {
  1941. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1942. }
  1943. block[j] = level;
  1944. sum+=level;
  1945. }
  1946. }
  1947. block[63]^=sum&1;
  1948. }
  1949. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  1950. DCTELEM *block, int n, int qscale)
  1951. {
  1952. int i, level, nCoeffs;
  1953. const uint16_t *quant_matrix;
  1954. int sum=-1;
  1955. if(s->alternate_scan) nCoeffs= 63;
  1956. else nCoeffs= s->block_last_index[n];
  1957. quant_matrix = s->inter_matrix;
  1958. for(i=0; i<=nCoeffs; i++) {
  1959. int j= s->intra_scantable.permutated[i];
  1960. level = block[j];
  1961. if (level) {
  1962. if (level < 0) {
  1963. level = -level;
  1964. level = (((level << 1) + 1) * qscale *
  1965. ((int) (quant_matrix[j]))) >> 4;
  1966. level = -level;
  1967. } else {
  1968. level = (((level << 1) + 1) * qscale *
  1969. ((int) (quant_matrix[j]))) >> 4;
  1970. }
  1971. block[j] = level;
  1972. sum+=level;
  1973. }
  1974. }
  1975. block[63]^=sum&1;
  1976. }
  1977. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  1978. DCTELEM *block, int n, int qscale)
  1979. {
  1980. int i, level, qmul, qadd;
  1981. int nCoeffs;
  1982. assert(s->block_last_index[n]>=0);
  1983. qmul = qscale << 1;
  1984. if (!s->h263_aic) {
  1985. if (n < 4)
  1986. block[0] = block[0] * s->y_dc_scale;
  1987. else
  1988. block[0] = block[0] * s->c_dc_scale;
  1989. qadd = (qscale - 1) | 1;
  1990. }else{
  1991. qadd = 0;
  1992. }
  1993. if(s->ac_pred)
  1994. nCoeffs=63;
  1995. else
  1996. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  1997. for(i=1; i<=nCoeffs; i++) {
  1998. level = block[i];
  1999. if (level) {
  2000. if (level < 0) {
  2001. level = level * qmul - qadd;
  2002. } else {
  2003. level = level * qmul + qadd;
  2004. }
  2005. block[i] = level;
  2006. }
  2007. }
  2008. }
  2009. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2010. DCTELEM *block, int n, int qscale)
  2011. {
  2012. int i, level, qmul, qadd;
  2013. int nCoeffs;
  2014. assert(s->block_last_index[n]>=0);
  2015. qadd = (qscale - 1) | 1;
  2016. qmul = qscale << 1;
  2017. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2018. for(i=0; i<=nCoeffs; i++) {
  2019. level = block[i];
  2020. if (level) {
  2021. if (level < 0) {
  2022. level = level * qmul - qadd;
  2023. } else {
  2024. level = level * qmul + qadd;
  2025. }
  2026. block[i] = level;
  2027. }
  2028. }
  2029. }
  2030. /**
  2031. * set qscale and update qscale dependent variables.
  2032. */
  2033. void ff_set_qscale(MpegEncContext * s, int qscale)
  2034. {
  2035. if (qscale < 1)
  2036. qscale = 1;
  2037. else if (qscale > 31)
  2038. qscale = 31;
  2039. s->qscale = qscale;
  2040. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2041. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2042. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2043. }