decode.c 62 KB

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