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.

1831 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 min_log2_align = INT_MAX;
  661. for (i = 0; frame->data[i]; i++) {
  662. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  663. min_log2_align = FFMIN(log2_align, min_log2_align);
  664. }
  665. if (min_log2_align < 5) {
  666. frame->crop_left &= ~((1 << min_log2_align) - 1);
  667. calc_cropping_offsets(offsets, frame, desc);
  668. }
  669. }
  670. for (i = 0; frame->data[i]; i++)
  671. frame->data[i] += offsets[i];
  672. frame->width -= (frame->crop_left + frame->crop_right);
  673. frame->height -= (frame->crop_top + frame->crop_bottom);
  674. frame->crop_left = 0;
  675. frame->crop_right = 0;
  676. frame->crop_top = 0;
  677. frame->crop_bottom = 0;
  678. return 0;
  679. }
  680. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  681. {
  682. AVCodecInternal *avci = avctx->internal;
  683. int ret;
  684. av_frame_unref(frame);
  685. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  686. return AVERROR(EINVAL);
  687. ret = bsfs_init(avctx);
  688. if (ret < 0)
  689. return ret;
  690. if (avci->buffer_frame->buf[0]) {
  691. av_frame_move_ref(frame, avci->buffer_frame);
  692. } else {
  693. ret = decode_receive_frame_internal(avctx, frame);
  694. if (ret < 0)
  695. return ret;
  696. }
  697. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  698. ret = apply_cropping(avctx, frame);
  699. if (ret < 0) {
  700. av_frame_unref(frame);
  701. return ret;
  702. }
  703. }
  704. avctx->frame_number++;
  705. return 0;
  706. }
  707. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  708. int *got_frame, const AVPacket *pkt)
  709. {
  710. AVCodecInternal *avci = avctx->internal;
  711. int ret = 0;
  712. av_assert0(avci->compat_decode_consumed == 0);
  713. *got_frame = 0;
  714. avci->compat_decode = 1;
  715. if (avci->compat_decode_partial_size > 0 &&
  716. avci->compat_decode_partial_size != pkt->size) {
  717. av_log(avctx, AV_LOG_ERROR,
  718. "Got unexpected packet size after a partial decode\n");
  719. ret = AVERROR(EINVAL);
  720. goto finish;
  721. }
  722. if (!avci->compat_decode_partial_size) {
  723. ret = avcodec_send_packet(avctx, pkt);
  724. if (ret == AVERROR_EOF)
  725. ret = 0;
  726. else if (ret == AVERROR(EAGAIN)) {
  727. /* we fully drain all the output in each decode call, so this should not
  728. * ever happen */
  729. ret = AVERROR_BUG;
  730. goto finish;
  731. } else if (ret < 0)
  732. goto finish;
  733. }
  734. while (ret >= 0) {
  735. ret = avcodec_receive_frame(avctx, frame);
  736. if (ret < 0) {
  737. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  738. ret = 0;
  739. goto finish;
  740. }
  741. if (frame != avci->compat_decode_frame) {
  742. if (!avctx->refcounted_frames) {
  743. ret = unrefcount_frame(avci, frame);
  744. if (ret < 0)
  745. goto finish;
  746. }
  747. *got_frame = 1;
  748. frame = avci->compat_decode_frame;
  749. } else {
  750. if (!avci->compat_decode_warned) {
  751. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  752. "API cannot return all the frames for this decoder. "
  753. "Some frames will be dropped. Update your code to the "
  754. "new decoding API to fix this.\n");
  755. avci->compat_decode_warned = 1;
  756. }
  757. }
  758. if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
  759. break;
  760. }
  761. finish:
  762. if (ret == 0) {
  763. /* if there are any bsfs then assume full packet is always consumed */
  764. if (avctx->codec->bsfs)
  765. ret = pkt->size;
  766. else
  767. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  768. }
  769. avci->compat_decode_consumed = 0;
  770. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  771. return ret;
  772. }
  773. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  774. int *got_picture_ptr,
  775. const AVPacket *avpkt)
  776. {
  777. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  778. }
  779. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  780. AVFrame *frame,
  781. int *got_frame_ptr,
  782. const AVPacket *avpkt)
  783. {
  784. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  785. }
  786. static void get_subtitle_defaults(AVSubtitle *sub)
  787. {
  788. memset(sub, 0, sizeof(*sub));
  789. sub->pts = AV_NOPTS_VALUE;
  790. }
  791. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  792. static int recode_subtitle(AVCodecContext *avctx,
  793. AVPacket *outpkt, const AVPacket *inpkt)
  794. {
  795. #if CONFIG_ICONV
  796. iconv_t cd = (iconv_t)-1;
  797. int ret = 0;
  798. char *inb, *outb;
  799. size_t inl, outl;
  800. AVPacket tmp;
  801. #endif
  802. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  803. return 0;
  804. #if CONFIG_ICONV
  805. cd = iconv_open("UTF-8", avctx->sub_charenc);
  806. av_assert0(cd != (iconv_t)-1);
  807. inb = inpkt->data;
  808. inl = inpkt->size;
  809. if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
  810. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  811. ret = AVERROR(ENOMEM);
  812. goto end;
  813. }
  814. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  815. if (ret < 0)
  816. goto end;
  817. outpkt->buf = tmp.buf;
  818. outpkt->data = tmp.data;
  819. outpkt->size = tmp.size;
  820. outb = outpkt->data;
  821. outl = outpkt->size;
  822. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  823. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  824. outl >= outpkt->size || inl != 0) {
  825. ret = FFMIN(AVERROR(errno), -1);
  826. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  827. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  828. av_packet_unref(&tmp);
  829. goto end;
  830. }
  831. outpkt->size -= outl;
  832. memset(outpkt->data + outpkt->size, 0, outl);
  833. end:
  834. if (cd != (iconv_t)-1)
  835. iconv_close(cd);
  836. return ret;
  837. #else
  838. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  839. return AVERROR(EINVAL);
  840. #endif
  841. }
  842. static int utf8_check(const uint8_t *str)
  843. {
  844. const uint8_t *byte;
  845. uint32_t codepoint, min;
  846. while (*str) {
  847. byte = str;
  848. GET_UTF8(codepoint, *(byte++), return 0;);
  849. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  850. 1 << (5 * (byte - str) - 4);
  851. if (codepoint < min || codepoint >= 0x110000 ||
  852. codepoint == 0xFFFE /* BOM */ ||
  853. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  854. return 0;
  855. str = byte;
  856. }
  857. return 1;
  858. }
  859. #if FF_API_ASS_TIMING
  860. static void insert_ts(AVBPrint *buf, int ts)
  861. {
  862. if (ts == -1) {
  863. av_bprintf(buf, "9:59:59.99,");
  864. } else {
  865. int h, m, s;
  866. h = ts/360000; ts -= 360000*h;
  867. m = ts/ 6000; ts -= 6000*m;
  868. s = ts/ 100; ts -= 100*s;
  869. av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
  870. }
  871. }
  872. static int convert_sub_to_old_ass_form(AVSubtitle *sub, const AVPacket *pkt, AVRational tb)
  873. {
  874. int i;
  875. AVBPrint buf;
  876. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  877. for (i = 0; i < sub->num_rects; i++) {
  878. char *final_dialog;
  879. const char *dialog;
  880. AVSubtitleRect *rect = sub->rects[i];
  881. int ts_start, ts_duration = -1;
  882. long int layer;
  883. if (rect->type != SUBTITLE_ASS || !strncmp(rect->ass, "Dialogue: ", 10))
  884. continue;
  885. av_bprint_clear(&buf);
  886. /* skip ReadOrder */
  887. dialog = strchr(rect->ass, ',');
  888. if (!dialog)
  889. continue;
  890. dialog++;
  891. /* extract Layer or Marked */
  892. layer = strtol(dialog, (char**)&dialog, 10);
  893. if (*dialog != ',')
  894. continue;
  895. dialog++;
  896. /* rescale timing to ASS time base (ms) */
  897. ts_start = av_rescale_q(pkt->pts, tb, av_make_q(1, 100));
  898. if (pkt->duration != -1)
  899. ts_duration = av_rescale_q(pkt->duration, tb, av_make_q(1, 100));
  900. sub->end_display_time = FFMAX(sub->end_display_time, 10 * ts_duration);
  901. /* construct ASS (standalone file form with timestamps) string */
  902. av_bprintf(&buf, "Dialogue: %ld,", layer);
  903. insert_ts(&buf, ts_start);
  904. insert_ts(&buf, ts_duration == -1 ? -1 : ts_start + ts_duration);
  905. av_bprintf(&buf, "%s\r\n", dialog);
  906. final_dialog = av_strdup(buf.str);
  907. if (!av_bprint_is_complete(&buf) || !final_dialog) {
  908. av_freep(&final_dialog);
  909. av_bprint_finalize(&buf, NULL);
  910. return AVERROR(ENOMEM);
  911. }
  912. av_freep(&rect->ass);
  913. rect->ass = final_dialog;
  914. }
  915. av_bprint_finalize(&buf, NULL);
  916. return 0;
  917. }
  918. #endif
  919. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  920. int *got_sub_ptr,
  921. AVPacket *avpkt)
  922. {
  923. int i, ret = 0;
  924. AVCodecInternal *avci = avctx->internal;
  925. if (!avpkt->data && avpkt->size) {
  926. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  927. return AVERROR(EINVAL);
  928. }
  929. if (!avctx->codec)
  930. return AVERROR(EINVAL);
  931. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  932. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  933. return AVERROR(EINVAL);
  934. }
  935. *got_sub_ptr = 0;
  936. get_subtitle_defaults(sub);
  937. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  938. AVPacket pkt_recoded;
  939. AVPacket tmp = *avpkt;
  940. #if FF_API_MERGE_SD
  941. FF_DISABLE_DEPRECATION_WARNINGS
  942. int did_split = avci->compat_decode_partial_size ?
  943. ff_packet_split_and_drop_side_data(&tmp) :
  944. av_packet_split_side_data(&tmp);
  945. //apply_param_change(avctx, &tmp);
  946. if (did_split) {
  947. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  948. * proper padding.
  949. * If the side data is smaller than the buffer padding size, the
  950. * remaining bytes should have already been filled with zeros by the
  951. * original packet allocation anyway. */
  952. memset(tmp.data + tmp.size, 0,
  953. FFMIN(avpkt->size - tmp.size, AV_INPUT_BUFFER_PADDING_SIZE));
  954. }
  955. FF_ENABLE_DEPRECATION_WARNINGS
  956. #endif
  957. pkt_recoded = tmp;
  958. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  959. if (ret < 0) {
  960. *got_sub_ptr = 0;
  961. } else {
  962. ret = extract_packet_props(avctx->internal, &pkt_recoded);
  963. if (ret < 0)
  964. return ret;
  965. if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
  966. sub->pts = av_rescale_q(avpkt->pts,
  967. avctx->pkt_timebase, AV_TIME_BASE_Q);
  968. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  969. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  970. !!*got_sub_ptr >= !!sub->num_rects);
  971. #if FF_API_ASS_TIMING
  972. if (avctx->sub_text_format == FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS
  973. && *got_sub_ptr && sub->num_rects) {
  974. const AVRational tb = avctx->pkt_timebase.num ? avctx->pkt_timebase
  975. : avctx->time_base;
  976. int err = convert_sub_to_old_ass_form(sub, avpkt, tb);
  977. if (err < 0)
  978. ret = err;
  979. }
  980. #endif
  981. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  982. avctx->pkt_timebase.num) {
  983. AVRational ms = { 1, 1000 };
  984. sub->end_display_time = av_rescale_q(avpkt->duration,
  985. avctx->pkt_timebase, ms);
  986. }
  987. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  988. sub->format = 0;
  989. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  990. sub->format = 1;
  991. for (i = 0; i < sub->num_rects; i++) {
  992. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  993. av_log(avctx, AV_LOG_ERROR,
  994. "Invalid UTF-8 in decoded subtitles text; "
  995. "maybe missing -sub_charenc option\n");
  996. avsubtitle_free(sub);
  997. ret = AVERROR_INVALIDDATA;
  998. break;
  999. }
  1000. }
  1001. if (tmp.data != pkt_recoded.data) { // did we recode?
  1002. /* prevent from destroying side data from original packet */
  1003. pkt_recoded.side_data = NULL;
  1004. pkt_recoded.side_data_elems = 0;
  1005. av_packet_unref(&pkt_recoded);
  1006. }
  1007. }
  1008. #if FF_API_MERGE_SD
  1009. if (did_split) {
  1010. av_packet_free_side_data(&tmp);
  1011. if(ret == tmp.size)
  1012. ret = avpkt->size;
  1013. }
  1014. #endif
  1015. if (*got_sub_ptr)
  1016. avctx->frame_number++;
  1017. }
  1018. return ret;
  1019. }
  1020. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  1021. {
  1022. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  1023. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  1024. }
  1025. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  1026. {
  1027. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  1028. ++fmt;
  1029. return fmt[0];
  1030. }
  1031. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  1032. enum AVPixelFormat pix_fmt)
  1033. {
  1034. AVHWAccel *hwaccel = NULL;
  1035. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1036. if (hwaccel->id == codec_id
  1037. && hwaccel->pix_fmt == pix_fmt)
  1038. return hwaccel;
  1039. return NULL;
  1040. }
  1041. static int setup_hwaccel(AVCodecContext *avctx,
  1042. const enum AVPixelFormat fmt,
  1043. const char *name)
  1044. {
  1045. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  1046. int ret = 0;
  1047. if (!hwa) {
  1048. av_log(avctx, AV_LOG_ERROR,
  1049. "Could not find an AVHWAccel for the pixel format: %s",
  1050. name);
  1051. return AVERROR(ENOENT);
  1052. }
  1053. if (hwa->capabilities & HWACCEL_CODEC_CAP_EXPERIMENTAL &&
  1054. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1055. av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
  1056. hwa->name);
  1057. return AVERROR_PATCHWELCOME;
  1058. }
  1059. if (hwa->priv_data_size) {
  1060. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  1061. if (!avctx->internal->hwaccel_priv_data)
  1062. return AVERROR(ENOMEM);
  1063. }
  1064. if (hwa->init) {
  1065. ret = hwa->init(avctx);
  1066. if (ret < 0) {
  1067. av_freep(&avctx->internal->hwaccel_priv_data);
  1068. return ret;
  1069. }
  1070. }
  1071. avctx->hwaccel = hwa;
  1072. return 0;
  1073. }
  1074. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1075. {
  1076. const AVPixFmtDescriptor *desc;
  1077. enum AVPixelFormat *choices;
  1078. enum AVPixelFormat ret;
  1079. unsigned n = 0;
  1080. while (fmt[n] != AV_PIX_FMT_NONE)
  1081. ++n;
  1082. av_assert0(n >= 1);
  1083. avctx->sw_pix_fmt = fmt[n - 1];
  1084. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  1085. choices = av_malloc_array(n + 1, sizeof(*choices));
  1086. if (!choices)
  1087. return AV_PIX_FMT_NONE;
  1088. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  1089. for (;;) {
  1090. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1091. avctx->hwaccel->uninit(avctx);
  1092. av_freep(&avctx->internal->hwaccel_priv_data);
  1093. avctx->hwaccel = NULL;
  1094. av_buffer_unref(&avctx->hw_frames_ctx);
  1095. ret = avctx->get_format(avctx, choices);
  1096. desc = av_pix_fmt_desc_get(ret);
  1097. if (!desc) {
  1098. ret = AV_PIX_FMT_NONE;
  1099. break;
  1100. }
  1101. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1102. break;
  1103. #if FF_API_CAP_VDPAU
  1104. if (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)
  1105. break;
  1106. #endif
  1107. if (avctx->hw_frames_ctx) {
  1108. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  1109. if (hw_frames_ctx->format != ret) {
  1110. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  1111. "does not match the format of provided AVHWFramesContext\n");
  1112. ret = AV_PIX_FMT_NONE;
  1113. break;
  1114. }
  1115. }
  1116. if (!setup_hwaccel(avctx, ret, desc->name))
  1117. break;
  1118. /* Remove failed hwaccel from choices */
  1119. for (n = 0; choices[n] != ret; n++)
  1120. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  1121. do
  1122. choices[n] = choices[n + 1];
  1123. while (choices[n++] != AV_PIX_FMT_NONE);
  1124. }
  1125. av_freep(&choices);
  1126. return ret;
  1127. }
  1128. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  1129. {
  1130. FramePool *pool = avctx->internal->pool;
  1131. int i, ret;
  1132. switch (avctx->codec_type) {
  1133. case AVMEDIA_TYPE_VIDEO: {
  1134. uint8_t *data[4];
  1135. int linesize[4];
  1136. int size[4] = { 0 };
  1137. int w = frame->width;
  1138. int h = frame->height;
  1139. int tmpsize, unaligned;
  1140. if (pool->format == frame->format &&
  1141. pool->width == frame->width && pool->height == frame->height)
  1142. return 0;
  1143. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  1144. do {
  1145. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  1146. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  1147. ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  1148. if (ret < 0)
  1149. return ret;
  1150. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  1151. w += w & ~(w - 1);
  1152. unaligned = 0;
  1153. for (i = 0; i < 4; i++)
  1154. unaligned |= linesize[i] % pool->stride_align[i];
  1155. } while (unaligned);
  1156. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  1157. NULL, linesize);
  1158. if (tmpsize < 0)
  1159. return -1;
  1160. for (i = 0; i < 3 && data[i + 1]; i++)
  1161. size[i] = data[i + 1] - data[i];
  1162. size[i] = tmpsize - (data[i] - data[0]);
  1163. for (i = 0; i < 4; i++) {
  1164. av_buffer_pool_uninit(&pool->pools[i]);
  1165. pool->linesize[i] = linesize[i];
  1166. if (size[i]) {
  1167. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  1168. CONFIG_MEMORY_POISONING ?
  1169. NULL :
  1170. av_buffer_allocz);
  1171. if (!pool->pools[i]) {
  1172. ret = AVERROR(ENOMEM);
  1173. goto fail;
  1174. }
  1175. }
  1176. }
  1177. pool->format = frame->format;
  1178. pool->width = frame->width;
  1179. pool->height = frame->height;
  1180. break;
  1181. }
  1182. case AVMEDIA_TYPE_AUDIO: {
  1183. int ch = frame->channels; //av_get_channel_layout_nb_channels(frame->channel_layout);
  1184. int planar = av_sample_fmt_is_planar(frame->format);
  1185. int planes = planar ? ch : 1;
  1186. if (pool->format == frame->format && pool->planes == planes &&
  1187. pool->channels == ch && frame->nb_samples == pool->samples)
  1188. return 0;
  1189. av_buffer_pool_uninit(&pool->pools[0]);
  1190. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  1191. frame->nb_samples, frame->format, 0);
  1192. if (ret < 0)
  1193. goto fail;
  1194. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  1195. if (!pool->pools[0]) {
  1196. ret = AVERROR(ENOMEM);
  1197. goto fail;
  1198. }
  1199. pool->format = frame->format;
  1200. pool->planes = planes;
  1201. pool->channels = ch;
  1202. pool->samples = frame->nb_samples;
  1203. break;
  1204. }
  1205. default: av_assert0(0);
  1206. }
  1207. return 0;
  1208. fail:
  1209. for (i = 0; i < 4; i++)
  1210. av_buffer_pool_uninit(&pool->pools[i]);
  1211. pool->format = -1;
  1212. pool->planes = pool->channels = pool->samples = 0;
  1213. pool->width = pool->height = 0;
  1214. return ret;
  1215. }
  1216. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  1217. {
  1218. FramePool *pool = avctx->internal->pool;
  1219. int planes = pool->planes;
  1220. int i;
  1221. frame->linesize[0] = pool->linesize[0];
  1222. if (planes > AV_NUM_DATA_POINTERS) {
  1223. frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
  1224. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  1225. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  1226. sizeof(*frame->extended_buf));
  1227. if (!frame->extended_data || !frame->extended_buf) {
  1228. av_freep(&frame->extended_data);
  1229. av_freep(&frame->extended_buf);
  1230. return AVERROR(ENOMEM);
  1231. }
  1232. } else {
  1233. frame->extended_data = frame->data;
  1234. av_assert0(frame->nb_extended_buf == 0);
  1235. }
  1236. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  1237. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  1238. if (!frame->buf[i])
  1239. goto fail;
  1240. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  1241. }
  1242. for (i = 0; i < frame->nb_extended_buf; i++) {
  1243. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  1244. if (!frame->extended_buf[i])
  1245. goto fail;
  1246. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  1247. }
  1248. if (avctx->debug & FF_DEBUG_BUFFERS)
  1249. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  1250. return 0;
  1251. fail:
  1252. av_frame_unref(frame);
  1253. return AVERROR(ENOMEM);
  1254. }
  1255. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  1256. {
  1257. FramePool *pool = s->internal->pool;
  1258. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  1259. int i;
  1260. if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
  1261. av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
  1262. return -1;
  1263. }
  1264. if (!desc) {
  1265. av_log(s, AV_LOG_ERROR,
  1266. "Unable to get pixel format descriptor for format %s\n",
  1267. av_get_pix_fmt_name(pic->format));
  1268. return AVERROR(EINVAL);
  1269. }
  1270. memset(pic->data, 0, sizeof(pic->data));
  1271. pic->extended_data = pic->data;
  1272. for (i = 0; i < 4 && pool->pools[i]; i++) {
  1273. pic->linesize[i] = pool->linesize[i];
  1274. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  1275. if (!pic->buf[i])
  1276. goto fail;
  1277. pic->data[i] = pic->buf[i]->data;
  1278. }
  1279. for (; i < AV_NUM_DATA_POINTERS; i++) {
  1280. pic->data[i] = NULL;
  1281. pic->linesize[i] = 0;
  1282. }
  1283. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  1284. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  1285. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
  1286. if (s->debug & FF_DEBUG_BUFFERS)
  1287. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  1288. return 0;
  1289. fail:
  1290. av_frame_unref(pic);
  1291. return AVERROR(ENOMEM);
  1292. }
  1293. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  1294. {
  1295. int ret;
  1296. if (avctx->hw_frames_ctx)
  1297. return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  1298. if ((ret = update_frame_pool(avctx, frame)) < 0)
  1299. return ret;
  1300. switch (avctx->codec_type) {
  1301. case AVMEDIA_TYPE_VIDEO:
  1302. return video_get_buffer(avctx, frame);
  1303. case AVMEDIA_TYPE_AUDIO:
  1304. return audio_get_buffer(avctx, frame);
  1305. default:
  1306. return -1;
  1307. }
  1308. }
  1309. static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
  1310. {
  1311. int size;
  1312. const uint8_t *side_metadata;
  1313. AVDictionary **frame_md = &frame->metadata;
  1314. side_metadata = av_packet_get_side_data(avpkt,
  1315. AV_PKT_DATA_STRINGS_METADATA, &size);
  1316. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1317. }
  1318. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  1319. {
  1320. const AVPacket *pkt = avctx->internal->last_pkt_props;
  1321. int i;
  1322. static const struct {
  1323. enum AVPacketSideDataType packet;
  1324. enum AVFrameSideDataType frame;
  1325. } sd[] = {
  1326. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  1327. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  1328. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  1329. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  1330. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  1331. { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
  1332. { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
  1333. };
  1334. if (pkt) {
  1335. frame->pts = pkt->pts;
  1336. #if FF_API_PKT_PTS
  1337. FF_DISABLE_DEPRECATION_WARNINGS
  1338. frame->pkt_pts = pkt->pts;
  1339. FF_ENABLE_DEPRECATION_WARNINGS
  1340. #endif
  1341. frame->pkt_pos = pkt->pos;
  1342. frame->pkt_duration = pkt->duration;
  1343. frame->pkt_size = pkt->size;
  1344. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  1345. int size;
  1346. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  1347. if (packet_sd) {
  1348. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  1349. sd[i].frame,
  1350. size);
  1351. if (!frame_sd)
  1352. return AVERROR(ENOMEM);
  1353. memcpy(frame_sd->data, packet_sd, size);
  1354. }
  1355. }
  1356. add_metadata_from_side_data(pkt, frame);
  1357. if (pkt->flags & AV_PKT_FLAG_DISCARD) {
  1358. frame->flags |= AV_FRAME_FLAG_DISCARD;
  1359. } else {
  1360. frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
  1361. }
  1362. }
  1363. frame->reordered_opaque = avctx->reordered_opaque;
  1364. if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
  1365. frame->color_primaries = avctx->color_primaries;
  1366. if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
  1367. frame->color_trc = avctx->color_trc;
  1368. if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
  1369. frame->colorspace = avctx->colorspace;
  1370. if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
  1371. frame->color_range = avctx->color_range;
  1372. if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
  1373. frame->chroma_location = avctx->chroma_sample_location;
  1374. switch (avctx->codec->type) {
  1375. case AVMEDIA_TYPE_VIDEO:
  1376. frame->format = avctx->pix_fmt;
  1377. if (!frame->sample_aspect_ratio.num)
  1378. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1379. if (frame->width && frame->height &&
  1380. av_image_check_sar(frame->width, frame->height,
  1381. frame->sample_aspect_ratio) < 0) {
  1382. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1383. frame->sample_aspect_ratio.num,
  1384. frame->sample_aspect_ratio.den);
  1385. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  1386. }
  1387. break;
  1388. case AVMEDIA_TYPE_AUDIO:
  1389. if (!frame->sample_rate)
  1390. frame->sample_rate = avctx->sample_rate;
  1391. if (frame->format < 0)
  1392. frame->format = avctx->sample_fmt;
  1393. if (!frame->channel_layout) {
  1394. if (avctx->channel_layout) {
  1395. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  1396. avctx->channels) {
  1397. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  1398. "configuration.\n");
  1399. return AVERROR(EINVAL);
  1400. }
  1401. frame->channel_layout = avctx->channel_layout;
  1402. } else {
  1403. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1404. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  1405. avctx->channels);
  1406. return AVERROR(ENOSYS);
  1407. }
  1408. }
  1409. }
  1410. frame->channels = avctx->channels;
  1411. break;
  1412. }
  1413. return 0;
  1414. }
  1415. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  1416. {
  1417. return ff_init_buffer_info(avctx, frame);
  1418. }
  1419. static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
  1420. {
  1421. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1422. int i;
  1423. int num_planes = av_pix_fmt_count_planes(frame->format);
  1424. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  1425. int flags = desc ? desc->flags : 0;
  1426. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
  1427. num_planes = 2;
  1428. for (i = 0; i < num_planes; i++) {
  1429. av_assert0(frame->data[i]);
  1430. }
  1431. // For now do not enforce anything for palette of pseudopal formats
  1432. if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PSEUDOPAL))
  1433. num_planes = 2;
  1434. // For formats without data like hwaccel allow unused pointers to be non-NULL.
  1435. for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
  1436. if (frame->data[i])
  1437. av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
  1438. frame->data[i] = NULL;
  1439. }
  1440. }
  1441. }
  1442. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  1443. {
  1444. const AVHWAccel *hwaccel = avctx->hwaccel;
  1445. int override_dimensions = 1;
  1446. int ret;
  1447. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1448. if ((ret = av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  1449. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  1450. return AVERROR(EINVAL);
  1451. }
  1452. if (frame->width <= 0 || frame->height <= 0) {
  1453. frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  1454. frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  1455. override_dimensions = 0;
  1456. }
  1457. if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
  1458. av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
  1459. return AVERROR(EINVAL);
  1460. }
  1461. }
  1462. ret = ff_decode_frame_props(avctx, frame);
  1463. if (ret < 0)
  1464. return ret;
  1465. if (hwaccel) {
  1466. if (hwaccel->alloc_frame) {
  1467. ret = hwaccel->alloc_frame(avctx, frame);
  1468. goto end;
  1469. }
  1470. } else
  1471. avctx->sw_pix_fmt = avctx->pix_fmt;
  1472. ret = avctx->get_buffer2(avctx, frame, flags);
  1473. if (ret >= 0)
  1474. validate_avframe_allocation(avctx, frame);
  1475. end:
  1476. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
  1477. !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
  1478. frame->width = avctx->width;
  1479. frame->height = avctx->height;
  1480. }
  1481. return ret;
  1482. }
  1483. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  1484. {
  1485. int ret = get_buffer_internal(avctx, frame, flags);
  1486. if (ret < 0) {
  1487. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1488. frame->width = frame->height = 0;
  1489. }
  1490. return ret;
  1491. }
  1492. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  1493. {
  1494. AVFrame *tmp;
  1495. int ret;
  1496. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  1497. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  1498. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  1499. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  1500. av_frame_unref(frame);
  1501. }
  1502. ff_init_buffer_info(avctx, frame);
  1503. if (!frame->data[0])
  1504. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1505. if (av_frame_is_writable(frame))
  1506. return ff_decode_frame_props(avctx, frame);
  1507. tmp = av_frame_alloc();
  1508. if (!tmp)
  1509. return AVERROR(ENOMEM);
  1510. av_frame_move_ref(tmp, frame);
  1511. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1512. if (ret < 0) {
  1513. av_frame_free(&tmp);
  1514. return ret;
  1515. }
  1516. av_frame_copy(frame, tmp);
  1517. av_frame_free(&tmp);
  1518. return 0;
  1519. }
  1520. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  1521. {
  1522. int ret = reget_buffer_internal(avctx, frame);
  1523. if (ret < 0)
  1524. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  1525. return ret;
  1526. }
  1527. void avcodec_flush_buffers(AVCodecContext *avctx)
  1528. {
  1529. avctx->internal->draining = 0;
  1530. avctx->internal->draining_done = 0;
  1531. avctx->internal->nb_draining_errors = 0;
  1532. av_frame_unref(avctx->internal->buffer_frame);
  1533. av_frame_unref(avctx->internal->compat_decode_frame);
  1534. av_packet_unref(avctx->internal->buffer_pkt);
  1535. avctx->internal->buffer_pkt_valid = 0;
  1536. av_packet_unref(avctx->internal->ds.in_pkt);
  1537. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1538. ff_thread_flush(avctx);
  1539. else if (avctx->codec->flush)
  1540. avctx->codec->flush(avctx);
  1541. avctx->pts_correction_last_pts =
  1542. avctx->pts_correction_last_dts = INT64_MIN;
  1543. ff_decode_bsfs_uninit(avctx);
  1544. if (!avctx->refcounted_frames)
  1545. av_frame_unref(avctx->internal->to_free);
  1546. }
  1547. void ff_decode_bsfs_uninit(AVCodecContext *avctx)
  1548. {
  1549. DecodeFilterContext *s = &avctx->internal->filter;
  1550. int i;
  1551. for (i = 0; i < s->nb_bsfs; i++)
  1552. av_bsf_free(&s->bsfs[i]);
  1553. av_freep(&s->bsfs);
  1554. s->nb_bsfs = 0;
  1555. }