decode.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. /*
  2. * generic decoding-related code
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include "config.h"
  23. #if CONFIG_ICONV
  24. # include <iconv.h>
  25. #endif
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/emms.h"
  30. #include "libavutil/frame.h"
  31. #include "libavutil/hwcontext.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/internal.h"
  34. #include "avcodec.h"
  35. #include "avcodec_internal.h"
  36. #include "bytestream.h"
  37. #include "bsf.h"
  38. #include "codec_desc.h"
  39. #include "codec_internal.h"
  40. #include "decode.h"
  41. #include "hwaccel_internal.h"
  42. #include "hwconfig.h"
  43. #include "internal.h"
  44. #include "packet_internal.h"
  45. #include "refstruct.h"
  46. #include "thread.h"
  47. typedef struct DecodeContext {
  48. AVCodecInternal avci;
  49. /* to prevent infinite loop on errors when draining */
  50. int nb_draining_errors;
  51. /**
  52. * The caller has submitted a NULL packet on input.
  53. */
  54. int draining_started;
  55. } DecodeContext;
  56. static DecodeContext *decode_ctx(AVCodecInternal *avci)
  57. {
  58. return (DecodeContext *)avci;
  59. }
  60. static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
  61. {
  62. int ret;
  63. size_t size;
  64. const uint8_t *data;
  65. uint32_t flags;
  66. int64_t val;
  67. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  68. if (!data)
  69. return 0;
  70. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  71. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  72. "changes, but PARAM_CHANGE side data was sent to it.\n");
  73. ret = AVERROR(EINVAL);
  74. goto fail2;
  75. }
  76. if (size < 4)
  77. goto fail;
  78. flags = bytestream_get_le32(&data);
  79. size -= 4;
  80. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  81. if (size < 4)
  82. goto fail;
  83. val = bytestream_get_le32(&data);
  84. if (val <= 0 || val > INT_MAX) {
  85. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
  86. ret = AVERROR_INVALIDDATA;
  87. goto fail2;
  88. }
  89. avctx->sample_rate = val;
  90. size -= 4;
  91. }
  92. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  93. if (size < 8)
  94. goto fail;
  95. avctx->width = bytestream_get_le32(&data);
  96. avctx->height = bytestream_get_le32(&data);
  97. size -= 8;
  98. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  99. if (ret < 0)
  100. goto fail2;
  101. }
  102. return 0;
  103. fail:
  104. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  105. ret = AVERROR_INVALIDDATA;
  106. fail2:
  107. if (ret < 0) {
  108. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  109. if (avctx->err_recognition & AV_EF_EXPLODE)
  110. return ret;
  111. }
  112. return 0;
  113. }
  114. static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
  115. {
  116. int ret = 0;
  117. av_packet_unref(avci->last_pkt_props);
  118. if (pkt) {
  119. ret = av_packet_copy_props(avci->last_pkt_props, pkt);
  120. #if FF_API_FRAME_PKT
  121. if (!ret)
  122. avci->last_pkt_props->stream_index = pkt->size; // Needed for ff_decode_frame_props().
  123. #endif
  124. }
  125. return ret;
  126. }
  127. static int decode_bsfs_init(AVCodecContext *avctx)
  128. {
  129. AVCodecInternal *avci = avctx->internal;
  130. const FFCodec *const codec = ffcodec(avctx->codec);
  131. int ret;
  132. if (avci->bsf)
  133. return 0;
  134. ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf);
  135. if (ret < 0) {
  136. av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret));
  137. if (ret != AVERROR(ENOMEM))
  138. ret = AVERROR_BUG;
  139. goto fail;
  140. }
  141. /* We do not currently have an API for passing the input timebase into decoders,
  142. * but no filters used here should actually need it.
  143. * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
  144. avci->bsf->time_base_in = (AVRational){ 1, 90000 };
  145. ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
  146. if (ret < 0)
  147. goto fail;
  148. ret = av_bsf_init(avci->bsf);
  149. if (ret < 0)
  150. goto fail;
  151. return 0;
  152. fail:
  153. av_bsf_free(&avci->bsf);
  154. return ret;
  155. }
  156. static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
  157. {
  158. AVCodecInternal *avci = avctx->internal;
  159. int ret;
  160. ret = av_bsf_receive_packet(avci->bsf, pkt);
  161. if (ret == AVERROR_EOF)
  162. avci->draining = 1;
  163. if (ret < 0)
  164. return ret;
  165. if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
  166. ret = extract_packet_props(avctx->internal, pkt);
  167. if (ret < 0)
  168. goto finish;
  169. }
  170. ret = apply_param_change(avctx, pkt);
  171. if (ret < 0)
  172. goto finish;
  173. return 0;
  174. finish:
  175. av_packet_unref(pkt);
  176. return ret;
  177. }
  178. int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
  179. {
  180. AVCodecInternal *avci = avctx->internal;
  181. DecodeContext *dc = decode_ctx(avci);
  182. if (avci->draining)
  183. return AVERROR_EOF;
  184. while (1) {
  185. int ret = decode_get_packet(avctx, pkt);
  186. if (ret == AVERROR(EAGAIN) &&
  187. (!AVPACKET_IS_EMPTY(avci->buffer_pkt) || dc->draining_started)) {
  188. ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
  189. if (ret < 0) {
  190. av_packet_unref(avci->buffer_pkt);
  191. return ret;
  192. }
  193. continue;
  194. }
  195. return ret;
  196. }
  197. }
  198. /**
  199. * Attempt to guess proper monotonic timestamps for decoded video frames
  200. * which might have incorrect times. Input timestamps may wrap around, in
  201. * which case the output will as well.
  202. *
  203. * @param pts the pts field of the decoded AVPacket, as passed through
  204. * AVFrame.pts
  205. * @param dts the dts field of the decoded AVPacket
  206. * @return one of the input values, may be AV_NOPTS_VALUE
  207. */
  208. static int64_t guess_correct_pts(AVCodecContext *ctx,
  209. int64_t reordered_pts, int64_t dts)
  210. {
  211. int64_t pts = AV_NOPTS_VALUE;
  212. if (dts != AV_NOPTS_VALUE) {
  213. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  214. ctx->pts_correction_last_dts = dts;
  215. } else if (reordered_pts != AV_NOPTS_VALUE)
  216. ctx->pts_correction_last_dts = reordered_pts;
  217. if (reordered_pts != AV_NOPTS_VALUE) {
  218. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  219. ctx->pts_correction_last_pts = reordered_pts;
  220. } else if(dts != AV_NOPTS_VALUE)
  221. ctx->pts_correction_last_pts = dts;
  222. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  223. && reordered_pts != AV_NOPTS_VALUE)
  224. pts = reordered_pts;
  225. else
  226. pts = dts;
  227. return pts;
  228. }
  229. static int discard_samples(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
  230. {
  231. AVCodecInternal *avci = avctx->internal;
  232. AVFrameSideData *side;
  233. uint32_t discard_padding = 0;
  234. uint8_t skip_reason = 0;
  235. uint8_t discard_reason = 0;
  236. side = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
  237. if (side && side->size >= 10) {
  238. avci->skip_samples = AV_RL32(side->data);
  239. avci->skip_samples = FFMAX(0, avci->skip_samples);
  240. discard_padding = AV_RL32(side->data + 4);
  241. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  242. avci->skip_samples, (int)discard_padding);
  243. skip_reason = AV_RL8(side->data + 8);
  244. discard_reason = AV_RL8(side->data + 9);
  245. }
  246. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  247. if (!side && (avci->skip_samples || discard_padding))
  248. side = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  249. if (side && (avci->skip_samples || discard_padding)) {
  250. AV_WL32(side->data, avci->skip_samples);
  251. AV_WL32(side->data + 4, discard_padding);
  252. AV_WL8(side->data + 8, skip_reason);
  253. AV_WL8(side->data + 9, discard_reason);
  254. avci->skip_samples = 0;
  255. }
  256. return 0;
  257. }
  258. av_frame_remove_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
  259. if ((frame->flags & AV_FRAME_FLAG_DISCARD)) {
  260. avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
  261. *discarded_samples += frame->nb_samples;
  262. return AVERROR(EAGAIN);
  263. }
  264. if (avci->skip_samples > 0) {
  265. if (frame->nb_samples <= avci->skip_samples){
  266. *discarded_samples += frame->nb_samples;
  267. avci->skip_samples -= frame->nb_samples;
  268. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  269. avci->skip_samples);
  270. return AVERROR(EAGAIN);
  271. } else {
  272. av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
  273. frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format);
  274. if (avctx->pkt_timebase.num && avctx->sample_rate) {
  275. int64_t diff_ts = av_rescale_q(avci->skip_samples,
  276. (AVRational){1, avctx->sample_rate},
  277. avctx->pkt_timebase);
  278. if (frame->pts != AV_NOPTS_VALUE)
  279. frame->pts += diff_ts;
  280. if (frame->pkt_dts != AV_NOPTS_VALUE)
  281. frame->pkt_dts += diff_ts;
  282. if (frame->duration >= diff_ts)
  283. frame->duration -= diff_ts;
  284. } else
  285. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  286. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  287. avci->skip_samples, frame->nb_samples);
  288. *discarded_samples += avci->skip_samples;
  289. frame->nb_samples -= avci->skip_samples;
  290. avci->skip_samples = 0;
  291. }
  292. }
  293. if (discard_padding > 0 && discard_padding <= frame->nb_samples) {
  294. if (discard_padding == frame->nb_samples) {
  295. *discarded_samples += frame->nb_samples;
  296. return AVERROR(EAGAIN);
  297. } else {
  298. if (avctx->pkt_timebase.num && avctx->sample_rate) {
  299. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  300. (AVRational){1, avctx->sample_rate},
  301. avctx->pkt_timebase);
  302. frame->duration = diff_ts;
  303. } else
  304. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  305. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  306. (int)discard_padding, frame->nb_samples);
  307. frame->nb_samples -= discard_padding;
  308. }
  309. }
  310. return 0;
  311. }
  312. /*
  313. * The core of the receive_frame_wrapper for the decoders implementing
  314. * the simple API. Certain decoders might consume partial packets without
  315. * returning any output, so this function needs to be called in a loop until it
  316. * returns EAGAIN.
  317. **/
  318. static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples)
  319. {
  320. AVCodecInternal *avci = avctx->internal;
  321. AVPacket *const pkt = avci->in_pkt;
  322. const FFCodec *const codec = ffcodec(avctx->codec);
  323. int got_frame, consumed;
  324. int ret;
  325. if (!pkt->data && !avci->draining) {
  326. av_packet_unref(pkt);
  327. ret = ff_decode_get_packet(avctx, pkt);
  328. if (ret < 0 && ret != AVERROR_EOF)
  329. return ret;
  330. }
  331. // Some codecs (at least wma lossless) will crash when feeding drain packets
  332. // after EOF was signaled.
  333. if (avci->draining_done)
  334. return AVERROR_EOF;
  335. if (!pkt->data &&
  336. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
  337. avctx->active_thread_type & FF_THREAD_FRAME))
  338. return AVERROR_EOF;
  339. got_frame = 0;
  340. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
  341. consumed = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
  342. } else {
  343. consumed = codec->cb.decode(avctx, frame, &got_frame, pkt);
  344. if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  345. frame->pkt_dts = pkt->dts;
  346. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  347. #if FF_API_FRAME_PKT
  348. FF_DISABLE_DEPRECATION_WARNINGS
  349. if(!avctx->has_b_frames)
  350. frame->pkt_pos = pkt->pos;
  351. FF_ENABLE_DEPRECATION_WARNINGS
  352. #endif
  353. }
  354. }
  355. emms_c();
  356. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  357. ret = (!got_frame || frame->flags & AV_FRAME_FLAG_DISCARD)
  358. ? AVERROR(EAGAIN)
  359. : 0;
  360. } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  361. ret = !got_frame ? AVERROR(EAGAIN)
  362. : discard_samples(avctx, frame, discarded_samples);
  363. }
  364. if (ret == AVERROR(EAGAIN))
  365. av_frame_unref(frame);
  366. // FF_CODEC_CB_TYPE_DECODE decoders must not return AVERROR EAGAIN
  367. // code later will add AVERROR(EAGAIN) to a pointer
  368. av_assert0(consumed != AVERROR(EAGAIN));
  369. if (consumed < 0)
  370. ret = consumed;
  371. if (consumed >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
  372. consumed = pkt->size;
  373. if (!ret)
  374. av_assert0(frame->buf[0]);
  375. if (ret == AVERROR(EAGAIN))
  376. ret = 0;
  377. /* do not stop draining when got_frame != 0 or ret < 0 */
  378. if (avci->draining && !got_frame) {
  379. if (ret < 0) {
  380. /* prevent infinite loop if a decoder wrongly always return error on draining */
  381. /* reasonable nb_errors_max = maximum b frames + thread count */
  382. int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ?
  383. avctx->thread_count : 1);
  384. if (decode_ctx(avci)->nb_draining_errors++ >= nb_errors_max) {
  385. av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
  386. "Stop draining and force EOF.\n");
  387. avci->draining_done = 1;
  388. ret = AVERROR_BUG;
  389. }
  390. } else {
  391. avci->draining_done = 1;
  392. }
  393. }
  394. if (consumed >= pkt->size || ret < 0) {
  395. av_packet_unref(pkt);
  396. } else {
  397. pkt->data += consumed;
  398. pkt->size -= consumed;
  399. pkt->pts = AV_NOPTS_VALUE;
  400. pkt->dts = AV_NOPTS_VALUE;
  401. if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
  402. #if FF_API_FRAME_PKT
  403. // See extract_packet_props() comment.
  404. avci->last_pkt_props->stream_index = avci->last_pkt_props->stream_index - consumed;
  405. #endif
  406. avci->last_pkt_props->pts = AV_NOPTS_VALUE;
  407. avci->last_pkt_props->dts = AV_NOPTS_VALUE;
  408. }
  409. }
  410. return ret;
  411. }
  412. #if CONFIG_LCMS2
  413. static int detect_colorspace(AVCodecContext *avctx, AVFrame *frame)
  414. {
  415. AVCodecInternal *avci = avctx->internal;
  416. enum AVColorTransferCharacteristic trc;
  417. AVColorPrimariesDesc coeffs;
  418. enum AVColorPrimaries prim;
  419. cmsHPROFILE profile;
  420. AVFrameSideData *sd;
  421. int ret;
  422. if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
  423. return 0;
  424. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
  425. if (!sd || !sd->size)
  426. return 0;
  427. if (!avci->icc.avctx) {
  428. ret = ff_icc_context_init(&avci->icc, avctx);
  429. if (ret < 0)
  430. return ret;
  431. }
  432. profile = cmsOpenProfileFromMemTHR(avci->icc.ctx, sd->data, sd->size);
  433. if (!profile)
  434. return AVERROR_INVALIDDATA;
  435. ret = ff_icc_profile_sanitize(&avci->icc, profile);
  436. if (!ret)
  437. ret = ff_icc_profile_read_primaries(&avci->icc, profile, &coeffs);
  438. if (!ret)
  439. ret = ff_icc_profile_detect_transfer(&avci->icc, profile, &trc);
  440. cmsCloseProfile(profile);
  441. if (ret < 0)
  442. return ret;
  443. prim = av_csp_primaries_id_from_desc(&coeffs);
  444. if (prim != AVCOL_PRI_UNSPECIFIED)
  445. frame->color_primaries = prim;
  446. if (trc != AVCOL_TRC_UNSPECIFIED)
  447. frame->color_trc = trc;
  448. return 0;
  449. }
  450. #else /* !CONFIG_LCMS2 */
  451. static int detect_colorspace(av_unused AVCodecContext *c, av_unused AVFrame *f)
  452. {
  453. return 0;
  454. }
  455. #endif
  456. static int fill_frame_props(const AVCodecContext *avctx, AVFrame *frame)
  457. {
  458. int ret;
  459. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  460. frame->color_primaries = avctx->color_primaries;
  461. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  462. frame->color_trc = avctx->color_trc;
  463. if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
  464. frame->colorspace = avctx->colorspace;
  465. if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
  466. frame->color_range = avctx->color_range;
  467. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  468. frame->chroma_location = avctx->chroma_sample_location;
  469. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  470. if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  471. if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
  472. } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  473. if (frame->format == AV_SAMPLE_FMT_NONE)
  474. frame->format = avctx->sample_fmt;
  475. if (!frame->ch_layout.nb_channels) {
  476. ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
  477. if (ret < 0)
  478. return ret;
  479. }
  480. if (!frame->sample_rate)
  481. frame->sample_rate = avctx->sample_rate;
  482. }
  483. return 0;
  484. }
  485. static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  486. {
  487. int ret;
  488. int64_t discarded_samples = 0;
  489. while (!frame->buf[0]) {
  490. if (discarded_samples > avctx->max_samples)
  491. return AVERROR(EAGAIN);
  492. ret = decode_simple_internal(avctx, frame, &discarded_samples);
  493. if (ret < 0)
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
  499. {
  500. AVCodecInternal *avci = avctx->internal;
  501. const FFCodec *const codec = ffcodec(avctx->codec);
  502. int ret, ok;
  503. av_assert0(!frame->buf[0]);
  504. if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) {
  505. ret = codec->cb.receive_frame(avctx, frame);
  506. emms_c();
  507. if (!ret) {
  508. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO)
  509. ret = (frame->flags & AV_FRAME_FLAG_DISCARD) ? AVERROR(EAGAIN) : 0;
  510. else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  511. int64_t discarded_samples = 0;
  512. ret = discard_samples(avctx, frame, &discarded_samples);
  513. }
  514. }
  515. } else
  516. ret = decode_simple_receive_frame(avctx, frame);
  517. if (ret == AVERROR_EOF)
  518. avci->draining_done = 1;
  519. /* preserve ret */
  520. ok = detect_colorspace(avctx, frame);
  521. if (ok < 0) {
  522. av_frame_unref(frame);
  523. return ok;
  524. }
  525. if (!ret) {
  526. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  527. if (!frame->width)
  528. frame->width = avctx->width;
  529. if (!frame->height)
  530. frame->height = avctx->height;
  531. } else
  532. frame->flags |= AV_FRAME_FLAG_KEY;
  533. ret = fill_frame_props(avctx, frame);
  534. if (ret < 0) {
  535. av_frame_unref(frame);
  536. return ret;
  537. }
  538. #if FF_API_FRAME_KEY
  539. FF_DISABLE_DEPRECATION_WARNINGS
  540. frame->key_frame = !!(frame->flags & AV_FRAME_FLAG_KEY);
  541. FF_ENABLE_DEPRECATION_WARNINGS
  542. #endif
  543. #if FF_API_INTERLACED_FRAME
  544. FF_DISABLE_DEPRECATION_WARNINGS
  545. frame->interlaced_frame = !!(frame->flags & AV_FRAME_FLAG_INTERLACED);
  546. frame->top_field_first = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
  547. FF_ENABLE_DEPRECATION_WARNINGS
  548. #endif
  549. frame->best_effort_timestamp = guess_correct_pts(avctx,
  550. frame->pts,
  551. frame->pkt_dts);
  552. /* the only case where decode data is not set should be decoders
  553. * that do not call ff_get_buffer() */
  554. av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
  555. !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
  556. if (frame->private_ref) {
  557. FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
  558. if (fdd->post_process) {
  559. ret = fdd->post_process(avctx, frame);
  560. if (ret < 0) {
  561. av_frame_unref(frame);
  562. return ret;
  563. }
  564. }
  565. }
  566. }
  567. /* free the per-frame decode data */
  568. av_buffer_unref(&frame->private_ref);
  569. return ret;
  570. }
  571. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  572. {
  573. AVCodecInternal *avci = avctx->internal;
  574. DecodeContext *dc = decode_ctx(avci);
  575. int ret;
  576. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  577. return AVERROR(EINVAL);
  578. if (dc->draining_started)
  579. return AVERROR_EOF;
  580. if (avpkt && !avpkt->size && avpkt->data)
  581. return AVERROR(EINVAL);
  582. if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
  583. if (!AVPACKET_IS_EMPTY(avci->buffer_pkt))
  584. return AVERROR(EAGAIN);
  585. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  586. if (ret < 0)
  587. return ret;
  588. } else
  589. dc->draining_started = 1;
  590. if (!avci->buffer_frame->buf[0] && !dc->draining_started) {
  591. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  592. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  593. return ret;
  594. }
  595. return 0;
  596. }
  597. static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
  598. {
  599. /* make sure we are noisy about decoders returning invalid cropping data */
  600. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  601. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  602. (frame->crop_left + frame->crop_right) >= frame->width ||
  603. (frame->crop_top + frame->crop_bottom) >= frame->height) {
  604. av_log(avctx, AV_LOG_WARNING,
  605. "Invalid cropping information set by a decoder: "
  606. "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
  607. "(frame size %dx%d). This is a bug, please report it\n",
  608. frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
  609. frame->width, frame->height);
  610. frame->crop_left = 0;
  611. frame->crop_right = 0;
  612. frame->crop_top = 0;
  613. frame->crop_bottom = 0;
  614. return 0;
  615. }
  616. if (!avctx->apply_cropping)
  617. return 0;
  618. return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
  619. AV_FRAME_CROP_UNALIGNED : 0);
  620. }
  621. // make sure frames returned to the caller are valid
  622. static int frame_validate(AVCodecContext *avctx, AVFrame *frame)
  623. {
  624. if (!frame->buf[0] || frame->format < 0)
  625. goto fail;
  626. switch (avctx->codec_type) {
  627. case AVMEDIA_TYPE_VIDEO:
  628. if (frame->width <= 0 || frame->height <= 0)
  629. goto fail;
  630. break;
  631. case AVMEDIA_TYPE_AUDIO:
  632. if (!av_channel_layout_check(&frame->ch_layout) ||
  633. frame->sample_rate <= 0)
  634. goto fail;
  635. break;
  636. default: av_assert0(0);
  637. }
  638. return 0;
  639. fail:
  640. av_log(avctx, AV_LOG_ERROR, "An invalid frame was output by a decoder. "
  641. "This is a bug, please report it.\n");
  642. return AVERROR_BUG;
  643. }
  644. int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  645. {
  646. AVCodecInternal *avci = avctx->internal;
  647. int ret;
  648. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  649. return AVERROR(EINVAL);
  650. if (avci->buffer_frame->buf[0]) {
  651. av_frame_move_ref(frame, avci->buffer_frame);
  652. } else {
  653. ret = decode_receive_frame_internal(avctx, frame);
  654. if (ret < 0)
  655. return ret;
  656. }
  657. ret = frame_validate(avctx, frame);
  658. if (ret < 0)
  659. goto fail;
  660. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  661. ret = apply_cropping(avctx, frame);
  662. if (ret < 0)
  663. goto fail;
  664. }
  665. avctx->frame_num++;
  666. #if FF_API_DROPCHANGED
  667. if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
  668. if (avctx->frame_num == 1) {
  669. avci->initial_format = frame->format;
  670. switch(avctx->codec_type) {
  671. case AVMEDIA_TYPE_VIDEO:
  672. avci->initial_width = frame->width;
  673. avci->initial_height = frame->height;
  674. break;
  675. case AVMEDIA_TYPE_AUDIO:
  676. avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
  677. avctx->sample_rate;
  678. ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout);
  679. if (ret < 0)
  680. goto fail;
  681. break;
  682. }
  683. }
  684. if (avctx->frame_num > 1) {
  685. int changed = avci->initial_format != frame->format;
  686. switch(avctx->codec_type) {
  687. case AVMEDIA_TYPE_VIDEO:
  688. changed |= avci->initial_width != frame->width ||
  689. avci->initial_height != frame->height;
  690. break;
  691. case AVMEDIA_TYPE_AUDIO:
  692. changed |= avci->initial_sample_rate != frame->sample_rate ||
  693. avci->initial_sample_rate != avctx->sample_rate ||
  694. av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout);
  695. break;
  696. }
  697. if (changed) {
  698. avci->changed_frames_dropped++;
  699. av_log(avctx, AV_LOG_INFO, "dropped changed frame #%"PRId64" pts %"PRId64
  700. " drop count: %d \n",
  701. avctx->frame_num, frame->pts,
  702. avci->changed_frames_dropped);
  703. ret = AVERROR_INPUT_CHANGED;
  704. goto fail;
  705. }
  706. }
  707. }
  708. #endif
  709. return 0;
  710. fail:
  711. av_frame_unref(frame);
  712. return ret;
  713. }
  714. static void get_subtitle_defaults(AVSubtitle *sub)
  715. {
  716. memset(sub, 0, sizeof(*sub));
  717. sub->pts = AV_NOPTS_VALUE;
  718. }
  719. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  720. static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt,
  721. const AVPacket *inpkt, AVPacket *buf_pkt)
  722. {
  723. #if CONFIG_ICONV
  724. iconv_t cd = (iconv_t)-1;
  725. int ret = 0;
  726. char *inb, *outb;
  727. size_t inl, outl;
  728. #endif
  729. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) {
  730. *outpkt = inpkt;
  731. return 0;
  732. }
  733. #if CONFIG_ICONV
  734. inb = inpkt->data;
  735. inl = inpkt->size;
  736. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  737. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  738. return AVERROR(ERANGE);
  739. }
  740. cd = iconv_open("UTF-8", avctx->sub_charenc);
  741. av_assert0(cd != (iconv_t)-1);
  742. ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES);
  743. if (ret < 0)
  744. goto end;
  745. ret = av_packet_copy_props(buf_pkt, inpkt);
  746. if (ret < 0)
  747. goto end;
  748. outb = buf_pkt->data;
  749. outl = buf_pkt->size;
  750. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  751. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  752. outl >= buf_pkt->size || inl != 0) {
  753. ret = FFMIN(AVERROR(errno), -1);
  754. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  755. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  756. goto end;
  757. }
  758. buf_pkt->size -= outl;
  759. memset(buf_pkt->data + buf_pkt->size, 0, outl);
  760. *outpkt = buf_pkt;
  761. ret = 0;
  762. end:
  763. if (ret < 0)
  764. av_packet_unref(buf_pkt);
  765. if (cd != (iconv_t)-1)
  766. iconv_close(cd);
  767. return ret;
  768. #else
  769. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  770. return AVERROR(EINVAL);
  771. #endif
  772. }
  773. static int utf8_check(const uint8_t *str)
  774. {
  775. const uint8_t *byte;
  776. uint32_t codepoint, min;
  777. while (*str) {
  778. byte = str;
  779. GET_UTF8(codepoint, *(byte++), return 0;);
  780. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  781. 1 << (5 * (byte - str) - 4);
  782. if (codepoint < min || codepoint >= 0x110000 ||
  783. codepoint == 0xFFFE /* BOM */ ||
  784. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  785. return 0;
  786. str = byte;
  787. }
  788. return 1;
  789. }
  790. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  791. int *got_sub_ptr, const AVPacket *avpkt)
  792. {
  793. int ret = 0;
  794. if (!avpkt->data && avpkt->size) {
  795. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  796. return AVERROR(EINVAL);
  797. }
  798. if (!avctx->codec)
  799. return AVERROR(EINVAL);
  800. if (ffcodec(avctx->codec)->cb_type != FF_CODEC_CB_TYPE_DECODE_SUB) {
  801. av_log(avctx, AV_LOG_ERROR, "Codec not subtitle decoder\n");
  802. return AVERROR(EINVAL);
  803. }
  804. *got_sub_ptr = 0;
  805. get_subtitle_defaults(sub);
  806. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  807. AVCodecInternal *avci = avctx->internal;
  808. const AVPacket *pkt;
  809. ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt);
  810. if (ret < 0)
  811. return ret;
  812. if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
  813. sub->pts = av_rescale_q(avpkt->pts,
  814. avctx->pkt_timebase, AV_TIME_BASE_Q);
  815. ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt);
  816. if (pkt == avci->buffer_pkt) // did we recode?
  817. av_packet_unref(avci->buffer_pkt);
  818. if (ret < 0) {
  819. *got_sub_ptr = 0;
  820. avsubtitle_free(sub);
  821. return ret;
  822. }
  823. av_assert1(!sub->num_rects || *got_sub_ptr);
  824. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  825. avctx->pkt_timebase.num) {
  826. AVRational ms = { 1, 1000 };
  827. sub->end_display_time = av_rescale_q(avpkt->duration,
  828. avctx->pkt_timebase, ms);
  829. }
  830. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  831. sub->format = 0;
  832. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  833. sub->format = 1;
  834. for (unsigned i = 0; i < sub->num_rects; i++) {
  835. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
  836. sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  837. av_log(avctx, AV_LOG_ERROR,
  838. "Invalid UTF-8 in decoded subtitles text; "
  839. "maybe missing -sub_charenc option\n");
  840. avsubtitle_free(sub);
  841. *got_sub_ptr = 0;
  842. return AVERROR_INVALIDDATA;
  843. }
  844. }
  845. if (*got_sub_ptr)
  846. avctx->frame_num++;
  847. }
  848. return ret;
  849. }
  850. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
  851. const enum AVPixelFormat *fmt)
  852. {
  853. const AVPixFmtDescriptor *desc;
  854. const AVCodecHWConfig *config;
  855. int i, n;
  856. // If a device was supplied when the codec was opened, assume that the
  857. // user wants to use it.
  858. if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) {
  859. AVHWDeviceContext *device_ctx =
  860. (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  861. for (i = 0;; i++) {
  862. config = &ffcodec(avctx->codec)->hw_configs[i]->public;
  863. if (!config)
  864. break;
  865. if (!(config->methods &
  866. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  867. continue;
  868. if (device_ctx->type != config->device_type)
  869. continue;
  870. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
  871. if (config->pix_fmt == fmt[n])
  872. return fmt[n];
  873. }
  874. }
  875. }
  876. // No device or other setup, so we have to choose from things which
  877. // don't any other external information.
  878. // If the last element of the list is a software format, choose it
  879. // (this should be best software format if any exist).
  880. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
  881. desc = av_pix_fmt_desc_get(fmt[n - 1]);
  882. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  883. return fmt[n - 1];
  884. // Finally, traverse the list in order and choose the first entry
  885. // with no external dependencies (if there is no hardware configuration
  886. // information available then this just picks the first entry).
  887. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
  888. for (i = 0;; i++) {
  889. config = avcodec_get_hw_config(avctx->codec, i);
  890. if (!config)
  891. break;
  892. if (config->pix_fmt == fmt[n])
  893. break;
  894. }
  895. if (!config) {
  896. // No specific config available, so the decoder must be able
  897. // to handle this format without any additional setup.
  898. return fmt[n];
  899. }
  900. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
  901. // Usable with only internal setup.
  902. return fmt[n];
  903. }
  904. }
  905. // Nothing is usable, give up.
  906. return AV_PIX_FMT_NONE;
  907. }
  908. int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
  909. enum AVHWDeviceType dev_type)
  910. {
  911. AVHWDeviceContext *device_ctx;
  912. AVHWFramesContext *frames_ctx;
  913. int ret;
  914. if (!avctx->hwaccel)
  915. return AVERROR(ENOSYS);
  916. if (avctx->hw_frames_ctx)
  917. return 0;
  918. if (!avctx->hw_device_ctx) {
  919. av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
  920. "required for hardware accelerated decoding.\n");
  921. return AVERROR(EINVAL);
  922. }
  923. device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
  924. if (device_ctx->type != dev_type) {
  925. av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
  926. "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
  927. av_hwdevice_get_type_name(device_ctx->type));
  928. return AVERROR(EINVAL);
  929. }
  930. ret = avcodec_get_hw_frames_parameters(avctx,
  931. avctx->hw_device_ctx,
  932. avctx->hwaccel->pix_fmt,
  933. &avctx->hw_frames_ctx);
  934. if (ret < 0)
  935. return ret;
  936. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  937. if (frames_ctx->initial_pool_size) {
  938. // We guarantee 4 base work surfaces. The function above guarantees 1
  939. // (the absolute minimum), so add the missing count.
  940. frames_ctx->initial_pool_size += 3;
  941. }
  942. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  943. if (ret < 0) {
  944. av_buffer_unref(&avctx->hw_frames_ctx);
  945. return ret;
  946. }
  947. return 0;
  948. }
  949. int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
  950. AVBufferRef *device_ref,
  951. enum AVPixelFormat hw_pix_fmt,
  952. AVBufferRef **out_frames_ref)
  953. {
  954. AVBufferRef *frames_ref = NULL;
  955. const AVCodecHWConfigInternal *hw_config;
  956. const FFHWAccel *hwa;
  957. int i, ret;
  958. for (i = 0;; i++) {
  959. hw_config = ffcodec(avctx->codec)->hw_configs[i];
  960. if (!hw_config)
  961. return AVERROR(ENOENT);
  962. if (hw_config->public.pix_fmt == hw_pix_fmt)
  963. break;
  964. }
  965. hwa = hw_config->hwaccel;
  966. if (!hwa || !hwa->frame_params)
  967. return AVERROR(ENOENT);
  968. frames_ref = av_hwframe_ctx_alloc(device_ref);
  969. if (!frames_ref)
  970. return AVERROR(ENOMEM);
  971. if (!avctx->internal->hwaccel_priv_data) {
  972. avctx->internal->hwaccel_priv_data =
  973. av_mallocz(hwa->priv_data_size);
  974. if (!avctx->internal->hwaccel_priv_data) {
  975. av_buffer_unref(&frames_ref);
  976. return AVERROR(ENOMEM);
  977. }
  978. }
  979. ret = hwa->frame_params(avctx, frames_ref);
  980. if (ret >= 0) {
  981. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
  982. if (frames_ctx->initial_pool_size) {
  983. // If the user has requested that extra output surfaces be
  984. // available then add them here.
  985. if (avctx->extra_hw_frames > 0)
  986. frames_ctx->initial_pool_size += avctx->extra_hw_frames;
  987. // If frame threading is enabled then an extra surface per thread
  988. // is also required.
  989. if (avctx->active_thread_type & FF_THREAD_FRAME)
  990. frames_ctx->initial_pool_size += avctx->thread_count;
  991. }
  992. *out_frames_ref = frames_ref;
  993. } else {
  994. av_buffer_unref(&frames_ref);
  995. }
  996. return ret;
  997. }
  998. static int hwaccel_init(AVCodecContext *avctx,
  999. const FFHWAccel *hwaccel)
  1000. {
  1001. int err;
  1002. if (hwaccel->p.capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  1003. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1004. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  1005. hwaccel->p.name);
  1006. return AVERROR_PATCHWELCOME;
  1007. }
  1008. if (!avctx->internal->hwaccel_priv_data && hwaccel->priv_data_size) {
  1009. avctx->internal->hwaccel_priv_data =
  1010. av_mallocz(hwaccel->priv_data_size);
  1011. if (!avctx->internal->hwaccel_priv_data)
  1012. return AVERROR(ENOMEM);
  1013. }
  1014. avctx->hwaccel = &hwaccel->p;
  1015. if (hwaccel->init) {
  1016. err = hwaccel->init(avctx);
  1017. if (err < 0) {
  1018. av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
  1019. "hwaccel initialisation returned error.\n",
  1020. av_get_pix_fmt_name(hwaccel->p.pix_fmt));
  1021. av_freep(&avctx->internal->hwaccel_priv_data);
  1022. avctx->hwaccel = NULL;
  1023. return err;
  1024. }
  1025. }
  1026. return 0;
  1027. }
  1028. void ff_hwaccel_uninit(AVCodecContext *avctx)
  1029. {
  1030. if (FF_HW_HAS_CB(avctx, uninit))
  1031. FF_HW_SIMPLE_CALL(avctx, uninit);
  1032. av_freep(&avctx->internal->hwaccel_priv_data);
  1033. avctx->hwaccel = NULL;
  1034. av_buffer_unref(&avctx->hw_frames_ctx);
  1035. }
  1036. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1037. {
  1038. const AVPixFmtDescriptor *desc;
  1039. enum AVPixelFormat *choices;
  1040. enum AVPixelFormat ret, user_choice;
  1041. const AVCodecHWConfigInternal *hw_config;
  1042. const AVCodecHWConfig *config;
  1043. int i, n, err;
  1044. // Find end of list.
  1045. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
  1046. // Must contain at least one entry.
  1047. av_assert0(n >= 1);
  1048. // If a software format is available, it must be the last entry.
  1049. desc = av_pix_fmt_desc_get(fmt[n - 1]);
  1050. if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
  1051. // No software format is available.
  1052. } else {
  1053. avctx->sw_pix_fmt = fmt[n - 1];
  1054. }
  1055. choices = av_memdup(fmt, (n + 1) * sizeof(*choices));
  1056. if (!choices)
  1057. return AV_PIX_FMT_NONE;
  1058. for (;;) {
  1059. // Remove the previous hwaccel, if there was one.
  1060. ff_hwaccel_uninit(avctx);
  1061. user_choice = avctx->get_format(avctx, choices);
  1062. if (user_choice == AV_PIX_FMT_NONE) {
  1063. // Explicitly chose nothing, give up.
  1064. ret = AV_PIX_FMT_NONE;
  1065. break;
  1066. }
  1067. desc = av_pix_fmt_desc_get(user_choice);
  1068. if (!desc) {
  1069. av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
  1070. "get_format() callback.\n");
  1071. ret = AV_PIX_FMT_NONE;
  1072. break;
  1073. }
  1074. av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
  1075. desc->name);
  1076. for (i = 0; i < n; i++) {
  1077. if (choices[i] == user_choice)
  1078. break;
  1079. }
  1080. if (i == n) {
  1081. av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
  1082. "%s not in possible list.\n", desc->name);
  1083. ret = AV_PIX_FMT_NONE;
  1084. break;
  1085. }
  1086. if (ffcodec(avctx->codec)->hw_configs) {
  1087. for (i = 0;; i++) {
  1088. hw_config = ffcodec(avctx->codec)->hw_configs[i];
  1089. if (!hw_config)
  1090. break;
  1091. if (hw_config->public.pix_fmt == user_choice)
  1092. break;
  1093. }
  1094. } else {
  1095. hw_config = NULL;
  1096. }
  1097. if (!hw_config) {
  1098. // No config available, so no extra setup required.
  1099. ret = user_choice;
  1100. break;
  1101. }
  1102. config = &hw_config->public;
  1103. if (config->methods &
  1104. AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
  1105. avctx->hw_frames_ctx) {
  1106. const AVHWFramesContext *frames_ctx =
  1107. (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  1108. if (frames_ctx->format != user_choice) {
  1109. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  1110. "does not match the format of the provided frames "
  1111. "context.\n", desc->name);
  1112. goto try_again;
  1113. }
  1114. } else if (config->methods &
  1115. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  1116. avctx->hw_device_ctx) {
  1117. const AVHWDeviceContext *device_ctx =
  1118. (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  1119. if (device_ctx->type != config->device_type) {
  1120. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  1121. "does not match the type of the provided device "
  1122. "context.\n", desc->name);
  1123. goto try_again;
  1124. }
  1125. } else if (config->methods &
  1126. AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
  1127. // Internal-only setup, no additional configuration.
  1128. } else if (config->methods &
  1129. AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
  1130. // Some ad-hoc configuration we can't see and can't check.
  1131. } else {
  1132. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  1133. "missing configuration.\n", desc->name);
  1134. goto try_again;
  1135. }
  1136. if (hw_config->hwaccel) {
  1137. av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
  1138. "initialisation.\n", desc->name);
  1139. err = hwaccel_init(avctx, hw_config->hwaccel);
  1140. if (err < 0)
  1141. goto try_again;
  1142. }
  1143. ret = user_choice;
  1144. break;
  1145. try_again:
  1146. av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
  1147. "get_format() without it.\n", desc->name);
  1148. for (i = 0; i < n; i++) {
  1149. if (choices[i] == user_choice)
  1150. break;
  1151. }
  1152. for (; i + 1 < n; i++)
  1153. choices[i] = choices[i + 1];
  1154. --n;
  1155. }
  1156. if (ret < 0)
  1157. ff_hwaccel_uninit(avctx);
  1158. av_freep(&choices);
  1159. return ret;
  1160. }
  1161. const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
  1162. enum AVPacketSideDataType type)
  1163. {
  1164. for (int i = 0; i < avctx->nb_coded_side_data; i++)
  1165. if (avctx->coded_side_data[i].type == type)
  1166. return &avctx->coded_side_data[i];
  1167. return NULL;
  1168. }
  1169. static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
  1170. {
  1171. size_t size;
  1172. const uint8_t *side_metadata;
  1173. AVDictionary **frame_md = &frame->metadata;
  1174. side_metadata = av_packet_get_side_data(avpkt,
  1175. AV_PKT_DATA_STRINGS_METADATA, &size);
  1176. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1177. }
  1178. static const struct {
  1179. enum AVPacketSideDataType packet;
  1180. enum AVFrameSideDataType frame;
  1181. } sd_global_map[] = {
  1182. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  1183. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  1184. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  1185. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  1186. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  1187. { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
  1188. { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
  1189. { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
  1190. { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT },
  1191. };
  1192. int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx,
  1193. AVFrame *frame, const AVPacket *pkt)
  1194. {
  1195. static const struct {
  1196. enum AVPacketSideDataType packet;
  1197. enum AVFrameSideDataType frame;
  1198. } sd[] = {
  1199. { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
  1200. { AV_PKT_DATA_AFD, AV_FRAME_DATA_AFD },
  1201. { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
  1202. { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
  1203. { AV_PKT_DATA_SKIP_SAMPLES, AV_FRAME_DATA_SKIP_SAMPLES },
  1204. };
  1205. frame->pts = pkt->pts;
  1206. frame->duration = pkt->duration;
  1207. #if FF_API_FRAME_PKT
  1208. FF_DISABLE_DEPRECATION_WARNINGS
  1209. frame->pkt_pos = pkt->pos;
  1210. frame->pkt_size = pkt->size;
  1211. FF_ENABLE_DEPRECATION_WARNINGS
  1212. #endif
  1213. for (int i = 0; i < FF_ARRAY_ELEMS(sd_global_map); i++) {
  1214. size_t size;
  1215. const uint8_t *packet_sd = av_packet_get_side_data(pkt, sd_global_map[i].packet, &size);
  1216. if (packet_sd) {
  1217. AVFrameSideData *frame_sd;
  1218. frame_sd = av_frame_new_side_data(frame, sd_global_map[i].frame, size);
  1219. if (!frame_sd)
  1220. return AVERROR(ENOMEM);
  1221. memcpy(frame_sd->data, packet_sd, size);
  1222. }
  1223. }
  1224. for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  1225. size_t size;
  1226. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  1227. if (packet_sd) {
  1228. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  1229. sd[i].frame,
  1230. size);
  1231. if (!frame_sd)
  1232. return AVERROR(ENOMEM);
  1233. memcpy(frame_sd->data, packet_sd, size);
  1234. }
  1235. }
  1236. add_metadata_from_side_data(pkt, frame);
  1237. if (pkt->flags & AV_PKT_FLAG_DISCARD) {
  1238. frame->flags |= AV_FRAME_FLAG_DISCARD;
  1239. } else {
  1240. frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
  1241. }
  1242. if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
  1243. int ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref);
  1244. if (ret < 0)
  1245. return ret;
  1246. frame->opaque = pkt->opaque;
  1247. }
  1248. return 0;
  1249. }
  1250. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  1251. {
  1252. int ret;
  1253. for (int i = 0; i < FF_ARRAY_ELEMS(sd_global_map); i++) {
  1254. const AVPacketSideData *packet_sd = ff_get_coded_side_data(avctx,
  1255. sd_global_map[i].packet);
  1256. if (packet_sd) {
  1257. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  1258. sd_global_map[i].frame,
  1259. packet_sd->size);
  1260. if (!frame_sd)
  1261. return AVERROR(ENOMEM);
  1262. memcpy(frame_sd->data, packet_sd->data, packet_sd->size);
  1263. }
  1264. }
  1265. if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) {
  1266. const AVPacket *pkt = avctx->internal->last_pkt_props;
  1267. ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt);
  1268. if (ret < 0)
  1269. return ret;
  1270. #if FF_API_FRAME_PKT
  1271. FF_DISABLE_DEPRECATION_WARNINGS
  1272. frame->pkt_size = pkt->stream_index;
  1273. FF_ENABLE_DEPRECATION_WARNINGS
  1274. #endif
  1275. }
  1276. #if FF_API_REORDERED_OPAQUE
  1277. FF_DISABLE_DEPRECATION_WARNINGS
  1278. frame->reordered_opaque = avctx->reordered_opaque;
  1279. FF_ENABLE_DEPRECATION_WARNINGS
  1280. #endif
  1281. ret = fill_frame_props(avctx, frame);
  1282. if (ret < 0)
  1283. return ret;
  1284. switch (avctx->codec->type) {
  1285. case AVMEDIA_TYPE_VIDEO:
  1286. if (frame->width && frame->height &&
  1287. av_image_check_sar(frame->width, frame->height,
  1288. frame->sample_aspect_ratio) < 0) {
  1289. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1290. frame->sample_aspect_ratio.num,
  1291. frame->sample_aspect_ratio.den);
  1292. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  1293. }
  1294. break;
  1295. }
  1296. return 0;
  1297. }
  1298. static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
  1299. {
  1300. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1301. int i;
  1302. int num_planes = av_pix_fmt_count_planes(frame->format);
  1303. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  1304. int flags = desc ? desc->flags : 0;
  1305. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
  1306. num_planes = 2;
  1307. for (i = 0; i < num_planes; i++) {
  1308. av_assert0(frame->data[i]);
  1309. }
  1310. // For formats without data like hwaccel allow unused pointers to be non-NULL.
  1311. for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
  1312. if (frame->data[i])
  1313. av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
  1314. frame->data[i] = NULL;
  1315. }
  1316. }
  1317. }
  1318. static void decode_data_free(void *opaque, uint8_t *data)
  1319. {
  1320. FrameDecodeData *fdd = (FrameDecodeData*)data;
  1321. if (fdd->post_process_opaque_free)
  1322. fdd->post_process_opaque_free(fdd->post_process_opaque);
  1323. if (fdd->hwaccel_priv_free)
  1324. fdd->hwaccel_priv_free(fdd->hwaccel_priv);
  1325. av_freep(&fdd);
  1326. }
  1327. int ff_attach_decode_data(AVFrame *frame)
  1328. {
  1329. AVBufferRef *fdd_buf;
  1330. FrameDecodeData *fdd;
  1331. av_assert1(!frame->private_ref);
  1332. av_buffer_unref(&frame->private_ref);
  1333. fdd = av_mallocz(sizeof(*fdd));
  1334. if (!fdd)
  1335. return AVERROR(ENOMEM);
  1336. fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
  1337. NULL, AV_BUFFER_FLAG_READONLY);
  1338. if (!fdd_buf) {
  1339. av_freep(&fdd);
  1340. return AVERROR(ENOMEM);
  1341. }
  1342. frame->private_ref = fdd_buf;
  1343. return 0;
  1344. }
  1345. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  1346. {
  1347. const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
  1348. int override_dimensions = 1;
  1349. int ret;
  1350. av_assert0(av_codec_is_decoder(avctx->codec));
  1351. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1352. if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
  1353. (ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  1354. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  1355. ret = AVERROR(EINVAL);
  1356. goto fail;
  1357. }
  1358. if (frame->width <= 0 || frame->height <= 0) {
  1359. frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  1360. frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  1361. override_dimensions = 0;
  1362. }
  1363. if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
  1364. av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
  1365. ret = AVERROR(EINVAL);
  1366. goto fail;
  1367. }
  1368. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1369. if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) {
  1370. av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
  1371. ret = AVERROR(EINVAL);
  1372. goto fail;
  1373. }
  1374. }
  1375. ret = ff_decode_frame_props(avctx, frame);
  1376. if (ret < 0)
  1377. goto fail;
  1378. if (hwaccel) {
  1379. if (hwaccel->alloc_frame) {
  1380. ret = hwaccel->alloc_frame(avctx, frame);
  1381. goto end;
  1382. }
  1383. } else
  1384. avctx->sw_pix_fmt = avctx->pix_fmt;
  1385. ret = avctx->get_buffer2(avctx, frame, flags);
  1386. if (ret < 0)
  1387. goto fail;
  1388. validate_avframe_allocation(avctx, frame);
  1389. ret = ff_attach_decode_data(frame);
  1390. if (ret < 0)
  1391. goto fail;
  1392. end:
  1393. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
  1394. !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
  1395. frame->width = avctx->width;
  1396. frame->height = avctx->height;
  1397. }
  1398. fail:
  1399. if (ret < 0) {
  1400. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1401. av_frame_unref(frame);
  1402. }
  1403. return ret;
  1404. }
  1405. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  1406. {
  1407. AVFrame *tmp;
  1408. int ret;
  1409. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  1410. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  1411. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  1412. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  1413. av_frame_unref(frame);
  1414. }
  1415. if (!frame->data[0])
  1416. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1417. if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
  1418. return ff_decode_frame_props(avctx, frame);
  1419. tmp = av_frame_alloc();
  1420. if (!tmp)
  1421. return AVERROR(ENOMEM);
  1422. av_frame_move_ref(tmp, frame);
  1423. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1424. if (ret < 0) {
  1425. av_frame_free(&tmp);
  1426. return ret;
  1427. }
  1428. av_frame_copy(frame, tmp);
  1429. av_frame_free(&tmp);
  1430. return 0;
  1431. }
  1432. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  1433. {
  1434. int ret = reget_buffer_internal(avctx, frame, flags);
  1435. if (ret < 0)
  1436. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  1437. return ret;
  1438. }
  1439. int ff_decode_preinit(AVCodecContext *avctx)
  1440. {
  1441. AVCodecInternal *avci = avctx->internal;
  1442. int ret = 0;
  1443. /* if the decoder init function was already called previously,
  1444. * free the already allocated subtitle_header before overwriting it */
  1445. av_freep(&avctx->subtitle_header);
  1446. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1447. av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
  1448. avctx->codec->max_lowres);
  1449. avctx->lowres = avctx->codec->max_lowres;
  1450. }
  1451. if (avctx->sub_charenc) {
  1452. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1453. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1454. "supported with subtitles codecs\n");
  1455. return AVERROR(EINVAL);
  1456. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1457. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1458. "subtitles character encoding will be ignored\n",
  1459. avctx->codec_descriptor->name);
  1460. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1461. } else {
  1462. /* input character encoding is set for a text based subtitle
  1463. * codec at this point */
  1464. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1465. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1466. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1467. #if CONFIG_ICONV
  1468. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1469. if (cd == (iconv_t)-1) {
  1470. ret = AVERROR(errno);
  1471. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1472. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1473. return ret;
  1474. }
  1475. iconv_close(cd);
  1476. #else
  1477. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1478. "conversion needs a libavcodec built with iconv support "
  1479. "for this codec\n");
  1480. return AVERROR(ENOSYS);
  1481. #endif
  1482. }
  1483. }
  1484. }
  1485. avctx->pts_correction_num_faulty_pts =
  1486. avctx->pts_correction_num_faulty_dts = 0;
  1487. avctx->pts_correction_last_pts =
  1488. avctx->pts_correction_last_dts = INT64_MIN;
  1489. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  1490. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  1491. av_log(avctx, AV_LOG_WARNING,
  1492. "gray decoding requested but not enabled at configuration time\n");
  1493. if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
  1494. avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
  1495. }
  1496. avci->in_pkt = av_packet_alloc();
  1497. avci->last_pkt_props = av_packet_alloc();
  1498. if (!avci->in_pkt || !avci->last_pkt_props)
  1499. return AVERROR(ENOMEM);
  1500. ret = decode_bsfs_init(avctx);
  1501. if (ret < 0)
  1502. return ret;
  1503. #if FF_API_DROPCHANGED
  1504. if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED)
  1505. av_log(avctx, AV_LOG_WARNING, "The dropchanged flag is deprecated.\n");
  1506. #endif
  1507. return 0;
  1508. }
  1509. int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
  1510. {
  1511. size_t size;
  1512. const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size);
  1513. if (pal && size == AVPALETTE_SIZE) {
  1514. memcpy(dst, pal, AVPALETTE_SIZE);
  1515. return 1;
  1516. } else if (pal) {
  1517. av_log(logctx, AV_LOG_ERROR,
  1518. "Palette size %"SIZE_SPECIFIER" is wrong\n", size);
  1519. }
  1520. return 0;
  1521. }
  1522. int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
  1523. {
  1524. const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
  1525. if (!hwaccel || !hwaccel->frame_priv_data_size)
  1526. return 0;
  1527. av_assert0(!*hwaccel_picture_private);
  1528. if (hwaccel->free_frame_priv) {
  1529. AVHWFramesContext *frames_ctx;
  1530. if (!avctx->hw_frames_ctx)
  1531. return AVERROR(EINVAL);
  1532. frames_ctx = (AVHWFramesContext *) avctx->hw_frames_ctx->data;
  1533. *hwaccel_picture_private = ff_refstruct_alloc_ext(hwaccel->frame_priv_data_size, 0,
  1534. frames_ctx->device_ctx,
  1535. hwaccel->free_frame_priv);
  1536. } else {
  1537. *hwaccel_picture_private = ff_refstruct_allocz(hwaccel->frame_priv_data_size);
  1538. }
  1539. if (!*hwaccel_picture_private)
  1540. return AVERROR(ENOMEM);
  1541. return 0;
  1542. }
  1543. void ff_decode_flush_buffers(AVCodecContext *avctx)
  1544. {
  1545. AVCodecInternal *avci = avctx->internal;
  1546. DecodeContext *dc = decode_ctx(avci);
  1547. av_packet_unref(avci->last_pkt_props);
  1548. av_packet_unref(avci->in_pkt);
  1549. avctx->pts_correction_last_pts =
  1550. avctx->pts_correction_last_dts = INT64_MIN;
  1551. av_bsf_flush(avci->bsf);
  1552. dc->nb_draining_errors = 0;
  1553. dc->draining_started = 0;
  1554. }
  1555. AVCodecInternal *ff_decode_internal_alloc(void)
  1556. {
  1557. return av_mallocz(sizeof(DecodeContext));
  1558. }