utils.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892
  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/atomic.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/bprint.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/crc.h"
  34. #include "libavutil/frame.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/mem_internal.h"
  38. #include "libavutil/pixdesc.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/samplefmt.h"
  41. #include "libavutil/dict.h"
  42. #include "avcodec.h"
  43. #include "libavutil/opt.h"
  44. #include "me_cmp.h"
  45. #include "mpegvideo.h"
  46. #include "thread.h"
  47. #include "frame_thread_encoder.h"
  48. #include "internal.h"
  49. #include "raw.h"
  50. #include "bytestream.h"
  51. #include "version.h"
  52. #include <stdlib.h>
  53. #include <stdarg.h>
  54. #include <limits.h>
  55. #include <float.h>
  56. #if CONFIG_ICONV
  57. # include <iconv.h>
  58. #endif
  59. #if HAVE_PTHREADS
  60. #include <pthread.h>
  61. #elif HAVE_W32THREADS
  62. #include "compat/w32pthreads.h"
  63. #elif HAVE_OS2THREADS
  64. #include "compat/os2threads.h"
  65. #endif
  66. #include "libavutil/ffversion.h"
  67. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  68. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  69. static int default_lockmgr_cb(void **arg, enum AVLockOp op)
  70. {
  71. void * volatile * mutex = arg;
  72. int err;
  73. switch (op) {
  74. case AV_LOCK_CREATE:
  75. return 0;
  76. case AV_LOCK_OBTAIN:
  77. if (!*mutex) {
  78. pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
  79. if (!tmp)
  80. return AVERROR(ENOMEM);
  81. if ((err = pthread_mutex_init(tmp, NULL))) {
  82. av_free(tmp);
  83. return AVERROR(err);
  84. }
  85. if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
  86. pthread_mutex_destroy(tmp);
  87. av_free(tmp);
  88. }
  89. }
  90. if ((err = pthread_mutex_lock(*mutex)))
  91. return AVERROR(err);
  92. return 0;
  93. case AV_LOCK_RELEASE:
  94. if ((err = pthread_mutex_unlock(*mutex)))
  95. return AVERROR(err);
  96. return 0;
  97. case AV_LOCK_DESTROY:
  98. if (*mutex)
  99. pthread_mutex_destroy(*mutex);
  100. av_free(*mutex);
  101. avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
  107. #else
  108. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
  109. #endif
  110. volatile int ff_avcodec_locked;
  111. static int volatile entangled_thread_counter = 0;
  112. static void *codec_mutex;
  113. static void *avformat_mutex;
  114. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  115. {
  116. uint8_t **p = ptr;
  117. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  118. av_freep(p);
  119. *size = 0;
  120. return;
  121. }
  122. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  123. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  124. }
  125. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  126. {
  127. uint8_t **p = ptr;
  128. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  129. av_freep(p);
  130. *size = 0;
  131. return;
  132. }
  133. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  134. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  135. }
  136. /* encoder management */
  137. static AVCodec *first_avcodec = NULL;
  138. static AVCodec **last_avcodec = &first_avcodec;
  139. AVCodec *av_codec_next(const AVCodec *c)
  140. {
  141. if (c)
  142. return c->next;
  143. else
  144. return first_avcodec;
  145. }
  146. static av_cold void avcodec_init(void)
  147. {
  148. static int initialized = 0;
  149. if (initialized != 0)
  150. return;
  151. initialized = 1;
  152. if (CONFIG_ME_CMP)
  153. ff_me_cmp_init_static();
  154. }
  155. int av_codec_is_encoder(const AVCodec *codec)
  156. {
  157. return codec && (codec->encode_sub || codec->encode2);
  158. }
  159. int av_codec_is_decoder(const AVCodec *codec)
  160. {
  161. return codec && codec->decode;
  162. }
  163. av_cold void avcodec_register(AVCodec *codec)
  164. {
  165. AVCodec **p;
  166. avcodec_init();
  167. p = last_avcodec;
  168. codec->next = NULL;
  169. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
  170. p = &(*p)->next;
  171. last_avcodec = &codec->next;
  172. if (codec->init_static_data)
  173. codec->init_static_data(codec);
  174. }
  175. #if FF_API_EMU_EDGE
  176. unsigned avcodec_get_edge_width(void)
  177. {
  178. return EDGE_WIDTH;
  179. }
  180. #endif
  181. #if FF_API_SET_DIMENSIONS
  182. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  183. {
  184. int ret = ff_set_dimensions(s, width, height);
  185. if (ret < 0) {
  186. av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
  187. }
  188. }
  189. #endif
  190. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  191. {
  192. int ret = av_image_check_size(width, height, 0, s);
  193. if (ret < 0)
  194. width = height = 0;
  195. s->coded_width = width;
  196. s->coded_height = height;
  197. s->width = FF_CEIL_RSHIFT(width, s->lowres);
  198. s->height = FF_CEIL_RSHIFT(height, s->lowres);
  199. return ret;
  200. }
  201. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  202. {
  203. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  204. if (ret < 0) {
  205. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  206. sar.num, sar.den);
  207. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  208. return ret;
  209. } else {
  210. avctx->sample_aspect_ratio = sar;
  211. }
  212. return 0;
  213. }
  214. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  215. enum AVMatrixEncoding matrix_encoding)
  216. {
  217. AVFrameSideData *side_data;
  218. enum AVMatrixEncoding *data;
  219. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  220. if (!side_data)
  221. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  222. sizeof(enum AVMatrixEncoding));
  223. if (!side_data)
  224. return AVERROR(ENOMEM);
  225. data = (enum AVMatrixEncoding*)side_data->data;
  226. *data = matrix_encoding;
  227. return 0;
  228. }
  229. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  230. int linesize_align[AV_NUM_DATA_POINTERS])
  231. {
  232. int i;
  233. int w_align = 1;
  234. int h_align = 1;
  235. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  236. if (desc) {
  237. w_align = 1 << desc->log2_chroma_w;
  238. h_align = 1 << desc->log2_chroma_h;
  239. }
  240. switch (s->pix_fmt) {
  241. case AV_PIX_FMT_YUV420P:
  242. case AV_PIX_FMT_YUYV422:
  243. case AV_PIX_FMT_YVYU422:
  244. case AV_PIX_FMT_UYVY422:
  245. case AV_PIX_FMT_YUV422P:
  246. case AV_PIX_FMT_YUV440P:
  247. case AV_PIX_FMT_YUV444P:
  248. case AV_PIX_FMT_GBRP:
  249. case AV_PIX_FMT_GBRAP:
  250. case AV_PIX_FMT_GRAY8:
  251. case AV_PIX_FMT_GRAY16BE:
  252. case AV_PIX_FMT_GRAY16LE:
  253. case AV_PIX_FMT_YUVJ420P:
  254. case AV_PIX_FMT_YUVJ422P:
  255. case AV_PIX_FMT_YUVJ440P:
  256. case AV_PIX_FMT_YUVJ444P:
  257. case AV_PIX_FMT_YUVA420P:
  258. case AV_PIX_FMT_YUVA422P:
  259. case AV_PIX_FMT_YUVA444P:
  260. case AV_PIX_FMT_YUV420P9LE:
  261. case AV_PIX_FMT_YUV420P9BE:
  262. case AV_PIX_FMT_YUV420P10LE:
  263. case AV_PIX_FMT_YUV420P10BE:
  264. case AV_PIX_FMT_YUV420P12LE:
  265. case AV_PIX_FMT_YUV420P12BE:
  266. case AV_PIX_FMT_YUV420P14LE:
  267. case AV_PIX_FMT_YUV420P14BE:
  268. case AV_PIX_FMT_YUV420P16LE:
  269. case AV_PIX_FMT_YUV420P16BE:
  270. case AV_PIX_FMT_YUVA420P9LE:
  271. case AV_PIX_FMT_YUVA420P9BE:
  272. case AV_PIX_FMT_YUVA420P10LE:
  273. case AV_PIX_FMT_YUVA420P10BE:
  274. case AV_PIX_FMT_YUVA420P16LE:
  275. case AV_PIX_FMT_YUVA420P16BE:
  276. case AV_PIX_FMT_YUV422P9LE:
  277. case AV_PIX_FMT_YUV422P9BE:
  278. case AV_PIX_FMT_YUV422P10LE:
  279. case AV_PIX_FMT_YUV422P10BE:
  280. case AV_PIX_FMT_YUV422P12LE:
  281. case AV_PIX_FMT_YUV422P12BE:
  282. case AV_PIX_FMT_YUV422P14LE:
  283. case AV_PIX_FMT_YUV422P14BE:
  284. case AV_PIX_FMT_YUV422P16LE:
  285. case AV_PIX_FMT_YUV422P16BE:
  286. case AV_PIX_FMT_YUVA422P9LE:
  287. case AV_PIX_FMT_YUVA422P9BE:
  288. case AV_PIX_FMT_YUVA422P10LE:
  289. case AV_PIX_FMT_YUVA422P10BE:
  290. case AV_PIX_FMT_YUVA422P16LE:
  291. case AV_PIX_FMT_YUVA422P16BE:
  292. case AV_PIX_FMT_YUV440P10LE:
  293. case AV_PIX_FMT_YUV440P10BE:
  294. case AV_PIX_FMT_YUV440P12LE:
  295. case AV_PIX_FMT_YUV440P12BE:
  296. case AV_PIX_FMT_YUV444P9LE:
  297. case AV_PIX_FMT_YUV444P9BE:
  298. case AV_PIX_FMT_YUV444P10LE:
  299. case AV_PIX_FMT_YUV444P10BE:
  300. case AV_PIX_FMT_YUV444P12LE:
  301. case AV_PIX_FMT_YUV444P12BE:
  302. case AV_PIX_FMT_YUV444P14LE:
  303. case AV_PIX_FMT_YUV444P14BE:
  304. case AV_PIX_FMT_YUV444P16LE:
  305. case AV_PIX_FMT_YUV444P16BE:
  306. case AV_PIX_FMT_YUVA444P9LE:
  307. case AV_PIX_FMT_YUVA444P9BE:
  308. case AV_PIX_FMT_YUVA444P10LE:
  309. case AV_PIX_FMT_YUVA444P10BE:
  310. case AV_PIX_FMT_YUVA444P16LE:
  311. case AV_PIX_FMT_YUVA444P16BE:
  312. case AV_PIX_FMT_GBRP9LE:
  313. case AV_PIX_FMT_GBRP9BE:
  314. case AV_PIX_FMT_GBRP10LE:
  315. case AV_PIX_FMT_GBRP10BE:
  316. case AV_PIX_FMT_GBRP12LE:
  317. case AV_PIX_FMT_GBRP12BE:
  318. case AV_PIX_FMT_GBRP14LE:
  319. case AV_PIX_FMT_GBRP14BE:
  320. case AV_PIX_FMT_GBRP16LE:
  321. case AV_PIX_FMT_GBRP16BE:
  322. w_align = 16; //FIXME assume 16 pixel per macroblock
  323. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  324. break;
  325. case AV_PIX_FMT_YUV411P:
  326. case AV_PIX_FMT_YUVJ411P:
  327. case AV_PIX_FMT_UYYVYY411:
  328. w_align = 32;
  329. h_align = 16 * 2;
  330. break;
  331. case AV_PIX_FMT_YUV410P:
  332. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  333. w_align = 64;
  334. h_align = 64;
  335. }
  336. break;
  337. case AV_PIX_FMT_RGB555:
  338. if (s->codec_id == AV_CODEC_ID_RPZA) {
  339. w_align = 4;
  340. h_align = 4;
  341. }
  342. break;
  343. case AV_PIX_FMT_PAL8:
  344. case AV_PIX_FMT_BGR8:
  345. case AV_PIX_FMT_RGB8:
  346. if (s->codec_id == AV_CODEC_ID_SMC ||
  347. s->codec_id == AV_CODEC_ID_CINEPAK) {
  348. w_align = 4;
  349. h_align = 4;
  350. }
  351. if (s->codec_id == AV_CODEC_ID_JV) {
  352. w_align = 8;
  353. h_align = 8;
  354. }
  355. break;
  356. case AV_PIX_FMT_BGR24:
  357. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  358. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  359. w_align = 4;
  360. h_align = 4;
  361. }
  362. break;
  363. case AV_PIX_FMT_RGB24:
  364. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  365. w_align = 4;
  366. h_align = 4;
  367. }
  368. break;
  369. default:
  370. break;
  371. }
  372. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  373. w_align = FFMAX(w_align, 8);
  374. }
  375. *width = FFALIGN(*width, w_align);
  376. *height = FFALIGN(*height, h_align);
  377. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres) {
  378. // some of the optimized chroma MC reads one line too much
  379. // which is also done in mpeg decoders with lowres > 0
  380. *height += 2;
  381. // H.264 uses edge emulation for out of frame motion vectors, for this
  382. // it requires a temporary area large enough to hold a 21x21 block,
  383. // increasing witdth ensure that the temporary area is large enough,
  384. // the next rounded up width is 32
  385. *width = FFMAX(*width, 32);
  386. }
  387. for (i = 0; i < 4; i++)
  388. linesize_align[i] = STRIDE_ALIGN;
  389. }
  390. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  391. {
  392. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  393. int chroma_shift = desc->log2_chroma_w;
  394. int linesize_align[AV_NUM_DATA_POINTERS];
  395. int align;
  396. avcodec_align_dimensions2(s, width, height, linesize_align);
  397. align = FFMAX(linesize_align[0], linesize_align[3]);
  398. linesize_align[1] <<= chroma_shift;
  399. linesize_align[2] <<= chroma_shift;
  400. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  401. *width = FFALIGN(*width, align);
  402. }
  403. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  404. {
  405. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  406. return AVERROR(EINVAL);
  407. pos--;
  408. *xpos = (pos&1) * 128;
  409. *ypos = ((pos>>1)^(pos<4)) * 128;
  410. return 0;
  411. }
  412. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  413. {
  414. int pos, xout, yout;
  415. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  416. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  417. return pos;
  418. }
  419. return AVCHROMA_LOC_UNSPECIFIED;
  420. }
  421. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  422. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  423. int buf_size, int align)
  424. {
  425. int ch, planar, needed_size, ret = 0;
  426. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  427. frame->nb_samples, sample_fmt,
  428. align);
  429. if (buf_size < needed_size)
  430. return AVERROR(EINVAL);
  431. planar = av_sample_fmt_is_planar(sample_fmt);
  432. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  433. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  434. sizeof(*frame->extended_data))))
  435. return AVERROR(ENOMEM);
  436. } else {
  437. frame->extended_data = frame->data;
  438. }
  439. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  440. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  441. sample_fmt, align)) < 0) {
  442. if (frame->extended_data != frame->data)
  443. av_freep(&frame->extended_data);
  444. return ret;
  445. }
  446. if (frame->extended_data != frame->data) {
  447. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  448. frame->data[ch] = frame->extended_data[ch];
  449. }
  450. return ret;
  451. }
  452. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  453. {
  454. FramePool *pool = avctx->internal->pool;
  455. int i, ret;
  456. switch (avctx->codec_type) {
  457. case AVMEDIA_TYPE_VIDEO: {
  458. AVPicture picture;
  459. int size[4] = { 0 };
  460. int w = frame->width;
  461. int h = frame->height;
  462. int tmpsize, unaligned;
  463. if (pool->format == frame->format &&
  464. pool->width == frame->width && pool->height == frame->height)
  465. return 0;
  466. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  467. do {
  468. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  469. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  470. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  471. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  472. w += w & ~(w - 1);
  473. unaligned = 0;
  474. for (i = 0; i < 4; i++)
  475. unaligned |= picture.linesize[i] % pool->stride_align[i];
  476. } while (unaligned);
  477. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  478. NULL, picture.linesize);
  479. if (tmpsize < 0)
  480. return -1;
  481. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  482. size[i] = picture.data[i + 1] - picture.data[i];
  483. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  484. for (i = 0; i < 4; i++) {
  485. av_buffer_pool_uninit(&pool->pools[i]);
  486. pool->linesize[i] = picture.linesize[i];
  487. if (size[i]) {
  488. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  489. CONFIG_MEMORY_POISONING ?
  490. NULL :
  491. av_buffer_allocz);
  492. if (!pool->pools[i]) {
  493. ret = AVERROR(ENOMEM);
  494. goto fail;
  495. }
  496. }
  497. }
  498. pool->format = frame->format;
  499. pool->width = frame->width;
  500. pool->height = frame->height;
  501. break;
  502. }
  503. case AVMEDIA_TYPE_AUDIO: {
  504. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  505. int planar = av_sample_fmt_is_planar(frame->format);
  506. int planes = planar ? ch : 1;
  507. if (pool->format == frame->format && pool->planes == planes &&
  508. pool->channels == ch && frame->nb_samples == pool->samples)
  509. return 0;
  510. av_buffer_pool_uninit(&pool->pools[0]);
  511. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  512. frame->nb_samples, frame->format, 0);
  513. if (ret < 0)
  514. goto fail;
  515. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  516. if (!pool->pools[0]) {
  517. ret = AVERROR(ENOMEM);
  518. goto fail;
  519. }
  520. pool->format = frame->format;
  521. pool->planes = planes;
  522. pool->channels = ch;
  523. pool->samples = frame->nb_samples;
  524. break;
  525. }
  526. default: av_assert0(0);
  527. }
  528. return 0;
  529. fail:
  530. for (i = 0; i < 4; i++)
  531. av_buffer_pool_uninit(&pool->pools[i]);
  532. pool->format = -1;
  533. pool->planes = pool->channels = pool->samples = 0;
  534. pool->width = pool->height = 0;
  535. return ret;
  536. }
  537. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  538. {
  539. FramePool *pool = avctx->internal->pool;
  540. int planes = pool->planes;
  541. int i;
  542. frame->linesize[0] = pool->linesize[0];
  543. if (planes > AV_NUM_DATA_POINTERS) {
  544. frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
  545. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  546. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  547. sizeof(*frame->extended_buf));
  548. if (!frame->extended_data || !frame->extended_buf) {
  549. av_freep(&frame->extended_data);
  550. av_freep(&frame->extended_buf);
  551. return AVERROR(ENOMEM);
  552. }
  553. } else {
  554. frame->extended_data = frame->data;
  555. av_assert0(frame->nb_extended_buf == 0);
  556. }
  557. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  558. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  559. if (!frame->buf[i])
  560. goto fail;
  561. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  562. }
  563. for (i = 0; i < frame->nb_extended_buf; i++) {
  564. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  565. if (!frame->extended_buf[i])
  566. goto fail;
  567. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  568. }
  569. if (avctx->debug & FF_DEBUG_BUFFERS)
  570. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  571. return 0;
  572. fail:
  573. av_frame_unref(frame);
  574. return AVERROR(ENOMEM);
  575. }
  576. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  577. {
  578. FramePool *pool = s->internal->pool;
  579. int i;
  580. if (pic->data[0]) {
  581. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  582. return -1;
  583. }
  584. memset(pic->data, 0, sizeof(pic->data));
  585. pic->extended_data = pic->data;
  586. for (i = 0; i < 4 && pool->pools[i]; i++) {
  587. pic->linesize[i] = pool->linesize[i];
  588. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  589. if (!pic->buf[i])
  590. goto fail;
  591. pic->data[i] = pic->buf[i]->data;
  592. }
  593. for (; i < AV_NUM_DATA_POINTERS; i++) {
  594. pic->data[i] = NULL;
  595. pic->linesize[i] = 0;
  596. }
  597. if (pic->data[1] && !pic->data[2])
  598. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  599. if (s->debug & FF_DEBUG_BUFFERS)
  600. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  601. return 0;
  602. fail:
  603. av_frame_unref(pic);
  604. return AVERROR(ENOMEM);
  605. }
  606. void avpriv_color_frame(AVFrame *frame, const int c[4])
  607. {
  608. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  609. int p, y, x;
  610. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  611. for (p = 0; p<desc->nb_components; p++) {
  612. uint8_t *dst = frame->data[p];
  613. int is_chroma = p == 1 || p == 2;
  614. int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  615. int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  616. for (y = 0; y < height; y++) {
  617. if (desc->comp[0].depth_minus1 >= 8) {
  618. for (x = 0; x<bytes; x++)
  619. ((uint16_t*)dst)[x] = c[p];
  620. }else
  621. memset(dst, c[p], bytes);
  622. dst += frame->linesize[p];
  623. }
  624. }
  625. }
  626. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  627. {
  628. int ret;
  629. if ((ret = update_frame_pool(avctx, frame)) < 0)
  630. return ret;
  631. #if FF_API_GET_BUFFER
  632. FF_DISABLE_DEPRECATION_WARNINGS
  633. frame->type = FF_BUFFER_TYPE_INTERNAL;
  634. FF_ENABLE_DEPRECATION_WARNINGS
  635. #endif
  636. switch (avctx->codec_type) {
  637. case AVMEDIA_TYPE_VIDEO:
  638. return video_get_buffer(avctx, frame);
  639. case AVMEDIA_TYPE_AUDIO:
  640. return audio_get_buffer(avctx, frame);
  641. default:
  642. return -1;
  643. }
  644. }
  645. static int add_metadata_from_side_data(AVPacket *avpkt, AVFrame *frame)
  646. {
  647. int size;
  648. const uint8_t *side_metadata;
  649. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  650. side_metadata = av_packet_get_side_data(avpkt,
  651. AV_PKT_DATA_STRINGS_METADATA, &size);
  652. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  653. }
  654. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  655. {
  656. AVPacket *pkt = avctx->internal->pkt;
  657. int i;
  658. static const struct {
  659. enum AVPacketSideDataType packet;
  660. enum AVFrameSideDataType frame;
  661. } sd[] = {
  662. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  663. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  664. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  665. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  666. };
  667. if (pkt) {
  668. frame->pkt_pts = pkt->pts;
  669. av_frame_set_pkt_pos (frame, pkt->pos);
  670. av_frame_set_pkt_duration(frame, pkt->duration);
  671. av_frame_set_pkt_size (frame, pkt->size);
  672. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  673. int size;
  674. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  675. if (packet_sd) {
  676. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  677. sd[i].frame,
  678. size);
  679. if (!frame_sd)
  680. return AVERROR(ENOMEM);
  681. memcpy(frame_sd->data, packet_sd, size);
  682. }
  683. }
  684. add_metadata_from_side_data(pkt, frame);
  685. } else {
  686. frame->pkt_pts = AV_NOPTS_VALUE;
  687. av_frame_set_pkt_pos (frame, -1);
  688. av_frame_set_pkt_duration(frame, 0);
  689. av_frame_set_pkt_size (frame, -1);
  690. }
  691. frame->reordered_opaque = avctx->reordered_opaque;
  692. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  693. frame->color_primaries = avctx->color_primaries;
  694. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  695. frame->color_trc = avctx->color_trc;
  696. if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  697. av_frame_set_colorspace(frame, avctx->colorspace);
  698. if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  699. av_frame_set_color_range(frame, avctx->color_range);
  700. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  701. frame->chroma_location = avctx->chroma_sample_location;
  702. switch (avctx->codec->type) {
  703. case AVMEDIA_TYPE_VIDEO:
  704. frame->format = avctx->pix_fmt;
  705. if (!frame->sample_aspect_ratio.num)
  706. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  707. if (frame->width && frame->height &&
  708. av_image_check_sar(frame->width, frame->height,
  709. frame->sample_aspect_ratio) < 0) {
  710. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  711. frame->sample_aspect_ratio.num,
  712. frame->sample_aspect_ratio.den);
  713. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  714. }
  715. break;
  716. case AVMEDIA_TYPE_AUDIO:
  717. if (!frame->sample_rate)
  718. frame->sample_rate = avctx->sample_rate;
  719. if (frame->format < 0)
  720. frame->format = avctx->sample_fmt;
  721. if (!frame->channel_layout) {
  722. if (avctx->channel_layout) {
  723. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  724. avctx->channels) {
  725. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  726. "configuration.\n");
  727. return AVERROR(EINVAL);
  728. }
  729. frame->channel_layout = avctx->channel_layout;
  730. } else {
  731. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  732. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  733. avctx->channels);
  734. return AVERROR(ENOSYS);
  735. }
  736. }
  737. }
  738. av_frame_set_channels(frame, avctx->channels);
  739. break;
  740. }
  741. return 0;
  742. }
  743. #if FF_API_GET_BUFFER
  744. FF_DISABLE_DEPRECATION_WARNINGS
  745. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  746. {
  747. return avcodec_default_get_buffer2(avctx, frame, 0);
  748. }
  749. typedef struct CompatReleaseBufPriv {
  750. AVCodecContext avctx;
  751. AVFrame frame;
  752. uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
  753. } CompatReleaseBufPriv;
  754. static void compat_free_buffer(void *opaque, uint8_t *data)
  755. {
  756. CompatReleaseBufPriv *priv = opaque;
  757. if (priv->avctx.release_buffer)
  758. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  759. av_freep(&priv);
  760. }
  761. static void compat_release_buffer(void *opaque, uint8_t *data)
  762. {
  763. AVBufferRef *buf = opaque;
  764. av_buffer_unref(&buf);
  765. }
  766. FF_ENABLE_DEPRECATION_WARNINGS
  767. #endif
  768. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  769. {
  770. return ff_init_buffer_info(avctx, frame);
  771. }
  772. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  773. {
  774. const AVHWAccel *hwaccel = avctx->hwaccel;
  775. int override_dimensions = 1;
  776. int ret;
  777. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  778. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  779. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  780. return AVERROR(EINVAL);
  781. }
  782. }
  783. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  784. if (frame->width <= 0 || frame->height <= 0) {
  785. frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  786. frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  787. override_dimensions = 0;
  788. }
  789. }
  790. ret = ff_decode_frame_props(avctx, frame);
  791. if (ret < 0)
  792. return ret;
  793. if (hwaccel) {
  794. if (hwaccel->alloc_frame) {
  795. ret = hwaccel->alloc_frame(avctx, frame);
  796. goto end;
  797. }
  798. } else
  799. avctx->sw_pix_fmt = avctx->pix_fmt;
  800. #if FF_API_GET_BUFFER
  801. FF_DISABLE_DEPRECATION_WARNINGS
  802. /*
  803. * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
  804. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  805. * a dummy AVBuffer as its private data, unreffing it on free.
  806. * When all the planes are freed, the dummy buffer's free callback calls
  807. * release_buffer().
  808. */
  809. if (avctx->get_buffer) {
  810. CompatReleaseBufPriv *priv = NULL;
  811. AVBufferRef *dummy_buf = NULL;
  812. int planes, i, ret;
  813. if (flags & AV_GET_BUFFER_FLAG_REF)
  814. frame->reference = 1;
  815. ret = avctx->get_buffer(avctx, frame);
  816. if (ret < 0)
  817. return ret;
  818. /* return if the buffers are already set up
  819. * this would happen e.g. when a custom get_buffer() calls
  820. * avcodec_default_get_buffer
  821. */
  822. if (frame->buf[0])
  823. goto end0;
  824. priv = av_mallocz(sizeof(*priv));
  825. if (!priv) {
  826. ret = AVERROR(ENOMEM);
  827. goto fail;
  828. }
  829. priv->avctx = *avctx;
  830. priv->frame = *frame;
  831. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  832. if (!dummy_buf) {
  833. ret = AVERROR(ENOMEM);
  834. goto fail;
  835. }
  836. #define WRAP_PLANE(ref_out, data, data_size) \
  837. do { \
  838. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  839. if (!dummy_ref) { \
  840. ret = AVERROR(ENOMEM); \
  841. goto fail; \
  842. } \
  843. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  844. dummy_ref, 0); \
  845. if (!ref_out) { \
  846. av_buffer_unref(&dummy_ref); \
  847. av_frame_unref(frame); \
  848. ret = AVERROR(ENOMEM); \
  849. goto fail; \
  850. } \
  851. } while (0)
  852. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  853. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  854. planes = av_pix_fmt_count_planes(frame->format);
  855. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  856. check for allocated buffers: make libavcodec happy */
  857. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  858. planes = 1;
  859. if (!desc || planes <= 0) {
  860. ret = AVERROR(EINVAL);
  861. goto fail;
  862. }
  863. for (i = 0; i < planes; i++) {
  864. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  865. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  866. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  867. }
  868. } else {
  869. int planar = av_sample_fmt_is_planar(frame->format);
  870. planes = planar ? avctx->channels : 1;
  871. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  872. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  873. frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
  874. frame->nb_extended_buf);
  875. if (!frame->extended_buf) {
  876. ret = AVERROR(ENOMEM);
  877. goto fail;
  878. }
  879. }
  880. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  881. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  882. for (i = 0; i < frame->nb_extended_buf; i++)
  883. WRAP_PLANE(frame->extended_buf[i],
  884. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  885. frame->linesize[0]);
  886. }
  887. av_buffer_unref(&dummy_buf);
  888. end0:
  889. frame->width = avctx->width;
  890. frame->height = avctx->height;
  891. return 0;
  892. fail:
  893. avctx->release_buffer(avctx, frame);
  894. av_freep(&priv);
  895. av_buffer_unref(&dummy_buf);
  896. return ret;
  897. }
  898. FF_ENABLE_DEPRECATION_WARNINGS
  899. #endif
  900. ret = avctx->get_buffer2(avctx, frame, flags);
  901. end:
  902. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  903. frame->width = avctx->width;
  904. frame->height = avctx->height;
  905. }
  906. return ret;
  907. }
  908. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  909. {
  910. int ret = get_buffer_internal(avctx, frame, flags);
  911. if (ret < 0)
  912. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  913. return ret;
  914. }
  915. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  916. {
  917. AVFrame *tmp;
  918. int ret;
  919. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  920. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  921. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  922. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  923. av_frame_unref(frame);
  924. }
  925. ff_init_buffer_info(avctx, frame);
  926. if (!frame->data[0])
  927. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  928. if (av_frame_is_writable(frame))
  929. return ff_decode_frame_props(avctx, frame);
  930. tmp = av_frame_alloc();
  931. if (!tmp)
  932. return AVERROR(ENOMEM);
  933. av_frame_move_ref(tmp, frame);
  934. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  935. if (ret < 0) {
  936. av_frame_free(&tmp);
  937. return ret;
  938. }
  939. av_frame_copy(frame, tmp);
  940. av_frame_free(&tmp);
  941. return 0;
  942. }
  943. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  944. {
  945. int ret = reget_buffer_internal(avctx, frame);
  946. if (ret < 0)
  947. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  948. return ret;
  949. }
  950. #if FF_API_GET_BUFFER
  951. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  952. {
  953. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  954. av_frame_unref(pic);
  955. }
  956. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  957. {
  958. av_assert0(0);
  959. return AVERROR_BUG;
  960. }
  961. #endif
  962. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  963. {
  964. int i;
  965. for (i = 0; i < count; i++) {
  966. int r = func(c, (char *)arg + i * size);
  967. if (ret)
  968. ret[i] = r;
  969. }
  970. return 0;
  971. }
  972. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  973. {
  974. int i;
  975. for (i = 0; i < count; i++) {
  976. int r = func(c, arg, i, 0);
  977. if (ret)
  978. ret[i] = r;
  979. }
  980. return 0;
  981. }
  982. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  983. unsigned int fourcc)
  984. {
  985. while (tags->pix_fmt >= 0) {
  986. if (tags->fourcc == fourcc)
  987. return tags->pix_fmt;
  988. tags++;
  989. }
  990. return AV_PIX_FMT_NONE;
  991. }
  992. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  993. {
  994. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  995. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  996. }
  997. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  998. {
  999. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  1000. ++fmt;
  1001. return fmt[0];
  1002. }
  1003. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  1004. enum AVPixelFormat pix_fmt)
  1005. {
  1006. AVHWAccel *hwaccel = NULL;
  1007. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1008. if (hwaccel->id == codec_id
  1009. && hwaccel->pix_fmt == pix_fmt)
  1010. return hwaccel;
  1011. return NULL;
  1012. }
  1013. static int setup_hwaccel(AVCodecContext *avctx,
  1014. const enum AVPixelFormat fmt,
  1015. const char *name)
  1016. {
  1017. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  1018. int ret = 0;
  1019. if (!hwa) {
  1020. av_log(avctx, AV_LOG_ERROR,
  1021. "Could not find an AVHWAccel for the pixel format: %s",
  1022. name);
  1023. return AVERROR(ENOENT);
  1024. }
  1025. if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  1026. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1027. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  1028. hwa->name);
  1029. return AVERROR_PATCHWELCOME;
  1030. }
  1031. if (hwa->priv_data_size) {
  1032. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  1033. if (!avctx->internal->hwaccel_priv_data)
  1034. return AVERROR(ENOMEM);
  1035. }
  1036. if (hwa->init) {
  1037. ret = hwa->init(avctx);
  1038. if (ret < 0) {
  1039. av_freep(&avctx->internal->hwaccel_priv_data);
  1040. return ret;
  1041. }
  1042. }
  1043. avctx->hwaccel = hwa;
  1044. return 0;
  1045. }
  1046. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1047. {
  1048. const AVPixFmtDescriptor *desc;
  1049. enum AVPixelFormat *choices;
  1050. enum AVPixelFormat ret;
  1051. unsigned n = 0;
  1052. while (fmt[n] != AV_PIX_FMT_NONE)
  1053. ++n;
  1054. av_assert0(n >= 1);
  1055. avctx->sw_pix_fmt = fmt[n - 1];
  1056. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  1057. choices = av_malloc_array(n + 1, sizeof(*choices));
  1058. if (!choices)
  1059. return AV_PIX_FMT_NONE;
  1060. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  1061. for (;;) {
  1062. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1063. avctx->hwaccel->uninit(avctx);
  1064. av_freep(&avctx->internal->hwaccel_priv_data);
  1065. avctx->hwaccel = NULL;
  1066. ret = avctx->get_format(avctx, choices);
  1067. desc = av_pix_fmt_desc_get(ret);
  1068. if (!desc) {
  1069. ret = AV_PIX_FMT_NONE;
  1070. break;
  1071. }
  1072. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1073. break;
  1074. #if FF_API_CAP_VDPAU
  1075. if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
  1076. break;
  1077. #endif
  1078. if (!setup_hwaccel(avctx, ret, desc->name))
  1079. break;
  1080. /* Remove failed hwaccel from choices */
  1081. for (n = 0; choices[n] != ret; n++)
  1082. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  1083. do
  1084. choices[n] = choices[n + 1];
  1085. while (choices[n++] != AV_PIX_FMT_NONE);
  1086. }
  1087. av_freep(&choices);
  1088. return ret;
  1089. }
  1090. #if FF_API_AVFRAME_LAVC
  1091. void avcodec_get_frame_defaults(AVFrame *frame)
  1092. {
  1093. #if LIBAVCODEC_VERSION_MAJOR >= 55
  1094. // extended_data should explicitly be freed when needed, this code is unsafe currently
  1095. // also this is not compatible to the <55 ABI/API
  1096. if (frame->extended_data != frame->data && 0)
  1097. av_freep(&frame->extended_data);
  1098. #endif
  1099. memset(frame, 0, sizeof(AVFrame));
  1100. av_frame_unref(frame);
  1101. }
  1102. AVFrame *avcodec_alloc_frame(void)
  1103. {
  1104. return av_frame_alloc();
  1105. }
  1106. void avcodec_free_frame(AVFrame **frame)
  1107. {
  1108. av_frame_free(frame);
  1109. }
  1110. #endif
  1111. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  1112. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  1113. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  1114. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  1115. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  1116. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  1117. {
  1118. return codec->properties;
  1119. }
  1120. int av_codec_get_max_lowres(const AVCodec *codec)
  1121. {
  1122. return codec->max_lowres;
  1123. }
  1124. static void get_subtitle_defaults(AVSubtitle *sub)
  1125. {
  1126. memset(sub, 0, sizeof(*sub));
  1127. sub->pts = AV_NOPTS_VALUE;
  1128. }
  1129. static int get_bit_rate(AVCodecContext *ctx)
  1130. {
  1131. int bit_rate;
  1132. int bits_per_sample;
  1133. switch (ctx->codec_type) {
  1134. case AVMEDIA_TYPE_VIDEO:
  1135. case AVMEDIA_TYPE_DATA:
  1136. case AVMEDIA_TYPE_SUBTITLE:
  1137. case AVMEDIA_TYPE_ATTACHMENT:
  1138. bit_rate = ctx->bit_rate;
  1139. break;
  1140. case AVMEDIA_TYPE_AUDIO:
  1141. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1142. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1143. break;
  1144. default:
  1145. bit_rate = 0;
  1146. break;
  1147. }
  1148. return bit_rate;
  1149. }
  1150. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1151. {
  1152. int ret = 0;
  1153. ff_unlock_avcodec();
  1154. ret = avcodec_open2(avctx, codec, options);
  1155. ff_lock_avcodec(avctx, codec);
  1156. return ret;
  1157. }
  1158. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  1159. {
  1160. int ret = 0;
  1161. AVDictionary *tmp = NULL;
  1162. if (avcodec_is_open(avctx))
  1163. return 0;
  1164. if ((!codec && !avctx->codec)) {
  1165. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  1166. return AVERROR(EINVAL);
  1167. }
  1168. if ((codec && avctx->codec && codec != avctx->codec)) {
  1169. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1170. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1171. return AVERROR(EINVAL);
  1172. }
  1173. if (!codec)
  1174. codec = avctx->codec;
  1175. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1176. return AVERROR(EINVAL);
  1177. if (options)
  1178. av_dict_copy(&tmp, *options, 0);
  1179. ret = ff_lock_avcodec(avctx, codec);
  1180. if (ret < 0)
  1181. return ret;
  1182. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1183. if (!avctx->internal) {
  1184. ret = AVERROR(ENOMEM);
  1185. goto end;
  1186. }
  1187. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1188. if (!avctx->internal->pool) {
  1189. ret = AVERROR(ENOMEM);
  1190. goto free_and_end;
  1191. }
  1192. avctx->internal->to_free = av_frame_alloc();
  1193. if (!avctx->internal->to_free) {
  1194. ret = AVERROR(ENOMEM);
  1195. goto free_and_end;
  1196. }
  1197. if (codec->priv_data_size > 0) {
  1198. if (!avctx->priv_data) {
  1199. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1200. if (!avctx->priv_data) {
  1201. ret = AVERROR(ENOMEM);
  1202. goto end;
  1203. }
  1204. if (codec->priv_class) {
  1205. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1206. av_opt_set_defaults(avctx->priv_data);
  1207. }
  1208. }
  1209. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1210. goto free_and_end;
  1211. } else {
  1212. avctx->priv_data = NULL;
  1213. }
  1214. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1215. goto free_and_end;
  1216. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  1217. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist\n", codec->name);
  1218. ret = AVERROR(EINVAL);
  1219. goto free_and_end;
  1220. }
  1221. // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1222. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1223. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1224. if (avctx->coded_width && avctx->coded_height)
  1225. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1226. else if (avctx->width && avctx->height)
  1227. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1228. if (ret < 0)
  1229. goto free_and_end;
  1230. }
  1231. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1232. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1233. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1234. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1235. ff_set_dimensions(avctx, 0, 0);
  1236. }
  1237. if (avctx->width > 0 && avctx->height > 0) {
  1238. if (av_image_check_sar(avctx->width, avctx->height,
  1239. avctx->sample_aspect_ratio) < 0) {
  1240. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1241. avctx->sample_aspect_ratio.num,
  1242. avctx->sample_aspect_ratio.den);
  1243. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  1244. }
  1245. }
  1246. /* if the decoder init function was already called previously,
  1247. * free the already allocated subtitle_header before overwriting it */
  1248. if (av_codec_is_decoder(codec))
  1249. av_freep(&avctx->subtitle_header);
  1250. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1251. ret = AVERROR(EINVAL);
  1252. goto free_and_end;
  1253. }
  1254. avctx->codec = codec;
  1255. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1256. avctx->codec_id == AV_CODEC_ID_NONE) {
  1257. avctx->codec_type = codec->type;
  1258. avctx->codec_id = codec->id;
  1259. }
  1260. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1261. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1262. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1263. ret = AVERROR(EINVAL);
  1264. goto free_and_end;
  1265. }
  1266. avctx->frame_number = 0;
  1267. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1268. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  1269. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1270. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1271. AVCodec *codec2;
  1272. av_log(avctx, AV_LOG_ERROR,
  1273. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1274. "add '-strict %d' if you want to use it.\n",
  1275. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1276. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1277. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  1278. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1279. codec_string, codec2->name);
  1280. ret = AVERROR_EXPERIMENTAL;
  1281. goto free_and_end;
  1282. }
  1283. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1284. (!avctx->time_base.num || !avctx->time_base.den)) {
  1285. avctx->time_base.num = 1;
  1286. avctx->time_base.den = avctx->sample_rate;
  1287. }
  1288. if (!HAVE_THREADS)
  1289. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1290. if (CONFIG_FRAME_THREAD_ENCODER) {
  1291. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1292. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1293. ff_lock_avcodec(avctx, codec);
  1294. if (ret < 0)
  1295. goto free_and_end;
  1296. }
  1297. if (HAVE_THREADS
  1298. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1299. ret = ff_thread_init(avctx);
  1300. if (ret < 0) {
  1301. goto free_and_end;
  1302. }
  1303. }
  1304. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  1305. avctx->thread_count = 1;
  1306. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1307. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1308. avctx->codec->max_lowres);
  1309. ret = AVERROR(EINVAL);
  1310. goto free_and_end;
  1311. }
  1312. #if FF_API_VISMV
  1313. if (avctx->debug_mv)
  1314. av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
  1315. "see the codecview filter instead.\n");
  1316. #endif
  1317. if (av_codec_is_encoder(avctx->codec)) {
  1318. int i;
  1319. #if FF_API_CODED_FRAME
  1320. FF_DISABLE_DEPRECATION_WARNINGS
  1321. avctx->coded_frame = av_frame_alloc();
  1322. if (!avctx->coded_frame) {
  1323. ret = AVERROR(ENOMEM);
  1324. goto free_and_end;
  1325. }
  1326. FF_ENABLE_DEPRECATION_WARNINGS
  1327. #endif
  1328. if (avctx->codec->sample_fmts) {
  1329. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1330. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1331. break;
  1332. if (avctx->channels == 1 &&
  1333. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1334. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1335. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1336. break;
  1337. }
  1338. }
  1339. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1340. char buf[128];
  1341. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1342. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1343. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1344. ret = AVERROR(EINVAL);
  1345. goto free_and_end;
  1346. }
  1347. }
  1348. if (avctx->codec->pix_fmts) {
  1349. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1350. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1351. break;
  1352. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1353. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1354. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1355. char buf[128];
  1356. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1357. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1358. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1359. ret = AVERROR(EINVAL);
  1360. goto free_and_end;
  1361. }
  1362. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1363. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  1364. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1365. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1366. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1367. avctx->color_range = AVCOL_RANGE_JPEG;
  1368. }
  1369. if (avctx->codec->supported_samplerates) {
  1370. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1371. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1372. break;
  1373. if (avctx->codec->supported_samplerates[i] == 0) {
  1374. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1375. avctx->sample_rate);
  1376. ret = AVERROR(EINVAL);
  1377. goto free_and_end;
  1378. }
  1379. }
  1380. if (avctx->sample_rate < 0) {
  1381. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1382. avctx->sample_rate);
  1383. ret = AVERROR(EINVAL);
  1384. goto free_and_end;
  1385. }
  1386. if (avctx->codec->channel_layouts) {
  1387. if (!avctx->channel_layout) {
  1388. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1389. } else {
  1390. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1391. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1392. break;
  1393. if (avctx->codec->channel_layouts[i] == 0) {
  1394. char buf[512];
  1395. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1396. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1397. ret = AVERROR(EINVAL);
  1398. goto free_and_end;
  1399. }
  1400. }
  1401. }
  1402. if (avctx->channel_layout && avctx->channels) {
  1403. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1404. if (channels != avctx->channels) {
  1405. char buf[512];
  1406. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1407. av_log(avctx, AV_LOG_ERROR,
  1408. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1409. buf, channels, avctx->channels);
  1410. ret = AVERROR(EINVAL);
  1411. goto free_and_end;
  1412. }
  1413. } else if (avctx->channel_layout) {
  1414. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1415. }
  1416. if (avctx->channels < 0) {
  1417. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  1418. avctx->channels);
  1419. ret = AVERROR(EINVAL);
  1420. goto free_and_end;
  1421. }
  1422. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1423. if (avctx->width <= 0 || avctx->height <= 0) {
  1424. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1425. ret = AVERROR(EINVAL);
  1426. goto free_and_end;
  1427. }
  1428. }
  1429. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1430. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1431. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1432. }
  1433. if (!avctx->rc_initial_buffer_occupancy)
  1434. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1435. }
  1436. avctx->pts_correction_num_faulty_pts =
  1437. avctx->pts_correction_num_faulty_dts = 0;
  1438. avctx->pts_correction_last_pts =
  1439. avctx->pts_correction_last_dts = INT64_MIN;
  1440. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  1441. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  1442. av_log(avctx, AV_LOG_WARNING,
  1443. "gray decoding requested but not enabled at configuration time\n");
  1444. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1445. || avctx->internal->frame_thread_encoder)) {
  1446. ret = avctx->codec->init(avctx);
  1447. if (ret < 0) {
  1448. goto free_and_end;
  1449. }
  1450. }
  1451. ret=0;
  1452. #if FF_API_AUDIOENC_DELAY
  1453. if (av_codec_is_encoder(avctx->codec))
  1454. avctx->delay = avctx->initial_padding;
  1455. #endif
  1456. if (av_codec_is_decoder(avctx->codec)) {
  1457. if (!avctx->bit_rate)
  1458. avctx->bit_rate = get_bit_rate(avctx);
  1459. /* validate channel layout from the decoder */
  1460. if (avctx->channel_layout) {
  1461. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1462. if (!avctx->channels)
  1463. avctx->channels = channels;
  1464. else if (channels != avctx->channels) {
  1465. char buf[512];
  1466. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1467. av_log(avctx, AV_LOG_WARNING,
  1468. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1469. "ignoring specified channel layout\n",
  1470. buf, channels, avctx->channels);
  1471. avctx->channel_layout = 0;
  1472. }
  1473. }
  1474. if (avctx->channels && avctx->channels < 0 ||
  1475. avctx->channels > FF_SANE_NB_CHANNELS) {
  1476. ret = AVERROR(EINVAL);
  1477. goto free_and_end;
  1478. }
  1479. if (avctx->sub_charenc) {
  1480. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1481. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1482. "supported with subtitles codecs\n");
  1483. ret = AVERROR(EINVAL);
  1484. goto free_and_end;
  1485. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1486. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1487. "subtitles character encoding will be ignored\n",
  1488. avctx->codec_descriptor->name);
  1489. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1490. } else {
  1491. /* input character encoding is set for a text based subtitle
  1492. * codec at this point */
  1493. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1494. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1495. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1496. #if CONFIG_ICONV
  1497. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1498. if (cd == (iconv_t)-1) {
  1499. ret = AVERROR(errno);
  1500. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1501. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1502. goto free_and_end;
  1503. }
  1504. iconv_close(cd);
  1505. #else
  1506. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1507. "conversion needs a libavcodec built with iconv support "
  1508. "for this codec\n");
  1509. ret = AVERROR(ENOSYS);
  1510. goto free_and_end;
  1511. #endif
  1512. }
  1513. }
  1514. }
  1515. #if FF_API_AVCTX_TIMEBASE
  1516. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1517. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  1518. #endif
  1519. }
  1520. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  1521. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  1522. }
  1523. end:
  1524. ff_unlock_avcodec();
  1525. if (options) {
  1526. av_dict_free(options);
  1527. *options = tmp;
  1528. }
  1529. return ret;
  1530. free_and_end:
  1531. if (avctx->codec &&
  1532. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  1533. avctx->codec->close(avctx);
  1534. if (codec->priv_class && codec->priv_data_size)
  1535. av_opt_free(avctx->priv_data);
  1536. av_opt_free(avctx);
  1537. #if FF_API_CODED_FRAME
  1538. FF_DISABLE_DEPRECATION_WARNINGS
  1539. av_frame_free(&avctx->coded_frame);
  1540. FF_ENABLE_DEPRECATION_WARNINGS
  1541. #endif
  1542. av_dict_free(&tmp);
  1543. av_freep(&avctx->priv_data);
  1544. if (avctx->internal) {
  1545. av_frame_free(&avctx->internal->to_free);
  1546. av_freep(&avctx->internal->pool);
  1547. }
  1548. av_freep(&avctx->internal);
  1549. avctx->codec = NULL;
  1550. goto end;
  1551. }
  1552. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
  1553. {
  1554. if (avpkt->size < 0) {
  1555. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1556. return AVERROR(EINVAL);
  1557. }
  1558. if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  1559. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1560. size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  1561. return AVERROR(EINVAL);
  1562. }
  1563. if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
  1564. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1565. if (!avpkt->data || avpkt->size < size) {
  1566. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1567. avpkt->data = avctx->internal->byte_buffer;
  1568. avpkt->size = avctx->internal->byte_buffer_size;
  1569. }
  1570. }
  1571. if (avpkt->data) {
  1572. AVBufferRef *buf = avpkt->buf;
  1573. if (avpkt->size < size) {
  1574. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1575. return AVERROR(EINVAL);
  1576. }
  1577. av_init_packet(avpkt);
  1578. avpkt->buf = buf;
  1579. avpkt->size = size;
  1580. return 0;
  1581. } else {
  1582. int ret = av_new_packet(avpkt, size);
  1583. if (ret < 0)
  1584. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1585. return ret;
  1586. }
  1587. }
  1588. int ff_alloc_packet(AVPacket *avpkt, int size)
  1589. {
  1590. return ff_alloc_packet2(NULL, avpkt, size, 0);
  1591. }
  1592. /**
  1593. * Pad last frame with silence.
  1594. */
  1595. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1596. {
  1597. AVFrame *frame = NULL;
  1598. int ret;
  1599. if (!(frame = av_frame_alloc()))
  1600. return AVERROR(ENOMEM);
  1601. frame->format = src->format;
  1602. frame->channel_layout = src->channel_layout;
  1603. av_frame_set_channels(frame, av_frame_get_channels(src));
  1604. frame->nb_samples = s->frame_size;
  1605. ret = av_frame_get_buffer(frame, 32);
  1606. if (ret < 0)
  1607. goto fail;
  1608. ret = av_frame_copy_props(frame, src);
  1609. if (ret < 0)
  1610. goto fail;
  1611. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1612. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1613. goto fail;
  1614. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1615. frame->nb_samples - src->nb_samples,
  1616. s->channels, s->sample_fmt)) < 0)
  1617. goto fail;
  1618. *dst = frame;
  1619. return 0;
  1620. fail:
  1621. av_frame_free(&frame);
  1622. return ret;
  1623. }
  1624. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1625. AVPacket *avpkt,
  1626. const AVFrame *frame,
  1627. int *got_packet_ptr)
  1628. {
  1629. AVFrame *extended_frame = NULL;
  1630. AVFrame *padded_frame = NULL;
  1631. int ret;
  1632. AVPacket user_pkt = *avpkt;
  1633. int needs_realloc = !user_pkt.data;
  1634. *got_packet_ptr = 0;
  1635. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1636. av_free_packet(avpkt);
  1637. av_init_packet(avpkt);
  1638. return 0;
  1639. }
  1640. /* ensure that extended_data is properly set */
  1641. if (frame && !frame->extended_data) {
  1642. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1643. avctx->channels > AV_NUM_DATA_POINTERS) {
  1644. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1645. "with more than %d channels, but extended_data is not set.\n",
  1646. AV_NUM_DATA_POINTERS);
  1647. return AVERROR(EINVAL);
  1648. }
  1649. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1650. extended_frame = av_frame_alloc();
  1651. if (!extended_frame)
  1652. return AVERROR(ENOMEM);
  1653. memcpy(extended_frame, frame, sizeof(AVFrame));
  1654. extended_frame->extended_data = extended_frame->data;
  1655. frame = extended_frame;
  1656. }
  1657. /* extract audio service type metadata */
  1658. if (frame) {
  1659. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1660. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1661. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1662. }
  1663. /* check for valid frame size */
  1664. if (frame) {
  1665. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1666. if (frame->nb_samples > avctx->frame_size) {
  1667. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1668. ret = AVERROR(EINVAL);
  1669. goto end;
  1670. }
  1671. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1672. if (frame->nb_samples < avctx->frame_size &&
  1673. !avctx->internal->last_audio_frame) {
  1674. ret = pad_last_frame(avctx, &padded_frame, frame);
  1675. if (ret < 0)
  1676. goto end;
  1677. frame = padded_frame;
  1678. avctx->internal->last_audio_frame = 1;
  1679. }
  1680. if (frame->nb_samples != avctx->frame_size) {
  1681. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1682. ret = AVERROR(EINVAL);
  1683. goto end;
  1684. }
  1685. }
  1686. }
  1687. av_assert0(avctx->codec->encode2);
  1688. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1689. if (!ret) {
  1690. if (*got_packet_ptr) {
  1691. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1692. if (avpkt->pts == AV_NOPTS_VALUE)
  1693. avpkt->pts = frame->pts;
  1694. if (!avpkt->duration)
  1695. avpkt->duration = ff_samples_to_time_base(avctx,
  1696. frame->nb_samples);
  1697. }
  1698. avpkt->dts = avpkt->pts;
  1699. } else {
  1700. avpkt->size = 0;
  1701. }
  1702. }
  1703. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1704. needs_realloc = 0;
  1705. if (user_pkt.data) {
  1706. if (user_pkt.size >= avpkt->size) {
  1707. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1708. } else {
  1709. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1710. avpkt->size = user_pkt.size;
  1711. ret = -1;
  1712. }
  1713. avpkt->buf = user_pkt.buf;
  1714. avpkt->data = user_pkt.data;
  1715. } else {
  1716. if (av_dup_packet(avpkt) < 0) {
  1717. ret = AVERROR(ENOMEM);
  1718. }
  1719. }
  1720. }
  1721. if (!ret) {
  1722. if (needs_realloc && avpkt->data) {
  1723. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1724. if (ret >= 0)
  1725. avpkt->data = avpkt->buf->data;
  1726. }
  1727. avctx->frame_number++;
  1728. }
  1729. if (ret < 0 || !*got_packet_ptr) {
  1730. av_free_packet(avpkt);
  1731. av_init_packet(avpkt);
  1732. goto end;
  1733. }
  1734. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1735. * this needs to be moved to the encoders, but for now we can do it
  1736. * here to simplify things */
  1737. avpkt->flags |= AV_PKT_FLAG_KEY;
  1738. end:
  1739. av_frame_free(&padded_frame);
  1740. av_free(extended_frame);
  1741. #if FF_API_AUDIOENC_DELAY
  1742. avctx->delay = avctx->initial_padding;
  1743. #endif
  1744. return ret;
  1745. }
  1746. #if FF_API_OLD_ENCODE_AUDIO
  1747. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1748. uint8_t *buf, int buf_size,
  1749. const short *samples)
  1750. {
  1751. AVPacket pkt;
  1752. AVFrame *frame;
  1753. int ret, samples_size, got_packet;
  1754. av_init_packet(&pkt);
  1755. pkt.data = buf;
  1756. pkt.size = buf_size;
  1757. if (samples) {
  1758. frame = av_frame_alloc();
  1759. if (!frame)
  1760. return AVERROR(ENOMEM);
  1761. if (avctx->frame_size) {
  1762. frame->nb_samples = avctx->frame_size;
  1763. } else {
  1764. /* if frame_size is not set, the number of samples must be
  1765. * calculated from the buffer size */
  1766. int64_t nb_samples;
  1767. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1768. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1769. "support this codec\n");
  1770. av_frame_free(&frame);
  1771. return AVERROR(EINVAL);
  1772. }
  1773. nb_samples = (int64_t)buf_size * 8 /
  1774. (av_get_bits_per_sample(avctx->codec_id) *
  1775. avctx->channels);
  1776. if (nb_samples >= INT_MAX) {
  1777. av_frame_free(&frame);
  1778. return AVERROR(EINVAL);
  1779. }
  1780. frame->nb_samples = nb_samples;
  1781. }
  1782. /* it is assumed that the samples buffer is large enough based on the
  1783. * relevant parameters */
  1784. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1785. frame->nb_samples,
  1786. avctx->sample_fmt, 1);
  1787. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1788. avctx->sample_fmt,
  1789. (const uint8_t *)samples,
  1790. samples_size, 1)) < 0) {
  1791. av_frame_free(&frame);
  1792. return ret;
  1793. }
  1794. /* fabricate frame pts from sample count.
  1795. * this is needed because the avcodec_encode_audio() API does not have
  1796. * a way for the user to provide pts */
  1797. if (avctx->sample_rate && avctx->time_base.num)
  1798. frame->pts = ff_samples_to_time_base(avctx,
  1799. avctx->internal->sample_count);
  1800. else
  1801. frame->pts = AV_NOPTS_VALUE;
  1802. avctx->internal->sample_count += frame->nb_samples;
  1803. } else {
  1804. frame = NULL;
  1805. }
  1806. got_packet = 0;
  1807. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1808. #if FF_API_CODED_FRAME
  1809. FF_DISABLE_DEPRECATION_WARNINGS
  1810. if (!ret && got_packet && avctx->coded_frame) {
  1811. avctx->coded_frame->pts = pkt.pts;
  1812. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1813. }
  1814. FF_ENABLE_DEPRECATION_WARNINGS
  1815. #endif
  1816. /* free any side data since we cannot return it */
  1817. av_packet_free_side_data(&pkt);
  1818. if (frame && frame->extended_data != frame->data)
  1819. av_freep(&frame->extended_data);
  1820. av_frame_free(&frame);
  1821. return ret ? ret : pkt.size;
  1822. }
  1823. #endif
  1824. #if FF_API_OLD_ENCODE_VIDEO
  1825. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1826. const AVFrame *pict)
  1827. {
  1828. AVPacket pkt;
  1829. int ret, got_packet = 0;
  1830. if (buf_size < AV_INPUT_BUFFER_MIN_SIZE) {
  1831. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1832. return -1;
  1833. }
  1834. av_init_packet(&pkt);
  1835. pkt.data = buf;
  1836. pkt.size = buf_size;
  1837. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1838. #if FF_API_CODED_FRAME
  1839. FF_DISABLE_DEPRECATION_WARNINGS
  1840. if (!ret && got_packet && avctx->coded_frame) {
  1841. avctx->coded_frame->pts = pkt.pts;
  1842. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1843. if (avctx->codec->capabilities & AV_CODEC_CAP_INTRA_ONLY)
  1844. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  1845. }
  1846. FF_ENABLE_DEPRECATION_WARNINGS
  1847. #endif
  1848. /* free any side data since we cannot return it */
  1849. if (pkt.side_data_elems > 0) {
  1850. int i;
  1851. for (i = 0; i < pkt.side_data_elems; i++)
  1852. av_free(pkt.side_data[i].data);
  1853. av_freep(&pkt.side_data);
  1854. pkt.side_data_elems = 0;
  1855. }
  1856. return ret ? ret : pkt.size;
  1857. }
  1858. #endif
  1859. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1860. AVPacket *avpkt,
  1861. const AVFrame *frame,
  1862. int *got_packet_ptr)
  1863. {
  1864. int ret;
  1865. AVPacket user_pkt = *avpkt;
  1866. int needs_realloc = !user_pkt.data;
  1867. *got_packet_ptr = 0;
  1868. if(CONFIG_FRAME_THREAD_ENCODER &&
  1869. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1870. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1871. if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
  1872. avctx->stats_out[0] = '\0';
  1873. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1874. av_free_packet(avpkt);
  1875. av_init_packet(avpkt);
  1876. avpkt->size = 0;
  1877. return 0;
  1878. }
  1879. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1880. return AVERROR(EINVAL);
  1881. if (frame && frame->format == AV_PIX_FMT_NONE)
  1882. av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
  1883. if (frame && (frame->width == 0 || frame->height == 0))
  1884. av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
  1885. av_assert0(avctx->codec->encode2);
  1886. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1887. av_assert0(ret <= 0);
  1888. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1889. needs_realloc = 0;
  1890. if (user_pkt.data) {
  1891. if (user_pkt.size >= avpkt->size) {
  1892. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1893. } else {
  1894. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1895. avpkt->size = user_pkt.size;
  1896. ret = -1;
  1897. }
  1898. avpkt->buf = user_pkt.buf;
  1899. avpkt->data = user_pkt.data;
  1900. } else {
  1901. if (av_dup_packet(avpkt) < 0) {
  1902. ret = AVERROR(ENOMEM);
  1903. }
  1904. }
  1905. }
  1906. if (!ret) {
  1907. if (!*got_packet_ptr)
  1908. avpkt->size = 0;
  1909. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1910. avpkt->pts = avpkt->dts = frame->pts;
  1911. if (needs_realloc && avpkt->data) {
  1912. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  1913. if (ret >= 0)
  1914. avpkt->data = avpkt->buf->data;
  1915. }
  1916. avctx->frame_number++;
  1917. }
  1918. if (ret < 0 || !*got_packet_ptr)
  1919. av_free_packet(avpkt);
  1920. emms_c();
  1921. return ret;
  1922. }
  1923. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1924. const AVSubtitle *sub)
  1925. {
  1926. int ret;
  1927. if (sub->start_display_time) {
  1928. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1929. return -1;
  1930. }
  1931. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1932. avctx->frame_number++;
  1933. return ret;
  1934. }
  1935. /**
  1936. * Attempt to guess proper monotonic timestamps for decoded video frames
  1937. * which might have incorrect times. Input timestamps may wrap around, in
  1938. * which case the output will as well.
  1939. *
  1940. * @param pts the pts field of the decoded AVPacket, as passed through
  1941. * AVFrame.pkt_pts
  1942. * @param dts the dts field of the decoded AVPacket
  1943. * @return one of the input values, may be AV_NOPTS_VALUE
  1944. */
  1945. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1946. int64_t reordered_pts, int64_t dts)
  1947. {
  1948. int64_t pts = AV_NOPTS_VALUE;
  1949. if (dts != AV_NOPTS_VALUE) {
  1950. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1951. ctx->pts_correction_last_dts = dts;
  1952. } else if (reordered_pts != AV_NOPTS_VALUE)
  1953. ctx->pts_correction_last_dts = reordered_pts;
  1954. if (reordered_pts != AV_NOPTS_VALUE) {
  1955. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1956. ctx->pts_correction_last_pts = reordered_pts;
  1957. } else if(dts != AV_NOPTS_VALUE)
  1958. ctx->pts_correction_last_pts = dts;
  1959. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1960. && reordered_pts != AV_NOPTS_VALUE)
  1961. pts = reordered_pts;
  1962. else
  1963. pts = dts;
  1964. return pts;
  1965. }
  1966. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1967. {
  1968. int size = 0, ret;
  1969. const uint8_t *data;
  1970. uint32_t flags;
  1971. int64_t val;
  1972. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1973. if (!data)
  1974. return 0;
  1975. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1976. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1977. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1978. return AVERROR(EINVAL);
  1979. }
  1980. if (size < 4)
  1981. goto fail;
  1982. flags = bytestream_get_le32(&data);
  1983. size -= 4;
  1984. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1985. if (size < 4)
  1986. goto fail;
  1987. val = bytestream_get_le32(&data);
  1988. if (val <= 0 || val > INT_MAX) {
  1989. av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
  1990. return AVERROR_INVALIDDATA;
  1991. }
  1992. avctx->channels = val;
  1993. size -= 4;
  1994. }
  1995. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1996. if (size < 8)
  1997. goto fail;
  1998. avctx->channel_layout = bytestream_get_le64(&data);
  1999. size -= 8;
  2000. }
  2001. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  2002. if (size < 4)
  2003. goto fail;
  2004. val = bytestream_get_le32(&data);
  2005. if (val <= 0 || val > INT_MAX) {
  2006. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
  2007. return AVERROR_INVALIDDATA;
  2008. }
  2009. avctx->sample_rate = val;
  2010. size -= 4;
  2011. }
  2012. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  2013. if (size < 8)
  2014. goto fail;
  2015. avctx->width = bytestream_get_le32(&data);
  2016. avctx->height = bytestream_get_le32(&data);
  2017. size -= 8;
  2018. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  2019. if (ret < 0)
  2020. return ret;
  2021. }
  2022. return 0;
  2023. fail:
  2024. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  2025. return AVERROR_INVALIDDATA;
  2026. }
  2027. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  2028. {
  2029. int ret;
  2030. /* move the original frame to our backup */
  2031. av_frame_unref(avci->to_free);
  2032. av_frame_move_ref(avci->to_free, frame);
  2033. /* now copy everything except the AVBufferRefs back
  2034. * note that we make a COPY of the side data, so calling av_frame_free() on
  2035. * the caller's frame will work properly */
  2036. ret = av_frame_copy_props(frame, avci->to_free);
  2037. if (ret < 0)
  2038. return ret;
  2039. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  2040. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  2041. if (avci->to_free->extended_data != avci->to_free->data) {
  2042. int planes = av_frame_get_channels(avci->to_free);
  2043. int size = planes * sizeof(*frame->extended_data);
  2044. if (!size) {
  2045. av_frame_unref(frame);
  2046. return AVERROR_BUG;
  2047. }
  2048. frame->extended_data = av_malloc(size);
  2049. if (!frame->extended_data) {
  2050. av_frame_unref(frame);
  2051. return AVERROR(ENOMEM);
  2052. }
  2053. memcpy(frame->extended_data, avci->to_free->extended_data,
  2054. size);
  2055. } else
  2056. frame->extended_data = frame->data;
  2057. frame->format = avci->to_free->format;
  2058. frame->width = avci->to_free->width;
  2059. frame->height = avci->to_free->height;
  2060. frame->channel_layout = avci->to_free->channel_layout;
  2061. frame->nb_samples = avci->to_free->nb_samples;
  2062. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  2063. return 0;
  2064. }
  2065. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  2066. int *got_picture_ptr,
  2067. const AVPacket *avpkt)
  2068. {
  2069. AVCodecInternal *avci = avctx->internal;
  2070. int ret;
  2071. // copy to ensure we do not change avpkt
  2072. AVPacket tmp = *avpkt;
  2073. if (!avctx->codec)
  2074. return AVERROR(EINVAL);
  2075. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  2076. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  2077. return AVERROR(EINVAL);
  2078. }
  2079. *got_picture_ptr = 0;
  2080. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  2081. return AVERROR(EINVAL);
  2082. av_frame_unref(picture);
  2083. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  2084. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2085. int did_split = av_packet_split_side_data(&tmp);
  2086. ret = apply_param_change(avctx, &tmp);
  2087. if (ret < 0) {
  2088. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2089. if (avctx->err_recognition & AV_EF_EXPLODE)
  2090. goto fail;
  2091. }
  2092. avctx->internal->pkt = &tmp;
  2093. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2094. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  2095. &tmp);
  2096. else {
  2097. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  2098. &tmp);
  2099. picture->pkt_dts = avpkt->dts;
  2100. if(!avctx->has_b_frames){
  2101. av_frame_set_pkt_pos(picture, avpkt->pos);
  2102. }
  2103. //FIXME these should be under if(!avctx->has_b_frames)
  2104. /* get_buffer is supposed to set frame parameters */
  2105. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  2106. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  2107. if (!picture->width) picture->width = avctx->width;
  2108. if (!picture->height) picture->height = avctx->height;
  2109. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  2110. }
  2111. }
  2112. fail:
  2113. emms_c(); //needed to avoid an emms_c() call before every return;
  2114. avctx->internal->pkt = NULL;
  2115. if (did_split) {
  2116. av_packet_free_side_data(&tmp);
  2117. if(ret == tmp.size)
  2118. ret = avpkt->size;
  2119. }
  2120. if (*got_picture_ptr) {
  2121. if (!avctx->refcounted_frames) {
  2122. int err = unrefcount_frame(avci, picture);
  2123. if (err < 0)
  2124. return err;
  2125. }
  2126. avctx->frame_number++;
  2127. av_frame_set_best_effort_timestamp(picture,
  2128. guess_correct_pts(avctx,
  2129. picture->pkt_pts,
  2130. picture->pkt_dts));
  2131. } else
  2132. av_frame_unref(picture);
  2133. } else
  2134. ret = 0;
  2135. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2136. * make sure it's set correctly */
  2137. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  2138. #if FF_API_AVCTX_TIMEBASE
  2139. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  2140. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  2141. #endif
  2142. return ret;
  2143. }
  2144. #if FF_API_OLD_DECODE_AUDIO
  2145. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  2146. int *frame_size_ptr,
  2147. AVPacket *avpkt)
  2148. {
  2149. AVFrame *frame = av_frame_alloc();
  2150. int ret, got_frame = 0;
  2151. if (!frame)
  2152. return AVERROR(ENOMEM);
  2153. #if FF_API_GET_BUFFER
  2154. FF_DISABLE_DEPRECATION_WARNINGS
  2155. if (avctx->get_buffer != avcodec_default_get_buffer) {
  2156. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  2157. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  2158. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  2159. "avcodec_decode_audio4()\n");
  2160. avctx->get_buffer = avcodec_default_get_buffer;
  2161. avctx->release_buffer = avcodec_default_release_buffer;
  2162. }
  2163. FF_ENABLE_DEPRECATION_WARNINGS
  2164. #endif
  2165. ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
  2166. if (ret >= 0 && got_frame) {
  2167. int ch, plane_size;
  2168. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  2169. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  2170. frame->nb_samples,
  2171. avctx->sample_fmt, 1);
  2172. if (*frame_size_ptr < data_size) {
  2173. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  2174. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  2175. av_frame_free(&frame);
  2176. return AVERROR(EINVAL);
  2177. }
  2178. memcpy(samples, frame->extended_data[0], plane_size);
  2179. if (planar && avctx->channels > 1) {
  2180. uint8_t *out = ((uint8_t *)samples) + plane_size;
  2181. for (ch = 1; ch < avctx->channels; ch++) {
  2182. memcpy(out, frame->extended_data[ch], plane_size);
  2183. out += plane_size;
  2184. }
  2185. }
  2186. *frame_size_ptr = data_size;
  2187. } else {
  2188. *frame_size_ptr = 0;
  2189. }
  2190. av_frame_free(&frame);
  2191. return ret;
  2192. }
  2193. #endif
  2194. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  2195. AVFrame *frame,
  2196. int *got_frame_ptr,
  2197. const AVPacket *avpkt)
  2198. {
  2199. AVCodecInternal *avci = avctx->internal;
  2200. int ret = 0;
  2201. *got_frame_ptr = 0;
  2202. if (!avpkt->data && avpkt->size) {
  2203. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2204. return AVERROR(EINVAL);
  2205. }
  2206. if (!avctx->codec)
  2207. return AVERROR(EINVAL);
  2208. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  2209. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  2210. return AVERROR(EINVAL);
  2211. }
  2212. av_frame_unref(frame);
  2213. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2214. uint8_t *side;
  2215. int side_size;
  2216. uint32_t discard_padding = 0;
  2217. uint8_t skip_reason = 0;
  2218. uint8_t discard_reason = 0;
  2219. // copy to ensure we do not change avpkt
  2220. AVPacket tmp = *avpkt;
  2221. int did_split = av_packet_split_side_data(&tmp);
  2222. ret = apply_param_change(avctx, &tmp);
  2223. if (ret < 0) {
  2224. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2225. if (avctx->err_recognition & AV_EF_EXPLODE)
  2226. goto fail;
  2227. }
  2228. avctx->internal->pkt = &tmp;
  2229. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2230. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  2231. else {
  2232. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  2233. av_assert0(ret <= tmp.size);
  2234. frame->pkt_dts = avpkt->dts;
  2235. }
  2236. if (ret >= 0 && *got_frame_ptr) {
  2237. avctx->frame_number++;
  2238. av_frame_set_best_effort_timestamp(frame,
  2239. guess_correct_pts(avctx,
  2240. frame->pkt_pts,
  2241. frame->pkt_dts));
  2242. if (frame->format == AV_SAMPLE_FMT_NONE)
  2243. frame->format = avctx->sample_fmt;
  2244. if (!frame->channel_layout)
  2245. frame->channel_layout = avctx->channel_layout;
  2246. if (!av_frame_get_channels(frame))
  2247. av_frame_set_channels(frame, avctx->channels);
  2248. if (!frame->sample_rate)
  2249. frame->sample_rate = avctx->sample_rate;
  2250. }
  2251. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2252. if(side && side_size>=10) {
  2253. avctx->internal->skip_samples = AV_RL32(side);
  2254. discard_padding = AV_RL32(side + 4);
  2255. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  2256. avctx->internal->skip_samples, (int)discard_padding);
  2257. skip_reason = AV_RL8(side + 8);
  2258. discard_reason = AV_RL8(side + 9);
  2259. }
  2260. if (avctx->internal->skip_samples && *got_frame_ptr &&
  2261. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  2262. if(frame->nb_samples <= avctx->internal->skip_samples){
  2263. *got_frame_ptr = 0;
  2264. avctx->internal->skip_samples -= frame->nb_samples;
  2265. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  2266. avctx->internal->skip_samples);
  2267. } else {
  2268. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  2269. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  2270. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2271. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  2272. (AVRational){1, avctx->sample_rate},
  2273. avctx->pkt_timebase);
  2274. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  2275. frame->pkt_pts += diff_ts;
  2276. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  2277. frame->pkt_dts += diff_ts;
  2278. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2279. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2280. } else {
  2281. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2282. }
  2283. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2284. avctx->internal->skip_samples, frame->nb_samples);
  2285. frame->nb_samples -= avctx->internal->skip_samples;
  2286. avctx->internal->skip_samples = 0;
  2287. }
  2288. }
  2289. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
  2290. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  2291. if (discard_padding == frame->nb_samples) {
  2292. *got_frame_ptr = 0;
  2293. } else {
  2294. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2295. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2296. (AVRational){1, avctx->sample_rate},
  2297. avctx->pkt_timebase);
  2298. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2299. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2300. } else {
  2301. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2302. }
  2303. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2304. (int)discard_padding, frame->nb_samples);
  2305. frame->nb_samples -= discard_padding;
  2306. }
  2307. }
  2308. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
  2309. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  2310. if (fside) {
  2311. AV_WL32(fside->data, avctx->internal->skip_samples);
  2312. AV_WL32(fside->data + 4, discard_padding);
  2313. AV_WL8(fside->data + 8, skip_reason);
  2314. AV_WL8(fside->data + 9, discard_reason);
  2315. avctx->internal->skip_samples = 0;
  2316. }
  2317. }
  2318. fail:
  2319. avctx->internal->pkt = NULL;
  2320. if (did_split) {
  2321. av_packet_free_side_data(&tmp);
  2322. if(ret == tmp.size)
  2323. ret = avpkt->size;
  2324. }
  2325. if (ret >= 0 && *got_frame_ptr) {
  2326. if (!avctx->refcounted_frames) {
  2327. int err = unrefcount_frame(avci, frame);
  2328. if (err < 0)
  2329. return err;
  2330. }
  2331. } else
  2332. av_frame_unref(frame);
  2333. }
  2334. return ret;
  2335. }
  2336. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2337. static int recode_subtitle(AVCodecContext *avctx,
  2338. AVPacket *outpkt, const AVPacket *inpkt)
  2339. {
  2340. #if CONFIG_ICONV
  2341. iconv_t cd = (iconv_t)-1;
  2342. int ret = 0;
  2343. char *inb, *outb;
  2344. size_t inl, outl;
  2345. AVPacket tmp;
  2346. #endif
  2347. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2348. return 0;
  2349. #if CONFIG_ICONV
  2350. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2351. av_assert0(cd != (iconv_t)-1);
  2352. inb = inpkt->data;
  2353. inl = inpkt->size;
  2354. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  2355. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2356. ret = AVERROR(ENOMEM);
  2357. goto end;
  2358. }
  2359. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2360. if (ret < 0)
  2361. goto end;
  2362. outpkt->buf = tmp.buf;
  2363. outpkt->data = tmp.data;
  2364. outpkt->size = tmp.size;
  2365. outb = outpkt->data;
  2366. outl = outpkt->size;
  2367. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2368. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2369. outl >= outpkt->size || inl != 0) {
  2370. ret = FFMIN(AVERROR(errno), -1);
  2371. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2372. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2373. av_free_packet(&tmp);
  2374. goto end;
  2375. }
  2376. outpkt->size -= outl;
  2377. memset(outpkt->data + outpkt->size, 0, outl);
  2378. end:
  2379. if (cd != (iconv_t)-1)
  2380. iconv_close(cd);
  2381. return ret;
  2382. #else
  2383. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2384. return AVERROR(EINVAL);
  2385. #endif
  2386. }
  2387. static int utf8_check(const uint8_t *str)
  2388. {
  2389. const uint8_t *byte;
  2390. uint32_t codepoint, min;
  2391. while (*str) {
  2392. byte = str;
  2393. GET_UTF8(codepoint, *(byte++), return 0;);
  2394. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2395. 1 << (5 * (byte - str) - 4);
  2396. if (codepoint < min || codepoint >= 0x110000 ||
  2397. codepoint == 0xFFFE /* BOM */ ||
  2398. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2399. return 0;
  2400. str = byte;
  2401. }
  2402. return 1;
  2403. }
  2404. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2405. int *got_sub_ptr,
  2406. AVPacket *avpkt)
  2407. {
  2408. int i, ret = 0;
  2409. if (!avpkt->data && avpkt->size) {
  2410. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2411. return AVERROR(EINVAL);
  2412. }
  2413. if (!avctx->codec)
  2414. return AVERROR(EINVAL);
  2415. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2416. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2417. return AVERROR(EINVAL);
  2418. }
  2419. *got_sub_ptr = 0;
  2420. get_subtitle_defaults(sub);
  2421. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  2422. AVPacket pkt_recoded;
  2423. AVPacket tmp = *avpkt;
  2424. int did_split = av_packet_split_side_data(&tmp);
  2425. //apply_param_change(avctx, &tmp);
  2426. if (did_split) {
  2427. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2428. * proper padding.
  2429. * If the side data is smaller than the buffer padding size, the
  2430. * remaining bytes should have already been filled with zeros by the
  2431. * original packet allocation anyway. */
  2432. memset(tmp.data + tmp.size, 0,
  2433. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  2434. }
  2435. pkt_recoded = tmp;
  2436. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2437. if (ret < 0) {
  2438. *got_sub_ptr = 0;
  2439. } else {
  2440. avctx->internal->pkt = &pkt_recoded;
  2441. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2442. sub->pts = av_rescale_q(avpkt->pts,
  2443. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2444. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2445. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2446. !!*got_sub_ptr >= !!sub->num_rects);
  2447. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2448. avctx->pkt_timebase.num) {
  2449. AVRational ms = { 1, 1000 };
  2450. sub->end_display_time = av_rescale_q(avpkt->duration,
  2451. avctx->pkt_timebase, ms);
  2452. }
  2453. for (i = 0; i < sub->num_rects; i++) {
  2454. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2455. av_log(avctx, AV_LOG_ERROR,
  2456. "Invalid UTF-8 in decoded subtitles text; "
  2457. "maybe missing -sub_charenc option\n");
  2458. avsubtitle_free(sub);
  2459. return AVERROR_INVALIDDATA;
  2460. }
  2461. }
  2462. if (tmp.data != pkt_recoded.data) { // did we recode?
  2463. /* prevent from destroying side data from original packet */
  2464. pkt_recoded.side_data = NULL;
  2465. pkt_recoded.side_data_elems = 0;
  2466. av_free_packet(&pkt_recoded);
  2467. }
  2468. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2469. sub->format = 0;
  2470. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2471. sub->format = 1;
  2472. avctx->internal->pkt = NULL;
  2473. }
  2474. if (did_split) {
  2475. av_packet_free_side_data(&tmp);
  2476. if(ret == tmp.size)
  2477. ret = avpkt->size;
  2478. }
  2479. if (*got_sub_ptr)
  2480. avctx->frame_number++;
  2481. }
  2482. return ret;
  2483. }
  2484. void avsubtitle_free(AVSubtitle *sub)
  2485. {
  2486. int i;
  2487. for (i = 0; i < sub->num_rects; i++) {
  2488. av_freep(&sub->rects[i]->pict.data[0]);
  2489. av_freep(&sub->rects[i]->pict.data[1]);
  2490. av_freep(&sub->rects[i]->pict.data[2]);
  2491. av_freep(&sub->rects[i]->pict.data[3]);
  2492. av_freep(&sub->rects[i]->text);
  2493. av_freep(&sub->rects[i]->ass);
  2494. av_freep(&sub->rects[i]);
  2495. }
  2496. av_freep(&sub->rects);
  2497. memset(sub, 0, sizeof(AVSubtitle));
  2498. }
  2499. av_cold int avcodec_close(AVCodecContext *avctx)
  2500. {
  2501. if (!avctx)
  2502. return 0;
  2503. if (avcodec_is_open(avctx)) {
  2504. FramePool *pool = avctx->internal->pool;
  2505. int i;
  2506. if (CONFIG_FRAME_THREAD_ENCODER &&
  2507. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2508. ff_frame_thread_encoder_free(avctx);
  2509. }
  2510. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2511. ff_thread_free(avctx);
  2512. if (avctx->codec && avctx->codec->close)
  2513. avctx->codec->close(avctx);
  2514. avctx->internal->byte_buffer_size = 0;
  2515. av_freep(&avctx->internal->byte_buffer);
  2516. av_frame_free(&avctx->internal->to_free);
  2517. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2518. av_buffer_pool_uninit(&pool->pools[i]);
  2519. av_freep(&avctx->internal->pool);
  2520. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2521. avctx->hwaccel->uninit(avctx);
  2522. av_freep(&avctx->internal->hwaccel_priv_data);
  2523. av_freep(&avctx->internal);
  2524. }
  2525. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2526. av_opt_free(avctx->priv_data);
  2527. av_opt_free(avctx);
  2528. av_freep(&avctx->priv_data);
  2529. if (av_codec_is_encoder(avctx->codec)) {
  2530. av_freep(&avctx->extradata);
  2531. #if FF_API_CODED_FRAME
  2532. FF_DISABLE_DEPRECATION_WARNINGS
  2533. av_frame_free(&avctx->coded_frame);
  2534. FF_ENABLE_DEPRECATION_WARNINGS
  2535. #endif
  2536. }
  2537. avctx->codec = NULL;
  2538. avctx->active_thread_type = 0;
  2539. return 0;
  2540. }
  2541. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2542. {
  2543. switch(id){
  2544. //This is for future deprecatec codec ids, its empty since
  2545. //last major bump but will fill up again over time, please don't remove it
  2546. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2547. case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
  2548. case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
  2549. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2550. case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
  2551. case AV_CODEC_ID_PCM_S16BE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S16BE_PLANAR;
  2552. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2553. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2554. case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
  2555. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2556. case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
  2557. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2558. case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
  2559. case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
  2560. case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
  2561. case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
  2562. case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
  2563. case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
  2564. case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
  2565. case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
  2566. default : return id;
  2567. }
  2568. }
  2569. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2570. {
  2571. AVCodec *p, *experimental = NULL;
  2572. p = first_avcodec;
  2573. id= remap_deprecated_codec_id(id);
  2574. while (p) {
  2575. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2576. p->id == id) {
  2577. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  2578. experimental = p;
  2579. } else
  2580. return p;
  2581. }
  2582. p = p->next;
  2583. }
  2584. return experimental;
  2585. }
  2586. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2587. {
  2588. return find_encdec(id, 1);
  2589. }
  2590. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2591. {
  2592. AVCodec *p;
  2593. if (!name)
  2594. return NULL;
  2595. p = first_avcodec;
  2596. while (p) {
  2597. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2598. return p;
  2599. p = p->next;
  2600. }
  2601. return NULL;
  2602. }
  2603. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2604. {
  2605. return find_encdec(id, 0);
  2606. }
  2607. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2608. {
  2609. AVCodec *p;
  2610. if (!name)
  2611. return NULL;
  2612. p = first_avcodec;
  2613. while (p) {
  2614. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2615. return p;
  2616. p = p->next;
  2617. }
  2618. return NULL;
  2619. }
  2620. const char *avcodec_get_name(enum AVCodecID id)
  2621. {
  2622. const AVCodecDescriptor *cd;
  2623. AVCodec *codec;
  2624. if (id == AV_CODEC_ID_NONE)
  2625. return "none";
  2626. cd = avcodec_descriptor_get(id);
  2627. if (cd)
  2628. return cd->name;
  2629. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2630. codec = avcodec_find_decoder(id);
  2631. if (codec)
  2632. return codec->name;
  2633. codec = avcodec_find_encoder(id);
  2634. if (codec)
  2635. return codec->name;
  2636. return "unknown_codec";
  2637. }
  2638. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2639. {
  2640. int i, len, ret = 0;
  2641. #define TAG_PRINT(x) \
  2642. (((x) >= '0' && (x) <= '9') || \
  2643. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2644. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2645. for (i = 0; i < 4; i++) {
  2646. len = snprintf(buf, buf_size,
  2647. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2648. buf += len;
  2649. buf_size = buf_size > len ? buf_size - len : 0;
  2650. ret += len;
  2651. codec_tag >>= 8;
  2652. }
  2653. return ret;
  2654. }
  2655. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2656. {
  2657. const char *codec_type;
  2658. const char *codec_name;
  2659. const char *profile = NULL;
  2660. const AVCodec *p;
  2661. int bitrate;
  2662. int new_line = 0;
  2663. AVRational display_aspect_ratio;
  2664. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  2665. if (!buf || buf_size <= 0)
  2666. return;
  2667. codec_type = av_get_media_type_string(enc->codec_type);
  2668. codec_name = avcodec_get_name(enc->codec_id);
  2669. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2670. if (enc->codec)
  2671. p = enc->codec;
  2672. else
  2673. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2674. avcodec_find_decoder(enc->codec_id);
  2675. if (p)
  2676. profile = av_get_profile_name(p, enc->profile);
  2677. }
  2678. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2679. codec_name);
  2680. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2681. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2682. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2683. if (profile)
  2684. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2685. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  2686. && av_log_get_level() >= AV_LOG_VERBOSE
  2687. && enc->refs)
  2688. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2689. ", %d reference frame%s",
  2690. enc->refs, enc->refs > 1 ? "s" : "");
  2691. if (enc->codec_tag) {
  2692. char tag_buf[32];
  2693. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2694. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2695. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2696. }
  2697. switch (enc->codec_type) {
  2698. case AVMEDIA_TYPE_VIDEO:
  2699. {
  2700. char detail[256] = "(";
  2701. av_strlcat(buf, separator, buf_size);
  2702. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2703. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  2704. av_get_pix_fmt_name(enc->pix_fmt));
  2705. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  2706. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2707. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2708. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2709. av_strlcatf(detail, sizeof(detail), "%s, ",
  2710. av_color_range_name(enc->color_range));
  2711. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  2712. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  2713. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  2714. if (enc->colorspace != (int)enc->color_primaries ||
  2715. enc->colorspace != (int)enc->color_trc) {
  2716. new_line = 1;
  2717. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  2718. av_color_space_name(enc->colorspace),
  2719. av_color_primaries_name(enc->color_primaries),
  2720. av_color_transfer_name(enc->color_trc));
  2721. } else
  2722. av_strlcatf(detail, sizeof(detail), "%s, ",
  2723. av_get_colorspace_name(enc->colorspace));
  2724. }
  2725. if (av_log_get_level() >= AV_LOG_DEBUG &&
  2726. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  2727. av_strlcatf(detail, sizeof(detail), "%s, ",
  2728. av_chroma_location_name(enc->chroma_sample_location));
  2729. if (strlen(detail) > 1) {
  2730. detail[strlen(detail) - 2] = 0;
  2731. av_strlcatf(buf, buf_size, "%s)", detail);
  2732. }
  2733. }
  2734. if (enc->width) {
  2735. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  2736. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2737. "%dx%d",
  2738. enc->width, enc->height);
  2739. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  2740. (enc->width != enc->coded_width ||
  2741. enc->height != enc->coded_height))
  2742. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2743. " (%dx%d)", enc->coded_width, enc->coded_height);
  2744. if (enc->sample_aspect_ratio.num) {
  2745. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2746. enc->width * enc->sample_aspect_ratio.num,
  2747. enc->height * enc->sample_aspect_ratio.den,
  2748. 1024 * 1024);
  2749. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2750. " [SAR %d:%d DAR %d:%d]",
  2751. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2752. display_aspect_ratio.num, display_aspect_ratio.den);
  2753. }
  2754. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2755. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2756. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2757. ", %d/%d",
  2758. enc->time_base.num / g, enc->time_base.den / g);
  2759. }
  2760. }
  2761. if (encode) {
  2762. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2763. ", q=%d-%d", enc->qmin, enc->qmax);
  2764. } else {
  2765. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  2766. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2767. ", Closed Captions");
  2768. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  2769. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2770. ", lossless");
  2771. }
  2772. break;
  2773. case AVMEDIA_TYPE_AUDIO:
  2774. av_strlcat(buf, separator, buf_size);
  2775. if (enc->sample_rate) {
  2776. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2777. "%d Hz, ", enc->sample_rate);
  2778. }
  2779. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2780. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2781. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2782. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2783. }
  2784. if ( enc->bits_per_raw_sample > 0
  2785. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2786. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2787. " (%d bit)", enc->bits_per_raw_sample);
  2788. break;
  2789. case AVMEDIA_TYPE_DATA:
  2790. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2791. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2792. if (g)
  2793. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2794. ", %d/%d",
  2795. enc->time_base.num / g, enc->time_base.den / g);
  2796. }
  2797. break;
  2798. case AVMEDIA_TYPE_SUBTITLE:
  2799. if (enc->width)
  2800. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2801. ", %dx%d", enc->width, enc->height);
  2802. break;
  2803. default:
  2804. return;
  2805. }
  2806. if (encode) {
  2807. if (enc->flags & AV_CODEC_FLAG_PASS1)
  2808. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2809. ", pass 1");
  2810. if (enc->flags & AV_CODEC_FLAG_PASS2)
  2811. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2812. ", pass 2");
  2813. }
  2814. bitrate = get_bit_rate(enc);
  2815. if (bitrate != 0) {
  2816. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2817. ", %d kb/s", bitrate / 1000);
  2818. } else if (enc->rc_max_rate > 0) {
  2819. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2820. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2821. }
  2822. }
  2823. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2824. {
  2825. const AVProfile *p;
  2826. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2827. return NULL;
  2828. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2829. if (p->profile == profile)
  2830. return p->name;
  2831. return NULL;
  2832. }
  2833. unsigned avcodec_version(void)
  2834. {
  2835. // av_assert0(AV_CODEC_ID_V410==164);
  2836. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2837. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2838. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2839. av_assert0(AV_CODEC_ID_SRT==94216);
  2840. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2841. #if FF_API_CODEC_ID
  2842. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2843. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2844. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2845. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2846. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2847. #endif
  2848. return LIBAVCODEC_VERSION_INT;
  2849. }
  2850. const char *avcodec_configuration(void)
  2851. {
  2852. return FFMPEG_CONFIGURATION;
  2853. }
  2854. const char *avcodec_license(void)
  2855. {
  2856. #define LICENSE_PREFIX "libavcodec license: "
  2857. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2858. }
  2859. void avcodec_flush_buffers(AVCodecContext *avctx)
  2860. {
  2861. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2862. ff_thread_flush(avctx);
  2863. else if (avctx->codec->flush)
  2864. avctx->codec->flush(avctx);
  2865. avctx->pts_correction_last_pts =
  2866. avctx->pts_correction_last_dts = INT64_MIN;
  2867. if (!avctx->refcounted_frames)
  2868. av_frame_unref(avctx->internal->to_free);
  2869. }
  2870. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2871. {
  2872. switch (codec_id) {
  2873. case AV_CODEC_ID_8SVX_EXP:
  2874. case AV_CODEC_ID_8SVX_FIB:
  2875. case AV_CODEC_ID_ADPCM_CT:
  2876. case AV_CODEC_ID_ADPCM_IMA_APC:
  2877. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2878. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2879. case AV_CODEC_ID_ADPCM_IMA_WS:
  2880. case AV_CODEC_ID_ADPCM_G722:
  2881. case AV_CODEC_ID_ADPCM_YAMAHA:
  2882. return 4;
  2883. case AV_CODEC_ID_DSD_LSBF:
  2884. case AV_CODEC_ID_DSD_MSBF:
  2885. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2886. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2887. case AV_CODEC_ID_PCM_ALAW:
  2888. case AV_CODEC_ID_PCM_MULAW:
  2889. case AV_CODEC_ID_PCM_S8:
  2890. case AV_CODEC_ID_PCM_S8_PLANAR:
  2891. case AV_CODEC_ID_PCM_U8:
  2892. case AV_CODEC_ID_PCM_ZORK:
  2893. return 8;
  2894. case AV_CODEC_ID_PCM_S16BE:
  2895. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2896. case AV_CODEC_ID_PCM_S16LE:
  2897. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2898. case AV_CODEC_ID_PCM_U16BE:
  2899. case AV_CODEC_ID_PCM_U16LE:
  2900. return 16;
  2901. case AV_CODEC_ID_PCM_S24DAUD:
  2902. case AV_CODEC_ID_PCM_S24BE:
  2903. case AV_CODEC_ID_PCM_S24LE:
  2904. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2905. case AV_CODEC_ID_PCM_U24BE:
  2906. case AV_CODEC_ID_PCM_U24LE:
  2907. return 24;
  2908. case AV_CODEC_ID_PCM_S32BE:
  2909. case AV_CODEC_ID_PCM_S32LE:
  2910. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2911. case AV_CODEC_ID_PCM_U32BE:
  2912. case AV_CODEC_ID_PCM_U32LE:
  2913. case AV_CODEC_ID_PCM_F32BE:
  2914. case AV_CODEC_ID_PCM_F32LE:
  2915. return 32;
  2916. case AV_CODEC_ID_PCM_F64BE:
  2917. case AV_CODEC_ID_PCM_F64LE:
  2918. return 64;
  2919. default:
  2920. return 0;
  2921. }
  2922. }
  2923. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2924. {
  2925. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2926. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2927. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2928. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2929. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2930. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2931. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2932. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2933. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2934. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2935. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2936. };
  2937. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2938. return AV_CODEC_ID_NONE;
  2939. if (be < 0 || be > 1)
  2940. be = AV_NE(1, 0);
  2941. return map[fmt][be];
  2942. }
  2943. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2944. {
  2945. switch (codec_id) {
  2946. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2947. return 2;
  2948. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2949. return 3;
  2950. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2951. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2952. case AV_CODEC_ID_ADPCM_IMA_QT:
  2953. case AV_CODEC_ID_ADPCM_SWF:
  2954. case AV_CODEC_ID_ADPCM_MS:
  2955. return 4;
  2956. default:
  2957. return av_get_exact_bits_per_sample(codec_id);
  2958. }
  2959. }
  2960. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2961. {
  2962. int id, sr, ch, ba, tag, bps;
  2963. id = avctx->codec_id;
  2964. sr = avctx->sample_rate;
  2965. ch = avctx->channels;
  2966. ba = avctx->block_align;
  2967. tag = avctx->codec_tag;
  2968. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2969. /* codecs with an exact constant bits per sample */
  2970. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2971. return (frame_bytes * 8LL) / (bps * ch);
  2972. bps = avctx->bits_per_coded_sample;
  2973. /* codecs with a fixed packet duration */
  2974. switch (id) {
  2975. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2976. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2977. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2978. case AV_CODEC_ID_AMR_NB:
  2979. case AV_CODEC_ID_EVRC:
  2980. case AV_CODEC_ID_GSM:
  2981. case AV_CODEC_ID_QCELP:
  2982. case AV_CODEC_ID_RA_288: return 160;
  2983. case AV_CODEC_ID_AMR_WB:
  2984. case AV_CODEC_ID_GSM_MS: return 320;
  2985. case AV_CODEC_ID_MP1: return 384;
  2986. case AV_CODEC_ID_ATRAC1: return 512;
  2987. case AV_CODEC_ID_ATRAC3: return 1024;
  2988. case AV_CODEC_ID_ATRAC3P: return 2048;
  2989. case AV_CODEC_ID_MP2:
  2990. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2991. case AV_CODEC_ID_AC3: return 1536;
  2992. }
  2993. if (sr > 0) {
  2994. /* calc from sample rate */
  2995. if (id == AV_CODEC_ID_TTA)
  2996. return 256 * sr / 245;
  2997. if (ch > 0) {
  2998. /* calc from sample rate and channels */
  2999. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  3000. return (480 << (sr / 22050)) / ch;
  3001. }
  3002. }
  3003. if (ba > 0) {
  3004. /* calc from block_align */
  3005. if (id == AV_CODEC_ID_SIPR) {
  3006. switch (ba) {
  3007. case 20: return 160;
  3008. case 19: return 144;
  3009. case 29: return 288;
  3010. case 37: return 480;
  3011. }
  3012. } else if (id == AV_CODEC_ID_ILBC) {
  3013. switch (ba) {
  3014. case 38: return 160;
  3015. case 50: return 240;
  3016. }
  3017. }
  3018. }
  3019. if (frame_bytes > 0) {
  3020. /* calc from frame_bytes only */
  3021. if (id == AV_CODEC_ID_TRUESPEECH)
  3022. return 240 * (frame_bytes / 32);
  3023. if (id == AV_CODEC_ID_NELLYMOSER)
  3024. return 256 * (frame_bytes / 64);
  3025. if (id == AV_CODEC_ID_RA_144)
  3026. return 160 * (frame_bytes / 20);
  3027. if (id == AV_CODEC_ID_G723_1)
  3028. return 240 * (frame_bytes / 24);
  3029. if (bps > 0) {
  3030. /* calc from frame_bytes and bits_per_coded_sample */
  3031. if (id == AV_CODEC_ID_ADPCM_G726)
  3032. return frame_bytes * 8 / bps;
  3033. }
  3034. if (ch > 0) {
  3035. /* calc from frame_bytes and channels */
  3036. switch (id) {
  3037. case AV_CODEC_ID_ADPCM_AFC:
  3038. return frame_bytes / (9 * ch) * 16;
  3039. case AV_CODEC_ID_ADPCM_DTK:
  3040. return frame_bytes / (16 * ch) * 28;
  3041. case AV_CODEC_ID_ADPCM_4XM:
  3042. case AV_CODEC_ID_ADPCM_IMA_ISS:
  3043. return (frame_bytes - 4 * ch) * 2 / ch;
  3044. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  3045. return (frame_bytes - 4) * 2 / ch;
  3046. case AV_CODEC_ID_ADPCM_IMA_AMV:
  3047. return (frame_bytes - 8) * 2 / ch;
  3048. case AV_CODEC_ID_ADPCM_THP:
  3049. case AV_CODEC_ID_ADPCM_THP_LE:
  3050. if (avctx->extradata)
  3051. return frame_bytes * 14 / (8 * ch);
  3052. break;
  3053. case AV_CODEC_ID_ADPCM_XA:
  3054. return (frame_bytes / 128) * 224 / ch;
  3055. case AV_CODEC_ID_INTERPLAY_DPCM:
  3056. return (frame_bytes - 6 - ch) / ch;
  3057. case AV_CODEC_ID_ROQ_DPCM:
  3058. return (frame_bytes - 8) / ch;
  3059. case AV_CODEC_ID_XAN_DPCM:
  3060. return (frame_bytes - 2 * ch) / ch;
  3061. case AV_CODEC_ID_MACE3:
  3062. return 3 * frame_bytes / ch;
  3063. case AV_CODEC_ID_MACE6:
  3064. return 6 * frame_bytes / ch;
  3065. case AV_CODEC_ID_PCM_LXF:
  3066. return 2 * (frame_bytes / (5 * ch));
  3067. case AV_CODEC_ID_IAC:
  3068. case AV_CODEC_ID_IMC:
  3069. return 4 * frame_bytes / ch;
  3070. }
  3071. if (tag) {
  3072. /* calc from frame_bytes, channels, and codec_tag */
  3073. if (id == AV_CODEC_ID_SOL_DPCM) {
  3074. if (tag == 3)
  3075. return frame_bytes / ch;
  3076. else
  3077. return frame_bytes * 2 / ch;
  3078. }
  3079. }
  3080. if (ba > 0) {
  3081. /* calc from frame_bytes, channels, and block_align */
  3082. int blocks = frame_bytes / ba;
  3083. switch (avctx->codec_id) {
  3084. case AV_CODEC_ID_ADPCM_IMA_WAV:
  3085. if (bps < 2 || bps > 5)
  3086. return 0;
  3087. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  3088. case AV_CODEC_ID_ADPCM_IMA_DK3:
  3089. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  3090. case AV_CODEC_ID_ADPCM_IMA_DK4:
  3091. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  3092. case AV_CODEC_ID_ADPCM_IMA_RAD:
  3093. return blocks * ((ba - 4 * ch) * 2 / ch);
  3094. case AV_CODEC_ID_ADPCM_MS:
  3095. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  3096. }
  3097. }
  3098. if (bps > 0) {
  3099. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  3100. switch (avctx->codec_id) {
  3101. case AV_CODEC_ID_PCM_DVD:
  3102. if(bps<4)
  3103. return 0;
  3104. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  3105. case AV_CODEC_ID_PCM_BLURAY:
  3106. if(bps<4)
  3107. return 0;
  3108. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  3109. case AV_CODEC_ID_S302M:
  3110. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  3111. }
  3112. }
  3113. }
  3114. }
  3115. /* Fall back on using frame_size */
  3116. if (avctx->frame_size > 1 && frame_bytes)
  3117. return avctx->frame_size;
  3118. //For WMA we currently have no other means to calculate duration thus we
  3119. //do it here by assuming CBR, which is true for all known cases.
  3120. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  3121. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  3122. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  3123. }
  3124. return 0;
  3125. }
  3126. #if !HAVE_THREADS
  3127. int ff_thread_init(AVCodecContext *s)
  3128. {
  3129. return -1;
  3130. }
  3131. #endif
  3132. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  3133. {
  3134. unsigned int n = 0;
  3135. while (v >= 0xff) {
  3136. *s++ = 0xff;
  3137. v -= 0xff;
  3138. n++;
  3139. }
  3140. *s = v;
  3141. n++;
  3142. return n;
  3143. }
  3144. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  3145. {
  3146. int i;
  3147. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  3148. return i;
  3149. }
  3150. #if FF_API_MISSING_SAMPLE
  3151. FF_DISABLE_DEPRECATION_WARNINGS
  3152. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  3153. {
  3154. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  3155. "version to the newest one from Git. If the problem still "
  3156. "occurs, it means that your file has a feature which has not "
  3157. "been implemented.\n", feature);
  3158. if(want_sample)
  3159. av_log_ask_for_sample(avc, NULL);
  3160. }
  3161. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  3162. {
  3163. va_list argument_list;
  3164. va_start(argument_list, msg);
  3165. if (msg)
  3166. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  3167. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  3168. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  3169. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  3170. va_end(argument_list);
  3171. }
  3172. FF_ENABLE_DEPRECATION_WARNINGS
  3173. #endif /* FF_API_MISSING_SAMPLE */
  3174. static AVHWAccel *first_hwaccel = NULL;
  3175. static AVHWAccel **last_hwaccel = &first_hwaccel;
  3176. void av_register_hwaccel(AVHWAccel *hwaccel)
  3177. {
  3178. AVHWAccel **p = last_hwaccel;
  3179. hwaccel->next = NULL;
  3180. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3181. p = &(*p)->next;
  3182. last_hwaccel = &hwaccel->next;
  3183. }
  3184. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  3185. {
  3186. return hwaccel ? hwaccel->next : first_hwaccel;
  3187. }
  3188. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3189. {
  3190. if (lockmgr_cb) {
  3191. // There is no good way to rollback a failure to destroy the
  3192. // mutex, so we ignore failures.
  3193. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  3194. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  3195. lockmgr_cb = NULL;
  3196. codec_mutex = NULL;
  3197. avformat_mutex = NULL;
  3198. }
  3199. if (cb) {
  3200. void *new_codec_mutex = NULL;
  3201. void *new_avformat_mutex = NULL;
  3202. int err;
  3203. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  3204. return err > 0 ? AVERROR_UNKNOWN : err;
  3205. }
  3206. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  3207. // Ignore failures to destroy the newly created mutex.
  3208. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  3209. return err > 0 ? AVERROR_UNKNOWN : err;
  3210. }
  3211. lockmgr_cb = cb;
  3212. codec_mutex = new_codec_mutex;
  3213. avformat_mutex = new_avformat_mutex;
  3214. }
  3215. return 0;
  3216. }
  3217. int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  3218. {
  3219. if (lockmgr_cb) {
  3220. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3221. return -1;
  3222. }
  3223. if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1 &&
  3224. !(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
  3225. av_log(log_ctx, AV_LOG_ERROR,
  3226. "Insufficient thread locking. At least %d threads are "
  3227. "calling avcodec_open2() at the same time right now.\n",
  3228. entangled_thread_counter);
  3229. if (!lockmgr_cb)
  3230. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3231. ff_avcodec_locked = 1;
  3232. ff_unlock_avcodec();
  3233. return AVERROR(EINVAL);
  3234. }
  3235. av_assert0(!ff_avcodec_locked);
  3236. ff_avcodec_locked = 1;
  3237. return 0;
  3238. }
  3239. int ff_unlock_avcodec(void)
  3240. {
  3241. av_assert0(ff_avcodec_locked);
  3242. ff_avcodec_locked = 0;
  3243. avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
  3244. if (lockmgr_cb) {
  3245. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3246. return -1;
  3247. }
  3248. return 0;
  3249. }
  3250. int avpriv_lock_avformat(void)
  3251. {
  3252. if (lockmgr_cb) {
  3253. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3254. return -1;
  3255. }
  3256. return 0;
  3257. }
  3258. int avpriv_unlock_avformat(void)
  3259. {
  3260. if (lockmgr_cb) {
  3261. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3262. return -1;
  3263. }
  3264. return 0;
  3265. }
  3266. unsigned int avpriv_toupper4(unsigned int x)
  3267. {
  3268. return av_toupper(x & 0xFF) +
  3269. (av_toupper((x >> 8) & 0xFF) << 8) +
  3270. (av_toupper((x >> 16) & 0xFF) << 16) +
  3271. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  3272. }
  3273. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3274. {
  3275. int ret;
  3276. dst->owner = src->owner;
  3277. ret = av_frame_ref(dst->f, src->f);
  3278. if (ret < 0)
  3279. return ret;
  3280. av_assert0(!dst->progress);
  3281. if (src->progress &&
  3282. !(dst->progress = av_buffer_ref(src->progress))) {
  3283. ff_thread_release_buffer(dst->owner, dst);
  3284. return AVERROR(ENOMEM);
  3285. }
  3286. return 0;
  3287. }
  3288. #if !HAVE_THREADS
  3289. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3290. {
  3291. return ff_get_format(avctx, fmt);
  3292. }
  3293. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3294. {
  3295. f->owner = avctx;
  3296. return ff_get_buffer(avctx, f->f, flags);
  3297. }
  3298. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3299. {
  3300. if (f->f)
  3301. av_frame_unref(f->f);
  3302. }
  3303. void ff_thread_finish_setup(AVCodecContext *avctx)
  3304. {
  3305. }
  3306. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3307. {
  3308. }
  3309. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3310. {
  3311. }
  3312. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3313. {
  3314. return 1;
  3315. }
  3316. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3317. {
  3318. return 0;
  3319. }
  3320. void ff_reset_entries(AVCodecContext *avctx)
  3321. {
  3322. }
  3323. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3324. {
  3325. }
  3326. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3327. {
  3328. }
  3329. #endif
  3330. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  3331. {
  3332. AVCodec *c= avcodec_find_decoder(codec_id);
  3333. if(!c)
  3334. c= avcodec_find_encoder(codec_id);
  3335. if(c)
  3336. return c->type;
  3337. if (codec_id <= AV_CODEC_ID_NONE)
  3338. return AVMEDIA_TYPE_UNKNOWN;
  3339. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  3340. return AVMEDIA_TYPE_VIDEO;
  3341. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  3342. return AVMEDIA_TYPE_AUDIO;
  3343. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  3344. return AVMEDIA_TYPE_SUBTITLE;
  3345. return AVMEDIA_TYPE_UNKNOWN;
  3346. }
  3347. int avcodec_is_open(AVCodecContext *s)
  3348. {
  3349. return !!s->internal;
  3350. }
  3351. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3352. {
  3353. int ret;
  3354. char *str;
  3355. ret = av_bprint_finalize(buf, &str);
  3356. if (ret < 0)
  3357. return ret;
  3358. if (!av_bprint_is_complete(buf)) {
  3359. av_free(str);
  3360. return AVERROR(ENOMEM);
  3361. }
  3362. avctx->extradata = str;
  3363. /* Note: the string is NUL terminated (so extradata can be read as a
  3364. * string), but the ending character is not accounted in the size (in
  3365. * binary formats you are likely not supposed to mux that character). When
  3366. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  3367. * zeros. */
  3368. avctx->extradata_size = buf->len;
  3369. return 0;
  3370. }
  3371. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3372. const uint8_t *end,
  3373. uint32_t *av_restrict state)
  3374. {
  3375. int i;
  3376. av_assert0(p <= end);
  3377. if (p >= end)
  3378. return end;
  3379. for (i = 0; i < 3; i++) {
  3380. uint32_t tmp = *state << 8;
  3381. *state = tmp + *(p++);
  3382. if (tmp == 0x100 || p == end)
  3383. return p;
  3384. }
  3385. while (p < end) {
  3386. if (p[-1] > 1 ) p += 3;
  3387. else if (p[-2] ) p += 2;
  3388. else if (p[-3]|(p[-1]-1)) p++;
  3389. else {
  3390. p++;
  3391. break;
  3392. }
  3393. }
  3394. p = FFMIN(p, end) - 4;
  3395. *state = AV_RB32(p);
  3396. return p + 4;
  3397. }