mpegvideo_enc.c 178 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750
  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. * non linear quantizers with large QPs and VBV with restrictive qmin fixes sponsored by NOA GmbH
  26. */
  27. /**
  28. * @file
  29. * The simplest mpeg encoder (well, it was the simplest!).
  30. */
  31. #include "config_components.h"
  32. #include <assert.h>
  33. #include <stdint.h>
  34. #include "libavutil/emms.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/intmath.h"
  37. #include "libavutil/mathematics.h"
  38. #include "libavutil/mem.h"
  39. #include "libavutil/mem_internal.h"
  40. #include "libavutil/opt.h"
  41. #include "libavutil/thread.h"
  42. #include "avcodec.h"
  43. #include "encode.h"
  44. #include "idctdsp.h"
  45. #include "mpeg12codecs.h"
  46. #include "mpeg12data.h"
  47. #include "mpeg12enc.h"
  48. #include "mpegvideo.h"
  49. #include "mpegvideodata.h"
  50. #include "mpegvideoenc.h"
  51. #include "h261enc.h"
  52. #include "h263.h"
  53. #include "h263data.h"
  54. #include "h263enc.h"
  55. #include "mjpegenc_common.h"
  56. #include "mathops.h"
  57. #include "mpegutils.h"
  58. #include "mpegvideo_unquantize.h"
  59. #include "mjpegenc.h"
  60. #include "speedhqenc.h"
  61. #include "msmpeg4enc.h"
  62. #include "pixblockdsp.h"
  63. #include "qpeldsp.h"
  64. #include "faandct.h"
  65. #include "aandcttab.h"
  66. #include "mpeg4video.h"
  67. #include "mpeg4videodata.h"
  68. #include "mpeg4videoenc.h"
  69. #include "internal.h"
  70. #include "bytestream.h"
  71. #include "rv10enc.h"
  72. #include "packet_internal.h"
  73. #include "libavutil/refstruct.h"
  74. #include <limits.h>
  75. #include "sp5x.h"
  76. #define QUANT_BIAS_SHIFT 8
  77. #define QMAT_SHIFT_MMX 16
  78. #define QMAT_SHIFT 21
  79. static int encode_picture(MPVMainEncContext *const s, const AVPacket *pkt);
  80. static int dct_quantize_refine(MPVEncContext *const s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
  81. static int sse_mb(MPVEncContext *const s);
  82. static void denoise_dct_c(MPVEncContext *const s, int16_t *block);
  83. static int dct_quantize_c(MPVEncContext *const s,
  84. int16_t *block, int n,
  85. int qscale, int *overflow);
  86. static int dct_quantize_trellis_c(MPVEncContext *const s, int16_t *block, int n, int qscale, int *overflow);
  87. static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
  88. static const AVOption mpv_generic_options[] = {
  89. FF_MPV_COMMON_OPTS
  90. FF_MPV_COMMON_MOTION_EST_OPTS
  91. { NULL },
  92. };
  93. const AVClass ff_mpv_enc_class = {
  94. .class_name = "generic mpegvideo encoder",
  95. .item_name = av_default_item_name,
  96. .option = mpv_generic_options,
  97. .version = LIBAVUTIL_VERSION_INT,
  98. };
  99. void ff_convert_matrix(MPVEncContext *const s, int (*qmat)[64],
  100. uint16_t (*qmat16)[2][64],
  101. const uint16_t *quant_matrix,
  102. int bias, int qmin, int qmax, int intra)
  103. {
  104. FDCTDSPContext *fdsp = &s->fdsp;
  105. int qscale;
  106. int shift = 0;
  107. for (qscale = qmin; qscale <= qmax; qscale++) {
  108. int i;
  109. int qscale2;
  110. if (s->c.q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
  111. else qscale2 = qscale << 1;
  112. if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
  113. #if CONFIG_FAANDCT
  114. fdsp->fdct == ff_faandct ||
  115. #endif /* CONFIG_FAANDCT */
  116. fdsp->fdct == ff_jpeg_fdct_islow_10) {
  117. for (i = 0; i < 64; i++) {
  118. const int j = s->c.idsp.idct_permutation[i];
  119. int64_t den = (int64_t) qscale2 * quant_matrix[j];
  120. /* 1 * 1 <= qscale2 * quant_matrix[j] <= 112 * 255
  121. * Assume x = qscale2 * quant_matrix[j]
  122. * 1 <= x <= 28560
  123. * (1 << 22) / 1 >= (1 << 22) / (x) >= (1 << 22) / 28560
  124. * 4194304 >= (1 << 22) / (x) >= 146 */
  125. qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
  126. }
  127. } else if (fdsp->fdct == ff_fdct_ifast) {
  128. for (i = 0; i < 64; i++) {
  129. const int j = s->c.idsp.idct_permutation[i];
  130. int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
  131. /* 1247 * 1 * 1 <= ff_aanscales[i] * qscale2 * quant_matrix[j] <= 31521 * 112 * 255
  132. * Assume x = ff_aanscales[i] * qscale2 * quant_matrix[j]
  133. * 1247 <= x <= 900239760
  134. * (1 << 36) / 1247 >= (1 << 36) / (x) >= (1 << 36) / 900239760
  135. * 55107840 >= (1 << 36) / (x) >= 76 */
  136. qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
  137. }
  138. } else {
  139. for (i = 0; i < 64; i++) {
  140. const int j = s->c.idsp.idct_permutation[i];
  141. int64_t den = (int64_t) qscale2 * quant_matrix[j];
  142. /* 1 * 1 <= qscale2 * quant_matrix[j] <= 112 * 255
  143. * Assume x = qscale2 * quant_matrix[j]
  144. * 1 <= x <= 28560
  145. * (1 << 22) / 1 >= (1 << 22) / (x) >= (1 << 22) / 28560
  146. * 4194304 >= (1 << 22) / (x) >= 146
  147. *
  148. * 1 <= x <= 28560
  149. * (1 << 17) / 1 >= (1 << 17) / (x) >= (1 << 17) / 28560
  150. * 131072 >= (1 << 17) / (x) >= 4 */
  151. qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
  152. qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
  153. if (qmat16[qscale][0][i] == 0 ||
  154. qmat16[qscale][0][i] == 128 * 256)
  155. qmat16[qscale][0][i] = 128 * 256 - 1;
  156. qmat16[qscale][1][i] =
  157. ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
  158. qmat16[qscale][0][i]);
  159. }
  160. }
  161. for (i = intra; i < 64; i++) {
  162. int64_t max = 8191;
  163. if (fdsp->fdct == ff_fdct_ifast) {
  164. max = (8191LL * ff_aanscales[i]) >> 14;
  165. }
  166. while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
  167. shift++;
  168. }
  169. }
  170. }
  171. if (shift) {
  172. av_log(s->c.avctx, AV_LOG_INFO,
  173. "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
  174. QMAT_SHIFT - shift);
  175. }
  176. }
  177. static inline void update_qscale(MPVMainEncContext *const m)
  178. {
  179. MPVEncContext *const s = &m->s;
  180. if (s->c.q_scale_type == 1 && 0) {
  181. int i;
  182. int bestdiff=INT_MAX;
  183. int best = 1;
  184. for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
  185. int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
  186. if (ff_mpeg2_non_linear_qscale[i] < s->c.avctx->qmin ||
  187. (ff_mpeg2_non_linear_qscale[i] > s->c.avctx->qmax && !m->vbv_ignore_qmax))
  188. continue;
  189. if (diff < bestdiff) {
  190. bestdiff = diff;
  191. best = i;
  192. }
  193. }
  194. s->c.qscale = best;
  195. } else {
  196. s->c.qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
  197. (FF_LAMBDA_SHIFT + 7);
  198. s->c.qscale = av_clip(s->c.qscale, s->c.avctx->qmin, m->vbv_ignore_qmax ? 31 : s->c.avctx->qmax);
  199. }
  200. s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
  201. FF_LAMBDA_SHIFT;
  202. }
  203. void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
  204. {
  205. int i;
  206. if (matrix) {
  207. put_bits(pb, 1, 1);
  208. for (i = 0; i < 64; i++) {
  209. put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
  210. }
  211. } else
  212. put_bits(pb, 1, 0);
  213. }
  214. /**
  215. * init s->c.cur_pic.qscale_table from s->lambda_table
  216. */
  217. static void init_qscale_tab(MPVEncContext *const s)
  218. {
  219. int8_t *const qscale_table = s->c.cur_pic.qscale_table;
  220. for (int i = 0; i < s->c.mb_num; i++) {
  221. unsigned int lam = s->lambda_table[s->c.mb_index2xy[i]];
  222. int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
  223. qscale_table[s->c.mb_index2xy[i]] = av_clip(qp, s->c.avctx->qmin,
  224. s->c.avctx->qmax);
  225. }
  226. }
  227. static void update_duplicate_context_after_me(MPVEncContext *const dst,
  228. const MPVEncContext *const src)
  229. {
  230. #define COPY(a) dst->a = src->a
  231. COPY(c.pict_type);
  232. COPY(f_code);
  233. COPY(b_code);
  234. COPY(c.qscale);
  235. COPY(lambda);
  236. COPY(lambda2);
  237. COPY(c.frame_pred_frame_dct); // FIXME don't set in encode_header
  238. COPY(c.progressive_frame); // FIXME don't set in encode_header
  239. COPY(partitioned_frame); // FIXME don't set in encode_header
  240. #undef COPY
  241. }
  242. static av_cold void mpv_encode_init_static(void)
  243. {
  244. for (int i = -16; i < 16; i++)
  245. default_fcode_tab[i + MAX_MV] = 1;
  246. }
  247. /**
  248. * Set the given MPVEncContext to defaults for encoding.
  249. */
  250. static av_cold void mpv_encode_defaults(MPVMainEncContext *const m)
  251. {
  252. MPVEncContext *const s = &m->s;
  253. static AVOnce init_static_once = AV_ONCE_INIT;
  254. ff_mpv_common_defaults(&s->c);
  255. s->f_code = 1;
  256. s->b_code = 1;
  257. if (!m->fcode_tab) {
  258. m->fcode_tab = default_fcode_tab + MAX_MV;
  259. ff_thread_once(&init_static_once, mpv_encode_init_static);
  260. }
  261. if (!s->c.y_dc_scale_table) {
  262. s->c.y_dc_scale_table =
  263. s->c.c_dc_scale_table = ff_mpeg1_dc_scale_table;
  264. }
  265. }
  266. av_cold void ff_dct_encode_init(MPVEncContext *const s)
  267. {
  268. s->dct_quantize = dct_quantize_c;
  269. s->denoise_dct = denoise_dct_c;
  270. #if ARCH_MIPS
  271. ff_mpvenc_dct_init_mips(s);
  272. #elif ARCH_X86
  273. ff_dct_encode_init_x86(s);
  274. #endif
  275. if (s->c.avctx->trellis)
  276. s->dct_quantize = dct_quantize_trellis_c;
  277. }
  278. static av_cold void init_unquantize(MPVEncContext *const s2, AVCodecContext *avctx)
  279. {
  280. MpegEncContext *const s = &s2->c;
  281. MPVUnquantDSPContext unquant_dsp_ctx;
  282. ff_mpv_unquantize_init(&unquant_dsp_ctx,
  283. avctx->flags & AV_CODEC_FLAG_BITEXACT, s->q_scale_type);
  284. if (s2->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  285. s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra;
  286. s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter;
  287. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  288. s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
  289. s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_h263_inter;
  290. } else {
  291. s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_mpeg1_intra;
  292. s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg1_inter;
  293. }
  294. }
  295. static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx)
  296. {
  297. MPVEncContext *const s = &m->s;
  298. MECmpContext mecc;
  299. me_cmp_func me_cmp[6];
  300. int ret;
  301. ff_me_cmp_init(&mecc, avctx);
  302. ret = ff_me_init(&s->me, avctx, &mecc, 1);
  303. if (ret < 0)
  304. return ret;
  305. ret = ff_set_cmp(&mecc, me_cmp, m->frame_skip_cmp, 1);
  306. if (ret < 0)
  307. return ret;
  308. m->frame_skip_cmp_fn = me_cmp[1];
  309. if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  310. ret = ff_set_cmp(&mecc, me_cmp, avctx->ildct_cmp, 1);
  311. if (ret < 0)
  312. return ret;
  313. if (!me_cmp[0] || !me_cmp[4])
  314. return AVERROR(EINVAL);
  315. s->ildct_cmp[0] = me_cmp[0];
  316. s->ildct_cmp[1] = me_cmp[4];
  317. }
  318. s->sum_abs_dctelem = mecc.sum_abs_dctelem;
  319. s->sse_cmp[0] = mecc.sse[0];
  320. s->sse_cmp[1] = mecc.sse[1];
  321. s->sad_cmp[0] = mecc.sad[0];
  322. s->sad_cmp[1] = mecc.sad[1];
  323. if (avctx->mb_cmp == FF_CMP_NSSE) {
  324. s->n_sse_cmp[0] = mecc.nsse[0];
  325. s->n_sse_cmp[1] = mecc.nsse[1];
  326. } else {
  327. s->n_sse_cmp[0] = mecc.sse[0];
  328. s->n_sse_cmp[1] = mecc.sse[1];
  329. }
  330. return 0;
  331. }
  332. #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
  333. static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avctx)
  334. {
  335. MPVEncContext *const s = &m->s;
  336. const int nb_matrices = 1 + (s->c.out_format == FMT_MJPEG) + !m->intra_only;
  337. const uint16_t *intra_matrix, *inter_matrix;
  338. int ret;
  339. if (!ALLOCZ_ARRAYS(s->q_intra_matrix, 32, nb_matrices) ||
  340. !ALLOCZ_ARRAYS(s->q_intra_matrix16, 32, nb_matrices))
  341. return AVERROR(ENOMEM);
  342. if (s->c.out_format == FMT_MJPEG) {
  343. s->q_chroma_intra_matrix = s->q_intra_matrix + 32;
  344. s->q_chroma_intra_matrix16 = s->q_intra_matrix16 + 32;
  345. // No need to set q_inter_matrix
  346. av_assert1(m->intra_only);
  347. // intra_matrix, chroma_intra_matrix will be set later for MJPEG.
  348. return 0;
  349. } else {
  350. s->q_chroma_intra_matrix = s->q_intra_matrix;
  351. s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
  352. }
  353. if (!m->intra_only) {
  354. s->q_inter_matrix = s->q_intra_matrix + 32;
  355. s->q_inter_matrix16 = s->q_intra_matrix16 + 32;
  356. }
  357. if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 &&
  358. s->mpeg_quant) {
  359. intra_matrix = ff_mpeg4_default_intra_matrix;
  360. inter_matrix = ff_mpeg4_default_non_intra_matrix;
  361. } else if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
  362. intra_matrix =
  363. inter_matrix = ff_mpeg1_default_non_intra_matrix;
  364. } else {
  365. /* MPEG-1/2, SpeedHQ */
  366. intra_matrix = ff_mpeg1_default_intra_matrix;
  367. inter_matrix = ff_mpeg1_default_non_intra_matrix;
  368. }
  369. if (avctx->intra_matrix)
  370. intra_matrix = avctx->intra_matrix;
  371. if (avctx->inter_matrix)
  372. inter_matrix = avctx->inter_matrix;
  373. /* init q matrix */
  374. for (int i = 0; i < 64; i++) {
  375. int j = s->c.idsp.idct_permutation[i];
  376. s->c.intra_matrix[j] = s->c.chroma_intra_matrix[j] = intra_matrix[i];
  377. s->c.inter_matrix[j] = inter_matrix[i];
  378. }
  379. /* precompute matrix */
  380. ret = ff_check_codec_matrices(avctx, FF_MATRIX_TYPE_INTRA | FF_MATRIX_TYPE_INTER, 1, 255);
  381. if (ret < 0)
  382. return ret;
  383. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  384. s->c.intra_matrix, s->intra_quant_bias, avctx->qmin,
  385. 31, 1);
  386. if (s->q_inter_matrix)
  387. ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
  388. s->c.inter_matrix, s->inter_quant_bias, avctx->qmin,
  389. 31, 0);
  390. return 0;
  391. }
  392. static av_cold int init_buffers(MPVMainEncContext *const m)
  393. {
  394. MPVEncContext *const s = &m->s;
  395. int has_b_frames = !!m->max_b_frames;
  396. int16_t (*mv_table)[2];
  397. /* Allocate MB type table */
  398. unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
  399. s->mb_type = av_calloc(mb_array_size, 3 * sizeof(*s->mb_type) + sizeof(*s->mb_mean));
  400. if (!s->mb_type)
  401. return AVERROR(ENOMEM);
  402. s->mc_mb_var = s->mb_type + mb_array_size;
  403. s->mb_var = s->mc_mb_var + mb_array_size;
  404. s->mb_mean = (uint8_t*)(s->mb_var + mb_array_size);
  405. if (!FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size))
  406. return AVERROR(ENOMEM);
  407. unsigned mv_table_size = (s->c.mb_height + 2) * s->c.mb_stride + 1;
  408. unsigned nb_mv_tables = 1 + 5 * has_b_frames;
  409. if (s->c.codec_id == AV_CODEC_ID_MPEG4 ||
  410. (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
  411. nb_mv_tables += 8 * has_b_frames;
  412. s->p_field_select_table[0] = av_calloc(mv_table_size, 2 * (2 + 4 * has_b_frames));
  413. if (!s->p_field_select_table[0])
  414. return AVERROR(ENOMEM);
  415. s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
  416. }
  417. mv_table = av_calloc(mv_table_size, nb_mv_tables * sizeof(*mv_table));
  418. if (!mv_table)
  419. return AVERROR(ENOMEM);
  420. m->mv_table_base = mv_table;
  421. mv_table += s->c.mb_stride + 1;
  422. s->p_mv_table = mv_table;
  423. if (has_b_frames) {
  424. s->b_forw_mv_table = mv_table += mv_table_size;
  425. s->b_back_mv_table = mv_table += mv_table_size;
  426. s->b_bidir_forw_mv_table = mv_table += mv_table_size;
  427. s->b_bidir_back_mv_table = mv_table += mv_table_size;
  428. s->b_direct_mv_table = mv_table += mv_table_size;
  429. if (s->p_field_select_table[1]) { // MPEG-4 or INTERLACED_ME above
  430. uint8_t *field_select = s->p_field_select_table[1];
  431. for (int j = 0; j < 2; j++) {
  432. for (int k = 0; k < 2; k++) {
  433. for (int l = 0; l < 2; l++)
  434. s->b_field_mv_table[j][k][l] = mv_table += mv_table_size;
  435. s->b_field_select_table[j][k] = field_select += 2 * mv_table_size;
  436. }
  437. }
  438. }
  439. }
  440. return 0;
  441. }
  442. static av_cold int init_slice_buffers(MPVMainEncContext *const m)
  443. {
  444. MPVEncContext *const s = &m->s;
  445. // Align the following per-thread buffers to avoid false sharing.
  446. enum {
  447. #ifndef _MSC_VER
  448. /// The number is supposed to match/exceed the cache-line size.
  449. ALIGN = FFMAX(128, _Alignof(max_align_t)),
  450. #else
  451. ALIGN = 128,
  452. #endif
  453. DCT_ERROR_SIZE = FFALIGN(2 * sizeof(*s->dct_error_sum), ALIGN),
  454. };
  455. static_assert(DCT_ERROR_SIZE * MAX_THREADS + ALIGN - 1 <= SIZE_MAX,
  456. "Need checks for potential overflow.");
  457. unsigned nb_slices = s->c.slice_context_count;
  458. char *dct_error = NULL;
  459. if (m->noise_reduction) {
  460. if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2))
  461. return AVERROR(ENOMEM);
  462. dct_error = av_mallocz(ALIGN - 1 + nb_slices * DCT_ERROR_SIZE);
  463. if (!dct_error)
  464. return AVERROR(ENOMEM);
  465. m->dct_error_sum_base = dct_error;
  466. dct_error += FFALIGN((uintptr_t)dct_error, ALIGN) - (uintptr_t)dct_error;
  467. }
  468. const int y_size = s->c.b8_stride * (2 * s->c.mb_height + 1);
  469. const int c_size = s->c.mb_stride * (s->c.mb_height + 1);
  470. const int yc_size = y_size + 2 * c_size;
  471. ptrdiff_t offset = 0;
  472. for (unsigned i = 0; i < nb_slices; ++i) {
  473. MPVEncContext *const s2 = s->c.enc_contexts[i];
  474. s2->block = s2->blocks[0];
  475. if (dct_error) {
  476. s2->dct_offset = s->dct_offset;
  477. s2->dct_error_sum = (void*)dct_error;
  478. dct_error += DCT_ERROR_SIZE;
  479. }
  480. if (s2->c.ac_val) {
  481. s2->c.dc_val += offset + i;
  482. s2->c.ac_val += offset;
  483. offset += yc_size;
  484. }
  485. }
  486. return 0;
  487. }
  488. /* init video encoder */
  489. av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
  490. {
  491. MPVMainEncContext *const m = avctx->priv_data;
  492. MPVEncContext *const s = &m->s;
  493. AVCPBProperties *cpb_props;
  494. int gcd, ret;
  495. mpv_encode_defaults(m);
  496. switch (avctx->pix_fmt) {
  497. case AV_PIX_FMT_YUVJ444P:
  498. case AV_PIX_FMT_YUV444P:
  499. s->c.chroma_format = CHROMA_444;
  500. break;
  501. case AV_PIX_FMT_YUVJ422P:
  502. case AV_PIX_FMT_YUV422P:
  503. s->c.chroma_format = CHROMA_422;
  504. break;
  505. default:
  506. av_unreachable("Already checked via CODEC_PIXFMTS");
  507. case AV_PIX_FMT_YUVJ420P:
  508. case AV_PIX_FMT_YUV420P:
  509. s->c.chroma_format = CHROMA_420;
  510. break;
  511. }
  512. avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
  513. m->bit_rate = avctx->bit_rate;
  514. s->c.width = avctx->width;
  515. s->c.height = avctx->height;
  516. if (avctx->gop_size > 600 &&
  517. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  518. av_log(avctx, AV_LOG_WARNING,
  519. "keyframe interval too large!, reducing it from %d to %d\n",
  520. avctx->gop_size, 600);
  521. avctx->gop_size = 600;
  522. }
  523. m->gop_size = avctx->gop_size;
  524. s->c.avctx = avctx;
  525. if (avctx->max_b_frames > MPVENC_MAX_B_FRAMES) {
  526. av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
  527. "is " AV_STRINGIFY(MPVENC_MAX_B_FRAMES) ".\n");
  528. avctx->max_b_frames = MPVENC_MAX_B_FRAMES;
  529. } else if (avctx->max_b_frames < 0) {
  530. av_log(avctx, AV_LOG_ERROR,
  531. "max b frames must be 0 or positive for mpegvideo based encoders\n");
  532. return AVERROR(EINVAL);
  533. }
  534. m->max_b_frames = avctx->max_b_frames;
  535. s->c.codec_id = avctx->codec->id;
  536. if (m->max_b_frames && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  537. av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
  538. return AVERROR(EINVAL);
  539. }
  540. s->c.quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
  541. s->rtp_mode = !!s->rtp_payload_size;
  542. s->c.intra_dc_precision = avctx->intra_dc_precision;
  543. // workaround some differences between how applications specify dc precision
  544. if (s->c.intra_dc_precision < 0) {
  545. s->c.intra_dc_precision += 8;
  546. } else if (s->c.intra_dc_precision >= 8)
  547. s->c.intra_dc_precision -= 8;
  548. if (s->c.intra_dc_precision < 0) {
  549. av_log(avctx, AV_LOG_ERROR,
  550. "intra dc precision must be positive, note some applications use"
  551. " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
  552. return AVERROR(EINVAL);
  553. }
  554. if (s->c.intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
  555. av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
  556. return AVERROR(EINVAL);
  557. }
  558. m->user_specified_pts = AV_NOPTS_VALUE;
  559. if (m->gop_size <= 1) {
  560. m->intra_only = 1;
  561. m->gop_size = 12;
  562. } else {
  563. m->intra_only = 0;
  564. }
  565. /* Fixed QSCALE */
  566. m->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
  567. s->adaptive_quant = (avctx->lumi_masking ||
  568. avctx->dark_masking ||
  569. avctx->temporal_cplx_masking ||
  570. avctx->spatial_cplx_masking ||
  571. avctx->p_masking ||
  572. m->border_masking ||
  573. (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
  574. !m->fixed_qscale;
  575. s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
  576. if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
  577. switch(avctx->codec_id) {
  578. case AV_CODEC_ID_MPEG1VIDEO:
  579. case AV_CODEC_ID_MPEG2VIDEO:
  580. avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
  581. break;
  582. case AV_CODEC_ID_MPEG4:
  583. case AV_CODEC_ID_MSMPEG4V1:
  584. case AV_CODEC_ID_MSMPEG4V2:
  585. case AV_CODEC_ID_MSMPEG4V3:
  586. if (avctx->rc_max_rate >= 15000000) {
  587. avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
  588. } else if(avctx->rc_max_rate >= 2000000) {
  589. avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
  590. } else if(avctx->rc_max_rate >= 384000) {
  591. avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
  592. } else
  593. avctx->rc_buffer_size = 40;
  594. avctx->rc_buffer_size *= 16384;
  595. break;
  596. }
  597. if (avctx->rc_buffer_size) {
  598. av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
  599. }
  600. }
  601. if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
  602. av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
  603. return AVERROR(EINVAL);
  604. }
  605. if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
  606. av_log(avctx, AV_LOG_INFO,
  607. "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
  608. }
  609. if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
  610. av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
  611. return AVERROR(EINVAL);
  612. }
  613. if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
  614. av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
  615. return AVERROR(EINVAL);
  616. }
  617. if (avctx->rc_max_rate &&
  618. avctx->rc_max_rate == avctx->bit_rate &&
  619. avctx->rc_max_rate != avctx->rc_min_rate) {
  620. av_log(avctx, AV_LOG_INFO,
  621. "impossible bitrate constraints, this will fail\n");
  622. }
  623. if (avctx->rc_buffer_size &&
  624. avctx->bit_rate * (int64_t)avctx->time_base.num >
  625. avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
  626. av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
  627. return AVERROR(EINVAL);
  628. }
  629. if (!m->fixed_qscale &&
  630. avctx->bit_rate * av_q2d(avctx->time_base) >
  631. avctx->bit_rate_tolerance) {
  632. double nbt = avctx->bit_rate * av_q2d(avctx->time_base) * 5;
  633. av_log(avctx, AV_LOG_WARNING,
  634. "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
  635. if (nbt <= INT_MAX) {
  636. avctx->bit_rate_tolerance = nbt;
  637. } else
  638. avctx->bit_rate_tolerance = INT_MAX;
  639. }
  640. if ((avctx->flags & AV_CODEC_FLAG_4MV) && s->c.codec_id != AV_CODEC_ID_MPEG4 &&
  641. s->c.codec_id != AV_CODEC_ID_H263 && s->c.codec_id != AV_CODEC_ID_H263P &&
  642. s->c.codec_id != AV_CODEC_ID_FLV1) {
  643. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  644. return AVERROR(EINVAL);
  645. }
  646. if (s->c.obmc && avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
  647. av_log(avctx, AV_LOG_ERROR,
  648. "OBMC is only supported with simple mb decision\n");
  649. return AVERROR(EINVAL);
  650. }
  651. if (s->c.quarter_sample && s->c.codec_id != AV_CODEC_ID_MPEG4) {
  652. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  653. return AVERROR(EINVAL);
  654. }
  655. if ((s->c.codec_id == AV_CODEC_ID_MPEG4 ||
  656. s->c.codec_id == AV_CODEC_ID_H263 ||
  657. s->c.codec_id == AV_CODEC_ID_H263P) &&
  658. (avctx->sample_aspect_ratio.num > 255 ||
  659. avctx->sample_aspect_ratio.den > 255)) {
  660. av_log(avctx, AV_LOG_WARNING,
  661. "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
  662. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
  663. av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
  664. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
  665. }
  666. if ((s->c.codec_id == AV_CODEC_ID_H263 ||
  667. s->c.codec_id == AV_CODEC_ID_H263P) &&
  668. (avctx->width > 2048 ||
  669. avctx->height > 1152 )) {
  670. av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
  671. return AVERROR(EINVAL);
  672. }
  673. if (s->c.codec_id == AV_CODEC_ID_FLV1 &&
  674. (avctx->width > 65535 ||
  675. avctx->height > 65535 )) {
  676. av_log(avctx, AV_LOG_ERROR, "FLV does not support resolutions above 16bit\n");
  677. return AVERROR(EINVAL);
  678. }
  679. if ((s->c.codec_id == AV_CODEC_ID_H263 ||
  680. s->c.codec_id == AV_CODEC_ID_H263P ||
  681. s->c.codec_id == AV_CODEC_ID_RV20) &&
  682. ((avctx->width &3) ||
  683. (avctx->height&3) )) {
  684. av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
  685. return AVERROR(EINVAL);
  686. }
  687. if (s->c.codec_id == AV_CODEC_ID_RV10 &&
  688. (avctx->width &15 ||
  689. avctx->height&15 )) {
  690. av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
  691. return AVERROR(EINVAL);
  692. }
  693. if ((s->c.codec_id == AV_CODEC_ID_WMV1 ||
  694. s->c.codec_id == AV_CODEC_ID_WMV2) &&
  695. avctx->width & 1) {
  696. av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
  697. return AVERROR(EINVAL);
  698. }
  699. if ((avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
  700. s->c.codec_id != AV_CODEC_ID_MPEG4 && s->c.codec_id != AV_CODEC_ID_MPEG2VIDEO) {
  701. av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
  702. return AVERROR(EINVAL);
  703. }
  704. if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
  705. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  706. return AVERROR(EINVAL);
  707. }
  708. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
  709. avctx->mb_decision != FF_MB_DECISION_RD) {
  710. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=rd\n");
  711. return AVERROR(EINVAL);
  712. }
  713. if (m->scenechange_threshold < 1000000000 &&
  714. (avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
  715. av_log(avctx, AV_LOG_ERROR,
  716. "closed gop with scene change detection are not supported yet, "
  717. "set threshold to 1000000000\n");
  718. return AVERROR_PATCHWELCOME;
  719. }
  720. if (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
  721. if (s->c.codec_id != AV_CODEC_ID_MPEG2VIDEO &&
  722. avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
  723. av_log(avctx, AV_LOG_ERROR,
  724. "low delay forcing is only available for mpeg2, "
  725. "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
  726. return AVERROR(EINVAL);
  727. }
  728. if (m->max_b_frames != 0) {
  729. av_log(avctx, AV_LOG_ERROR,
  730. "B-frames cannot be used with low delay\n");
  731. return AVERROR(EINVAL);
  732. }
  733. }
  734. if (avctx->slices > 1 &&
  735. !(avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS)) {
  736. av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
  737. return AVERROR(EINVAL);
  738. }
  739. if (m->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
  740. av_log(avctx, AV_LOG_INFO,
  741. "notice: b_frame_strategy only affects the first pass\n");
  742. m->b_frame_strategy = 0;
  743. }
  744. gcd = av_gcd(avctx->time_base.den, avctx->time_base.num);
  745. if (gcd > 1) {
  746. av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
  747. avctx->time_base.den /= gcd;
  748. avctx->time_base.num /= gcd;
  749. //return -1;
  750. }
  751. if (s->mpeg_quant || s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO || s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO || s->c.codec_id == AV_CODEC_ID_MJPEG || s->c.codec_id == AV_CODEC_ID_AMV || s->c.codec_id == AV_CODEC_ID_SPEEDHQ) {
  752. // (a + x * 3 / 8) / x
  753. s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
  754. s->inter_quant_bias = 0;
  755. } else {
  756. s->intra_quant_bias = 0;
  757. // (a - x / 4) / x
  758. s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
  759. }
  760. if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
  761. av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
  762. return AVERROR(EINVAL);
  763. }
  764. av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
  765. switch (avctx->codec->id) {
  766. #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
  767. case AV_CODEC_ID_MPEG2VIDEO:
  768. s->rtp_mode = 1;
  769. /* fallthrough */
  770. case AV_CODEC_ID_MPEG1VIDEO:
  771. s->c.out_format = FMT_MPEG1;
  772. s->c.low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
  773. avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
  774. ff_mpeg1_encode_init(s);
  775. break;
  776. #endif
  777. #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
  778. case AV_CODEC_ID_MJPEG:
  779. case AV_CODEC_ID_AMV:
  780. s->c.out_format = FMT_MJPEG;
  781. m->intra_only = 1; /* force intra only for jpeg */
  782. avctx->delay = 0;
  783. s->c.low_delay = 1;
  784. break;
  785. #endif
  786. case AV_CODEC_ID_SPEEDHQ:
  787. s->c.out_format = FMT_SPEEDHQ;
  788. m->intra_only = 1; /* force intra only for SHQ */
  789. avctx->delay = 0;
  790. s->c.low_delay = 1;
  791. break;
  792. case AV_CODEC_ID_H261:
  793. s->c.out_format = FMT_H261;
  794. avctx->delay = 0;
  795. s->c.low_delay = 1;
  796. s->rtp_mode = 0; /* Sliced encoding not supported */
  797. break;
  798. case AV_CODEC_ID_H263:
  799. if (!CONFIG_H263_ENCODER)
  800. return AVERROR_ENCODER_NOT_FOUND;
  801. if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
  802. s->c.width, s->c.height) == 8) {
  803. av_log(avctx, AV_LOG_ERROR,
  804. "The specified picture size of %dx%d is not valid for "
  805. "the H.263 codec.\nValid sizes are 128x96, 176x144, "
  806. "352x288, 704x576, and 1408x1152. "
  807. "Try H.263+.\n", s->c.width, s->c.height);
  808. return AVERROR(EINVAL);
  809. }
  810. s->c.out_format = FMT_H263;
  811. avctx->delay = 0;
  812. s->c.low_delay = 1;
  813. break;
  814. case AV_CODEC_ID_H263P:
  815. s->c.out_format = FMT_H263;
  816. /* Fx */
  817. s->c.h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
  818. s->modified_quant = s->c.h263_aic;
  819. s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
  820. s->me.unrestricted_mv = s->c.obmc || s->loop_filter || s->umvplus;
  821. s->flipflop_rounding = 1;
  822. /* /Fx */
  823. /* These are just to be sure */
  824. avctx->delay = 0;
  825. s->c.low_delay = 1;
  826. break;
  827. case AV_CODEC_ID_FLV1:
  828. s->c.out_format = FMT_H263;
  829. s->me.unrestricted_mv = 1;
  830. s->rtp_mode = 0; /* don't allow GOB */
  831. avctx->delay = 0;
  832. s->c.low_delay = 1;
  833. break;
  834. #if CONFIG_RV10_ENCODER
  835. case AV_CODEC_ID_RV10:
  836. m->encode_picture_header = ff_rv10_encode_picture_header;
  837. s->c.out_format = FMT_H263;
  838. avctx->delay = 0;
  839. s->c.low_delay = 1;
  840. break;
  841. #endif
  842. #if CONFIG_RV20_ENCODER
  843. case AV_CODEC_ID_RV20:
  844. m->encode_picture_header = ff_rv20_encode_picture_header;
  845. s->c.out_format = FMT_H263;
  846. avctx->delay = 0;
  847. s->c.low_delay = 1;
  848. s->modified_quant = 1;
  849. // Set here to force allocation of dc_val;
  850. // will be set later on a per-frame basis.
  851. s->c.h263_aic = 1;
  852. s->loop_filter = 1;
  853. s->me.unrestricted_mv = 0;
  854. break;
  855. #endif
  856. case AV_CODEC_ID_MPEG4:
  857. s->c.out_format = FMT_H263;
  858. s->c.h263_pred = 1;
  859. s->me.unrestricted_mv = 1;
  860. s->flipflop_rounding = 1;
  861. s->c.low_delay = m->max_b_frames ? 0 : 1;
  862. avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
  863. break;
  864. case AV_CODEC_ID_MSMPEG4V2:
  865. s->c.out_format = FMT_H263;
  866. s->c.h263_pred = 1;
  867. s->me.unrestricted_mv = 1;
  868. s->c.msmpeg4_version = MSMP4_V2;
  869. avctx->delay = 0;
  870. s->c.low_delay = 1;
  871. break;
  872. case AV_CODEC_ID_MSMPEG4V3:
  873. s->c.out_format = FMT_H263;
  874. s->c.h263_pred = 1;
  875. s->me.unrestricted_mv = 1;
  876. s->c.msmpeg4_version = MSMP4_V3;
  877. s->flipflop_rounding = 1;
  878. avctx->delay = 0;
  879. s->c.low_delay = 1;
  880. break;
  881. case AV_CODEC_ID_WMV1:
  882. s->c.out_format = FMT_H263;
  883. s->c.h263_pred = 1;
  884. s->me.unrestricted_mv = 1;
  885. s->c.msmpeg4_version = MSMP4_WMV1;
  886. s->flipflop_rounding = 1;
  887. avctx->delay = 0;
  888. s->c.low_delay = 1;
  889. break;
  890. case AV_CODEC_ID_WMV2:
  891. s->c.out_format = FMT_H263;
  892. s->c.h263_pred = 1;
  893. s->me.unrestricted_mv = 1;
  894. s->c.msmpeg4_version = MSMP4_WMV2;
  895. s->flipflop_rounding = 1;
  896. avctx->delay = 0;
  897. s->c.low_delay = 1;
  898. break;
  899. default:
  900. av_unreachable("List contains all codecs using ff_mpv_encode_init()");
  901. }
  902. avctx->has_b_frames = !s->c.low_delay;
  903. s->c.encoding = 1;
  904. s->c.progressive_frame =
  905. s->c.progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
  906. AV_CODEC_FLAG_INTERLACED_ME) ||
  907. s->c.alternate_scan);
  908. if (avctx->flags & AV_CODEC_FLAG_PSNR || avctx->mb_decision == FF_MB_DECISION_RD ||
  909. m->frame_skip_threshold || m->frame_skip_factor) {
  910. s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
  911. (1 << AV_PICTURE_TYPE_P) |
  912. (1 << AV_PICTURE_TYPE_B);
  913. } else if (!m->intra_only) {
  914. s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
  915. (1 << AV_PICTURE_TYPE_P);
  916. } else {
  917. s->frame_reconstruction_bitfield = 0;
  918. }
  919. if (m->lmin > m->lmax) {
  920. av_log(avctx, AV_LOG_WARNING, "Clipping lmin value to %d\n", m->lmax);
  921. m->lmin = m->lmax;
  922. }
  923. /* ff_mpv_init_duplicate_contexts() will copy (memdup) the contents of the
  924. * main slice to the slice contexts, so we initialize various fields of it
  925. * before calling ff_mpv_init_duplicate_contexts(). */
  926. s->parent = m;
  927. ff_mpv_idct_init(&s->c);
  928. init_unquantize(s, avctx);
  929. ff_fdctdsp_init(&s->fdsp, avctx);
  930. ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
  931. ff_pixblockdsp_init(&s->pdsp, 8);
  932. ret = me_cmp_init(m, avctx);
  933. if (ret < 0)
  934. return ret;
  935. if (!(avctx->stats_out = av_mallocz(256)) ||
  936. !(s->new_pic = av_frame_alloc()) ||
  937. !(s->c.picture_pool = ff_mpv_alloc_pic_pool(0)))
  938. return AVERROR(ENOMEM);
  939. ret = init_matrices(m, avctx);
  940. if (ret < 0)
  941. return ret;
  942. ff_dct_encode_init(s);
  943. if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263) {
  944. ff_h263_encode_init(m);
  945. #if CONFIG_MSMPEG4ENC
  946. if (s->c.msmpeg4_version != MSMP4_UNUSED)
  947. ff_msmpeg4_encode_init(m);
  948. #endif
  949. }
  950. s->c.slice_ctx_size = sizeof(*s);
  951. ret = ff_mpv_common_init(&s->c);
  952. if (ret < 0)
  953. return ret;
  954. ret = init_buffers(m);
  955. if (ret < 0)
  956. return ret;
  957. if (s->c.slice_context_count > 1) {
  958. s->rtp_mode = 1;
  959. if (avctx->codec_id == AV_CODEC_ID_H263P)
  960. s->h263_slice_structured = 1;
  961. }
  962. ret = ff_mpv_init_duplicate_contexts(&s->c);
  963. if (ret < 0)
  964. return ret;
  965. ret = init_slice_buffers(m);
  966. if (ret < 0)
  967. return ret;
  968. ret = ff_rate_control_init(m);
  969. if (ret < 0)
  970. return ret;
  971. if (m->b_frame_strategy == 2) {
  972. for (int i = 0; i < m->max_b_frames + 2; i++) {
  973. m->tmp_frames[i] = av_frame_alloc();
  974. if (!m->tmp_frames[i])
  975. return AVERROR(ENOMEM);
  976. m->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
  977. m->tmp_frames[i]->width = s->c.width >> m->brd_scale;
  978. m->tmp_frames[i]->height = s->c.height >> m->brd_scale;
  979. ret = av_frame_get_buffer(m->tmp_frames[i], 0);
  980. if (ret < 0)
  981. return ret;
  982. }
  983. }
  984. cpb_props = ff_encode_add_cpb_side_data(avctx);
  985. if (!cpb_props)
  986. return AVERROR(ENOMEM);
  987. cpb_props->max_bitrate = avctx->rc_max_rate;
  988. cpb_props->min_bitrate = avctx->rc_min_rate;
  989. cpb_props->avg_bitrate = avctx->bit_rate;
  990. cpb_props->buffer_size = avctx->rc_buffer_size;
  991. return 0;
  992. }
  993. av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
  994. {
  995. MPVMainEncContext *const m = avctx->priv_data;
  996. MPVEncContext *const s = &m->s;
  997. ff_rate_control_uninit(&m->rc_context);
  998. ff_mpv_common_end(&s->c);
  999. av_refstruct_pool_uninit(&s->c.picture_pool);
  1000. for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
  1001. av_refstruct_unref(&m->input_picture[i]);
  1002. av_refstruct_unref(&m->reordered_input_picture[i]);
  1003. }
  1004. for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
  1005. av_frame_free(&m->tmp_frames[i]);
  1006. av_frame_free(&s->new_pic);
  1007. av_freep(&avctx->stats_out);
  1008. av_freep(&m->mv_table_base);
  1009. av_freep(&s->p_field_select_table[0]);
  1010. av_freep(&m->dct_error_sum_base);
  1011. av_freep(&s->mb_type);
  1012. av_freep(&s->lambda_table);
  1013. av_freep(&s->q_intra_matrix);
  1014. av_freep(&s->q_intra_matrix16);
  1015. av_freep(&s->dct_offset);
  1016. return 0;
  1017. }
  1018. /* put block[] to dest[] */
  1019. static inline void put_dct(MPVEncContext *const s,
  1020. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1021. {
  1022. s->c.dct_unquantize_intra(&s->c, block, i, qscale);
  1023. s->c.idsp.idct_put(dest, line_size, block);
  1024. }
  1025. static inline void add_dequant_dct(MPVEncContext *const s,
  1026. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1027. {
  1028. if (s->c.block_last_index[i] >= 0) {
  1029. s->c.dct_unquantize_inter(&s->c, block, i, qscale);
  1030. s->c.idsp.idct_add(dest, line_size, block);
  1031. }
  1032. }
  1033. /**
  1034. * Performs dequantization and IDCT (if necessary)
  1035. */
  1036. static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64])
  1037. {
  1038. if (s->c.avctx->debug & FF_DEBUG_DCT_COEFF) {
  1039. /* print DCT coefficients */
  1040. av_log(s->c.avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->c.mb_x, s->c.mb_y);
  1041. for (int i = 0; i < 6; i++) {
  1042. for (int j = 0; j < 64; j++) {
  1043. av_log(s->c.avctx, AV_LOG_DEBUG, "%5d",
  1044. block[i][s->c.idsp.idct_permutation[j]]);
  1045. }
  1046. av_log(s->c.avctx, AV_LOG_DEBUG, "\n");
  1047. }
  1048. }
  1049. if ((1 << s->c.pict_type) & s->frame_reconstruction_bitfield) {
  1050. uint8_t *dest_y = s->c.dest[0], *dest_cb = s->c.dest[1], *dest_cr = s->c.dest[2];
  1051. int dct_linesize, dct_offset;
  1052. const int linesize = s->c.cur_pic.linesize[0];
  1053. const int uvlinesize = s->c.cur_pic.linesize[1];
  1054. const int block_size = 8;
  1055. dct_linesize = linesize << s->c.interlaced_dct;
  1056. dct_offset = s->c.interlaced_dct ? linesize : linesize * block_size;
  1057. if (!s->c.mb_intra) {
  1058. /* No MC, as that was already done otherwise */
  1059. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
  1060. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
  1061. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
  1062. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->c.qscale);
  1063. if (!CONFIG_GRAY || !(s->c.avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1064. if (s->c.chroma_y_shift) {
  1065. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
  1066. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale);
  1067. } else {
  1068. dct_linesize >>= 1;
  1069. dct_offset >>= 1;
  1070. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->c.chroma_qscale);
  1071. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->c.chroma_qscale);
  1072. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->c.chroma_qscale);
  1073. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->c.chroma_qscale);
  1074. }
  1075. }
  1076. } else {
  1077. /* dct only in intra block */
  1078. put_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
  1079. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
  1080. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
  1081. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->c.qscale);
  1082. if (!CONFIG_GRAY || !(s->c.avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1083. if (s->c.chroma_y_shift) {
  1084. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
  1085. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale);
  1086. } else {
  1087. dct_offset >>= 1;
  1088. dct_linesize >>= 1;
  1089. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->c.chroma_qscale);
  1090. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->c.chroma_qscale);
  1091. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->c.chroma_qscale);
  1092. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->c.chroma_qscale);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }
  1098. static int get_sae(const uint8_t *src, int ref, int stride)
  1099. {
  1100. int x,y;
  1101. int acc = 0;
  1102. for (y = 0; y < 16; y++) {
  1103. for (x = 0; x < 16; x++) {
  1104. acc += FFABS(src[x + y * stride] - ref);
  1105. }
  1106. }
  1107. return acc;
  1108. }
  1109. static int get_intra_count(MPVEncContext *const s, const uint8_t *src,
  1110. const uint8_t *ref, int stride)
  1111. {
  1112. int x, y, w, h;
  1113. int acc = 0;
  1114. w = s->c.width & ~15;
  1115. h = s->c.height & ~15;
  1116. for (y = 0; y < h; y += 16) {
  1117. for (x = 0; x < w; x += 16) {
  1118. int offset = x + y * stride;
  1119. int sad = s->sad_cmp[0](NULL, src + offset, ref + offset,
  1120. stride, 16);
  1121. int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
  1122. int sae = get_sae(src + offset, mean, stride);
  1123. acc += sae + 500 < sad;
  1124. }
  1125. }
  1126. return acc;
  1127. }
  1128. /**
  1129. * Allocates new buffers for an AVFrame and copies the properties
  1130. * from another AVFrame.
  1131. */
  1132. static int prepare_picture(MPVEncContext *const s, AVFrame *f, const AVFrame *props_frame)
  1133. {
  1134. AVCodecContext *avctx = s->c.avctx;
  1135. int ret;
  1136. f->width = avctx->width + 2 * EDGE_WIDTH;
  1137. f->height = avctx->height + 2 * EDGE_WIDTH;
  1138. ret = ff_encode_alloc_frame(avctx, f);
  1139. if (ret < 0)
  1140. return ret;
  1141. ret = ff_mpv_pic_check_linesize(avctx, f, &s->c.linesize, &s->c.uvlinesize);
  1142. if (ret < 0)
  1143. return ret;
  1144. for (int i = 0; f->data[i]; i++) {
  1145. int offset = (EDGE_WIDTH >> (i ? s->c.chroma_y_shift : 0)) *
  1146. f->linesize[i] +
  1147. (EDGE_WIDTH >> (i ? s->c.chroma_x_shift : 0));
  1148. f->data[i] += offset;
  1149. }
  1150. f->width = avctx->width;
  1151. f->height = avctx->height;
  1152. ret = av_frame_copy_props(f, props_frame);
  1153. if (ret < 0)
  1154. return ret;
  1155. return 0;
  1156. }
  1157. static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg)
  1158. {
  1159. MPVEncContext *const s = &m->s;
  1160. MPVPicture *pic = NULL;
  1161. int64_t pts;
  1162. int display_picture_number = 0, ret;
  1163. int encoding_delay = m->max_b_frames ? m->max_b_frames
  1164. : (s->c.low_delay ? 0 : 1);
  1165. int flush_offset = 1;
  1166. int direct = 1;
  1167. av_assert1(!m->input_picture[0]);
  1168. if (pic_arg) {
  1169. pts = pic_arg->pts;
  1170. display_picture_number = m->input_picture_number++;
  1171. if (pts != AV_NOPTS_VALUE) {
  1172. if (m->user_specified_pts != AV_NOPTS_VALUE) {
  1173. int64_t last = m->user_specified_pts;
  1174. if (pts <= last) {
  1175. av_log(s->c.avctx, AV_LOG_ERROR,
  1176. "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
  1177. pts, last);
  1178. return AVERROR(EINVAL);
  1179. }
  1180. if (!s->c.low_delay && display_picture_number == 1)
  1181. m->dts_delta = pts - last;
  1182. }
  1183. m->user_specified_pts = pts;
  1184. } else {
  1185. if (m->user_specified_pts != AV_NOPTS_VALUE) {
  1186. m->user_specified_pts =
  1187. pts = m->user_specified_pts + 1;
  1188. av_log(s->c.avctx, AV_LOG_INFO,
  1189. "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
  1190. pts);
  1191. } else {
  1192. pts = display_picture_number;
  1193. }
  1194. }
  1195. if (pic_arg->linesize[0] != s->c.linesize ||
  1196. pic_arg->linesize[1] != s->c.uvlinesize ||
  1197. pic_arg->linesize[2] != s->c.uvlinesize)
  1198. direct = 0;
  1199. if ((s->c.width & 15) || (s->c.height & 15))
  1200. direct = 0;
  1201. if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
  1202. direct = 0;
  1203. if (s->c.linesize & (STRIDE_ALIGN-1))
  1204. direct = 0;
  1205. ff_dlog(s->c.avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
  1206. pic_arg->linesize[1], s->c.linesize, s->c.uvlinesize);
  1207. pic = av_refstruct_pool_get(s->c.picture_pool);
  1208. if (!pic)
  1209. return AVERROR(ENOMEM);
  1210. if (direct) {
  1211. if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
  1212. goto fail;
  1213. pic->shared = 1;
  1214. } else {
  1215. ret = prepare_picture(s, pic->f, pic_arg);
  1216. if (ret < 0)
  1217. goto fail;
  1218. for (int i = 0; i < 3; i++) {
  1219. ptrdiff_t src_stride = pic_arg->linesize[i];
  1220. ptrdiff_t dst_stride = i ? s->c.uvlinesize : s->c.linesize;
  1221. int h_shift = i ? s->c.chroma_x_shift : 0;
  1222. int v_shift = i ? s->c.chroma_y_shift : 0;
  1223. int w = AV_CEIL_RSHIFT(s->c.width , h_shift);
  1224. int h = AV_CEIL_RSHIFT(s->c.height, v_shift);
  1225. const uint8_t *src = pic_arg->data[i];
  1226. uint8_t *dst = pic->f->data[i];
  1227. int vpad = 16;
  1228. if ( s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO
  1229. && !s->c.progressive_sequence
  1230. && FFALIGN(s->c.height, 32) - s->c.height > 16)
  1231. vpad = 32;
  1232. if (!s->c.avctx->rc_buffer_size)
  1233. dst += INPLACE_OFFSET;
  1234. if (src_stride == dst_stride)
  1235. memcpy(dst, src, src_stride * h - src_stride + w);
  1236. else {
  1237. int h2 = h;
  1238. uint8_t *dst2 = dst;
  1239. while (h2--) {
  1240. memcpy(dst2, src, w);
  1241. dst2 += dst_stride;
  1242. src += src_stride;
  1243. }
  1244. }
  1245. if ((s->c.width & 15) || (s->c.height & (vpad-1))) {
  1246. s->mpvencdsp.draw_edges(dst, dst_stride,
  1247. w, h,
  1248. 16 >> h_shift,
  1249. vpad >> v_shift,
  1250. EDGE_BOTTOM);
  1251. }
  1252. }
  1253. emms_c();
  1254. }
  1255. pic->display_picture_number = display_picture_number;
  1256. pic->f->pts = pts; // we set this here to avoid modifying pic_arg
  1257. } else if (!m->reordered_input_picture[1]) {
  1258. /* Flushing: When the above check is true, the encoder is about to run
  1259. * out of frames to encode. Check if there are input_pictures left;
  1260. * if so, ensure m->input_picture[0] contains the first picture.
  1261. * A flush_offset != 1 will only happen if we did not receive enough
  1262. * input frames. */
  1263. for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
  1264. if (m->input_picture[flush_offset])
  1265. break;
  1266. encoding_delay -= flush_offset - 1;
  1267. }
  1268. /* shift buffer entries */
  1269. for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
  1270. m->input_picture[i - flush_offset] = m->input_picture[i];
  1271. for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
  1272. m->input_picture[i] = NULL;
  1273. m->input_picture[encoding_delay] = pic;
  1274. return 0;
  1275. fail:
  1276. av_refstruct_unref(&pic);
  1277. return ret;
  1278. }
  1279. static int skip_check(MPVMainEncContext *const m,
  1280. const MPVPicture *p, const MPVPicture *ref)
  1281. {
  1282. MPVEncContext *const s = &m->s;
  1283. int score = 0;
  1284. int64_t score64 = 0;
  1285. for (int plane = 0; plane < 3; plane++) {
  1286. const int stride = p->f->linesize[plane];
  1287. const int bw = plane ? 1 : 2;
  1288. for (int y = 0; y < s->c.mb_height * bw; y++) {
  1289. for (int x = 0; x < s->c.mb_width * bw; x++) {
  1290. int off = p->shared ? 0 : 16;
  1291. const uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
  1292. const uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
  1293. int v = m->frame_skip_cmp_fn(s, dptr, rptr, stride, 8);
  1294. switch (FFABS(m->frame_skip_exp)) {
  1295. case 0: score = FFMAX(score, v); break;
  1296. case 1: score += FFABS(v); break;
  1297. case 2: score64 += v * (int64_t)v; break;
  1298. case 3: score64 += FFABS(v * (int64_t)v * v); break;
  1299. case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
  1300. }
  1301. }
  1302. }
  1303. }
  1304. emms_c();
  1305. if (score)
  1306. score64 = score;
  1307. if (m->frame_skip_exp < 0)
  1308. score64 = pow(score64 / (double)(s->c.mb_width * s->c.mb_height),
  1309. -1.0/m->frame_skip_exp);
  1310. if (score64 < m->frame_skip_threshold)
  1311. return 1;
  1312. if (score64 < ((m->frame_skip_factor * (int64_t) s->lambda) >> 8))
  1313. return 1;
  1314. return 0;
  1315. }
  1316. static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt)
  1317. {
  1318. int ret;
  1319. int size = 0;
  1320. ret = avcodec_send_frame(c, frame);
  1321. if (ret < 0)
  1322. return ret;
  1323. do {
  1324. ret = avcodec_receive_packet(c, pkt);
  1325. if (ret >= 0) {
  1326. size += pkt->size;
  1327. av_packet_unref(pkt);
  1328. } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  1329. return ret;
  1330. } while (ret >= 0);
  1331. return size;
  1332. }
  1333. static int estimate_best_b_count(MPVMainEncContext *const m)
  1334. {
  1335. MPVEncContext *const s = &m->s;
  1336. AVPacket *pkt;
  1337. const int scale = m->brd_scale;
  1338. int width = s->c.width >> scale;
  1339. int height = s->c.height >> scale;
  1340. int out_size, p_lambda, b_lambda, lambda2;
  1341. int64_t best_rd = INT64_MAX;
  1342. int best_b_count = -1;
  1343. int ret = 0;
  1344. av_assert0(scale >= 0 && scale <= 3);
  1345. pkt = av_packet_alloc();
  1346. if (!pkt)
  1347. return AVERROR(ENOMEM);
  1348. //emms_c();
  1349. p_lambda = m->last_lambda_for[AV_PICTURE_TYPE_P];
  1350. //p_lambda * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset;
  1351. b_lambda = m->last_lambda_for[AV_PICTURE_TYPE_B];
  1352. if (!b_lambda) // FIXME we should do this somewhere else
  1353. b_lambda = p_lambda;
  1354. lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
  1355. FF_LAMBDA_SHIFT;
  1356. for (int i = 0; i < m->max_b_frames + 2; i++) {
  1357. const MPVPicture *pre_input_ptr = i ? m->input_picture[i - 1] :
  1358. s->c.next_pic.ptr;
  1359. if (pre_input_ptr) {
  1360. const uint8_t *data[4];
  1361. memcpy(data, pre_input_ptr->f->data, sizeof(data));
  1362. if (!pre_input_ptr->shared && i) {
  1363. data[0] += INPLACE_OFFSET;
  1364. data[1] += INPLACE_OFFSET;
  1365. data[2] += INPLACE_OFFSET;
  1366. }
  1367. s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0],
  1368. m->tmp_frames[i]->linesize[0],
  1369. data[0],
  1370. pre_input_ptr->f->linesize[0],
  1371. width, height);
  1372. s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1],
  1373. m->tmp_frames[i]->linesize[1],
  1374. data[1],
  1375. pre_input_ptr->f->linesize[1],
  1376. width >> 1, height >> 1);
  1377. s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2],
  1378. m->tmp_frames[i]->linesize[2],
  1379. data[2],
  1380. pre_input_ptr->f->linesize[2],
  1381. width >> 1, height >> 1);
  1382. }
  1383. }
  1384. for (int j = 0; j < m->max_b_frames + 1; j++) {
  1385. AVCodecContext *c;
  1386. int64_t rd = 0;
  1387. if (!m->input_picture[j])
  1388. break;
  1389. c = avcodec_alloc_context3(NULL);
  1390. if (!c) {
  1391. ret = AVERROR(ENOMEM);
  1392. goto fail;
  1393. }
  1394. c->width = width;
  1395. c->height = height;
  1396. c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
  1397. c->flags |= s->c.avctx->flags & AV_CODEC_FLAG_QPEL;
  1398. c->mb_decision = s->c.avctx->mb_decision;
  1399. c->me_cmp = s->c.avctx->me_cmp;
  1400. c->mb_cmp = s->c.avctx->mb_cmp;
  1401. c->me_sub_cmp = s->c.avctx->me_sub_cmp;
  1402. c->pix_fmt = AV_PIX_FMT_YUV420P;
  1403. c->time_base = s->c.avctx->time_base;
  1404. c->max_b_frames = m->max_b_frames;
  1405. ret = avcodec_open2(c, s->c.avctx->codec, NULL);
  1406. if (ret < 0)
  1407. goto fail;
  1408. m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
  1409. m->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
  1410. out_size = encode_frame(c, m->tmp_frames[0], pkt);
  1411. if (out_size < 0) {
  1412. ret = out_size;
  1413. goto fail;
  1414. }
  1415. //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
  1416. for (int i = 0; i < m->max_b_frames + 1; i++) {
  1417. int is_p = i % (j + 1) == j || i == m->max_b_frames;
  1418. m->tmp_frames[i + 1]->pict_type = is_p ?
  1419. AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
  1420. m->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
  1421. out_size = encode_frame(c, m->tmp_frames[i + 1], pkt);
  1422. if (out_size < 0) {
  1423. ret = out_size;
  1424. goto fail;
  1425. }
  1426. rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
  1427. }
  1428. /* get the delayed frames */
  1429. out_size = encode_frame(c, NULL, pkt);
  1430. if (out_size < 0) {
  1431. ret = out_size;
  1432. goto fail;
  1433. }
  1434. rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
  1435. rd += c->error[0] + c->error[1] + c->error[2];
  1436. if (rd < best_rd) {
  1437. best_rd = rd;
  1438. best_b_count = j;
  1439. }
  1440. fail:
  1441. avcodec_free_context(&c);
  1442. av_packet_unref(pkt);
  1443. if (ret < 0) {
  1444. best_b_count = ret;
  1445. break;
  1446. }
  1447. }
  1448. av_packet_free(&pkt);
  1449. return best_b_count;
  1450. }
  1451. /**
  1452. * Determines whether an input picture is discarded or not
  1453. * and if not determines the length of the next chain of B frames
  1454. * and moves these pictures (including the P frame) into
  1455. * reordered_input_picture.
  1456. * input_picture[0] is always NULL when exiting this function, even on error;
  1457. * reordered_input_picture[0] is always NULL when exiting this function on error.
  1458. */
  1459. static int set_bframe_chain_length(MPVMainEncContext *const m)
  1460. {
  1461. MPVEncContext *const s = &m->s;
  1462. /* Either nothing to do or can't do anything */
  1463. if (m->reordered_input_picture[0] || !m->input_picture[0])
  1464. return 0;
  1465. /* set next picture type & ordering */
  1466. if (m->frame_skip_threshold || m->frame_skip_factor) {
  1467. if (m->picture_in_gop_number < m->gop_size &&
  1468. s->c.next_pic.ptr &&
  1469. skip_check(m, m->input_picture[0], s->c.next_pic.ptr)) {
  1470. // FIXME check that the gop check above is +-1 correct
  1471. av_refstruct_unref(&m->input_picture[0]);
  1472. ff_vbv_update(m, 0);
  1473. return 0;
  1474. }
  1475. }
  1476. if (/* m->picture_in_gop_number >= m->gop_size || */
  1477. !s->c.next_pic.ptr || m->intra_only) {
  1478. m->reordered_input_picture[0] = m->input_picture[0];
  1479. m->input_picture[0] = NULL;
  1480. m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
  1481. m->reordered_input_picture[0]->coded_picture_number =
  1482. m->coded_picture_number++;
  1483. } else {
  1484. int b_frames = 0;
  1485. if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
  1486. for (int i = 0; i < m->max_b_frames + 1; i++) {
  1487. int pict_num = m->input_picture[0]->display_picture_number + i;
  1488. if (pict_num >= m->rc_context.num_entries)
  1489. break;
  1490. if (!m->input_picture[i]) {
  1491. m->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
  1492. break;
  1493. }
  1494. m->input_picture[i]->f->pict_type =
  1495. m->rc_context.entry[pict_num].new_pict_type;
  1496. }
  1497. }
  1498. if (m->b_frame_strategy == 0) {
  1499. b_frames = m->max_b_frames;
  1500. while (b_frames && !m->input_picture[b_frames])
  1501. b_frames--;
  1502. } else if (m->b_frame_strategy == 1) {
  1503. for (int i = 1; i < m->max_b_frames + 1; i++) {
  1504. if (m->input_picture[i] &&
  1505. m->input_picture[i]->b_frame_score == 0) {
  1506. m->input_picture[i]->b_frame_score =
  1507. get_intra_count(s,
  1508. m->input_picture[i ]->f->data[0],
  1509. m->input_picture[i - 1]->f->data[0],
  1510. s->c.linesize) + 1;
  1511. }
  1512. }
  1513. for (int i = 0;; i++) {
  1514. if (i >= m->max_b_frames + 1 ||
  1515. !m->input_picture[i] ||
  1516. m->input_picture[i]->b_frame_score - 1 >
  1517. s->c.mb_num / m->b_sensitivity) {
  1518. b_frames = FFMAX(0, i - 1);
  1519. break;
  1520. }
  1521. }
  1522. /* reset scores */
  1523. for (int i = 0; i < b_frames + 1; i++)
  1524. m->input_picture[i]->b_frame_score = 0;
  1525. } else if (m->b_frame_strategy == 2) {
  1526. b_frames = estimate_best_b_count(m);
  1527. if (b_frames < 0) {
  1528. av_refstruct_unref(&m->input_picture[0]);
  1529. return b_frames;
  1530. }
  1531. }
  1532. emms_c();
  1533. for (int i = b_frames - 1; i >= 0; i--) {
  1534. int type = m->input_picture[i]->f->pict_type;
  1535. if (type && type != AV_PICTURE_TYPE_B)
  1536. b_frames = i;
  1537. }
  1538. if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
  1539. b_frames == m->max_b_frames) {
  1540. av_log(s->c.avctx, AV_LOG_ERROR,
  1541. "warning, too many B-frames in a row\n");
  1542. }
  1543. if (m->picture_in_gop_number + b_frames >= m->gop_size) {
  1544. if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
  1545. m->gop_size > m->picture_in_gop_number) {
  1546. b_frames = m->gop_size - m->picture_in_gop_number - 1;
  1547. } else {
  1548. if (s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
  1549. b_frames = 0;
  1550. m->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
  1551. }
  1552. }
  1553. if ((s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
  1554. m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
  1555. b_frames--;
  1556. m->reordered_input_picture[0] = m->input_picture[b_frames];
  1557. m->input_picture[b_frames] = NULL;
  1558. if (m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
  1559. m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
  1560. m->reordered_input_picture[0]->coded_picture_number =
  1561. m->coded_picture_number++;
  1562. for (int i = 0; i < b_frames; i++) {
  1563. m->reordered_input_picture[i + 1] = m->input_picture[i];
  1564. m->input_picture[i] = NULL;
  1565. m->reordered_input_picture[i + 1]->f->pict_type =
  1566. AV_PICTURE_TYPE_B;
  1567. m->reordered_input_picture[i + 1]->coded_picture_number =
  1568. m->coded_picture_number++;
  1569. }
  1570. }
  1571. return 0;
  1572. }
  1573. static int select_input_picture(MPVMainEncContext *const m)
  1574. {
  1575. MPVEncContext *const s = &m->s;
  1576. int ret;
  1577. av_assert1(!m->reordered_input_picture[0]);
  1578. for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++)
  1579. m->reordered_input_picture[i - 1] = m->reordered_input_picture[i];
  1580. m->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
  1581. ret = set_bframe_chain_length(m);
  1582. av_assert1(!m->input_picture[0]);
  1583. if (ret < 0)
  1584. return ret;
  1585. av_frame_unref(s->new_pic);
  1586. if (m->reordered_input_picture[0]) {
  1587. m->reordered_input_picture[0]->reference =
  1588. m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B;
  1589. if (m->reordered_input_picture[0]->shared || s->c.avctx->rc_buffer_size) {
  1590. // input is a shared pix, so we can't modify it -> allocate a new
  1591. // one & ensure that the shared one is reusable
  1592. av_frame_move_ref(s->new_pic, m->reordered_input_picture[0]->f);
  1593. ret = prepare_picture(s, m->reordered_input_picture[0]->f, s->new_pic);
  1594. if (ret < 0)
  1595. goto fail;
  1596. } else {
  1597. // input is not a shared pix -> reuse buffer for current_pix
  1598. ret = av_frame_ref(s->new_pic, m->reordered_input_picture[0]->f);
  1599. if (ret < 0)
  1600. goto fail;
  1601. for (int i = 0; i < MPV_MAX_PLANES; i++)
  1602. s->new_pic->data[i] += INPLACE_OFFSET;
  1603. }
  1604. s->c.cur_pic.ptr = m->reordered_input_picture[0];
  1605. m->reordered_input_picture[0] = NULL;
  1606. av_assert1(s->c.mb_width == s->c.buffer_pools.alloc_mb_width);
  1607. av_assert1(s->c.mb_height == s->c.buffer_pools.alloc_mb_height);
  1608. av_assert1(s->c.mb_stride == s->c.buffer_pools.alloc_mb_stride);
  1609. ret = ff_mpv_alloc_pic_accessories(s->c.avctx, &s->c.cur_pic,
  1610. &s->c.sc, &s->c.buffer_pools, s->c.mb_height);
  1611. if (ret < 0) {
  1612. ff_mpv_unref_picture(&s->c.cur_pic);
  1613. return ret;
  1614. }
  1615. s->picture_number = s->c.cur_pic.ptr->display_picture_number;
  1616. }
  1617. return 0;
  1618. fail:
  1619. av_refstruct_unref(&m->reordered_input_picture[0]);
  1620. return ret;
  1621. }
  1622. static void frame_end(MPVMainEncContext *const m)
  1623. {
  1624. MPVEncContext *const s = &m->s;
  1625. if (s->me.unrestricted_mv &&
  1626. s->c.cur_pic.reference &&
  1627. !m->intra_only) {
  1628. int hshift = s->c.chroma_x_shift;
  1629. int vshift = s->c.chroma_y_shift;
  1630. s->mpvencdsp.draw_edges(s->c.cur_pic.data[0],
  1631. s->c.cur_pic.linesize[0],
  1632. s->c.h_edge_pos, s->c.v_edge_pos,
  1633. EDGE_WIDTH, EDGE_WIDTH,
  1634. EDGE_TOP | EDGE_BOTTOM);
  1635. s->mpvencdsp.draw_edges(s->c.cur_pic.data[1],
  1636. s->c.cur_pic.linesize[1],
  1637. s->c.h_edge_pos >> hshift,
  1638. s->c.v_edge_pos >> vshift,
  1639. EDGE_WIDTH >> hshift,
  1640. EDGE_WIDTH >> vshift,
  1641. EDGE_TOP | EDGE_BOTTOM);
  1642. s->mpvencdsp.draw_edges(s->c.cur_pic.data[2],
  1643. s->c.cur_pic.linesize[2],
  1644. s->c.h_edge_pos >> hshift,
  1645. s->c.v_edge_pos >> vshift,
  1646. EDGE_WIDTH >> hshift,
  1647. EDGE_WIDTH >> vshift,
  1648. EDGE_TOP | EDGE_BOTTOM);
  1649. }
  1650. emms_c();
  1651. m->last_pict_type = s->c.pict_type;
  1652. m->last_lambda_for[s->c.pict_type] = s->c.cur_pic.ptr->f->quality;
  1653. if (s->c.pict_type != AV_PICTURE_TYPE_B)
  1654. m->last_non_b_pict_type = s->c.pict_type;
  1655. }
  1656. static void update_noise_reduction(MPVMainEncContext *const m)
  1657. {
  1658. MPVEncContext *const s = &m->s;
  1659. int intra, i;
  1660. for (intra = 0; intra < 2; intra++) {
  1661. if (s->dct_count[intra] > (1 << 16)) {
  1662. for (i = 0; i < 64; i++) {
  1663. s->dct_error_sum[intra][i] >>= 1;
  1664. }
  1665. s->dct_count[intra] >>= 1;
  1666. }
  1667. for (i = 0; i < 64; i++) {
  1668. s->dct_offset[intra][i] = (m->noise_reduction *
  1669. s->dct_count[intra] +
  1670. s->dct_error_sum[intra][i] / 2) /
  1671. (s->dct_error_sum[intra][i] + 1);
  1672. }
  1673. }
  1674. }
  1675. static void frame_start(MPVMainEncContext *const m)
  1676. {
  1677. MPVEncContext *const s = &m->s;
  1678. s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
  1679. if (s->c.pict_type != AV_PICTURE_TYPE_B) {
  1680. ff_mpv_replace_picture(&s->c.last_pic, &s->c.next_pic);
  1681. ff_mpv_replace_picture(&s->c.next_pic, &s->c.cur_pic);
  1682. }
  1683. av_assert2(!!m->noise_reduction == !!s->dct_error_sum);
  1684. if (s->dct_error_sum) {
  1685. update_noise_reduction(m);
  1686. }
  1687. }
  1688. int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  1689. const AVFrame *pic_arg, int *got_packet)
  1690. {
  1691. MPVMainEncContext *const m = avctx->priv_data;
  1692. MPVEncContext *const s = &m->s;
  1693. int stuffing_count, ret;
  1694. int context_count = s->c.slice_context_count;
  1695. ff_mpv_unref_picture(&s->c.cur_pic);
  1696. m->vbv_ignore_qmax = 0;
  1697. m->picture_in_gop_number++;
  1698. ret = load_input_picture(m, pic_arg);
  1699. if (ret < 0)
  1700. return ret;
  1701. ret = select_input_picture(m);
  1702. if (ret < 0)
  1703. return ret;
  1704. /* output? */
  1705. if (s->new_pic->data[0]) {
  1706. int growing_buffer = context_count == 1 && !s->data_partitioning;
  1707. size_t pkt_size = 10000 + s->c.mb_width * s->c.mb_height *
  1708. (growing_buffer ? 64 : (MAX_MB_BYTES + 100));
  1709. if (CONFIG_MJPEG_ENCODER && avctx->codec_id == AV_CODEC_ID_MJPEG) {
  1710. ret = ff_mjpeg_add_icc_profile_size(avctx, s->new_pic, &pkt_size);
  1711. if (ret < 0)
  1712. return ret;
  1713. }
  1714. if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
  1715. return ret;
  1716. pkt->size = avctx->internal->byte_buffer_size - AV_INPUT_BUFFER_PADDING_SIZE;
  1717. if (s->mb_info) {
  1718. s->mb_info_ptr = av_packet_new_side_data(pkt,
  1719. AV_PKT_DATA_H263_MB_INFO,
  1720. s->c.mb_width*s->c.mb_height*12);
  1721. if (!s->mb_info_ptr)
  1722. return AVERROR(ENOMEM);
  1723. s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
  1724. }
  1725. s->c.pict_type = s->new_pic->pict_type;
  1726. //emms_c();
  1727. frame_start(m);
  1728. vbv_retry:
  1729. ret = encode_picture(m, pkt);
  1730. if (growing_buffer) {
  1731. av_assert0(s->pb.buf == avctx->internal->byte_buffer);
  1732. pkt->data = s->pb.buf;
  1733. pkt->size = avctx->internal->byte_buffer_size;
  1734. }
  1735. if (ret < 0)
  1736. return -1;
  1737. frame_end(m);
  1738. if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->c.out_format == FMT_MJPEG)
  1739. ff_mjpeg_encode_picture_trailer(&s->pb, m->header_bits);
  1740. if (avctx->rc_buffer_size) {
  1741. RateControlContext *rcc = &m->rc_context;
  1742. int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
  1743. int hq = (avctx->mb_decision == FF_MB_DECISION_RD || avctx->trellis);
  1744. int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
  1745. if (put_bits_count(&s->pb) > max_size &&
  1746. s->lambda < m->lmax) {
  1747. m->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
  1748. (s->c.qscale + 1) / s->c.qscale);
  1749. if (s->adaptive_quant) {
  1750. for (int i = 0; i < s->c.mb_height * s->c.mb_stride; i++)
  1751. s->lambda_table[i] =
  1752. FFMAX(s->lambda_table[i] + min_step,
  1753. s->lambda_table[i] * (s->c.qscale + 1) /
  1754. s->c.qscale);
  1755. }
  1756. s->c.mb_skipped = 0; // done in frame_start()
  1757. // done in encode_picture() so we must undo it
  1758. if (s->c.pict_type == AV_PICTURE_TYPE_P) {
  1759. s->c.no_rounding ^= s->flipflop_rounding;
  1760. }
  1761. if (s->c.pict_type != AV_PICTURE_TYPE_B) {
  1762. s->c.time_base = s->c.last_time_base;
  1763. s->c.last_non_b_time = s->c.time - s->c.pp_time;
  1764. }
  1765. m->vbv_ignore_qmax = 1;
  1766. av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
  1767. goto vbv_retry;
  1768. }
  1769. av_assert0(avctx->rc_max_rate);
  1770. }
  1771. if (avctx->flags & AV_CODEC_FLAG_PASS1)
  1772. ff_write_pass1_stats(m);
  1773. for (int i = 0; i < MPV_MAX_PLANES; i++)
  1774. avctx->error[i] += s->encoding_error[i];
  1775. ff_side_data_set_encoder_stats(pkt, s->c.cur_pic.ptr->f->quality,
  1776. s->encoding_error,
  1777. (avctx->flags&AV_CODEC_FLAG_PSNR) ? MPV_MAX_PLANES : 0,
  1778. s->c.pict_type);
  1779. if (avctx->flags & AV_CODEC_FLAG_PASS1)
  1780. assert(put_bits_count(&s->pb) == m->header_bits + s->mv_bits +
  1781. s->misc_bits + s->i_tex_bits +
  1782. s->p_tex_bits);
  1783. flush_put_bits(&s->pb);
  1784. m->frame_bits = put_bits_count(&s->pb);
  1785. stuffing_count = ff_vbv_update(m, m->frame_bits);
  1786. m->stuffing_bits = 8*stuffing_count;
  1787. if (stuffing_count) {
  1788. if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
  1789. av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
  1790. return -1;
  1791. }
  1792. switch (s->c.codec_id) {
  1793. case AV_CODEC_ID_MPEG1VIDEO:
  1794. case AV_CODEC_ID_MPEG2VIDEO:
  1795. while (stuffing_count--) {
  1796. put_bits(&s->pb, 8, 0);
  1797. }
  1798. break;
  1799. case AV_CODEC_ID_MPEG4:
  1800. put_bits(&s->pb, 16, 0);
  1801. put_bits(&s->pb, 16, 0x1C3);
  1802. stuffing_count -= 4;
  1803. while (stuffing_count--) {
  1804. put_bits(&s->pb, 8, 0xFF);
  1805. }
  1806. break;
  1807. default:
  1808. av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1809. m->stuffing_bits = 0;
  1810. }
  1811. flush_put_bits(&s->pb);
  1812. m->frame_bits = put_bits_count(&s->pb);
  1813. }
  1814. /* update MPEG-1/2 vbv_delay for CBR */
  1815. if (avctx->rc_max_rate &&
  1816. avctx->rc_min_rate == avctx->rc_max_rate &&
  1817. s->c.out_format == FMT_MPEG1 &&
  1818. 90000LL * (avctx->rc_buffer_size - 1) <=
  1819. avctx->rc_max_rate * 0xFFFFLL) {
  1820. AVCPBProperties *props;
  1821. size_t props_size;
  1822. int vbv_delay, min_delay;
  1823. double inbits = avctx->rc_max_rate *
  1824. av_q2d(avctx->time_base);
  1825. int minbits = m->frame_bits - 8 *
  1826. (m->vbv_delay_pos - 1);
  1827. double bits = m->rc_context.buffer_index + minbits - inbits;
  1828. uint8_t *const vbv_delay_ptr = s->pb.buf + m->vbv_delay_pos;
  1829. if (bits < 0)
  1830. av_log(avctx, AV_LOG_ERROR,
  1831. "Internal error, negative bits\n");
  1832. av_assert1(s->c.repeat_first_field == 0);
  1833. vbv_delay = bits * 90000 / avctx->rc_max_rate;
  1834. min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
  1835. avctx->rc_max_rate;
  1836. vbv_delay = FFMAX(vbv_delay, min_delay);
  1837. av_assert0(vbv_delay < 0xFFFF);
  1838. vbv_delay_ptr[0] &= 0xF8;
  1839. vbv_delay_ptr[0] |= vbv_delay >> 13;
  1840. vbv_delay_ptr[1] = vbv_delay >> 5;
  1841. vbv_delay_ptr[2] &= 0x07;
  1842. vbv_delay_ptr[2] |= vbv_delay << 3;
  1843. props = av_cpb_properties_alloc(&props_size);
  1844. if (!props)
  1845. return AVERROR(ENOMEM);
  1846. props->vbv_delay = vbv_delay * 300;
  1847. ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
  1848. (uint8_t*)props, props_size);
  1849. if (ret < 0) {
  1850. av_freep(&props);
  1851. return ret;
  1852. }
  1853. }
  1854. m->total_bits += m->frame_bits;
  1855. pkt->pts = s->c.cur_pic.ptr->f->pts;
  1856. pkt->duration = s->c.cur_pic.ptr->f->duration;
  1857. if (!s->c.low_delay && s->c.pict_type != AV_PICTURE_TYPE_B) {
  1858. if (!s->c.cur_pic.ptr->coded_picture_number)
  1859. pkt->dts = pkt->pts - m->dts_delta;
  1860. else
  1861. pkt->dts = m->reordered_pts;
  1862. m->reordered_pts = pkt->pts;
  1863. } else
  1864. pkt->dts = pkt->pts;
  1865. // the no-delay case is handled in generic code
  1866. if (avctx->codec->capabilities & AV_CODEC_CAP_DELAY) {
  1867. ret = ff_encode_reordered_opaque(avctx, pkt, s->c.cur_pic.ptr->f);
  1868. if (ret < 0)
  1869. return ret;
  1870. }
  1871. if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
  1872. pkt->flags |= AV_PKT_FLAG_KEY;
  1873. if (s->mb_info)
  1874. av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
  1875. } else {
  1876. m->frame_bits = 0;
  1877. }
  1878. ff_mpv_unref_picture(&s->c.cur_pic);
  1879. av_assert1((m->frame_bits & 7) == 0);
  1880. pkt->size = m->frame_bits / 8;
  1881. *got_packet = !!pkt->size;
  1882. return 0;
  1883. }
  1884. static inline void dct_single_coeff_elimination(MPVEncContext *const s,
  1885. int n, int threshold)
  1886. {
  1887. static const char tab[64] = {
  1888. 3, 2, 2, 1, 1, 1, 1, 1,
  1889. 1, 1, 1, 1, 1, 1, 1, 1,
  1890. 1, 1, 1, 1, 1, 1, 1, 1,
  1891. 0, 0, 0, 0, 0, 0, 0, 0,
  1892. 0, 0, 0, 0, 0, 0, 0, 0,
  1893. 0, 0, 0, 0, 0, 0, 0, 0,
  1894. 0, 0, 0, 0, 0, 0, 0, 0,
  1895. 0, 0, 0, 0, 0, 0, 0, 0
  1896. };
  1897. int score = 0;
  1898. int run = 0;
  1899. int i;
  1900. int16_t *block = s->block[n];
  1901. const int last_index = s->c.block_last_index[n];
  1902. int skip_dc;
  1903. if (threshold < 0) {
  1904. skip_dc = 0;
  1905. threshold = -threshold;
  1906. } else
  1907. skip_dc = 1;
  1908. /* Are all we could set to zero already zero? */
  1909. if (last_index <= skip_dc - 1)
  1910. return;
  1911. for (i = 0; i <= last_index; i++) {
  1912. const int j = s->c.intra_scantable.permutated[i];
  1913. const int level = FFABS(block[j]);
  1914. if (level == 1) {
  1915. if (skip_dc && i == 0)
  1916. continue;
  1917. score += tab[run];
  1918. run = 0;
  1919. } else if (level > 1) {
  1920. return;
  1921. } else {
  1922. run++;
  1923. }
  1924. }
  1925. if (score >= threshold)
  1926. return;
  1927. for (i = skip_dc; i <= last_index; i++) {
  1928. const int j = s->c.intra_scantable.permutated[i];
  1929. block[j] = 0;
  1930. }
  1931. if (block[0])
  1932. s->c.block_last_index[n] = 0;
  1933. else
  1934. s->c.block_last_index[n] = -1;
  1935. }
  1936. static inline void clip_coeffs(const MPVEncContext *const s, int16_t block[],
  1937. int last_index)
  1938. {
  1939. int i;
  1940. const int maxlevel = s->max_qcoeff;
  1941. const int minlevel = s->min_qcoeff;
  1942. int overflow = 0;
  1943. if (s->c.mb_intra) {
  1944. i = 1; // skip clipping of intra dc
  1945. } else
  1946. i = 0;
  1947. for (; i <= last_index; i++) {
  1948. const int j = s->c.intra_scantable.permutated[i];
  1949. int level = block[j];
  1950. if (level > maxlevel) {
  1951. level = maxlevel;
  1952. overflow++;
  1953. } else if (level < minlevel) {
  1954. level = minlevel;
  1955. overflow++;
  1956. }
  1957. block[j] = level;
  1958. }
  1959. if (overflow && s->c.avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  1960. av_log(s->c.avctx, AV_LOG_INFO,
  1961. "warning, clipping %d dct coefficients to %d..%d\n",
  1962. overflow, minlevel, maxlevel);
  1963. }
  1964. static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride)
  1965. {
  1966. int x, y;
  1967. // FIXME optimize
  1968. for (y = 0; y < 8; y++) {
  1969. for (x = 0; x < 8; x++) {
  1970. int x2, y2;
  1971. int sum = 0;
  1972. int sqr = 0;
  1973. int count = 0;
  1974. for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
  1975. for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
  1976. int v = ptr[x2 + y2 * stride];
  1977. sum += v;
  1978. sqr += v * v;
  1979. count++;
  1980. }
  1981. }
  1982. weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
  1983. }
  1984. }
  1985. }
  1986. static av_always_inline void encode_mb_internal(MPVEncContext *const s,
  1987. int motion_x, int motion_y,
  1988. int mb_block_height,
  1989. int mb_block_width,
  1990. int mb_block_count,
  1991. int chroma_x_shift,
  1992. int chroma_y_shift,
  1993. int chroma_format)
  1994. {
  1995. /* Interlaced DCT is only possible with MPEG-2 and MPEG-4
  1996. * and neither of these encoders currently supports 444. */
  1997. #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
  1998. (s)->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
  1999. int16_t weight[12][64];
  2000. int16_t orig[12][64];
  2001. const int mb_x = s->c.mb_x;
  2002. const int mb_y = s->c.mb_y;
  2003. int i;
  2004. int skip_dct[12];
  2005. int dct_offset = s->c.linesize * 8; // default for progressive frames
  2006. int uv_dct_offset = s->c.uvlinesize * 8;
  2007. const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2008. ptrdiff_t wrap_y, wrap_c;
  2009. for (i = 0; i < mb_block_count; i++)
  2010. skip_dct[i] = s->skipdct;
  2011. if (s->adaptive_quant) {
  2012. const int last_qp = s->c.qscale;
  2013. const int mb_xy = mb_x + mb_y * s->c.mb_stride;
  2014. s->lambda = s->lambda_table[mb_xy];
  2015. s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
  2016. FF_LAMBDA_SHIFT;
  2017. if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
  2018. s->dquant = s->c.cur_pic.qscale_table[mb_xy] - last_qp;
  2019. if (s->c.out_format == FMT_H263) {
  2020. s->dquant = av_clip(s->dquant, -2, 2);
  2021. if (s->c.codec_id == AV_CODEC_ID_MPEG4) {
  2022. if (!s->c.mb_intra) {
  2023. if (s->c.pict_type == AV_PICTURE_TYPE_B) {
  2024. if (s->dquant & 1 || s->c.mv_dir & MV_DIRECT)
  2025. s->dquant = 0;
  2026. }
  2027. if (s->c.mv_type == MV_TYPE_8X8)
  2028. s->dquant = 0;
  2029. }
  2030. }
  2031. }
  2032. }
  2033. ff_set_qscale(&s->c, last_qp + s->dquant);
  2034. } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
  2035. ff_set_qscale(&s->c, s->c.qscale + s->dquant);
  2036. wrap_y = s->c.linesize;
  2037. wrap_c = s->c.uvlinesize;
  2038. ptr_y = s->new_pic->data[0] +
  2039. (mb_y * 16 * wrap_y) + mb_x * 16;
  2040. ptr_cb = s->new_pic->data[1] +
  2041. (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
  2042. ptr_cr = s->new_pic->data[2] +
  2043. (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
  2044. if ((mb_x * 16 + 16 > s->c.width || mb_y * 16 + 16 > s->c.height) &&
  2045. s->c.codec_id != AV_CODEC_ID_AMV) {
  2046. uint8_t *ebuf = s->c.sc.edge_emu_buffer + 38 * wrap_y;
  2047. int cw = (s->c.width + chroma_x_shift) >> chroma_x_shift;
  2048. int ch = (s->c.height + chroma_y_shift) >> chroma_y_shift;
  2049. s->c.vdsp.emulated_edge_mc(ebuf, ptr_y,
  2050. wrap_y, wrap_y,
  2051. 16, 16, mb_x * 16, mb_y * 16,
  2052. s->c.width, s->c.height);
  2053. ptr_y = ebuf;
  2054. s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
  2055. wrap_c, wrap_c,
  2056. mb_block_width, mb_block_height,
  2057. mb_x * mb_block_width, mb_y * mb_block_height,
  2058. cw, ch);
  2059. ptr_cb = ebuf + 16 * wrap_y;
  2060. s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
  2061. wrap_c, wrap_c,
  2062. mb_block_width, mb_block_height,
  2063. mb_x * mb_block_width, mb_y * mb_block_height,
  2064. cw, ch);
  2065. ptr_cr = ebuf + 16 * wrap_y + 16;
  2066. }
  2067. if (s->c.mb_intra) {
  2068. if (INTERLACED_DCT(s)) {
  2069. int progressive_score, interlaced_score;
  2070. s->c.interlaced_dct = 0;
  2071. progressive_score = s->ildct_cmp[1](s, ptr_y, NULL, wrap_y, 8) +
  2072. s->ildct_cmp[1](s, ptr_y + wrap_y * 8,
  2073. NULL, wrap_y, 8) - 400;
  2074. if (progressive_score > 0) {
  2075. interlaced_score = s->ildct_cmp[1](s, ptr_y,
  2076. NULL, wrap_y * 2, 8) +
  2077. s->ildct_cmp[1](s, ptr_y + wrap_y,
  2078. NULL, wrap_y * 2, 8);
  2079. if (progressive_score > interlaced_score) {
  2080. s->c.interlaced_dct = 1;
  2081. dct_offset = wrap_y;
  2082. uv_dct_offset = wrap_c;
  2083. wrap_y <<= 1;
  2084. if (chroma_format == CHROMA_422 ||
  2085. chroma_format == CHROMA_444)
  2086. wrap_c <<= 1;
  2087. }
  2088. }
  2089. }
  2090. s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
  2091. s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  2092. s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
  2093. s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  2094. if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
  2095. skip_dct[4] = 1;
  2096. skip_dct[5] = 1;
  2097. } else {
  2098. s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  2099. s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  2100. if (chroma_format == CHROMA_422) {
  2101. s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
  2102. s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
  2103. } else if (chroma_format == CHROMA_444) {
  2104. s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
  2105. s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
  2106. s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
  2107. s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
  2108. s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
  2109. s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
  2110. }
  2111. }
  2112. } else {
  2113. op_pixels_func (*op_pix)[4];
  2114. qpel_mc_func (*op_qpix)[16];
  2115. uint8_t *dest_y, *dest_cb, *dest_cr;
  2116. dest_y = s->c.dest[0];
  2117. dest_cb = s->c.dest[1];
  2118. dest_cr = s->c.dest[2];
  2119. if ((!s->c.no_rounding) || s->c.pict_type == AV_PICTURE_TYPE_B) {
  2120. op_pix = s->c.hdsp.put_pixels_tab;
  2121. op_qpix = s->c.qdsp.put_qpel_pixels_tab;
  2122. } else {
  2123. op_pix = s->c.hdsp.put_no_rnd_pixels_tab;
  2124. op_qpix = s->c.qdsp.put_no_rnd_qpel_pixels_tab;
  2125. }
  2126. if (s->c.mv_dir & MV_DIR_FORWARD) {
  2127. ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 0,
  2128. s->c.last_pic.data,
  2129. op_pix, op_qpix);
  2130. op_pix = s->c.hdsp.avg_pixels_tab;
  2131. op_qpix = s->c.qdsp.avg_qpel_pixels_tab;
  2132. }
  2133. if (s->c.mv_dir & MV_DIR_BACKWARD) {
  2134. ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 1,
  2135. s->c.next_pic.data,
  2136. op_pix, op_qpix);
  2137. }
  2138. if (INTERLACED_DCT(s)) {
  2139. int progressive_score, interlaced_score;
  2140. s->c.interlaced_dct = 0;
  2141. progressive_score = s->ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
  2142. s->ildct_cmp[0](s, dest_y + wrap_y * 8,
  2143. ptr_y + wrap_y * 8,
  2144. wrap_y, 8) - 400;
  2145. if (s->c.avctx->ildct_cmp == FF_CMP_VSSE)
  2146. progressive_score -= 400;
  2147. if (progressive_score > 0) {
  2148. interlaced_score = s->ildct_cmp[0](s, dest_y, ptr_y,
  2149. wrap_y * 2, 8) +
  2150. s->ildct_cmp[0](s, dest_y + wrap_y,
  2151. ptr_y + wrap_y,
  2152. wrap_y * 2, 8);
  2153. if (progressive_score > interlaced_score) {
  2154. s->c.interlaced_dct = 1;
  2155. dct_offset = wrap_y;
  2156. uv_dct_offset = wrap_c;
  2157. wrap_y <<= 1;
  2158. if (chroma_format == CHROMA_422)
  2159. wrap_c <<= 1;
  2160. }
  2161. }
  2162. }
  2163. s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
  2164. s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2165. s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
  2166. dest_y + dct_offset, wrap_y);
  2167. s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
  2168. dest_y + dct_offset + 8, wrap_y);
  2169. if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
  2170. skip_dct[4] = 1;
  2171. skip_dct[5] = 1;
  2172. } else {
  2173. s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2174. s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2175. if (!chroma_y_shift) { /* 422 */
  2176. s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
  2177. dest_cb + uv_dct_offset, wrap_c);
  2178. s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
  2179. dest_cr + uv_dct_offset, wrap_c);
  2180. }
  2181. }
  2182. /* pre quantization */
  2183. if (s->mc_mb_var[s->c.mb_stride * mb_y + mb_x] < 2 * s->c.qscale * s->c.qscale) {
  2184. // FIXME optimize
  2185. if (s->sad_cmp[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->c.qscale)
  2186. skip_dct[0] = 1;
  2187. if (s->sad_cmp[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->c.qscale)
  2188. skip_dct[1] = 1;
  2189. if (s->sad_cmp[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
  2190. wrap_y, 8) < 20 * s->c.qscale)
  2191. skip_dct[2] = 1;
  2192. if (s->sad_cmp[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
  2193. wrap_y, 8) < 20 * s->c.qscale)
  2194. skip_dct[3] = 1;
  2195. if (s->sad_cmp[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->c.qscale)
  2196. skip_dct[4] = 1;
  2197. if (s->sad_cmp[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->c.qscale)
  2198. skip_dct[5] = 1;
  2199. if (!chroma_y_shift) { /* 422 */
  2200. if (s->sad_cmp[1](NULL, ptr_cb + uv_dct_offset,
  2201. dest_cb + uv_dct_offset,
  2202. wrap_c, 8) < 20 * s->c.qscale)
  2203. skip_dct[6] = 1;
  2204. if (s->sad_cmp[1](NULL, ptr_cr + uv_dct_offset,
  2205. dest_cr + uv_dct_offset,
  2206. wrap_c, 8) < 20 * s->c.qscale)
  2207. skip_dct[7] = 1;
  2208. }
  2209. }
  2210. }
  2211. if (s->quantizer_noise_shaping) {
  2212. if (!skip_dct[0])
  2213. get_visual_weight(weight[0], ptr_y , wrap_y);
  2214. if (!skip_dct[1])
  2215. get_visual_weight(weight[1], ptr_y + 8, wrap_y);
  2216. if (!skip_dct[2])
  2217. get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  2218. if (!skip_dct[3])
  2219. get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  2220. if (!skip_dct[4])
  2221. get_visual_weight(weight[4], ptr_cb , wrap_c);
  2222. if (!skip_dct[5])
  2223. get_visual_weight(weight[5], ptr_cr , wrap_c);
  2224. if (!chroma_y_shift) { /* 422 */
  2225. if (!skip_dct[6])
  2226. get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
  2227. wrap_c);
  2228. if (!skip_dct[7])
  2229. get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
  2230. wrap_c);
  2231. }
  2232. memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
  2233. }
  2234. /* DCT & quantize */
  2235. av_assert2(s->c.out_format != FMT_MJPEG || s->c.qscale == 8);
  2236. {
  2237. for (i = 0; i < mb_block_count; i++) {
  2238. if (!skip_dct[i]) {
  2239. int overflow;
  2240. s->c.block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->c.qscale, &overflow);
  2241. // FIXME we could decide to change to quantizer instead of
  2242. // clipping
  2243. // JS: I don't think that would be a good idea it could lower
  2244. // quality instead of improve it. Just INTRADC clipping
  2245. // deserves changes in quantizer
  2246. if (overflow)
  2247. clip_coeffs(s, s->block[i], s->c.block_last_index[i]);
  2248. } else
  2249. s->c.block_last_index[i] = -1;
  2250. }
  2251. if (s->quantizer_noise_shaping) {
  2252. for (i = 0; i < mb_block_count; i++) {
  2253. if (!skip_dct[i]) {
  2254. s->c.block_last_index[i] =
  2255. dct_quantize_refine(s, s->block[i], weight[i],
  2256. orig[i], i, s->c.qscale);
  2257. }
  2258. }
  2259. }
  2260. if (s->luma_elim_threshold && !s->c.mb_intra)
  2261. for (i = 0; i < 4; i++)
  2262. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2263. if (s->chroma_elim_threshold && !s->c.mb_intra)
  2264. for (i = 4; i < mb_block_count; i++)
  2265. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2266. if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
  2267. for (i = 0; i < mb_block_count; i++) {
  2268. if (s->c.block_last_index[i] == -1)
  2269. s->coded_score[i] = INT_MAX / 256;
  2270. }
  2271. }
  2272. }
  2273. if ((s->c.avctx->flags & AV_CODEC_FLAG_GRAY) && s->c.mb_intra) {
  2274. s->c.block_last_index[4] =
  2275. s->c.block_last_index[5] = 0;
  2276. s->block[4][0] =
  2277. s->block[5][0] = (1024 + s->c.c_dc_scale / 2) / s->c.c_dc_scale;
  2278. if (!chroma_y_shift) { /* 422 / 444 */
  2279. for (i=6; i<12; i++) {
  2280. s->c.block_last_index[i] = 0;
  2281. s->block[i][0] = s->block[4][0];
  2282. }
  2283. }
  2284. }
  2285. // non c quantize code returns incorrect block_last_index FIXME
  2286. if (s->c.alternate_scan && s->dct_quantize != dct_quantize_c) {
  2287. for (i = 0; i < mb_block_count; i++) {
  2288. int j;
  2289. if (s->c.block_last_index[i] > 0) {
  2290. for (j = 63; j > 0; j--) {
  2291. if (s->block[i][s->c.intra_scantable.permutated[j]])
  2292. break;
  2293. }
  2294. s->c.block_last_index[i] = j;
  2295. }
  2296. }
  2297. }
  2298. s->encode_mb(s, s->block, motion_x, motion_y);
  2299. }
  2300. static void encode_mb(MPVEncContext *const s, int motion_x, int motion_y)
  2301. {
  2302. if (s->c.chroma_format == CHROMA_420)
  2303. encode_mb_internal(s, motion_x, motion_y, 8, 8, 6, 1, 1, CHROMA_420);
  2304. else if (s->c.chroma_format == CHROMA_422)
  2305. encode_mb_internal(s, motion_x, motion_y, 16, 8, 8, 1, 0, CHROMA_422);
  2306. else
  2307. encode_mb_internal(s, motion_x, motion_y, 16, 16, 12, 0, 0, CHROMA_444);
  2308. }
  2309. typedef struct MBBackup {
  2310. struct {
  2311. int mv[2][4][2];
  2312. int last_mv[2][2][2];
  2313. int mv_type, mv_dir;
  2314. int last_dc[3];
  2315. int mb_intra, mb_skipped;
  2316. int qscale;
  2317. int block_last_index[8];
  2318. int interlaced_dct;
  2319. } c;
  2320. int mb_skip_run;
  2321. int mv_bits, i_tex_bits, p_tex_bits, i_count, misc_bits, last_bits;
  2322. int dquant;
  2323. int esc3_level_length;
  2324. int16_t (*block)[64];
  2325. PutBitContext pb, pb2, tex_pb;
  2326. } MBBackup;
  2327. #define COPY_CONTEXT(BEFORE, AFTER, DST_TYPE, SRC_TYPE) \
  2328. static inline void BEFORE ##_context_before_encode(DST_TYPE *const d, \
  2329. const SRC_TYPE *const s) \
  2330. { \
  2331. /* FIXME is memcpy faster than a loop? */ \
  2332. memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
  2333. \
  2334. /* MPEG-1 */ \
  2335. d->mb_skip_run = s->mb_skip_run; \
  2336. for (int i = 0; i < 3; i++) \
  2337. d->c.last_dc[i] = s->c.last_dc[i]; \
  2338. \
  2339. /* statistics */ \
  2340. d->mv_bits = s->mv_bits; \
  2341. d->i_tex_bits = s->i_tex_bits; \
  2342. d->p_tex_bits = s->p_tex_bits; \
  2343. d->i_count = s->i_count; \
  2344. d->misc_bits = s->misc_bits; \
  2345. d->last_bits = 0; \
  2346. \
  2347. d->c.mb_skipped = 0; \
  2348. d->c.qscale = s->c.qscale; \
  2349. d->dquant = s->dquant; \
  2350. \
  2351. d->esc3_level_length = s->esc3_level_length; \
  2352. } \
  2353. \
  2354. static inline void AFTER ## _context_after_encode(DST_TYPE *const d, \
  2355. const SRC_TYPE *const s, \
  2356. int data_partitioning) \
  2357. { \
  2358. /* FIXME is memcpy faster than a loop? */ \
  2359. memcpy(d->c.mv, s->c.mv, 2*4*2*sizeof(int)); \
  2360. memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
  2361. \
  2362. /* MPEG-1 */ \
  2363. d->mb_skip_run = s->mb_skip_run; \
  2364. for (int i = 0; i < 3; i++) \
  2365. d->c.last_dc[i] = s->c.last_dc[i]; \
  2366. \
  2367. /* statistics */ \
  2368. d->mv_bits = s->mv_bits; \
  2369. d->i_tex_bits = s->i_tex_bits; \
  2370. d->p_tex_bits = s->p_tex_bits; \
  2371. d->i_count = s->i_count; \
  2372. d->misc_bits = s->misc_bits; \
  2373. \
  2374. d->c.mb_intra = s->c.mb_intra; \
  2375. d->c.mb_skipped = s->c.mb_skipped; \
  2376. d->c.mv_type = s->c.mv_type; \
  2377. d->c.mv_dir = s->c.mv_dir; \
  2378. d->pb = s->pb; \
  2379. if (data_partitioning) { \
  2380. d->pb2 = s->pb2; \
  2381. d->tex_pb = s->tex_pb; \
  2382. } \
  2383. d->block = s->block; \
  2384. for (int i = 0; i < 8; i++) \
  2385. d->c.block_last_index[i] = s->c.block_last_index[i]; \
  2386. d->c.interlaced_dct = s->c.interlaced_dct; \
  2387. d->c.qscale = s->c.qscale; \
  2388. \
  2389. d->esc3_level_length = s->esc3_level_length; \
  2390. }
  2391. COPY_CONTEXT(backup, save, MBBackup, MPVEncContext)
  2392. COPY_CONTEXT(reset, store, MPVEncContext, MBBackup)
  2393. static void encode_mb_hq(MPVEncContext *const s, MBBackup *const backup, MBBackup *const best,
  2394. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2395. int *dmin, int *next_block, int motion_x, int motion_y)
  2396. {
  2397. int score;
  2398. uint8_t *dest_backup[3];
  2399. reset_context_before_encode(s, backup);
  2400. s->block = s->blocks[*next_block];
  2401. s->pb = pb[*next_block];
  2402. if (s->data_partitioning) {
  2403. s->pb2 = pb2 [*next_block];
  2404. s->tex_pb= tex_pb[*next_block];
  2405. }
  2406. if(*next_block){
  2407. memcpy(dest_backup, s->c.dest, sizeof(s->c.dest));
  2408. s->c.dest[0] = s->c.sc.rd_scratchpad;
  2409. s->c.dest[1] = s->c.sc.rd_scratchpad + 16*s->c.linesize;
  2410. s->c.dest[2] = s->c.sc.rd_scratchpad + 16*s->c.linesize + 8;
  2411. av_assert0(s->c.linesize >= 32); //FIXME
  2412. }
  2413. encode_mb(s, motion_x, motion_y);
  2414. score= put_bits_count(&s->pb);
  2415. if (s->data_partitioning) {
  2416. score+= put_bits_count(&s->pb2);
  2417. score+= put_bits_count(&s->tex_pb);
  2418. }
  2419. if (s->c.avctx->mb_decision == FF_MB_DECISION_RD) {
  2420. mpv_reconstruct_mb(s, s->block);
  2421. score *= s->lambda2;
  2422. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  2423. }
  2424. if(*next_block){
  2425. memcpy(s->c.dest, dest_backup, sizeof(s->c.dest));
  2426. }
  2427. if(score<*dmin){
  2428. *dmin= score;
  2429. *next_block^=1;
  2430. save_context_after_encode(best, s, s->data_partitioning);
  2431. }
  2432. }
  2433. static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
  2434. {
  2435. const uint32_t *sq = ff_square_tab + 256;
  2436. int acc=0;
  2437. int x,y;
  2438. if(w==16 && h==16)
  2439. return s->sse_cmp[0](NULL, src1, src2, stride, 16);
  2440. else if(w==8 && h==8)
  2441. return s->sse_cmp[1](NULL, src1, src2, stride, 8);
  2442. for(y=0; y<h; y++){
  2443. for(x=0; x<w; x++){
  2444. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2445. }
  2446. }
  2447. av_assert2(acc>=0);
  2448. return acc;
  2449. }
  2450. static int sse_mb(MPVEncContext *const s)
  2451. {
  2452. int w= 16;
  2453. int h= 16;
  2454. int chroma_mb_w = w >> s->c.chroma_x_shift;
  2455. int chroma_mb_h = h >> s->c.chroma_y_shift;
  2456. if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
  2457. if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
  2458. if(w==16 && h==16)
  2459. return s->n_sse_cmp[0](s, s->new_pic->data[0] + s->c.mb_x * 16 + s->c.mb_y * s->c.linesize * 16,
  2460. s->c.dest[0], s->c.linesize, 16) +
  2461. s->n_sse_cmp[1](s, s->new_pic->data[1] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
  2462. s->c.dest[1], s->c.uvlinesize, chroma_mb_h) +
  2463. s->n_sse_cmp[1](s, s->new_pic->data[2] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
  2464. s->c.dest[2], s->c.uvlinesize, chroma_mb_h);
  2465. else
  2466. return sse(s, s->new_pic->data[0] + s->c.mb_x * 16 + s->c.mb_y * s->c.linesize * 16,
  2467. s->c.dest[0], w, h, s->c.linesize) +
  2468. sse(s, s->new_pic->data[1] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
  2469. s->c.dest[1], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize) +
  2470. sse(s, s->new_pic->data[2] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
  2471. s->c.dest[2], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize);
  2472. }
  2473. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  2474. MPVEncContext *const s = *(void**)arg;
  2475. s->me.pre_pass = 1;
  2476. s->me.dia_size = s->c.avctx->pre_dia_size;
  2477. s->c.first_slice_line = 1;
  2478. for (s->c.mb_y = s->c.end_mb_y - 1; s->c.mb_y >= s->c.start_mb_y; s->c.mb_y--) {
  2479. for (s->c.mb_x = s->c.mb_width - 1; s->c.mb_x >=0 ; s->c.mb_x--)
  2480. ff_pre_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
  2481. s->c.first_slice_line = 0;
  2482. }
  2483. s->me.pre_pass = 0;
  2484. return 0;
  2485. }
  2486. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  2487. MPVEncContext *const s = *(void**)arg;
  2488. s->me.dia_size = s->c.avctx->dia_size;
  2489. s->c.first_slice_line = 1;
  2490. for (s->c.mb_y = s->c.start_mb_y; s->c.mb_y < s->c.end_mb_y; s->c.mb_y++) {
  2491. s->c.mb_x = 0; //for block init below
  2492. ff_init_block_index(&s->c);
  2493. for (s->c.mb_x = 0; s->c.mb_x < s->c.mb_width; s->c.mb_x++) {
  2494. s->c.block_index[0] += 2;
  2495. s->c.block_index[1] += 2;
  2496. s->c.block_index[2] += 2;
  2497. s->c.block_index[3] += 2;
  2498. /* compute motion vector & mb_type and store in context */
  2499. if (s->c.pict_type == AV_PICTURE_TYPE_B)
  2500. ff_estimate_b_frame_motion(s, s->c.mb_x, s->c.mb_y);
  2501. else
  2502. ff_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
  2503. }
  2504. s->c.first_slice_line = 0;
  2505. }
  2506. return 0;
  2507. }
  2508. static int mb_var_thread(AVCodecContext *c, void *arg){
  2509. MPVEncContext *const s = *(void**)arg;
  2510. for (int mb_y = s->c.start_mb_y; mb_y < s->c.end_mb_y; mb_y++) {
  2511. for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
  2512. int xx = mb_x * 16;
  2513. int yy = mb_y * 16;
  2514. const uint8_t *pix = s->new_pic->data[0] + (yy * s->c.linesize) + xx;
  2515. int varc;
  2516. int sum = s->mpvencdsp.pix_sum(pix, s->c.linesize);
  2517. varc = (s->mpvencdsp.pix_norm1(pix, s->c.linesize) -
  2518. (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
  2519. s->mb_var [s->c.mb_stride * mb_y + mb_x] = varc;
  2520. s->mb_mean[s->c.mb_stride * mb_y + mb_x] = (sum+128)>>8;
  2521. s->me.mb_var_sum_temp += varc;
  2522. }
  2523. }
  2524. return 0;
  2525. }
  2526. static void write_slice_end(MPVEncContext *const s)
  2527. {
  2528. if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
  2529. if (s->partitioned_frame)
  2530. ff_mpeg4_merge_partitions(s);
  2531. ff_mpeg4_stuffing(&s->pb);
  2532. } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
  2533. s->c.out_format == FMT_MJPEG) {
  2534. ff_mjpeg_encode_stuffing(s);
  2535. } else if (CONFIG_SPEEDHQ_ENCODER && s->c.out_format == FMT_SPEEDHQ) {
  2536. ff_speedhq_end_slice(s);
  2537. }
  2538. flush_put_bits(&s->pb);
  2539. if ((s->c.avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
  2540. s->misc_bits+= get_bits_diff(s);
  2541. }
  2542. static void write_mb_info(MPVEncContext *const s)
  2543. {
  2544. uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
  2545. int offset = put_bits_count(&s->pb);
  2546. int mba = s->c.mb_x + s->c.mb_width * (s->c.mb_y % s->gob_index);
  2547. int gobn = s->c.mb_y / s->gob_index;
  2548. int pred_x, pred_y;
  2549. if (CONFIG_H263_ENCODER)
  2550. ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
  2551. bytestream_put_le32(&ptr, offset);
  2552. bytestream_put_byte(&ptr, s->c.qscale);
  2553. bytestream_put_byte(&ptr, gobn);
  2554. bytestream_put_le16(&ptr, mba);
  2555. bytestream_put_byte(&ptr, pred_x); /* hmv1 */
  2556. bytestream_put_byte(&ptr, pred_y); /* vmv1 */
  2557. /* 4MV not implemented */
  2558. bytestream_put_byte(&ptr, 0); /* hmv2 */
  2559. bytestream_put_byte(&ptr, 0); /* vmv2 */
  2560. }
  2561. static void update_mb_info(MPVEncContext *const s, int startcode)
  2562. {
  2563. if (!s->mb_info)
  2564. return;
  2565. if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
  2566. s->mb_info_size += 12;
  2567. s->prev_mb_info = s->last_mb_info;
  2568. }
  2569. if (startcode) {
  2570. s->prev_mb_info = put_bytes_count(&s->pb, 0);
  2571. /* This might have incremented mb_info_size above, and we return without
  2572. * actually writing any info into that slot yet. But in that case,
  2573. * this will be called again at the start of the after writing the
  2574. * start code, actually writing the mb info. */
  2575. return;
  2576. }
  2577. s->last_mb_info = put_bytes_count(&s->pb, 0);
  2578. if (!s->mb_info_size)
  2579. s->mb_info_size += 12;
  2580. write_mb_info(s);
  2581. }
  2582. int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const s, size_t threshold, size_t size_increase)
  2583. {
  2584. if (put_bytes_left(&s->pb, 0) < threshold
  2585. && s->c.slice_context_count == 1
  2586. && s->pb.buf == s->c.avctx->internal->byte_buffer) {
  2587. int lastgob_pos = s->ptr_lastgob - s->pb.buf;
  2588. uint8_t *new_buffer = NULL;
  2589. int new_buffer_size = 0;
  2590. if ((s->c.avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
  2591. av_log(s->c.avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
  2592. return AVERROR(ENOMEM);
  2593. }
  2594. emms_c();
  2595. av_fast_padded_malloc(&new_buffer, &new_buffer_size,
  2596. s->c.avctx->internal->byte_buffer_size + size_increase);
  2597. if (!new_buffer)
  2598. return AVERROR(ENOMEM);
  2599. memcpy(new_buffer, s->c.avctx->internal->byte_buffer, s->c.avctx->internal->byte_buffer_size);
  2600. av_free(s->c.avctx->internal->byte_buffer);
  2601. s->c.avctx->internal->byte_buffer = new_buffer;
  2602. s->c.avctx->internal->byte_buffer_size = new_buffer_size;
  2603. rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
  2604. s->ptr_lastgob = s->pb.buf + lastgob_pos;
  2605. }
  2606. if (put_bytes_left(&s->pb, 0) < threshold)
  2607. return AVERROR(EINVAL);
  2608. return 0;
  2609. }
  2610. static int encode_thread(AVCodecContext *c, void *arg){
  2611. MPVEncContext *const s = *(void**)arg;
  2612. int chr_h = 16 >> s->c.chroma_y_shift;
  2613. int i;
  2614. MBBackup best_s = { 0 }, backup_s;
  2615. uint8_t bit_buf[2][MAX_MB_BYTES];
  2616. // + 2 because ff_copy_bits() overreads
  2617. uint8_t bit_buf2[2][MAX_PB2_MB_SIZE + 2];
  2618. uint8_t bit_buf_tex[2][MAX_AC_TEX_MB_SIZE + 2];
  2619. PutBitContext pb[2], pb2[2], tex_pb[2];
  2620. for(i=0; i<2; i++){
  2621. init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
  2622. init_put_bits(&pb2 [i], bit_buf2 [i], MAX_PB2_MB_SIZE);
  2623. init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_AC_TEX_MB_SIZE);
  2624. }
  2625. s->last_bits= put_bits_count(&s->pb);
  2626. s->mv_bits=0;
  2627. s->misc_bits=0;
  2628. s->i_tex_bits=0;
  2629. s->p_tex_bits=0;
  2630. s->i_count=0;
  2631. for(i=0; i<3; i++){
  2632. /* init last dc values */
  2633. /* note: quant matrix value (8) is implied here */
  2634. s->c.last_dc[i] = 128 << s->c.intra_dc_precision;
  2635. s->encoding_error[i] = 0;
  2636. }
  2637. if (s->c.codec_id == AV_CODEC_ID_AMV) {
  2638. s->c.last_dc[0] = 128 * 8 / 13;
  2639. s->c.last_dc[1] = 128 * 8 / 14;
  2640. s->c.last_dc[2] = 128 * 8 / 14;
  2641. #if CONFIG_MPEG4_ENCODER
  2642. } else if (s->partitioned_frame) {
  2643. av_assert1(s->c.codec_id == AV_CODEC_ID_MPEG4);
  2644. ff_mpeg4_init_partitions(s);
  2645. #endif
  2646. }
  2647. s->mb_skip_run = 0;
  2648. memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
  2649. s->last_mv_dir = 0;
  2650. s->c.resync_mb_x = 0;
  2651. s->c.resync_mb_y = 0;
  2652. s->c.first_slice_line = 1;
  2653. s->ptr_lastgob = s->pb.buf;
  2654. for (int mb_y_order = s->c.start_mb_y; mb_y_order < s->c.end_mb_y; mb_y_order++) {
  2655. int mb_y;
  2656. if (CONFIG_SPEEDHQ_ENCODER && s->c.codec_id == AV_CODEC_ID_SPEEDHQ) {
  2657. int first_in_slice;
  2658. mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->c.mb_height, &first_in_slice);
  2659. if (first_in_slice && mb_y_order != s->c.start_mb_y)
  2660. ff_speedhq_end_slice(s);
  2661. s->c.last_dc[0] = s->c.last_dc[1] = s->c.last_dc[2] = 1024 << s->c.intra_dc_precision;
  2662. } else {
  2663. mb_y = mb_y_order;
  2664. }
  2665. s->c.mb_x = 0;
  2666. s->c.mb_y = mb_y;
  2667. ff_set_qscale(&s->c, s->c.qscale);
  2668. ff_init_block_index(&s->c);
  2669. for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
  2670. int mb_type, xy;
  2671. // int d;
  2672. int dmin= INT_MAX;
  2673. int dir;
  2674. int size_increase = s->c.avctx->internal->byte_buffer_size/4
  2675. + s->c.mb_width*MAX_MB_BYTES;
  2676. ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
  2677. if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
  2678. av_log(s->c.avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2679. return -1;
  2680. }
  2681. if (s->data_partitioning) {
  2682. if (put_bytes_left(&s->pb2, 0) < MAX_MB_BYTES ||
  2683. put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
  2684. av_log(s->c.avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
  2685. return -1;
  2686. }
  2687. }
  2688. s->c.mb_x = mb_x;
  2689. s->c.mb_y = mb_y; // moved into loop, can get changed by H.261
  2690. ff_update_block_index(&s->c, 8, 0, s->c.chroma_x_shift);
  2691. if (CONFIG_H261_ENCODER && s->c.codec_id == AV_CODEC_ID_H261)
  2692. ff_h261_reorder_mb_index(s);
  2693. xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
  2694. mb_type = s->mb_type[xy];
  2695. /* write gob / video packet header */
  2696. if(s->rtp_mode){
  2697. int current_packet_size, is_gob_start;
  2698. current_packet_size = put_bytes_count(&s->pb, 1)
  2699. - (s->ptr_lastgob - s->pb.buf);
  2700. is_gob_start = s->rtp_payload_size &&
  2701. current_packet_size >= s->rtp_payload_size &&
  2702. mb_y + mb_x > 0;
  2703. if (s->c.start_mb_y == mb_y && mb_y > 0 && mb_x == 0) is_gob_start = 1;
  2704. switch (s->c.codec_id) {
  2705. case AV_CODEC_ID_H263:
  2706. case AV_CODEC_ID_H263P:
  2707. if (!s->h263_slice_structured)
  2708. if (s->c.mb_x || s->c.mb_y % s->gob_index) is_gob_start = 0;
  2709. break;
  2710. case AV_CODEC_ID_MPEG2VIDEO:
  2711. if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
  2712. case AV_CODEC_ID_MPEG1VIDEO:
  2713. if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO && s->c.mb_y >= 175 ||
  2714. s->mb_skip_run)
  2715. is_gob_start=0;
  2716. break;
  2717. case AV_CODEC_ID_MJPEG:
  2718. if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
  2719. break;
  2720. }
  2721. if(is_gob_start){
  2722. if (s->c.start_mb_y != mb_y || mb_x != 0) {
  2723. write_slice_end(s);
  2724. if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 && s->partitioned_frame)
  2725. ff_mpeg4_init_partitions(s);
  2726. }
  2727. av_assert2((put_bits_count(&s->pb)&7) == 0);
  2728. current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
  2729. if (s->error_rate && s->c.resync_mb_x + s->c.resync_mb_y > 0) {
  2730. int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->c.mb_x + s->c.mb_y;
  2731. int d = 100 / s->error_rate;
  2732. if(r % d == 0){
  2733. current_packet_size=0;
  2734. s->pb.buf_ptr= s->ptr_lastgob;
  2735. av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
  2736. }
  2737. }
  2738. switch (s->c.codec_id) {
  2739. case AV_CODEC_ID_MPEG4:
  2740. if (CONFIG_MPEG4_ENCODER) {
  2741. ff_mpeg4_encode_video_packet_header(s);
  2742. ff_mpeg4_clean_buffers(&s->c);
  2743. ff_h263_mpeg4_reset_dc(s);
  2744. }
  2745. break;
  2746. case AV_CODEC_ID_MPEG1VIDEO:
  2747. case AV_CODEC_ID_MPEG2VIDEO:
  2748. if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
  2749. ff_mpeg1_encode_slice_header(s);
  2750. ff_mpeg1_clean_buffers(&s->c);
  2751. }
  2752. break;
  2753. #if CONFIG_H263P_ENCODER
  2754. case AV_CODEC_ID_H263P:
  2755. if (s->c.dc_val)
  2756. ff_h263_mpeg4_reset_dc(s);
  2757. // fallthrough
  2758. #endif
  2759. case AV_CODEC_ID_H263:
  2760. if (CONFIG_H263_ENCODER) {
  2761. update_mb_info(s, 1);
  2762. ff_h263_encode_gob_header(s, mb_y);
  2763. }
  2764. break;
  2765. }
  2766. if (s->c.avctx->flags & AV_CODEC_FLAG_PASS1) {
  2767. int bits= put_bits_count(&s->pb);
  2768. s->misc_bits+= bits - s->last_bits;
  2769. s->last_bits= bits;
  2770. }
  2771. s->ptr_lastgob += current_packet_size;
  2772. s->c.first_slice_line = 1;
  2773. s->c.resync_mb_x = mb_x;
  2774. s->c.resync_mb_y = mb_y;
  2775. }
  2776. }
  2777. if (s->c.resync_mb_x == s->c.mb_x &&
  2778. s->c.resync_mb_y+1 == s->c.mb_y)
  2779. s->c.first_slice_line = 0;
  2780. s->c.mb_skipped = 0;
  2781. s->dquant=0; //only for QP_RD
  2782. update_mb_info(s, 0);
  2783. if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) { // more than 1 MB type possible or FF_MPV_FLAG_QP_RD
  2784. int next_block=0;
  2785. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2786. backup_context_before_encode(&backup_s, s);
  2787. backup_s.pb= s->pb;
  2788. if (s->data_partitioning) {
  2789. backup_s.pb2= s->pb2;
  2790. backup_s.tex_pb= s->tex_pb;
  2791. }
  2792. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  2793. s->c.mv_dir = MV_DIR_FORWARD;
  2794. s->c.mv_type = MV_TYPE_16X16;
  2795. s->c.mb_intra = 0;
  2796. s->c.mv[0][0][0] = s->p_mv_table[xy][0];
  2797. s->c.mv[0][0][1] = s->p_mv_table[xy][1];
  2798. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2799. &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
  2800. }
  2801. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  2802. s->c.mv_dir = MV_DIR_FORWARD;
  2803. s->c.mv_type = MV_TYPE_FIELD;
  2804. s->c.mb_intra = 0;
  2805. for(i=0; i<2; i++){
  2806. int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
  2807. s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
  2808. s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
  2809. }
  2810. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2811. &dmin, &next_block, 0, 0);
  2812. }
  2813. if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
  2814. s->c.mv_dir = MV_DIR_FORWARD;
  2815. s->c.mv_type = MV_TYPE_16X16;
  2816. s->c.mb_intra = 0;
  2817. s->c.mv[0][0][0] = 0;
  2818. s->c.mv[0][0][1] = 0;
  2819. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2820. &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
  2821. }
  2822. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  2823. s->c.mv_dir = MV_DIR_FORWARD;
  2824. s->c.mv_type = MV_TYPE_8X8;
  2825. s->c.mb_intra = 0;
  2826. for(i=0; i<4; i++){
  2827. s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
  2828. s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
  2829. }
  2830. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2831. &dmin, &next_block, 0, 0);
  2832. }
  2833. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  2834. s->c.mv_dir = MV_DIR_FORWARD;
  2835. s->c.mv_type = MV_TYPE_16X16;
  2836. s->c.mb_intra = 0;
  2837. s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2838. s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2839. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2840. &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
  2841. }
  2842. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  2843. s->c.mv_dir = MV_DIR_BACKWARD;
  2844. s->c.mv_type = MV_TYPE_16X16;
  2845. s->c.mb_intra = 0;
  2846. s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
  2847. s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
  2848. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2849. &dmin, &next_block, s->c.mv[1][0][0], s->c.mv[1][0][1]);
  2850. }
  2851. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  2852. s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2853. s->c.mv_type = MV_TYPE_16X16;
  2854. s->c.mb_intra = 0;
  2855. s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2856. s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2857. s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2858. s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2859. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2860. &dmin, &next_block, 0, 0);
  2861. }
  2862. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  2863. s->c.mv_dir = MV_DIR_FORWARD;
  2864. s->c.mv_type = MV_TYPE_FIELD;
  2865. s->c.mb_intra = 0;
  2866. for(i=0; i<2; i++){
  2867. int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
  2868. s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  2869. s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  2870. }
  2871. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2872. &dmin, &next_block, 0, 0);
  2873. }
  2874. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  2875. s->c.mv_dir = MV_DIR_BACKWARD;
  2876. s->c.mv_type = MV_TYPE_FIELD;
  2877. s->c.mb_intra = 0;
  2878. for(i=0; i<2; i++){
  2879. int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
  2880. s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  2881. s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  2882. }
  2883. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2884. &dmin, &next_block, 0, 0);
  2885. }
  2886. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  2887. s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2888. s->c.mv_type = MV_TYPE_FIELD;
  2889. s->c.mb_intra = 0;
  2890. for(dir=0; dir<2; dir++){
  2891. for(i=0; i<2; i++){
  2892. int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  2893. s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  2894. s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  2895. }
  2896. }
  2897. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2898. &dmin, &next_block, 0, 0);
  2899. }
  2900. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  2901. s->c.mv_dir = 0;
  2902. s->c.mv_type = MV_TYPE_16X16;
  2903. s->c.mb_intra = 1;
  2904. s->c.mv[0][0][0] = 0;
  2905. s->c.mv[0][0][1] = 0;
  2906. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2907. &dmin, &next_block, 0, 0);
  2908. s->c.mbintra_table[xy] = 1;
  2909. }
  2910. if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
  2911. if (best_s.c.mv_type == MV_TYPE_16X16) { //FIXME move 4mv after QPRD
  2912. const int last_qp = backup_s.c.qscale;
  2913. int qpi, qp, dc[6];
  2914. int16_t ac[6][16];
  2915. const int mvdir = (best_s.c.mv_dir & MV_DIR_BACKWARD) ? 1 : 0;
  2916. static const int dquant_tab[4]={-1,1,-2,2};
  2917. int storecoefs = s->c.mb_intra && s->c.dc_val;
  2918. av_assert2(backup_s.dquant == 0);
  2919. //FIXME intra
  2920. s->c.mv_dir = best_s.c.mv_dir;
  2921. s->c.mv_type = MV_TYPE_16X16;
  2922. s->c.mb_intra = best_s.c.mb_intra;
  2923. s->c.mv[0][0][0] = best_s.c.mv[0][0][0];
  2924. s->c.mv[0][0][1] = best_s.c.mv[0][0][1];
  2925. s->c.mv[1][0][0] = best_s.c.mv[1][0][0];
  2926. s->c.mv[1][0][1] = best_s.c.mv[1][0][1];
  2927. qpi = s->c.pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
  2928. for(; qpi<4; qpi++){
  2929. int dquant= dquant_tab[qpi];
  2930. qp= last_qp + dquant;
  2931. if (qp < s->c.avctx->qmin || qp > s->c.avctx->qmax)
  2932. continue;
  2933. backup_s.dquant= dquant;
  2934. if(storecoefs){
  2935. for(i=0; i<6; i++){
  2936. dc[i] = s->c.dc_val[s->c.block_index[i]];
  2937. memcpy(ac[i], s->c.ac_val[s->c.block_index[i]], sizeof(*s->c.ac_val));
  2938. }
  2939. }
  2940. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2941. &dmin, &next_block, s->c.mv[mvdir][0][0], s->c.mv[mvdir][0][1]);
  2942. if (best_s.c.qscale != qp) {
  2943. if(storecoefs){
  2944. for(i=0; i<6; i++){
  2945. s->c.dc_val[s->c.block_index[i]] = dc[i];
  2946. memcpy(s->c.ac_val[s->c.block_index[i]], ac[i], sizeof(*s->c.ac_val));
  2947. }
  2948. }
  2949. }
  2950. }
  2951. }
  2952. }
  2953. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
  2954. int mx= s->b_direct_mv_table[xy][0];
  2955. int my= s->b_direct_mv_table[xy][1];
  2956. backup_s.dquant = 0;
  2957. s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2958. s->c.mb_intra = 0;
  2959. ff_mpeg4_set_direct_mv(&s->c, mx, my);
  2960. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2961. &dmin, &next_block, mx, my);
  2962. }
  2963. if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
  2964. backup_s.dquant = 0;
  2965. s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2966. s->c.mb_intra = 0;
  2967. ff_mpeg4_set_direct_mv(&s->c, 0, 0);
  2968. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2969. &dmin, &next_block, 0, 0);
  2970. }
  2971. if (!best_s.c.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
  2972. int coded=0;
  2973. for(i=0; i<6; i++)
  2974. coded |= s->c.block_last_index[i];
  2975. if(coded){
  2976. int mx,my;
  2977. memcpy(s->c.mv, best_s.c.mv, sizeof(s->c.mv));
  2978. if (CONFIG_MPEG4_ENCODER && best_s.c.mv_dir & MV_DIRECT) {
  2979. mx=my=0; //FIXME find the one we actually used
  2980. ff_mpeg4_set_direct_mv(&s->c, mx, my);
  2981. } else if (best_s.c.mv_dir & MV_DIR_BACKWARD) {
  2982. mx = s->c.mv[1][0][0];
  2983. my = s->c.mv[1][0][1];
  2984. }else{
  2985. mx = s->c.mv[0][0][0];
  2986. my = s->c.mv[0][0][1];
  2987. }
  2988. s->c.mv_dir = best_s.c.mv_dir;
  2989. s->c.mv_type = best_s.c.mv_type;
  2990. s->c.mb_intra = 0;
  2991. /* s->c.mv[0][0][0] = best_s.mv[0][0][0];
  2992. s->c.mv[0][0][1] = best_s.mv[0][0][1];
  2993. s->c.mv[1][0][0] = best_s.mv[1][0][0];
  2994. s->c.mv[1][0][1] = best_s.mv[1][0][1];*/
  2995. backup_s.dquant= 0;
  2996. s->skipdct=1;
  2997. encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
  2998. &dmin, &next_block, mx, my);
  2999. s->skipdct=0;
  3000. }
  3001. }
  3002. store_context_after_encode(s, &best_s, s->data_partitioning);
  3003. pb_bits_count= put_bits_count(&s->pb);
  3004. flush_put_bits(&s->pb);
  3005. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  3006. s->pb= backup_s.pb;
  3007. if (s->data_partitioning) {
  3008. pb2_bits_count= put_bits_count(&s->pb2);
  3009. flush_put_bits(&s->pb2);
  3010. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  3011. s->pb2= backup_s.pb2;
  3012. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  3013. flush_put_bits(&s->tex_pb);
  3014. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  3015. s->tex_pb= backup_s.tex_pb;
  3016. }
  3017. s->last_bits= put_bits_count(&s->pb);
  3018. if (CONFIG_H263_ENCODER &&
  3019. s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
  3020. ff_h263_update_mb(s);
  3021. if(next_block==0){ //FIXME 16 vs linesize16
  3022. s->c.hdsp.put_pixels_tab[0][0](s->c.dest[0], s->c.sc.rd_scratchpad , s->c.linesize ,16);
  3023. s->c.hdsp.put_pixels_tab[1][0](s->c.dest[1], s->c.sc.rd_scratchpad + 16*s->c.linesize , s->c.uvlinesize, 8);
  3024. s->c.hdsp.put_pixels_tab[1][0](s->c.dest[2], s->c.sc.rd_scratchpad + 16*s->c.linesize + 8, s->c.uvlinesize, 8);
  3025. }
  3026. if (s->c.avctx->mb_decision == FF_MB_DECISION_BITS)
  3027. mpv_reconstruct_mb(s, s->block);
  3028. } else {
  3029. int motion_x = 0, motion_y = 0;
  3030. s->c.mv_type = MV_TYPE_16X16;
  3031. // only one MB-Type possible
  3032. switch(mb_type){
  3033. case CANDIDATE_MB_TYPE_INTRA:
  3034. s->c.mv_dir = 0;
  3035. s->c.mb_intra = 1;
  3036. motion_x= s->c.mv[0][0][0] = 0;
  3037. motion_y= s->c.mv[0][0][1] = 0;
  3038. s->c.mbintra_table[xy] = 1;
  3039. break;
  3040. case CANDIDATE_MB_TYPE_INTER:
  3041. s->c.mv_dir = MV_DIR_FORWARD;
  3042. s->c.mb_intra = 0;
  3043. motion_x= s->c.mv[0][0][0] = s->p_mv_table[xy][0];
  3044. motion_y= s->c.mv[0][0][1] = s->p_mv_table[xy][1];
  3045. break;
  3046. case CANDIDATE_MB_TYPE_INTER_I:
  3047. s->c.mv_dir = MV_DIR_FORWARD;
  3048. s->c.mv_type = MV_TYPE_FIELD;
  3049. s->c.mb_intra = 0;
  3050. for(i=0; i<2; i++){
  3051. int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
  3052. s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
  3053. s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
  3054. }
  3055. break;
  3056. case CANDIDATE_MB_TYPE_INTER4V:
  3057. s->c.mv_dir = MV_DIR_FORWARD;
  3058. s->c.mv_type = MV_TYPE_8X8;
  3059. s->c.mb_intra = 0;
  3060. for(i=0; i<4; i++){
  3061. s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
  3062. s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
  3063. }
  3064. break;
  3065. case CANDIDATE_MB_TYPE_DIRECT:
  3066. if (CONFIG_MPEG4_ENCODER) {
  3067. s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  3068. s->c.mb_intra = 0;
  3069. motion_x=s->b_direct_mv_table[xy][0];
  3070. motion_y=s->b_direct_mv_table[xy][1];
  3071. ff_mpeg4_set_direct_mv(&s->c, motion_x, motion_y);
  3072. }
  3073. break;
  3074. case CANDIDATE_MB_TYPE_DIRECT0:
  3075. if (CONFIG_MPEG4_ENCODER) {
  3076. s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
  3077. s->c.mb_intra = 0;
  3078. ff_mpeg4_set_direct_mv(&s->c, 0, 0);
  3079. }
  3080. break;
  3081. case CANDIDATE_MB_TYPE_BIDIR:
  3082. s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3083. s->c.mb_intra = 0;
  3084. s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3085. s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3086. s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3087. s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3088. break;
  3089. case CANDIDATE_MB_TYPE_BACKWARD:
  3090. s->c.mv_dir = MV_DIR_BACKWARD;
  3091. s->c.mb_intra = 0;
  3092. motion_x= s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
  3093. motion_y= s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
  3094. break;
  3095. case CANDIDATE_MB_TYPE_FORWARD:
  3096. s->c.mv_dir = MV_DIR_FORWARD;
  3097. s->c.mb_intra = 0;
  3098. motion_x= s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3099. motion_y= s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3100. break;
  3101. case CANDIDATE_MB_TYPE_FORWARD_I:
  3102. s->c.mv_dir = MV_DIR_FORWARD;
  3103. s->c.mv_type = MV_TYPE_FIELD;
  3104. s->c.mb_intra = 0;
  3105. for(i=0; i<2; i++){
  3106. int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
  3107. s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  3108. s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  3109. }
  3110. break;
  3111. case CANDIDATE_MB_TYPE_BACKWARD_I:
  3112. s->c.mv_dir = MV_DIR_BACKWARD;
  3113. s->c.mv_type = MV_TYPE_FIELD;
  3114. s->c.mb_intra = 0;
  3115. for(i=0; i<2; i++){
  3116. int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
  3117. s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  3118. s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  3119. }
  3120. break;
  3121. case CANDIDATE_MB_TYPE_BIDIR_I:
  3122. s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3123. s->c.mv_type = MV_TYPE_FIELD;
  3124. s->c.mb_intra = 0;
  3125. for(dir=0; dir<2; dir++){
  3126. for(i=0; i<2; i++){
  3127. int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  3128. s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  3129. s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  3130. }
  3131. }
  3132. break;
  3133. default:
  3134. av_unreachable("There is a case for every CANDIDATE_MB_TYPE_* "
  3135. "except CANDIDATE_MB_TYPE_SKIPPED which is never "
  3136. "the only candidate (always coupled with INTER) "
  3137. "so that it never reaches this switch");
  3138. }
  3139. encode_mb(s, motion_x, motion_y);
  3140. // RAL: Update last macroblock type
  3141. s->last_mv_dir = s->c.mv_dir;
  3142. if (CONFIG_H263_ENCODER &&
  3143. s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
  3144. ff_h263_update_mb(s);
  3145. mpv_reconstruct_mb(s, s->block);
  3146. }
  3147. s->c.cur_pic.qscale_table[xy] = s->c.qscale;
  3148. /* clean the MV table in IPS frames for direct mode in B-frames */
  3149. if (s->c.mb_intra /* && I,P,S_TYPE */) {
  3150. s->p_mv_table[xy][0]=0;
  3151. s->p_mv_table[xy][1]=0;
  3152. #if CONFIG_H263_ENCODER
  3153. } else if (s->c.h263_pred || s->c.h263_aic) {
  3154. ff_h263_clean_intra_table_entries(&s->c, xy);
  3155. #endif
  3156. }
  3157. if (s->c.avctx->flags & AV_CODEC_FLAG_PSNR) {
  3158. int w= 16;
  3159. int h= 16;
  3160. if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
  3161. if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
  3162. s->encoding_error[0] += sse(
  3163. s, s->new_pic->data[0] + s->c.mb_x*16 + s->c.mb_y*s->c.linesize*16,
  3164. s->c.dest[0], w, h, s->c.linesize);
  3165. s->encoding_error[1] += sse(
  3166. s, s->new_pic->data[1] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
  3167. s->c.dest[1], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
  3168. s->encoding_error[2] += sse(
  3169. s, s->new_pic->data[2] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
  3170. s->c.dest[2], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
  3171. }
  3172. if (s->loop_filter) {
  3173. if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263)
  3174. ff_h263_loop_filter(&s->c);
  3175. }
  3176. ff_dlog(s->c.avctx, "MB %d %d bits\n",
  3177. s->c.mb_x + s->c.mb_y * s->c.mb_stride, put_bits_count(&s->pb));
  3178. }
  3179. }
  3180. #if CONFIG_MSMPEG4ENC
  3181. //not beautiful here but we must write it before flushing so it has to be here
  3182. if (s->c.msmpeg4_version != MSMP4_UNUSED && s->c.msmpeg4_version < MSMP4_WMV1 &&
  3183. s->c.pict_type == AV_PICTURE_TYPE_I)
  3184. ff_msmpeg4_encode_ext_header(s);
  3185. #endif
  3186. write_slice_end(s);
  3187. return 0;
  3188. }
  3189. #define ADD(field) dst->field += src->field;
  3190. #define MERGE(field) dst->field += src->field; src->field=0
  3191. static void merge_context_after_me(MPVEncContext *const dst, MPVEncContext *const src)
  3192. {
  3193. ADD(me.scene_change_score);
  3194. ADD(me.mc_mb_var_sum_temp);
  3195. ADD(me.mb_var_sum_temp);
  3196. }
  3197. static void merge_context_after_encode(MPVEncContext *const dst, MPVEncContext *const src)
  3198. {
  3199. int i;
  3200. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  3201. MERGE(dct_count[1]);
  3202. ADD(mv_bits);
  3203. ADD(i_tex_bits);
  3204. ADD(p_tex_bits);
  3205. ADD(i_count);
  3206. ADD(misc_bits);
  3207. ADD(encoding_error[0]);
  3208. ADD(encoding_error[1]);
  3209. ADD(encoding_error[2]);
  3210. if (dst->dct_error_sum) {
  3211. for(i=0; i<64; i++){
  3212. MERGE(dct_error_sum[0][i]);
  3213. MERGE(dct_error_sum[1][i]);
  3214. }
  3215. }
  3216. av_assert1(put_bits_count(&src->pb) % 8 ==0);
  3217. av_assert1(put_bits_count(&dst->pb) % 8 ==0);
  3218. ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  3219. flush_put_bits(&dst->pb);
  3220. }
  3221. static int estimate_qp(MPVMainEncContext *const m, int dry_run)
  3222. {
  3223. MPVEncContext *const s = &m->s;
  3224. if (m->next_lambda){
  3225. s->c.cur_pic.ptr->f->quality = m->next_lambda;
  3226. if(!dry_run) m->next_lambda= 0;
  3227. } else if (!m->fixed_qscale) {
  3228. int quality = ff_rate_estimate_qscale(m, dry_run);
  3229. s->c.cur_pic.ptr->f->quality = quality;
  3230. if (s->c.cur_pic.ptr->f->quality < 0)
  3231. return -1;
  3232. }
  3233. if(s->adaptive_quant){
  3234. init_qscale_tab(s);
  3235. switch (s->c.codec_id) {
  3236. case AV_CODEC_ID_MPEG4:
  3237. if (CONFIG_MPEG4_ENCODER)
  3238. ff_clean_mpeg4_qscales(s);
  3239. break;
  3240. case AV_CODEC_ID_H263:
  3241. case AV_CODEC_ID_H263P:
  3242. case AV_CODEC_ID_FLV1:
  3243. if (CONFIG_H263_ENCODER)
  3244. ff_clean_h263_qscales(s);
  3245. break;
  3246. }
  3247. s->lambda = s->lambda_table[0];
  3248. //FIXME broken
  3249. }else
  3250. s->lambda = s->c.cur_pic.ptr->f->quality;
  3251. update_qscale(m);
  3252. return 0;
  3253. }
  3254. /* must be called before writing the header */
  3255. static void set_frame_distances(MPVEncContext *const s)
  3256. {
  3257. av_assert1(s->c.cur_pic.ptr->f->pts != AV_NOPTS_VALUE);
  3258. s->c.time = s->c.cur_pic.ptr->f->pts * s->c.avctx->time_base.num;
  3259. if (s->c.pict_type == AV_PICTURE_TYPE_B) {
  3260. s->c.pb_time = s->c.pp_time - (s->c.last_non_b_time - s->c.time);
  3261. av_assert1(s->c.pb_time > 0 && s->c.pb_time < s->c.pp_time);
  3262. }else{
  3263. s->c.pp_time = s->c.time - s->c.last_non_b_time;
  3264. s->c.last_non_b_time = s->c.time;
  3265. av_assert1(s->picture_number == 0 || s->c.pp_time > 0);
  3266. }
  3267. }
  3268. static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt)
  3269. {
  3270. MPVEncContext *const s = &m->s;
  3271. int i, ret;
  3272. int bits;
  3273. int context_count = s->c.slice_context_count;
  3274. /* we need to initialize some time vars before we can encode B-frames */
  3275. // RAL: Condition added for MPEG1VIDEO
  3276. if (s->c.out_format == FMT_MPEG1 || (s->c.h263_pred && s->c.msmpeg4_version == MSMP4_UNUSED))
  3277. set_frame_distances(s);
  3278. if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4)
  3279. ff_set_mpeg4_time(s);
  3280. // s->lambda = s->c.cur_pic.ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
  3281. if (s->c.pict_type == AV_PICTURE_TYPE_I) {
  3282. s->c.no_rounding = s->c.msmpeg4_version >= MSMP4_V3;
  3283. } else if (s->c.pict_type != AV_PICTURE_TYPE_B) {
  3284. s->c.no_rounding ^= s->flipflop_rounding;
  3285. }
  3286. if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
  3287. ret = estimate_qp(m, 1);
  3288. if (ret < 0)
  3289. return ret;
  3290. ff_get_2pass_fcode(m);
  3291. } else if (!(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE)) {
  3292. if (s->c.pict_type == AV_PICTURE_TYPE_B)
  3293. s->lambda = m->last_lambda_for[s->c.pict_type];
  3294. else
  3295. s->lambda = m->last_lambda_for[m->last_non_b_pict_type];
  3296. update_qscale(m);
  3297. }
  3298. s->c.mb_intra = 0; //for the rate distortion & bit compare functions
  3299. for (int i = 0; i < context_count; i++) {
  3300. MPVEncContext *const slice = s->c.enc_contexts[i];
  3301. int h = s->c.mb_height;
  3302. uint8_t *start = pkt->data + (int64_t)pkt->size * slice->c.start_mb_y / h;
  3303. uint8_t *end = pkt->data + (int64_t)pkt->size * slice->c. end_mb_y / h;
  3304. init_put_bits(&slice->pb, start, end - start);
  3305. if (i) {
  3306. ret = ff_update_duplicate_context(&slice->c, &s->c);
  3307. if (ret < 0)
  3308. return ret;
  3309. slice->lambda = s->lambda;
  3310. slice->lambda2 = s->lambda2;
  3311. }
  3312. slice->me.temp = slice->me.scratchpad = slice->c.sc.scratchpad_buf;
  3313. ff_me_init_pic(slice);
  3314. }
  3315. /* Estimate motion for every MB */
  3316. if (s->c.pict_type != AV_PICTURE_TYPE_I) {
  3317. s->lambda = (s->lambda * m->me_penalty_compensation + 128) >> 8;
  3318. s->lambda2 = (s->lambda2 * (int64_t) m->me_penalty_compensation + 128) >> 8;
  3319. if (s->c.pict_type != AV_PICTURE_TYPE_B) {
  3320. if ((m->me_pre && m->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
  3321. m->me_pre == 2) {
  3322. s->c.avctx->execute(s->c.avctx, pre_estimate_motion_thread,
  3323. &s->c.enc_contexts[0], NULL,
  3324. context_count, sizeof(void*));
  3325. }
  3326. }
  3327. s->c.avctx->execute(s->c.avctx, estimate_motion_thread, &s->c.enc_contexts[0],
  3328. NULL, context_count, sizeof(void*));
  3329. }else /* if (s->c.pict_type == AV_PICTURE_TYPE_I) */{
  3330. /* I-Frame */
  3331. for (int i = 0; i < s->c.mb_stride * s->c.mb_height; i++)
  3332. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3333. if (!m->fixed_qscale) {
  3334. /* finding spatial complexity for I-frame rate control */
  3335. s->c.avctx->execute(s->c.avctx, mb_var_thread, &s->c.enc_contexts[0],
  3336. NULL, context_count, sizeof(void*));
  3337. }
  3338. }
  3339. for(i=1; i<context_count; i++){
  3340. merge_context_after_me(s, s->c.enc_contexts[i]);
  3341. }
  3342. m->mc_mb_var_sum = s->me.mc_mb_var_sum_temp;
  3343. m->mb_var_sum = s->me. mb_var_sum_temp;
  3344. emms_c();
  3345. if (s->me.scene_change_score > m->scenechange_threshold &&
  3346. s->c.pict_type == AV_PICTURE_TYPE_P) {
  3347. s->c.pict_type = AV_PICTURE_TYPE_I;
  3348. for (int i = 0; i < s->c.mb_stride * s->c.mb_height; i++)
  3349. s->mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
  3350. if (s->c.msmpeg4_version >= MSMP4_V3)
  3351. s->c.no_rounding = 1;
  3352. ff_dlog(s->c.avctx, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
  3353. m->mb_var_sum, m->mc_mb_var_sum);
  3354. }
  3355. if (!s->umvplus) {
  3356. if (s->c.pict_type == AV_PICTURE_TYPE_P || s->c.pict_type == AV_PICTURE_TYPE_S) {
  3357. s->f_code = ff_get_best_fcode(m, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  3358. if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3359. int a,b;
  3360. a = ff_get_best_fcode(m, s->c.p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  3361. b = ff_get_best_fcode(m, s->c.p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  3362. s->f_code = FFMAX3(s->f_code, a, b);
  3363. }
  3364. ff_fix_long_p_mvs(s, s->intra_penalty ? CANDIDATE_MB_TYPE_INTER : CANDIDATE_MB_TYPE_INTRA);
  3365. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
  3366. if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3367. int j;
  3368. for(i=0; i<2; i++){
  3369. for(j=0; j<2; j++)
  3370. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  3371. s->c.p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
  3372. }
  3373. }
  3374. } else if (s->c.pict_type == AV_PICTURE_TYPE_B) {
  3375. int a, b;
  3376. a = ff_get_best_fcode(m, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  3377. b = ff_get_best_fcode(m, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3378. s->f_code = FFMAX(a, b);
  3379. a = ff_get_best_fcode(m, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  3380. b = ff_get_best_fcode(m, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3381. s->b_code = FFMAX(a, b);
  3382. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  3383. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  3384. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3385. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3386. if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
  3387. int dir, j;
  3388. for(dir=0; dir<2; dir++){
  3389. for(i=0; i<2; i++){
  3390. for(j=0; j<2; j++){
  3391. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  3392. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  3393. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  3394. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  3395. }
  3396. }
  3397. }
  3398. }
  3399. }
  3400. }
  3401. ret = estimate_qp(m, 0);
  3402. if (ret < 0)
  3403. return ret;
  3404. if (s->c.qscale < 3 && s->max_qcoeff <= 128 &&
  3405. s->c.pict_type == AV_PICTURE_TYPE_I &&
  3406. !(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE))
  3407. s->c.qscale = 3; //reduce clipping problems
  3408. if (s->c.out_format == FMT_MJPEG) {
  3409. ret = ff_check_codec_matrices(s->c.avctx, FF_MATRIX_TYPE_INTRA | FF_MATRIX_TYPE_CHROMA_INTRA,
  3410. (7 + s->c.qscale) / s->c.qscale, 65535);
  3411. if (ret < 0)
  3412. return ret;
  3413. if (s->c.codec_id != AV_CODEC_ID_AMV) {
  3414. const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
  3415. const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
  3416. if (s->c.avctx->intra_matrix) {
  3417. chroma_matrix =
  3418. luma_matrix = s->c.avctx->intra_matrix;
  3419. }
  3420. if (s->c.avctx->chroma_intra_matrix)
  3421. chroma_matrix = s->c.avctx->chroma_intra_matrix;
  3422. /* for mjpeg, we do include qscale in the matrix */
  3423. for (int i = 1; i < 64; i++) {
  3424. int j = s->c.idsp.idct_permutation[i];
  3425. s->c.chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->c.qscale) >> 3);
  3426. s->c. intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->c.qscale) >> 3);
  3427. }
  3428. s->c.y_dc_scale_table =
  3429. s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[s->c.intra_dc_precision];
  3430. s->c.chroma_intra_matrix[0] =
  3431. s->c.intra_matrix[0] = ff_mpeg12_dc_scale_table[s->c.intra_dc_precision][8];
  3432. } else {
  3433. static const uint8_t y[32] = {13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
  3434. static const uint8_t c[32] = {14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
  3435. for (int i = 1; i < 64; i++) {
  3436. int j = s->c.idsp.idct_permutation[ff_zigzag_direct[i]];
  3437. s->c.intra_matrix[j] = sp5x_qscale_five_quant_table[0][i];
  3438. s->c.chroma_intra_matrix[j] = sp5x_qscale_five_quant_table[1][i];
  3439. }
  3440. s->c.y_dc_scale_table = y;
  3441. s->c.c_dc_scale_table = c;
  3442. s->c.intra_matrix[0] = 13;
  3443. s->c.chroma_intra_matrix[0] = 14;
  3444. }
  3445. ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  3446. s->c.intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3447. ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
  3448. s->c.chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
  3449. s->c.qscale = 8;
  3450. }
  3451. if (s->c.pict_type == AV_PICTURE_TYPE_I) {
  3452. s->c.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY;
  3453. } else {
  3454. s->c.cur_pic.ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
  3455. }
  3456. s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
  3457. if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
  3458. m->picture_in_gop_number = 0;
  3459. s->c.mb_x = s->c.mb_y = 0;
  3460. s->last_bits= put_bits_count(&s->pb);
  3461. ret = m->encode_picture_header(m);
  3462. if (ret < 0)
  3463. return ret;
  3464. bits= put_bits_count(&s->pb);
  3465. m->header_bits = bits - s->last_bits;
  3466. for(i=1; i<context_count; i++){
  3467. update_duplicate_context_after_me(s->c.enc_contexts[i], s);
  3468. }
  3469. s->c.avctx->execute(s->c.avctx, encode_thread, &s->c.enc_contexts[0],
  3470. NULL, context_count, sizeof(void*));
  3471. for(i=1; i<context_count; i++){
  3472. if (s->pb.buf_end == s->c.enc_contexts[i]->pb.buf)
  3473. set_put_bits_buffer_size(&s->pb, FFMIN(s->c.enc_contexts[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
  3474. merge_context_after_encode(s, s->c.enc_contexts[i]);
  3475. }
  3476. emms_c();
  3477. return 0;
  3478. }
  3479. static void denoise_dct_c(MPVEncContext *const s, int16_t *block)
  3480. {
  3481. const int intra = s->c.mb_intra;
  3482. int i;
  3483. s->dct_count[intra]++;
  3484. for(i=0; i<64; i++){
  3485. int level= block[i];
  3486. if(level){
  3487. if(level>0){
  3488. s->dct_error_sum[intra][i] += level;
  3489. level -= s->dct_offset[intra][i];
  3490. if(level<0) level=0;
  3491. }else{
  3492. s->dct_error_sum[intra][i] -= level;
  3493. level += s->dct_offset[intra][i];
  3494. if(level>0) level=0;
  3495. }
  3496. block[i]= level;
  3497. }
  3498. }
  3499. }
  3500. static int dct_quantize_trellis_c(MPVEncContext *const s,
  3501. int16_t *block, int n,
  3502. int qscale, int *overflow){
  3503. const int *qmat;
  3504. const uint16_t *matrix;
  3505. const uint8_t *scantable;
  3506. const uint8_t *perm_scantable;
  3507. int max=0;
  3508. unsigned int threshold1, threshold2;
  3509. int bias=0;
  3510. int run_tab[65];
  3511. int level_tab[65];
  3512. int score_tab[65];
  3513. int survivor[65];
  3514. int survivor_count;
  3515. int last_run=0;
  3516. int last_level=0;
  3517. int last_score= 0;
  3518. int last_i;
  3519. int coeff[2][64];
  3520. int coeff_count[64];
  3521. int qmul, qadd, start_i, last_non_zero, i, dc;
  3522. const int esc_length= s->ac_esc_length;
  3523. const uint8_t *length, *last_length;
  3524. const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  3525. int mpeg2_qscale;
  3526. s->fdsp.fdct(block);
  3527. if(s->dct_error_sum)
  3528. s->denoise_dct(s, block);
  3529. qmul= qscale*16;
  3530. qadd= ((qscale-1)|1)*8;
  3531. if (s->c.q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
  3532. else mpeg2_qscale = qscale << 1;
  3533. if (s->c.mb_intra) {
  3534. int q;
  3535. scantable = s->c.intra_scantable.scantable;
  3536. perm_scantable = s->c.intra_scantable.permutated;
  3537. if (!s->c.h263_aic) {
  3538. if (n < 4)
  3539. q = s->c.y_dc_scale;
  3540. else
  3541. q = s->c.c_dc_scale;
  3542. q = q << 3;
  3543. } else{
  3544. /* For AIC we skip quant/dequant of INTRADC */
  3545. q = 1 << 3;
  3546. qadd=0;
  3547. }
  3548. /* note: block[0] is assumed to be positive */
  3549. block[0] = (block[0] + (q >> 1)) / q;
  3550. start_i = 1;
  3551. last_non_zero = 0;
  3552. qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
  3553. matrix = n < 4 ? s->c.intra_matrix : s->c.chroma_intra_matrix;
  3554. if (s->mpeg_quant || s->c.out_format == FMT_MPEG1 || s->c.out_format == FMT_MJPEG)
  3555. bias= 1<<(QMAT_SHIFT-1);
  3556. if (n > 3 && s->intra_chroma_ac_vlc_length) {
  3557. length = s->intra_chroma_ac_vlc_length;
  3558. last_length= s->intra_chroma_ac_vlc_last_length;
  3559. } else {
  3560. length = s->intra_ac_vlc_length;
  3561. last_length= s->intra_ac_vlc_last_length;
  3562. }
  3563. } else {
  3564. scantable = s->c.inter_scantable.scantable;
  3565. perm_scantable = s->c.inter_scantable.permutated;
  3566. start_i = 0;
  3567. last_non_zero = -1;
  3568. qmat = s->q_inter_matrix[qscale];
  3569. matrix = s->c.inter_matrix;
  3570. length = s->inter_ac_vlc_length;
  3571. last_length= s->inter_ac_vlc_last_length;
  3572. }
  3573. last_i= start_i;
  3574. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3575. threshold2= (threshold1<<1);
  3576. for(i=63; i>=start_i; i--) {
  3577. const int j = scantable[i];
  3578. int64_t level = (int64_t)block[j] * qmat[j];
  3579. if(((uint64_t)(level+threshold1))>threshold2){
  3580. last_non_zero = i;
  3581. break;
  3582. }
  3583. }
  3584. for(i=start_i; i<=last_non_zero; i++) {
  3585. const int j = scantable[i];
  3586. int64_t level = (int64_t)block[j] * qmat[j];
  3587. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3588. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3589. if(((uint64_t)(level+threshold1))>threshold2){
  3590. if(level>0){
  3591. level= (bias + level)>>QMAT_SHIFT;
  3592. coeff[0][i]= level;
  3593. coeff[1][i]= level-1;
  3594. // coeff[2][k]= level-2;
  3595. }else{
  3596. level= (bias - level)>>QMAT_SHIFT;
  3597. coeff[0][i]= -level;
  3598. coeff[1][i]= -level+1;
  3599. // coeff[2][k]= -level+2;
  3600. }
  3601. coeff_count[i]= FFMIN(level, 2);
  3602. av_assert2(coeff_count[i]);
  3603. max |=level;
  3604. }else{
  3605. coeff[0][i]= (level>>31)|1;
  3606. coeff_count[i]= 1;
  3607. }
  3608. }
  3609. *overflow= s->max_qcoeff < max; //overflow might have happened
  3610. if(last_non_zero < start_i){
  3611. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3612. return last_non_zero;
  3613. }
  3614. score_tab[start_i]= 0;
  3615. survivor[0]= start_i;
  3616. survivor_count= 1;
  3617. for(i=start_i; i<=last_non_zero; i++){
  3618. int level_index, j, zero_distortion;
  3619. int dct_coeff= FFABS(block[ scantable[i] ]);
  3620. int best_score=256*256*256*120;
  3621. if (s->fdsp.fdct == ff_fdct_ifast)
  3622. dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
  3623. zero_distortion= dct_coeff*dct_coeff;
  3624. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3625. int distortion;
  3626. int level= coeff[level_index][i];
  3627. const int alevel= FFABS(level);
  3628. int unquant_coeff;
  3629. av_assert2(level);
  3630. if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
  3631. unquant_coeff= alevel*qmul + qadd;
  3632. } else if (s->c.out_format == FMT_MJPEG) {
  3633. j = s->c.idsp.idct_permutation[scantable[i]];
  3634. unquant_coeff = alevel * matrix[j] * 8;
  3635. }else{ // MPEG-1
  3636. j = s->c.idsp.idct_permutation[scantable[i]]; // FIXME: optimize
  3637. if (s->c.mb_intra) {
  3638. unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
  3639. unquant_coeff = (unquant_coeff - 1) | 1;
  3640. }else{
  3641. unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
  3642. unquant_coeff = (unquant_coeff - 1) | 1;
  3643. }
  3644. unquant_coeff<<= 3;
  3645. }
  3646. distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
  3647. level+=64;
  3648. if((level&(~127)) == 0){
  3649. for(j=survivor_count-1; j>=0; j--){
  3650. int run= i - survivor[j];
  3651. int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3652. score += score_tab[i-run];
  3653. if(score < best_score){
  3654. best_score= score;
  3655. run_tab[i+1]= run;
  3656. level_tab[i+1]= level-64;
  3657. }
  3658. }
  3659. if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
  3660. for(j=survivor_count-1; j>=0; j--){
  3661. int run= i - survivor[j];
  3662. int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3663. score += score_tab[i-run];
  3664. if(score < last_score){
  3665. last_score= score;
  3666. last_run= run;
  3667. last_level= level-64;
  3668. last_i= i+1;
  3669. }
  3670. }
  3671. }
  3672. }else{
  3673. distortion += esc_length*lambda;
  3674. for(j=survivor_count-1; j>=0; j--){
  3675. int run= i - survivor[j];
  3676. int score= distortion + score_tab[i-run];
  3677. if(score < best_score){
  3678. best_score= score;
  3679. run_tab[i+1]= run;
  3680. level_tab[i+1]= level-64;
  3681. }
  3682. }
  3683. if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
  3684. for(j=survivor_count-1; j>=0; j--){
  3685. int run= i - survivor[j];
  3686. int score= distortion + score_tab[i-run];
  3687. if(score < last_score){
  3688. last_score= score;
  3689. last_run= run;
  3690. last_level= level-64;
  3691. last_i= i+1;
  3692. }
  3693. }
  3694. }
  3695. }
  3696. }
  3697. score_tab[i+1]= best_score;
  3698. // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
  3699. if(last_non_zero <= 27){
  3700. for(; survivor_count; survivor_count--){
  3701. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  3702. break;
  3703. }
  3704. }else{
  3705. for(; survivor_count; survivor_count--){
  3706. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  3707. break;
  3708. }
  3709. }
  3710. survivor[ survivor_count++ ]= i+1;
  3711. }
  3712. if (s->c.out_format != FMT_H263 && s->c.out_format != FMT_H261) {
  3713. last_score= 256*256*256*120;
  3714. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  3715. int score= score_tab[i];
  3716. if (i)
  3717. score += lambda * 2; // FIXME more exact?
  3718. if(score < last_score){
  3719. last_score= score;
  3720. last_i= i;
  3721. last_level= level_tab[i];
  3722. last_run= run_tab[i];
  3723. }
  3724. }
  3725. }
  3726. s->coded_score[n] = last_score;
  3727. dc= FFABS(block[0]);
  3728. last_non_zero= last_i - 1;
  3729. memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
  3730. if(last_non_zero < start_i)
  3731. return last_non_zero;
  3732. if(last_non_zero == 0 && start_i == 0){
  3733. int best_level= 0;
  3734. int best_score= dc * dc;
  3735. for(i=0; i<coeff_count[0]; i++){
  3736. int level= coeff[i][0];
  3737. int alevel= FFABS(level);
  3738. int unquant_coeff, score, distortion;
  3739. if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
  3740. unquant_coeff= (alevel*qmul + qadd)>>3;
  3741. } else{ // MPEG-1
  3742. unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
  3743. unquant_coeff = (unquant_coeff - 1) | 1;
  3744. }
  3745. unquant_coeff = (unquant_coeff + 4) >> 3;
  3746. unquant_coeff<<= 3 + 3;
  3747. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  3748. level+=64;
  3749. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  3750. else score= distortion + esc_length*lambda;
  3751. if(score < best_score){
  3752. best_score= score;
  3753. best_level= level - 64;
  3754. }
  3755. }
  3756. block[0]= best_level;
  3757. s->coded_score[n] = best_score - dc*dc;
  3758. if(best_level == 0) return -1;
  3759. else return last_non_zero;
  3760. }
  3761. i= last_i;
  3762. av_assert2(last_level);
  3763. block[ perm_scantable[last_non_zero] ]= last_level;
  3764. i -= last_run + 1;
  3765. for(; i>start_i; i -= run_tab[i] + 1){
  3766. block[ perm_scantable[i-1] ]= level_tab[i];
  3767. }
  3768. return last_non_zero;
  3769. }
  3770. static int16_t basis[64][64];
  3771. static void build_basis(uint8_t *perm){
  3772. int i, j, x, y;
  3773. emms_c();
  3774. for(i=0; i<8; i++){
  3775. for(j=0; j<8; j++){
  3776. for(y=0; y<8; y++){
  3777. for(x=0; x<8; x++){
  3778. double s= 0.25*(1<<BASIS_SHIFT);
  3779. int index= 8*i + j;
  3780. int perm_index= perm[index];
  3781. if(i==0) s*= sqrt(0.5);
  3782. if(j==0) s*= sqrt(0.5);
  3783. basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
  3784. }
  3785. }
  3786. }
  3787. }
  3788. }
  3789. static int dct_quantize_refine(MPVEncContext *const s, //FIXME breaks denoise?
  3790. int16_t *block, int16_t *weight, int16_t *orig,
  3791. int n, int qscale){
  3792. int16_t rem[64];
  3793. LOCAL_ALIGNED_16(int16_t, d1, [64]);
  3794. const uint8_t *scantable;
  3795. const uint8_t *perm_scantable;
  3796. // unsigned int threshold1, threshold2;
  3797. // int bias=0;
  3798. int run_tab[65];
  3799. int prev_run=0;
  3800. int prev_level=0;
  3801. int qmul, qadd, start_i, last_non_zero, i, dc;
  3802. const uint8_t *length;
  3803. const uint8_t *last_length;
  3804. int lambda;
  3805. int rle_index, run, q = 1, sum; //q is only used when s->c.mb_intra is true
  3806. if(basis[0][0] == 0)
  3807. build_basis(s->c.idsp.idct_permutation);
  3808. qmul= qscale*2;
  3809. qadd= (qscale-1)|1;
  3810. if (s->c.mb_intra) {
  3811. scantable = s->c.intra_scantable.scantable;
  3812. perm_scantable = s->c.intra_scantable.permutated;
  3813. if (!s->c.h263_aic) {
  3814. if (n < 4)
  3815. q = s->c.y_dc_scale;
  3816. else
  3817. q = s->c.c_dc_scale;
  3818. } else{
  3819. /* For AIC we skip quant/dequant of INTRADC */
  3820. q = 1;
  3821. qadd=0;
  3822. }
  3823. q <<= RECON_SHIFT-3;
  3824. /* note: block[0] is assumed to be positive */
  3825. dc= block[0]*q;
  3826. // block[0] = (block[0] + (q >> 1)) / q;
  3827. start_i = 1;
  3828. // if (s->mpeg_quant || s->c.out_format == FMT_MPEG1)
  3829. // bias= 1<<(QMAT_SHIFT-1);
  3830. if (n > 3 && s->intra_chroma_ac_vlc_length) {
  3831. length = s->intra_chroma_ac_vlc_length;
  3832. last_length= s->intra_chroma_ac_vlc_last_length;
  3833. } else {
  3834. length = s->intra_ac_vlc_length;
  3835. last_length= s->intra_ac_vlc_last_length;
  3836. }
  3837. } else {
  3838. scantable = s->c.inter_scantable.scantable;
  3839. perm_scantable = s->c.inter_scantable.permutated;
  3840. dc= 0;
  3841. start_i = 0;
  3842. length = s->inter_ac_vlc_length;
  3843. last_length= s->inter_ac_vlc_last_length;
  3844. }
  3845. last_non_zero = s->c.block_last_index[n];
  3846. dc += (1<<(RECON_SHIFT-1));
  3847. for(i=0; i<64; i++){
  3848. rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
  3849. }
  3850. sum=0;
  3851. for(i=0; i<64; i++){
  3852. int one= 36;
  3853. int qns=4;
  3854. int w;
  3855. w= FFABS(weight[i]) + qns*one;
  3856. w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  3857. weight[i] = w;
  3858. // w=weight[i] = (63*qns + (w/2)) / w;
  3859. av_assert2(w>0);
  3860. av_assert2(w<(1<<6));
  3861. sum += w*w;
  3862. }
  3863. lambda = sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  3864. run=0;
  3865. rle_index=0;
  3866. for(i=start_i; i<=last_non_zero; i++){
  3867. int j= perm_scantable[i];
  3868. const int level= block[j];
  3869. int coeff;
  3870. if(level){
  3871. if(level<0) coeff= qmul*level - qadd;
  3872. else coeff= qmul*level + qadd;
  3873. run_tab[rle_index++]=run;
  3874. run=0;
  3875. s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
  3876. }else{
  3877. run++;
  3878. }
  3879. }
  3880. for(;;){
  3881. int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
  3882. int best_coeff=0;
  3883. int best_change=0;
  3884. int run2, best_unquant_change=0, analyze_gradient;
  3885. analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
  3886. if(analyze_gradient){
  3887. for(i=0; i<64; i++){
  3888. int w= weight[i];
  3889. d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  3890. }
  3891. s->fdsp.fdct(d1);
  3892. }
  3893. if(start_i){
  3894. const int level= block[0];
  3895. int change, old_coeff;
  3896. av_assert2(s->c.mb_intra);
  3897. old_coeff= q*level;
  3898. for(change=-1; change<=1; change+=2){
  3899. int new_level= level + change;
  3900. int score, new_coeff;
  3901. new_coeff= q*new_level;
  3902. if(new_coeff >= 2048 || new_coeff < 0)
  3903. continue;
  3904. score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
  3905. new_coeff - old_coeff);
  3906. if(score<best_score){
  3907. best_score= score;
  3908. best_coeff= 0;
  3909. best_change= change;
  3910. best_unquant_change= new_coeff - old_coeff;
  3911. }
  3912. }
  3913. }
  3914. run=0;
  3915. rle_index=0;
  3916. run2= run_tab[rle_index++];
  3917. prev_level=0;
  3918. prev_run=0;
  3919. for(i=start_i; i<64; i++){
  3920. int j= perm_scantable[i];
  3921. const int level= block[j];
  3922. int change, old_coeff;
  3923. if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  3924. break;
  3925. if(level){
  3926. if(level<0) old_coeff= qmul*level - qadd;
  3927. else old_coeff= qmul*level + qadd;
  3928. run2= run_tab[rle_index++]; //FIXME ! maybe after last
  3929. }else{
  3930. old_coeff=0;
  3931. run2--;
  3932. av_assert2(run2>=0 || i >= last_non_zero );
  3933. }
  3934. for(change=-1; change<=1; change+=2){
  3935. int new_level= level + change;
  3936. int score, new_coeff, unquant_change;
  3937. score=0;
  3938. if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
  3939. continue;
  3940. if(new_level){
  3941. if(new_level<0) new_coeff= qmul*new_level - qadd;
  3942. else new_coeff= qmul*new_level + qadd;
  3943. if(new_coeff >= 2048 || new_coeff <= -2048)
  3944. continue;
  3945. //FIXME check for overflow
  3946. if(level){
  3947. if(level < 63 && level > -63){
  3948. if(i < last_non_zero)
  3949. score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3950. - length[UNI_AC_ENC_INDEX(run, level+64)];
  3951. else
  3952. score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  3953. - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  3954. }
  3955. }else{
  3956. av_assert2(FFABS(new_level)==1);
  3957. if(analyze_gradient){
  3958. int g= d1[ scantable[i] ];
  3959. if(g && (g^new_level) >= 0)
  3960. continue;
  3961. }
  3962. if(i < last_non_zero){
  3963. int next_i= i + run2 + 1;
  3964. int next_level= block[ perm_scantable[next_i] ] + 64;
  3965. if(next_level&(~127))
  3966. next_level= 0;
  3967. if(next_i < last_non_zero)
  3968. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3969. + length[UNI_AC_ENC_INDEX(run2, next_level)]
  3970. - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3971. else
  3972. score += length[UNI_AC_ENC_INDEX(run, 65)]
  3973. + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3974. - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  3975. }else{
  3976. score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  3977. if(prev_level){
  3978. score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  3979. - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  3980. }
  3981. }
  3982. }
  3983. }else{
  3984. new_coeff=0;
  3985. av_assert2(FFABS(level)==1);
  3986. if(i < last_non_zero){
  3987. int next_i= i + run2 + 1;
  3988. int next_level= block[ perm_scantable[next_i] ] + 64;
  3989. if(next_level&(~127))
  3990. next_level= 0;
  3991. if(next_i < last_non_zero)
  3992. score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3993. - length[UNI_AC_ENC_INDEX(run2, next_level)]
  3994. - length[UNI_AC_ENC_INDEX(run, 65)];
  3995. else
  3996. score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  3997. - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  3998. - length[UNI_AC_ENC_INDEX(run, 65)];
  3999. }else{
  4000. score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  4001. if(prev_level){
  4002. score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  4003. - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  4004. }
  4005. }
  4006. }
  4007. score *= lambda;
  4008. unquant_change= new_coeff - old_coeff;
  4009. av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
  4010. score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
  4011. unquant_change);
  4012. if(score<best_score){
  4013. best_score= score;
  4014. best_coeff= i;
  4015. best_change= change;
  4016. best_unquant_change= unquant_change;
  4017. }
  4018. }
  4019. if(level){
  4020. prev_level= level + 64;
  4021. if(prev_level&(~127))
  4022. prev_level= 0;
  4023. prev_run= run;
  4024. run=0;
  4025. }else{
  4026. run++;
  4027. }
  4028. }
  4029. if(best_change){
  4030. int j= perm_scantable[ best_coeff ];
  4031. block[j] += best_change;
  4032. if(best_coeff > last_non_zero){
  4033. last_non_zero= best_coeff;
  4034. av_assert2(block[j]);
  4035. }else{
  4036. for(; last_non_zero>=start_i; last_non_zero--){
  4037. if(block[perm_scantable[last_non_zero]])
  4038. break;
  4039. }
  4040. }
  4041. run=0;
  4042. rle_index=0;
  4043. for(i=start_i; i<=last_non_zero; i++){
  4044. int j= perm_scantable[i];
  4045. const int level= block[j];
  4046. if(level){
  4047. run_tab[rle_index++]=run;
  4048. run=0;
  4049. }else{
  4050. run++;
  4051. }
  4052. }
  4053. s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
  4054. }else{
  4055. break;
  4056. }
  4057. }
  4058. return last_non_zero;
  4059. }
  4060. /**
  4061. * Permute an 8x8 block according to permutation.
  4062. * @param block the block which will be permuted according to
  4063. * the given permutation vector
  4064. * @param permutation the permutation vector
  4065. * @param last the last non zero coefficient in scantable order, used to
  4066. * speed the permutation up
  4067. * @param scantable the used scantable, this is only used to speed the
  4068. * permutation up, the block is not (inverse) permutated
  4069. * to scantable order!
  4070. */
  4071. void ff_block_permute(int16_t *block, const uint8_t *permutation,
  4072. const uint8_t *scantable, int last)
  4073. {
  4074. int i;
  4075. int16_t temp[64];
  4076. if (last <= 0)
  4077. return;
  4078. //FIXME it is ok but not clean and might fail for some permutations
  4079. // if (permutation[1] == 1)
  4080. // return;
  4081. for (i = 0; i <= last; i++) {
  4082. const int j = scantable[i];
  4083. temp[j] = block[j];
  4084. block[j] = 0;
  4085. }
  4086. for (i = 0; i <= last; i++) {
  4087. const int j = scantable[i];
  4088. const int perm_j = permutation[j];
  4089. block[perm_j] = temp[j];
  4090. }
  4091. }
  4092. static int dct_quantize_c(MPVEncContext *const s,
  4093. int16_t *block, int n,
  4094. int qscale, int *overflow)
  4095. {
  4096. int i, last_non_zero, q, start_i;
  4097. const int *qmat;
  4098. const uint8_t *scantable;
  4099. int bias;
  4100. int max=0;
  4101. unsigned int threshold1, threshold2;
  4102. s->fdsp.fdct(block);
  4103. if(s->dct_error_sum)
  4104. s->denoise_dct(s, block);
  4105. if (s->c.mb_intra) {
  4106. scantable = s->c.intra_scantable.scantable;
  4107. if (!s->c.h263_aic) {
  4108. if (n < 4)
  4109. q = s->c.y_dc_scale;
  4110. else
  4111. q = s->c.c_dc_scale;
  4112. q = q << 3;
  4113. } else
  4114. /* For AIC we skip quant/dequant of INTRADC */
  4115. q = 1 << 3;
  4116. /* note: block[0] is assumed to be positive */
  4117. block[0] = (block[0] + (q >> 1)) / q;
  4118. start_i = 1;
  4119. last_non_zero = 0;
  4120. qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
  4121. bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
  4122. } else {
  4123. scantable = s->c.inter_scantable.scantable;
  4124. start_i = 0;
  4125. last_non_zero = -1;
  4126. qmat = s->q_inter_matrix[qscale];
  4127. bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
  4128. }
  4129. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4130. threshold2= (threshold1<<1);
  4131. for(i=63;i>=start_i;i--) {
  4132. const int j = scantable[i];
  4133. int64_t level = (int64_t)block[j] * qmat[j];
  4134. if(((uint64_t)(level+threshold1))>threshold2){
  4135. last_non_zero = i;
  4136. break;
  4137. }else{
  4138. block[j]=0;
  4139. }
  4140. }
  4141. for(i=start_i; i<=last_non_zero; i++) {
  4142. const int j = scantable[i];
  4143. int64_t level = (int64_t)block[j] * qmat[j];
  4144. // if( bias+level >= (1<<QMAT_SHIFT)
  4145. // || bias-level >= (1<<QMAT_SHIFT)){
  4146. if(((uint64_t)(level+threshold1))>threshold2){
  4147. if(level>0){
  4148. level= (bias + level)>>QMAT_SHIFT;
  4149. block[j]= level;
  4150. }else{
  4151. level= (bias - level)>>QMAT_SHIFT;
  4152. block[j]= -level;
  4153. }
  4154. max |=level;
  4155. }else{
  4156. block[j]=0;
  4157. }
  4158. }
  4159. *overflow= s->max_qcoeff < max; //overflow might have happened
  4160. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  4161. if (s->c.idsp.perm_type != FF_IDCT_PERM_NONE)
  4162. ff_block_permute(block, s->c.idsp.idct_permutation,
  4163. scantable, last_non_zero);
  4164. return last_non_zero;
  4165. }