2
0

mpegvideo.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  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
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/motion_vector.h"
  33. #include "libavutil/timer.h"
  34. #include "avcodec.h"
  35. #include "blockdsp.h"
  36. #include "h264chroma.h"
  37. #include "idctdsp.h"
  38. #include "internal.h"
  39. #include "mathops.h"
  40. #include "mpeg_er.h"
  41. #include "mpegutils.h"
  42. #include "mpegvideo.h"
  43. #include "mpegvideodata.h"
  44. #include "mjpegenc.h"
  45. #include "msmpeg4.h"
  46. #include "qpeldsp.h"
  47. #include "thread.h"
  48. #include "wmv2.h"
  49. #include <limits.h>
  50. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  51. int16_t *block, int n, int qscale)
  52. {
  53. int i, level, nCoeffs;
  54. const uint16_t *quant_matrix;
  55. nCoeffs= s->block_last_index[n];
  56. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  57. /* XXX: only MPEG-1 */
  58. quant_matrix = s->intra_matrix;
  59. for(i=1;i<=nCoeffs;i++) {
  60. int j= s->intra_scantable.permutated[i];
  61. level = block[j];
  62. if (level) {
  63. if (level < 0) {
  64. level = -level;
  65. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  66. level = (level - 1) | 1;
  67. level = -level;
  68. } else {
  69. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  70. level = (level - 1) | 1;
  71. }
  72. block[j] = level;
  73. }
  74. }
  75. }
  76. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  77. int16_t *block, int n, int qscale)
  78. {
  79. int i, level, nCoeffs;
  80. const uint16_t *quant_matrix;
  81. nCoeffs= s->block_last_index[n];
  82. quant_matrix = s->inter_matrix;
  83. for(i=0; i<=nCoeffs; i++) {
  84. int j= s->intra_scantable.permutated[i];
  85. level = block[j];
  86. if (level) {
  87. if (level < 0) {
  88. level = -level;
  89. level = (((level << 1) + 1) * qscale *
  90. ((int) (quant_matrix[j]))) >> 4;
  91. level = (level - 1) | 1;
  92. level = -level;
  93. } else {
  94. level = (((level << 1) + 1) * qscale *
  95. ((int) (quant_matrix[j]))) >> 4;
  96. level = (level - 1) | 1;
  97. }
  98. block[j] = level;
  99. }
  100. }
  101. }
  102. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  103. int16_t *block, int n, int qscale)
  104. {
  105. int i, level, nCoeffs;
  106. const uint16_t *quant_matrix;
  107. if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
  108. else qscale <<= 1;
  109. if(s->alternate_scan) nCoeffs= 63;
  110. else nCoeffs= s->block_last_index[n];
  111. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  112. quant_matrix = s->intra_matrix;
  113. for(i=1;i<=nCoeffs;i++) {
  114. int j= s->intra_scantable.permutated[i];
  115. level = block[j];
  116. if (level) {
  117. if (level < 0) {
  118. level = -level;
  119. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  120. level = -level;
  121. } else {
  122. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  123. }
  124. block[j] = level;
  125. }
  126. }
  127. }
  128. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  129. int16_t *block, int n, int qscale)
  130. {
  131. int i, level, nCoeffs;
  132. const uint16_t *quant_matrix;
  133. int sum=-1;
  134. if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
  135. else qscale <<= 1;
  136. if(s->alternate_scan) nCoeffs= 63;
  137. else nCoeffs= s->block_last_index[n];
  138. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  139. sum += block[0];
  140. quant_matrix = s->intra_matrix;
  141. for(i=1;i<=nCoeffs;i++) {
  142. int j= s->intra_scantable.permutated[i];
  143. level = block[j];
  144. if (level) {
  145. if (level < 0) {
  146. level = -level;
  147. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  148. level = -level;
  149. } else {
  150. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  151. }
  152. block[j] = level;
  153. sum+=level;
  154. }
  155. }
  156. block[63]^=sum&1;
  157. }
  158. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  159. int16_t *block, int n, int qscale)
  160. {
  161. int i, level, nCoeffs;
  162. const uint16_t *quant_matrix;
  163. int sum=-1;
  164. if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
  165. else qscale <<= 1;
  166. if(s->alternate_scan) nCoeffs= 63;
  167. else nCoeffs= s->block_last_index[n];
  168. quant_matrix = s->inter_matrix;
  169. for(i=0; i<=nCoeffs; i++) {
  170. int j= s->intra_scantable.permutated[i];
  171. level = block[j];
  172. if (level) {
  173. if (level < 0) {
  174. level = -level;
  175. level = (((level << 1) + 1) * qscale *
  176. ((int) (quant_matrix[j]))) >> 5;
  177. level = -level;
  178. } else {
  179. level = (((level << 1) + 1) * qscale *
  180. ((int) (quant_matrix[j]))) >> 5;
  181. }
  182. block[j] = level;
  183. sum+=level;
  184. }
  185. }
  186. block[63]^=sum&1;
  187. }
  188. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  189. int16_t *block, int n, int qscale)
  190. {
  191. int i, level, qmul, qadd;
  192. int nCoeffs;
  193. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  194. qmul = qscale << 1;
  195. if (!s->h263_aic) {
  196. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  197. qadd = (qscale - 1) | 1;
  198. }else{
  199. qadd = 0;
  200. }
  201. if(s->ac_pred)
  202. nCoeffs=63;
  203. else
  204. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  205. for(i=1; i<=nCoeffs; i++) {
  206. level = block[i];
  207. if (level) {
  208. if (level < 0) {
  209. level = level * qmul - qadd;
  210. } else {
  211. level = level * qmul + qadd;
  212. }
  213. block[i] = level;
  214. }
  215. }
  216. }
  217. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  218. int16_t *block, int n, int qscale)
  219. {
  220. int i, level, qmul, qadd;
  221. int nCoeffs;
  222. av_assert2(s->block_last_index[n]>=0);
  223. qadd = (qscale - 1) | 1;
  224. qmul = qscale << 1;
  225. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  226. for(i=0; i<=nCoeffs; i++) {
  227. level = block[i];
  228. if (level) {
  229. if (level < 0) {
  230. level = level * qmul - qadd;
  231. } else {
  232. level = level * qmul + qadd;
  233. }
  234. block[i] = level;
  235. }
  236. }
  237. }
  238. static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
  239. {
  240. while(h--)
  241. memset(dst + h*linesize, 128, 16);
  242. }
  243. static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
  244. {
  245. while(h--)
  246. memset(dst + h*linesize, 128, 8);
  247. }
  248. /* init common dct for both encoder and decoder */
  249. static av_cold int dct_init(MpegEncContext *s)
  250. {
  251. ff_blockdsp_init(&s->bdsp, s->avctx);
  252. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  253. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  254. ff_mpegvideodsp_init(&s->mdsp);
  255. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  256. if (s->avctx->debug & FF_DEBUG_NOMC) {
  257. int i;
  258. for (i=0; i<4; i++) {
  259. s->hdsp.avg_pixels_tab[0][i] = gray16;
  260. s->hdsp.put_pixels_tab[0][i] = gray16;
  261. s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
  262. s->hdsp.avg_pixels_tab[1][i] = gray8;
  263. s->hdsp.put_pixels_tab[1][i] = gray8;
  264. s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
  265. }
  266. }
  267. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  268. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  269. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  270. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  271. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  272. if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
  273. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  274. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  275. if (HAVE_INTRINSICS_NEON)
  276. ff_mpv_common_init_neon(s);
  277. if (ARCH_ALPHA)
  278. ff_mpv_common_init_axp(s);
  279. if (ARCH_ARM)
  280. ff_mpv_common_init_arm(s);
  281. if (ARCH_PPC)
  282. ff_mpv_common_init_ppc(s);
  283. if (ARCH_X86)
  284. ff_mpv_common_init_x86(s);
  285. if (ARCH_MIPS)
  286. ff_mpv_common_init_mips(s);
  287. return 0;
  288. }
  289. av_cold void ff_mpv_idct_init(MpegEncContext *s)
  290. {
  291. if (s->codec_id == AV_CODEC_ID_MPEG4)
  292. s->idsp.mpeg4_studio_profile = s->studio_profile;
  293. ff_idctdsp_init(&s->idsp, s->avctx);
  294. /* load & permutate scantables
  295. * note: only wmv uses different ones
  296. */
  297. if (s->alternate_scan) {
  298. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  299. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  300. } else {
  301. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  302. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  303. }
  304. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  305. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  306. }
  307. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  308. {
  309. return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
  310. s->chroma_x_shift, s->chroma_y_shift, s->out_format,
  311. s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
  312. &s->linesize, &s->uvlinesize);
  313. }
  314. static int init_duplicate_context(MpegEncContext *s)
  315. {
  316. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  317. int c_size = s->mb_stride * (s->mb_height + 1);
  318. int yc_size = y_size + 2 * c_size;
  319. int i;
  320. if (s->mb_height & 1)
  321. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  322. s->sc.edge_emu_buffer =
  323. s->me.scratchpad =
  324. s->me.temp =
  325. s->sc.rd_scratchpad =
  326. s->sc.b_scratchpad =
  327. s->sc.obmc_scratchpad = NULL;
  328. if (s->encoding) {
  329. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  330. ME_MAP_SIZE * sizeof(uint32_t), fail)
  331. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  332. ME_MAP_SIZE * sizeof(uint32_t), fail)
  333. if (s->noise_reduction) {
  334. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  335. 2 * 64 * sizeof(int), fail)
  336. }
  337. }
  338. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  339. s->block = s->blocks[0];
  340. for (i = 0; i < 12; i++) {
  341. s->pblocks[i] = &s->block[i];
  342. }
  343. FF_ALLOCZ_OR_GOTO(s->avctx, s->block32, sizeof(*s->block32), fail)
  344. s->dpcm_direction = 0;
  345. FF_ALLOCZ_OR_GOTO(s->avctx, s->dpcm_macroblock, sizeof(*s->dpcm_macroblock), fail)
  346. if (s->avctx->codec_tag == AV_RL32("VCR2")) {
  347. // exchange uv
  348. FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
  349. }
  350. if (s->out_format == FMT_H263) {
  351. /* ac values */
  352. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  353. yc_size * sizeof(int16_t) * 16, fail);
  354. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  355. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  356. s->ac_val[2] = s->ac_val[1] + c_size;
  357. }
  358. return 0;
  359. fail:
  360. return -1; // free() through ff_mpv_common_end()
  361. }
  362. static void free_duplicate_context(MpegEncContext *s)
  363. {
  364. if (!s)
  365. return;
  366. av_freep(&s->sc.edge_emu_buffer);
  367. av_freep(&s->me.scratchpad);
  368. s->me.temp =
  369. s->sc.rd_scratchpad =
  370. s->sc.b_scratchpad =
  371. s->sc.obmc_scratchpad = NULL;
  372. av_freep(&s->dct_error_sum);
  373. av_freep(&s->me.map);
  374. av_freep(&s->me.score_map);
  375. av_freep(&s->blocks);
  376. av_freep(&s->block32);
  377. av_freep(&s->dpcm_macroblock);
  378. av_freep(&s->ac_val_base);
  379. s->block = NULL;
  380. }
  381. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  382. {
  383. #define COPY(a) bak->a = src->a
  384. COPY(sc.edge_emu_buffer);
  385. COPY(me.scratchpad);
  386. COPY(me.temp);
  387. COPY(sc.rd_scratchpad);
  388. COPY(sc.b_scratchpad);
  389. COPY(sc.obmc_scratchpad);
  390. COPY(me.map);
  391. COPY(me.score_map);
  392. COPY(blocks);
  393. COPY(block);
  394. COPY(block32);
  395. COPY(dpcm_macroblock);
  396. COPY(dpcm_direction);
  397. COPY(start_mb_y);
  398. COPY(end_mb_y);
  399. COPY(me.map_generation);
  400. COPY(pb);
  401. COPY(dct_error_sum);
  402. COPY(dct_count[0]);
  403. COPY(dct_count[1]);
  404. COPY(ac_val_base);
  405. COPY(ac_val[0]);
  406. COPY(ac_val[1]);
  407. COPY(ac_val[2]);
  408. #undef COPY
  409. }
  410. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  411. {
  412. MpegEncContext bak;
  413. int i, ret;
  414. // FIXME copy only needed parts
  415. // START_TIMER
  416. backup_duplicate_context(&bak, dst);
  417. memcpy(dst, src, sizeof(MpegEncContext));
  418. backup_duplicate_context(dst, &bak);
  419. for (i = 0; i < 12; i++) {
  420. dst->pblocks[i] = &dst->block[i];
  421. }
  422. if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
  423. // exchange uv
  424. FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
  425. }
  426. if (!dst->sc.edge_emu_buffer &&
  427. (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
  428. &dst->sc, dst->linesize)) < 0) {
  429. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  430. "scratch buffers.\n");
  431. return ret;
  432. }
  433. // STOP_TIMER("update_duplicate_context")
  434. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  435. return 0;
  436. }
  437. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  438. const AVCodecContext *src)
  439. {
  440. int i, ret;
  441. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  442. if (dst == src)
  443. return 0;
  444. av_assert0(s != s1);
  445. // FIXME can parameters change on I-frames?
  446. // in that case dst may need a reinit
  447. if (!s->context_initialized) {
  448. int err;
  449. memcpy(s, s1, sizeof(MpegEncContext));
  450. s->avctx = dst;
  451. s->bitstream_buffer = NULL;
  452. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  453. if (s1->context_initialized){
  454. // s->picture_range_start += MAX_PICTURE_COUNT;
  455. // s->picture_range_end += MAX_PICTURE_COUNT;
  456. ff_mpv_idct_init(s);
  457. if((err = ff_mpv_common_init(s)) < 0){
  458. memset(s, 0, sizeof(MpegEncContext));
  459. s->avctx = dst;
  460. return err;
  461. }
  462. }
  463. }
  464. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  465. s->context_reinit = 0;
  466. s->height = s1->height;
  467. s->width = s1->width;
  468. if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
  469. return ret;
  470. }
  471. s->avctx->coded_height = s1->avctx->coded_height;
  472. s->avctx->coded_width = s1->avctx->coded_width;
  473. s->avctx->width = s1->avctx->width;
  474. s->avctx->height = s1->avctx->height;
  475. s->coded_picture_number = s1->coded_picture_number;
  476. s->picture_number = s1->picture_number;
  477. av_assert0(!s->picture || s->picture != s1->picture);
  478. if(s->picture)
  479. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  480. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  481. if (s1->picture && s1->picture[i].f->buf[0] &&
  482. (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
  483. return ret;
  484. }
  485. #define UPDATE_PICTURE(pic)\
  486. do {\
  487. ff_mpeg_unref_picture(s->avctx, &s->pic);\
  488. if (s1->pic.f && s1->pic.f->buf[0])\
  489. ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
  490. else\
  491. ret = ff_update_picture_tables(&s->pic, &s1->pic);\
  492. if (ret < 0)\
  493. return ret;\
  494. } while (0)
  495. UPDATE_PICTURE(current_picture);
  496. UPDATE_PICTURE(last_picture);
  497. UPDATE_PICTURE(next_picture);
  498. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  499. ((pic && pic >= old_ctx->picture && \
  500. pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
  501. &new_ctx->picture[pic - old_ctx->picture] : NULL)
  502. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  503. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  504. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  505. // Error/bug resilience
  506. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  507. s->workaround_bugs = s1->workaround_bugs;
  508. s->padding_bug_score = s1->padding_bug_score;
  509. // MPEG-4 timing info
  510. memcpy(&s->last_time_base, &s1->last_time_base,
  511. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  512. (char *) &s1->last_time_base);
  513. // B-frame info
  514. s->max_b_frames = s1->max_b_frames;
  515. s->low_delay = s1->low_delay;
  516. s->droppable = s1->droppable;
  517. // DivX handling (doesn't work)
  518. s->divx_packed = s1->divx_packed;
  519. if (s1->bitstream_buffer) {
  520. if (s1->bitstream_buffer_size +
  521. AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) {
  522. av_fast_malloc(&s->bitstream_buffer,
  523. &s->allocated_bitstream_buffer_size,
  524. s1->allocated_bitstream_buffer_size);
  525. if (!s->bitstream_buffer) {
  526. s->bitstream_buffer_size = 0;
  527. return AVERROR(ENOMEM);
  528. }
  529. }
  530. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  531. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  532. s1->bitstream_buffer_size);
  533. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  534. AV_INPUT_BUFFER_PADDING_SIZE);
  535. }
  536. // linesize-dependent scratch buffer allocation
  537. if (!s->sc.edge_emu_buffer)
  538. if (s1->linesize) {
  539. if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
  540. &s->sc, s1->linesize) < 0) {
  541. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  542. "scratch buffers.\n");
  543. return AVERROR(ENOMEM);
  544. }
  545. } else {
  546. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  547. "be allocated due to unknown size.\n");
  548. }
  549. // MPEG-2/interlacing info
  550. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  551. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  552. if (!s1->first_field) {
  553. s->last_pict_type = s1->pict_type;
  554. if (s1->current_picture_ptr)
  555. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  556. }
  557. return 0;
  558. }
  559. /**
  560. * Set the given MpegEncContext to common defaults
  561. * (same for encoding and decoding).
  562. * The changed fields will not depend upon the
  563. * prior state of the MpegEncContext.
  564. */
  565. void ff_mpv_common_defaults(MpegEncContext *s)
  566. {
  567. s->y_dc_scale_table =
  568. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  569. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  570. s->progressive_frame = 1;
  571. s->progressive_sequence = 1;
  572. s->picture_structure = PICT_FRAME;
  573. s->coded_picture_number = 0;
  574. s->picture_number = 0;
  575. s->f_code = 1;
  576. s->b_code = 1;
  577. s->slice_context_count = 1;
  578. }
  579. /**
  580. * Set the given MpegEncContext to defaults for decoding.
  581. * the changed fields will not depend upon
  582. * the prior state of the MpegEncContext.
  583. */
  584. void ff_mpv_decode_defaults(MpegEncContext *s)
  585. {
  586. ff_mpv_common_defaults(s);
  587. }
  588. void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
  589. {
  590. s->avctx = avctx;
  591. s->width = avctx->coded_width;
  592. s->height = avctx->coded_height;
  593. s->codec_id = avctx->codec->id;
  594. s->workaround_bugs = avctx->workaround_bugs;
  595. /* convert fourcc to upper case */
  596. s->codec_tag = avpriv_toupper4(avctx->codec_tag);
  597. }
  598. /**
  599. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  600. */
  601. static int init_context_frame(MpegEncContext *s)
  602. {
  603. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  604. s->mb_width = (s->width + 15) / 16;
  605. s->mb_stride = s->mb_width + 1;
  606. s->b8_stride = s->mb_width * 2 + 1;
  607. mb_array_size = s->mb_height * s->mb_stride;
  608. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  609. /* set default edge pos, will be overridden
  610. * in decode_header if needed */
  611. s->h_edge_pos = s->mb_width * 16;
  612. s->v_edge_pos = s->mb_height * 16;
  613. s->mb_num = s->mb_width * s->mb_height;
  614. s->block_wrap[0] =
  615. s->block_wrap[1] =
  616. s->block_wrap[2] =
  617. s->block_wrap[3] = s->b8_stride;
  618. s->block_wrap[4] =
  619. s->block_wrap[5] = s->mb_stride;
  620. y_size = s->b8_stride * (2 * s->mb_height + 1);
  621. c_size = s->mb_stride * (s->mb_height + 1);
  622. yc_size = y_size + 2 * c_size;
  623. if (s->mb_height & 1)
  624. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  625. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  626. fail); // error resilience code looks cleaner with this
  627. for (y = 0; y < s->mb_height; y++)
  628. for (x = 0; x < s->mb_width; x++)
  629. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  630. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  631. if (s->encoding) {
  632. /* Allocate MV tables */
  633. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  634. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  635. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  636. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  637. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  638. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  639. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  640. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  641. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  642. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  643. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  644. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  645. /* Allocate MB type table */
  646. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  647. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  648. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  649. mb_array_size * sizeof(float), fail);
  650. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  651. mb_array_size * sizeof(float), fail);
  652. }
  653. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  654. (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
  655. /* interlaced direct mode decoding tables */
  656. for (i = 0; i < 2; i++) {
  657. int j, k;
  658. for (j = 0; j < 2; j++) {
  659. for (k = 0; k < 2; k++) {
  660. FF_ALLOCZ_OR_GOTO(s->avctx,
  661. s->b_field_mv_table_base[i][j][k],
  662. mv_table_size * 2 * sizeof(int16_t),
  663. fail);
  664. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  665. s->mb_stride + 1;
  666. }
  667. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  668. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  669. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  670. }
  671. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  672. }
  673. }
  674. if (s->out_format == FMT_H263) {
  675. /* cbp values */
  676. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  677. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  678. /* cbp, ac_pred, pred_dir */
  679. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  680. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  681. }
  682. if (s->h263_pred || s->h263_plus || !s->encoding) {
  683. /* dc values */
  684. // MN: we need these for error resilience of intra-frames
  685. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  686. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  687. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  688. s->dc_val[2] = s->dc_val[1] + c_size;
  689. for (i = 0; i < yc_size; i++)
  690. s->dc_val_base[i] = 1024;
  691. }
  692. /* which mb is an intra block */
  693. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  694. memset(s->mbintra_table, 1, mb_array_size);
  695. /* init macroblock skip table */
  696. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  697. // Note the + 1 is for a quicker MPEG-4 slice_end detection
  698. return ff_mpeg_er_init(s);
  699. fail:
  700. return AVERROR(ENOMEM);
  701. }
  702. static void clear_context(MpegEncContext *s)
  703. {
  704. int i, j, k;
  705. memset(&s->next_picture, 0, sizeof(s->next_picture));
  706. memset(&s->last_picture, 0, sizeof(s->last_picture));
  707. memset(&s->current_picture, 0, sizeof(s->current_picture));
  708. memset(&s->new_picture, 0, sizeof(s->new_picture));
  709. memset(s->thread_context, 0, sizeof(s->thread_context));
  710. s->me.map = NULL;
  711. s->me.score_map = NULL;
  712. s->dct_error_sum = NULL;
  713. s->block = NULL;
  714. s->blocks = NULL;
  715. s->block32 = NULL;
  716. memset(s->pblocks, 0, sizeof(s->pblocks));
  717. s->dpcm_direction = 0;
  718. s->dpcm_macroblock = NULL;
  719. s->ac_val_base = NULL;
  720. s->ac_val[0] =
  721. s->ac_val[1] =
  722. s->ac_val[2] =NULL;
  723. s->sc.edge_emu_buffer = NULL;
  724. s->me.scratchpad = NULL;
  725. s->me.temp =
  726. s->sc.rd_scratchpad =
  727. s->sc.b_scratchpad =
  728. s->sc.obmc_scratchpad = NULL;
  729. s->bitstream_buffer = NULL;
  730. s->allocated_bitstream_buffer_size = 0;
  731. s->picture = NULL;
  732. s->mb_type = NULL;
  733. s->p_mv_table_base = NULL;
  734. s->b_forw_mv_table_base = NULL;
  735. s->b_back_mv_table_base = NULL;
  736. s->b_bidir_forw_mv_table_base = NULL;
  737. s->b_bidir_back_mv_table_base = NULL;
  738. s->b_direct_mv_table_base = NULL;
  739. s->p_mv_table = NULL;
  740. s->b_forw_mv_table = NULL;
  741. s->b_back_mv_table = NULL;
  742. s->b_bidir_forw_mv_table = NULL;
  743. s->b_bidir_back_mv_table = NULL;
  744. s->b_direct_mv_table = NULL;
  745. for (i = 0; i < 2; i++) {
  746. for (j = 0; j < 2; j++) {
  747. for (k = 0; k < 2; k++) {
  748. s->b_field_mv_table_base[i][j][k] = NULL;
  749. s->b_field_mv_table[i][j][k] = NULL;
  750. }
  751. s->b_field_select_table[i][j] = NULL;
  752. s->p_field_mv_table_base[i][j] = NULL;
  753. s->p_field_mv_table[i][j] = NULL;
  754. }
  755. s->p_field_select_table[i] = NULL;
  756. }
  757. s->dc_val_base = NULL;
  758. s->coded_block_base = NULL;
  759. s->mbintra_table = NULL;
  760. s->cbp_table = NULL;
  761. s->pred_dir_table = NULL;
  762. s->mbskip_table = NULL;
  763. s->er.error_status_table = NULL;
  764. s->er.er_temp_buffer = NULL;
  765. s->mb_index2xy = NULL;
  766. s->lambda_table = NULL;
  767. s->cplx_tab = NULL;
  768. s->bits_tab = NULL;
  769. }
  770. /**
  771. * init common structure for both encoder and decoder.
  772. * this assumes that some variables like width/height are already set
  773. */
  774. av_cold int ff_mpv_common_init(MpegEncContext *s)
  775. {
  776. int i, ret;
  777. int nb_slices = (HAVE_THREADS &&
  778. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  779. s->avctx->thread_count : 1;
  780. clear_context(s);
  781. if (s->encoding && s->avctx->slices)
  782. nb_slices = s->avctx->slices;
  783. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  784. s->mb_height = (s->height + 31) / 32 * 2;
  785. else
  786. s->mb_height = (s->height + 15) / 16;
  787. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  788. av_log(s->avctx, AV_LOG_ERROR,
  789. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  790. return -1;
  791. }
  792. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  793. int max_slices;
  794. if (s->mb_height)
  795. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  796. else
  797. max_slices = MAX_THREADS;
  798. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  799. " reducing to %d\n", nb_slices, max_slices);
  800. nb_slices = max_slices;
  801. }
  802. if ((s->width || s->height) &&
  803. av_image_check_size(s->width, s->height, 0, s->avctx))
  804. return -1;
  805. dct_init(s);
  806. /* set chroma shifts */
  807. ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  808. &s->chroma_x_shift,
  809. &s->chroma_y_shift);
  810. if (ret)
  811. return ret;
  812. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  813. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  814. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  815. s->picture[i].f = av_frame_alloc();
  816. if (!s->picture[i].f)
  817. goto fail;
  818. }
  819. s->next_picture.f = av_frame_alloc();
  820. if (!s->next_picture.f)
  821. goto fail;
  822. s->last_picture.f = av_frame_alloc();
  823. if (!s->last_picture.f)
  824. goto fail;
  825. s->current_picture.f = av_frame_alloc();
  826. if (!s->current_picture.f)
  827. goto fail;
  828. s->new_picture.f = av_frame_alloc();
  829. if (!s->new_picture.f)
  830. goto fail;
  831. if (init_context_frame(s))
  832. goto fail;
  833. s->parse_context.state = -1;
  834. s->context_initialized = 1;
  835. memset(s->thread_context, 0, sizeof(s->thread_context));
  836. s->thread_context[0] = s;
  837. // if (s->width && s->height) {
  838. if (nb_slices > 1) {
  839. for (i = 0; i < nb_slices; i++) {
  840. if (i) {
  841. s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
  842. if (!s->thread_context[i])
  843. goto fail;
  844. }
  845. if (init_duplicate_context(s->thread_context[i]) < 0)
  846. goto fail;
  847. s->thread_context[i]->start_mb_y =
  848. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  849. s->thread_context[i]->end_mb_y =
  850. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  851. }
  852. } else {
  853. if (init_duplicate_context(s) < 0)
  854. goto fail;
  855. s->start_mb_y = 0;
  856. s->end_mb_y = s->mb_height;
  857. }
  858. s->slice_context_count = nb_slices;
  859. // }
  860. return 0;
  861. fail:
  862. ff_mpv_common_end(s);
  863. return -1;
  864. }
  865. /**
  866. * Frees and resets MpegEncContext fields depending on the resolution.
  867. * Is used during resolution changes to avoid a full reinitialization of the
  868. * codec.
  869. */
  870. static void free_context_frame(MpegEncContext *s)
  871. {
  872. int i, j, k;
  873. av_freep(&s->mb_type);
  874. av_freep(&s->p_mv_table_base);
  875. av_freep(&s->b_forw_mv_table_base);
  876. av_freep(&s->b_back_mv_table_base);
  877. av_freep(&s->b_bidir_forw_mv_table_base);
  878. av_freep(&s->b_bidir_back_mv_table_base);
  879. av_freep(&s->b_direct_mv_table_base);
  880. s->p_mv_table = NULL;
  881. s->b_forw_mv_table = NULL;
  882. s->b_back_mv_table = NULL;
  883. s->b_bidir_forw_mv_table = NULL;
  884. s->b_bidir_back_mv_table = NULL;
  885. s->b_direct_mv_table = NULL;
  886. for (i = 0; i < 2; i++) {
  887. for (j = 0; j < 2; j++) {
  888. for (k = 0; k < 2; k++) {
  889. av_freep(&s->b_field_mv_table_base[i][j][k]);
  890. s->b_field_mv_table[i][j][k] = NULL;
  891. }
  892. av_freep(&s->b_field_select_table[i][j]);
  893. av_freep(&s->p_field_mv_table_base[i][j]);
  894. s->p_field_mv_table[i][j] = NULL;
  895. }
  896. av_freep(&s->p_field_select_table[i]);
  897. }
  898. av_freep(&s->dc_val_base);
  899. av_freep(&s->coded_block_base);
  900. av_freep(&s->mbintra_table);
  901. av_freep(&s->cbp_table);
  902. av_freep(&s->pred_dir_table);
  903. av_freep(&s->mbskip_table);
  904. av_freep(&s->er.error_status_table);
  905. av_freep(&s->er.er_temp_buffer);
  906. av_freep(&s->mb_index2xy);
  907. av_freep(&s->lambda_table);
  908. av_freep(&s->cplx_tab);
  909. av_freep(&s->bits_tab);
  910. s->linesize = s->uvlinesize = 0;
  911. }
  912. int ff_mpv_common_frame_size_change(MpegEncContext *s)
  913. {
  914. int i, err = 0;
  915. if (!s->context_initialized)
  916. return AVERROR(EINVAL);
  917. if (s->slice_context_count > 1) {
  918. for (i = 0; i < s->slice_context_count; i++) {
  919. free_duplicate_context(s->thread_context[i]);
  920. }
  921. for (i = 1; i < s->slice_context_count; i++) {
  922. av_freep(&s->thread_context[i]);
  923. }
  924. } else
  925. free_duplicate_context(s);
  926. free_context_frame(s);
  927. if (s->picture)
  928. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  929. s->picture[i].needs_realloc = 1;
  930. }
  931. s->last_picture_ptr =
  932. s->next_picture_ptr =
  933. s->current_picture_ptr = NULL;
  934. // init
  935. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  936. s->mb_height = (s->height + 31) / 32 * 2;
  937. else
  938. s->mb_height = (s->height + 15) / 16;
  939. if ((s->width || s->height) &&
  940. (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  941. goto fail;
  942. if ((err = init_context_frame(s)))
  943. goto fail;
  944. memset(s->thread_context, 0, sizeof(s->thread_context));
  945. s->thread_context[0] = s;
  946. if (s->width && s->height) {
  947. int nb_slices = s->slice_context_count;
  948. if (nb_slices > 1) {
  949. for (i = 0; i < nb_slices; i++) {
  950. if (i) {
  951. s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
  952. if (!s->thread_context[i]) {
  953. err = AVERROR(ENOMEM);
  954. goto fail;
  955. }
  956. }
  957. if ((err = init_duplicate_context(s->thread_context[i])) < 0)
  958. goto fail;
  959. s->thread_context[i]->start_mb_y =
  960. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  961. s->thread_context[i]->end_mb_y =
  962. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  963. }
  964. } else {
  965. err = init_duplicate_context(s);
  966. if (err < 0)
  967. goto fail;
  968. s->start_mb_y = 0;
  969. s->end_mb_y = s->mb_height;
  970. }
  971. s->slice_context_count = nb_slices;
  972. }
  973. return 0;
  974. fail:
  975. ff_mpv_common_end(s);
  976. return err;
  977. }
  978. /* init common structure for both encoder and decoder */
  979. void ff_mpv_common_end(MpegEncContext *s)
  980. {
  981. int i;
  982. if (!s)
  983. return ;
  984. if (s->slice_context_count > 1) {
  985. for (i = 0; i < s->slice_context_count; i++) {
  986. free_duplicate_context(s->thread_context[i]);
  987. }
  988. for (i = 1; i < s->slice_context_count; i++) {
  989. av_freep(&s->thread_context[i]);
  990. }
  991. s->slice_context_count = 1;
  992. } else free_duplicate_context(s);
  993. av_freep(&s->parse_context.buffer);
  994. s->parse_context.buffer_size = 0;
  995. av_freep(&s->bitstream_buffer);
  996. s->allocated_bitstream_buffer_size = 0;
  997. if (s->picture) {
  998. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  999. ff_free_picture_tables(&s->picture[i]);
  1000. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1001. av_frame_free(&s->picture[i].f);
  1002. }
  1003. }
  1004. av_freep(&s->picture);
  1005. ff_free_picture_tables(&s->last_picture);
  1006. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1007. av_frame_free(&s->last_picture.f);
  1008. ff_free_picture_tables(&s->current_picture);
  1009. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1010. av_frame_free(&s->current_picture.f);
  1011. ff_free_picture_tables(&s->next_picture);
  1012. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1013. av_frame_free(&s->next_picture.f);
  1014. ff_free_picture_tables(&s->new_picture);
  1015. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  1016. av_frame_free(&s->new_picture.f);
  1017. free_context_frame(s);
  1018. s->context_initialized = 0;
  1019. s->last_picture_ptr =
  1020. s->next_picture_ptr =
  1021. s->current_picture_ptr = NULL;
  1022. s->linesize = s->uvlinesize = 0;
  1023. }
  1024. static void gray_frame(AVFrame *frame)
  1025. {
  1026. int i, h_chroma_shift, v_chroma_shift;
  1027. av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
  1028. for(i=0; i<frame->height; i++)
  1029. memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
  1030. for(i=0; i<AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
  1031. memset(frame->data[1] + frame->linesize[1]*i,
  1032. 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1033. memset(frame->data[2] + frame->linesize[2]*i,
  1034. 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1035. }
  1036. }
  1037. /**
  1038. * generic function called after decoding
  1039. * the header and before a frame is decoded.
  1040. */
  1041. int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1042. {
  1043. int i, ret;
  1044. Picture *pic;
  1045. s->mb_skipped = 0;
  1046. if (!ff_thread_can_start_frame(avctx)) {
  1047. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1048. return -1;
  1049. }
  1050. /* mark & release old frames */
  1051. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1052. s->last_picture_ptr != s->next_picture_ptr &&
  1053. s->last_picture_ptr->f->buf[0]) {
  1054. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1055. }
  1056. /* release forgotten pictures */
  1057. /* if (MPEG-124 / H.263) */
  1058. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1059. if (&s->picture[i] != s->last_picture_ptr &&
  1060. &s->picture[i] != s->next_picture_ptr &&
  1061. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1062. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1063. }
  1064. }
  1065. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1066. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1067. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1068. /* release non reference frames */
  1069. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1070. if (!s->picture[i].reference)
  1071. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1072. }
  1073. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  1074. // we already have an unused image
  1075. // (maybe it was set before reading the header)
  1076. pic = s->current_picture_ptr;
  1077. } else {
  1078. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1079. if (i < 0) {
  1080. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1081. return i;
  1082. }
  1083. pic = &s->picture[i];
  1084. }
  1085. pic->reference = 0;
  1086. if (!s->droppable) {
  1087. if (s->pict_type != AV_PICTURE_TYPE_B)
  1088. pic->reference = 3;
  1089. }
  1090. pic->f->coded_picture_number = s->coded_picture_number++;
  1091. if (alloc_picture(s, pic, 0) < 0)
  1092. return -1;
  1093. s->current_picture_ptr = pic;
  1094. // FIXME use only the vars from current_pic
  1095. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1096. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1097. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1098. if (s->picture_structure != PICT_FRAME)
  1099. s->current_picture_ptr->f->top_field_first =
  1100. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1101. }
  1102. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1103. !s->progressive_sequence;
  1104. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1105. s->current_picture_ptr->f->pict_type = s->pict_type;
  1106. // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
  1107. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1108. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1109. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1110. s->current_picture_ptr)) < 0)
  1111. return ret;
  1112. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1113. s->last_picture_ptr = s->next_picture_ptr;
  1114. if (!s->droppable)
  1115. s->next_picture_ptr = s->current_picture_ptr;
  1116. }
  1117. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1118. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1119. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1120. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1121. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1122. s->pict_type, s->droppable);
  1123. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  1124. (s->pict_type != AV_PICTURE_TYPE_I)) {
  1125. int h_chroma_shift, v_chroma_shift;
  1126. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1127. &h_chroma_shift, &v_chroma_shift);
  1128. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
  1129. av_log(avctx, AV_LOG_DEBUG,
  1130. "allocating dummy last picture for B frame\n");
  1131. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1132. av_log(avctx, AV_LOG_ERROR,
  1133. "warning: first frame is no keyframe\n");
  1134. /* Allocate a dummy frame */
  1135. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1136. if (i < 0) {
  1137. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1138. return i;
  1139. }
  1140. s->last_picture_ptr = &s->picture[i];
  1141. s->last_picture_ptr->reference = 3;
  1142. s->last_picture_ptr->f->key_frame = 0;
  1143. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1144. if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1145. s->last_picture_ptr = NULL;
  1146. return -1;
  1147. }
  1148. if (!avctx->hwaccel) {
  1149. for(i=0; i<avctx->height; i++)
  1150. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
  1151. 0x80, avctx->width);
  1152. if (s->last_picture_ptr->f->data[2]) {
  1153. for(i=0; i<AV_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1154. memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
  1155. 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1156. memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
  1157. 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1158. }
  1159. }
  1160. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1161. for(i=0; i<avctx->height; i++)
  1162. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
  1163. }
  1164. }
  1165. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1166. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1167. }
  1168. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1169. s->pict_type == AV_PICTURE_TYPE_B) {
  1170. /* Allocate a dummy frame */
  1171. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1172. if (i < 0) {
  1173. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1174. return i;
  1175. }
  1176. s->next_picture_ptr = &s->picture[i];
  1177. s->next_picture_ptr->reference = 3;
  1178. s->next_picture_ptr->f->key_frame = 0;
  1179. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1180. if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1181. s->next_picture_ptr = NULL;
  1182. return -1;
  1183. }
  1184. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1185. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1186. }
  1187. #if 0 // BUFREF-FIXME
  1188. memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
  1189. memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
  1190. #endif
  1191. if (s->last_picture_ptr) {
  1192. if (s->last_picture_ptr->f->buf[0] &&
  1193. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1194. s->last_picture_ptr)) < 0)
  1195. return ret;
  1196. }
  1197. if (s->next_picture_ptr) {
  1198. if (s->next_picture_ptr->f->buf[0] &&
  1199. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1200. s->next_picture_ptr)) < 0)
  1201. return ret;
  1202. }
  1203. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1204. s->last_picture_ptr->f->buf[0]));
  1205. if (s->picture_structure!= PICT_FRAME) {
  1206. int i;
  1207. for (i = 0; i < 4; i++) {
  1208. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1209. s->current_picture.f->data[i] +=
  1210. s->current_picture.f->linesize[i];
  1211. }
  1212. s->current_picture.f->linesize[i] *= 2;
  1213. s->last_picture.f->linesize[i] *= 2;
  1214. s->next_picture.f->linesize[i] *= 2;
  1215. }
  1216. }
  1217. /* set dequantizer, we can't do it during init as
  1218. * it might change for MPEG-4 and we can't do it in the header
  1219. * decode as init is not called for MPEG-4 there yet */
  1220. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1221. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1222. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1223. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1224. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1225. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1226. } else {
  1227. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1228. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1229. }
  1230. if (s->avctx->debug & FF_DEBUG_NOMC) {
  1231. gray_frame(s->current_picture_ptr->f);
  1232. }
  1233. return 0;
  1234. }
  1235. /* called after a frame has been decoded. */
  1236. void ff_mpv_frame_end(MpegEncContext *s)
  1237. {
  1238. emms_c();
  1239. if (s->current_picture.reference)
  1240. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1241. }
  1242. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1243. {
  1244. ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
  1245. p->qscale_table, p->motion_val, &s->low_delay,
  1246. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1247. }
  1248. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1249. {
  1250. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1251. int offset = 2*s->mb_stride + 1;
  1252. if(!ref)
  1253. return AVERROR(ENOMEM);
  1254. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1255. ref->size -= offset;
  1256. ref->data += offset;
  1257. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1258. }
  1259. static inline int hpel_motion_lowres(MpegEncContext *s,
  1260. uint8_t *dest, uint8_t *src,
  1261. int field_based, int field_select,
  1262. int src_x, int src_y,
  1263. int width, int height, ptrdiff_t stride,
  1264. int h_edge_pos, int v_edge_pos,
  1265. int w, int h, h264_chroma_mc_func *pix_op,
  1266. int motion_x, int motion_y)
  1267. {
  1268. const int lowres = s->avctx->lowres;
  1269. const int op_index = FFMIN(lowres, 3);
  1270. const int s_mask = (2 << lowres) - 1;
  1271. int emu = 0;
  1272. int sx, sy;
  1273. if (s->quarter_sample) {
  1274. motion_x /= 2;
  1275. motion_y /= 2;
  1276. }
  1277. sx = motion_x & s_mask;
  1278. sy = motion_y & s_mask;
  1279. src_x += motion_x >> lowres + 1;
  1280. src_y += motion_y >> lowres + 1;
  1281. src += src_y * stride + src_x;
  1282. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1283. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1284. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
  1285. s->linesize, s->linesize,
  1286. w + 1, (h + 1) << field_based,
  1287. src_x, src_y << field_based,
  1288. h_edge_pos, v_edge_pos);
  1289. src = s->sc.edge_emu_buffer;
  1290. emu = 1;
  1291. }
  1292. sx = (sx << 2) >> lowres;
  1293. sy = (sy << 2) >> lowres;
  1294. if (field_select)
  1295. src += s->linesize;
  1296. pix_op[op_index](dest, src, stride, h, sx, sy);
  1297. return emu;
  1298. }
  1299. /* apply one mpeg motion vector to the three components */
  1300. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1301. uint8_t *dest_y,
  1302. uint8_t *dest_cb,
  1303. uint8_t *dest_cr,
  1304. int field_based,
  1305. int bottom_field,
  1306. int field_select,
  1307. uint8_t **ref_picture,
  1308. h264_chroma_mc_func *pix_op,
  1309. int motion_x, int motion_y,
  1310. int h, int mb_y)
  1311. {
  1312. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1313. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  1314. ptrdiff_t uvlinesize, linesize;
  1315. const int lowres = s->avctx->lowres;
  1316. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  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;
  1320. const int v_edge_pos = s->v_edge_pos >> lowres;
  1321. linesize = s->current_picture.f->linesize[0] << field_based;
  1322. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  1323. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1324. if (s->quarter_sample) {
  1325. motion_x /= 2;
  1326. motion_y /= 2;
  1327. }
  1328. if(field_based){
  1329. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1330. }
  1331. sx = motion_x & s_mask;
  1332. sy = motion_y & s_mask;
  1333. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1334. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1335. if (s->out_format == FMT_H263) {
  1336. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1337. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1338. uvsrc_x = src_x >> 1;
  1339. uvsrc_y = src_y >> 1;
  1340. } else if (s->out_format == FMT_H261) {
  1341. // even chroma mv's are full pel in H261
  1342. mx = motion_x / 4;
  1343. my = motion_y / 4;
  1344. uvsx = (2 * mx) & s_mask;
  1345. uvsy = (2 * my) & s_mask;
  1346. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1347. uvsrc_y = mb_y * block_s + (my >> lowres);
  1348. } else {
  1349. if(s->chroma_y_shift){
  1350. mx = motion_x / 2;
  1351. my = motion_y / 2;
  1352. uvsx = mx & s_mask;
  1353. uvsy = my & s_mask;
  1354. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1355. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1356. } else {
  1357. if(s->chroma_x_shift){
  1358. //Chroma422
  1359. mx = motion_x / 2;
  1360. uvsx = mx & s_mask;
  1361. uvsy = motion_y & s_mask;
  1362. uvsrc_y = src_y;
  1363. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1364. } else {
  1365. //Chroma444
  1366. uvsx = motion_x & s_mask;
  1367. uvsy = motion_y & s_mask;
  1368. uvsrc_x = src_x;
  1369. uvsrc_y = src_y;
  1370. }
  1371. }
  1372. }
  1373. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1374. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1375. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1376. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  1377. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1378. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
  1379. linesize >> field_based, linesize >> field_based,
  1380. 17, 17 + field_based,
  1381. src_x, src_y << field_based, h_edge_pos,
  1382. v_edge_pos);
  1383. ptr_y = s->sc.edge_emu_buffer;
  1384. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1385. uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
  1386. uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
  1387. if (s->workaround_bugs & FF_BUG_IEDGE)
  1388. vbuf -= s->uvlinesize;
  1389. s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
  1390. uvlinesize >> field_based, uvlinesize >> field_based,
  1391. 9, 9 + field_based,
  1392. uvsrc_x, uvsrc_y << field_based,
  1393. h_edge_pos >> 1, v_edge_pos >> 1);
  1394. s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
  1395. uvlinesize >> field_based,uvlinesize >> field_based,
  1396. 9, 9 + field_based,
  1397. uvsrc_x, uvsrc_y << field_based,
  1398. h_edge_pos >> 1, v_edge_pos >> 1);
  1399. ptr_cb = ubuf;
  1400. ptr_cr = vbuf;
  1401. }
  1402. }
  1403. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
  1404. if (bottom_field) {
  1405. dest_y += s->linesize;
  1406. dest_cb += s->uvlinesize;
  1407. dest_cr += s->uvlinesize;
  1408. }
  1409. if (field_select) {
  1410. ptr_y += s->linesize;
  1411. ptr_cb += s->uvlinesize;
  1412. ptr_cr += s->uvlinesize;
  1413. }
  1414. sx = (sx << 2) >> lowres;
  1415. sy = (sy << 2) >> lowres;
  1416. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1417. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1418. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  1419. uvsx = (uvsx << 2) >> lowres;
  1420. uvsy = (uvsy << 2) >> lowres;
  1421. if (hc) {
  1422. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  1423. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  1424. }
  1425. }
  1426. // FIXME h261 lowres loop filter
  1427. }
  1428. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1429. uint8_t *dest_cb, uint8_t *dest_cr,
  1430. uint8_t **ref_picture,
  1431. h264_chroma_mc_func * pix_op,
  1432. int mx, int my)
  1433. {
  1434. const int lowres = s->avctx->lowres;
  1435. const int op_index = FFMIN(lowres, 3);
  1436. const int block_s = 8 >> lowres;
  1437. const int s_mask = (2 << lowres) - 1;
  1438. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1439. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1440. int emu = 0, src_x, src_y, sx, sy;
  1441. ptrdiff_t offset;
  1442. uint8_t *ptr;
  1443. if (s->quarter_sample) {
  1444. mx /= 2;
  1445. my /= 2;
  1446. }
  1447. /* In case of 8X8, we construct a single chroma motion vector
  1448. with a special rounding */
  1449. mx = ff_h263_round_chroma(mx);
  1450. my = ff_h263_round_chroma(my);
  1451. sx = mx & s_mask;
  1452. sy = my & s_mask;
  1453. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  1454. src_y = s->mb_y * block_s + (my >> lowres + 1);
  1455. offset = src_y * s->uvlinesize + src_x;
  1456. ptr = ref_picture[1] + offset;
  1457. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  1458. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  1459. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1460. s->uvlinesize, s->uvlinesize,
  1461. 9, 9,
  1462. src_x, src_y, h_edge_pos, v_edge_pos);
  1463. ptr = s->sc.edge_emu_buffer;
  1464. emu = 1;
  1465. }
  1466. sx = (sx << 2) >> lowres;
  1467. sy = (sy << 2) >> lowres;
  1468. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1469. ptr = ref_picture[2] + offset;
  1470. if (emu) {
  1471. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1472. s->uvlinesize, s->uvlinesize,
  1473. 9, 9,
  1474. src_x, src_y, h_edge_pos, v_edge_pos);
  1475. ptr = s->sc.edge_emu_buffer;
  1476. }
  1477. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1478. }
  1479. /**
  1480. * motion compensation of a single macroblock
  1481. * @param s context
  1482. * @param dest_y luma destination pointer
  1483. * @param dest_cb chroma cb/u destination pointer
  1484. * @param dest_cr chroma cr/v destination pointer
  1485. * @param dir direction (0->forward, 1->backward)
  1486. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1487. * @param pix_op halfpel motion compensation function (average or put normally)
  1488. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1489. */
  1490. static inline void MPV_motion_lowres(MpegEncContext *s,
  1491. uint8_t *dest_y, uint8_t *dest_cb,
  1492. uint8_t *dest_cr,
  1493. int dir, uint8_t **ref_picture,
  1494. h264_chroma_mc_func *pix_op)
  1495. {
  1496. int mx, my;
  1497. int mb_x, mb_y, i;
  1498. const int lowres = s->avctx->lowres;
  1499. const int block_s = 8 >>lowres;
  1500. mb_x = s->mb_x;
  1501. mb_y = s->mb_y;
  1502. switch (s->mv_type) {
  1503. case MV_TYPE_16X16:
  1504. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1505. 0, 0, 0,
  1506. ref_picture, pix_op,
  1507. s->mv[dir][0][0], s->mv[dir][0][1],
  1508. 2 * block_s, mb_y);
  1509. break;
  1510. case MV_TYPE_8X8:
  1511. mx = 0;
  1512. my = 0;
  1513. for (i = 0; i < 4; i++) {
  1514. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  1515. s->linesize) * block_s,
  1516. ref_picture[0], 0, 0,
  1517. (2 * mb_x + (i & 1)) * block_s,
  1518. (2 * mb_y + (i >> 1)) * block_s,
  1519. s->width, s->height, s->linesize,
  1520. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1521. block_s, block_s, pix_op,
  1522. s->mv[dir][i][0], s->mv[dir][i][1]);
  1523. mx += s->mv[dir][i][0];
  1524. my += s->mv[dir][i][1];
  1525. }
  1526. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
  1527. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  1528. pix_op, mx, my);
  1529. break;
  1530. case MV_TYPE_FIELD:
  1531. if (s->picture_structure == PICT_FRAME) {
  1532. /* top field */
  1533. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1534. 1, 0, s->field_select[dir][0],
  1535. ref_picture, pix_op,
  1536. s->mv[dir][0][0], s->mv[dir][0][1],
  1537. block_s, mb_y);
  1538. /* bottom field */
  1539. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1540. 1, 1, s->field_select[dir][1],
  1541. ref_picture, pix_op,
  1542. s->mv[dir][1][0], s->mv[dir][1][1],
  1543. block_s, mb_y);
  1544. } else {
  1545. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  1546. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  1547. ref_picture = s->current_picture_ptr->f->data;
  1548. }
  1549. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1550. 0, 0, s->field_select[dir][0],
  1551. ref_picture, pix_op,
  1552. s->mv[dir][0][0],
  1553. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  1554. }
  1555. break;
  1556. case MV_TYPE_16X8:
  1557. for (i = 0; i < 2; i++) {
  1558. uint8_t **ref2picture;
  1559. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  1560. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  1561. ref2picture = ref_picture;
  1562. } else {
  1563. ref2picture = s->current_picture_ptr->f->data;
  1564. }
  1565. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1566. 0, 0, s->field_select[dir][i],
  1567. ref2picture, pix_op,
  1568. s->mv[dir][i][0], s->mv[dir][i][1] +
  1569. 2 * block_s * i, block_s, mb_y >> 1);
  1570. dest_y += 2 * block_s * s->linesize;
  1571. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  1572. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  1573. }
  1574. break;
  1575. case MV_TYPE_DMV:
  1576. if (s->picture_structure == PICT_FRAME) {
  1577. for (i = 0; i < 2; i++) {
  1578. int j;
  1579. for (j = 0; j < 2; j++) {
  1580. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1581. 1, j, j ^ i,
  1582. ref_picture, pix_op,
  1583. s->mv[dir][2 * i + j][0],
  1584. s->mv[dir][2 * i + j][1],
  1585. block_s, mb_y);
  1586. }
  1587. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  1588. }
  1589. } else {
  1590. for (i = 0; i < 2; i++) {
  1591. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1592. 0, 0, s->picture_structure != i + 1,
  1593. ref_picture, pix_op,
  1594. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  1595. 2 * block_s, mb_y >> 1);
  1596. // after put we make avg of the same block
  1597. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  1598. // opposite parity is always in the same
  1599. // frame if this is second field
  1600. if (!s->first_field) {
  1601. ref_picture = s->current_picture_ptr->f->data;
  1602. }
  1603. }
  1604. }
  1605. break;
  1606. default:
  1607. av_assert2(0);
  1608. }
  1609. }
  1610. /**
  1611. * find the lowest MB row referenced in the MVs
  1612. */
  1613. static int lowest_referenced_row(MpegEncContext *s, int dir)
  1614. {
  1615. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1616. int my, off, i, mvs;
  1617. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1618. goto unhandled;
  1619. switch (s->mv_type) {
  1620. case MV_TYPE_16X16:
  1621. mvs = 1;
  1622. break;
  1623. case MV_TYPE_16X8:
  1624. mvs = 2;
  1625. break;
  1626. case MV_TYPE_8X8:
  1627. mvs = 4;
  1628. break;
  1629. default:
  1630. goto unhandled;
  1631. }
  1632. for (i = 0; i < mvs; i++) {
  1633. my = s->mv[dir][i][1];
  1634. my_max = FFMAX(my_max, my);
  1635. my_min = FFMIN(my_min, my);
  1636. }
  1637. off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
  1638. return av_clip(s->mb_y + off, 0, s->mb_height - 1);
  1639. unhandled:
  1640. return s->mb_height-1;
  1641. }
  1642. /* put block[] to dest[] */
  1643. static inline void put_dct(MpegEncContext *s,
  1644. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1645. {
  1646. s->dct_unquantize_intra(s, block, i, qscale);
  1647. s->idsp.idct_put(dest, line_size, block);
  1648. }
  1649. /* add block[] to dest[] */
  1650. static inline void add_dct(MpegEncContext *s,
  1651. int16_t *block, int i, uint8_t *dest, int line_size)
  1652. {
  1653. if (s->block_last_index[i] >= 0) {
  1654. s->idsp.idct_add(dest, line_size, block);
  1655. }
  1656. }
  1657. static inline void add_dequant_dct(MpegEncContext *s,
  1658. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1659. {
  1660. if (s->block_last_index[i] >= 0) {
  1661. s->dct_unquantize_inter(s, block, i, qscale);
  1662. s->idsp.idct_add(dest, line_size, block);
  1663. }
  1664. }
  1665. /**
  1666. * Clean dc, ac, coded_block for the current non-intra MB.
  1667. */
  1668. void ff_clean_intra_table_entries(MpegEncContext *s)
  1669. {
  1670. int wrap = s->b8_stride;
  1671. int xy = s->block_index[0];
  1672. s->dc_val[0][xy ] =
  1673. s->dc_val[0][xy + 1 ] =
  1674. s->dc_val[0][xy + wrap] =
  1675. s->dc_val[0][xy + 1 + wrap] = 1024;
  1676. /* ac pred */
  1677. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1678. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1679. if (s->msmpeg4_version>=3) {
  1680. s->coded_block[xy ] =
  1681. s->coded_block[xy + 1 ] =
  1682. s->coded_block[xy + wrap] =
  1683. s->coded_block[xy + 1 + wrap] = 0;
  1684. }
  1685. /* chroma */
  1686. wrap = s->mb_stride;
  1687. xy = s->mb_x + s->mb_y * wrap;
  1688. s->dc_val[1][xy] =
  1689. s->dc_val[2][xy] = 1024;
  1690. /* ac pred */
  1691. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1692. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1693. s->mbintra_table[xy]= 0;
  1694. }
  1695. /* generic function called after a macroblock has been parsed by the
  1696. decoder or after it has been encoded by the encoder.
  1697. Important variables used:
  1698. s->mb_intra : true if intra macroblock
  1699. s->mv_dir : motion vector direction
  1700. s->mv_type : motion vector type
  1701. s->mv : motion vector
  1702. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1703. */
  1704. static av_always_inline
  1705. void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1706. int lowres_flag, int is_mpeg12)
  1707. {
  1708. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1709. if (CONFIG_XVMC &&
  1710. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  1711. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  1712. return;
  1713. }
  1714. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1715. /* print DCT coefficients */
  1716. int i,j;
  1717. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1718. for(i=0; i<6; i++){
  1719. for(j=0; j<64; j++){
  1720. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  1721. block[i][s->idsp.idct_permutation[j]]);
  1722. }
  1723. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1724. }
  1725. }
  1726. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1727. /* update DC predictors for P macroblocks */
  1728. if (!s->mb_intra) {
  1729. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1730. if(s->mbintra_table[mb_xy])
  1731. ff_clean_intra_table_entries(s);
  1732. } else {
  1733. s->last_dc[0] =
  1734. s->last_dc[1] =
  1735. s->last_dc[2] = 128 << s->intra_dc_precision;
  1736. }
  1737. }
  1738. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1739. s->mbintra_table[mb_xy]=1;
  1740. if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->frame_skip_threshold || s->frame_skip_factor ||
  1741. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  1742. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  1743. uint8_t *dest_y, *dest_cb, *dest_cr;
  1744. int dct_linesize, dct_offset;
  1745. op_pixels_func (*op_pix)[4];
  1746. qpel_mc_func (*op_qpix)[16];
  1747. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1748. const int uvlinesize = s->current_picture.f->linesize[1];
  1749. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1750. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1751. /* avoid copy if macroblock skipped in last frame too */
  1752. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1753. if(!s->encoding){
  1754. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1755. if (s->mb_skipped) {
  1756. s->mb_skipped= 0;
  1757. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  1758. *mbskip_ptr = 1;
  1759. } else if(!s->current_picture.reference) {
  1760. *mbskip_ptr = 1;
  1761. } else{
  1762. *mbskip_ptr = 0; /* not skipped */
  1763. }
  1764. }
  1765. dct_linesize = linesize << s->interlaced_dct;
  1766. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1767. if(readable){
  1768. dest_y= s->dest[0];
  1769. dest_cb= s->dest[1];
  1770. dest_cr= s->dest[2];
  1771. }else{
  1772. dest_y = s->sc.b_scratchpad;
  1773. dest_cb= s->sc.b_scratchpad+16*linesize;
  1774. dest_cr= s->sc.b_scratchpad+32*linesize;
  1775. }
  1776. if (!s->mb_intra) {
  1777. /* motion handling */
  1778. /* decoding or more than one mb_type (MC was already done otherwise) */
  1779. if(!s->encoding){
  1780. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1781. if (s->mv_dir & MV_DIR_FORWARD) {
  1782. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1783. lowest_referenced_row(s, 0),
  1784. 0);
  1785. }
  1786. if (s->mv_dir & MV_DIR_BACKWARD) {
  1787. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1788. lowest_referenced_row(s, 1),
  1789. 0);
  1790. }
  1791. }
  1792. if(lowres_flag){
  1793. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  1794. if (s->mv_dir & MV_DIR_FORWARD) {
  1795. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
  1796. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  1797. }
  1798. if (s->mv_dir & MV_DIR_BACKWARD) {
  1799. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
  1800. }
  1801. }else{
  1802. op_qpix = s->me.qpel_put;
  1803. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1804. op_pix = s->hdsp.put_pixels_tab;
  1805. }else{
  1806. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1807. }
  1808. if (s->mv_dir & MV_DIR_FORWARD) {
  1809. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  1810. op_pix = s->hdsp.avg_pixels_tab;
  1811. op_qpix= s->me.qpel_avg;
  1812. }
  1813. if (s->mv_dir & MV_DIR_BACKWARD) {
  1814. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  1815. }
  1816. }
  1817. }
  1818. /* skip dequant / idct if we are really late ;) */
  1819. if(s->avctx->skip_idct){
  1820. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1821. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1822. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1823. goto skip_idct;
  1824. }
  1825. /* add dct residue */
  1826. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1827. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1828. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1829. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1830. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1831. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1832. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1833. if (s->chroma_y_shift){
  1834. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1835. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1836. }else{
  1837. dct_linesize >>= 1;
  1838. dct_offset >>=1;
  1839. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1840. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1841. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1842. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1843. }
  1844. }
  1845. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1846. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1847. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1848. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1849. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1850. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1851. if(s->chroma_y_shift){//Chroma420
  1852. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1853. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1854. }else{
  1855. //chroma422
  1856. dct_linesize = uvlinesize << s->interlaced_dct;
  1857. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1858. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1859. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1860. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1861. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1862. if(!s->chroma_x_shift){//Chroma444
  1863. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  1864. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  1865. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  1866. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  1867. }
  1868. }
  1869. }//fi gray
  1870. }
  1871. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1872. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1873. }
  1874. } else {
  1875. /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
  1876. TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
  1877. if (s->avctx->bits_per_raw_sample > 8){
  1878. const int act_block_size = block_size * 2;
  1879. if(s->dpcm_direction == 0) {
  1880. s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)(*s->block32)[0]);
  1881. s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)(*s->block32)[1]);
  1882. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)(*s->block32)[2]);
  1883. s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)(*s->block32)[3]);
  1884. dct_linesize = uvlinesize << s->interlaced_dct;
  1885. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1886. s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)(*s->block32)[4]);
  1887. s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)(*s->block32)[5]);
  1888. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)(*s->block32)[6]);
  1889. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)(*s->block32)[7]);
  1890. if(!s->chroma_x_shift){//Chroma444
  1891. s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)(*s->block32)[8]);
  1892. s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)(*s->block32)[9]);
  1893. s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[10]);
  1894. s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[11]);
  1895. }
  1896. } else if(s->dpcm_direction == 1) {
  1897. int i, w, h;
  1898. uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
  1899. int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
  1900. for(i = 0; i < 3; i++) {
  1901. int idx = 0;
  1902. int vsub = i ? s->chroma_y_shift : 0;
  1903. int hsub = i ? s->chroma_x_shift : 0;
  1904. for(h = 0; h < (16 >> vsub); h++){
  1905. for(w = 0; w < (16 >> hsub); w++)
  1906. dest_pcm[i][w] = (*s->dpcm_macroblock)[i][idx++];
  1907. dest_pcm[i] += linesize[i] / 2;
  1908. }
  1909. }
  1910. } else if(s->dpcm_direction == -1) {
  1911. int i, w, h;
  1912. uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
  1913. int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
  1914. for(i = 0; i < 3; i++) {
  1915. int idx = 0;
  1916. int vsub = i ? s->chroma_y_shift : 0;
  1917. int hsub = i ? s->chroma_x_shift : 0;
  1918. dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub) - 1);
  1919. for(h = (16 >> vsub)-1; h >= 1; h--){
  1920. for(w = (16 >> hsub)-1; w >= 1; w--)
  1921. dest_pcm[i][w] = (*s->dpcm_macroblock)[i][idx++];
  1922. dest_pcm[i] -= linesize[i] / 2;
  1923. }
  1924. }
  1925. }
  1926. }
  1927. /* dct only in intra block */
  1928. else if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1929. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1930. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1931. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1932. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1933. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1934. if(s->chroma_y_shift){
  1935. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1936. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1937. }else{
  1938. dct_offset >>=1;
  1939. dct_linesize >>=1;
  1940. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1941. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1942. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1943. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1944. }
  1945. }
  1946. }else{
  1947. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  1948. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1949. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  1950. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1951. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1952. if(s->chroma_y_shift){
  1953. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  1954. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  1955. }else{
  1956. dct_linesize = uvlinesize << s->interlaced_dct;
  1957. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1958. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  1959. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  1960. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1961. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1962. if(!s->chroma_x_shift){//Chroma444
  1963. s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  1964. s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  1965. s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  1966. s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  1967. }
  1968. }
  1969. }//gray
  1970. }
  1971. }
  1972. skip_idct:
  1973. if(!readable){
  1974. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1975. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1976. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1977. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1978. }
  1979. }
  1980. }
  1981. }
  1982. void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
  1983. {
  1984. #if !CONFIG_SMALL
  1985. if(s->out_format == FMT_MPEG1) {
  1986. if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 1);
  1987. else mpv_reconstruct_mb_internal(s, block, 0, 1);
  1988. } else
  1989. #endif
  1990. if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 0);
  1991. else mpv_reconstruct_mb_internal(s, block, 0, 0);
  1992. }
  1993. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1994. {
  1995. ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
  1996. s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
  1997. s->first_field, s->low_delay);
  1998. }
  1999. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2000. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2001. const int uvlinesize = s->current_picture.f->linesize[1];
  2002. const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
  2003. const int height_of_mb = 4 - s->avctx->lowres;
  2004. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2005. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2006. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2007. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2008. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2009. 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;
  2010. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2011. s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << width_of_mb);
  2012. s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
  2013. s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
  2014. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2015. {
  2016. if(s->picture_structure==PICT_FRAME){
  2017. s->dest[0] += s->mb_y * linesize << height_of_mb;
  2018. s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2019. s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2020. }else{
  2021. s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
  2022. s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2023. s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2024. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2025. }
  2026. }
  2027. }
  2028. void ff_mpeg_flush(AVCodecContext *avctx){
  2029. int i;
  2030. MpegEncContext *s = avctx->priv_data;
  2031. if (!s || !s->picture)
  2032. return;
  2033. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2034. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  2035. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2036. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  2037. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  2038. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  2039. s->mb_x= s->mb_y= 0;
  2040. s->closed_gop= 0;
  2041. s->parse_context.state= -1;
  2042. s->parse_context.frame_start_found= 0;
  2043. s->parse_context.overread= 0;
  2044. s->parse_context.overread_index= 0;
  2045. s->parse_context.index= 0;
  2046. s->parse_context.last_index= 0;
  2047. s->bitstream_buffer_size=0;
  2048. s->pp_time=0;
  2049. }
  2050. /**
  2051. * set qscale and update qscale dependent variables.
  2052. */
  2053. void ff_set_qscale(MpegEncContext * s, int qscale)
  2054. {
  2055. if (qscale < 1)
  2056. qscale = 1;
  2057. else if (qscale > 31)
  2058. qscale = 31;
  2059. s->qscale = qscale;
  2060. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2061. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2062. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2063. }
  2064. void ff_mpv_report_decode_progress(MpegEncContext *s)
  2065. {
  2066. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2067. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2068. }