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.

1765 lines
57KB

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