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.

1428 lines
43KB

  1. /*
  2. * generic decoding-related code
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/frame.h"
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/intmath.h"
  30. #include "avcodec.h"
  31. #include "bytestream.h"
  32. #include "decode.h"
  33. #include "hwaccel.h"
  34. #include "internal.h"
  35. #include "thread.h"
  36. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  37. {
  38. int size = 0, ret;
  39. const uint8_t *data;
  40. uint32_t flags;
  41. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  42. if (!data)
  43. return 0;
  44. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  45. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  46. "changes, but PARAM_CHANGE side data was sent to it.\n");
  47. ret = AVERROR(EINVAL);
  48. goto fail2;
  49. }
  50. if (size < 4)
  51. goto fail;
  52. flags = bytestream_get_le32(&data);
  53. size -= 4;
  54. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  55. if (size < 4)
  56. goto fail;
  57. avctx->channels = bytestream_get_le32(&data);
  58. size -= 4;
  59. }
  60. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  61. if (size < 8)
  62. goto fail;
  63. avctx->channel_layout = bytestream_get_le64(&data);
  64. size -= 8;
  65. }
  66. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  67. if (size < 4)
  68. goto fail;
  69. avctx->sample_rate = bytestream_get_le32(&data);
  70. size -= 4;
  71. }
  72. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  73. if (size < 8)
  74. goto fail;
  75. avctx->width = bytestream_get_le32(&data);
  76. avctx->height = bytestream_get_le32(&data);
  77. size -= 8;
  78. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  79. if (ret < 0)
  80. goto fail2;
  81. }
  82. return 0;
  83. fail:
  84. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  85. ret = AVERROR_INVALIDDATA;
  86. fail2:
  87. if (ret < 0) {
  88. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  89. if (avctx->err_recognition & AV_EF_EXPLODE)
  90. return ret;
  91. }
  92. return 0;
  93. }
  94. static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
  95. {
  96. av_packet_unref(avci->last_pkt_props);
  97. if (pkt)
  98. return av_packet_copy_props(avci->last_pkt_props, pkt);
  99. return 0;
  100. }
  101. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  102. {
  103. int ret;
  104. /* move the original frame to our backup */
  105. av_frame_unref(avci->to_free);
  106. av_frame_move_ref(avci->to_free, frame);
  107. /* now copy everything except the AVBufferRefs back
  108. * note that we make a COPY of the side data, so calling av_frame_free() on
  109. * the caller's frame will work properly */
  110. ret = av_frame_copy_props(frame, avci->to_free);
  111. if (ret < 0)
  112. return ret;
  113. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  114. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  115. if (avci->to_free->extended_data != avci->to_free->data) {
  116. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  117. int size = planes * sizeof(*frame->extended_data);
  118. if (!size) {
  119. av_frame_unref(frame);
  120. return AVERROR_BUG;
  121. }
  122. frame->extended_data = av_malloc(size);
  123. if (!frame->extended_data) {
  124. av_frame_unref(frame);
  125. return AVERROR(ENOMEM);
  126. }
  127. memcpy(frame->extended_data, avci->to_free->extended_data,
  128. size);
  129. } else
  130. frame->extended_data = frame->data;
  131. frame->format = avci->to_free->format;
  132. frame->width = avci->to_free->width;
  133. frame->height = avci->to_free->height;
  134. frame->channel_layout = avci->to_free->channel_layout;
  135. frame->nb_samples = avci->to_free->nb_samples;
  136. return 0;
  137. }
  138. static int bsfs_init(AVCodecContext *avctx)
  139. {
  140. AVCodecInternal *avci = avctx->internal;
  141. DecodeFilterContext *s = &avci->filter;
  142. const char *bsfs_str;
  143. int ret;
  144. if (s->nb_bsfs)
  145. return 0;
  146. bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
  147. while (bsfs_str && *bsfs_str) {
  148. AVBSFContext **tmp;
  149. const AVBitStreamFilter *filter;
  150. char *bsf;
  151. bsf = av_get_token(&bsfs_str, ",");
  152. if (!bsf) {
  153. ret = AVERROR(ENOMEM);
  154. goto fail;
  155. }
  156. filter = av_bsf_get_by_name(bsf);
  157. if (!filter) {
  158. av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
  159. "requested by a decoder. This is a bug, please report it.\n",
  160. bsf);
  161. ret = AVERROR_BUG;
  162. av_freep(&bsf);
  163. goto fail;
  164. }
  165. av_freep(&bsf);
  166. tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
  167. if (!tmp) {
  168. ret = AVERROR(ENOMEM);
  169. goto fail;
  170. }
  171. s->bsfs = tmp;
  172. s->nb_bsfs++;
  173. ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
  174. if (ret < 0)
  175. goto fail;
  176. if (s->nb_bsfs == 1) {
  177. /* We do not currently have an API for passing the input timebase into decoders,
  178. * but no filters used here should actually need it.
  179. * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
  180. s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
  181. ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
  182. avctx);
  183. } else {
  184. s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
  185. ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
  186. s->bsfs[s->nb_bsfs - 2]->par_out);
  187. }
  188. if (ret < 0)
  189. goto fail;
  190. ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
  191. if (ret < 0)
  192. goto fail;
  193. }
  194. return 0;
  195. fail:
  196. ff_decode_bsfs_uninit(avctx);
  197. return ret;
  198. }
  199. /* try to get one output packet from the filter chain */
  200. static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
  201. {
  202. DecodeFilterContext *s = &avctx->internal->filter;
  203. int idx, ret;
  204. /* start with the last filter in the chain */
  205. idx = s->nb_bsfs - 1;
  206. while (idx >= 0) {
  207. /* request a packet from the currently selected filter */
  208. ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
  209. if (ret == AVERROR(EAGAIN)) {
  210. /* no packets available, try the next filter up the chain */
  211. ret = 0;
  212. idx--;
  213. continue;
  214. } else if (ret < 0 && ret != AVERROR_EOF) {
  215. return ret;
  216. }
  217. /* got a packet or EOF -- pass it to the caller or to the next filter
  218. * down the chain */
  219. if (idx == s->nb_bsfs - 1) {
  220. return ret;
  221. } else {
  222. idx++;
  223. ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
  224. if (ret < 0) {
  225. av_log(avctx, AV_LOG_ERROR,
  226. "Error pre-processing a packet before decoding\n");
  227. av_packet_unref(pkt);
  228. return ret;
  229. }
  230. }
  231. }
  232. return AVERROR(EAGAIN);
  233. }
  234. int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
  235. {
  236. AVCodecInternal *avci = avctx->internal;
  237. int ret;
  238. if (avci->draining)
  239. return AVERROR_EOF;
  240. ret = bsfs_poll(avctx, pkt);
  241. if (ret == AVERROR_EOF)
  242. avci->draining = 1;
  243. if (ret < 0)
  244. return ret;
  245. ret = extract_packet_props(avctx->internal, pkt);
  246. if (ret < 0)
  247. goto finish;
  248. ret = apply_param_change(avctx, pkt);
  249. if (ret < 0)
  250. goto finish;
  251. if (avctx->codec->receive_frame)
  252. avci->compat_decode_consumed += pkt->size;
  253. return 0;
  254. finish:
  255. av_packet_unref(pkt);
  256. return ret;
  257. }
  258. /*
  259. * The core of the receive_frame_wrapper for the decoders implementing
  260. * the simple API. Certain decoders might consume partial packets without
  261. * returning any output, so this function needs to be called in a loop until it
  262. * returns EAGAIN.
  263. **/
  264. static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
  265. {
  266. AVCodecInternal *avci = avctx->internal;
  267. DecodeSimpleContext *ds = &avci->ds;
  268. AVPacket *pkt = ds->in_pkt;
  269. int got_frame;
  270. int ret;
  271. if (!pkt->data && !avci->draining) {
  272. av_packet_unref(pkt);
  273. ret = ff_decode_get_packet(avctx, pkt);
  274. if (ret < 0 && ret != AVERROR_EOF)
  275. return ret;
  276. }
  277. // Some codecs (at least wma lossless) will crash when feeding drain packets
  278. // after EOF was signaled.
  279. if (avci->draining_done)
  280. return AVERROR_EOF;
  281. if (!pkt->data &&
  282. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
  283. avctx->active_thread_type & FF_THREAD_FRAME))
  284. return AVERROR_EOF;
  285. got_frame = 0;
  286. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
  287. ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
  288. } else {
  289. ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
  290. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  291. frame->pkt_dts = pkt->dts;
  292. /* get_buffer is supposed to set frame parameters */
  293. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  294. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  295. frame->width = avctx->width;
  296. frame->height = avctx->height;
  297. frame->format = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
  298. avctx->pix_fmt : avctx->sample_fmt;
  299. }
  300. }
  301. emms_c();
  302. if (!got_frame)
  303. av_frame_unref(frame);
  304. if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
  305. ret = pkt->size;
  306. if (avctx->internal->draining && !got_frame)
  307. avci->draining_done = 1;
  308. avci->compat_decode_consumed += ret;
  309. if (ret >= pkt->size || ret < 0) {
  310. av_packet_unref(pkt);
  311. } else {
  312. int consumed = ret;
  313. pkt->data += consumed;
  314. pkt->size -= consumed;
  315. pkt->pts = AV_NOPTS_VALUE;
  316. pkt->dts = AV_NOPTS_VALUE;
  317. avci->last_pkt_props->pts = AV_NOPTS_VALUE;
  318. avci->last_pkt_props->dts = AV_NOPTS_VALUE;
  319. }
  320. if (got_frame)
  321. av_assert0(frame->buf[0]);
  322. return ret < 0 ? ret : 0;
  323. }
  324. static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  325. {
  326. int ret;
  327. while (!frame->buf[0]) {
  328. ret = decode_simple_internal(avctx, frame);
  329. if (ret < 0)
  330. return ret;
  331. }
  332. return 0;
  333. }
  334. static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
  335. {
  336. AVCodecInternal *avci = avctx->internal;
  337. int ret;
  338. av_assert0(!frame->buf[0]);
  339. if (avctx->codec->receive_frame)
  340. ret = avctx->codec->receive_frame(avctx, frame);
  341. else
  342. ret = decode_simple_receive_frame(avctx, frame);
  343. if (ret == AVERROR_EOF)
  344. avci->draining_done = 1;
  345. /* unwrap the per-frame decode data and restore the original opaque_ref*/
  346. if (!ret) {
  347. /* the only case where decode data is not set should be decoders
  348. * that do not call ff_get_buffer() */
  349. av_assert0((frame->opaque_ref && frame->opaque_ref->size == sizeof(FrameDecodeData)) ||
  350. !(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
  351. if (frame->opaque_ref) {
  352. FrameDecodeData *fdd;
  353. AVBufferRef *user_opaque_ref;
  354. fdd = (FrameDecodeData*)frame->opaque_ref->data;
  355. if (fdd->post_process) {
  356. ret = fdd->post_process(avctx, frame);
  357. if (ret < 0) {
  358. av_frame_unref(frame);
  359. return ret;
  360. }
  361. }
  362. user_opaque_ref = fdd->user_opaque_ref;
  363. fdd->user_opaque_ref = NULL;
  364. av_buffer_unref(&frame->opaque_ref);
  365. frame->opaque_ref = user_opaque_ref;
  366. }
  367. }
  368. return ret;
  369. }
  370. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  371. {
  372. AVCodecInternal *avci = avctx->internal;
  373. int ret = 0;
  374. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  375. return AVERROR(EINVAL);
  376. if (avctx->internal->draining)
  377. return AVERROR_EOF;
  378. ret = bsfs_init(avctx);
  379. if (ret < 0)
  380. return ret;
  381. av_packet_unref(avci->buffer_pkt);
  382. if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
  383. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  384. if (ret < 0)
  385. return ret;
  386. }
  387. ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
  388. if (ret < 0) {
  389. av_packet_unref(avci->buffer_pkt);
  390. return ret;
  391. }
  392. if (!avci->buffer_frame->buf[0]) {
  393. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  394. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  395. return ret;
  396. }
  397. return 0;
  398. }
  399. static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
  400. {
  401. /* make sure we are noisy about decoders returning invalid cropping data */
  402. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  403. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  404. (frame->crop_left + frame->crop_right) >= frame->width ||
  405. (frame->crop_top + frame->crop_bottom) >= frame->height) {
  406. av_log(avctx, AV_LOG_WARNING,
  407. "Invalid cropping information set by a decoder: %zu/%zu/%zu/%zu "
  408. "(frame size %dx%d). This is a bug, please report it\n",
  409. frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
  410. frame->width, frame->height);
  411. frame->crop_left = 0;
  412. frame->crop_right = 0;
  413. frame->crop_top = 0;
  414. frame->crop_bottom = 0;
  415. return 0;
  416. }
  417. if (!avctx->apply_cropping)
  418. return 0;
  419. return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
  420. AV_FRAME_CROP_UNALIGNED : 0);
  421. }
  422. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  423. {
  424. AVCodecInternal *avci = avctx->internal;
  425. int ret;
  426. av_frame_unref(frame);
  427. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  428. return AVERROR(EINVAL);
  429. ret = bsfs_init(avctx);
  430. if (ret < 0)
  431. return ret;
  432. if (avci->buffer_frame->buf[0]) {
  433. av_frame_move_ref(frame, avci->buffer_frame);
  434. } else {
  435. ret = decode_receive_frame_internal(avctx, frame);
  436. if (ret < 0)
  437. return ret;
  438. }
  439. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  440. ret = apply_cropping(avctx, frame);
  441. if (ret < 0) {
  442. av_frame_unref(frame);
  443. return ret;
  444. }
  445. }
  446. avctx->frame_number++;
  447. return 0;
  448. }
  449. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  450. int *got_frame, AVPacket *pkt)
  451. {
  452. AVCodecInternal *avci = avctx->internal;
  453. int ret = 0;
  454. av_assert0(avci->compat_decode_consumed == 0);
  455. *got_frame = 0;
  456. avci->compat_decode = 1;
  457. if (avci->compat_decode_partial_size > 0 &&
  458. avci->compat_decode_partial_size != pkt->size) {
  459. av_log(avctx, AV_LOG_ERROR,
  460. "Got unexpected packet size after a partial decode\n");
  461. ret = AVERROR(EINVAL);
  462. goto finish;
  463. }
  464. if (!avci->compat_decode_partial_size) {
  465. ret = avcodec_send_packet(avctx, pkt);
  466. if (ret == AVERROR_EOF)
  467. ret = 0;
  468. else if (ret == AVERROR(EAGAIN)) {
  469. /* we fully drain all the output in each decode call, so this should not
  470. * ever happen */
  471. ret = AVERROR_BUG;
  472. goto finish;
  473. } else if (ret < 0)
  474. goto finish;
  475. }
  476. while (ret >= 0) {
  477. ret = avcodec_receive_frame(avctx, frame);
  478. if (ret < 0) {
  479. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  480. ret = 0;
  481. goto finish;
  482. }
  483. if (frame != avci->compat_decode_frame) {
  484. if (!avctx->refcounted_frames) {
  485. ret = unrefcount_frame(avci, frame);
  486. if (ret < 0)
  487. goto finish;
  488. }
  489. *got_frame = 1;
  490. frame = avci->compat_decode_frame;
  491. } else {
  492. if (!avci->compat_decode_warned) {
  493. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  494. "API cannot return all the frames for this decoder. "
  495. "Some frames will be dropped. Update your code to the "
  496. "new decoding API to fix this.\n");
  497. avci->compat_decode_warned = 1;
  498. }
  499. }
  500. if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
  501. break;
  502. }
  503. finish:
  504. if (ret == 0) {
  505. /* if there are any bsfs then assume full packet is always consumed */
  506. if (avctx->codec->bsfs)
  507. ret = pkt->size;
  508. else
  509. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  510. }
  511. avci->compat_decode_consumed = 0;
  512. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  513. return ret;
  514. }
  515. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  516. int *got_picture_ptr,
  517. AVPacket *avpkt)
  518. {
  519. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  520. }
  521. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  522. AVFrame *frame,
  523. int *got_frame_ptr,
  524. AVPacket *avpkt)
  525. {
  526. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  527. }
  528. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  529. int *got_sub_ptr,
  530. AVPacket *avpkt)
  531. {
  532. int ret;
  533. ret = extract_packet_props(avctx->internal, avpkt);
  534. if (ret < 0)
  535. return ret;
  536. *got_sub_ptr = 0;
  537. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  538. if (*got_sub_ptr)
  539. avctx->frame_number++;
  540. return ret;
  541. }
  542. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
  543. const enum AVPixelFormat *fmt)
  544. {
  545. const AVPixFmtDescriptor *desc;
  546. const AVCodecHWConfig *config;
  547. int i, n;
  548. // If a device was supplied when the codec was opened, assume that the
  549. // user wants to use it.
  550. if (avctx->hw_device_ctx && avctx->codec->hw_configs) {
  551. AVHWDeviceContext *device_ctx =
  552. (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  553. for (i = 0;; i++) {
  554. config = &avctx->codec->hw_configs[i]->public;
  555. if (!config)
  556. break;
  557. if (!(config->methods &
  558. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  559. continue;
  560. if (device_ctx->type != config->device_type)
  561. continue;
  562. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
  563. if (config->pix_fmt == fmt[n])
  564. return fmt[n];
  565. }
  566. }
  567. }
  568. // No device or other setup, so we have to choose from things which
  569. // don't any other external information.
  570. // If the last element of the list is a software format, choose it
  571. // (this should be best software format if any exist).
  572. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
  573. desc = av_pix_fmt_desc_get(fmt[n - 1]);
  574. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  575. return fmt[n - 1];
  576. // Finally, traverse the list in order and choose the first entry
  577. // with no external dependencies (if there is no hardware configuration
  578. // information available then this just picks the first entry).
  579. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
  580. for (i = 0;; i++) {
  581. config = avcodec_get_hw_config(avctx->codec, i);
  582. if (!config)
  583. break;
  584. if (config->pix_fmt == fmt[n])
  585. break;
  586. }
  587. if (!config) {
  588. // No specific config available, so the decoder must be able
  589. // to handle this format without any additional setup.
  590. return fmt[n];
  591. }
  592. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
  593. // Usable with only internal setup.
  594. return fmt[n];
  595. }
  596. }
  597. // Nothing is usable, give up.
  598. return AV_PIX_FMT_NONE;
  599. }
  600. int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
  601. enum AVHWDeviceType dev_type)
  602. {
  603. AVHWDeviceContext *device_ctx;
  604. AVHWFramesContext *frames_ctx;
  605. int ret;
  606. if (!avctx->hwaccel)
  607. return AVERROR(ENOSYS);
  608. if (avctx->hw_frames_ctx)
  609. return 0;
  610. if (!avctx->hw_device_ctx) {
  611. av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
  612. "required for hardware accelerated decoding.\n");
  613. return AVERROR(EINVAL);
  614. }
  615. device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
  616. if (device_ctx->type != dev_type) {
  617. av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
  618. "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
  619. av_hwdevice_get_type_name(device_ctx->type));
  620. return AVERROR(EINVAL);
  621. }
  622. ret = avcodec_get_hw_frames_parameters(avctx,
  623. avctx->hw_device_ctx,
  624. avctx->hwaccel->pix_fmt,
  625. &avctx->hw_frames_ctx);
  626. if (ret < 0)
  627. return ret;
  628. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  629. if (frames_ctx->initial_pool_size) {
  630. // We guarantee 4 base work surfaces. The function above guarantees 1
  631. // (the absolute minimum), so add the missing count.
  632. frames_ctx->initial_pool_size += 3;
  633. // Add an additional surface per thread is frame threading is enabled.
  634. if (avctx->active_thread_type & FF_THREAD_FRAME)
  635. frames_ctx->initial_pool_size += avctx->thread_count;
  636. }
  637. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  638. if (ret < 0) {
  639. av_buffer_unref(&avctx->hw_frames_ctx);
  640. return ret;
  641. }
  642. return 0;
  643. }
  644. int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
  645. AVBufferRef *device_ref,
  646. enum AVPixelFormat hw_pix_fmt,
  647. AVBufferRef **out_frames_ref)
  648. {
  649. AVBufferRef *frames_ref = NULL;
  650. const AVCodecHWConfigInternal *hw_config;
  651. const AVHWAccel *hwa;
  652. int i, ret;
  653. for (i = 0;; i++) {
  654. hw_config = avctx->codec->hw_configs[i];
  655. if (!hw_config)
  656. return AVERROR(ENOENT);
  657. if (hw_config->public.pix_fmt == hw_pix_fmt)
  658. break;
  659. }
  660. hwa = hw_config->hwaccel;
  661. if (!hwa || !hwa->frame_params)
  662. return AVERROR(ENOENT);
  663. frames_ref = av_hwframe_ctx_alloc(device_ref);
  664. if (!frames_ref)
  665. return AVERROR(ENOMEM);
  666. ret = hwa->frame_params(avctx, frames_ref);
  667. if (ret >= 0) {
  668. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
  669. if (frames_ctx->initial_pool_size) {
  670. // If the user has requested that extra output surfaces be
  671. // available then add them here.
  672. if (avctx->extra_hw_frames > 0)
  673. frames_ctx->initial_pool_size += avctx->extra_hw_frames;
  674. }
  675. *out_frames_ref = frames_ref;
  676. } else {
  677. av_buffer_unref(&frames_ref);
  678. }
  679. return ret;
  680. }
  681. static int hwaccel_init(AVCodecContext *avctx,
  682. const AVCodecHWConfigInternal *hw_config)
  683. {
  684. const AVHWAccel *hwaccel;
  685. int err;
  686. hwaccel = hw_config->hwaccel;
  687. if (hwaccel->priv_data_size) {
  688. avctx->internal->hwaccel_priv_data =
  689. av_mallocz(hwaccel->priv_data_size);
  690. if (!avctx->internal->hwaccel_priv_data)
  691. return AVERROR(ENOMEM);
  692. }
  693. avctx->hwaccel = hwaccel;
  694. err = hwaccel->init(avctx);
  695. if (err < 0) {
  696. av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
  697. "hwaccel initialisation returned error.\n",
  698. av_get_pix_fmt_name(hw_config->public.pix_fmt));
  699. av_freep(&avctx->internal->hwaccel_priv_data);
  700. avctx->hwaccel = NULL;
  701. return err;
  702. }
  703. return 0;
  704. }
  705. static void hwaccel_uninit(AVCodecContext *avctx)
  706. {
  707. if (avctx->hwaccel && avctx->hwaccel->uninit)
  708. avctx->hwaccel->uninit(avctx);
  709. av_freep(&avctx->internal->hwaccel_priv_data);
  710. avctx->hwaccel = NULL;
  711. av_buffer_unref(&avctx->hw_frames_ctx);
  712. }
  713. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  714. {
  715. const AVPixFmtDescriptor *desc;
  716. enum AVPixelFormat *choices;
  717. enum AVPixelFormat ret, user_choice;
  718. const AVCodecHWConfigInternal *hw_config;
  719. const AVCodecHWConfig *config;
  720. int i, n, err;
  721. // Find end of list.
  722. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
  723. // Must contain at least one entry.
  724. av_assert0(n >= 1);
  725. // If a software format is available, it must be the last entry.
  726. desc = av_pix_fmt_desc_get(fmt[n - 1]);
  727. if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
  728. // No software format is available.
  729. } else {
  730. avctx->sw_pix_fmt = fmt[n - 1];
  731. }
  732. choices = av_malloc_array(n + 1, sizeof(*choices));
  733. if (!choices)
  734. return AV_PIX_FMT_NONE;
  735. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  736. for (;;) {
  737. // Remove the previous hwaccel, if there was one.
  738. hwaccel_uninit(avctx);
  739. user_choice = avctx->get_format(avctx, choices);
  740. if (user_choice == AV_PIX_FMT_NONE) {
  741. // Explicitly chose nothing, give up.
  742. ret = AV_PIX_FMT_NONE;
  743. break;
  744. }
  745. desc = av_pix_fmt_desc_get(user_choice);
  746. if (!desc) {
  747. av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
  748. "get_format() callback.\n");
  749. ret = AV_PIX_FMT_NONE;
  750. break;
  751. }
  752. av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
  753. desc->name);
  754. for (i = 0; i < n; i++) {
  755. if (choices[i] == user_choice)
  756. break;
  757. }
  758. if (i == n) {
  759. av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
  760. "%s not in possible list.\n", desc->name);
  761. break;
  762. }
  763. if (avctx->codec->hw_configs) {
  764. for (i = 0;; i++) {
  765. hw_config = avctx->codec->hw_configs[i];
  766. if (!hw_config)
  767. break;
  768. if (hw_config->public.pix_fmt == user_choice)
  769. break;
  770. }
  771. } else {
  772. hw_config = NULL;
  773. }
  774. if (!hw_config) {
  775. // No config available, so no extra setup required.
  776. ret = user_choice;
  777. break;
  778. }
  779. config = &hw_config->public;
  780. if (config->methods &
  781. AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
  782. avctx->hw_frames_ctx) {
  783. const AVHWFramesContext *frames_ctx =
  784. (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  785. if (frames_ctx->format != user_choice) {
  786. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  787. "does not match the format of the provided frames "
  788. "context.\n", desc->name);
  789. goto try_again;
  790. }
  791. } else if (config->methods &
  792. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  793. avctx->hw_device_ctx) {
  794. const AVHWDeviceContext *device_ctx =
  795. (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  796. if (device_ctx->type != config->device_type) {
  797. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  798. "does not match the type of the provided device "
  799. "context.\n", desc->name);
  800. goto try_again;
  801. }
  802. } else if (config->methods &
  803. AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
  804. // Internal-only setup, no additional configuration.
  805. } else if (config->methods &
  806. AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
  807. // Some ad-hoc configuration we can't see and can't check.
  808. } else {
  809. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  810. "missing configuration.\n", desc->name);
  811. goto try_again;
  812. }
  813. if (hw_config->hwaccel) {
  814. av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
  815. "initialisation.\n", desc->name);
  816. err = hwaccel_init(avctx, hw_config);
  817. if (err < 0)
  818. goto try_again;
  819. }
  820. ret = user_choice;
  821. break;
  822. try_again:
  823. av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
  824. "get_format() without it.\n", desc->name);
  825. for (i = 0; i < n; i++) {
  826. if (choices[i] == user_choice)
  827. break;
  828. }
  829. for (; i + 1 < n; i++)
  830. choices[i] = choices[i + 1];
  831. --n;
  832. }
  833. av_freep(&choices);
  834. return ret;
  835. }
  836. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  837. {
  838. FramePool *pool = avctx->internal->pool;
  839. int i, ret;
  840. switch (avctx->codec_type) {
  841. case AVMEDIA_TYPE_VIDEO: {
  842. uint8_t *data[4];
  843. int linesize[4];
  844. int size[4] = { 0 };
  845. int w = frame->width;
  846. int h = frame->height;
  847. int tmpsize, unaligned;
  848. if (pool->format == frame->format &&
  849. pool->width == frame->width && pool->height == frame->height)
  850. return 0;
  851. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  852. do {
  853. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  854. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  855. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  856. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  857. w += w & ~(w - 1);
  858. unaligned = 0;
  859. for (i = 0; i < 4; i++)
  860. unaligned |= linesize[i] % pool->stride_align[i];
  861. } while (unaligned);
  862. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  863. NULL, linesize);
  864. if (tmpsize < 0)
  865. return -1;
  866. for (i = 0; i < 3 && data[i + 1]; i++)
  867. size[i] = data[i + 1] - data[i];
  868. size[i] = tmpsize - (data[i] - data[0]);
  869. for (i = 0; i < 4; i++) {
  870. av_buffer_pool_uninit(&pool->pools[i]);
  871. pool->linesize[i] = linesize[i];
  872. if (size[i]) {
  873. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  874. if (!pool->pools[i]) {
  875. ret = AVERROR(ENOMEM);
  876. goto fail;
  877. }
  878. }
  879. }
  880. pool->format = frame->format;
  881. pool->width = frame->width;
  882. pool->height = frame->height;
  883. break;
  884. }
  885. case AVMEDIA_TYPE_AUDIO: {
  886. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  887. int planar = av_sample_fmt_is_planar(frame->format);
  888. int planes = planar ? ch : 1;
  889. if (pool->format == frame->format && pool->planes == planes &&
  890. pool->channels == ch && frame->nb_samples == pool->samples)
  891. return 0;
  892. av_buffer_pool_uninit(&pool->pools[0]);
  893. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  894. frame->nb_samples, frame->format, 0);
  895. if (ret < 0)
  896. goto fail;
  897. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  898. if (!pool->pools[0]) {
  899. ret = AVERROR(ENOMEM);
  900. goto fail;
  901. }
  902. pool->format = frame->format;
  903. pool->planes = planes;
  904. pool->channels = ch;
  905. pool->samples = frame->nb_samples;
  906. break;
  907. }
  908. default: av_assert0(0);
  909. }
  910. return 0;
  911. fail:
  912. for (i = 0; i < 4; i++)
  913. av_buffer_pool_uninit(&pool->pools[i]);
  914. pool->format = -1;
  915. pool->planes = pool->channels = pool->samples = 0;
  916. pool->width = pool->height = 0;
  917. return ret;
  918. }
  919. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  920. {
  921. FramePool *pool = avctx->internal->pool;
  922. int planes = pool->planes;
  923. int i;
  924. frame->linesize[0] = pool->linesize[0];
  925. if (planes > AV_NUM_DATA_POINTERS) {
  926. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  927. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  928. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  929. sizeof(*frame->extended_buf));
  930. if (!frame->extended_data || !frame->extended_buf) {
  931. av_freep(&frame->extended_data);
  932. av_freep(&frame->extended_buf);
  933. return AVERROR(ENOMEM);
  934. }
  935. } else
  936. frame->extended_data = frame->data;
  937. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  938. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  939. if (!frame->buf[i])
  940. goto fail;
  941. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  942. }
  943. for (i = 0; i < frame->nb_extended_buf; i++) {
  944. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  945. if (!frame->extended_buf[i])
  946. goto fail;
  947. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  948. }
  949. if (avctx->debug & FF_DEBUG_BUFFERS)
  950. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  951. return 0;
  952. fail:
  953. av_frame_unref(frame);
  954. return AVERROR(ENOMEM);
  955. }
  956. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  957. {
  958. FramePool *pool = s->internal->pool;
  959. int i;
  960. if (pic->data[0]) {
  961. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  962. return -1;
  963. }
  964. memset(pic->data, 0, sizeof(pic->data));
  965. pic->extended_data = pic->data;
  966. for (i = 0; i < 4 && pool->pools[i]; i++) {
  967. pic->linesize[i] = pool->linesize[i];
  968. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  969. if (!pic->buf[i])
  970. goto fail;
  971. pic->data[i] = pic->buf[i]->data;
  972. }
  973. for (; i < AV_NUM_DATA_POINTERS; i++) {
  974. pic->data[i] = NULL;
  975. pic->linesize[i] = 0;
  976. }
  977. if (pic->data[1] && !pic->data[2])
  978. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  979. if (s->debug & FF_DEBUG_BUFFERS)
  980. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  981. return 0;
  982. fail:
  983. av_frame_unref(pic);
  984. return AVERROR(ENOMEM);
  985. }
  986. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  987. {
  988. int ret;
  989. if (avctx->hw_frames_ctx) {
  990. ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  991. frame->width = avctx->coded_width;
  992. frame->height = avctx->coded_height;
  993. return ret;
  994. }
  995. if ((ret = update_frame_pool(avctx, frame)) < 0)
  996. return ret;
  997. switch (avctx->codec_type) {
  998. case AVMEDIA_TYPE_VIDEO:
  999. return video_get_buffer(avctx, frame);
  1000. case AVMEDIA_TYPE_AUDIO:
  1001. return audio_get_buffer(avctx, frame);
  1002. default:
  1003. return -1;
  1004. }
  1005. }
  1006. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  1007. {
  1008. AVPacket *pkt = avctx->internal->last_pkt_props;
  1009. int i;
  1010. struct {
  1011. enum AVPacketSideDataType packet;
  1012. enum AVFrameSideDataType frame;
  1013. } sd[] = {
  1014. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  1015. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  1016. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  1017. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  1018. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  1019. };
  1020. frame->color_primaries = avctx->color_primaries;
  1021. frame->color_trc = avctx->color_trc;
  1022. frame->colorspace = avctx->colorspace;
  1023. frame->color_range = avctx->color_range;
  1024. frame->chroma_location = avctx->chroma_sample_location;
  1025. frame->reordered_opaque = avctx->reordered_opaque;
  1026. #if FF_API_PKT_PTS
  1027. FF_DISABLE_DEPRECATION_WARNINGS
  1028. frame->pkt_pts = pkt->pts;
  1029. FF_ENABLE_DEPRECATION_WARNINGS
  1030. #endif
  1031. frame->pts = pkt->pts;
  1032. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  1033. int size;
  1034. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  1035. if (packet_sd) {
  1036. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  1037. sd[i].frame,
  1038. size);
  1039. if (!frame_sd)
  1040. return AVERROR(ENOMEM);
  1041. memcpy(frame_sd->data, packet_sd, size);
  1042. }
  1043. }
  1044. return 0;
  1045. }
  1046. static void decode_data_free(void *opaque, uint8_t *data)
  1047. {
  1048. FrameDecodeData *fdd = (FrameDecodeData*)data;
  1049. av_buffer_unref(&fdd->user_opaque_ref);
  1050. if (fdd->post_process_opaque_free)
  1051. fdd->post_process_opaque_free(fdd->post_process_opaque);
  1052. if (fdd->hwaccel_priv_free)
  1053. fdd->hwaccel_priv_free(fdd->hwaccel_priv);
  1054. av_freep(&fdd);
  1055. }
  1056. static int attach_decode_data(AVFrame *frame)
  1057. {
  1058. AVBufferRef *fdd_buf;
  1059. FrameDecodeData *fdd;
  1060. fdd = av_mallocz(sizeof(*fdd));
  1061. if (!fdd)
  1062. return AVERROR(ENOMEM);
  1063. fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
  1064. NULL, AV_BUFFER_FLAG_READONLY);
  1065. if (!fdd_buf) {
  1066. av_freep(&fdd);
  1067. return AVERROR(ENOMEM);
  1068. }
  1069. fdd->user_opaque_ref = frame->opaque_ref;
  1070. frame->opaque_ref = fdd_buf;
  1071. return 0;
  1072. }
  1073. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  1074. {
  1075. const AVHWAccel *hwaccel = avctx->hwaccel;
  1076. int override_dimensions = 1;
  1077. int ret;
  1078. switch (avctx->codec_type) {
  1079. case AVMEDIA_TYPE_VIDEO:
  1080. if (frame->width <= 0 || frame->height <= 0) {
  1081. frame->width = FFMAX(avctx->width, avctx->coded_width);
  1082. frame->height = FFMAX(avctx->height, avctx->coded_height);
  1083. override_dimensions = 0;
  1084. }
  1085. if (frame->format < 0)
  1086. frame->format = avctx->pix_fmt;
  1087. if (!frame->sample_aspect_ratio.num)
  1088. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1089. if (av_image_check_sar(frame->width, frame->height,
  1090. frame->sample_aspect_ratio) < 0) {
  1091. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1092. frame->sample_aspect_ratio.num,
  1093. frame->sample_aspect_ratio.den);
  1094. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  1095. }
  1096. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  1097. return ret;
  1098. break;
  1099. case AVMEDIA_TYPE_AUDIO:
  1100. if (!frame->sample_rate)
  1101. frame->sample_rate = avctx->sample_rate;
  1102. if (frame->format < 0)
  1103. frame->format = avctx->sample_fmt;
  1104. if (!frame->channel_layout) {
  1105. if (avctx->channel_layout) {
  1106. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  1107. avctx->channels) {
  1108. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  1109. "configuration.\n");
  1110. return AVERROR(EINVAL);
  1111. }
  1112. frame->channel_layout = avctx->channel_layout;
  1113. } else {
  1114. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1115. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  1116. avctx->channels);
  1117. return AVERROR(ENOSYS);
  1118. }
  1119. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  1120. if (!frame->channel_layout)
  1121. frame->channel_layout = (1ULL << avctx->channels) - 1;
  1122. }
  1123. }
  1124. break;
  1125. default: return AVERROR(EINVAL);
  1126. }
  1127. ret = ff_decode_frame_props(avctx, frame);
  1128. if (ret < 0)
  1129. return ret;
  1130. if (hwaccel) {
  1131. if (hwaccel->alloc_frame) {
  1132. ret = hwaccel->alloc_frame(avctx, frame);
  1133. goto end;
  1134. }
  1135. } else
  1136. avctx->sw_pix_fmt = avctx->pix_fmt;
  1137. ret = avctx->get_buffer2(avctx, frame, flags);
  1138. if (ret < 0)
  1139. goto end;
  1140. ret = attach_decode_data(frame);
  1141. if (ret < 0)
  1142. goto end;
  1143. end:
  1144. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
  1145. !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
  1146. frame->width = avctx->width;
  1147. frame->height = avctx->height;
  1148. }
  1149. if (ret < 0)
  1150. av_frame_unref(frame);
  1151. return ret;
  1152. }
  1153. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  1154. {
  1155. AVFrame *tmp;
  1156. int ret;
  1157. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  1158. if (!frame->data[0])
  1159. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1160. if (av_frame_is_writable(frame))
  1161. return ff_decode_frame_props(avctx, frame);
  1162. tmp = av_frame_alloc();
  1163. if (!tmp)
  1164. return AVERROR(ENOMEM);
  1165. av_frame_move_ref(tmp, frame);
  1166. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1167. if (ret < 0) {
  1168. av_frame_free(&tmp);
  1169. return ret;
  1170. }
  1171. av_frame_copy(frame, tmp);
  1172. av_frame_free(&tmp);
  1173. return 0;
  1174. }
  1175. void avcodec_flush_buffers(AVCodecContext *avctx)
  1176. {
  1177. avctx->internal->draining = 0;
  1178. avctx->internal->draining_done = 0;
  1179. av_frame_unref(avctx->internal->buffer_frame);
  1180. av_frame_unref(avctx->internal->compat_decode_frame);
  1181. av_packet_unref(avctx->internal->buffer_pkt);
  1182. avctx->internal->buffer_pkt_valid = 0;
  1183. av_packet_unref(avctx->internal->ds.in_pkt);
  1184. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1185. ff_thread_flush(avctx);
  1186. else if (avctx->codec->flush)
  1187. avctx->codec->flush(avctx);
  1188. ff_decode_bsfs_uninit(avctx);
  1189. if (!avctx->refcounted_frames)
  1190. av_frame_unref(avctx->internal->to_free);
  1191. }
  1192. void ff_decode_bsfs_uninit(AVCodecContext *avctx)
  1193. {
  1194. DecodeFilterContext *s = &avctx->internal->filter;
  1195. int i;
  1196. for (i = 0; i < s->nb_bsfs; i++)
  1197. av_bsf_free(&s->bsfs[i]);
  1198. av_freep(&s->bsfs);
  1199. s->nb_bsfs = 0;
  1200. }