0012_ffmpeg.patch 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812
  1. commit 1430832d00ef9a9f65c6aea84eba63606cc0b924
  2. Author: CYBERDEV <cyberdevil-no-reply@notabug.org>
  3. Date: Thu Jun 6 00:12:56 2024 +0200
  4. ffmpeg: Fix first arg of av_packet_unref (my bad)
  5. commit cc06806e12600076b1f7b1de4adcf41f0d7e9a6f
  6. Author: CYBERDEViL <cyberdevil@notabug.org>
  7. Date: Sun Dec 24 22:59:30 2023 +0100
  8. ffmpeg: fix building BGE with latest FFMpeg
  9. commit 5e5a2a1eaf4205f10ac25c61358c613b240cc7cc
  10. Author: CYBERDEViL <cyberdevil@notabug.org>
  11. Date: Tue Dec 19 21:46:52 2023 +0100
  12. ffmpeg: support for FFmpeg 5
  13. Used Blender upstream refs:
  14. - 8d6264ea12 "Cleanup: Remove deprecated variables and functions calls from our ffmpeg code"
  15. - dd2e187344 "Audaspace: add support for latest ffmpeg."
  16. - af6a1b08e3 "VSE: Refactor our code to be compatible with ffmpeg 5.0"
  17. - 08a6321501 "FFmpeg pixel format conversion improvements"
  18. - fba35aa8c5 "Use FFmpeg's own `av_guess_frame_rate()` function instead of guessing ourselves"
  19. Updated the FFmpeg related code as much as possible to above refs
  20. commit abec792ffaf1f70ce68c18ff06065ae4224de1b1
  21. Author: CYBERDEViL <cyberdevil@notabug.org>
  22. Date: Tue Dec 19 18:09:24 2023 +0100
  23. ffmpeg: replace deprecated av_free_packet() with av_packet_unref()
  24. commit 528f895ac4a3cd9b992c2e0b4e8c742e22b68e28
  25. Author: CYBERDEViL <cyberdevil@notabug.org>
  26. Date: Tue Dec 19 18:07:17 2023 +0100
  27. ffmpeg: remove use of deprecated av_register_all()
  28. commit b234ee57030f620b7b5cee5f655cfcac27a5cb91
  29. Author: CYBERDEViL <cyberdevil@notabug.org>
  30. Date: Mon Dec 18 22:36:24 2023 +0100
  31. ffmpeg: "Fix building with latest versions of ffmpeg."
  32. Fully applied Blender upstream ref: 4e4a93bc454d93ec8523f44b73a42977e2868ecc
  33. diff --git a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp
  34. index e9eea19..f28fb80 100644
  35. --- a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp
  36. +++ b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.cpp
  37. @@ -38,37 +38,36 @@ extern "C" {
  38. #include <libavcodec/avcodec.h>
  39. #include <libavformat/avformat.h>
  40. #include <libavformat/avio.h>
  41. +#include <libavutil/avutil.h>
  42. #include "ffmpeg_compat.h"
  43. }
  44. +#if LIBAVCODEC_VERSION_MAJOR < 58
  45. +#define FFMPEG_OLD_CODE
  46. +#endif
  47. +
  48. int AUD_FFMPEGReader::decode(AVPacket& packet, AUD_Buffer& buffer)
  49. {
  50. -#ifdef FFMPEG_HAVE_DECODE_AUDIO4
  51. - AVFrame* frame = NULL;
  52. + int buf_size = buffer.getSize();
  53. + int buf_pos = 0;
  54. +
  55. +#ifdef FFMPEG_OLD_CODE
  56. int got_frame;
  57. int read_length;
  58. uint8_t* orig_data = packet.data;
  59. int orig_size = packet.size;
  60. - int buf_size = buffer.getSize();
  61. - int buf_pos = 0;
  62. -
  63. while(packet.size > 0)
  64. {
  65. got_frame = 0;
  66. - if(!frame)
  67. - frame = av_frame_alloc();
  68. - else
  69. - av_frame_unref(frame);
  70. -
  71. - read_length = avcodec_decode_audio4(m_codecCtx, frame, &got_frame, &packet);
  72. + read_length = avcodec_decode_audio4(m_codecCtx, m_frame, &got_frame, &packet);
  73. if(read_length < 0)
  74. break;
  75. if(got_frame)
  76. {
  77. - int data_size = av_samples_get_buffer_size(NULL, m_codecCtx->channels, frame->nb_samples, m_codecCtx->sample_fmt, 1);
  78. + int data_size = av_samples_get_buffer_size(nullptr, m_codecCtx->channels, m_frame->nb_samples, m_codecCtx->sample_fmt, 1);
  79. if(buf_size - buf_pos < data_size)
  80. {
  81. @@ -78,18 +77,18 @@ int AUD_FFMPEGReader::decode(AVPacket& packet, AUD_Buffer& buffer)
  82. if(m_tointerleave)
  83. {
  84. - int single_size = data_size / m_codecCtx->channels / frame->nb_samples;
  85. + int single_size = data_size / m_codecCtx->channels / m_frame->nb_samples;
  86. for(int channel = 0; channel < m_codecCtx->channels; channel++)
  87. {
  88. - for(int i = 0; i < frame->nb_samples; i++)
  89. + for(int i = 0; i < m_frame->nb_samples; i++)
  90. {
  91. memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((m_codecCtx->channels * i) + channel) * single_size,
  92. - frame->data[channel] + i * single_size, single_size);
  93. + m_frame->data[channel] + i * single_size, single_size);
  94. }
  95. }
  96. }
  97. else
  98. - memcpy(((data_t*)buffer.getBuffer()) + buf_pos, frame->data[0], data_size);
  99. + memcpy(((data_t*)buffer.getBuffer()) + buf_pos, m_frame->data[0], data_size);
  100. buf_pos += data_size;
  101. }
  102. @@ -99,57 +98,44 @@ int AUD_FFMPEGReader::decode(AVPacket& packet, AUD_Buffer& buffer)
  103. packet.data = orig_data;
  104. packet.size = orig_size;
  105. - av_free(frame);
  106. -
  107. - return buf_pos;
  108. #else
  109. - // save packet parameters
  110. - uint8_t *audio_pkg_data = packet.data;
  111. - int audio_pkg_size = packet.size;
  112. -
  113. - int buf_size = buffer.getSize();
  114. - int buf_pos = 0;
  115. + avcodec_send_packet(m_codecCtx, &packet);
  116. - int read_length, data_size;
  117. + while(true)
  118. + {
  119. + auto ret = avcodec_receive_frame(m_codecCtx, m_frame);
  120. - AVPacket tmp_pkt;
  121. + if(ret != 0)
  122. + break;
  123. - av_init_packet(&tmp_pkt);
  124. + int data_size = av_samples_get_buffer_size(nullptr, m_codecCtx->channels, m_frame->nb_samples, m_codecCtx->sample_fmt, 1);
  125. - // as long as there is still data in the package
  126. - while(audio_pkg_size > 0)
  127. - {
  128. - // resize buffer if needed
  129. - if(buf_size - buf_pos < AVCODEC_MAX_AUDIO_FRAME_SIZE)
  130. + if(buf_size - buf_pos < data_size)
  131. {
  132. - buffer.resize(buf_size + AVCODEC_MAX_AUDIO_FRAME_SIZE, true);
  133. - buf_size += AVCODEC_MAX_AUDIO_FRAME_SIZE;
  134. + buffer.resize(buf_size + data_size, true);
  135. + buf_size += data_size;
  136. }
  137. - // read samples from the packet
  138. - data_size = buf_size - buf_pos;
  139. -
  140. - tmp_pkt.data = audio_pkg_data;
  141. - tmp_pkt.size = audio_pkg_size;
  142. -
  143. - read_length = avcodec_decode_audio3(
  144. - m_codecCtx,
  145. - (int16_t*)(((data_t*)buffer.getBuffer()) + buf_pos),
  146. - &data_size, &tmp_pkt);
  147. -
  148. - // read error, next packet!
  149. - if(read_length < 0)
  150. - break;
  151. + if(m_tointerleave)
  152. + {
  153. + int single_size = data_size / m_codecCtx->channels / m_frame->nb_samples;
  154. + for(int channel = 0; channel < m_codecCtx->channels; channel++)
  155. + {
  156. + for(int i = 0; i < m_frame->nb_samples; i++)
  157. + {
  158. + std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((m_codecCtx->channels * i) + channel) * single_size,
  159. + m_frame->data[channel] + i * single_size, single_size);
  160. + }
  161. + }
  162. + }
  163. + else
  164. + std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos, m_frame->data[0], data_size);
  165. buf_pos += data_size;
  166. -
  167. - // move packet parameters
  168. - audio_pkg_data += read_length;
  169. - audio_pkg_size -= read_length;
  170. }
  171. +#endif
  172. return buf_pos;
  173. -#endif
  174. }
  175. static const char* streaminfo_error = "AUD_FFMPEGReader: Stream info couldn't "
  176. @@ -176,7 +162,11 @@ void AUD_FFMPEGReader::init()
  177. for(unsigned int i = 0; i < m_formatCtx->nb_streams; i++)
  178. {
  179. +#ifdef FFMPEG_OLD_CODE
  180. if((m_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  181. +#else
  182. + if((m_formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  183. +#endif
  184. && (m_stream < 0))
  185. {
  186. m_stream=i;
  187. @@ -187,12 +177,35 @@ void AUD_FFMPEGReader::init()
  188. if(m_stream == -1)
  189. AUD_THROW(AUD_ERROR_FFMPEG, noaudio_error);
  190. - m_codecCtx = m_formatCtx->streams[m_stream]->codec;
  191. -
  192. // get a decoder and open it
  193. - AVCodec *aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
  194. - if(!aCodec)
  195. +#ifndef FFMPEG_OLD_CODE
  196. + const AVCodec* aCodec = avcodec_find_decoder(m_formatCtx->streams[m_stream]->codecpar->codec_id);
  197. +
  198. + if(!aCodec) {
  199. AUD_THROW(AUD_ERROR_FFMPEG, nodecoder_error);
  200. + }
  201. +#endif
  202. +
  203. + m_frame = av_frame_alloc();
  204. +
  205. + if(!m_frame)
  206. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be read, ffmpeg frame couldn't be allocated.");
  207. +
  208. +#ifdef FFMPEG_OLD_CODE
  209. + m_codecCtx = m_formatCtx->streams[m_stream]->codec;
  210. +
  211. + AVCodec* aCodec = avcodec_find_decoder(m_codecCtx->codec_id);
  212. +#else
  213. + m_codecCtx = avcodec_alloc_context3(aCodec);
  214. +#endif
  215. +
  216. + if(!m_codecCtx)
  217. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be read, ffmpeg context couldn't be allocated.");
  218. +
  219. +#ifndef FFMPEG_OLD_CODE
  220. + if(avcodec_parameters_to_context(m_codecCtx, m_formatCtx->streams[m_stream]->codecpar) < 0)
  221. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be read, ffmpeg decoder parameters couldn't be copied to decoder context.");
  222. +#endif
  223. if(avcodec_open2(m_codecCtx, aCodec, NULL) < 0)
  224. AUD_THROW(AUD_ERROR_FFMPEG, codecopen_error);
  225. @@ -236,8 +249,10 @@ static const char* fileopen_error = "AUD_FFMPEGReader: File couldn't be "
  226. "opened.";
  227. AUD_FFMPEGReader::AUD_FFMPEGReader(std::string filename) :
  228. - m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
  229. + m_pkgbuf(),
  230. m_formatCtx(NULL),
  231. + m_codecCtx(nullptr),
  232. + m_frame(nullptr),
  233. m_aviocontext(NULL),
  234. m_membuf(NULL)
  235. {
  236. @@ -260,13 +275,15 @@ static const char* streamopen_error = "AUD_FFMPEGReader: Stream couldn't be "
  237. "opened.";
  238. AUD_FFMPEGReader::AUD_FFMPEGReader(boost::shared_ptr<AUD_Buffer> buffer) :
  239. - m_pkgbuf(AVCODEC_MAX_AUDIO_FRAME_SIZE<<1),
  240. + m_pkgbuf(),
  241. + m_codecCtx(nullptr),
  242. + m_frame(nullptr),
  243. m_membuffer(buffer),
  244. m_membufferpos(0)
  245. {
  246. - m_membuf = reinterpret_cast<data_t*>(av_malloc(FF_MIN_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE));
  247. + m_membuf = reinterpret_cast<data_t*>(av_malloc(AV_INPUT_BUFFER_MIN_SIZE + AV_INPUT_BUFFER_PADDING_SIZE));
  248. - m_aviocontext = avio_alloc_context(m_membuf, FF_MIN_BUFFER_SIZE, 0, this,
  249. + m_aviocontext = avio_alloc_context(m_membuf, AV_INPUT_BUFFER_MIN_SIZE, 0, this,
  250. read_packet, NULL, seek_packet);
  251. if(!m_aviocontext)
  252. @@ -297,7 +314,14 @@ AUD_FFMPEGReader::AUD_FFMPEGReader(boost::shared_ptr<AUD_Buffer> buffer) :
  253. AUD_FFMPEGReader::~AUD_FFMPEGReader()
  254. {
  255. + if(m_frame)
  256. + av_frame_free(&m_frame);
  257. +#ifdef FFMPEG_OLD_CODE
  258. avcodec_close(m_codecCtx);
  259. +#else
  260. + if(m_codecCtx)
  261. + avcodec_free_context(&m_codecCtx);
  262. +#endif
  263. avformat_close_input(&m_formatCtx);
  264. }
  265. @@ -398,7 +422,7 @@ void AUD_FFMPEGReader::seek(int position)
  266. }
  267. }
  268. }
  269. - av_free_packet(&packet);
  270. + av_packet_unref(&packet);
  271. }
  272. }
  273. else
  274. @@ -429,7 +453,7 @@ AUD_Specs AUD_FFMPEGReader::getSpecs() const
  275. void AUD_FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
  276. {
  277. // read packages and decode them
  278. - AVPacket packet;
  279. + AVPacket packet = {};
  280. int data_size = 0;
  281. int pkgbuf_pos;
  282. int left = length;
  283. @@ -446,7 +470,7 @@ void AUD_FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
  284. m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(),
  285. data_size / AUD_FORMAT_SIZE(m_specs.format));
  286. buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
  287. - left -= data_size/sample_size;
  288. + left -= data_size / sample_size;
  289. }
  290. // for each frame read as long as there isn't enough data already
  291. @@ -463,9 +487,9 @@ void AUD_FFMPEGReader::read(int& length, bool& eos, sample_t* buffer)
  292. m_convert((data_t*) buf, (data_t*) m_pkgbuf.getBuffer(),
  293. data_size / AUD_FORMAT_SIZE(m_specs.format));
  294. buf += data_size / AUD_FORMAT_SIZE(m_specs.format);
  295. - left -= data_size/sample_size;
  296. + left -= data_size / sample_size;
  297. }
  298. - av_free_packet(&packet);
  299. + av_packet_unref(&packet);
  300. }
  301. // read more data than necessary?
  302. if(pkgbuf_pos > data_size)
  303. diff --git a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h
  304. index 377086e..a86be99 100644
  305. --- a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h
  306. +++ b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGReader.h
  307. @@ -81,6 +81,11 @@ private:
  308. */
  309. AVCodecContext* m_codecCtx;
  310. + /**
  311. + * The AVFrame structure for using ffmpeg.
  312. + */
  313. + AVFrame* m_frame;
  314. +
  315. /**
  316. * The AVIOContext to read the data from.
  317. */
  318. @@ -129,9 +134,9 @@ private:
  319. */
  320. void init();
  321. - // hide copy constructor and operator=
  322. - AUD_FFMPEGReader(const AUD_FFMPEGReader&);
  323. - AUD_FFMPEGReader& operator=(const AUD_FFMPEGReader&);
  324. + // delete copy constructor and operator=
  325. + AUD_FFMPEGReader(const AUD_FFMPEGReader&) = delete;
  326. + AUD_FFMPEGReader& operator=(const AUD_FFMPEGReader&) = delete;
  327. public:
  328. /**
  329. diff --git a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.cpp b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.cpp
  330. index 3f95ac7..bde90a6 100644
  331. --- a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.cpp
  332. +++ b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.cpp
  333. @@ -36,11 +36,15 @@
  334. extern "C" {
  335. #include <libavcodec/avcodec.h>
  336. -#include <libavformat/avformat.h>
  337. #include <libavformat/avio.h>
  338. +#include <libavutil/channel_layout.h>
  339. #include "ffmpeg_compat.h"
  340. }
  341. +#if LIBAVCODEC_VERSION_MAJOR < 58
  342. +#define FFMPEG_OLD_CODE
  343. +#endif
  344. +
  345. static const char* context_error = "AUD_FFMPEGWriter: Couldn't allocate context.";
  346. static const char* codec_error = "AUD_FFMPEGWriter: Invalid codec or codec not found.";
  347. static const char* stream_error = "AUD_FFMPEGWriter: Couldn't allocate stream.";
  348. @@ -51,88 +55,135 @@ static const char* write_error = "AUD_FFMPEGWriter: Error writing packet.";
  349. AUD_FFMPEGWriter::AUD_FFMPEGWriter(std::string filename, AUD_DeviceSpecs specs, AUD_Container format, AUD_Codec codec, unsigned int bitrate) :
  350. m_position(0),
  351. m_specs(specs),
  352. + m_formatCtx(nullptr),
  353. + m_codecCtx(nullptr),
  354. + m_stream(nullptr),
  355. + m_packet(nullptr),
  356. + m_frame(nullptr),
  357. + m_deinterleave(false),
  358. m_input_samples(0)
  359. {
  360. - static const char* formats[] = { NULL, "ac3", "flac", "matroska", "mp2", "mp3", "ogg", "wav" };
  361. + static const char* formats[] = { nullptr, "ac3", "flac", "matroska", "mp2", "mp3", "ogg", "wav" };
  362. - m_formatCtx = avformat_alloc_context();
  363. - if (!m_formatCtx) AUD_THROW(AUD_ERROR_FFMPEG, context_error);
  364. + if(avformat_alloc_output_context2(&m_formatCtx, nullptr, formats[format], filename.c_str()) < 0)
  365. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, format couldn't be found with ffmpeg.");
  366. - strcpy(m_formatCtx->filename, filename.c_str());
  367. - m_outputFmt = m_formatCtx->oformat = av_guess_format(formats[format], filename.c_str(), NULL);
  368. - if (!m_outputFmt) {
  369. + const AVOutputFormat* outputFmt = m_formatCtx->oformat;
  370. +
  371. + if(!outputFmt) {
  372. avformat_free_context(m_formatCtx);
  373. - AUD_THROW(AUD_ERROR_FFMPEG, context_error);
  374. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, output format couldn't be found with ffmpeg.");
  375. }
  376. + AVCodecID audio_codec = AV_CODEC_ID_NONE;
  377. +
  378. switch(codec)
  379. {
  380. case AUD_CODEC_AAC:
  381. - m_outputFmt->audio_codec = AV_CODEC_ID_AAC;
  382. + audio_codec = AV_CODEC_ID_AAC;
  383. break;
  384. case AUD_CODEC_AC3:
  385. - m_outputFmt->audio_codec = AV_CODEC_ID_AC3;
  386. + audio_codec = AV_CODEC_ID_AC3;
  387. break;
  388. case AUD_CODEC_FLAC:
  389. - m_outputFmt->audio_codec = AV_CODEC_ID_FLAC;
  390. + audio_codec = AV_CODEC_ID_FLAC;
  391. break;
  392. case AUD_CODEC_MP2:
  393. - m_outputFmt->audio_codec = AV_CODEC_ID_MP2;
  394. + audio_codec = AV_CODEC_ID_MP2;
  395. break;
  396. case AUD_CODEC_MP3:
  397. - m_outputFmt->audio_codec = AV_CODEC_ID_MP3;
  398. + audio_codec = AV_CODEC_ID_MP3;
  399. + break;
  400. + case AUD_CODEC_OPUS:
  401. + audio_codec = AV_CODEC_ID_OPUS;
  402. break;
  403. case AUD_CODEC_PCM:
  404. switch(specs.format)
  405. {
  406. case AUD_FORMAT_U8:
  407. - m_outputFmt->audio_codec = AV_CODEC_ID_PCM_U8;
  408. + audio_codec = AV_CODEC_ID_PCM_U8;
  409. break;
  410. case AUD_FORMAT_S16:
  411. - m_outputFmt->audio_codec = AV_CODEC_ID_PCM_S16LE;
  412. + audio_codec = AV_CODEC_ID_PCM_S16LE;
  413. break;
  414. case AUD_FORMAT_S24:
  415. - m_outputFmt->audio_codec = AV_CODEC_ID_PCM_S24LE;
  416. + audio_codec = AV_CODEC_ID_PCM_S24LE;
  417. break;
  418. case AUD_FORMAT_S32:
  419. - m_outputFmt->audio_codec = AV_CODEC_ID_PCM_S32LE;
  420. + audio_codec = AV_CODEC_ID_PCM_S32LE;
  421. break;
  422. case AUD_FORMAT_FLOAT32:
  423. - m_outputFmt->audio_codec = AV_CODEC_ID_PCM_F32LE;
  424. + audio_codec = AV_CODEC_ID_PCM_F32LE;
  425. break;
  426. case AUD_FORMAT_FLOAT64:
  427. - m_outputFmt->audio_codec = AV_CODEC_ID_PCM_F64LE;
  428. + audio_codec = AV_CODEC_ID_PCM_F64LE;
  429. break;
  430. default:
  431. - m_outputFmt->audio_codec = AV_CODEC_ID_NONE;
  432. + audio_codec = AV_CODEC_ID_NONE;
  433. break;
  434. }
  435. break;
  436. case AUD_CODEC_VORBIS:
  437. - m_outputFmt->audio_codec = AV_CODEC_ID_VORBIS;
  438. + audio_codec = AV_CODEC_ID_VORBIS;
  439. break;
  440. default:
  441. - m_outputFmt->audio_codec = AV_CODEC_ID_NONE;
  442. + audio_codec = AV_CODEC_ID_NONE;
  443. + break;
  444. + }
  445. +
  446. + uint64_t channel_layout = 0;
  447. +
  448. + switch(m_specs.channels)
  449. + {
  450. + case AUD_CHANNELS_MONO:
  451. + channel_layout = AV_CH_LAYOUT_MONO;
  452. + break;
  453. + case AUD_CHANNELS_STEREO:
  454. + channel_layout = AV_CH_LAYOUT_STEREO;
  455. + break;
  456. + case AUD_CHANNELS_STEREO_LFE:
  457. + channel_layout = AV_CH_LAYOUT_2POINT1;
  458. + break;
  459. + case AUD_CHANNELS_SURROUND4:
  460. + channel_layout = AV_CH_LAYOUT_QUAD;
  461. + break;
  462. + case AUD_CHANNELS_SURROUND5:
  463. + channel_layout = AV_CH_LAYOUT_5POINT0_BACK;
  464. + break;
  465. + case AUD_CHANNELS_SURROUND51:
  466. + channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
  467. + break;
  468. + case AUD_CHANNELS_SURROUND61:
  469. + channel_layout = AV_CH_LAYOUT_6POINT1_BACK;
  470. + break;
  471. + case AUD_CHANNELS_SURROUND71:
  472. + channel_layout = AV_CH_LAYOUT_7POINT1;
  473. break;
  474. }
  475. try
  476. {
  477. - if(m_outputFmt->audio_codec == AV_CODEC_ID_NONE)
  478. - AUD_THROW(AUD_ERROR_SPECS, codec_error);
  479. + if(audio_codec == AV_CODEC_ID_NONE)
  480. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, audio codec not found with ffmpeg.");
  481. +
  482. + const AVCodec* codec = avcodec_find_encoder(audio_codec);
  483. + if(!codec)
  484. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, audio encoder couldn't be found with ffmpeg.");
  485. - m_stream = avformat_new_stream(m_formatCtx, NULL);
  486. + m_stream = avformat_new_stream(m_formatCtx, codec);
  487. if(!m_stream)
  488. - AUD_THROW(AUD_ERROR_FFMPEG, stream_error);
  489. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, stream creation failed with ffmpeg.");
  490. + m_stream->id = m_formatCtx->nb_streams - 1;
  491. +
  492. +#ifdef FFMPEG_OLD_CODE
  493. m_codecCtx = m_stream->codec;
  494. - m_codecCtx->codec_id = m_outputFmt->audio_codec;
  495. - m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
  496. - m_codecCtx->bit_rate = bitrate;
  497. - m_codecCtx->sample_rate = int(m_specs.rate);
  498. - m_codecCtx->channels = m_specs.channels;
  499. - m_codecCtx->time_base.num = 1;
  500. - m_codecCtx->time_base.den = m_codecCtx->sample_rate;
  501. +#else
  502. + m_codecCtx = avcodec_alloc_context3(codec);
  503. +#endif
  504. +
  505. + if(!m_codecCtx)
  506. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, context creation failed with ffmpeg.");
  507. switch(m_specs.format)
  508. {
  509. @@ -148,133 +199,182 @@ AUD_FFMPEGWriter::AUD_FFMPEGWriter(std::string filename, AUD_DeviceSpecs specs,
  510. m_convert = AUD_convert_float_s32;
  511. m_codecCtx->sample_fmt = AV_SAMPLE_FMT_S32;
  512. break;
  513. - case AUD_FORMAT_FLOAT32:
  514. - m_convert = AUD_convert_copy<float>;
  515. - m_codecCtx->sample_fmt = AV_SAMPLE_FMT_FLT;
  516. - break;
  517. case AUD_FORMAT_FLOAT64:
  518. m_convert = AUD_convert_float_double;
  519. m_codecCtx->sample_fmt = AV_SAMPLE_FMT_DBL;
  520. break;
  521. default:
  522. - AUD_THROW(AUD_ERROR_FFMPEG, format_error);
  523. + m_convert = AUD_convert_copy<sample_t>;
  524. + m_codecCtx->sample_fmt = AV_SAMPLE_FMT_FLT;
  525. + break;
  526. }
  527. - try
  528. - {
  529. - if(m_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
  530. - m_codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  531. -
  532. - AVCodec* codec = avcodec_find_encoder(m_codecCtx->codec_id);
  533. - if(!codec)
  534. - AUD_THROW(AUD_ERROR_FFMPEG, codec_error);
  535. -
  536. - if(codec->sample_fmts) {
  537. - // Check if the preferred sample format for this codec is supported.
  538. - const enum AVSampleFormat *p = codec->sample_fmts;
  539. - for(; *p != -1; p++) {
  540. - if(*p == m_stream->codec->sample_fmt)
  541. - break;
  542. - }
  543. - if(*p == -1) {
  544. - // Sample format incompatible with codec. Defaulting to a format known to work.
  545. - m_stream->codec->sample_fmt = codec->sample_fmts[0];
  546. - }
  547. - }
  548. -
  549. - if(avcodec_open2(m_codecCtx, codec, NULL))
  550. - AUD_THROW(AUD_ERROR_FFMPEG, codec_error);
  551. + if(m_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
  552. + m_codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  553. - m_output_buffer.resize(FF_MIN_BUFFER_SIZE);
  554. - int samplesize = AUD_MAX(AUD_SAMPLE_SIZE(m_specs), AUD_DEVICE_SAMPLE_SIZE(m_specs));
  555. + bool format_supported = false;
  556. - if(m_codecCtx->frame_size <= 1) {
  557. - m_input_size = FF_MIN_BUFFER_SIZE * 8 / m_codecCtx->bits_per_coded_sample / m_codecCtx->channels;
  558. - m_input_buffer.resize(m_input_size * samplesize);
  559. - }
  560. - else
  561. + for(int i = 0; codec->sample_fmts[i] != -1; i++)
  562. + {
  563. + if(av_get_alt_sample_fmt(codec->sample_fmts[i], false) == m_codecCtx->sample_fmt)
  564. {
  565. - m_input_buffer.resize(m_codecCtx->frame_size * samplesize);
  566. - m_input_size = m_codecCtx->frame_size;
  567. + m_deinterleave = av_sample_fmt_is_planar(codec->sample_fmts[i]);
  568. + m_codecCtx->sample_fmt = codec->sample_fmts[i];
  569. + format_supported = true;
  570. }
  571. + }
  572. -#ifdef FFMPEG_HAVE_ENCODE_AUDIO2
  573. - m_frame = av_frame_alloc();
  574. - if (!m_frame)
  575. - AUD_THROW(AUD_ERROR_FFMPEG, codec_error);
  576. - av_frame_unref(m_frame);
  577. - m_frame->linesize[0] = m_input_size * samplesize;
  578. - m_frame->format = m_codecCtx->sample_fmt;
  579. - m_frame->nb_samples = m_input_size;
  580. -# ifdef FFMPEG_HAVE_AVFRAME_SAMPLE_RATE
  581. - m_frame->sample_rate = m_codecCtx->sample_rate;
  582. -# endif
  583. -# ifdef FFMPEG_HAVE_FRAME_CHANNEL_LAYOUT
  584. - m_frame->channel_layout = m_codecCtx->channel_layout;
  585. -# endif
  586. - m_sample_size = av_get_bytes_per_sample(m_codecCtx->sample_fmt);
  587. - m_frame_pts = 0;
  588. - m_deinterleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
  589. - if(m_deinterleave)
  590. - m_deinterleave_buffer.resize(m_input_size * m_codecCtx->channels * m_sample_size);
  591. -#endif
  592. -
  593. - try
  594. + if(!format_supported)
  595. + {
  596. + int chosen_index = 0;
  597. + auto chosen = av_get_alt_sample_fmt(codec->sample_fmts[chosen_index], false);
  598. + for(int i = 1; codec->sample_fmts[i] != -1; i++)
  599. {
  600. - if(avio_open(&m_formatCtx->pb, filename.c_str(), AVIO_FLAG_WRITE))
  601. - AUD_THROW(AUD_ERROR_FILE, file_error);
  602. -
  603. - if(avformat_write_header(m_formatCtx, NULL) < 0) {
  604. - throw;
  605. + auto fmt = av_get_alt_sample_fmt(codec->sample_fmts[i], false);
  606. + if((fmt > chosen && chosen < m_codecCtx->sample_fmt) || (fmt > m_codecCtx->sample_fmt && fmt < chosen))
  607. + {
  608. + chosen = fmt;
  609. + chosen_index = i;
  610. }
  611. }
  612. - catch(AUD_Exception&)
  613. +
  614. + m_codecCtx->sample_fmt = codec->sample_fmts[chosen_index];
  615. + m_deinterleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
  616. + switch(av_get_alt_sample_fmt(m_codecCtx->sample_fmt, false))
  617. {
  618. - avcodec_close(m_codecCtx);
  619. - av_freep(&m_formatCtx->streams[0]->codec);
  620. - throw;
  621. + case AV_SAMPLE_FMT_U8:
  622. + specs.format = AUD_FORMAT_U8;
  623. + m_convert = AUD_convert_float_u8;
  624. + break;
  625. + case AV_SAMPLE_FMT_S16:
  626. + specs.format = AUD_FORMAT_S16;
  627. + m_convert = AUD_convert_float_s16;
  628. + break;
  629. + case AV_SAMPLE_FMT_S32:
  630. + specs.format = AUD_FORMAT_S32;
  631. + m_convert = AUD_convert_float_s32;
  632. + break;
  633. + case AV_SAMPLE_FMT_FLT:
  634. + specs.format = AUD_FORMAT_FLOAT32;
  635. + m_convert = AUD_convert_copy<sample_t>;
  636. + break;
  637. + case AV_SAMPLE_FMT_DBL:
  638. + specs.format = AUD_FORMAT_FLOAT64;
  639. + m_convert = AUD_convert_float_double;
  640. + break;
  641. + default:
  642. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, sample format not supported with ffmpeg.");
  643. }
  644. }
  645. - catch(AUD_Exception&)
  646. +
  647. + m_codecCtx->sample_rate = 0;
  648. +
  649. + if(codec->supported_samplerates)
  650. {
  651. - av_freep(&m_formatCtx->streams[0]);
  652. - throw;
  653. + for(int i = 0; codec->supported_samplerates[i]; i++)
  654. + {
  655. + if(codec->supported_samplerates[i] == m_specs.rate)
  656. + {
  657. + m_codecCtx->sample_rate = codec->supported_samplerates[i];
  658. + break;
  659. + }
  660. + else if((codec->supported_samplerates[i] > m_codecCtx->sample_rate && m_specs.rate > m_codecCtx->sample_rate) ||
  661. + (codec->supported_samplerates[i] < m_codecCtx->sample_rate && m_specs.rate < codec->supported_samplerates[i]))
  662. + {
  663. + m_codecCtx->sample_rate = codec->supported_samplerates[i];
  664. + }
  665. + }
  666. }
  667. +
  668. + if(m_codecCtx->sample_rate == 0)
  669. + m_codecCtx->sample_rate = m_specs.rate;
  670. +
  671. + m_specs.rate = m_codecCtx->sample_rate;
  672. +
  673. +#ifdef FFMPEG_OLD_CODE
  674. + m_codecCtx->codec_id = audio_codec;
  675. +#endif
  676. +
  677. + m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
  678. + m_codecCtx->bit_rate = bitrate;
  679. + m_codecCtx->channel_layout = channel_layout;
  680. + m_codecCtx->channels = m_specs.channels;
  681. + m_stream->time_base.num = m_codecCtx->time_base.num = 1;
  682. + m_stream->time_base.den = m_codecCtx->time_base.den = m_codecCtx->sample_rate;
  683. +
  684. + if(avcodec_open2(m_codecCtx, codec, nullptr) < 0)
  685. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, encoder couldn't be opened with ffmpeg.");
  686. +
  687. +#ifndef FFMPEG_OLD_CODE
  688. + if(avcodec_parameters_from_context(m_stream->codecpar, m_codecCtx) < 0)
  689. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, codec parameters couldn't be copied to the context.");
  690. +#endif
  691. +
  692. + int samplesize = std::max(int(AUD_SAMPLE_SIZE(m_specs)), AUD_DEVICE_SAMPLE_SIZE(m_specs));
  693. +
  694. + if((m_input_size = m_codecCtx->frame_size))
  695. + m_input_buffer.resize(m_input_size * samplesize);
  696. +
  697. + if(avio_open(&m_formatCtx->pb, filename.c_str(), AVIO_FLAG_WRITE))
  698. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, file opening failed with ffmpeg.");
  699. +
  700. + if(avformat_write_header(m_formatCtx, nullptr) < 0)
  701. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, writing the header failed.");
  702. }
  703. catch(AUD_Exception&)
  704. {
  705. - av_free(m_formatCtx);
  706. +#ifndef FFMPEG_OLD_CODE
  707. + if(m_codecCtx)
  708. + avcodec_free_context(&m_codecCtx);
  709. +#endif
  710. + avformat_free_context(m_formatCtx);
  711. throw;
  712. }
  713. +
  714. +#ifdef FFMPEG_OLD_CODE
  715. + m_packet = new AVPacket({});
  716. +#else
  717. + m_packet = av_packet_alloc();
  718. +#endif
  719. +
  720. + m_frame = av_frame_alloc();
  721. }
  722. AUD_FFMPEGWriter::~AUD_FFMPEGWriter()
  723. {
  724. // writte missing data
  725. if(m_input_samples)
  726. - {
  727. - sample_t* buf = m_input_buffer.getBuffer();
  728. - memset(buf + m_specs.channels * m_input_samples, 0,
  729. - (m_input_size - m_input_samples) * AUD_DEVICE_SAMPLE_SIZE(m_specs));
  730. + encode();
  731. - encode(buf);
  732. - }
  733. + close();
  734. av_write_trailer(m_formatCtx);
  735. - avcodec_close(m_codecCtx);
  736. + if(m_frame)
  737. + av_frame_free(&m_frame);
  738. - av_freep(&m_formatCtx->streams[0]->codec);
  739. - av_freep(&m_formatCtx->streams[0]);
  740. + if(m_packet)
  741. + {
  742. +#ifdef FFMPEG_OLD_CODE
  743. + delete m_packet;
  744. +#else
  745. + av_packet_free(&m_packet);
  746. +#endif
  747. + }
  748. -#ifdef FFMPEG_HAVE_ENCODE_AUDIO2
  749. - av_frame_free(&m_frame);
  750. +#ifdef FFMPEG_OLD_CODE
  751. + avcodec_close(m_codecCtx);
  752. +#else
  753. + if(m_codecCtx)
  754. + avcodec_free_context(&m_codecCtx);
  755. #endif
  756. - avio_close(m_formatCtx->pb);
  757. - av_free(m_formatCtx);
  758. + avio_closep(&m_formatCtx->pb);
  759. + avformat_free_context(m_formatCtx);
  760. }
  761. +
  762. +
  763. int AUD_FFMPEGWriter::getPosition() const
  764. {
  765. return m_position;
  766. @@ -285,72 +385,130 @@ AUD_DeviceSpecs AUD_FFMPEGWriter::getSpecs() const
  767. return m_specs;
  768. }
  769. -void AUD_FFMPEGWriter::encode(sample_t* data)
  770. +void AUD_FFMPEGWriter::encode()
  771. {
  772. - // convert first
  773. - if(m_input_size)
  774. - m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(data), m_input_size * m_specs.channels);
  775. + sample_t* data = m_input_buffer.getBuffer();
  776. - AVPacket packet = { 0 };
  777. - av_init_packet(&packet);
  778. -
  779. -#ifdef FFMPEG_HAVE_ENCODE_AUDIO2
  780. - int got_output, ret;
  781. - m_frame->pts = m_frame_pts / av_q2d(m_codecCtx->time_base);
  782. - m_frame_pts++;
  783. -#ifdef FFMPEG_HAVE_FRAME_CHANNEL_LAYOUT
  784. - m_frame->channel_layout = m_codecCtx->channel_layout;
  785. -#endif
  786. + if(m_deinterleave)
  787. + {
  788. + m_deinterleave_buffer.assureSize(m_input_buffer.getSize());
  789. - if(m_deinterleave) {
  790. - for(int channel = 0; channel < m_codecCtx->channels; channel++) {
  791. - for(int i = 0; i < m_frame->nb_samples; i++) {
  792. - memcpy(reinterpret_cast<uint8_t*>(m_deinterleave_buffer.getBuffer()) + (i + channel * m_frame->nb_samples) * m_sample_size,
  793. - reinterpret_cast<uint8_t*>(data) + (m_codecCtx->channels * i + channel) * m_sample_size, m_sample_size);
  794. + sample_t* dbuf = m_deinterleave_buffer.getBuffer();
  795. + // deinterleave
  796. + int single_size = sizeof(sample_t);
  797. + for(int channel = 0; channel < m_specs.channels; channel++)
  798. + {
  799. + for(int i = 0; i < m_input_buffer.getSize() / AUD_SAMPLE_SIZE(m_specs); i++)
  800. + {
  801. + std::memcpy(((data_t*)dbuf) + (m_input_samples * channel + i) * single_size,
  802. + ((data_t*)data) + ((m_specs.channels * i) + channel) * single_size, single_size);
  803. }
  804. }
  805. - data = m_deinterleave_buffer.getBuffer();
  806. + // convert first
  807. + if(m_input_size)
  808. + m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(dbuf), m_input_samples * m_specs.channels);
  809. + else
  810. + std::memcpy(data, dbuf, m_input_buffer.getSize());
  811. }
  812. + else
  813. + // convert first
  814. + if(m_input_size)
  815. + m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(data), m_input_samples * m_specs.channels);
  816. - avcodec_fill_audio_frame(m_frame, m_codecCtx->channels, m_codecCtx->sample_fmt, reinterpret_cast<uint8_t*>(data),
  817. - m_frame->nb_samples * av_get_bytes_per_sample(m_codecCtx->sample_fmt) * m_codecCtx->channels, 1);
  818. +#ifdef FFMPEG_OLD_CODE
  819. + m_packet->data = nullptr;
  820. + m_packet->size = 0;
  821. - ret = avcodec_encode_audio2(m_codecCtx, &packet, m_frame, &got_output);
  822. - if(ret < 0)
  823. - AUD_THROW(AUD_ERROR_FFMPEG, codec_error);
  824. + av_init_packet(m_packet);
  825. - if(!got_output)
  826. - return;
  827. + av_frame_unref(m_frame);
  828. + int got_packet;
  829. +#endif
  830. +
  831. + m_frame->nb_samples = m_input_samples;
  832. + m_frame->format = m_codecCtx->sample_fmt;
  833. + m_frame->channel_layout = m_codecCtx->channel_layout;
  834. +
  835. + if(avcodec_fill_audio_frame(m_frame, m_specs.channels, m_codecCtx->sample_fmt, reinterpret_cast<data_t*>(data), m_input_buffer.getSize(), 0) < 0)
  836. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, filling the audio frame failed with ffmpeg.");
  837. +
  838. + AVRational sample_time = { 1, static_cast<int>(m_specs.rate) };
  839. + m_frame->pts = av_rescale_q(m_position - m_input_samples, m_codecCtx->time_base, sample_time);
  840. +
  841. +#ifdef FFMPEG_OLD_CODE
  842. + if(avcodec_encode_audio2(m_codecCtx, m_packet, m_frame, &got_packet))
  843. + {
  844. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, audio encoding failed with ffmpeg.");
  845. + }
  846. +
  847. + if(got_packet)
  848. + {
  849. + m_packet->flags |= AV_PKT_FLAG_KEY;
  850. + m_packet->stream_index = m_stream->index;
  851. + if(av_write_frame(m_formatCtx, m_packet) < 0)
  852. + {
  853. + av_free_packet(m_packet);
  854. + AUD_THROW(AUD_ERROR_FILE, "Frame couldn't be writen to the file with ffmpeg.");
  855. + }
  856. + av_free_packet(m_packet);
  857. + }
  858. #else
  859. - sample_t* outbuf = m_output_buffer.getBuffer();
  860. + if(avcodec_send_frame(m_codecCtx, m_frame) < 0)
  861. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, audio encoding failed with ffmpeg.");
  862. - packet.size = avcodec_encode_audio(m_codecCtx, reinterpret_cast<uint8_t*>(outbuf), m_output_buffer.getSize(), reinterpret_cast<short*>(data));
  863. - if(m_codecCtx->coded_frame && m_codecCtx->coded_frame->pts != AV_NOPTS_VALUE)
  864. - packet.pts = av_rescale_q(m_codecCtx->coded_frame->pts, m_codecCtx->time_base, m_stream->time_base);
  865. - packet.flags |= AV_PKT_FLAG_KEY;
  866. - packet.data = reinterpret_cast<uint8_t*>(outbuf);
  867. + while(avcodec_receive_packet(m_codecCtx, m_packet) == 0)
  868. + {
  869. + m_packet->stream_index = m_stream->index;
  870. +
  871. + if(av_write_frame(m_formatCtx, m_packet) < 0)
  872. + AUD_THROW(AUD_ERROR_FILE, "Frame couldn't be writen to the file with ffmpeg.");
  873. + }
  874. #endif
  875. +}
  876. +
  877. +void AUD_FFMPEGWriter::close()
  878. +{
  879. +#ifdef FFMPEG_OLD_CODE
  880. + int got_packet = true;
  881. - if(packet.pts != AV_NOPTS_VALUE)
  882. - packet.pts = av_rescale_q(packet.pts, m_codecCtx->time_base, m_stream->time_base);
  883. - if(packet.dts != AV_NOPTS_VALUE)
  884. - packet.dts = av_rescale_q(packet.dts, m_codecCtx->time_base, m_stream->time_base);
  885. - if(packet.duration > 0)
  886. - packet.duration = av_rescale_q(packet.duration, m_codecCtx->time_base, m_stream->time_base);
  887. + while(got_packet)
  888. + {
  889. + m_packet->data = nullptr;
  890. + m_packet->size = 0;
  891. - packet.stream_index = m_stream->index;
  892. + av_init_packet(m_packet);
  893. - packet.flags |= AV_PKT_FLAG_KEY;
  894. + if(avcodec_encode_audio2(m_codecCtx, m_packet, nullptr, &got_packet))
  895. + AUD_THROW(AUD_ERROR_FILE, "File end couldn't be written, audio encoding failed with ffmpeg.");
  896. - if(av_interleaved_write_frame(m_formatCtx, &packet)) {
  897. - av_free_packet(&packet);
  898. - AUD_THROW(AUD_ERROR_FFMPEG, write_error);
  899. + if(got_packet)
  900. + {
  901. + m_packet->flags |= AV_PKT_FLAG_KEY;
  902. + m_packet->stream_index = m_stream->index;
  903. + if(av_write_frame(m_formatCtx, m_packet))
  904. + {
  905. + av_free_packet(m_packet);
  906. + AUD_THROW(AUD_ERROR_FILE, "Final frames couldn't be writen to the file with ffmpeg.");
  907. + }
  908. + av_free_packet(m_packet);
  909. + }
  910. }
  911. +#else
  912. + if(avcodec_send_frame(m_codecCtx, nullptr) < 0)
  913. + AUD_THROW(AUD_ERROR_FILE, "File couldn't be written, audio encoding failed with ffmpeg.");
  914. +
  915. + while(avcodec_receive_packet(m_codecCtx, m_packet) == 0)
  916. + {
  917. + m_packet->stream_index = m_stream->index;
  918. - av_free_packet(&packet);
  919. + if(av_write_frame(m_formatCtx, m_packet) < 0)
  920. + AUD_THROW(AUD_ERROR_FILE, "Frame couldn't be writen to the file with ffmpeg.");
  921. + }
  922. +#endif
  923. }
  924. +
  925. void AUD_FFMPEGWriter::write(unsigned int length, sample_t* buffer)
  926. {
  927. unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
  928. @@ -361,9 +519,9 @@ void AUD_FFMPEGWriter::write(unsigned int length, sample_t* buffer)
  929. while(length)
  930. {
  931. - unsigned int len = AUD_MIN(m_input_size - m_input_samples, length);
  932. + unsigned int len = std::min(m_input_size - m_input_samples, length);
  933. - memcpy(inbuf + m_input_samples * m_specs.channels, buffer, len * samplesize);
  934. + std::memcpy(inbuf + m_input_samples * m_specs.channels, buffer, len * samplesize);
  935. buffer += len * m_specs.channels;
  936. m_input_samples += len;
  937. @@ -372,7 +530,7 @@ void AUD_FFMPEGWriter::write(unsigned int length, sample_t* buffer)
  938. if(m_input_samples == m_input_size)
  939. {
  940. - encode(inbuf);
  941. + encode();
  942. m_input_samples = 0;
  943. }
  944. @@ -381,15 +539,15 @@ void AUD_FFMPEGWriter::write(unsigned int length, sample_t* buffer)
  945. else // PCM data, can write directly!
  946. {
  947. int samplesize = AUD_SAMPLE_SIZE(m_specs);
  948. - if(m_output_buffer.getSize() != length * m_specs.channels * m_codecCtx->bits_per_coded_sample / 8)
  949. - m_output_buffer.resize(length * m_specs.channels * m_codecCtx->bits_per_coded_sample / 8);
  950. - m_input_buffer.assureSize(length * AUD_MAX(AUD_DEVICE_SAMPLE_SIZE(m_specs), samplesize));
  951. + m_input_buffer.assureSize(length * std::max(AUD_DEVICE_SAMPLE_SIZE(m_specs), samplesize));
  952. sample_t* buf = m_input_buffer.getBuffer();
  953. m_convert(reinterpret_cast<data_t*>(buf), reinterpret_cast<data_t*>(buffer), length * m_specs.channels);
  954. - encode(buf);
  955. + m_input_samples = length;
  956. m_position += length;
  957. +
  958. + encode();
  959. }
  960. }
  961. diff --git a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.h b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.h
  962. index 492aa35..a77d250 100644
  963. --- a/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.h
  964. +++ b/blender-2.79b/intern/audaspace/ffmpeg/AUD_FFMPEGWriter.h
  965. @@ -68,19 +68,19 @@ private:
  966. AVCodecContext* m_codecCtx;
  967. /**
  968. - * The AVOutputFormat structure for using ffmpeg.
  969. + * The AVStream structure for using ffmpeg.
  970. */
  971. - AVOutputFormat* m_outputFmt;
  972. + AVStream* m_stream;
  973. /**
  974. - * The AVStream structure for using ffmpeg.
  975. + * The AVPacket structure for using ffmpeg.
  976. */
  977. - AVStream* m_stream;
  978. + AVPacket* m_packet;
  979. /**
  980. - * Frame sent to the encoder.
  981. + * The AVFrame structure for using ffmpeg.
  982. */
  983. - AVFrame *m_frame;
  984. + AVFrame* m_frame;
  985. /**
  986. * PTS of next frame to write.
  987. @@ -132,7 +132,13 @@ private:
  988. * Encodes to the output buffer.
  989. * \param data Pointer to the data to encode.
  990. */
  991. - void encode(sample_t* data);
  992. + void encode();
  993. +
  994. + /**
  995. + * Finishes writing to the file.
  996. + */
  997. + void close();
  998. +
  999. public:
  1000. /**
  1001. diff --git a/blender-2.79b/intern/audaspace/intern/AUD_C-API.cpp b/blender-2.79b/intern/audaspace/intern/AUD_C-API.cpp
  1002. index 52cf256..ea2faae 100644
  1003. --- a/blender-2.79b/intern/audaspace/intern/AUD_C-API.cpp
  1004. +++ b/blender-2.79b/intern/audaspace/intern/AUD_C-API.cpp
  1005. @@ -113,9 +113,6 @@ static AUD_I3DDevice *AUD_3ddevice;
  1006. void AUD_initOnce()
  1007. {
  1008. -#ifdef WITH_FFMPEG
  1009. - av_register_all();
  1010. -#endif
  1011. #ifdef WITH_JACK
  1012. AUD_jack_init();
  1013. #endif
  1014. diff --git a/blender-2.79b/intern/audaspace/intern/AUD_Space.h b/blender-2.79b/intern/audaspace/intern/AUD_Space.h
  1015. index 26bbdc5..bda500b 100644
  1016. --- a/blender-2.79b/intern/audaspace/intern/AUD_Space.h
  1017. +++ b/blender-2.79b/intern/audaspace/intern/AUD_Space.h
  1018. @@ -195,7 +195,8 @@ typedef enum
  1019. AUD_CODEC_MP2,
  1020. AUD_CODEC_MP3,
  1021. AUD_CODEC_PCM,
  1022. - AUD_CODEC_VORBIS
  1023. + AUD_CODEC_VORBIS,
  1024. + AUD_CODEC_OPUS
  1025. } AUD_Codec;
  1026. /// Sample type.(float samples)
  1027. diff --git a/blender-2.79b/intern/ffmpeg/ffmpeg_compat.h b/blender-2.79b/intern/ffmpeg/ffmpeg_compat.h
  1028. index 9c06c8a..0e4bcbb 100644
  1029. --- a/blender-2.79b/intern/ffmpeg/ffmpeg_compat.h
  1030. +++ b/blender-2.79b/intern/ffmpeg/ffmpeg_compat.h
  1031. @@ -23,9 +23,17 @@
  1032. #include <libavformat/avformat.h>
  1033. -/* check our ffmpeg is new enough, avoids user complaints */
  1034. -#if (LIBAVFORMAT_VERSION_MAJOR < 52) || ((LIBAVFORMAT_VERSION_MAJOR == 52) && (LIBAVFORMAT_VERSION_MINOR <= 64))
  1035. -# error "FFmpeg 0.7 or newer is needed, Upgrade your FFmpeg or disable it"
  1036. +/* Check if our ffmpeg is new enough, avoids user complaints.
  1037. + * Minimum supported version is currently 3.2.0 which mean the following library versions:
  1038. + * libavutil > 55.30
  1039. + * libavcodec > 57.60
  1040. + * libavformat > 57.50
  1041. + *
  1042. + * We only check for one of these as they are usually updated in tandem.
  1043. + */
  1044. +#if (LIBAVFORMAT_VERSION_MAJOR < 57) || \
  1045. + ((LIBAVFORMAT_VERSION_MAJOR == 57) && (LIBAVFORMAT_VERSION_MINOR <= 50))
  1046. +# error "FFmpeg 3.2.0 or newer is needed, Upgrade your FFmpeg or disable it"
  1047. #endif
  1048. /* end sanity check */
  1049. @@ -36,214 +44,7 @@
  1050. # define FFMPEG_INLINE static inline
  1051. #endif
  1052. -#include <libavcodec/avcodec.h>
  1053. -#include <libavutil/rational.h>
  1054. -#include <libavutil/opt.h>
  1055. -#include <libavutil/mathematics.h>
  1056. -
  1057. -#if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
  1058. -# define FFMPEG_HAVE_PARSE_UTILS 1
  1059. -# include <libavutil/parseutils.h>
  1060. -#endif
  1061. -
  1062. -#include <libswscale/swscale.h>
  1063. -
  1064. -#if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 105))
  1065. -# define FFMPEG_HAVE_AVIO 1
  1066. -#endif
  1067. -
  1068. -#if (LIBAVCODEC_VERSION_MAJOR > 53) || ((LIBAVCODEC_VERSION_MAJOR == 53) && (LIBAVCODEC_VERSION_MINOR > 1)) || ((LIBAVCODEC_VERSION_MAJOR == 53) && (LIBAVCODEC_VERSION_MINOR == 1) && (LIBAVCODEC_VERSION_MICRO >= 1)) || ((LIBAVCODEC_VERSION_MAJOR == 52) && (LIBAVCODEC_VERSION_MINOR >= 121))
  1069. -# define FFMPEG_HAVE_DEFAULT_VAL_UNION 1
  1070. -#endif
  1071. -
  1072. -#if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
  1073. -# define FFMPEG_HAVE_AV_DUMP_FORMAT 1
  1074. -#endif
  1075. -
  1076. -#if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 45))
  1077. -# define FFMPEG_HAVE_AV_GUESS_FORMAT 1
  1078. -#endif
  1079. -
  1080. -#if (LIBAVCODEC_VERSION_MAJOR > 52) || ((LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 23))
  1081. -# define FFMPEG_HAVE_DECODE_AUDIO3 1
  1082. -# define FFMPEG_HAVE_DECODE_VIDEO2 1
  1083. -#endif
  1084. -
  1085. -#if (LIBAVCODEC_VERSION_MAJOR > 52) || ((LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 64))
  1086. -# define FFMPEG_HAVE_AVMEDIA_TYPES 1
  1087. -#endif
  1088. -
  1089. -#if ((LIBAVCODEC_VERSION_MAJOR > 52) || (LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 29)) && \
  1090. - ((LIBSWSCALE_VERSION_MAJOR > 0) || (LIBSWSCALE_VERSION_MAJOR >= 0) && (LIBSWSCALE_VERSION_MINOR >= 10))
  1091. -# define FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
  1092. -#endif
  1093. -
  1094. -#if ((LIBAVCODEC_VERSION_MAJOR > 54) || (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR > 14))
  1095. -# define FFMPEG_HAVE_CANON_H264_RESOLUTION_FIX
  1096. -#endif
  1097. -
  1098. -#if ((LIBAVCODEC_VERSION_MAJOR > 53) || (LIBAVCODEC_VERSION_MAJOR >= 53) && (LIBAVCODEC_VERSION_MINOR >= 60))
  1099. -# define FFMPEG_HAVE_ENCODE_AUDIO2
  1100. -#endif
  1101. -
  1102. -#if ((LIBAVCODEC_VERSION_MAJOR > 53) || (LIBAVCODEC_VERSION_MAJOR >= 53) && (LIBAVCODEC_VERSION_MINOR >= 42))
  1103. -# define FFMPEG_HAVE_DECODE_AUDIO4
  1104. -#endif
  1105. -
  1106. -#if ((LIBAVCODEC_VERSION_MAJOR > 54) || (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR >= 13))
  1107. -# define FFMPEG_HAVE_AVFRAME_SAMPLE_RATE
  1108. -#endif
  1109. -
  1110. -#if ((LIBAVUTIL_VERSION_MAJOR > 51) || (LIBAVUTIL_VERSION_MAJOR == 51) && (LIBAVUTIL_VERSION_MINOR >= 21))
  1111. -# define FFMPEG_FFV1_ALPHA_SUPPORTED
  1112. -# define FFMPEG_SAMPLE_FMT_S16P_SUPPORTED
  1113. -#else
  1114. -
  1115. -FFMPEG_INLINE
  1116. -int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
  1117. -{
  1118. - /* no planar formats in FFmpeg < 0.9 */
  1119. - (void) sample_fmt;
  1120. - return 0;
  1121. -}
  1122. -
  1123. -#endif
  1124. -
  1125. -/* FFmpeg upstream 1.0 is the first who added AV_ prefix. */
  1126. -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
  1127. -# define AV_CODEC_ID_NONE CODEC_ID_NONE
  1128. -# define AV_CODEC_ID_MPEG4 CODEC_ID_MPEG4
  1129. -# define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
  1130. -# define AV_CODEC_ID_DNXHD CODEC_ID_DNXHD
  1131. -# define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
  1132. -# define AV_CODEC_ID_MPEG1VIDEO CODEC_ID_MPEG1VIDEO
  1133. -# define AV_CODEC_ID_DVVIDEO CODEC_ID_DVVIDEO
  1134. -# define AV_CODEC_ID_THEORA CODEC_ID_THEORA
  1135. -# define AV_CODEC_ID_PNG CODEC_ID_PNG
  1136. -# define AV_CODEC_ID_QTRLE CODEC_ID_QTRLE
  1137. -# define AV_CODEC_ID_FFV1 CODEC_ID_FFV1
  1138. -# define AV_CODEC_ID_HUFFYUV CODEC_ID_HUFFYUV
  1139. -# define AV_CODEC_ID_H264 CODEC_ID_H264
  1140. -# define AV_CODEC_ID_FLV1 CODEC_ID_FLV1
  1141. -
  1142. -# define AV_CODEC_ID_AAC CODEC_ID_AAC
  1143. -# define AV_CODEC_ID_AC3 CODEC_ID_AC3
  1144. -# define AV_CODEC_ID_MP3 CODEC_ID_MP3
  1145. -# define AV_CODEC_ID_MP2 CODEC_ID_MP2
  1146. -# define AV_CODEC_ID_FLAC CODEC_ID_FLAC
  1147. -# define AV_CODEC_ID_PCM_U8 CODEC_ID_PCM_U8
  1148. -# define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
  1149. -# define AV_CODEC_ID_PCM_S24LE CODEC_ID_PCM_S24LE
  1150. -# define AV_CODEC_ID_PCM_S32LE CODEC_ID_PCM_S32LE
  1151. -# define AV_CODEC_ID_PCM_F32LE CODEC_ID_PCM_F32LE
  1152. -# define AV_CODEC_ID_PCM_F64LE CODEC_ID_PCM_F64LE
  1153. -# define AV_CODEC_ID_VORBIS CODEC_ID_VORBIS
  1154. -#endif
  1155. -
  1156. -FFMPEG_INLINE
  1157. -int av_get_cropped_height_from_codec(AVCodecContext *pCodecCtx)
  1158. -{
  1159. - int y = pCodecCtx->height;
  1160. -
  1161. -#ifndef FFMPEG_HAVE_CANON_H264_RESOLUTION_FIX
  1162. -/* really bad hack to remove this dreadfull black bar at the bottom
  1163. - with Canon footage and old ffmpeg versions.
  1164. - (to fix this properly in older ffmpeg versions one has to write a new
  1165. - demuxer...)
  1166. -
  1167. - see the actual fix here for reference:
  1168. -
  1169. - http://git.libav.org/?p=libav.git;a=commit;h=30f515091c323da59c0f1b533703dedca2f4b95d
  1170. -
  1171. - We do our best to apply this only to matching footage.
  1172. -*/
  1173. - if (pCodecCtx->width == 1920 &&
  1174. - pCodecCtx->height == 1088 &&
  1175. - pCodecCtx->pix_fmt == PIX_FMT_YUVJ420P &&
  1176. - pCodecCtx->codec_id == AV_CODEC_ID_H264 ) {
  1177. - y = 1080;
  1178. - }
  1179. -#endif
  1180. -
  1181. - return y;
  1182. -}
  1183. -
  1184. -#if ((LIBAVUTIL_VERSION_MAJOR < 51) || (LIBAVUTIL_VERSION_MAJOR == 51) && (LIBAVUTIL_VERSION_MINOR < 22))
  1185. -FFMPEG_INLINE
  1186. -int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
  1187. -{
  1188. - const AVOption *rv = NULL;
  1189. - (void) search_flags;
  1190. - av_set_string3(obj, name, val, 1, &rv);
  1191. - return rv != NULL;
  1192. -}
  1193. -
  1194. -FFMPEG_INLINE
  1195. -int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
  1196. -{
  1197. - const AVOption *rv = NULL;
  1198. - (void) search_flags;
  1199. - rv = av_set_int(obj, name, val);
  1200. - return rv != NULL;
  1201. -}
  1202. -
  1203. -FFMPEG_INLINE
  1204. -int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
  1205. -{
  1206. - const AVOption *rv = NULL;
  1207. - (void) search_flags;
  1208. - rv = av_set_double(obj, name, val);
  1209. - return rv != NULL;
  1210. -}
  1211. -
  1212. -# define AV_OPT_TYPE_INT FF_OPT_TYPE_INT
  1213. -# define AV_OPT_TYPE_INT64 FF_OPT_TYPE_INT64
  1214. -# define AV_OPT_TYPE_STRING FF_OPT_TYPE_STRING
  1215. -# define AV_OPT_TYPE_CONST FF_OPT_TYPE_CONST
  1216. -# define AV_OPT_TYPE_DOUBLE FF_OPT_TYPE_DOUBLE
  1217. -# define AV_OPT_TYPE_FLOAT FF_OPT_TYPE_FLOAT
  1218. -#endif
  1219. -
  1220. -#if ((LIBAVUTIL_VERSION_MAJOR < 51) || (LIBAVUTIL_VERSION_MAJOR == 51) && (LIBAVUTIL_VERSION_MINOR < 54))
  1221. -FFMPEG_INLINE
  1222. -enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
  1223. -{
  1224. - if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
  1225. - return AV_SAMPLE_FMT_NONE;
  1226. - return sample_fmt;
  1227. -}
  1228. -#endif
  1229. -
  1230. -#if ((LIBAVCODEC_VERSION_MAJOR < 53) || (LIBAVCODEC_VERSION_MAJOR == 53 && LIBAVCODEC_VERSION_MINOR < 35))
  1231. -FFMPEG_INLINE
  1232. -int avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  1233. -{
  1234. - /* TODO: no options are taking into account */
  1235. - (void) options;
  1236. - return avcodec_open(avctx, codec);
  1237. -}
  1238. -#endif
  1239. -
  1240. -#if ((LIBAVFORMAT_VERSION_MAJOR < 53) || (LIBAVFORMAT_VERSION_MAJOR == 53 && LIBAVFORMAT_VERSION_MINOR < 21))
  1241. -FFMPEG_INLINE
  1242. -AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)
  1243. -{
  1244. - /* TODO: no codec is taking into account */
  1245. - (void) c;
  1246. - return av_new_stream(s, 0);
  1247. -}
  1248. -
  1249. -FFMPEG_INLINE
  1250. -int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
  1251. -{
  1252. - /* TODO: no options are taking into account */
  1253. - (void) options;
  1254. - return av_find_stream_info(ic);
  1255. -}
  1256. -#endif
  1257. -
  1258. -#if ((LIBAVFORMAT_VERSION_MAJOR > 53) || ((LIBAVFORMAT_VERSION_MAJOR == 53) && (LIBAVFORMAT_VERSION_MINOR > 32)) || ((LIBAVFORMAT_VERSION_MAJOR == 53) && (LIBAVFORMAT_VERSION_MINOR == 24) && (LIBAVFORMAT_VERSION_MICRO >= 100)))
  1259. -FFMPEG_INLINE
  1260. +/*FFMPEG_INLINE
  1261. void my_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
  1262. {
  1263. int i;
  1264. @@ -251,9 +52,9 @@ void my_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
  1265. for (i = 0; i < s->nb_streams; i++) {
  1266. AVStream *st = s->streams[i];
  1267. - st->cur_dts = av_rescale(timestamp,
  1268. - st->time_base.den * (int64_t)ref_st->time_base.num,
  1269. - st->time_base.num * (int64_t)ref_st->time_base.den);
  1270. + st->internal->cur_dts = av_rescale(timestamp,
  1271. + st->time_base.den * (int64_t)ref_st->time_base.num,
  1272. + st->time_base.num * (int64_t)ref_st->time_base.den);
  1273. }
  1274. }
  1275. @@ -261,101 +62,13 @@ FFMPEG_INLINE
  1276. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
  1277. {
  1278. my_update_cur_dts(s, ref_st, timestamp);
  1279. -}
  1280. -#endif
  1281. -
  1282. -#if ((LIBAVCODEC_VERSION_MAJOR < 54) || (LIBAVCODEC_VERSION_MAJOR == 54 && LIBAVCODEC_VERSION_MINOR < 28))
  1283. -FFMPEG_INLINE
  1284. -void avcodec_free_frame(AVFrame **frame)
  1285. -{
  1286. - /* don't need to do anything with old AVFrame
  1287. - * since it does not have malloced members */
  1288. - (void)frame;
  1289. -}
  1290. -#endif
  1291. -
  1292. -#if ((LIBAVCODEC_VERSION_MAJOR > 54) || (LIBAVCODEC_VERSION_MAJOR >= 54) && (LIBAVCODEC_VERSION_MINOR >= 13))
  1293. -# define FFMPEG_HAVE_AVFRAME_SAMPLE_RATE
  1294. -#endif
  1295. -
  1296. -#if ((LIBAVCODEC_VERSION_MAJOR > 54) || (LIBAVCODEC_VERSION_MAJOR == 54 && LIBAVCODEC_VERSION_MINOR >= 13))
  1297. -# define FFMPEG_HAVE_FRAME_CHANNEL_LAYOUT
  1298. -#endif
  1299. -
  1300. -#ifndef FFMPEG_HAVE_AVIO
  1301. -# define AVIO_FLAG_WRITE URL_WRONLY
  1302. -# define avio_open url_fopen
  1303. -# define avio_tell url_ftell
  1304. -# define avio_close url_fclose
  1305. -# define avio_size url_fsize
  1306. -#endif
  1307. -
  1308. -/* there are some version inbetween, which have avio_... functions but no
  1309. - * AVIO_FLAG_... */
  1310. -#ifndef AVIO_FLAG_WRITE
  1311. -# define AVIO_FLAG_WRITE URL_WRONLY
  1312. -#endif
  1313. -
  1314. -#ifndef AV_PKT_FLAG_KEY
  1315. -# define AV_PKT_FLAG_KEY PKT_FLAG_KEY
  1316. -#endif
  1317. -
  1318. -#ifndef FFMPEG_HAVE_AV_DUMP_FORMAT
  1319. -# define av_dump_format dump_format
  1320. -#endif
  1321. -
  1322. -#ifndef FFMPEG_HAVE_AV_GUESS_FORMAT
  1323. -# define av_guess_format guess_format
  1324. -#endif
  1325. -
  1326. -#ifndef FFMPEG_HAVE_PARSE_UTILS
  1327. -# define av_parse_video_rate av_parse_video_frame_rate
  1328. -#endif
  1329. -
  1330. -#ifdef FFMPEG_HAVE_DEFAULT_VAL_UNION
  1331. -# define FFMPEG_DEF_OPT_VAL_INT(OPT) OPT->default_val.i64
  1332. -# define FFMPEG_DEF_OPT_VAL_DOUBLE(OPT) OPT->default_val.dbl
  1333. -#else
  1334. -# define FFMPEG_DEF_OPT_VAL_INT(OPT) OPT->default_val
  1335. -# define FFMPEG_DEF_OPT_VAL_DOUBLE(OPT) OPT->default_val
  1336. -#endif
  1337. -
  1338. -#ifndef FFMPEG_HAVE_AVMEDIA_TYPES
  1339. -# define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
  1340. -# define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
  1341. -#endif
  1342. -
  1343. -#ifndef FFMPEG_HAVE_DECODE_AUDIO3
  1344. -FFMPEG_INLINE
  1345. -int avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1346. - int *frame_size_ptr, AVPacket *avpkt)
  1347. -{
  1348. - return avcodec_decode_audio2(avctx, samples,
  1349. - frame_size_ptr, avpkt->data,
  1350. - avpkt->size);
  1351. -}
  1352. -#endif
  1353. -
  1354. -#ifndef FFMPEG_HAVE_DECODE_VIDEO2
  1355. -FFMPEG_INLINE
  1356. -int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1357. - int *got_picture_ptr,
  1358. - AVPacket *avpkt)
  1359. -{
  1360. - return avcodec_decode_video(avctx, picture, got_picture_ptr,
  1361. - avpkt->data, avpkt->size);
  1362. -}
  1363. -#endif
  1364. +}*/
  1365. FFMPEG_INLINE
  1366. int64_t av_get_pts_from_frame(AVFormatContext *avctx, AVFrame * picture)
  1367. {
  1368. int64_t pts;
  1369. -#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 34, 100)
  1370. pts = picture->pts;
  1371. -#else
  1372. - pts = picture->pkt_pts;
  1373. -#endif
  1374. if (pts == AV_NOPTS_VALUE) {
  1375. pts = picture->pkt_dts;
  1376. @@ -368,101 +81,7 @@ int64_t av_get_pts_from_frame(AVFormatContext *avctx, AVFrame * picture)
  1377. return pts;
  1378. }
  1379. -/* obsolete constant formerly defined in FFMpeg libavcodec/avcodec.h */
  1380. -#ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
  1381. -# define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
  1382. -#endif
  1383. -
  1384. -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 1, 0)
  1385. -FFMPEG_INLINE
  1386. -int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *pkt,
  1387. - const AVFrame *frame, int *got_output)
  1388. -{
  1389. - int outsize, ret;
  1390. -
  1391. - ret = av_new_packet(pkt, avctx->width * avctx->height * 7 + 10000);
  1392. - if (ret < 0)
  1393. - return ret;
  1394. -
  1395. - outsize = avcodec_encode_video(avctx, pkt->data, pkt->size, frame);
  1396. - if (outsize <= 0) {
  1397. - *got_output = 0;
  1398. - av_free_packet(pkt);
  1399. - }
  1400. - else {
  1401. - *got_output = 1;
  1402. - av_shrink_packet(pkt, outsize);
  1403. - if (avctx->coded_frame) {
  1404. - pkt->pts = avctx->coded_frame->pts;
  1405. - if (avctx->coded_frame->key_frame)
  1406. - pkt->flags |= AV_PKT_FLAG_KEY;
  1407. - }
  1408. - }
  1409. -
  1410. - return outsize >= 0 ? 0 : outsize;
  1411. -}
  1412. -
  1413. -#endif
  1414. -
  1415. -#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 17, 0)
  1416. -FFMPEG_INLINE
  1417. -void avformat_close_input(AVFormatContext **ctx)
  1418. -{
  1419. - av_close_input_file(*ctx);
  1420. - *ctx = NULL;
  1421. -}
  1422. -#endif
  1423. -
  1424. -#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 8, 0)
  1425. -FFMPEG_INLINE
  1426. -AVFrame *av_frame_alloc(void)
  1427. -{
  1428. - return avcodec_alloc_frame();
  1429. -}
  1430. -
  1431. -FFMPEG_INLINE
  1432. -void av_frame_free(AVFrame **frame)
  1433. -{
  1434. - av_freep(frame);
  1435. -}
  1436. -#endif
  1437. -
  1438. -FFMPEG_INLINE
  1439. -AVRational av_get_r_frame_rate_compat(const AVStream *stream)
  1440. -{
  1441. -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 23, 1)
  1442. - /* For until r_frame_rate was deprecated use it. */
  1443. - return stream->r_frame_rate;
  1444. -#else
  1445. - return stream->avg_frame_rate;
  1446. -#endif
  1447. -}
  1448. -
  1449. -#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(51, 32, 0)
  1450. -# define AV_OPT_SEARCH_FAKE_OBJ 0
  1451. -#endif
  1452. -
  1453. -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
  1454. -# define FFMPEG_HAVE_DEPRECATED_FLAGS2
  1455. -#endif
  1456. -
  1457. -/* Since FFmpeg-1.1 this constant have AV_ prefix. */
  1458. -#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 3, 100)
  1459. -# define AV_PIX_FMT_BGR32 PIX_FMT_BGR32
  1460. -# define AV_PIX_FMT_YUV422P PIX_FMT_YUV422P
  1461. -# define AV_PIX_FMT_BGRA PIX_FMT_BGRA
  1462. -# define AV_PIX_FMT_ARGB PIX_FMT_ARGB
  1463. -# define AV_PIX_FMT_RGBA PIX_FMT_RGBA
  1464. -#endif
  1465. -
  1466. -/* New API from FFmpeg-2.0 which soon became recommended one. */
  1467. -#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(52, 38, 100)
  1468. -# define av_frame_alloc avcodec_alloc_frame
  1469. -# define av_frame_free avcodec_free_frame
  1470. -# define av_frame_unref avcodec_get_frame_defaults
  1471. -#endif
  1472. -
  1473. -#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 24, 102)
  1474. +/* --- Deinterlace code block begin --- */
  1475. /* NOTE: The code in this block are from FFmpeg 2.6.4, which is licensed by LGPL. */
  1476. @@ -586,8 +205,9 @@ int deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1477. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1478. int y;
  1479. uint8_t *buf = (uint8_t *)av_malloc(width);
  1480. - if (!buf)
  1481. + if (!buf) {
  1482. return AVERROR(ENOMEM);
  1483. + }
  1484. src_m1 = src1;
  1485. memcpy(buf,src_m1,width);
  1486. @@ -607,14 +227,9 @@ int deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1487. return 0;
  1488. }
  1489. -#ifdef __GNUC__
  1490. -# pragma GCC diagnostic push
  1491. -# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1492. -#endif
  1493. -
  1494. FFMPEG_INLINE
  1495. -int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1496. - enum AVPixelFormat pix_fmt, int width, int height)
  1497. +int av_image_deinterlace(
  1498. + AVFrame *dst, const AVFrame *src, enum AVPixelFormat pix_fmt, int width, int height)
  1499. {
  1500. int i, ret;
  1501. @@ -624,10 +239,12 @@ int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1502. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  1503. pix_fmt != AV_PIX_FMT_YUV444P &&
  1504. pix_fmt != AV_PIX_FMT_YUV411P &&
  1505. - pix_fmt != AV_PIX_FMT_GRAY8)
  1506. + pix_fmt != AV_PIX_FMT_GRAY8) {
  1507. return -1;
  1508. - if ((width & 3) != 0 || (height & 3) != 0)
  1509. + }
  1510. + if ((width & 3) != 0 || (height & 3) != 0) {
  1511. return -1;
  1512. + }
  1513. for(i=0;i<3;i++) {
  1514. if (i == 1) {
  1515. @@ -655,8 +272,9 @@ int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1516. ret = deinterlace_bottom_field_inplace(dst->data[i],
  1517. dst->linesize[i],
  1518. width, height);
  1519. - if (ret < 0)
  1520. + if (ret < 0) {
  1521. return ret;
  1522. + }
  1523. } else {
  1524. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1525. src->data[i], src->linesize[i],
  1526. @@ -666,10 +284,6 @@ int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1527. return 0;
  1528. }
  1529. -#ifdef __GNUC__
  1530. -# pragma GCC diagnostic pop
  1531. -#endif
  1532. -
  1533. -#endif
  1534. +/* --- Deinterlace code block end --- */
  1535. #endif
  1536. diff --git a/blender-2.79b/source/blender/blenkernel/BKE_writeffmpeg.h b/blender-2.79b/source/blender/blenkernel/BKE_writeffmpeg.h
  1537. index a40c310..9a0d9ef 100644
  1538. --- a/blender-2.79b/source/blender/blenkernel/BKE_writeffmpeg.h
  1539. +++ b/blender-2.79b/source/blender/blenkernel/BKE_writeffmpeg.h
  1540. @@ -76,12 +76,8 @@ void BKE_ffmpeg_filepath_get(char *string, struct RenderData *rd, bool preview,
  1541. void BKE_ffmpeg_preset_set(struct RenderData *rd, int preset);
  1542. void BKE_ffmpeg_image_type_verify(struct RenderData *rd, struct ImageFormatData *imf);
  1543. -void BKE_ffmpeg_codec_settings_verify(struct RenderData *rd);
  1544. bool BKE_ffmpeg_alpha_channel_is_supported(struct RenderData *rd);
  1545. -int BKE_ffmpeg_property_add_string(struct RenderData *rd, const char *type, const char *str);
  1546. -void BKE_ffmpeg_property_del(struct RenderData *rd, void *type, void *prop_);
  1547. -
  1548. void *BKE_ffmpeg_context_create(void);
  1549. void BKE_ffmpeg_context_free(void *context_v);
  1550. diff --git a/blender-2.79b/source/blender/blenkernel/intern/scene.c b/blender-2.79b/source/blender/blenkernel/intern/scene.c
  1551. index 6fd53bb..bd428b2 100644
  1552. --- a/blender-2.79b/source/blender/blenkernel/intern/scene.c
  1553. +++ b/blender-2.79b/source/blender/blenkernel/intern/scene.c
  1554. @@ -325,10 +325,6 @@ Scene *BKE_scene_copy(Main *bmain, Scene *sce, int type)
  1555. scen->r.qtcodecdata = MEM_dupallocN(sce->r.qtcodecdata);
  1556. scen->r.qtcodecdata->cdParms = MEM_dupallocN(scen->r.qtcodecdata->cdParms);
  1557. }
  1558. -
  1559. - if (sce->r.ffcodecdata.properties) { /* intentionally check scen not sce. */
  1560. - scen->r.ffcodecdata.properties = IDP_CopyProperty(sce->r.ffcodecdata.properties);
  1561. - }
  1562. /* NOTE: part of SCE_COPY_LINK_DATA and SCE_COPY_FULL operations
  1563. * are done outside of blenkernel with ED_objects_single_users! */
  1564. @@ -424,11 +420,6 @@ void BKE_scene_free(Scene *sce)
  1565. MEM_freeN(sce->r.qtcodecdata);
  1566. sce->r.qtcodecdata = NULL;
  1567. }
  1568. - if (sce->r.ffcodecdata.properties) {
  1569. - IDP_FreeProperty(sce->r.ffcodecdata.properties);
  1570. - MEM_freeN(sce->r.ffcodecdata.properties);
  1571. - sce->r.ffcodecdata.properties = NULL;
  1572. - }
  1573. for (srl = sce->r.layers.first; srl; srl = srl->next) {
  1574. if (srl->prop != NULL) {
  1575. diff --git a/blender-2.79b/source/blender/blenkernel/intern/writeffmpeg.c b/blender-2.79b/source/blender/blenkernel/intern/writeffmpeg.c
  1576. index a19e414..a32278d 100644
  1577. --- a/blender-2.79b/source/blender/blenkernel/intern/writeffmpeg.c
  1578. +++ b/blender-2.79b/source/blender/blenkernel/intern/writeffmpeg.c
  1579. @@ -34,6 +34,9 @@
  1580. #include <libavformat/avformat.h>
  1581. #include <libavcodec/avcodec.h>
  1582. +#include <libavutil/channel_layout.h>
  1583. +#include <libavutil/imgutils.h>
  1584. +#include <libavutil/opt.h>
  1585. #include <libavutil/rational.h>
  1586. #include <libavutil/samplefmt.h>
  1587. #include <libswscale/swscale.h>
  1588. @@ -50,6 +53,7 @@
  1589. #endif
  1590. #include "BLI_utildefines.h"
  1591. +#include "BLI_threads.h"
  1592. #include "BKE_global.h"
  1593. #include "BKE_idprop.h"
  1594. @@ -78,18 +82,19 @@ typedef struct FFMpegContext {
  1595. int ffmpeg_preset; /* see FFMpegPreset */
  1596. AVFormatContext *outfile;
  1597. + AVCodecContext *video_codec;
  1598. + AVCodecContext *audio_codec;
  1599. AVStream *video_stream;
  1600. AVStream *audio_stream;
  1601. - AVFrame *current_frame;
  1602. + AVFrame *current_frame; /* Image frame in output pixel format. */
  1603. +
  1604. + /* Image frame in Blender's own pixel format, may need conversion to the output pixel format. */
  1605. + AVFrame *img_convert_frame;
  1606. struct SwsContext *img_convert_ctx;
  1607. uint8_t *audio_input_buffer;
  1608. uint8_t *audio_deinterleave_buffer;
  1609. int audio_input_samples;
  1610. -#ifndef FFMPEG_HAVE_ENCODE_AUDIO2
  1611. - uint8_t *audio_output_buffer;
  1612. - int audio_outbuf_size;
  1613. -#endif
  1614. double audio_time;
  1615. bool audio_deinterleave;
  1616. int audio_sample_size;
  1617. @@ -104,8 +109,6 @@ typedef struct FFMpegContext {
  1618. #define PRINT if (G.debug & G_DEBUG_FFMPEG) printf
  1619. static void ffmpeg_dict_set_int(AVDictionary **dict, const char *key, int value);
  1620. -static void ffmpeg_dict_set_float(AVDictionary **dict, const char *key, float value);
  1621. -static void ffmpeg_set_expert_options(RenderData *rd);
  1622. static void ffmpeg_filepath_get(FFMpegContext *context, char *string, struct RenderData *rd, bool preview, const char *suffix);
  1623. /* Delete a picture buffer */
  1624. @@ -125,31 +128,20 @@ static int request_float_audio_buffer(int codec_id)
  1625. }
  1626. #ifdef WITH_AUDASPACE
  1627. +
  1628. static int write_audio_frame(FFMpegContext *context)
  1629. {
  1630. - AVCodecContext *c = NULL;
  1631. - AVPacket pkt;
  1632. AVFrame *frame = NULL;
  1633. - int got_output = 0;
  1634. -
  1635. - c = context->audio_stream->codec;
  1636. -
  1637. - av_init_packet(&pkt);
  1638. - pkt.size = 0;
  1639. - pkt.data = NULL;
  1640. + AVCodecContext *c = context->audio_codec;
  1641. AUD_Device_read(context->audio_mixdown_device, context->audio_input_buffer, context->audio_input_samples);
  1642. context->audio_time += (double) context->audio_input_samples / (double) c->sample_rate;
  1643. -#ifdef FFMPEG_HAVE_ENCODE_AUDIO2
  1644. frame = av_frame_alloc();
  1645. - av_frame_unref(frame);
  1646. frame->pts = context->audio_time / av_q2d(c->time_base);
  1647. frame->nb_samples = context->audio_input_samples;
  1648. frame->format = c->sample_fmt;
  1649. -#ifdef FFMPEG_HAVE_FRAME_CHANNEL_LAYOUT
  1650. frame->channel_layout = c->channel_layout;
  1651. -#endif
  1652. if (context->audio_deinterleave) {
  1653. int channel, i;
  1654. @@ -170,53 +162,49 @@ static int write_audio_frame(FFMpegContext *context)
  1655. avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, context->audio_input_buffer,
  1656. context->audio_input_samples * c->channels * context->audio_sample_size, 1);
  1657. - if (avcodec_encode_audio2(c, &pkt, frame, &got_output) < 0) {
  1658. - // XXX error("Error writing audio packet");
  1659. - return -1;
  1660. - }
  1661. + int success = 0;
  1662. - if (!got_output) {
  1663. - av_frame_free(&frame);
  1664. - return 0;
  1665. + int ret = avcodec_send_frame(c, frame);
  1666. + if (ret < 0) {
  1667. + /* Can't send frame to encoder. This shouldn't happen. */
  1668. + fprintf(stderr, "Can't send audio frame: %s\n", av_err2str(ret));
  1669. + success = -1;
  1670. }
  1671. -#else
  1672. - pkt.size = avcodec_encode_audio(c, context->audio_output_buffer, context->audio_outbuf_size, (short *) context->audio_input_buffer);
  1673. - if (pkt.size < 0) {
  1674. - // XXX error("Error writing audio packet");
  1675. - return -1;
  1676. - }
  1677. + AVPacket *pkt = av_packet_alloc();
  1678. - pkt.data = context->audio_output_buffer;
  1679. - got_output = 1;
  1680. -#endif
  1681. + while (ret >= 0) {
  1682. - if (got_output) {
  1683. - if (pkt.pts != AV_NOPTS_VALUE)
  1684. - pkt.pts = av_rescale_q(pkt.pts, c->time_base, context->audio_stream->time_base);
  1685. - if (pkt.dts != AV_NOPTS_VALUE)
  1686. - pkt.dts = av_rescale_q(pkt.dts, c->time_base, context->audio_stream->time_base);
  1687. - if (pkt.duration > 0)
  1688. - pkt.duration = av_rescale_q(pkt.duration, c->time_base, context->audio_stream->time_base);
  1689. + ret = avcodec_receive_packet(c, pkt);
  1690. + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  1691. + break;
  1692. + }
  1693. + if (ret < 0) {
  1694. + fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
  1695. + success = -1;
  1696. + }
  1697. - pkt.stream_index = context->audio_stream->index;
  1698. + av_packet_rescale_ts(pkt, c->time_base, context->audio_stream->time_base);
  1699. + if (pkt->duration > 0) {
  1700. + pkt->duration = av_rescale_q(pkt->duration, c->time_base, context->audio_stream->time_base);
  1701. + }
  1702. - pkt.flags |= AV_PKT_FLAG_KEY;
  1703. + pkt->stream_index = context->audio_stream->index;
  1704. - if (av_interleaved_write_frame(context->outfile, &pkt) != 0) {
  1705. - fprintf(stderr, "Error writing audio packet!\n");
  1706. - if (frame)
  1707. - av_frame_free(&frame);
  1708. - return -1;
  1709. - }
  1710. + pkt->flags |= AV_PKT_FLAG_KEY;
  1711. - av_free_packet(&pkt);
  1712. + int write_ret = av_interleaved_write_frame(context->outfile, pkt);
  1713. + if (write_ret != 0) {
  1714. + fprintf(stderr, "Error writing audio packet: %s\n", av_err2str(write_ret));
  1715. + success = -1;
  1716. + break;
  1717. + }
  1718. }
  1719. - if (frame)
  1720. - av_frame_free(&frame);
  1721. + av_packet_free(&pkt);
  1722. + av_frame_free(&frame);
  1723. - return 0;
  1724. + return success;
  1725. }
  1726. #endif // #ifdef WITH_AUDASPACE
  1727. @@ -229,15 +217,22 @@ static AVFrame *alloc_picture(int pix_fmt, int width, int height)
  1728. /* allocate space for the struct */
  1729. f = av_frame_alloc();
  1730. - if (!f) return NULL;
  1731. - size = avpicture_get_size(pix_fmt, width, height);
  1732. + if (!f) {
  1733. + return NULL;
  1734. + }
  1735. + size = av_image_get_buffer_size(pix_fmt, width, height, 1);
  1736. /* allocate the actual picture buffer */
  1737. buf = MEM_mallocN(size, "AVFrame buffer");
  1738. if (!buf) {
  1739. free(f);
  1740. return NULL;
  1741. }
  1742. - avpicture_fill((AVPicture *)f, buf, pix_fmt, width, height);
  1743. +
  1744. + av_image_fill_arrays(f->data, f->linesize, buf, pix_fmt, width, height, 1);
  1745. + f->format = pix_fmt;
  1746. + f->width = width;
  1747. + f->height = height;
  1748. +
  1749. return f;
  1750. }
  1751. @@ -310,225 +305,114 @@ static const char **get_file_extensions(int format)
  1752. }
  1753. /* Write a frame to the output file */
  1754. -static int write_video_frame(FFMpegContext *context, RenderData *rd, int cfra, AVFrame *frame, ReportList *reports)
  1755. +static int write_video_frame(FFMpegContext *context, int cfra, AVFrame *frame, ReportList *reports)
  1756. {
  1757. - int got_output;
  1758. int ret, success = 1;
  1759. - AVCodecContext *c = context->video_stream->codec;
  1760. - AVPacket packet = { 0 };
  1761. + AVPacket *packet = av_packet_alloc();
  1762. - av_init_packet(&packet);
  1763. + AVCodecContext *c = context->video_codec;
  1764. frame->pts = cfra;
  1765. - if (rd->mode & R_FIELDS) {
  1766. - frame->top_field_first = ((rd->mode & R_ODDFIELD) != 0);
  1767. + ret = avcodec_send_frame(c, frame);
  1768. + if (ret < 0) {
  1769. + /* Can't send frame to encoder. This shouldn't happen. */
  1770. + fprintf(stderr, "Can't send video frame: %s\n", av_err2str(ret));
  1771. + success = -1;
  1772. }
  1773. - ret = avcodec_encode_video2(c, &packet, frame, &got_output);
  1774. + while (ret >= 0) {
  1775. + ret = avcodec_receive_packet(c, packet);
  1776. - if (ret >= 0 && got_output) {
  1777. - if (packet.pts != AV_NOPTS_VALUE) {
  1778. - packet.pts = av_rescale_q(packet.pts, c->time_base, context->video_stream->time_base);
  1779. - PRINT("Video Frame PTS: %d\n", (int)packet.pts);
  1780. - }
  1781. - else {
  1782. - PRINT("Video Frame PTS: not set\n");
  1783. - }
  1784. - if (packet.dts != AV_NOPTS_VALUE) {
  1785. - packet.dts = av_rescale_q(packet.dts, c->time_base, context->video_stream->time_base);
  1786. - PRINT("Video Frame DTS: %d\n", (int)packet.dts);
  1787. + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  1788. + /* No more packets available. */
  1789. + break;
  1790. }
  1791. - else {
  1792. - PRINT("Video Frame DTS: not set\n");
  1793. + if (ret < 0) {
  1794. + fprintf(stderr, "Error encoding frame: %s\n", av_err2str(ret));
  1795. + break;
  1796. }
  1797. - packet.stream_index = context->video_stream->index;
  1798. - ret = av_interleaved_write_frame(context->outfile, &packet);
  1799. - success = (ret == 0);
  1800. + packet->stream_index = context->video_stream->index;
  1801. + av_packet_rescale_ts(packet, c->time_base, context->video_stream->time_base);
  1802. + if (av_interleaved_write_frame(context->outfile, packet) != 0) {
  1803. + success = -1;
  1804. + break;
  1805. + }
  1806. }
  1807. - else if (ret < 0) {
  1808. - success = 0;
  1809. +
  1810. + if (!success) {
  1811. + PRINT("Error writing frame: %s\n", av_err2str(ret));
  1812. }
  1813. - if (!success)
  1814. - BKE_report(reports, RPT_ERROR, "Error writing frame");
  1815. + av_packet_free(&packet);
  1816. return success;
  1817. }
  1818. /* read and encode a frame of audio from the buffer */
  1819. -static AVFrame *generate_video_frame(FFMpegContext *context, uint8_t *pixels, ReportList *reports)
  1820. +static AVFrame *generate_video_frame(FFMpegContext *context, const uint8_t *pixels)
  1821. {
  1822. - uint8_t *rendered_frame;
  1823. -
  1824. - AVCodecContext *c = context->video_stream->codec;
  1825. - int width = c->width;
  1826. - int height = c->height;
  1827. + AVCodecParameters *codec = context->video_stream->codecpar;
  1828. + int height = codec->height;
  1829. AVFrame *rgb_frame;
  1830. - if (c->pix_fmt != AV_PIX_FMT_BGR32) {
  1831. - rgb_frame = alloc_picture(AV_PIX_FMT_BGR32, width, height);
  1832. - if (!rgb_frame) {
  1833. - BKE_report(reports, RPT_ERROR, "Could not allocate temporary frame");
  1834. - return NULL;
  1835. - }
  1836. + if (context->img_convert_frame != NULL) {
  1837. + /* Pixel format conversion is needed. */
  1838. + rgb_frame = context->img_convert_frame;
  1839. }
  1840. else {
  1841. + /* The output pixel format is Blender's internal pixel format. */
  1842. rgb_frame = context->current_frame;
  1843. }
  1844. - rendered_frame = pixels;
  1845. + /* Copy the Blender pixels into the FFmpeg datastructure, taking care of endianness and flipping
  1846. + * the image vertically. */
  1847. + int linesize = rgb_frame->linesize[0];
  1848. + for (int y = 0; y < height; y++) {
  1849. + uint8_t *target = rgb_frame->data[0] + linesize * (height - y - 1);
  1850. + const uint8_t *src = pixels + linesize * y;
  1851. - /* Do RGBA-conversion and flipping in one step depending
  1852. - * on CPU-Endianess */
  1853. +# if ENDIAN_ORDER == L_ENDIAN
  1854. + memcpy(target, src, linesize);
  1855. - if (ENDIAN_ORDER == L_ENDIAN) {
  1856. - int y;
  1857. - for (y = 0; y < height; y++) {
  1858. - uint8_t *target = rgb_frame->data[0] + width * 4 * (height - y - 1);
  1859. - uint8_t *src = rendered_frame + width * 4 * y;
  1860. - uint8_t *end = src + width * 4;
  1861. - while (src != end) {
  1862. - target[3] = src[3];
  1863. - target[2] = src[2];
  1864. - target[1] = src[1];
  1865. - target[0] = src[0];
  1866. +# elif ENDIAN_ORDER == B_ENDIAN
  1867. + const uint8_t *end = src + linesize;
  1868. + while (src != end) {
  1869. + target[3] = src[0];
  1870. + target[2] = src[1];
  1871. + target[1] = src[2];
  1872. + target[0] = src[3];
  1873. - target += 4;
  1874. - src += 4;
  1875. - }
  1876. - }
  1877. - }
  1878. - else {
  1879. - int y;
  1880. - for (y = 0; y < height; y++) {
  1881. - uint8_t *target = rgb_frame->data[0] + width * 4 * (height - y - 1);
  1882. - uint8_t *src = rendered_frame + width * 4 * y;
  1883. - uint8_t *end = src + width * 4;
  1884. - while (src != end) {
  1885. - target[3] = src[0];
  1886. - target[2] = src[1];
  1887. - target[1] = src[2];
  1888. - target[0] = src[3];
  1889. -
  1890. - target += 4;
  1891. - src += 4;
  1892. - }
  1893. + target += 4;
  1894. + src += 4;
  1895. }
  1896. +# else
  1897. +# error ENDIAN_ORDER should either be L_ENDIAN or B_ENDIAN.
  1898. +# endif
  1899. }
  1900. - if (c->pix_fmt != AV_PIX_FMT_BGR32) {
  1901. - sws_scale(context->img_convert_ctx, (const uint8_t *const *) rgb_frame->data,
  1902. - rgb_frame->linesize, 0, c->height,
  1903. - context->current_frame->data, context->current_frame->linesize);
  1904. - delete_picture(rgb_frame);
  1905. + /* Convert to the output pixel format, if it's different that Blender's internal one. */
  1906. + if (context->img_convert_frame != NULL) {
  1907. + BLI_assert(context->img_convert_ctx != NULL);
  1908. + sws_scale(context->img_convert_ctx,
  1909. + (const uint8_t *const *)rgb_frame->data,
  1910. + rgb_frame->linesize,
  1911. + 0,
  1912. + codec->height,
  1913. + context->current_frame->data,
  1914. + context->current_frame->linesize);
  1915. }
  1916. - context->current_frame->format = AV_PIX_FMT_BGR32;
  1917. - context->current_frame->width = width;
  1918. - context->current_frame->height = height;
  1919. -
  1920. return context->current_frame;
  1921. }
  1922. -static void set_ffmpeg_property_option(AVCodecContext *c, IDProperty *prop, AVDictionary **dictionary)
  1923. -{
  1924. - char name[128];
  1925. - char *param;
  1926. -
  1927. - PRINT("FFMPEG expert option: %s: ", prop->name);
  1928. -
  1929. - BLI_strncpy(name, prop->name, sizeof(name));
  1930. -
  1931. - param = strchr(name, ':');
  1932. -
  1933. - if (param) {
  1934. - *param++ = '\0';
  1935. - }
  1936. -
  1937. - switch (prop->type) {
  1938. - case IDP_STRING:
  1939. - PRINT("%s.\n", IDP_String(prop));
  1940. - av_dict_set(dictionary, name, IDP_String(prop), 0);
  1941. - break;
  1942. - case IDP_FLOAT:
  1943. - PRINT("%g.\n", IDP_Float(prop));
  1944. - ffmpeg_dict_set_float(dictionary, prop->name, IDP_Float(prop));
  1945. - break;
  1946. - case IDP_INT:
  1947. - PRINT("%d.\n", IDP_Int(prop));
  1948. -
  1949. - if (param) {
  1950. - if (IDP_Int(prop)) {
  1951. - av_dict_set(dictionary, name, param, 0);
  1952. - }
  1953. - else {
  1954. - return;
  1955. - }
  1956. - }
  1957. - else {
  1958. - ffmpeg_dict_set_int(dictionary, prop->name, IDP_Int(prop));
  1959. - }
  1960. - break;
  1961. - }
  1962. -}
  1963. -
  1964. -static int ffmpeg_proprty_valid(AVCodecContext *c, const char *prop_name, IDProperty *curr)
  1965. -{
  1966. - int valid = 1;
  1967. -
  1968. - if (STREQ(prop_name, "video")) {
  1969. - if (STREQ(curr->name, "bf")) {
  1970. - /* flash codec doesn't support b frames */
  1971. - valid &= c->codec_id != AV_CODEC_ID_FLV1;
  1972. - }
  1973. - }
  1974. -
  1975. - return valid;
  1976. -}
  1977. -
  1978. -static void set_ffmpeg_properties(RenderData *rd, AVCodecContext *c, const char *prop_name,
  1979. - AVDictionary **dictionary)
  1980. -{
  1981. - IDProperty *prop;
  1982. - IDProperty *curr;
  1983. -
  1984. - /* TODO(sergey): This is actually rather stupid, because changing
  1985. - * codec settings in render panel would also set expert options.
  1986. - *
  1987. - * But we need ti here in order to get rid of deprecated settings
  1988. - * when opening old files in new blender.
  1989. - *
  1990. - * For as long we don't allow editing properties in the interface
  1991. - * it's all good. bug if we allow editing them, we'll need to
  1992. - * replace it with some smarter code which would port settings
  1993. - * from deprecated to new one.
  1994. - */
  1995. - ffmpeg_set_expert_options(rd);
  1996. -
  1997. - if (!rd->ffcodecdata.properties) {
  1998. - return;
  1999. - }
  2000. -
  2001. - prop = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, prop_name);
  2002. - if (!prop) {
  2003. - return;
  2004. - }
  2005. -
  2006. - for (curr = prop->data.group.first; curr; curr = curr->next) {
  2007. - if (ffmpeg_proprty_valid(c, prop_name, curr))
  2008. - set_ffmpeg_property_option(c, curr, dictionary);
  2009. - }
  2010. -}
  2011. -
  2012. /* prepare a video stream for the output file */
  2013. static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int codec_id, AVFormatContext *of,
  2014. int rectx, int recty, char *error, int error_size)
  2015. {
  2016. AVStream *st;
  2017. - AVCodecContext *c;
  2018. AVCodec *codec;
  2019. AVDictionary *opts = NULL;
  2020. @@ -539,13 +423,14 @@ static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int
  2021. st->id = 0;
  2022. /* Set up the codec context */
  2023. -
  2024. - c = st->codec;
  2025. +
  2026. + context->video_codec = avcodec_alloc_context3(NULL);
  2027. + AVCodecContext *c = context->video_codec;
  2028. c->codec_id = codec_id;
  2029. c->codec_type = AVMEDIA_TYPE_VIDEO;
  2030. /* Get some values from the current render settings */
  2031. -
  2032. +
  2033. c->width = rectx;
  2034. c->height = recty;
  2035. @@ -605,12 +490,12 @@ static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int
  2036. c->rc_buffer_aggressivity = 1.0;
  2037. #endif
  2038. - c->me_method = ME_EPZS;
  2039. -
  2040. codec = avcodec_find_encoder(c->codec_id);
  2041. - if (!codec)
  2042. + if (!codec) {
  2043. + avcodec_free_context(&c);
  2044. return NULL;
  2045. -
  2046. + }
  2047. +
  2048. /* Be sure to use the correct pixel format(e.g. RGB, YUV) */
  2049. if (codec->pix_fmts) {
  2050. @@ -643,6 +528,13 @@ static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int
  2051. }
  2052. }
  2053. + if (codec_id == AV_CODEC_ID_DNXHD) {
  2054. + if (rd->ffcodecdata.flags & FFMPEG_LOSSLESS_OUTPUT) {
  2055. + /* Set the block decision algorithm to be of the highest quality ("rd" == 2). */
  2056. + c->mb_decision = 2;
  2057. + }
  2058. + }
  2059. +
  2060. if (codec_id == AV_CODEC_ID_FFV1) {
  2061. c->pix_fmt = AV_PIX_FMT_RGB32;
  2062. }
  2063. @@ -668,14 +560,14 @@ static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int
  2064. )
  2065. {
  2066. PRINT("Using global header\n");
  2067. - c->flags |= CODEC_FLAG_GLOBAL_HEADER;
  2068. + c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  2069. }
  2070. /* Determine whether we are encoding interlaced material or not */
  2071. if (rd->mode & R_FIELDS) {
  2072. PRINT("Encoding interlaced video\n");
  2073. - c->flags |= CODEC_FLAG_INTERLACED_DCT;
  2074. - c->flags |= CODEC_FLAG_INTERLACED_ME;
  2075. + c->flags |= AV_CODEC_FLAG_INTERLACED_DCT;
  2076. + c->flags |= AV_CODEC_FLAG_INTERLACED_ME;
  2077. }
  2078. /* xasp & yasp got float lately... */
  2079. @@ -683,28 +575,46 @@ static AVStream *alloc_video_stream(FFMpegContext *context, RenderData *rd, int
  2080. st->sample_aspect_ratio = c->sample_aspect_ratio = av_d2q(((double) rd->xasp / (double) rd->yasp), 255);
  2081. st->avg_frame_rate = av_inv_q(c->time_base);
  2082. - set_ffmpeg_properties(rd, c, "video", &opts);
  2083. -
  2084. if (avcodec_open2(c, codec, &opts) < 0) {
  2085. BLI_strncpy(error, IMB_ffmpeg_last_error(), error_size);
  2086. av_dict_free(&opts);
  2087. + avcodec_free_context(&c);
  2088. return NULL;
  2089. }
  2090. av_dict_free(&opts);
  2091. + /* FFmpeg expects its data in the output pixel format. */
  2092. context->current_frame = alloc_picture(c->pix_fmt, c->width, c->height);
  2093. - context->img_convert_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_BGR32, c->width, c->height, c->pix_fmt, SWS_BICUBIC,
  2094. - NULL, NULL, NULL);
  2095. + if (c->pix_fmt == AV_PIX_FMT_RGBA) {
  2096. + /* Output pixel format is the same we use internally, no conversion necessary. */
  2097. + context->img_convert_frame = NULL;
  2098. + context->img_convert_ctx = NULL;
  2099. + }
  2100. + else {
  2101. + /* Output pixel format is different, allocate frame for conversion. */
  2102. + context->img_convert_frame = alloc_picture(AV_PIX_FMT_RGBA, c->width, c->height);
  2103. + context->img_convert_ctx = sws_getContext(c->width,
  2104. + c->height,
  2105. + AV_PIX_FMT_RGBA,
  2106. + c->width,
  2107. + c->height,
  2108. + c->pix_fmt,
  2109. + SWS_BICUBIC,
  2110. + NULL,
  2111. + NULL,
  2112. + NULL);
  2113. + }
  2114. +
  2115. + avcodec_parameters_from_context(st->codecpar, c);
  2116. +
  2117. return st;
  2118. }
  2119. static AVStream *alloc_audio_stream(FFMpegContext *context, RenderData *rd, int codec_id, AVFormatContext *of, char *error, int error_size)
  2120. {
  2121. AVStream *st;
  2122. - AVCodecContext *c;
  2123. - AVCodec *codec;
  2124. - AVDictionary *opts = NULL;
  2125. + const AVCodec *codec;
  2126. error[0] = '\0';
  2127. @@ -712,27 +622,47 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, RenderData *rd, int
  2128. if (!st) return NULL;
  2129. st->id = 1;
  2130. - c = st->codec;
  2131. - c->codec_id = codec_id;
  2132. - c->codec_type = AVMEDIA_TYPE_AUDIO;
  2133. + codec = avcodec_find_encoder(codec_id);
  2134. + if (!codec) {
  2135. + fprintf(stderr, "Couldn't find valid audio codec\n");
  2136. + context->audio_codec = NULL;
  2137. + return NULL;
  2138. + }
  2139. +
  2140. + context->audio_codec = avcodec_alloc_context3(codec);
  2141. + AVCodecContext *c = context->audio_codec;
  2142. + c->thread_count = BLI_system_thread_count();
  2143. + c->thread_type = FF_THREAD_SLICE;
  2144. c->sample_rate = rd->ffcodecdata.audio_mixrate;
  2145. c->bit_rate = context->ffmpeg_audio_bitrate * 1000;
  2146. c->sample_fmt = AV_SAMPLE_FMT_S16;
  2147. c->channels = rd->ffcodecdata.audio_channels;
  2148. + switch (rd->ffcodecdata.audio_channels) {
  2149. + case AUD_CHANNELS_MONO:
  2150. + c->channel_layout = AV_CH_LAYOUT_MONO;
  2151. + break;
  2152. + case AUD_CHANNELS_STEREO:
  2153. + c->channel_layout = AV_CH_LAYOUT_STEREO;
  2154. + break;
  2155. + case AUD_CHANNELS_SURROUND4:
  2156. + c->channel_layout = AV_CH_LAYOUT_QUAD;
  2157. + break;
  2158. + case AUD_CHANNELS_SURROUND51:
  2159. + c->channel_layout = AV_CH_LAYOUT_5POINT1_BACK;
  2160. + break;
  2161. + case AUD_CHANNELS_SURROUND71:
  2162. + c->channel_layout = AV_CH_LAYOUT_7POINT1;
  2163. + break;
  2164. + }
  2165. +
  2166. if (request_float_audio_buffer(codec_id)) {
  2167. /* mainly for AAC codec which is experimental */
  2168. c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  2169. c->sample_fmt = AV_SAMPLE_FMT_FLT;
  2170. }
  2171. - codec = avcodec_find_encoder(c->codec_id);
  2172. - if (!codec) {
  2173. - //XXX error("Couldn't find a valid audio codec");
  2174. - return NULL;
  2175. - }
  2176. -
  2177. if (codec->sample_fmts) {
  2178. /* check if the preferred sample format for this codec is supported.
  2179. * this is because, depending on the version of libav, and with the whole ffmpeg/libav fork situation,
  2180. @@ -740,12 +670,13 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, RenderData *rd, int
  2181. */
  2182. const enum AVSampleFormat *p = codec->sample_fmts;
  2183. for (; *p != -1; p++) {
  2184. - if (*p == st->codec->sample_fmt)
  2185. + if (*p == c->sample_fmt) {
  2186. break;
  2187. + }
  2188. }
  2189. if (*p == -1) {
  2190. /* sample format incompatible with codec. Defaulting to a format known to work */
  2191. - st->codec->sample_fmt = codec->sample_fmts[0];
  2192. + c->sample_fmt = codec->sample_fmts[0];
  2193. }
  2194. }
  2195. @@ -754,50 +685,40 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, RenderData *rd, int
  2196. int best = 0;
  2197. int best_dist = INT_MAX;
  2198. for (; *p; p++) {
  2199. - int dist = abs(st->codec->sample_rate - *p);
  2200. + int dist = abs(c->sample_rate - *p);
  2201. if (dist < best_dist) {
  2202. best_dist = dist;
  2203. best = *p;
  2204. }
  2205. }
  2206. /* best is the closest supported sample rate (same as selected if best_dist == 0) */
  2207. - st->codec->sample_rate = best;
  2208. + c->sample_rate = best;
  2209. }
  2210. if (of->oformat->flags & AVFMT_GLOBALHEADER) {
  2211. - c->flags |= CODEC_FLAG_GLOBAL_HEADER;
  2212. + c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  2213. }
  2214. - set_ffmpeg_properties(rd, c, "audio", &opts);
  2215. -
  2216. - if (avcodec_open2(c, codec, &opts) < 0) {
  2217. + if (avcodec_open2(c, codec, NULL) < 0) {
  2218. //XXX error("Couldn't initialize audio codec");
  2219. BLI_strncpy(error, IMB_ffmpeg_last_error(), error_size);
  2220. - av_dict_free(&opts);
  2221. + avcodec_free_context(&c);
  2222. + context->audio_codec = NULL;
  2223. return NULL;
  2224. }
  2225. - av_dict_free(&opts);
  2226. /* need to prevent floating point exception when using vorbis audio codec,
  2227. * initialize this value in the same way as it's done in FFmpeg itself (sergey) */
  2228. - st->codec->time_base.num = 1;
  2229. - st->codec->time_base.den = st->codec->sample_rate;
  2230. -
  2231. -#ifndef FFMPEG_HAVE_ENCODE_AUDIO2
  2232. - context->audio_outbuf_size = FF_MIN_BUFFER_SIZE;
  2233. -#endif
  2234. + c->time_base.num = 1;
  2235. + c->time_base.den = c->sample_rate;
  2236. if (c->frame_size == 0)
  2237. // used to be if ((c->codec_id >= CODEC_ID_PCM_S16LE) && (c->codec_id <= CODEC_ID_PCM_DVD))
  2238. // not sure if that is needed anymore, so let's try out if there are any
  2239. // complaints regarding some ffmpeg versions users might have
  2240. - context->audio_input_samples = FF_MIN_BUFFER_SIZE * 8 / c->bits_per_coded_sample / c->channels;
  2241. + context->audio_input_samples = AV_INPUT_BUFFER_MIN_SIZE * 8 / c->bits_per_coded_sample / c->channels;
  2242. else {
  2243. context->audio_input_samples = c->frame_size;
  2244. -#ifndef FFMPEG_HAVE_ENCODE_AUDIO2
  2245. - if (c->frame_size * c->channels * sizeof(int16_t) * 4 > context->audio_outbuf_size)
  2246. - context->audio_outbuf_size = c->frame_size * c->channels * sizeof(int16_t) * 4;
  2247. -#endif
  2248. }
  2249. context->audio_deinterleave = av_sample_fmt_is_planar(c->sample_fmt);
  2250. @@ -805,15 +726,14 @@ static AVStream *alloc_audio_stream(FFMpegContext *context, RenderData *rd, int
  2251. context->audio_sample_size = av_get_bytes_per_sample(c->sample_fmt);
  2252. context->audio_input_buffer = (uint8_t *) av_malloc(context->audio_input_samples * c->channels * context->audio_sample_size);
  2253. -#ifndef FFMPEG_HAVE_ENCODE_AUDIO2
  2254. - context->audio_output_buffer = (uint8_t *) av_malloc(context->audio_outbuf_size);
  2255. -#endif
  2256. if (context->audio_deinterleave)
  2257. context->audio_deinterleave_buffer = (uint8_t *) av_malloc(context->audio_input_samples * c->channels * context->audio_sample_size);
  2258. context->audio_time = 0.0f;
  2259. + avcodec_parameters_from_context(st->codecpar, c);
  2260. +
  2261. return st;
  2262. }
  2263. /* essential functions -- start, append, end */
  2264. @@ -827,21 +747,11 @@ static void ffmpeg_dict_set_int(AVDictionary **dict, const char *key, int value)
  2265. av_dict_set(dict, key, buffer, 0);
  2266. }
  2267. -static void ffmpeg_dict_set_float(AVDictionary **dict, const char *key, float value)
  2268. -{
  2269. - char buffer[32];
  2270. -
  2271. - BLI_snprintf(buffer, sizeof(buffer), "%.8f", value);
  2272. -
  2273. - av_dict_set(dict, key, buffer, 0);
  2274. -}
  2275. -
  2276. static int start_ffmpeg_impl(FFMpegContext *context, struct RenderData *rd, int rectx, int recty, const char *suffix, ReportList *reports)
  2277. {
  2278. /* Handle to the output file */
  2279. AVFormatContext *of;
  2280. - AVOutputFormat *fmt;
  2281. - AVDictionary *opts = NULL;
  2282. + const AVOutputFormat *fmt;
  2283. char name[FILE_MAX], error[1024];
  2284. const char **exts;
  2285. @@ -869,12 +779,14 @@ static int start_ffmpeg_impl(FFMpegContext *context, struct RenderData *rd, int
  2286. name, context->ffmpeg_type, context->ffmpeg_codec, context->ffmpeg_audio_codec,
  2287. context->ffmpeg_video_bitrate, context->ffmpeg_audio_bitrate,
  2288. context->ffmpeg_gop_size, context->ffmpeg_autosplit, rectx, recty);
  2289. -
  2290. +
  2291. + /* Sanity checks for the output file extensions. */
  2292. exts = get_file_extensions(context->ffmpeg_type);
  2293. if (!exts) {
  2294. BKE_report(reports, RPT_ERROR, "No valid formats found");
  2295. return 0;
  2296. }
  2297. +
  2298. fmt = av_guess_format(NULL, exts[0], NULL);
  2299. if (!fmt) {
  2300. BKE_report(reports, RPT_ERROR, "No valid formats found");
  2301. @@ -883,67 +795,50 @@ static int start_ffmpeg_impl(FFMpegContext *context, struct RenderData *rd, int
  2302. of = avformat_alloc_context();
  2303. if (!of) {
  2304. - BKE_report(reports, RPT_ERROR, "Error opening output file");
  2305. + BKE_report(reports, RPT_ERROR, "Can't allocate ffmpeg format context");
  2306. return 0;
  2307. }
  2308. + enum AVCodecID audio_codec = context->ffmpeg_audio_codec;
  2309. + enum AVCodecID video_codec = context->ffmpeg_codec;
  2310. - /* Returns after this must 'goto fail;' */
  2311. -
  2312. - of->oformat = fmt;
  2313. -
  2314. - /* Only bother with setting packet size & mux rate when CRF is not used. */
  2315. - if (context->ffmpeg_crf == 0) {
  2316. - of->packet_size = rd->ffcodecdata.mux_packet_size;
  2317. - if (context->ffmpeg_audio_codec != AV_CODEC_ID_NONE) {
  2318. - ffmpeg_dict_set_int(&opts, "muxrate", rd->ffcodecdata.mux_rate);
  2319. - }
  2320. - else {
  2321. - av_dict_set(&opts, "muxrate", "0", 0);
  2322. - }
  2323. - }
  2324. -
  2325. - ffmpeg_dict_set_int(&opts, "preload", (int)(0.5 * AV_TIME_BASE));
  2326. -
  2327. - of->max_delay = (int)(0.7 * AV_TIME_BASE);
  2328. -
  2329. - fmt->audio_codec = context->ffmpeg_audio_codec;
  2330. -
  2331. - BLI_strncpy(of->filename, name, sizeof(of->filename));
  2332. - /* set the codec to the user's selection */
  2333. + of->url = av_strdup(name);
  2334. + /* Check if we need to force change the codec because of file type codec restrictions */
  2335. switch (context->ffmpeg_type) {
  2336. - case FFMPEG_AVI:
  2337. - case FFMPEG_MOV:
  2338. - case FFMPEG_MKV:
  2339. - fmt->video_codec = context->ffmpeg_codec;
  2340. - break;
  2341. case FFMPEG_OGG:
  2342. - fmt->video_codec = AV_CODEC_ID_THEORA;
  2343. + video_codec = AV_CODEC_ID_THEORA;
  2344. break;
  2345. case FFMPEG_DV:
  2346. - fmt->video_codec = AV_CODEC_ID_DVVIDEO;
  2347. + video_codec = AV_CODEC_ID_DVVIDEO;
  2348. break;
  2349. case FFMPEG_MPEG1:
  2350. - fmt->video_codec = AV_CODEC_ID_MPEG1VIDEO;
  2351. + video_codec = AV_CODEC_ID_MPEG1VIDEO;
  2352. break;
  2353. case FFMPEG_MPEG2:
  2354. - fmt->video_codec = AV_CODEC_ID_MPEG2VIDEO;
  2355. + video_codec = AV_CODEC_ID_MPEG2VIDEO;
  2356. break;
  2357. case FFMPEG_H264:
  2358. - fmt->video_codec = AV_CODEC_ID_H264;
  2359. + video_codec = AV_CODEC_ID_H264;
  2360. break;
  2361. case FFMPEG_XVID:
  2362. - fmt->video_codec = AV_CODEC_ID_MPEG4;
  2363. + video_codec = AV_CODEC_ID_MPEG4;
  2364. break;
  2365. case FFMPEG_FLV:
  2366. - fmt->video_codec = AV_CODEC_ID_FLV1;
  2367. + video_codec = AV_CODEC_ID_FLV1;
  2368. break;
  2369. - case FFMPEG_MPEG4:
  2370. default:
  2371. - fmt->video_codec = context->ffmpeg_codec;
  2372. + /* These containers are not restricted to any specific codec types.
  2373. + * Currently we expect these to be .avi, .mov, .mkv, and .mp4.
  2374. + */
  2375. + video_codec = context->ffmpeg_codec;
  2376. break;
  2377. }
  2378. - if (fmt->video_codec == AV_CODEC_ID_DVVIDEO) {
  2379. +
  2380. + /* Returns after this must 'goto fail;' */
  2381. +
  2382. + of->oformat = fmt;
  2383. +
  2384. + if (video_codec == AV_CODEC_ID_DVVIDEO) {
  2385. if (rectx != 720) {
  2386. BKE_report(reports, RPT_ERROR, "Render width has to be 720 pixels for DV!");
  2387. goto fail;
  2388. @@ -957,51 +852,62 @@ static int start_ffmpeg_impl(FFMpegContext *context, struct RenderData *rd, int
  2389. goto fail;
  2390. }
  2391. }
  2392. -
  2393. +
  2394. if (context->ffmpeg_type == FFMPEG_DV) {
  2395. - fmt->audio_codec = AV_CODEC_ID_PCM_S16LE;
  2396. + audio_codec = AV_CODEC_ID_PCM_S16LE;
  2397. if (context->ffmpeg_audio_codec != AV_CODEC_ID_NONE && rd->ffcodecdata.audio_mixrate != 48000 && rd->ffcodecdata.audio_channels != 2) {
  2398. BKE_report(reports, RPT_ERROR, "FFMPEG only supports 48khz / stereo audio for DV!");
  2399. goto fail;
  2400. }
  2401. }
  2402. - if (fmt->video_codec != AV_CODEC_ID_NONE) {
  2403. - context->video_stream = alloc_video_stream(context, rd, fmt->video_codec, of, rectx, recty, error, sizeof(error));
  2404. + if (video_codec != AV_CODEC_ID_NONE) {
  2405. + context->video_stream = alloc_video_stream(context, rd, video_codec, of, rectx, recty, error, sizeof(error));
  2406. PRINT("alloc video stream %p\n", context->video_stream);
  2407. if (!context->video_stream) {
  2408. - if (error[0])
  2409. + if (error[0]) {
  2410. BKE_report(reports, RPT_ERROR, error);
  2411. - else
  2412. + PRINT("Video stream error: %s\n", error);
  2413. + }
  2414. + else {
  2415. BKE_report(reports, RPT_ERROR, "Error initializing video stream");
  2416. + PRINT("Error initializing video stream");
  2417. + }
  2418. goto fail;
  2419. }
  2420. }
  2421. if (context->ffmpeg_audio_codec != AV_CODEC_ID_NONE) {
  2422. - context->audio_stream = alloc_audio_stream(context, rd, fmt->audio_codec, of, error, sizeof(error));
  2423. + context->audio_stream = alloc_audio_stream(context, rd, audio_codec, of, error, sizeof(error));
  2424. if (!context->audio_stream) {
  2425. - if (error[0])
  2426. + if (error[0]) {
  2427. BKE_report(reports, RPT_ERROR, error);
  2428. - else
  2429. + PRINT("Audio stream error: %s\n", error);
  2430. + }
  2431. + else {
  2432. BKE_report(reports, RPT_ERROR, "Error initializing audio stream");
  2433. + PRINT("Error initializing audio stream");
  2434. + }
  2435. goto fail;
  2436. }
  2437. }
  2438. if (!(fmt->flags & AVFMT_NOFILE)) {
  2439. if (avio_open(&of->pb, name, AVIO_FLAG_WRITE) < 0) {
  2440. BKE_report(reports, RPT_ERROR, "Could not open file for writing");
  2441. + PRINT("Could not open file for writing\n");
  2442. goto fail;
  2443. }
  2444. }
  2445. - if (avformat_write_header(of, NULL) < 0) {
  2446. +
  2447. + int ret = avformat_write_header(of, NULL);
  2448. + if (ret < 0) {
  2449. BKE_report(reports, RPT_ERROR, "Could not initialize streams, probably unsupported codec combination");
  2450. + PRINT("Could not write media header: %s\n", av_err2str(ret));
  2451. goto fail;
  2452. }
  2453. context->outfile = of;
  2454. av_dump_format(of, 0, name, 1);
  2455. - av_dict_free(&opts);
  2456. return 1;
  2457. @@ -1011,17 +917,14 @@ fail:
  2458. avio_close(of->pb);
  2459. }
  2460. - if (context->video_stream && context->video_stream->codec) {
  2461. - avcodec_close(context->video_stream->codec);
  2462. + if (context->video_stream) {
  2463. context->video_stream = NULL;
  2464. }
  2465. - if (context->audio_stream && context->audio_stream->codec) {
  2466. - avcodec_close(context->audio_stream->codec);
  2467. + if (context->audio_stream) {
  2468. context->audio_stream = NULL;
  2469. }
  2470. - av_dict_free(&opts);
  2471. avformat_free_context(of);
  2472. return 0;
  2473. }
  2474. @@ -1045,46 +948,35 @@ fail:
  2475. */
  2476. static void flush_ffmpeg(FFMpegContext *context)
  2477. {
  2478. - int ret = 0;
  2479. -
  2480. - AVCodecContext *c = context->video_stream->codec;
  2481. - /* get the delayed frames */
  2482. - while (1) {
  2483. - int got_output;
  2484. - AVPacket packet = { 0 };
  2485. - av_init_packet(&packet);
  2486. -
  2487. - ret = avcodec_encode_video2(c, &packet, NULL, &got_output);
  2488. - if (ret < 0) {
  2489. - fprintf(stderr, "Error encoding delayed frame %d\n", ret);
  2490. + AVCodecContext *c = context->video_codec;
  2491. + AVPacket *packet = av_packet_alloc();
  2492. +
  2493. + avcodec_send_frame(c, NULL);
  2494. + /* Get the packets frames. */
  2495. + int ret = 1;
  2496. + while (ret >= 0) {
  2497. + ret = avcodec_receive_packet(c, packet);
  2498. +
  2499. + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  2500. + /* No more packets to flush. */
  2501. break;
  2502. }
  2503. - if (!got_output) {
  2504. + if (ret < 0) {
  2505. + fprintf(stderr, "Error encoding delayed frame: %s\n", av_err2str(ret));
  2506. break;
  2507. }
  2508. - if (packet.pts != AV_NOPTS_VALUE) {
  2509. - packet.pts = av_rescale_q(packet.pts, c->time_base, context->video_stream->time_base);
  2510. - PRINT("Video Frame PTS: %d\n", (int) packet.pts);
  2511. - }
  2512. - else {
  2513. - PRINT("Video Frame PTS: not set\n");
  2514. - }
  2515. - if (packet.dts != AV_NOPTS_VALUE) {
  2516. - packet.dts = av_rescale_q(packet.dts, c->time_base, context->video_stream->time_base);
  2517. - PRINT("Video Frame DTS: %d\n", (int) packet.dts);
  2518. - }
  2519. - else {
  2520. - PRINT("Video Frame DTS: not set\n");
  2521. - }
  2522. - packet.stream_index = context->video_stream->index;
  2523. - ret = av_interleaved_write_frame(context->outfile, &packet);
  2524. - if (ret != 0) {
  2525. - fprintf(stderr, "Error writing delayed frame %d\n", ret);
  2526. + packet->stream_index = context->video_stream->index;
  2527. + av_packet_rescale_ts(packet, c->time_base, context->video_stream->time_base);
  2528. +
  2529. + int write_ret = av_interleaved_write_frame(context->outfile, packet);
  2530. + if (write_ret != 0) {
  2531. + fprintf(stderr, "Error writing delayed frame: %s\n", av_err2str(write_ret));
  2532. break;
  2533. }
  2534. }
  2535. - avcodec_flush_buffers(context->video_stream->codec);
  2536. +
  2537. + av_packet_free(&packet);
  2538. }
  2539. /* **********************************************************************
  2540. @@ -1172,7 +1064,8 @@ int BKE_ffmpeg_start(void *context_v, struct Scene *scene, RenderData *rd, int r
  2541. success = start_ffmpeg_impl(context, rd, rectx, recty, suffix, reports);
  2542. #ifdef WITH_AUDASPACE
  2543. if (context->audio_stream) {
  2544. - AVCodecContext *c = context->audio_stream->codec;
  2545. + AVCodecContext *c = context->audio_codec;
  2546. +
  2547. AUD_DeviceSpecs specs;
  2548. specs.channels = c->channels;
  2549. @@ -1198,10 +1091,6 @@ int BKE_ffmpeg_start(void *context_v, struct Scene *scene, RenderData *rd, int r
  2550. specs.rate = rd->ffcodecdata.audio_mixrate;
  2551. context->audio_mixdown_device = BKE_sound_mixdown(scene, specs, preview ? rd->psfra : rd->sfra, rd->ffcodecdata.audio_volume);
  2552. -#ifdef FFMPEG_CODEC_TIME_BASE
  2553. - c->time_base.den = specs.rate;
  2554. - c->time_base.num = 1;
  2555. -#endif
  2556. }
  2557. #endif
  2558. return success;
  2559. @@ -1237,8 +1126,8 @@ int BKE_ffmpeg_append(void *context_v, RenderData *rd, int start_frame, int fram
  2560. // write_audio_frames(frame / (((double)rd->frs_sec) / rd->frs_sec_base));
  2561. if (context->video_stream) {
  2562. - avframe = generate_video_frame(context, (unsigned char *) pixels, reports);
  2563. - success = (avframe && write_video_frame(context, rd, frame - start_frame, avframe, reports));
  2564. + avframe = generate_video_frame(context, (unsigned char *)pixels);
  2565. + success = (avframe && write_video_frame(context, frame - start_frame, avframe, reports));
  2566. if (context->ffmpeg_autosplit) {
  2567. if (avio_tell(context->outfile->pb) > FFMPEG_AUTOSPLIT_SIZE) {
  2568. @@ -1274,7 +1163,7 @@ static void end_ffmpeg_impl(FFMpegContext *context, int is_autosplit)
  2569. }
  2570. #endif
  2571. - if (context->video_stream && context->video_stream->codec) {
  2572. + if (context->video_stream) {
  2573. PRINT("Flushing delayed frames...\n");
  2574. flush_ffmpeg(context);
  2575. }
  2576. @@ -1285,14 +1174,12 @@ static void end_ffmpeg_impl(FFMpegContext *context, int is_autosplit)
  2577. /* Close the video codec */
  2578. - if (context->video_stream != NULL && context->video_stream->codec != NULL) {
  2579. - avcodec_close(context->video_stream->codec);
  2580. + if (context->video_stream != NULL) {
  2581. PRINT("zero video stream %p\n", context->video_stream);
  2582. context->video_stream = NULL;
  2583. }
  2584. - if (context->audio_stream != NULL && context->audio_stream->codec != NULL) {
  2585. - avcodec_close(context->audio_stream->codec);
  2586. + if (context->audio_stream != NULL) {
  2587. context->audio_stream = NULL;
  2588. }
  2589. @@ -1306,20 +1193,30 @@ static void end_ffmpeg_impl(FFMpegContext *context, int is_autosplit)
  2590. avio_close(context->outfile->pb);
  2591. }
  2592. }
  2593. +
  2594. + if (context->video_codec != NULL) {
  2595. + avcodec_free_context(&context->video_codec);
  2596. + context->video_codec = NULL;
  2597. + }
  2598. + if (context->audio_codec != NULL) {
  2599. + avcodec_free_context(&context->audio_codec);
  2600. + context->audio_codec = NULL;
  2601. + }
  2602. +
  2603. + if (context->img_convert_frame != NULL) {
  2604. + delete_picture(context->img_convert_frame);
  2605. + context->img_convert_frame = NULL;
  2606. + }
  2607. +
  2608. if (context->outfile != NULL) {
  2609. avformat_free_context(context->outfile);
  2610. context->outfile = NULL;
  2611. }
  2612. +
  2613. if (context->audio_input_buffer != NULL) {
  2614. av_free(context->audio_input_buffer);
  2615. context->audio_input_buffer = NULL;
  2616. }
  2617. -#ifndef FFMPEG_HAVE_ENCODE_AUDIO2
  2618. - if (context->audio_output_buffer != NULL) {
  2619. - av_free(context->audio_output_buffer);
  2620. - context->audio_output_buffer = NULL;
  2621. - }
  2622. -#endif
  2623. if (context->audio_deinterleave_buffer != NULL) {
  2624. av_free(context->audio_deinterleave_buffer);
  2625. @@ -1338,235 +1235,17 @@ void BKE_ffmpeg_end(void *context_v)
  2626. end_ffmpeg_impl(context, false);
  2627. }
  2628. -/* properties */
  2629. -
  2630. -void BKE_ffmpeg_property_del(RenderData *rd, void *type, void *prop_)
  2631. -{
  2632. - struct IDProperty *prop = (struct IDProperty *) prop_;
  2633. - IDProperty *group;
  2634. -
  2635. - if (!rd->ffcodecdata.properties) {
  2636. - return;
  2637. - }
  2638. -
  2639. - group = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, type);
  2640. - if (group && prop) {
  2641. - IDP_FreeFromGroup(group, prop);
  2642. - }
  2643. -}
  2644. -
  2645. -static IDProperty *BKE_ffmpeg_property_add(RenderData *rd, const char *type, const AVOption *o, const AVOption *parent)
  2646. -{
  2647. - AVCodecContext c;
  2648. - IDProperty *group;
  2649. - IDProperty *prop;
  2650. - IDPropertyTemplate val;
  2651. - int idp_type;
  2652. - char name[256];
  2653. -
  2654. - val.i = 0;
  2655. -
  2656. - avcodec_get_context_defaults3(&c, NULL);
  2657. -
  2658. - if (!rd->ffcodecdata.properties) {
  2659. - rd->ffcodecdata.properties = IDP_New(IDP_GROUP, &val, "ffmpeg");
  2660. - }
  2661. -
  2662. - group = IDP_GetPropertyFromGroup(rd->ffcodecdata.properties, type);
  2663. -
  2664. - if (!group) {
  2665. - group = IDP_New(IDP_GROUP, &val, type);
  2666. - IDP_AddToGroup(rd->ffcodecdata.properties, group);
  2667. - }
  2668. -
  2669. - if (parent) {
  2670. - BLI_snprintf(name, sizeof(name), "%s:%s", parent->name, o->name);
  2671. - }
  2672. - else {
  2673. - BLI_strncpy(name, o->name, sizeof(name));
  2674. - }
  2675. -
  2676. - PRINT("ffmpeg_property_add: %s %s\n", type, name);
  2677. -
  2678. - prop = IDP_GetPropertyFromGroup(group, name);
  2679. - if (prop) {
  2680. - return prop;
  2681. - }
  2682. -
  2683. - switch (o->type) {
  2684. - case AV_OPT_TYPE_INT:
  2685. - case AV_OPT_TYPE_INT64:
  2686. - val.i = FFMPEG_DEF_OPT_VAL_INT(o);
  2687. - idp_type = IDP_INT;
  2688. - break;
  2689. - case AV_OPT_TYPE_DOUBLE:
  2690. - case AV_OPT_TYPE_FLOAT:
  2691. - val.f = FFMPEG_DEF_OPT_VAL_DOUBLE(o);
  2692. - idp_type = IDP_FLOAT;
  2693. - break;
  2694. - case AV_OPT_TYPE_STRING:
  2695. - val.string.str = (char *)" ";
  2696. - val.string.len = 80;
  2697. -/* val.str = (char *)" ";*/
  2698. - idp_type = IDP_STRING;
  2699. - break;
  2700. - case AV_OPT_TYPE_CONST:
  2701. - val.i = 1;
  2702. - idp_type = IDP_INT;
  2703. - break;
  2704. - default:
  2705. - return NULL;
  2706. - }
  2707. - prop = IDP_New(idp_type, &val, name);
  2708. - IDP_AddToGroup(group, prop);
  2709. - return prop;
  2710. -}
  2711. -
  2712. -/* not all versions of ffmpeg include that, so here we go ... */
  2713. -
  2714. -int BKE_ffmpeg_property_add_string(RenderData *rd, const char *type, const char *str)
  2715. -{
  2716. - AVCodecContext c;
  2717. - const AVOption *o = NULL;
  2718. - const AVOption *p = NULL;
  2719. - char name_[128];
  2720. - char *name;
  2721. - char *param;
  2722. - IDProperty *prop = NULL;
  2723. -
  2724. - avcodec_get_context_defaults3(&c, NULL);
  2725. -
  2726. - BLI_strncpy(name_, str, sizeof(name_));
  2727. -
  2728. - name = name_;
  2729. - while (*name == ' ') name++;
  2730. -
  2731. - param = strchr(name, ':');
  2732. -
  2733. - if (!param) {
  2734. - param = strchr(name, ' ');
  2735. - }
  2736. - if (param) {
  2737. - *param++ = '\0';
  2738. - while (*param == ' ') param++;
  2739. - }
  2740. -
  2741. - o = av_opt_find(&c, name, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  2742. - if (!o) {
  2743. - PRINT("Ignoring unknown expert option %s\n", str);
  2744. - return 0;
  2745. - }
  2746. - if (param && o->type == AV_OPT_TYPE_CONST) {
  2747. - return 0;
  2748. - }
  2749. - if (param && o->type != AV_OPT_TYPE_CONST && o->unit) {
  2750. - p = av_opt_find(&c, param, o->unit, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  2751. - if (p) {
  2752. - prop = BKE_ffmpeg_property_add(rd, (char *) type, p, o);
  2753. - }
  2754. - else {
  2755. - PRINT("Ignoring unknown expert option %s\n", str);
  2756. - }
  2757. - }
  2758. - else {
  2759. - prop = BKE_ffmpeg_property_add(rd, (char *) type, o, NULL);
  2760. - }
  2761. -
  2762. -
  2763. - if (!prop) {
  2764. - return 0;
  2765. - }
  2766. -
  2767. - if (param && !p) {
  2768. - switch (prop->type) {
  2769. - case IDP_INT:
  2770. - IDP_Int(prop) = atoi(param);
  2771. - break;
  2772. - case IDP_FLOAT:
  2773. - IDP_Float(prop) = atof(param);
  2774. - break;
  2775. - case IDP_STRING:
  2776. - strncpy(IDP_String(prop), param, prop->len);
  2777. - break;
  2778. - }
  2779. - }
  2780. - return 1;
  2781. -}
  2782. -
  2783. -static void ffmpeg_set_expert_options(RenderData *rd)
  2784. -{
  2785. - int codec_id = rd->ffcodecdata.codec;
  2786. -
  2787. - if (rd->ffcodecdata.properties)
  2788. - IDP_FreeProperty(rd->ffcodecdata.properties);
  2789. -
  2790. - if (codec_id == AV_CODEC_ID_H264) {
  2791. - /*
  2792. - * All options here are for x264, but must be set via ffmpeg.
  2793. - * The names are therefore different - Search for "x264 to FFmpeg option mapping"
  2794. - * to get a list.
  2795. - */
  2796. -
  2797. - /*
  2798. - * Use CABAC coder. Using "coder:1", which should be equivalent,
  2799. - * crashes Blender for some reason. Either way - this is no big deal.
  2800. - */
  2801. - BKE_ffmpeg_property_add_string(rd, "video", "coder:vlc");
  2802. -
  2803. - /*
  2804. - * The other options were taken from the libx264-default.preset
  2805. - * included in the ffmpeg distribution.
  2806. - */
  2807. -// ffmpeg_property_add_string(rd, "video", "flags:loop"); // this breaks compatibility for QT
  2808. - BKE_ffmpeg_property_add_string(rd, "video", "cmp:chroma");
  2809. - BKE_ffmpeg_property_add_string(rd, "video", "partitions:parti4x4"); // Deprecated.
  2810. - BKE_ffmpeg_property_add_string(rd, "video", "partitions:partp8x8"); // Deprecated.
  2811. - BKE_ffmpeg_property_add_string(rd, "video", "partitions:partb8x8"); // Deprecated.
  2812. - BKE_ffmpeg_property_add_string(rd, "video", "me:hex");
  2813. - BKE_ffmpeg_property_add_string(rd, "video", "subq:6");
  2814. - BKE_ffmpeg_property_add_string(rd, "video", "me_range:16");
  2815. - BKE_ffmpeg_property_add_string(rd, "video", "qdiff:4");
  2816. - BKE_ffmpeg_property_add_string(rd, "video", "keyint_min:25");
  2817. - BKE_ffmpeg_property_add_string(rd, "video", "sc_threshold:40");
  2818. - BKE_ffmpeg_property_add_string(rd, "video", "i_qfactor:0.71");
  2819. - BKE_ffmpeg_property_add_string(rd, "video", "b_strategy:1");
  2820. - BKE_ffmpeg_property_add_string(rd, "video", "bf:3");
  2821. - BKE_ffmpeg_property_add_string(rd, "video", "refs:2");
  2822. - BKE_ffmpeg_property_add_string(rd, "video", "qcomp:0.6");
  2823. -
  2824. - BKE_ffmpeg_property_add_string(rd, "video", "trellis:0");
  2825. - BKE_ffmpeg_property_add_string(rd, "video", "weightb:1");
  2826. -#ifdef FFMPEG_HAVE_DEPRECATED_FLAGS2
  2827. - BKE_ffmpeg_property_add_string(rd, "video", "flags2:dct8x8");
  2828. - BKE_ffmpeg_property_add_string(rd, "video", "directpred:3");
  2829. - BKE_ffmpeg_property_add_string(rd, "video", "flags2:fastpskip");
  2830. - BKE_ffmpeg_property_add_string(rd, "video", "flags2:wpred");
  2831. -#else
  2832. - BKE_ffmpeg_property_add_string(rd, "video", "8x8dct:1");
  2833. - BKE_ffmpeg_property_add_string(rd, "video", "fast-pskip:1");
  2834. - BKE_ffmpeg_property_add_string(rd, "video", "wpredp:2");
  2835. -#endif
  2836. - }
  2837. - else if (codec_id == AV_CODEC_ID_DNXHD) {
  2838. - if (rd->ffcodecdata.flags & FFMPEG_LOSSLESS_OUTPUT)
  2839. - BKE_ffmpeg_property_add_string(rd, "video", "mbd:rd");
  2840. - }
  2841. -}
  2842. -
  2843. void BKE_ffmpeg_preset_set(RenderData *rd, int preset)
  2844. {
  2845. - int isntsc = (rd->frs_sec != 25);
  2846. -
  2847. - if (rd->ffcodecdata.properties)
  2848. - IDP_FreeProperty(rd->ffcodecdata.properties);
  2849. + bool is_ntsc = (rd->frs_sec != 25);
  2850. switch (preset) {
  2851. case FFMPEG_PRESET_VCD:
  2852. rd->ffcodecdata.type = FFMPEG_MPEG1;
  2853. rd->ffcodecdata.video_bitrate = 1150;
  2854. rd->xsch = 352;
  2855. - rd->ysch = isntsc ? 240 : 288;
  2856. - rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
  2857. + rd->ysch = is_ntsc ? 240 : 288;
  2858. + rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
  2859. rd->ffcodecdata.rc_max_rate = 1150;
  2860. rd->ffcodecdata.rc_min_rate = 1150;
  2861. rd->ffcodecdata.rc_buffer_size = 40 * 8;
  2862. @@ -1578,8 +1257,8 @@ void BKE_ffmpeg_preset_set(RenderData *rd, int preset)
  2863. rd->ffcodecdata.type = FFMPEG_MPEG2;
  2864. rd->ffcodecdata.video_bitrate = 2040;
  2865. rd->xsch = 480;
  2866. - rd->ysch = isntsc ? 480 : 576;
  2867. - rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
  2868. + rd->ysch = is_ntsc ? 480 : 576;
  2869. + rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
  2870. rd->ffcodecdata.rc_max_rate = 2516;
  2871. rd->ffcodecdata.rc_min_rate = 0;
  2872. rd->ffcodecdata.rc_buffer_size = 224 * 8;
  2873. @@ -1593,9 +1272,9 @@ void BKE_ffmpeg_preset_set(RenderData *rd, int preset)
  2874. /* Don't set resolution, see [#21351]
  2875. * rd->xsch = 720;
  2876. - * rd->ysch = isntsc ? 480 : 576; */
  2877. + * rd->ysch = is_ntsc ? 480 : 576; */
  2878. - rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
  2879. + rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
  2880. rd->ffcodecdata.rc_max_rate = 9000;
  2881. rd->ffcodecdata.rc_min_rate = 0;
  2882. rd->ffcodecdata.rc_buffer_size = 224 * 8;
  2883. @@ -1606,14 +1285,14 @@ void BKE_ffmpeg_preset_set(RenderData *rd, int preset)
  2884. case FFMPEG_PRESET_DV:
  2885. rd->ffcodecdata.type = FFMPEG_DV;
  2886. rd->xsch = 720;
  2887. - rd->ysch = isntsc ? 480 : 576;
  2888. + rd->ysch = is_ntsc ? 480 : 576;
  2889. break;
  2890. case FFMPEG_PRESET_H264:
  2891. rd->ffcodecdata.type = FFMPEG_AVI;
  2892. rd->ffcodecdata.codec = AV_CODEC_ID_H264;
  2893. rd->ffcodecdata.video_bitrate = 6000;
  2894. - rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
  2895. + rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
  2896. rd->ffcodecdata.rc_max_rate = 9000;
  2897. rd->ffcodecdata.rc_min_rate = 0;
  2898. rd->ffcodecdata.rc_buffer_size = 224 * 8;
  2899. @@ -1634,17 +1313,14 @@ void BKE_ffmpeg_preset_set(RenderData *rd, int preset)
  2900. }
  2901. rd->ffcodecdata.video_bitrate = 6000;
  2902. - rd->ffcodecdata.gop_size = isntsc ? 18 : 15;
  2903. + rd->ffcodecdata.gop_size = is_ntsc ? 18 : 15;
  2904. rd->ffcodecdata.rc_max_rate = 9000;
  2905. rd->ffcodecdata.rc_min_rate = 0;
  2906. rd->ffcodecdata.rc_buffer_size = 224 * 8;
  2907. rd->ffcodecdata.mux_packet_size = 2048;
  2908. rd->ffcodecdata.mux_rate = 10080000;
  2909. break;
  2910. -
  2911. }
  2912. -
  2913. - ffmpeg_set_expert_options(rd);
  2914. }
  2915. void BKE_ffmpeg_image_type_verify(RenderData *rd, ImageFormatData *imf)
  2916. @@ -1693,30 +1369,17 @@ void BKE_ffmpeg_image_type_verify(RenderData *rd, ImageFormatData *imf)
  2917. }
  2918. }
  2919. -void BKE_ffmpeg_codec_settings_verify(RenderData *rd)
  2920. -{
  2921. - ffmpeg_set_expert_options(rd);
  2922. -}
  2923. -
  2924. bool BKE_ffmpeg_alpha_channel_is_supported(RenderData *rd)
  2925. {
  2926. int codec = rd->ffcodecdata.codec;
  2927. - if (codec == AV_CODEC_ID_QTRLE)
  2928. - return true;
  2929. -
  2930. - if (codec == AV_CODEC_ID_PNG)
  2931. - return true;
  2932. -
  2933. - if (codec == AV_CODEC_ID_HUFFYUV)
  2934. - return true;
  2935. -
  2936. -#ifdef FFMPEG_FFV1_ALPHA_SUPPORTED
  2937. - if (codec == AV_CODEC_ID_FFV1)
  2938. - return true;
  2939. -#endif
  2940. + return ELEM(codec,
  2941. + AV_CODEC_ID_FFV1,
  2942. + AV_CODEC_ID_QTRLE,
  2943. + AV_CODEC_ID_PNG,
  2944. + AV_CODEC_ID_VP9,
  2945. + AV_CODEC_ID_HUFFYUV);
  2946. - return false;
  2947. }
  2948. void *BKE_ffmpeg_context_create(void)
  2949. diff --git a/blender-2.79b/source/blender/blenlib/BLI_math_base.h b/blender-2.79b/source/blender/blenlib/BLI_math_base.h
  2950. index e7e89a6..0872000 100644
  2951. --- a/blender-2.79b/source/blender/blenlib/BLI_math_base.h
  2952. +++ b/blender-2.79b/source/blender/blenlib/BLI_math_base.h
  2953. @@ -153,6 +153,8 @@ MINLINE int iroundf(float a);
  2954. MINLINE int divide_round_i(int a, int b);
  2955. MINLINE int mod_i(int i, int n);
  2956. +MINLINE int round_fl_to_int(float a);
  2957. +
  2958. MINLINE signed char round_fl_to_char_clamp(float a);
  2959. MINLINE unsigned char round_fl_to_uchar_clamp(float a);
  2960. MINLINE short round_fl_to_short_clamp(float a);
  2961. diff --git a/blender-2.79b/source/blender/blenlib/intern/math_base_inline.c b/blender-2.79b/source/blender/blenlib/intern/math_base_inline.c
  2962. index 37efe95..95ff62b 100644
  2963. --- a/blender-2.79b/source/blender/blenlib/intern/math_base_inline.c
  2964. +++ b/blender-2.79b/source/blender/blenlib/intern/math_base_inline.c
  2965. @@ -189,6 +189,15 @@ MINLINE int iroundf(float a)
  2966. return (int)floorf(a + 0.5f);
  2967. }
  2968. +#define _round_fl_impl(arg, ty) \
  2969. + { \
  2970. + return (ty)floorf(arg + 0.5f); \
  2971. + }
  2972. +
  2973. +MINLINE int round_fl_to_int(float a){_round_fl_impl(a, int)}
  2974. +
  2975. +#undef _round_fl_impl
  2976. +
  2977. #define _round_clamp_fl_impl(arg, ty, min, max) { \
  2978. float r = floorf(arg + 0.5f); \
  2979. if (UNLIKELY(r <= (float)min)) return (ty)min; \
  2980. diff --git a/blender-2.79b/source/blender/blenloader/intern/readfile.c b/blender-2.79b/source/blender/blenloader/intern/readfile.c
  2981. index f440cca..bd6168c 100644
  2982. --- a/blender-2.79b/source/blender/blenloader/intern/readfile.c
  2983. +++ b/blender-2.79b/source/blender/blenloader/intern/readfile.c
  2984. @@ -6169,7 +6169,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
  2985. }
  2986. }
  2987. }
  2988. -
  2989. +
  2990. sce->r.avicodecdata = newdataadr(fd, sce->r.avicodecdata);
  2991. if (sce->r.avicodecdata) {
  2992. sce->r.avicodecdata->lpFormat = newdataadr(fd, sce->r.avicodecdata->lpFormat);
  2993. @@ -6180,11 +6180,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
  2994. if (sce->r.qtcodecdata) {
  2995. sce->r.qtcodecdata->cdParms = newdataadr(fd, sce->r.qtcodecdata->cdParms);
  2996. }
  2997. - if (sce->r.ffcodecdata.properties) {
  2998. - sce->r.ffcodecdata.properties = newdataadr(fd, sce->r.ffcodecdata.properties);
  2999. - IDP_DirectLinkGroup_OrFree(&sce->r.ffcodecdata.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
  3000. - }
  3001. -
  3002. +
  3003. link_list(fd, &(sce->markers));
  3004. link_list(fd, &(sce->transform_spaces));
  3005. link_list(fd, &(sce->r.layers));
  3006. @@ -6209,7 +6205,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
  3007. }
  3008. direct_link_view_settings(fd, &sce->view_settings);
  3009. -
  3010. +
  3011. sce->rigidbody_world = newdataadr(fd, sce->rigidbody_world);
  3012. rbw = sce->rigidbody_world;
  3013. if (rbw) {
  3014. diff --git a/blender-2.79b/source/blender/blenloader/intern/writefile.c b/blender-2.79b/source/blender/blenloader/intern/writefile.c
  3015. index a50afc4..0c54b11 100644
  3016. --- a/blender-2.79b/source/blender/blenloader/intern/writefile.c
  3017. +++ b/blender-2.79b/source/blender/blenloader/intern/writefile.c
  3018. @@ -2697,9 +2697,6 @@ static void write_scene(WriteData *wd, Scene *sce)
  3019. writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
  3020. }
  3021. }
  3022. - if (sce->r.ffcodecdata.properties) {
  3023. - IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
  3024. - }
  3025. /* writing dynamic list of TimeMarkers to the blend file */
  3026. for (TimeMarker *marker = sce->markers.first; marker; marker = marker->next) {
  3027. diff --git a/blender-2.79b/source/blender/imbuf/intern/IMB_anim.h b/blender-2.79b/source/blender/imbuf/intern/IMB_anim.h
  3028. index 5f47769..8d380fb 100644
  3029. --- a/blender-2.79b/source/blender/imbuf/intern/IMB_anim.h
  3030. +++ b/blender-2.79b/source/blender/imbuf/intern/IMB_anim.h
  3031. @@ -145,7 +145,7 @@ struct anim {
  3032. #ifdef WITH_FFMPEG
  3033. AVFormatContext *pFormatCtx;
  3034. AVCodecContext *pCodecCtx;
  3035. - AVCodec *pCodec;
  3036. + const AVCodec *pCodec;
  3037. AVFrame *pFrame;
  3038. int pFrameComplete;
  3039. AVFrame *pFrameRGB;
  3040. @@ -156,7 +156,7 @@ struct anim {
  3041. struct ImBuf *last_frame;
  3042. int64_t last_pts;
  3043. int64_t next_pts;
  3044. - AVPacket next_packet;
  3045. + AVPacket *next_packet;
  3046. #endif
  3047. char index_dir[768];
  3048. diff --git a/blender-2.79b/source/blender/imbuf/intern/anim_movie.c b/blender-2.79b/source/blender/imbuf/intern/anim_movie.c
  3049. index 8bd808f..fc4de5c 100644
  3050. --- a/blender-2.79b/source/blender/imbuf/intern/anim_movie.c
  3051. +++ b/blender-2.79b/source/blender/imbuf/intern/anim_movie.c
  3052. @@ -92,6 +92,7 @@
  3053. #ifdef WITH_FFMPEG
  3054. # include <libavformat/avformat.h>
  3055. # include <libavcodec/avcodec.h>
  3056. +# include <libavutil/imgutils.h>
  3057. # include <libavutil/rational.h>
  3058. # include <libswscale/swscale.h>
  3059. @@ -448,24 +449,25 @@ BLI_INLINE bool need_aligned_ffmpeg_buffer(struct anim *anim)
  3060. static int startffmpeg(struct anim *anim)
  3061. {
  3062. - int i, videoStream;
  3063. + int i, video_stream_index;
  3064. - AVCodec *pCodec;
  3065. + const AVCodec *pCodec;
  3066. AVFormatContext *pFormatCtx = NULL;
  3067. AVCodecContext *pCodecCtx;
  3068. AVRational frame_rate;
  3069. + AVStream *video_stream;
  3070. int frs_num;
  3071. double frs_den;
  3072. int streamcount;
  3073. -#ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
  3074. /* The following for color space determination */
  3075. int srcRange, dstRange, brightness, contrast, saturation;
  3076. int *table;
  3077. const int *inv_table;
  3078. -#endif
  3079. - if (anim == NULL) return(-1);
  3080. + if (anim == NULL) {
  3081. + return(-1);
  3082. + }
  3083. streamcount = anim->streamindex;
  3084. @@ -482,47 +484,50 @@ static int startffmpeg(struct anim *anim)
  3085. /* Find the video stream */
  3086. - videoStream = -1;
  3087. + video_stream_index = -1;
  3088. - for (i = 0; i < pFormatCtx->nb_streams; i++)
  3089. - if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3090. + for (i = 0; i < pFormatCtx->nb_streams; i++) {
  3091. + if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3092. if (streamcount > 0) {
  3093. streamcount--;
  3094. continue;
  3095. }
  3096. - videoStream = i;
  3097. + video_stream_index = i;
  3098. break;
  3099. }
  3100. + }
  3101. - if (videoStream == -1) {
  3102. + if (video_stream_index == -1) {
  3103. avformat_close_input(&pFormatCtx);
  3104. return -1;
  3105. }
  3106. - pCodecCtx = pFormatCtx->streams[videoStream]->codec;
  3107. + video_stream = pFormatCtx->streams[video_stream_index];
  3108. /* Find the decoder for the video stream */
  3109. - pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  3110. + pCodec = avcodec_find_decoder(video_stream->codecpar->codec_id);
  3111. if (pCodec == NULL) {
  3112. avformat_close_input(&pFormatCtx);
  3113. return -1;
  3114. }
  3115. - pCodecCtx->workaround_bugs = 1;
  3116. + pCodecCtx = avcodec_alloc_context3(NULL);
  3117. + avcodec_parameters_to_context(pCodecCtx, video_stream->codecpar);
  3118. + pCodecCtx->workaround_bugs = FF_BUG_AUTODETECT;
  3119. if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
  3120. avformat_close_input(&pFormatCtx);
  3121. return -1;
  3122. }
  3123. if (pCodecCtx->pix_fmt == AV_PIX_FMT_NONE) {
  3124. - avcodec_close(anim->pCodecCtx);
  3125. + avcodec_free_context(&anim->pCodecCtx);
  3126. avformat_close_input(&pFormatCtx);
  3127. return -1;
  3128. }
  3129. - frame_rate = av_get_r_frame_rate_compat(pFormatCtx->streams[videoStream]);
  3130. - if (pFormatCtx->streams[videoStream]->nb_frames != 0) {
  3131. - anim->duration = pFormatCtx->streams[videoStream]->nb_frames;
  3132. + frame_rate = av_guess_frame_rate(pFormatCtx, video_stream, NULL);
  3133. + if (video_stream->nb_frames != 0) {
  3134. + anim->duration = video_stream->nb_frames;
  3135. }
  3136. else {
  3137. anim->duration = (int)(pFormatCtx->duration *
  3138. @@ -546,12 +551,12 @@ static int startffmpeg(struct anim *anim)
  3139. anim->params = 0;
  3140. anim->x = pCodecCtx->width;
  3141. - anim->y = av_get_cropped_height_from_codec(pCodecCtx);
  3142. + anim->y = pCodecCtx->height;
  3143. anim->pFormatCtx = pFormatCtx;
  3144. anim->pCodecCtx = pCodecCtx;
  3145. anim->pCodec = pCodec;
  3146. - anim->videoStream = videoStream;
  3147. + anim->videoStream = video_stream_index;
  3148. anim->interlacing = 0;
  3149. anim->orientation = 0;
  3150. @@ -561,7 +566,8 @@ static int startffmpeg(struct anim *anim)
  3151. anim->last_frame = 0;
  3152. anim->last_pts = -1;
  3153. anim->next_pts = -1;
  3154. - anim->next_packet.stream_index = -1;
  3155. + anim->next_packet = av_packet_alloc();
  3156. + anim->next_packet->stream_index = -1;
  3157. anim->pFrame = av_frame_alloc();
  3158. anim->pFrameComplete = false;
  3159. @@ -575,8 +581,9 @@ static int startffmpeg(struct anim *anim)
  3160. if (av_frame_get_buffer(anim->pFrameRGB, 32) < 0) {
  3161. fprintf(stderr, "Could not allocate frame data.\n");
  3162. - avcodec_close(anim->pCodecCtx);
  3163. + avcodec_free_context(&anim->pCodecCtx);
  3164. avformat_close_input(&anim->pFormatCtx);
  3165. + av_packet_free(&anim->next_packet);
  3166. av_frame_free(&anim->pFrameRGB);
  3167. av_frame_free(&anim->pFrameDeinterlaced);
  3168. av_frame_free(&anim->pFrame);
  3169. @@ -585,13 +592,13 @@ static int startffmpeg(struct anim *anim)
  3170. }
  3171. }
  3172. - if (avpicture_get_size(AV_PIX_FMT_RGBA, anim->x, anim->y) !=
  3173. - anim->x * anim->y * 4)
  3174. + if (av_image_get_buffer_size(AV_PIX_FMT_RGBA, anim->x, anim->y, 1) != anim->x * anim->y * 4)
  3175. {
  3176. fprintf(stderr,
  3177. "ffmpeg has changed alloc scheme ... ARGHHH!\n");
  3178. - avcodec_close(anim->pCodecCtx);
  3179. + avcodec_free_context(&anim->pCodecCtx);
  3180. avformat_close_input(&anim->pFormatCtx);
  3181. + av_packet_free(&anim->next_packet);
  3182. av_frame_free(&anim->pFrameRGB);
  3183. av_frame_free(&anim->pFrameDeinterlaced);
  3184. av_frame_free(&anim->pFrame);
  3185. @@ -600,15 +607,17 @@ static int startffmpeg(struct anim *anim)
  3186. }
  3187. if (anim->ib_flags & IB_animdeinterlace) {
  3188. - avpicture_fill((AVPicture *) anim->pFrameDeinterlaced,
  3189. - MEM_callocN(avpicture_get_size(
  3190. - anim->pCodecCtx->pix_fmt,
  3191. - anim->pCodecCtx->width,
  3192. - anim->pCodecCtx->height),
  3193. - "ffmpeg deinterlace"),
  3194. - anim->pCodecCtx->pix_fmt,
  3195. - anim->pCodecCtx->width,
  3196. - anim->pCodecCtx->height);
  3197. + av_image_fill_arrays(anim->pFrameDeinterlaced->data,
  3198. + anim->pFrameDeinterlaced->linesize,
  3199. + MEM_callocN(av_image_get_buffer_size(anim->pCodecCtx->pix_fmt,
  3200. + anim->pCodecCtx->width,
  3201. + anim->pCodecCtx->height,
  3202. + 1),
  3203. + "ffmpeg deinterlace"),
  3204. + anim->pCodecCtx->pix_fmt,
  3205. + anim->pCodecCtx->width,
  3206. + anim->pCodecCtx->height,
  3207. + 1);
  3208. }
  3209. if (pCodecCtx->has_b_frames) {
  3210. @@ -617,7 +626,7 @@ static int startffmpeg(struct anim *anim)
  3211. else {
  3212. anim->preseek = 0;
  3213. }
  3214. -
  3215. +
  3216. anim->img_convert_ctx = sws_getContext(
  3217. anim->x,
  3218. anim->y,
  3219. @@ -627,12 +636,13 @@ static int startffmpeg(struct anim *anim)
  3220. AV_PIX_FMT_RGBA,
  3221. SWS_FAST_BILINEAR | SWS_PRINT_INFO | SWS_FULL_CHR_H_INT,
  3222. NULL, NULL, NULL);
  3223. -
  3224. +
  3225. if (!anim->img_convert_ctx) {
  3226. fprintf(stderr,
  3227. "Can't transform color space??? Bailing out...\n");
  3228. - avcodec_close(anim->pCodecCtx);
  3229. + avcodec_free_context(&anim->pCodecCtx);
  3230. avformat_close_input(&anim->pFormatCtx);
  3231. + av_packet_free(&anim->next_packet);
  3232. av_frame_free(&anim->pFrameRGB);
  3233. av_frame_free(&anim->pFrameDeinterlaced);
  3234. av_frame_free(&anim->pFrame);
  3235. @@ -640,7 +650,6 @@ static int startffmpeg(struct anim *anim)
  3236. return -1;
  3237. }
  3238. -#ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
  3239. /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
  3240. if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int **)&inv_table, &srcRange,
  3241. &table, &dstRange, &brightness, &contrast, &saturation))
  3242. @@ -657,8 +666,7 @@ static int startffmpeg(struct anim *anim)
  3243. else {
  3244. fprintf(stderr, "Warning: Could not set libswscale colorspace details.\n");
  3245. }
  3246. -#endif
  3247. -
  3248. +
  3249. return (0);
  3250. }
  3251. @@ -695,14 +703,11 @@ static void ffmpeg_postprocess(struct anim *anim)
  3252. if (anim->ib_flags & IB_animdeinterlace) {
  3253. - if (avpicture_deinterlace(
  3254. - (AVPicture *)
  3255. - anim->pFrameDeinterlaced,
  3256. - (const AVPicture *)
  3257. - anim->pFrame,
  3258. - anim->pCodecCtx->pix_fmt,
  3259. - anim->pCodecCtx->width,
  3260. - anim->pCodecCtx->height) < 0)
  3261. + if (av_image_deinterlace(anim->pFrameDeinterlaced,
  3262. + anim->pFrame,
  3263. + anim->pCodecCtx->pix_fmt,
  3264. + anim->pCodecCtx->width,
  3265. + anim->pCodecCtx->height) < 0)
  3266. {
  3267. filter_y = true;
  3268. }
  3269. @@ -712,9 +717,13 @@ static void ffmpeg_postprocess(struct anim *anim)
  3270. }
  3271. if (!need_aligned_ffmpeg_buffer(anim)) {
  3272. - avpicture_fill((AVPicture *) anim->pFrameRGB,
  3273. - (unsigned char *) ibuf->rect,
  3274. - AV_PIX_FMT_RGBA, anim->x, anim->y);
  3275. + av_image_fill_arrays(anim->pFrameRGB->data,
  3276. + anim->pFrameRGB->linesize,
  3277. + (unsigned char *)ibuf->rect,
  3278. + AV_PIX_FMT_RGBA,
  3279. + anim->x,
  3280. + anim->y,
  3281. + 1);
  3282. }
  3283. if (ENDIAN_ORDER == B_ENDIAN) {
  3284. @@ -803,33 +812,27 @@ static int ffmpeg_decode_video_frame(struct anim *anim)
  3285. av_log(anim->pFormatCtx, AV_LOG_DEBUG, " DECODE VIDEO FRAME\n");
  3286. - if (anim->next_packet.stream_index == anim->videoStream) {
  3287. - av_free_packet(&anim->next_packet);
  3288. - anim->next_packet.stream_index = -1;
  3289. + if (anim->next_packet->stream_index == anim->videoStream) {
  3290. + av_packet_unref(anim->next_packet);
  3291. + anim->next_packet->stream_index = -1;
  3292. }
  3293. -
  3294. - while ((rval = av_read_frame(anim->pFormatCtx, &anim->next_packet)) >= 0) {
  3295. +
  3296. + while ((rval = av_read_frame(anim->pFormatCtx, anim->next_packet)) >= 0) {
  3297. av_log(anim->pFormatCtx,
  3298. AV_LOG_DEBUG,
  3299. "%sREAD: strID=%d (VID: %d) dts=%lld pts=%lld "
  3300. "%s\n",
  3301. - (anim->next_packet.stream_index == anim->videoStream)
  3302. - ? "->" : " ",
  3303. - anim->next_packet.stream_index,
  3304. + (anim->next_packet->stream_index == anim->videoStream) ? "->" : " ",
  3305. + anim->next_packet->stream_index,
  3306. anim->videoStream,
  3307. - (anim->next_packet.dts == AV_NOPTS_VALUE) ? -1 :
  3308. - (long long int)anim->next_packet.dts,
  3309. - (anim->next_packet.pts == AV_NOPTS_VALUE) ? -1 :
  3310. - (long long int)anim->next_packet.pts,
  3311. - (anim->next_packet.flags & AV_PKT_FLAG_KEY) ?
  3312. - " KEY" : "");
  3313. - if (anim->next_packet.stream_index == anim->videoStream) {
  3314. + (anim->next_packet->dts == AV_NOPTS_VALUE) ? -1 : (int64_t)anim->next_packet->dts,
  3315. + (anim->next_packet->pts == AV_NOPTS_VALUE) ? -1 : (int64_t)anim->next_packet->pts,
  3316. + (anim->next_packet->flags & AV_PKT_FLAG_KEY) ? " KEY" : "");
  3317. + if (anim->next_packet->stream_index == anim->videoStream) {
  3318. anim->pFrameComplete = 0;
  3319. - avcodec_decode_video2(
  3320. - anim->pCodecCtx,
  3321. - anim->pFrame, &anim->pFrameComplete,
  3322. - &anim->next_packet);
  3323. + avcodec_send_packet(anim->pCodecCtx, anim->next_packet);
  3324. + anim->pFrameComplete = avcodec_receive_frame(anim->pCodecCtx, anim->pFrame) == 0;
  3325. if (anim->pFrameComplete) {
  3326. anim->next_pts = av_get_pts_from_frame(
  3327. @@ -837,38 +840,23 @@ static int ffmpeg_decode_video_frame(struct anim *anim)
  3328. av_log(anim->pFormatCtx,
  3329. AV_LOG_DEBUG,
  3330. - " FRAME DONE: next_pts=%lld "
  3331. - "pkt_pts=%lld, guessed_pts=%lld\n",
  3332. + " FRAME DONE: next_pts=%" PRId64 ", guessed_pts=%" PRId64 "\n",
  3333. (anim->pFrame->pts == AV_NOPTS_VALUE) ?
  3334. - -1 : (long long int)anim->pFrame->pts,
  3335. - (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
  3336. - -1 : (long long int)anim->pFrame->pkt_pts,
  3337. - (long long int)anim->next_pts);
  3338. + -1 : (int64_t)anim->pFrame->pts,
  3339. + (int64_t)anim->next_pts);
  3340. break;
  3341. }
  3342. }
  3343. - av_free_packet(&anim->next_packet);
  3344. - anim->next_packet.stream_index = -1;
  3345. + av_packet_unref(anim->next_packet);
  3346. + anim->next_packet->stream_index = -1;
  3347. }
  3348. if (rval == AVERROR_EOF) {
  3349. - /* this sets size and data fields to zero,
  3350. - * which is necessary to decode the remaining data
  3351. - * in the decoder engine after EOF. It also prevents a memory
  3352. - * leak, since av_read_frame spills out a full size packet even
  3353. - * on EOF... (and: it's safe to call on NULL packets) */
  3354. -
  3355. - av_free_packet(&anim->next_packet);
  3356. -
  3357. - anim->next_packet.size = 0;
  3358. - anim->next_packet.data = 0;
  3359. -
  3360. + /* Flush any remaining frames out of the decoder. */
  3361. anim->pFrameComplete = 0;
  3362. - avcodec_decode_video2(
  3363. - anim->pCodecCtx,
  3364. - anim->pFrame, &anim->pFrameComplete,
  3365. - &anim->next_packet);
  3366. + avcodec_send_packet(anim->pCodecCtx, NULL);
  3367. + anim->pFrameComplete = avcodec_receive_frame(anim->pCodecCtx, anim->pFrame) == 0;
  3368. if (anim->pFrameComplete) {
  3369. anim->next_pts = av_get_pts_from_frame(
  3370. @@ -876,23 +864,21 @@ static int ffmpeg_decode_video_frame(struct anim *anim)
  3371. av_log(anim->pFormatCtx,
  3372. AV_LOG_DEBUG,
  3373. - " FRAME DONE (after EOF): next_pts=%lld "
  3374. - "pkt_pts=%lld, guessed_pts=%lld\n",
  3375. + " FRAME DONE (after EOF): next_pts=%" PRId64 ", guessed_pts=%" PRId64 "\n",
  3376. (anim->pFrame->pts == AV_NOPTS_VALUE) ?
  3377. -1 : (long long int)anim->pFrame->pts,
  3378. - (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
  3379. - -1 : (long long int)anim->pFrame->pkt_pts,
  3380. (long long int)anim->next_pts);
  3381. rval = 0;
  3382. }
  3383. }
  3384. if (rval < 0) {
  3385. - anim->next_packet.stream_index = -1;
  3386. + av_packet_unref(anim->next_packet);
  3387. + anim->next_packet->stream_index = -1;
  3388. av_log(anim->pFormatCtx,
  3389. AV_LOG_ERROR, " DECODE READ FAILED: av_read_frame() "
  3390. - "returned error: %d\n", rval);
  3391. + "returned error: %s\n", av_err2str(rval));
  3392. }
  3393. return (rval >= 0);
  3394. @@ -998,7 +984,7 @@ static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
  3395. v_st = anim->pFormatCtx->streams[anim->videoStream];
  3396. - frame_rate = av_q2d(av_get_r_frame_rate_compat(v_st));
  3397. + frame_rate = av_q2d(av_guess_frame_rate(anim->pFormatCtx, v_st, NULL));
  3398. st_time = anim->pFormatCtx->start_time;
  3399. pts_time_base = av_q2d(v_st->time_base);
  3400. @@ -1082,7 +1068,6 @@ static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
  3401. ret = av_seek_frame(anim->pFormatCtx,
  3402. -1,
  3403. pos, AVSEEK_FLAG_BYTE);
  3404. - av_update_cur_dts(anim->pFormatCtx, v_st, dts);
  3405. }
  3406. else {
  3407. av_log(anim->pFormatCtx, AV_LOG_DEBUG,
  3408. @@ -1127,9 +1112,9 @@ static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
  3409. anim->next_pts = -1;
  3410. - if (anim->next_packet.stream_index == anim->videoStream) {
  3411. - av_free_packet(&anim->next_packet);
  3412. - anim->next_packet.stream_index = -1;
  3413. + if (anim->next_packet->stream_index == anim->videoStream) {
  3414. + av_packet_unref(anim->next_packet);
  3415. + anim->next_packet->stream_index = -1;
  3416. }
  3417. /* memset(anim->pFrame, ...) ?? */
  3418. @@ -1154,11 +1139,11 @@ static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
  3419. ffmpeg_postprocess(anim);
  3420. anim->last_pts = anim->next_pts;
  3421. -
  3422. +
  3423. ffmpeg_decode_video_frame(anim);
  3424. -
  3425. +
  3426. anim->curposition = position;
  3427. -
  3428. +
  3429. IMB_refImBuf(anim->last_frame);
  3430. return anim->last_frame;
  3431. @@ -1169,35 +1154,30 @@ static void free_anim_ffmpeg(struct anim *anim)
  3432. if (anim == NULL) return;
  3433. if (anim->pCodecCtx) {
  3434. - avcodec_close(anim->pCodecCtx);
  3435. + avcodec_free_context(&anim->pCodecCtx);
  3436. avformat_close_input(&anim->pFormatCtx);
  3437. + av_packet_free(&anim->next_packet);
  3438. - /* Special case here: pFrame could share pointers with codec,
  3439. - * so in order to avoid double-free we don't use av_frame_free()
  3440. - * to free the frame.
  3441. - *
  3442. - * Could it be a bug in FFmpeg?
  3443. - */
  3444. - av_free(anim->pFrame);
  3445. + av_frame_free(&anim->pFrame);
  3446. if (!need_aligned_ffmpeg_buffer(anim)) {
  3447. /* If there's no need for own aligned buffer it means that FFmpeg's
  3448. * frame shares the same buffer as temporary ImBuf. In this case we
  3449. * should not free the buffer when freeing the FFmpeg buffer.
  3450. */
  3451. - avpicture_fill((AVPicture *)anim->pFrameRGB,
  3452. - NULL,
  3453. - AV_PIX_FMT_RGBA,
  3454. - anim->x, anim->y);
  3455. + av_image_fill_arrays(anim->pFrameRGB->data,
  3456. + anim->pFrameRGB->linesize,
  3457. + NULL,
  3458. + AV_PIX_FMT_RGBA,
  3459. + anim->x,
  3460. + anim->y,
  3461. + 1);
  3462. }
  3463. av_frame_free(&anim->pFrameRGB);
  3464. av_frame_free(&anim->pFrameDeinterlaced);
  3465. sws_freeContext(anim->img_convert_ctx);
  3466. IMB_freeImBuf(anim->last_frame);
  3467. - if (anim->next_packet.stream_index != -1) {
  3468. - av_free_packet(&anim->next_packet);
  3469. - }
  3470. }
  3471. anim->duration = 0;
  3472. }
  3473. diff --git a/blender-2.79b/source/blender/imbuf/intern/indexer.c b/blender-2.79b/source/blender/imbuf/intern/indexer.c
  3474. index e1b3abc..df8d5c4 100644
  3475. --- a/blender-2.79b/source/blender/imbuf/intern/indexer.c
  3476. +++ b/blender-2.79b/source/blender/imbuf/intern/indexer.c
  3477. @@ -32,6 +32,7 @@
  3478. #include "BLI_utildefines.h"
  3479. #include "BLI_endian_switch.h"
  3480. +#include "BLI_math_base.h"
  3481. #include "BLI_path_util.h"
  3482. #include "BLI_string.h"
  3483. #include "BLI_fileops.h"
  3484. @@ -49,6 +50,7 @@
  3485. #ifdef WITH_FFMPEG
  3486. # include "ffmpeg_compat.h"
  3487. +# include <libavutil/imgutils.h>
  3488. #endif
  3489. @@ -452,7 +454,7 @@ struct proxy_output_ctx {
  3490. AVFormatContext *of;
  3491. AVStream *st;
  3492. AVCodecContext *c;
  3493. - AVCodec *codec;
  3494. + const AVCodec *codec;
  3495. struct SwsContext *sws_ctx;
  3496. AVFrame *frame;
  3497. int cfra;
  3498. @@ -477,7 +479,6 @@ static struct proxy_output_ctx *alloc_proxy_output_ffmpeg(
  3499. sizeof(struct proxy_output_ctx), "alloc_proxy_output");
  3500. char fname[FILE_MAX];
  3501. - int ffmpeg_quality;
  3502. /* JPEG requires this */
  3503. width = round_up(width, 8);
  3504. @@ -491,30 +492,30 @@ static struct proxy_output_ctx *alloc_proxy_output_ffmpeg(
  3505. rv->of = avformat_alloc_context();
  3506. rv->of->oformat = av_guess_format("avi", NULL, NULL);
  3507. -
  3508. - BLI_strncpy(rv->of->filename, fname, sizeof(rv->of->filename));
  3509. - fprintf(stderr, "Starting work on proxy: %s\n", rv->of->filename);
  3510. + rv->of->url = av_strdup(fname);
  3511. +
  3512. + fprintf(stderr, "Starting work on proxy: %s\n", rv->of->url);
  3513. rv->st = avformat_new_stream(rv->of, NULL);
  3514. rv->st->id = 0;
  3515. - rv->c = rv->st->codec;
  3516. - rv->c->codec_type = AVMEDIA_TYPE_VIDEO;
  3517. - rv->c->codec_id = AV_CODEC_ID_MJPEG;
  3518. - rv->c->width = width;
  3519. - rv->c->height = height;
  3520. + rv->codec = avcodec_find_encoder(AV_CODEC_ID_H264);
  3521. - rv->of->oformat->video_codec = rv->c->codec_id;
  3522. - rv->codec = avcodec_find_encoder(rv->c->codec_id);
  3523. + rv->c = avcodec_alloc_context3(rv->codec);
  3524. if (!rv->codec) {
  3525. fprintf(stderr, "No ffmpeg MJPEG encoder available? "
  3526. "Proxy not built!\n");
  3527. - av_free(rv->of);
  3528. + avcodec_free_context(&rv->c);
  3529. + avformat_free_context(rv->of);
  3530. + MEM_freeN(rv);
  3531. return NULL;
  3532. }
  3533. + rv->c->width = width;
  3534. + rv->c->height = height;
  3535. +
  3536. if (rv->codec->pix_fmts) {
  3537. rv->c->pix_fmt = rv->codec->pix_fmts[0];
  3538. }
  3539. @@ -522,76 +523,105 @@ static struct proxy_output_ctx *alloc_proxy_output_ffmpeg(
  3540. rv->c->pix_fmt = AV_PIX_FMT_YUVJ420P;
  3541. }
  3542. - rv->c->sample_aspect_ratio =
  3543. - rv->st->sample_aspect_ratio =
  3544. - st->codec->sample_aspect_ratio;
  3545. + rv->c->sample_aspect_ratio = rv->st->sample_aspect_ratio = st->sample_aspect_ratio;
  3546. rv->c->time_base.den = 25;
  3547. rv->c->time_base.num = 1;
  3548. rv->st->time_base = rv->c->time_base;
  3549. - /* there's no way to set JPEG quality in the same way as in AVI JPEG and image sequence,
  3550. - * but this seems to be giving expected quality result */
  3551. - ffmpeg_quality = (int)(1.0f + 30.0f * (1.0f - (float)quality / 100.0f) + 0.5f);
  3552. - av_opt_set_int(rv->c, "qmin", ffmpeg_quality, 0);
  3553. - av_opt_set_int(rv->c, "qmax", ffmpeg_quality, 0);
  3554. + /* This range matches #eFFMpegCrf. `crf_range_min` corresponds to lowest quality,
  3555. + * `crf_range_max` to highest quality. */
  3556. + const int crf_range_min = 32;
  3557. + const int crf_range_max = 17;
  3558. + int crf = round_fl_to_int((quality / 100.0f) * (crf_range_max - crf_range_min) + crf_range_min);
  3559. +
  3560. + AVDictionary *codec_opts = NULL;
  3561. + /* High quality preset value. */
  3562. + av_dict_set_int(&codec_opts, "crf", crf, 0);
  3563. + /* Prefer smaller file-size. Presets from `veryslow` to `veryfast` produce output with very
  3564. + * similar file-size, but there is big difference in performance.
  3565. + * In some cases `veryfast` preset will produce smallest file-size. */
  3566. + av_dict_set(&codec_opts, "preset", "veryfast", 0);
  3567. + av_dict_set(&codec_opts, "tune", "fastdecode", 0);
  3568. if (rv->of->flags & AVFMT_GLOBALHEADER) {
  3569. - rv->c->flags |= CODEC_FLAG_GLOBAL_HEADER;
  3570. + rv->c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  3571. }
  3572. - if (avio_open(&rv->of->pb, fname, AVIO_FLAG_WRITE) < 0) {
  3573. - fprintf(stderr, "Couldn't open outputfile! "
  3574. - "Proxy not built!\n");
  3575. - av_free(rv->of);
  3576. - return 0;
  3577. + avcodec_parameters_from_context(rv->st->codecpar, rv->c);
  3578. +
  3579. + int ret = avio_open(&rv->of->pb, fname, AVIO_FLAG_WRITE);
  3580. +
  3581. + if (ret < 0) {
  3582. + fprintf(stderr, "Couldn't open IO: %s\n"
  3583. + "Proxy not built!\n",
  3584. + av_err2str(ret));
  3585. + avcodec_free_context(&rv->c);
  3586. + avformat_free_context(rv->of);
  3587. + MEM_freeN(rv);
  3588. + return NULL;
  3589. }
  3590. - avcodec_open2(rv->c, rv->codec, NULL);
  3591. + ret = avcodec_open2(rv->c, rv->codec, &codec_opts);
  3592. + if (ret < 0) {
  3593. + fprintf(stderr,
  3594. + "Couldn't open codec: %s\n"
  3595. + "Proxy not built!\n",
  3596. + av_err2str(ret));
  3597. + avcodec_free_context(&rv->c);
  3598. + avformat_free_context(rv->of);
  3599. + MEM_freeN(rv);
  3600. + return NULL;
  3601. + }
  3602. - rv->orig_height = av_get_cropped_height_from_codec(st->codec);
  3603. + rv->orig_height = st->codecpar->height;
  3604. - if (st->codec->width != width || st->codec->height != height ||
  3605. - st->codec->pix_fmt != rv->c->pix_fmt)
  3606. + if (st->codecpar->width != width || st->codecpar->height != height ||
  3607. + st->codecpar->format != rv->c->pix_fmt)
  3608. {
  3609. rv->frame = av_frame_alloc();
  3610. - avpicture_fill((AVPicture *) rv->frame,
  3611. - MEM_mallocN(avpicture_get_size(
  3612. - rv->c->pix_fmt,
  3613. - round_up(width, 16), height),
  3614. - "alloc proxy output frame"),
  3615. - rv->c->pix_fmt, round_up(width, 16), height);
  3616. + av_image_fill_arrays(rv->frame->data,
  3617. + rv->frame->linesize,
  3618. + MEM_mallocN(av_image_get_buffer_size(rv->c->pix_fmt, round_up(width, 16), height, 1), "alloc proxy output frame"),
  3619. + rv->c->pix_fmt,
  3620. + round_up(width, 16),
  3621. + height,
  3622. + 1);
  3623. rv->sws_ctx = sws_getContext(
  3624. - st->codec->width,
  3625. + st->codecpar->width,
  3626. rv->orig_height,
  3627. - st->codec->pix_fmt,
  3628. + st->codecpar->format,
  3629. width, height,
  3630. rv->c->pix_fmt,
  3631. SWS_FAST_BILINEAR | SWS_PRINT_INFO,
  3632. NULL, NULL, NULL);
  3633. }
  3634. - if (avformat_write_header(rv->of, NULL) < 0) {
  3635. - fprintf(stderr, "Couldn't set output parameters? "
  3636. - "Proxy not built!\n");
  3637. - av_free(rv->of);
  3638. - return 0;
  3639. + ret = avformat_write_header(rv->of, NULL);
  3640. + if (ret < 0) {
  3641. + fprintf(stderr, "Couldn't write header: %s\n"
  3642. + "Proxy not built!\n",
  3643. + av_err2str(ret));
  3644. +
  3645. + if (rv->frame) {
  3646. + av_frame_free(&rv->frame);
  3647. + }
  3648. +
  3649. + avcodec_free_context(&rv->c);
  3650. + avformat_free_context(rv->of);
  3651. + MEM_freeN(rv);
  3652. + return NULL;
  3653. }
  3654. return rv;
  3655. }
  3656. -static int add_to_proxy_output_ffmpeg(
  3657. +static void add_to_proxy_output_ffmpeg(
  3658. struct proxy_output_ctx *ctx, AVFrame *frame)
  3659. {
  3660. - AVPacket packet = { 0 };
  3661. - int ret, got_output;
  3662. -
  3663. - av_init_packet(&packet);
  3664. -
  3665. if (!ctx) {
  3666. - return 0;
  3667. + return;
  3668. }
  3669. if (ctx->sws_ctx && frame &&
  3670. @@ -609,39 +639,42 @@ static int add_to_proxy_output_ffmpeg(
  3671. frame->pts = ctx->cfra++;
  3672. }
  3673. - ret = avcodec_encode_video2(ctx->c, &packet, frame, &got_output);
  3674. + int ret = avcodec_send_frame(ctx->c, frame);
  3675. if (ret < 0) {
  3676. - fprintf(stderr, "Error encoding proxy frame %d for '%s'\n",
  3677. - ctx->cfra - 1, ctx->of->filename);
  3678. - return 0;
  3679. + /* Can't send frame to encoder. This shouldn't happen. */
  3680. + fprintf(stderr, "Can't send video frame: %s\n", av_err2str(ret));
  3681. + return;
  3682. }
  3683. + AVPacket *packet = av_packet_alloc();
  3684. +
  3685. + while (ret >= 0) {
  3686. + ret = avcodec_receive_packet(ctx->c, packet);
  3687. - if (got_output) {
  3688. - if (packet.pts != AV_NOPTS_VALUE) {
  3689. - packet.pts = av_rescale_q(packet.pts,
  3690. - ctx->c->time_base,
  3691. - ctx->st->time_base);
  3692. + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  3693. + /* No more packets to flush. */
  3694. + break;
  3695. }
  3696. - if (packet.dts != AV_NOPTS_VALUE) {
  3697. - packet.dts = av_rescale_q(packet.dts,
  3698. - ctx->c->time_base,
  3699. - ctx->st->time_base);
  3700. + if (ret < 0) {
  3701. + fprintf(stderr,
  3702. + "Error encoding proxy frame %d for '%s': %s\n",
  3703. + ctx->cfra - 1,
  3704. + ctx->of->url,
  3705. + av_err2str(ret));
  3706. + break;
  3707. }
  3708. - packet.stream_index = ctx->st->index;
  3709. + packet->stream_index = ctx->st->index;
  3710. + av_packet_rescale_ts(packet, ctx->c->time_base, ctx->st->time_base);
  3711. - if (av_interleaved_write_frame(ctx->of, &packet) != 0) {
  3712. + int write_ret = av_interleaved_write_frame(ctx->of, packet);
  3713. + if (write_ret != 0) {
  3714. fprintf(stderr, "Error writing proxy frame %d "
  3715. - "into '%s'\n", ctx->cfra - 1,
  3716. - ctx->of->filename);
  3717. - return 0;
  3718. + "into '%s': %s\n", ctx->cfra - 1,
  3719. + ctx->of->url, av_err2str(write_ret));
  3720. + break;
  3721. }
  3722. -
  3723. - return 1;
  3724. - }
  3725. - else {
  3726. - return 0;
  3727. }
  3728. + av_packet_free(&packet);
  3729. }
  3730. static void free_proxy_output_ffmpeg(struct proxy_output_ctx *ctx,
  3731. @@ -655,15 +688,16 @@ static void free_proxy_output_ffmpeg(struct proxy_output_ctx *ctx,
  3732. }
  3733. if (!rollback) {
  3734. - while (add_to_proxy_output_ffmpeg(ctx, NULL)) {}
  3735. + /* Flush the remaining packets. */
  3736. + add_to_proxy_output_ffmpeg(ctx, NULL);
  3737. }
  3738. avcodec_flush_buffers(ctx->c);
  3739. av_write_trailer(ctx->of);
  3740. -
  3741. - avcodec_close(ctx->c);
  3742. -
  3743. +
  3744. + avcodec_free_context(&ctx->c);
  3745. +
  3746. if (ctx->of->oformat) {
  3747. if (!(ctx->of->oformat->flags & AVFMT_NOFILE)) {
  3748. avio_close(ctx->of->pb);
  3749. @@ -699,7 +733,7 @@ typedef struct FFmpegIndexBuilderContext {
  3750. AVFormatContext *iFormatCtx;
  3751. AVCodecContext *iCodecCtx;
  3752. - AVCodec *iCodec;
  3753. + const AVCodec *iCodec;
  3754. AVStream *iStream;
  3755. int videoStream;
  3756. @@ -756,7 +790,7 @@ static IndexBuildContext *index_ffmpeg_create_context(struct anim *anim, IMB_Tim
  3757. /* Find the video stream */
  3758. context->videoStream = -1;
  3759. for (i = 0; i < context->iFormatCtx->nb_streams; i++)
  3760. - if (context->iFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3761. + if (context->iFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  3762. if (streamcount > 0) {
  3763. streamcount--;
  3764. continue;
  3765. @@ -772,9 +806,8 @@ static IndexBuildContext *index_ffmpeg_create_context(struct anim *anim, IMB_Tim
  3766. }
  3767. context->iStream = context->iFormatCtx->streams[context->videoStream];
  3768. - context->iCodecCtx = context->iStream->codec;
  3769. - context->iCodec = avcodec_find_decoder(context->iCodecCtx->codec_id);
  3770. + context->iCodec = avcodec_find_decoder(context->iStream->codecpar->codec_id);
  3771. if (context->iCodec == NULL) {
  3772. avformat_close_input(&context->iFormatCtx);
  3773. @@ -782,22 +815,25 @@ static IndexBuildContext *index_ffmpeg_create_context(struct anim *anim, IMB_Tim
  3774. return NULL;
  3775. }
  3776. - context->iCodecCtx->workaround_bugs = 1;
  3777. + context->iCodecCtx = avcodec_alloc_context3(NULL);
  3778. + avcodec_parameters_to_context(context->iCodecCtx, context->iStream->codecpar);
  3779. + context->iCodecCtx->workaround_bugs = FF_BUG_AUTODETECT;
  3780. if (avcodec_open2(context->iCodecCtx, context->iCodec, NULL) < 0) {
  3781. avformat_close_input(&context->iFormatCtx);
  3782. + avcodec_free_context(&context->iCodecCtx);
  3783. MEM_freeN(context);
  3784. return NULL;
  3785. }
  3786. for (i = 0; i < num_proxy_sizes; i++) {
  3787. if (proxy_sizes_in_use & proxy_sizes[i]) {
  3788. - context->proxy_ctx[i] = alloc_proxy_output_ffmpeg(
  3789. - anim, context->iStream, proxy_sizes[i],
  3790. - context->iCodecCtx->width * proxy_fac[i],
  3791. - av_get_cropped_height_from_codec(
  3792. - context->iCodecCtx) * proxy_fac[i],
  3793. - quality);
  3794. + context->proxy_ctx[i] = alloc_proxy_output_ffmpeg(anim,
  3795. + context->iStream,
  3796. + proxy_sizes[i],
  3797. + context->iCodecCtx->width * proxy_fac[i],
  3798. + context->iCodecCtx->height * proxy_fac[i],
  3799. + quality);
  3800. if (!context->proxy_ctx[i]) {
  3801. proxy_sizes_in_use &= ~proxy_sizes[i];
  3802. }
  3803. @@ -836,7 +872,7 @@ static void index_rebuild_ffmpeg_finish(FFmpegIndexBuilderContext *context, int
  3804. }
  3805. }
  3806. - avcodec_close(context->iCodecCtx);
  3807. + avcodec_free_context(&context->iCodecCtx);
  3808. avformat_close_input(&context->iFormatCtx);
  3809. MEM_freeN(context);
  3810. @@ -899,23 +935,18 @@ static void index_rebuild_ffmpeg_proc_decoded_frame(
  3811. static int index_rebuild_ffmpeg(FFmpegIndexBuilderContext *context,
  3812. short *stop, short *do_update, float *progress)
  3813. {
  3814. - AVFrame *in_frame = 0;
  3815. - AVPacket next_packet;
  3816. + AVFrame *in_frame = av_frame_alloc();
  3817. + AVPacket *next_packet = av_packet_alloc();
  3818. uint64_t stream_size;
  3819. - memset(&next_packet, 0, sizeof(AVPacket));
  3820. -
  3821. - in_frame = av_frame_alloc();
  3822. -
  3823. stream_size = avio_size(context->iFormatCtx->pb);
  3824. - context->frame_rate = av_q2d(av_get_r_frame_rate_compat(context->iStream));
  3825. + context->frame_rate = av_q2d(av_guess_frame_rate(context->iFormatCtx, context->iStream, NULL));
  3826. context->pts_time_base = av_q2d(context->iStream->time_base);
  3827. - while (av_read_frame(context->iFormatCtx, &next_packet) >= 0) {
  3828. + while (av_read_frame(context->iFormatCtx, next_packet) >= 0) {
  3829. int frame_finished = 0;
  3830. - float next_progress = (float)((int)floor(((double) next_packet.pos) * 100 /
  3831. - ((double) stream_size) + 0.5)) / 100;
  3832. + float next_progress = (float)((int)floor(((double)next_packet->pos) * 100 / ((double)stream_size) + 0.5)) / 100;
  3833. if (*progress != next_progress) {
  3834. *progress = next_progress;
  3835. @@ -923,56 +954,59 @@ static int index_rebuild_ffmpeg(FFmpegIndexBuilderContext *context,
  3836. }
  3837. if (*stop) {
  3838. - av_free_packet(&next_packet);
  3839. break;
  3840. }
  3841. - if (next_packet.stream_index == context->videoStream) {
  3842. - if (next_packet.flags & AV_PKT_FLAG_KEY) {
  3843. + if (next_packet->stream_index == context->videoStream) {
  3844. + if (next_packet->flags & AV_PKT_FLAG_KEY) {
  3845. context->last_seek_pos = context->seek_pos;
  3846. context->last_seek_pos_dts = context->seek_pos_dts;
  3847. - context->seek_pos = next_packet.pos;
  3848. - context->seek_pos_dts = next_packet.dts;
  3849. - context->seek_pos_pts = next_packet.pts;
  3850. + context->seek_pos = next_packet->pos;
  3851. + context->seek_pos_dts = next_packet->dts;
  3852. + context->seek_pos_pts = next_packet->pts;
  3853. }
  3854. - avcodec_decode_video2(
  3855. - context->iCodecCtx, in_frame, &frame_finished,
  3856. - &next_packet);
  3857. - }
  3858. -
  3859. - if (frame_finished) {
  3860. - index_rebuild_ffmpeg_proc_decoded_frame(
  3861. - context, &next_packet, in_frame);
  3862. + int ret = avcodec_send_packet(context->iCodecCtx, next_packet);
  3863. + while (ret >= 0) {
  3864. + ret = avcodec_receive_frame(context->iCodecCtx, in_frame);
  3865. + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  3866. + /* No more frames to flush. */
  3867. + break;
  3868. + }
  3869. + if (ret < 0) {
  3870. + fprintf(stderr, "Error decoding proxy frame: %s\n", av_err2str(ret));
  3871. + break;
  3872. + }
  3873. + index_rebuild_ffmpeg_proc_decoded_frame(context, next_packet, in_frame);
  3874. + }
  3875. }
  3876. - av_free_packet(&next_packet);
  3877. }
  3878. /* process pictures still stuck in decoder engine after EOF
  3879. - * according to ffmpeg docs using 0-size packets.
  3880. + * according to ffmpeg docs using NULL packets.
  3881. *
  3882. * At least, if we haven't already stopped... */
  3883. - /* this creates the 0-size packet and prevents a memory leak. */
  3884. - av_free_packet(&next_packet);
  3885. -
  3886. if (!*stop) {
  3887. - int frame_finished;
  3888. + int ret = avcodec_send_packet(context->iCodecCtx, NULL);
  3889. - do {
  3890. - frame_finished = 0;
  3891. + while (ret >= 0) {
  3892. + ret = avcodec_receive_frame(context->iCodecCtx, in_frame);
  3893. - avcodec_decode_video2(
  3894. - context->iCodecCtx, in_frame, &frame_finished,
  3895. - &next_packet);
  3896. + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  3897. + /* No more frames to flush. */
  3898. + break;
  3899. + }
  3900. - if (frame_finished) {
  3901. - index_rebuild_ffmpeg_proc_decoded_frame(
  3902. - context, &next_packet, in_frame);
  3903. + if (ret < 0) {
  3904. + fprintf(stderr, "Error flushing proxy frame: %s\n", av_err2str(ret));
  3905. + break;
  3906. }
  3907. - } while (frame_finished);
  3908. + index_rebuild_ffmpeg_proc_decoded_frame(context, next_packet, in_frame);
  3909. + }
  3910. }
  3911. + av_packet_free(&next_packet);
  3912. av_free(in_frame);
  3913. return 1;
  3914. diff --git a/blender-2.79b/source/blender/imbuf/intern/util.c b/blender-2.79b/source/blender/imbuf/intern/util.c
  3915. index ba8480b..24e360c 100644
  3916. --- a/blender-2.79b/source/blender/imbuf/intern/util.c
  3917. +++ b/blender-2.79b/source/blender/imbuf/intern/util.c
  3918. @@ -290,7 +290,6 @@ static void ffmpeg_log_callback(void *ptr, int level, const char *format, va_lis
  3919. void IMB_ffmpeg_init(void)
  3920. {
  3921. - av_register_all();
  3922. avdevice_register_all();
  3923. ffmpeg_last_error[0] = '\0';
  3924. @@ -312,8 +311,7 @@ static int isffmpeg(const char *filename)
  3925. AVFormatContext *pFormatCtx = NULL;
  3926. unsigned int i;
  3927. int videoStream;
  3928. - AVCodec *pCodec;
  3929. - AVCodecContext *pCodecCtx;
  3930. + const AVCodec *pCodec;
  3931. if (BLI_testextensie_n(
  3932. filename,
  3933. @@ -339,9 +337,8 @@ static int isffmpeg(const char *filename)
  3934. /* Find the first video stream */
  3935. videoStream = -1;
  3936. for (i = 0; i < pFormatCtx->nb_streams; i++)
  3937. - if (pFormatCtx->streams[i] &&
  3938. - pFormatCtx->streams[i]->codec &&
  3939. - (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
  3940. + if (pFormatCtx->streams[i] && pFormatCtx->streams[i]->codecpar &&
  3941. + (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
  3942. {
  3943. videoStream = i;
  3944. break;
  3945. @@ -352,21 +349,15 @@ static int isffmpeg(const char *filename)
  3946. return 0;
  3947. }
  3948. - pCodecCtx = pFormatCtx->streams[videoStream]->codec;
  3949. + AVCodecParameters *codec_par = pFormatCtx->streams[videoStream]->codecpar;
  3950. /* Find the decoder for the video stream */
  3951. - pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
  3952. + pCodec = avcodec_find_decoder(codec_par->codec_id);
  3953. if (pCodec == NULL) {
  3954. avformat_close_input(&pFormatCtx);
  3955. return 0;
  3956. }
  3957. - if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
  3958. - avformat_close_input(&pFormatCtx);
  3959. - return 0;
  3960. - }
  3961. -
  3962. - avcodec_close(pCodecCtx);
  3963. avformat_close_input(&pFormatCtx);
  3964. return 1;
  3965. diff --git a/blender-2.79b/source/blender/makesdna/DNA_scene_types.h b/blender-2.79b/source/blender/makesdna/DNA_scene_types.h
  3966. index 79ee91b..d7e377a 100644
  3967. --- a/blender-2.79b/source/blender/makesdna/DNA_scene_types.h
  3968. +++ b/blender-2.79b/source/blender/makesdna/DNA_scene_types.h
  3969. @@ -174,7 +174,6 @@ typedef struct FFMpegCodecData {
  3970. int audio_bitrate;
  3971. int audio_mixrate;
  3972. int audio_channels;
  3973. - int audio_pad;
  3974. float audio_volume;
  3975. int gop_size;
  3976. int max_b_frames; /* only used if FFMPEG_USE_MAX_B_FRAMES flag is set. */
  3977. @@ -187,9 +186,7 @@ typedef struct FFMpegCodecData {
  3978. int rc_buffer_size;
  3979. int mux_packet_size;
  3980. int mux_rate;
  3981. - int pad1;
  3982. -
  3983. - IDProperty *properties;
  3984. + void *_pad1;
  3985. } FFMpegCodecData;
  3986. /* ************************************************************* */
  3987. diff --git a/blender-2.79b/source/blender/makesrna/intern/rna_scene.c b/blender-2.79b/source/blender/makesrna/intern/rna_scene.c
  3988. index db3ff9b..fa02539 100644
  3989. --- a/blender-2.79b/source/blender/makesrna/intern/rna_scene.c
  3990. +++ b/blender-2.79b/source/blender/makesrna/intern/rna_scene.c
  3991. @@ -1406,20 +1406,12 @@ static void rna_FFmpegSettings_lossless_output_set(PointerRNA *ptr, int value)
  3992. Scene *scene = (Scene *) ptr->id.data;
  3993. RenderData *rd = &scene->r;
  3994. - if (value)
  3995. + if (value) {
  3996. rd->ffcodecdata.flags |= FFMPEG_LOSSLESS_OUTPUT;
  3997. - else
  3998. + }
  3999. + else {
  4000. rd->ffcodecdata.flags &= ~FFMPEG_LOSSLESS_OUTPUT;
  4001. -
  4002. - BKE_ffmpeg_codec_settings_verify(rd);
  4003. -}
  4004. -
  4005. -static void rna_FFmpegSettings_codec_settings_update(Main *UNUSED(bmain), Scene *UNUSED(scene_unused), PointerRNA *ptr)
  4006. -{
  4007. - Scene *scene = (Scene *) ptr->id.data;
  4008. - RenderData *rd = &scene->r;
  4009. -
  4010. - BKE_ffmpeg_codec_settings_verify(rd);
  4011. + }
  4012. }
  4013. #endif
  4014. @@ -5594,7 +5586,6 @@ static void rna_def_scene_ffmpeg_settings(BlenderRNA *brna)
  4015. RNA_def_property_enum_items(prop, ffmpeg_format_items);
  4016. RNA_def_property_enum_default(prop, FFMPEG_MKV);
  4017. RNA_def_property_ui_text(prop, "Container", "Output file container");
  4018. - RNA_def_property_update(prop, NC_SCENE | ND_RENDER_OPTIONS, "rna_FFmpegSettings_codec_settings_update");
  4019. prop = RNA_def_property(srna, "codec", PROP_ENUM, PROP_NONE);
  4020. RNA_def_property_enum_bitflag_sdna(prop, NULL, "codec");
  4021. @@ -5602,7 +5593,6 @@ static void rna_def_scene_ffmpeg_settings(BlenderRNA *brna)
  4022. RNA_def_property_enum_items(prop, ffmpeg_codec_items);
  4023. RNA_def_property_enum_default(prop, AV_CODEC_ID_H264);
  4024. RNA_def_property_ui_text(prop, "Codec", "FFmpeg codec to use");
  4025. - RNA_def_property_update(prop, NC_SCENE | ND_RENDER_OPTIONS, "rna_FFmpegSettings_codec_settings_update");
  4026. prop = RNA_def_property(srna, "video_bitrate", PROP_INT, PROP_NONE);
  4027. RNA_def_property_int_sdna(prop, NULL, "video_bitrate");
  4028. diff --git a/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.cpp b/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.cpp
  4029. index 083e9e2..7246278 100644
  4030. --- a/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.cpp
  4031. +++ b/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.cpp
  4032. @@ -36,6 +36,10 @@
  4033. #define __STDC_CONSTANT_MACROS
  4034. #ifdef __STDC_CONSTANT_MACROS /* quiet warning */
  4035. #endif
  4036. +extern "C" {
  4037. +#include <libavutil/imgutils.h>
  4038. +#include <libswscale/swscale.h>
  4039. +}
  4040. #endif
  4041. #include <stdint.h>
  4042. @@ -63,7 +67,7 @@ const double defFrameRate = 25.0;
  4043. VideoFFmpeg::VideoFFmpeg (HRESULT * hRslt) : VideoBase(),
  4044. m_codec(NULL), m_formatCtx(NULL), m_codecCtx(NULL),
  4045. m_frame(NULL), m_frameDeinterlaced(NULL), m_frameRGB(NULL), m_imgConvertCtx(NULL),
  4046. -m_deinterlace(false), m_preseek(0), m_videoStream(-1), m_baseFrameRate(25.0),
  4047. +m_deinterlace(false), m_preseek(0), m_videoStreamIndex(-1), m_baseFrameRate(25.0),
  4048. m_lastFrame(-1), m_eof(false), m_externTime(false), m_curPosition(-1), m_startTime(0),
  4049. m_captWidth(0), m_captHeight(0), m_captRate(0.f), m_isImage(false),
  4050. m_isThreaded(false), m_isStreaming(false), m_stopThread(false), m_cacheStarted(false)
  4051. @@ -144,20 +148,22 @@ AVFrame *VideoFFmpeg::allocFrameRGB()
  4052. frame = av_frame_alloc();
  4053. if (m_format == RGBA32)
  4054. {
  4055. - avpicture_fill((AVPicture*)frame,
  4056. - (uint8_t*)MEM_callocN(avpicture_get_size(
  4057. - AV_PIX_FMT_RGBA,
  4058. - m_codecCtx->width, m_codecCtx->height),
  4059. - "ffmpeg rgba"),
  4060. - AV_PIX_FMT_RGBA, m_codecCtx->width, m_codecCtx->height);
  4061. + av_image_fill_arrays(frame->data,
  4062. + frame->linesize,
  4063. + (uint8_t*)MEM_mallocN(av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_codecCtx->width, m_codecCtx->height, 1), "ffmpeg rgba"),
  4064. + AV_PIX_FMT_RGBA,
  4065. + m_codecCtx->width,
  4066. + m_codecCtx->height,
  4067. + 1);
  4068. } else
  4069. {
  4070. - avpicture_fill((AVPicture*)frame,
  4071. - (uint8_t*)MEM_callocN(avpicture_get_size(
  4072. - AV_PIX_FMT_RGB24,
  4073. - m_codecCtx->width, m_codecCtx->height),
  4074. - "ffmpeg rgb"),
  4075. - AV_PIX_FMT_RGB24, m_codecCtx->width, m_codecCtx->height);
  4076. + av_image_fill_arrays(frame->data,
  4077. + frame->linesize,
  4078. + (uint8_t*)MEM_mallocN(av_image_get_buffer_size(AV_PIX_FMT_RGB24, m_codecCtx->width, m_codecCtx->height, 1), "ffmpeg rgba"),
  4079. + AV_PIX_FMT_RGBA,
  4080. + m_codecCtx->width,
  4081. + m_codecCtx->height,
  4082. + 1);
  4083. }
  4084. return frame;
  4085. }
  4086. @@ -172,12 +178,13 @@ void VideoFFmpeg::initParams (short width, short height, float rate, bool image)
  4087. }
  4088. -int VideoFFmpeg::openStream(const char *filename, AVInputFormat *inputFormat, AVDictionary **formatParams)
  4089. +int VideoFFmpeg::openStream(const char *filename, const AVInputFormat *inputFormat, AVDictionary **formatParams)
  4090. {
  4091. AVFormatContext *formatCtx = NULL;
  4092. - int i, videoStream;
  4093. - AVCodec *codec;
  4094. + int i, video_stream_index;
  4095. + const AVCodec *codec;
  4096. AVCodecContext *codecCtx;
  4097. + AVStream *video_stream;
  4098. if (avformat_open_input(&formatCtx, filename, inputFormat, formatParams)!=0)
  4099. return -1;
  4100. @@ -188,65 +195,63 @@ int VideoFFmpeg::openStream(const char *filename, AVInputFormat *inputFormat, AV
  4101. return -1;
  4102. }
  4103. - /* Find the first video stream */
  4104. - videoStream=-1;
  4105. - for (i=0; i<formatCtx->nb_streams; i++)
  4106. - {
  4107. + /* Find the video stream */
  4108. + video_stream_index = -1;
  4109. +
  4110. + for (i = 0; i < formatCtx->nb_streams; i++) {
  4111. if (formatCtx->streams[i] &&
  4112. - get_codec_from_stream(formatCtx->streams[i]) &&
  4113. - (get_codec_from_stream(formatCtx->streams[i])->codec_type==AVMEDIA_TYPE_VIDEO))
  4114. + formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  4115. {
  4116. - videoStream=i;
  4117. + video_stream_index = i;
  4118. break;
  4119. }
  4120. }
  4121. - if (videoStream==-1)
  4122. + if (video_stream_index == -1)
  4123. {
  4124. avformat_close_input(&formatCtx);
  4125. return -1;
  4126. }
  4127. - codecCtx = get_codec_from_stream(formatCtx->streams[videoStream]);
  4128. + video_stream = formatCtx->streams[video_stream_index];
  4129. /* Find the decoder for the video stream */
  4130. - codec=avcodec_find_decoder(codecCtx->codec_id);
  4131. - if (codec==NULL)
  4132. - {
  4133. + codec = avcodec_find_decoder(video_stream->codecpar->codec_id);
  4134. + if (codec == nullptr) {
  4135. avformat_close_input(&formatCtx);
  4136. return -1;
  4137. }
  4138. - codecCtx->workaround_bugs = 1;
  4139. - if (avcodec_open2(codecCtx, codec, NULL) < 0)
  4140. +
  4141. + codecCtx = avcodec_alloc_context3(NULL);
  4142. + avcodec_parameters_to_context(codecCtx, video_stream->codecpar);
  4143. + codecCtx->workaround_bugs = FF_BUG_AUTODETECT;
  4144. +
  4145. + if (avcodec_open2(codecCtx, codec, nullptr) < 0)
  4146. {
  4147. avformat_close_input(&formatCtx);
  4148. return -1;
  4149. }
  4150. -#ifdef FFMPEG_OLD_FRAME_RATE
  4151. - if (codecCtx->frame_rate>1000 && codecCtx->frame_rate_base==1)
  4152. - codecCtx->frame_rate_base=1000;
  4153. - m_baseFrameRate = (double)codecCtx->frame_rate / (double)codecCtx->frame_rate_base;
  4154. -#else
  4155. - m_baseFrameRate = av_q2d(av_get_r_frame_rate_compat(formatCtx->streams[videoStream]));
  4156. -#endif
  4157. - if (m_baseFrameRate <= 0.0)
  4158. + m_baseFrameRate = av_q2d(av_guess_frame_rate(formatCtx, video_stream, nullptr));
  4159. + if (m_baseFrameRate <= 0.0) {
  4160. m_baseFrameRate = defFrameRate;
  4161. + }
  4162. m_codec = codec;
  4163. m_codecCtx = codecCtx;
  4164. m_formatCtx = formatCtx;
  4165. - m_videoStream = videoStream;
  4166. + m_videoStreamIndex = video_stream_index;
  4167. m_frame = av_frame_alloc();
  4168. m_frameDeinterlaced = av_frame_alloc();
  4169. // allocate buffer if deinterlacing is required
  4170. - avpicture_fill((AVPicture*)m_frameDeinterlaced,
  4171. - (uint8_t*)MEM_callocN(avpicture_get_size(
  4172. - m_codecCtx->pix_fmt,
  4173. - m_codecCtx->width, m_codecCtx->height),
  4174. - "ffmpeg deinterlace"),
  4175. - m_codecCtx->pix_fmt, m_codecCtx->width, m_codecCtx->height);
  4176. + av_image_fill_arrays(m_frameDeinterlaced->data,
  4177. + m_frameDeinterlaced->linesize,
  4178. + (uint8_t*)MEM_mallocN(av_image_get_buffer_size(m_codecCtx->pix_fmt, m_codecCtx->width, m_codecCtx->height, 1), "ffmpeg deinterlace"),
  4179. + m_codecCtx->pix_fmt,
  4180. + m_codecCtx->width,
  4181. + m_codecCtx->height,
  4182. + 1);
  4183. // check if the pixel format supports Alpha
  4184. if (m_codecCtx->pix_fmt == AV_PIX_FMT_RGB32 ||
  4185. @@ -321,8 +326,8 @@ void *VideoFFmpeg::cacheThread(void *data)
  4186. CachePacket *cachePacket;
  4187. bool endOfFile = false;
  4188. int frameFinished = 0;
  4189. - double timeBase = av_q2d(video->m_formatCtx->streams[video->m_videoStream]->time_base);
  4190. - int64_t startTs = video->m_formatCtx->streams[video->m_videoStream]->start_time;
  4191. + double timeBase = av_q2d(video->m_formatCtx->streams[video->m_videoStreamIndex]->time_base);
  4192. + int64_t startTs = video->m_formatCtx->streams[video->m_videoStreamIndex]->start_time;
  4193. if (startTs == AV_NOPTS_VALUE)
  4194. startTs = 0;
  4195. @@ -340,17 +345,17 @@ void *VideoFFmpeg::cacheThread(void *data)
  4196. // free packet => packet cache is not full yet, just read more
  4197. if (av_read_frame(video->m_formatCtx, &cachePacket->packet)>=0)
  4198. {
  4199. - if (cachePacket->packet.stream_index == video->m_videoStream)
  4200. + if (cachePacket->packet.stream_index == video->m_videoStreamIndex)
  4201. {
  4202. // make sure fresh memory is allocated for the packet and move it to queue
  4203. - av_dup_packet(&cachePacket->packet);
  4204. + av_packet_ref(&cachePacket->packet, nullptr);
  4205. BLI_remlink(&video->m_packetCacheFree, cachePacket);
  4206. BLI_addtail(&video->m_packetCacheBase, cachePacket);
  4207. break;
  4208. } else {
  4209. // this is not a good packet for us, just leave it on free queue
  4210. // Note: here we could handle sound packet
  4211. - av_free_packet(&cachePacket->packet);
  4212. + av_packet_unref(&cachePacket->packet);
  4213. frameFinished++;
  4214. }
  4215. @@ -380,9 +385,8 @@ void *VideoFFmpeg::cacheThread(void *data)
  4216. BLI_remlink(&video->m_packetCacheBase, cachePacket);
  4217. // use m_frame because when caching, it is not used in main thread
  4218. // we can't use currentFrame directly because we need to convert to RGB first
  4219. - avcodec_decode_video2(video->m_codecCtx,
  4220. - video->m_frame, &frameFinished,
  4221. - &cachePacket->packet);
  4222. + avcodec_send_packet(video->m_codecCtx, &cachePacket->packet);
  4223. + frameFinished = avcodec_receive_frame(video->m_codecCtx, video->m_frame) == 0;
  4224. if (frameFinished)
  4225. {
  4226. AVFrame * input = video->m_frame;
  4227. @@ -393,9 +397,9 @@ void *VideoFFmpeg::cacheThread(void *data)
  4228. {
  4229. if (video->m_deinterlace)
  4230. {
  4231. - if (avpicture_deinterlace(
  4232. - (AVPicture*) video->m_frameDeinterlaced,
  4233. - (const AVPicture*) video->m_frame,
  4234. + if (av_image_deinterlace(
  4235. + video->m_frameDeinterlaced,
  4236. + video->m_frame,
  4237. video->m_codecCtx->pix_fmt,
  4238. video->m_codecCtx->width,
  4239. video->m_codecCtx->height) >= 0)
  4240. @@ -420,7 +424,7 @@ void *VideoFFmpeg::cacheThread(void *data)
  4241. currentFrame = NULL;
  4242. }
  4243. }
  4244. - av_free_packet(&cachePacket->packet);
  4245. + av_packet_unref(&cachePacket->packet);
  4246. BLI_addtail(&video->m_packetCacheFree, cachePacket);
  4247. }
  4248. if (currentFrame && endOfFile)
  4249. @@ -500,7 +504,7 @@ void VideoFFmpeg::stopCache()
  4250. while ((packet = (CachePacket *)m_packetCacheBase.first) != NULL)
  4251. {
  4252. BLI_remlink(&m_packetCacheBase, packet);
  4253. - av_free_packet(&packet->packet);
  4254. + av_packet_unref(&packet->packet);
  4255. delete packet;
  4256. }
  4257. while ((packet = (CachePacket *)m_packetCacheFree.first) != NULL)
  4258. @@ -590,7 +594,7 @@ void VideoFFmpeg::openFile (char *filename)
  4259. void VideoFFmpeg::openCam (char *file, short camIdx)
  4260. {
  4261. // open camera source
  4262. - AVInputFormat *inputFormat;
  4263. + const AVInputFormat *inputFormat;
  4264. AVDictionary *formatParams = NULL;
  4265. char filename[28], rateStr[20];
  4266. @@ -930,8 +934,8 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4267. pthread_mutex_unlock(&m_cacheMutex);
  4268. } while (true);
  4269. }
  4270. - double timeBase = av_q2d(m_formatCtx->streams[m_videoStream]->time_base);
  4271. - int64_t startTs = m_formatCtx->streams[m_videoStream]->start_time;
  4272. + double timeBase = av_q2d(m_formatCtx->streams[m_videoStreamIndex]->time_base);
  4273. + int64_t startTs = m_formatCtx->streams[m_videoStreamIndex]->start_time;
  4274. if (startTs == AV_NOPTS_VALUE)
  4275. startTs = 0;
  4276. @@ -947,18 +951,16 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4277. {
  4278. while (av_read_frame(m_formatCtx, &packet)>=0)
  4279. {
  4280. - if (packet.stream_index == m_videoStream)
  4281. + if (packet.stream_index == m_videoStreamIndex)
  4282. {
  4283. - avcodec_decode_video2(
  4284. - m_codecCtx,
  4285. - m_frame, &frameFinished,
  4286. - &packet);
  4287. + avcodec_send_packet(m_codecCtx, &packet);
  4288. + frameFinished = avcodec_receive_frame(m_codecCtx, m_frame) == 0;
  4289. if (frameFinished)
  4290. {
  4291. m_curPosition = (long)((packet.dts-startTs) * (m_baseFrameRate*timeBase) + 0.5);
  4292. }
  4293. }
  4294. - av_free_packet(&packet);
  4295. + av_packet_unref(&packet);
  4296. if (position == m_curPosition+1)
  4297. break;
  4298. }
  4299. @@ -983,10 +985,10 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4300. if (position <= m_preseek)
  4301. {
  4302. // we can safely go the beginning of the file
  4303. - if (av_seek_frame(m_formatCtx, m_videoStream, 0, AVSEEK_FLAG_BYTE) >= 0)
  4304. + if (av_seek_frame(m_formatCtx, m_videoStreamIndex, 0, AVSEEK_FLAG_BYTE) >= 0)
  4305. {
  4306. // binary seek does not reset the timestamp, must do it now
  4307. - av_update_cur_dts(m_formatCtx, m_formatCtx->streams[m_videoStream], startTs);
  4308. + av_update_cur_dts(m_formatCtx, m_formatCtx->streams[m_videoStreamIndex], startTs);
  4309. m_curPosition = 0;
  4310. }
  4311. }
  4312. @@ -994,7 +996,7 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4313. #endif
  4314. {
  4315. // current position is now lost, guess a value.
  4316. - if (av_seek_frame(m_formatCtx, m_videoStream, pos, AVSEEK_FLAG_BACKWARD) >= 0)
  4317. + if (av_seek_frame(m_formatCtx, m_videoStreamIndex, pos, AVSEEK_FLAG_BACKWARD) >= 0)
  4318. {
  4319. // current position is now lost, guess a value.
  4320. // It's not important because it will be set at this end of this function
  4321. @@ -1022,14 +1024,15 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4322. // return the next frame. This is not quite correct, may need more work
  4323. while (av_read_frame(m_formatCtx, &packet) >= 0)
  4324. {
  4325. - if (packet.stream_index == m_videoStream)
  4326. + if (packet.stream_index == m_videoStreamIndex)
  4327. {
  4328. AVFrame *input = m_frame;
  4329. short counter = 0;
  4330. /* If m_isImage, while the data is not read properly (png, tiffs, etc formats may need several pass), else don't need while loop*/
  4331. do {
  4332. - avcodec_decode_video2(m_codecCtx, m_frame, &frameFinished, &packet);
  4333. + avcodec_send_packet(m_codecCtx, &packet);
  4334. + frameFinished = avcodec_receive_frame(m_codecCtx, m_frame) == 0;
  4335. counter++;
  4336. } while ((input->data[0] == 0 && input->data[1] == 0 && input->data[2] == 0 && input->data[3] == 0) && counter < 10 && m_isImage);
  4337. @@ -1052,15 +1055,15 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4338. if ( input->data[0]==0 && input->data[1]==0
  4339. && input->data[2]==0 && input->data[3]==0)
  4340. {
  4341. - av_free_packet(&packet);
  4342. + av_packet_unref(&packet);
  4343. break;
  4344. }
  4345. if (m_deinterlace)
  4346. {
  4347. - if (avpicture_deinterlace(
  4348. - (AVPicture*) m_frameDeinterlaced,
  4349. - (const AVPicture*) m_frame,
  4350. + if (av_image_deinterlace(
  4351. + m_frameDeinterlaced,
  4352. + m_frame,
  4353. m_codecCtx->pix_fmt,
  4354. m_codecCtx->width,
  4355. m_codecCtx->height) >= 0)
  4356. @@ -1076,12 +1079,12 @@ AVFrame *VideoFFmpeg::grabFrame(long position)
  4357. m_codecCtx->height,
  4358. m_frameRGB->data,
  4359. m_frameRGB->linesize);
  4360. - av_free_packet(&packet);
  4361. + av_packet_unref(&packet);
  4362. frameLoaded = true;
  4363. break;
  4364. }
  4365. }
  4366. - av_free_packet(&packet);
  4367. + av_packet_unref(&packet);
  4368. }
  4369. m_eof = m_isFile && !frameLoaded;
  4370. if (frameLoaded)
  4371. diff --git a/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.h b/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.h
  4372. index 0a49a0b..b25d569 100644
  4373. --- a/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.h
  4374. +++ b/blender-2.79b/source/gameengine/VideoTexture/VideoFFmpeg.h
  4375. @@ -36,32 +36,17 @@
  4376. #if defined(__FreeBSD__)
  4377. # include <inttypes.h>
  4378. #endif
  4379. +
  4380. +struct AVCodecContext;
  4381. extern "C" {
  4382. #include <pthread.h>
  4383. #include "ffmpeg_compat.h"
  4384. #include "DNA_listBase.h"
  4385. #include "BLI_threads.h"
  4386. #include "BLI_blenlib.h"
  4387. +#include <libavcodec/avcodec.h>
  4388. }
  4389. -#if LIBAVFORMAT_VERSION_INT < (49 << 16)
  4390. -# define FFMPEG_OLD_FRAME_RATE 1
  4391. -#else
  4392. -# define FFMPEG_CODEC_IS_POINTER 1
  4393. -#endif
  4394. -
  4395. -#ifdef FFMPEG_CODEC_IS_POINTER
  4396. -static inline AVCodecContext *get_codec_from_stream(AVStream* stream)
  4397. -{
  4398. - return stream->codec;
  4399. -}
  4400. -#else
  4401. -static inline AVCodecContext *get_codec_from_stream(AVStream* stream)
  4402. -{
  4403. - return &stream->codec;
  4404. -}
  4405. -#endif
  4406. -
  4407. #include "VideoBase.h"
  4408. #define CACHE_FRAME_SIZE 10
  4409. @@ -106,7 +91,7 @@ public:
  4410. protected:
  4411. // format and codec information
  4412. - AVCodec *m_codec;
  4413. + const AVCodec *m_codec;
  4414. AVFormatContext *m_formatCtx;
  4415. AVCodecContext *m_codecCtx;
  4416. // raw frame extracted from video file
  4417. @@ -122,7 +107,7 @@ protected:
  4418. // number of frame of preseek
  4419. int m_preseek;
  4420. // order number of stream holding the video in format context
  4421. - int m_videoStream;
  4422. + int m_videoStreamIndex;
  4423. // the actual frame rate
  4424. double m_baseFrameRate;
  4425. @@ -173,7 +158,7 @@ protected:
  4426. double actFrameRate (void) { return m_frameRate * m_baseFrameRate; }
  4427. /// common function to video file and capture
  4428. - int openStream(const char *filename, AVInputFormat *inputFormat, AVDictionary **formatParams);
  4429. + int openStream(const char *filename, const AVInputFormat *inputFormat, AVDictionary **formatParams);
  4430. /// check if a frame is available and load it in pFrame, return true if a frame could be retrieved
  4431. AVFrame* grabFrame(long frame);