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.

1693 lines
54KB

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