decode.c 57 KB

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