You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1837 lines
59KB

  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. AVPacket tmp;
  324. int got_frame, actual_got_frame, did_split;
  325. int ret;
  326. if (!pkt->data && !avci->draining) {
  327. av_packet_unref(pkt);
  328. ret = ff_decode_get_packet(avctx, pkt);
  329. if (ret < 0 && ret != AVERROR_EOF)
  330. return ret;
  331. }
  332. // Some codecs (at least wma lossless) will crash when feeding drain packets
  333. // after EOF was signaled.
  334. if (avci->draining_done)
  335. return AVERROR_EOF;
  336. if (!pkt->data &&
  337. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
  338. avctx->active_thread_type & FF_THREAD_FRAME))
  339. return AVERROR_EOF;
  340. tmp = *pkt;
  341. #if FF_API_MERGE_SD
  342. FF_DISABLE_DEPRECATION_WARNINGS
  343. did_split = avci->compat_decode_partial_size ?
  344. ff_packet_split_and_drop_side_data(&tmp) :
  345. av_packet_split_side_data(&tmp);
  346. if (did_split) {
  347. ret = extract_packet_props(avctx->internal, &tmp);
  348. if (ret < 0)
  349. return ret;
  350. ret = apply_param_change(avctx, &tmp);
  351. if (ret < 0)
  352. return ret;
  353. }
  354. FF_ENABLE_DEPRECATION_WARNINGS
  355. #endif
  356. got_frame = 0;
  357. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
  358. ret = ff_thread_decode_frame(avctx, frame, &got_frame, &tmp);
  359. } else {
  360. ret = avctx->codec->decode(avctx, frame, &got_frame, &tmp);
  361. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  362. frame->pkt_dts = pkt->dts;
  363. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  364. if(!avctx->has_b_frames)
  365. frame->pkt_pos = pkt->pos;
  366. //FIXME these should be under if(!avctx->has_b_frames)
  367. /* get_buffer is supposed to set frame parameters */
  368. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  369. if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  370. if (!frame->width) frame->width = avctx->width;
  371. if (!frame->height) frame->height = avctx->height;
  372. if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
  373. }
  374. }
  375. }
  376. emms_c();
  377. actual_got_frame = got_frame;
  378. if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
  379. if (frame->flags & AV_FRAME_FLAG_DISCARD)
  380. got_frame = 0;
  381. if (got_frame)
  382. frame->best_effort_timestamp = guess_correct_pts(avctx,
  383. frame->pts,
  384. frame->pkt_dts);
  385. } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
  386. uint8_t *side;
  387. int side_size;
  388. uint32_t discard_padding = 0;
  389. uint8_t skip_reason = 0;
  390. uint8_t discard_reason = 0;
  391. if (ret >= 0 && got_frame) {
  392. frame->best_effort_timestamp = guess_correct_pts(avctx,
  393. frame->pts,
  394. frame->pkt_dts);
  395. if (frame->format == AV_SAMPLE_FMT_NONE)
  396. frame->format = avctx->sample_fmt;
  397. if (!frame->channel_layout)
  398. frame->channel_layout = avctx->channel_layout;
  399. if (!frame->channels)
  400. frame->channels = avctx->channels;
  401. if (!frame->sample_rate)
  402. frame->sample_rate = avctx->sample_rate;
  403. }
  404. side= av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  405. if(side && side_size>=10) {
  406. avctx->internal->skip_samples = AV_RL32(side) * avctx->internal->skip_samples_multiplier;
  407. discard_padding = AV_RL32(side + 4);
  408. av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
  409. avctx->internal->skip_samples, (int)discard_padding);
  410. skip_reason = AV_RL8(side + 8);
  411. discard_reason = AV_RL8(side + 9);
  412. }
  413. if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame &&
  414. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  415. avctx->internal->skip_samples = FFMAX(0, avctx->internal->skip_samples - frame->nb_samples);
  416. got_frame = 0;
  417. }
  418. if (avctx->internal->skip_samples > 0 && got_frame &&
  419. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  420. if(frame->nb_samples <= avctx->internal->skip_samples){
  421. got_frame = 0;
  422. avctx->internal->skip_samples -= frame->nb_samples;
  423. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  424. avctx->internal->skip_samples);
  425. } else {
  426. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  427. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  428. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  429. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  430. (AVRational){1, avctx->sample_rate},
  431. avctx->pkt_timebase);
  432. if(frame->pts!=AV_NOPTS_VALUE)
  433. frame->pts += diff_ts;
  434. #if FF_API_PKT_PTS
  435. FF_DISABLE_DEPRECATION_WARNINGS
  436. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  437. frame->pkt_pts += diff_ts;
  438. FF_ENABLE_DEPRECATION_WARNINGS
  439. #endif
  440. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  441. frame->pkt_dts += diff_ts;
  442. if (frame->pkt_duration >= diff_ts)
  443. frame->pkt_duration -= diff_ts;
  444. } else {
  445. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  446. }
  447. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  448. avctx->internal->skip_samples, frame->nb_samples);
  449. frame->nb_samples -= avctx->internal->skip_samples;
  450. avctx->internal->skip_samples = 0;
  451. }
  452. }
  453. if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame &&
  454. !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
  455. if (discard_padding == frame->nb_samples) {
  456. got_frame = 0;
  457. } else {
  458. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  459. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  460. (AVRational){1, avctx->sample_rate},
  461. avctx->pkt_timebase);
  462. frame->pkt_duration = diff_ts;
  463. } else {
  464. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  465. }
  466. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  467. (int)discard_padding, frame->nb_samples);
  468. frame->nb_samples -= discard_padding;
  469. }
  470. }
  471. if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) {
  472. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  473. if (fside) {
  474. AV_WL32(fside->data, avctx->internal->skip_samples);
  475. AV_WL32(fside->data + 4, discard_padding);
  476. AV_WL8(fside->data + 8, skip_reason);
  477. AV_WL8(fside->data + 9, discard_reason);
  478. avctx->internal->skip_samples = 0;
  479. }
  480. }
  481. }
  482. #if FF_API_MERGE_SD
  483. if (did_split) {
  484. av_packet_free_side_data(&tmp);
  485. if(ret == tmp.size)
  486. ret = pkt->size;
  487. }
  488. #endif
  489. if (avctx->codec->type == AVMEDIA_TYPE_AUDIO &&
  490. !avci->showed_multi_packet_warning &&
  491. ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
  492. av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
  493. avci->showed_multi_packet_warning = 1;
  494. }
  495. if (!got_frame)
  496. av_frame_unref(frame);
  497. if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED))
  498. ret = pkt->size;
  499. #if FF_API_AVCTX_TIMEBASE
  500. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  501. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  502. #endif
  503. /* do not stop draining when actual_got_frame != 0 or ret < 0 */
  504. /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
  505. if (avctx->internal->draining && !actual_got_frame) {
  506. if (ret < 0) {
  507. /* prevent infinite loop if a decoder wrongly always return error on draining */
  508. /* reasonable nb_errors_max = maximum b frames + thread count */
  509. int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ?
  510. avctx->thread_count : 1);
  511. if (avci->nb_draining_errors++ >= nb_errors_max) {
  512. av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
  513. "Stop draining and force EOF.\n");
  514. avci->draining_done = 1;
  515. ret = AVERROR_BUG;
  516. }
  517. } else {
  518. avci->draining_done = 1;
  519. }
  520. }
  521. avci->compat_decode_consumed += ret;
  522. if (ret >= pkt->size || ret < 0) {
  523. av_packet_unref(pkt);
  524. } else {
  525. int consumed = ret;
  526. pkt->data += consumed;
  527. pkt->size -= consumed;
  528. avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
  529. pkt->pts = AV_NOPTS_VALUE;
  530. pkt->dts = AV_NOPTS_VALUE;
  531. avci->last_pkt_props->pts = AV_NOPTS_VALUE;
  532. avci->last_pkt_props->dts = AV_NOPTS_VALUE;
  533. }
  534. if (got_frame)
  535. av_assert0(frame->buf[0]);
  536. return ret < 0 ? ret : 0;
  537. }
  538. static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  539. {
  540. int ret;
  541. while (!frame->buf[0]) {
  542. ret = decode_simple_internal(avctx, frame);
  543. if (ret < 0)
  544. return ret;
  545. }
  546. return 0;
  547. }
  548. static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
  549. {
  550. AVCodecInternal *avci = avctx->internal;
  551. int ret;
  552. av_assert0(!frame->buf[0]);
  553. if (avctx->codec->receive_frame)
  554. ret = avctx->codec->receive_frame(avctx, frame);
  555. else
  556. ret = decode_simple_receive_frame(avctx, frame);
  557. if (ret == AVERROR_EOF)
  558. avci->draining_done = 1;
  559. return ret;
  560. }
  561. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  562. {
  563. AVCodecInternal *avci = avctx->internal;
  564. int ret;
  565. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  566. return AVERROR(EINVAL);
  567. if (avctx->internal->draining)
  568. return AVERROR_EOF;
  569. if (avpkt && !avpkt->size && avpkt->data)
  570. return AVERROR(EINVAL);
  571. ret = bsfs_init(avctx);
  572. if (ret < 0)
  573. return ret;
  574. av_packet_unref(avci->buffer_pkt);
  575. if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
  576. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  577. if (ret < 0)
  578. return ret;
  579. }
  580. ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
  581. if (ret < 0) {
  582. av_packet_unref(avci->buffer_pkt);
  583. return ret;
  584. }
  585. if (!avci->buffer_frame->buf[0]) {
  586. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  587. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  588. return ret;
  589. }
  590. return 0;
  591. }
  592. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  593. const AVPixFmtDescriptor *desc)
  594. {
  595. int i, j;
  596. for (i = 0; frame->data[i]; i++) {
  597. const AVComponentDescriptor *comp = NULL;
  598. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  599. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  600. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
  601. offsets[i] = 0;
  602. break;
  603. }
  604. /* find any component descriptor for this plane */
  605. for (j = 0; j < desc->nb_components; j++) {
  606. if (desc->comp[j].plane == i) {
  607. comp = &desc->comp[j];
  608. break;
  609. }
  610. }
  611. if (!comp)
  612. return AVERROR_BUG;
  613. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  614. (frame->crop_left >> shift_x) * comp->step;
  615. }
  616. return 0;
  617. }
  618. static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
  619. {
  620. const AVPixFmtDescriptor *desc;
  621. size_t offsets[4];
  622. int i;
  623. /* make sure we are noisy about decoders returning invalid cropping data */
  624. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  625. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  626. (frame->crop_left + frame->crop_right) >= frame->width ||
  627. (frame->crop_top + frame->crop_bottom) >= frame->height) {
  628. av_log(avctx, AV_LOG_WARNING,
  629. "Invalid cropping information set by a decoder: "
  630. "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
  631. "(frame size %dx%d). This is a bug, please report it\n",
  632. frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
  633. frame->width, frame->height);
  634. frame->crop_left = 0;
  635. frame->crop_right = 0;
  636. frame->crop_top = 0;
  637. frame->crop_bottom = 0;
  638. return 0;
  639. }
  640. if (!avctx->apply_cropping)
  641. return 0;
  642. desc = av_pix_fmt_desc_get(frame->format);
  643. if (!desc)
  644. return AVERROR_BUG;
  645. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  646. * formats cannot be easily handled here either (and corresponding decoders
  647. * should not export any cropping anyway), so do the same for those as well.
  648. * */
  649. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  650. frame->width -= frame->crop_right;
  651. frame->height -= frame->crop_bottom;
  652. frame->crop_right = 0;
  653. frame->crop_bottom = 0;
  654. return 0;
  655. }
  656. /* calculate the offsets for each plane */
  657. calc_cropping_offsets(offsets, frame, desc);
  658. /* adjust the offsets to avoid breaking alignment */
  659. if (!(avctx->flags & AV_CODEC_FLAG_UNALIGNED)) {
  660. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  661. int min_log2_align = INT_MAX;
  662. for (i = 0; frame->data[i]; i++) {
  663. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  664. min_log2_align = FFMIN(log2_align, min_log2_align);
  665. }
  666. /* we assume, and it should always be true, that the data alignment is
  667. * related to the cropping alignment by a constant power-of-2 factor */
  668. if (log2_crop_align < min_log2_align)
  669. return AVERROR_BUG;
  670. if (min_log2_align < 5) {
  671. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  672. calc_cropping_offsets(offsets, frame, desc);
  673. }
  674. }
  675. for (i = 0; frame->data[i]; i++)
  676. frame->data[i] += offsets[i];
  677. frame->width -= (frame->crop_left + frame->crop_right);
  678. frame->height -= (frame->crop_top + frame->crop_bottom);
  679. frame->crop_left = 0;
  680. frame->crop_right = 0;
  681. frame->crop_top = 0;
  682. frame->crop_bottom = 0;
  683. return 0;
  684. }
  685. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  686. {
  687. AVCodecInternal *avci = avctx->internal;
  688. int ret;
  689. av_frame_unref(frame);
  690. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  691. return AVERROR(EINVAL);
  692. ret = bsfs_init(avctx);
  693. if (ret < 0)
  694. return ret;
  695. if (avci->buffer_frame->buf[0]) {
  696. av_frame_move_ref(frame, avci->buffer_frame);
  697. } else {
  698. ret = decode_receive_frame_internal(avctx, frame);
  699. if (ret < 0)
  700. return ret;
  701. }
  702. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  703. ret = apply_cropping(avctx, frame);
  704. if (ret < 0) {
  705. av_frame_unref(frame);
  706. return ret;
  707. }
  708. }
  709. avctx->frame_number++;
  710. return 0;
  711. }
  712. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  713. int *got_frame, const AVPacket *pkt)
  714. {
  715. AVCodecInternal *avci = avctx->internal;
  716. int ret = 0;
  717. av_assert0(avci->compat_decode_consumed == 0);
  718. *got_frame = 0;
  719. avci->compat_decode = 1;
  720. if (avci->compat_decode_partial_size > 0 &&
  721. avci->compat_decode_partial_size != pkt->size) {
  722. av_log(avctx, AV_LOG_ERROR,
  723. "Got unexpected packet size after a partial decode\n");
  724. ret = AVERROR(EINVAL);
  725. goto finish;
  726. }
  727. if (!avci->compat_decode_partial_size) {
  728. ret = avcodec_send_packet(avctx, pkt);
  729. if (ret == AVERROR_EOF)
  730. ret = 0;
  731. else if (ret == AVERROR(EAGAIN)) {
  732. /* we fully drain all the output in each decode call, so this should not
  733. * ever happen */
  734. ret = AVERROR_BUG;
  735. goto finish;
  736. } else if (ret < 0)
  737. goto finish;
  738. }
  739. while (ret >= 0) {
  740. ret = avcodec_receive_frame(avctx, frame);
  741. if (ret < 0) {
  742. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  743. ret = 0;
  744. goto finish;
  745. }
  746. if (frame != avci->compat_decode_frame) {
  747. if (!avctx->refcounted_frames) {
  748. ret = unrefcount_frame(avci, frame);
  749. if (ret < 0)
  750. goto finish;
  751. }
  752. *got_frame = 1;
  753. frame = avci->compat_decode_frame;
  754. } else {
  755. if (!avci->compat_decode_warned) {
  756. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  757. "API cannot return all the frames for this decoder. "
  758. "Some frames will be dropped. Update your code to the "
  759. "new decoding API to fix this.\n");
  760. avci->compat_decode_warned = 1;
  761. }
  762. }
  763. if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
  764. break;
  765. }
  766. finish:
  767. if (ret == 0) {
  768. /* if there are any bsfs then assume full packet is always consumed */
  769. if (avctx->codec->bsfs)
  770. ret = pkt->size;
  771. else
  772. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  773. }
  774. avci->compat_decode_consumed = 0;
  775. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  776. return ret;
  777. }
  778. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  779. int *got_picture_ptr,
  780. const AVPacket *avpkt)
  781. {
  782. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  783. }
  784. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  785. AVFrame *frame,
  786. int *got_frame_ptr,
  787. const AVPacket *avpkt)
  788. {
  789. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  790. }
  791. static void get_subtitle_defaults(AVSubtitle *sub)
  792. {
  793. memset(sub, 0, sizeof(*sub));
  794. sub->pts = AV_NOPTS_VALUE;
  795. }
  796. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  797. static int recode_subtitle(AVCodecContext *avctx,
  798. AVPacket *outpkt, const AVPacket *inpkt)
  799. {
  800. #if CONFIG_ICONV
  801. iconv_t cd = (iconv_t)-1;
  802. int ret = 0;
  803. char *inb, *outb;
  804. size_t inl, outl;
  805. AVPacket tmp;
  806. #endif
  807. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  808. return 0;
  809. #if CONFIG_ICONV
  810. cd = iconv_open("UTF-8", avctx->sub_charenc);
  811. av_assert0(cd != (iconv_t)-1);
  812. inb = inpkt->data;
  813. inl = inpkt->size;
  814. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  815. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  816. ret = AVERROR(ENOMEM);
  817. goto end;
  818. }
  819. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  820. if (ret < 0)
  821. goto end;
  822. outpkt->buf = tmp.buf;
  823. outpkt->data = tmp.data;
  824. outpkt->size = tmp.size;
  825. outb = outpkt->data;
  826. outl = outpkt->size;
  827. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  828. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  829. outl >= outpkt->size || inl != 0) {
  830. ret = FFMIN(AVERROR(errno), -1);
  831. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  832. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  833. av_packet_unref(&tmp);
  834. goto end;
  835. }
  836. outpkt->size -= outl;
  837. memset(outpkt->data + outpkt->size, 0, outl);
  838. end:
  839. if (cd != (iconv_t)-1)
  840. iconv_close(cd);
  841. return ret;
  842. #else
  843. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  844. return AVERROR(EINVAL);
  845. #endif
  846. }
  847. static int utf8_check(const uint8_t *str)
  848. {
  849. const uint8_t *byte;
  850. uint32_t codepoint, min;
  851. while (*str) {
  852. byte = str;
  853. GET_UTF8(codepoint, *(byte++), return 0;);
  854. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  855. 1 << (5 * (byte - str) - 4);
  856. if (codepoint < min || codepoint >= 0x110000 ||
  857. codepoint == 0xFFFE /* BOM */ ||
  858. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  859. return 0;
  860. str = byte;
  861. }
  862. return 1;
  863. }
  864. #if FF_API_ASS_TIMING
  865. static void insert_ts(AVBPrint *buf, int ts)
  866. {
  867. if (ts == -1) {
  868. av_bprintf(buf, "9:59:59.99,");
  869. } else {
  870. int h, m, s;
  871. h = ts/360000; ts -= 360000*h;
  872. m = ts/ 6000; ts -= 6000*m;
  873. s = ts/ 100; ts -= 100*s;
  874. av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
  875. }
  876. }
  877. static int convert_sub_to_old_ass_form(AVSubtitle *sub, const AVPacket *pkt, AVRational tb)
  878. {
  879. int i;
  880. AVBPrint buf;
  881. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  882. for (i = 0; i < sub->num_rects; i++) {
  883. char *final_dialog;
  884. const char *dialog;
  885. AVSubtitleRect *rect = sub->rects[i];
  886. int ts_start, ts_duration = -1;
  887. long int layer;
  888. if (rect->type != SUBTITLE_ASS || !strncmp(rect->ass, "Dialogue: ", 10))
  889. continue;
  890. av_bprint_clear(&buf);
  891. /* skip ReadOrder */
  892. dialog = strchr(rect->ass, ',');
  893. if (!dialog)
  894. continue;
  895. dialog++;
  896. /* extract Layer or Marked */
  897. layer = strtol(dialog, (char**)&dialog, 10);
  898. if (*dialog != ',')
  899. continue;
  900. dialog++;
  901. /* rescale timing to ASS time base (ms) */
  902. ts_start = av_rescale_q(pkt->pts, tb, av_make_q(1, 100));
  903. if (pkt->duration != -1)
  904. ts_duration = av_rescale_q(pkt->duration, tb, av_make_q(1, 100));
  905. sub->end_display_time = FFMAX(sub->end_display_time, 10 * ts_duration);
  906. /* construct ASS (standalone file form with timestamps) string */
  907. av_bprintf(&buf, "Dialogue: %ld,", layer);
  908. insert_ts(&buf, ts_start);
  909. insert_ts(&buf, ts_duration == -1 ? -1 : ts_start + ts_duration);
  910. av_bprintf(&buf, "%s\r\n", dialog);
  911. final_dialog = av_strdup(buf.str);
  912. if (!av_bprint_is_complete(&buf) || !final_dialog) {
  913. av_freep(&final_dialog);
  914. av_bprint_finalize(&buf, NULL);
  915. return AVERROR(ENOMEM);
  916. }
  917. av_freep(&rect->ass);
  918. rect->ass = final_dialog;
  919. }
  920. av_bprint_finalize(&buf, NULL);
  921. return 0;
  922. }
  923. #endif
  924. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  925. int *got_sub_ptr,
  926. AVPacket *avpkt)
  927. {
  928. int i, ret = 0;
  929. AVCodecInternal *avci = avctx->internal;
  930. if (!avpkt->data && avpkt->size) {
  931. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  932. return AVERROR(EINVAL);
  933. }
  934. if (!avctx->codec)
  935. return AVERROR(EINVAL);
  936. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  937. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  938. return AVERROR(EINVAL);
  939. }
  940. *got_sub_ptr = 0;
  941. get_subtitle_defaults(sub);
  942. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  943. AVPacket pkt_recoded;
  944. AVPacket tmp = *avpkt;
  945. #if FF_API_MERGE_SD
  946. FF_DISABLE_DEPRECATION_WARNINGS
  947. int did_split = avci->compat_decode_partial_size ?
  948. ff_packet_split_and_drop_side_data(&tmp) :
  949. av_packet_split_side_data(&tmp);
  950. //apply_param_change(avctx, &tmp);
  951. if (did_split) {
  952. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  953. * proper padding.
  954. * If the side data is smaller than the buffer padding size, the
  955. * remaining bytes should have already been filled with zeros by the
  956. * original packet allocation anyway. */
  957. memset(tmp.data + tmp.size, 0,
  958. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  959. }
  960. FF_ENABLE_DEPRECATION_WARNINGS
  961. #endif
  962. pkt_recoded = tmp;
  963. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  964. if (ret < 0) {
  965. *got_sub_ptr = 0;
  966. } else {
  967. ret = extract_packet_props(avctx->internal, &pkt_recoded);
  968. if (ret < 0)
  969. return ret;
  970. if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
  971. sub->pts = av_rescale_q(avpkt->pts,
  972. avctx->pkt_timebase, AV_TIME_BASE_Q);
  973. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  974. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  975. !!*got_sub_ptr >= !!sub->num_rects);
  976. #if FF_API_ASS_TIMING
  977. if (avctx->sub_text_format == FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS
  978. && *got_sub_ptr && sub->num_rects) {
  979. const AVRational tb = avctx->pkt_timebase.num ? avctx->pkt_timebase
  980. : avctx->time_base;
  981. int err = convert_sub_to_old_ass_form(sub, avpkt, tb);
  982. if (err < 0)
  983. ret = err;
  984. }
  985. #endif
  986. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  987. avctx->pkt_timebase.num) {
  988. AVRational ms = { 1, 1000 };
  989. sub->end_display_time = av_rescale_q(avpkt->duration,
  990. avctx->pkt_timebase, ms);
  991. }
  992. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  993. sub->format = 0;
  994. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  995. sub->format = 1;
  996. for (i = 0; i < sub->num_rects; i++) {
  997. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  998. av_log(avctx, AV_LOG_ERROR,
  999. "Invalid UTF-8 in decoded subtitles text; "
  1000. "maybe missing -sub_charenc option\n");
  1001. avsubtitle_free(sub);
  1002. ret = AVERROR_INVALIDDATA;
  1003. break;
  1004. }
  1005. }
  1006. if (tmp.data != pkt_recoded.data) { // did we recode?
  1007. /* prevent from destroying side data from original packet */
  1008. pkt_recoded.side_data = NULL;
  1009. pkt_recoded.side_data_elems = 0;
  1010. av_packet_unref(&pkt_recoded);
  1011. }
  1012. }
  1013. #if FF_API_MERGE_SD
  1014. if (did_split) {
  1015. av_packet_free_side_data(&tmp);
  1016. if(ret == tmp.size)
  1017. ret = avpkt->size;
  1018. }
  1019. #endif
  1020. if (*got_sub_ptr)
  1021. avctx->frame_number++;
  1022. }
  1023. return ret;
  1024. }
  1025. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  1026. {
  1027. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  1028. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  1029. }
  1030. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  1031. {
  1032. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  1033. ++fmt;
  1034. return fmt[0];
  1035. }
  1036. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  1037. enum AVPixelFormat pix_fmt)
  1038. {
  1039. AVHWAccel *hwaccel = NULL;
  1040. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1041. if (hwaccel->id == codec_id
  1042. && hwaccel->pix_fmt == pix_fmt)
  1043. return hwaccel;
  1044. return NULL;
  1045. }
  1046. static int setup_hwaccel(AVCodecContext *avctx,
  1047. const enum AVPixelFormat fmt,
  1048. const char *name)
  1049. {
  1050. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  1051. int ret = 0;
  1052. if (!hwa) {
  1053. av_log(avctx, AV_LOG_ERROR,
  1054. "Could not find an AVHWAccel for the pixel format: %s",
  1055. name);
  1056. return AVERROR(ENOENT);
  1057. }
  1058. if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  1059. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1060. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  1061. hwa->name);
  1062. return AVERROR_PATCHWELCOME;
  1063. }
  1064. if (hwa->priv_data_size) {
  1065. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  1066. if (!avctx->internal->hwaccel_priv_data)
  1067. return AVERROR(ENOMEM);
  1068. }
  1069. if (hwa->init) {
  1070. ret = hwa->init(avctx);
  1071. if (ret < 0) {
  1072. av_freep(&avctx->internal->hwaccel_priv_data);
  1073. return ret;
  1074. }
  1075. }
  1076. avctx->hwaccel = hwa;
  1077. return 0;
  1078. }
  1079. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1080. {
  1081. const AVPixFmtDescriptor *desc;
  1082. enum AVPixelFormat *choices;
  1083. enum AVPixelFormat ret;
  1084. unsigned n = 0;
  1085. while (fmt[n] != AV_PIX_FMT_NONE)
  1086. ++n;
  1087. av_assert0(n >= 1);
  1088. avctx->sw_pix_fmt = fmt[n - 1];
  1089. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  1090. choices = av_malloc_array(n + 1, sizeof(*choices));
  1091. if (!choices)
  1092. return AV_PIX_FMT_NONE;
  1093. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  1094. for (;;) {
  1095. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1096. avctx->hwaccel->uninit(avctx);
  1097. av_freep(&avctx->internal->hwaccel_priv_data);
  1098. avctx->hwaccel = NULL;
  1099. av_buffer_unref(&avctx->hw_frames_ctx);
  1100. ret = avctx->get_format(avctx, choices);
  1101. desc = av_pix_fmt_desc_get(ret);
  1102. if (!desc) {
  1103. ret = AV_PIX_FMT_NONE;
  1104. break;
  1105. }
  1106. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1107. break;
  1108. #if FF_API_CAP_VDPAU
  1109. if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
  1110. break;
  1111. #endif
  1112. if (avctx->hw_frames_ctx) {
  1113. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  1114. if (hw_frames_ctx->format != ret) {
  1115. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  1116. "does not match the format of provided AVHWFramesContext\n");
  1117. ret = AV_PIX_FMT_NONE;
  1118. break;
  1119. }
  1120. }
  1121. if (!setup_hwaccel(avctx, ret, desc->name))
  1122. break;
  1123. /* Remove failed hwaccel from choices */
  1124. for (n = 0; choices[n] != ret; n++)
  1125. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  1126. do
  1127. choices[n] = choices[n + 1];
  1128. while (choices[n++] != AV_PIX_FMT_NONE);
  1129. }
  1130. av_freep(&choices);
  1131. return ret;
  1132. }
  1133. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  1134. {
  1135. FramePool *pool = avctx->internal->pool;
  1136. int i, ret;
  1137. switch (avctx->codec_type) {
  1138. case AVMEDIA_TYPE_VIDEO: {
  1139. uint8_t *data[4];
  1140. int linesize[4];
  1141. int size[4] = { 0 };
  1142. int w = frame->width;
  1143. int h = frame->height;
  1144. int tmpsize, unaligned;
  1145. if (pool->format == frame->format &&
  1146. pool->width == frame->width && pool->height == frame->height)
  1147. return 0;
  1148. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  1149. do {
  1150. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  1151. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  1152. ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  1153. if (ret < 0)
  1154. return ret;
  1155. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  1156. w += w & ~(w - 1);
  1157. unaligned = 0;
  1158. for (i = 0; i < 4; i++)
  1159. unaligned |= linesize[i] % pool->stride_align[i];
  1160. } while (unaligned);
  1161. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  1162. NULL, linesize);
  1163. if (tmpsize < 0)
  1164. return -1;
  1165. for (i = 0; i < 3 && data[i + 1]; i++)
  1166. size[i] = data[i + 1] - data[i];
  1167. size[i] = tmpsize - (data[i] - data[0]);
  1168. for (i = 0; i < 4; i++) {
  1169. av_buffer_pool_uninit(&pool->pools[i]);
  1170. pool->linesize[i] = linesize[i];
  1171. if (size[i]) {
  1172. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  1173. CONFIG_MEMORY_POISONING ?
  1174. NULL :
  1175. av_buffer_allocz);
  1176. if (!pool->pools[i]) {
  1177. ret = AVERROR(ENOMEM);
  1178. goto fail;
  1179. }
  1180. }
  1181. }
  1182. pool->format = frame->format;
  1183. pool->width = frame->width;
  1184. pool->height = frame->height;
  1185. break;
  1186. }
  1187. case AVMEDIA_TYPE_AUDIO: {
  1188. int ch = frame->channels; //av_get_channel_layout_nb_channels(frame->channel_layout);
  1189. int planar = av_sample_fmt_is_planar(frame->format);
  1190. int planes = planar ? ch : 1;
  1191. if (pool->format == frame->format && pool->planes == planes &&
  1192. pool->channels == ch && frame->nb_samples == pool->samples)
  1193. return 0;
  1194. av_buffer_pool_uninit(&pool->pools[0]);
  1195. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  1196. frame->nb_samples, frame->format, 0);
  1197. if (ret < 0)
  1198. goto fail;
  1199. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  1200. if (!pool->pools[0]) {
  1201. ret = AVERROR(ENOMEM);
  1202. goto fail;
  1203. }
  1204. pool->format = frame->format;
  1205. pool->planes = planes;
  1206. pool->channels = ch;
  1207. pool->samples = frame->nb_samples;
  1208. break;
  1209. }
  1210. default: av_assert0(0);
  1211. }
  1212. return 0;
  1213. fail:
  1214. for (i = 0; i < 4; i++)
  1215. av_buffer_pool_uninit(&pool->pools[i]);
  1216. pool->format = -1;
  1217. pool->planes = pool->channels = pool->samples = 0;
  1218. pool->width = pool->height = 0;
  1219. return ret;
  1220. }
  1221. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  1222. {
  1223. FramePool *pool = avctx->internal->pool;
  1224. int planes = pool->planes;
  1225. int i;
  1226. frame->linesize[0] = pool->linesize[0];
  1227. if (planes > AV_NUM_DATA_POINTERS) {
  1228. frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
  1229. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  1230. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  1231. sizeof(*frame->extended_buf));
  1232. if (!frame->extended_data || !frame->extended_buf) {
  1233. av_freep(&frame->extended_data);
  1234. av_freep(&frame->extended_buf);
  1235. return AVERROR(ENOMEM);
  1236. }
  1237. } else {
  1238. frame->extended_data = frame->data;
  1239. av_assert0(frame->nb_extended_buf == 0);
  1240. }
  1241. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  1242. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  1243. if (!frame->buf[i])
  1244. goto fail;
  1245. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  1246. }
  1247. for (i = 0; i < frame->nb_extended_buf; i++) {
  1248. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  1249. if (!frame->extended_buf[i])
  1250. goto fail;
  1251. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  1252. }
  1253. if (avctx->debug & FF_DEBUG_BUFFERS)
  1254. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  1255. return 0;
  1256. fail:
  1257. av_frame_unref(frame);
  1258. return AVERROR(ENOMEM);
  1259. }
  1260. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  1261. {
  1262. FramePool *pool = s->internal->pool;
  1263. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  1264. int i;
  1265. if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
  1266. av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
  1267. return -1;
  1268. }
  1269. if (!desc) {
  1270. av_log(s, AV_LOG_ERROR,
  1271. "Unable to get pixel format descriptor for format %s\n",
  1272. av_get_pix_fmt_name(pic->format));
  1273. return AVERROR(EINVAL);
  1274. }
  1275. memset(pic->data, 0, sizeof(pic->data));
  1276. pic->extended_data = pic->data;
  1277. for (i = 0; i < 4 && pool->pools[i]; i++) {
  1278. pic->linesize[i] = pool->linesize[i];
  1279. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  1280. if (!pic->buf[i])
  1281. goto fail;
  1282. pic->data[i] = pic->buf[i]->data;
  1283. }
  1284. for (; i < AV_NUM_DATA_POINTERS; i++) {
  1285. pic->data[i] = NULL;
  1286. pic->linesize[i] = 0;
  1287. }
  1288. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  1289. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  1290. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
  1291. if (s->debug & FF_DEBUG_BUFFERS)
  1292. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  1293. return 0;
  1294. fail:
  1295. av_frame_unref(pic);
  1296. return AVERROR(ENOMEM);
  1297. }
  1298. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  1299. {
  1300. int ret;
  1301. if (avctx->hw_frames_ctx)
  1302. return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  1303. if ((ret = update_frame_pool(avctx, frame)) < 0)
  1304. return ret;
  1305. switch (avctx->codec_type) {
  1306. case AVMEDIA_TYPE_VIDEO:
  1307. return video_get_buffer(avctx, frame);
  1308. case AVMEDIA_TYPE_AUDIO:
  1309. return audio_get_buffer(avctx, frame);
  1310. default:
  1311. return -1;
  1312. }
  1313. }
  1314. static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
  1315. {
  1316. int size;
  1317. const uint8_t *side_metadata;
  1318. AVDictionary **frame_md = &frame->metadata;
  1319. side_metadata = av_packet_get_side_data(avpkt,
  1320. AV_PKT_DATA_STRINGS_METADATA, &size);
  1321. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1322. }
  1323. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  1324. {
  1325. const AVPacket *pkt = avctx->internal->last_pkt_props;
  1326. int i;
  1327. static const struct {
  1328. enum AVPacketSideDataType packet;
  1329. enum AVFrameSideDataType frame;
  1330. } sd[] = {
  1331. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  1332. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  1333. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  1334. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  1335. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  1336. { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
  1337. { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
  1338. };
  1339. if (pkt) {
  1340. frame->pts = pkt->pts;
  1341. #if FF_API_PKT_PTS
  1342. FF_DISABLE_DEPRECATION_WARNINGS
  1343. frame->pkt_pts = pkt->pts;
  1344. FF_ENABLE_DEPRECATION_WARNINGS
  1345. #endif
  1346. frame->pkt_pos = pkt->pos;
  1347. frame->pkt_duration = pkt->duration;
  1348. frame->pkt_size = pkt->size;
  1349. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  1350. int size;
  1351. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  1352. if (packet_sd) {
  1353. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  1354. sd[i].frame,
  1355. size);
  1356. if (!frame_sd)
  1357. return AVERROR(ENOMEM);
  1358. memcpy(frame_sd->data, packet_sd, size);
  1359. }
  1360. }
  1361. add_metadata_from_side_data(pkt, frame);
  1362. if (pkt->flags & AV_PKT_FLAG_DISCARD) {
  1363. frame->flags |= AV_FRAME_FLAG_DISCARD;
  1364. } else {
  1365. frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
  1366. }
  1367. }
  1368. frame->reordered_opaque = avctx->reordered_opaque;
  1369. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  1370. frame->color_primaries = avctx->color_primaries;
  1371. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  1372. frame->color_trc = avctx->color_trc;
  1373. if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
  1374. frame->colorspace = avctx->colorspace;
  1375. if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
  1376. frame->color_range = avctx->color_range;
  1377. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  1378. frame->chroma_location = avctx->chroma_sample_location;
  1379. switch (avctx->codec->type) {
  1380. case AVMEDIA_TYPE_VIDEO:
  1381. frame->format = avctx->pix_fmt;
  1382. if (!frame->sample_aspect_ratio.num)
  1383. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1384. if (frame->width && frame->height &&
  1385. av_image_check_sar(frame->width, frame->height,
  1386. frame->sample_aspect_ratio) < 0) {
  1387. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1388. frame->sample_aspect_ratio.num,
  1389. frame->sample_aspect_ratio.den);
  1390. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  1391. }
  1392. break;
  1393. case AVMEDIA_TYPE_AUDIO:
  1394. if (!frame->sample_rate)
  1395. frame->sample_rate = avctx->sample_rate;
  1396. if (frame->format < 0)
  1397. frame->format = avctx->sample_fmt;
  1398. if (!frame->channel_layout) {
  1399. if (avctx->channel_layout) {
  1400. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  1401. avctx->channels) {
  1402. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  1403. "configuration.\n");
  1404. return AVERROR(EINVAL);
  1405. }
  1406. frame->channel_layout = avctx->channel_layout;
  1407. } else {
  1408. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1409. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  1410. avctx->channels);
  1411. return AVERROR(ENOSYS);
  1412. }
  1413. }
  1414. }
  1415. frame->channels = avctx->channels;
  1416. break;
  1417. }
  1418. return 0;
  1419. }
  1420. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  1421. {
  1422. return ff_init_buffer_info(avctx, frame);
  1423. }
  1424. static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
  1425. {
  1426. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1427. int i;
  1428. int num_planes = av_pix_fmt_count_planes(frame->format);
  1429. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  1430. int flags = desc ? desc->flags : 0;
  1431. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
  1432. num_planes = 2;
  1433. for (i = 0; i < num_planes; i++) {
  1434. av_assert0(frame->data[i]);
  1435. }
  1436. // For now do not enforce anything for palette of pseudopal formats
  1437. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PSEUDOPAL))
  1438. num_planes = 2;
  1439. // For formats without data like hwaccel allow unused pointers to be non-NULL.
  1440. for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
  1441. if (frame->data[i])
  1442. av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
  1443. frame->data[i] = NULL;
  1444. }
  1445. }
  1446. }
  1447. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  1448. {
  1449. const AVHWAccel *hwaccel = avctx->hwaccel;
  1450. int override_dimensions = 1;
  1451. int ret;
  1452. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1453. if ((ret = av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  1454. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  1455. return AVERROR(EINVAL);
  1456. }
  1457. if (frame->width <= 0 || frame->height <= 0) {
  1458. frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  1459. frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  1460. override_dimensions = 0;
  1461. }
  1462. if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
  1463. av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
  1464. return AVERROR(EINVAL);
  1465. }
  1466. }
  1467. ret = ff_decode_frame_props(avctx, frame);
  1468. if (ret < 0)
  1469. return ret;
  1470. if (hwaccel) {
  1471. if (hwaccel->alloc_frame) {
  1472. ret = hwaccel->alloc_frame(avctx, frame);
  1473. goto end;
  1474. }
  1475. } else
  1476. avctx->sw_pix_fmt = avctx->pix_fmt;
  1477. ret = avctx->get_buffer2(avctx, frame, flags);
  1478. if (ret >= 0)
  1479. validate_avframe_allocation(avctx, frame);
  1480. end:
  1481. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
  1482. !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
  1483. frame->width = avctx->width;
  1484. frame->height = avctx->height;
  1485. }
  1486. return ret;
  1487. }
  1488. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  1489. {
  1490. int ret = get_buffer_internal(avctx, frame, flags);
  1491. if (ret < 0) {
  1492. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1493. frame->width = frame->height = 0;
  1494. }
  1495. return ret;
  1496. }
  1497. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  1498. {
  1499. AVFrame *tmp;
  1500. int ret;
  1501. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  1502. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  1503. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  1504. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  1505. av_frame_unref(frame);
  1506. }
  1507. ff_init_buffer_info(avctx, frame);
  1508. if (!frame->data[0])
  1509. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1510. if (av_frame_is_writable(frame))
  1511. return ff_decode_frame_props(avctx, frame);
  1512. tmp = av_frame_alloc();
  1513. if (!tmp)
  1514. return AVERROR(ENOMEM);
  1515. av_frame_move_ref(tmp, frame);
  1516. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1517. if (ret < 0) {
  1518. av_frame_free(&tmp);
  1519. return ret;
  1520. }
  1521. av_frame_copy(frame, tmp);
  1522. av_frame_free(&tmp);
  1523. return 0;
  1524. }
  1525. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  1526. {
  1527. int ret = reget_buffer_internal(avctx, frame);
  1528. if (ret < 0)
  1529. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  1530. return ret;
  1531. }
  1532. void avcodec_flush_buffers(AVCodecContext *avctx)
  1533. {
  1534. avctx->internal->draining = 0;
  1535. avctx->internal->draining_done = 0;
  1536. avctx->internal->nb_draining_errors = 0;
  1537. av_frame_unref(avctx->internal->buffer_frame);
  1538. av_frame_unref(avctx->internal->compat_decode_frame);
  1539. av_packet_unref(avctx->internal->buffer_pkt);
  1540. avctx->internal->buffer_pkt_valid = 0;
  1541. av_packet_unref(avctx->internal->ds.in_pkt);
  1542. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1543. ff_thread_flush(avctx);
  1544. else if (avctx->codec->flush)
  1545. avctx->codec->flush(avctx);
  1546. avctx->pts_correction_last_pts =
  1547. avctx->pts_correction_last_dts = INT64_MIN;
  1548. ff_decode_bsfs_uninit(avctx);
  1549. if (!avctx->refcounted_frames)
  1550. av_frame_unref(avctx->internal->to_free);
  1551. }
  1552. void ff_decode_bsfs_uninit(AVCodecContext *avctx)
  1553. {
  1554. DecodeFilterContext *s = &avctx->internal->filter;
  1555. int i;
  1556. for (i = 0; i < s->nb_bsfs; i++)
  1557. av_bsf_free(&s->bsfs[i]);
  1558. av_freep(&s->bsfs);
  1559. s->nb_bsfs = 0;
  1560. }