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.

1429 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. int ff_decode_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. av_packet_unref(avci->buffer_pkt);
  379. if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
  380. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  381. if (ret < 0)
  382. return ret;
  383. }
  384. ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
  385. if (ret < 0) {
  386. av_packet_unref(avci->buffer_pkt);
  387. return ret;
  388. }
  389. if (!avci->buffer_frame->buf[0]) {
  390. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  391. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
  397. {
  398. /* make sure we are noisy about decoders returning invalid cropping data */
  399. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  400. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  401. (frame->crop_left + frame->crop_right) >= frame->width ||
  402. (frame->crop_top + frame->crop_bottom) >= frame->height) {
  403. av_log(avctx, AV_LOG_WARNING,
  404. "Invalid cropping information set by a decoder: %zu/%zu/%zu/%zu "
  405. "(frame size %dx%d). This is a bug, please report it\n",
  406. frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
  407. frame->width, frame->height);
  408. frame->crop_left = 0;
  409. frame->crop_right = 0;
  410. frame->crop_top = 0;
  411. frame->crop_bottom = 0;
  412. return 0;
  413. }
  414. if (!avctx->apply_cropping)
  415. return 0;
  416. return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
  417. AV_FRAME_CROP_UNALIGNED : 0);
  418. }
  419. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  420. {
  421. AVCodecInternal *avci = avctx->internal;
  422. int ret;
  423. av_frame_unref(frame);
  424. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  425. return AVERROR(EINVAL);
  426. if (avci->buffer_frame->buf[0]) {
  427. av_frame_move_ref(frame, avci->buffer_frame);
  428. } else {
  429. ret = decode_receive_frame_internal(avctx, frame);
  430. if (ret < 0)
  431. return ret;
  432. }
  433. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  434. ret = apply_cropping(avctx, frame);
  435. if (ret < 0) {
  436. av_frame_unref(frame);
  437. return ret;
  438. }
  439. }
  440. avctx->frame_number++;
  441. return 0;
  442. }
  443. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  444. int *got_frame, AVPacket *pkt)
  445. {
  446. AVCodecInternal *avci = avctx->internal;
  447. int ret = 0;
  448. av_assert0(avci->compat_decode_consumed == 0);
  449. *got_frame = 0;
  450. avci->compat_decode = 1;
  451. if (avci->compat_decode_partial_size > 0 &&
  452. avci->compat_decode_partial_size != pkt->size) {
  453. av_log(avctx, AV_LOG_ERROR,
  454. "Got unexpected packet size after a partial decode\n");
  455. ret = AVERROR(EINVAL);
  456. goto finish;
  457. }
  458. if (!avci->compat_decode_partial_size) {
  459. ret = avcodec_send_packet(avctx, pkt);
  460. if (ret == AVERROR_EOF)
  461. ret = 0;
  462. else if (ret == AVERROR(EAGAIN)) {
  463. /* we fully drain all the output in each decode call, so this should not
  464. * ever happen */
  465. ret = AVERROR_BUG;
  466. goto finish;
  467. } else if (ret < 0)
  468. goto finish;
  469. }
  470. while (ret >= 0) {
  471. ret = avcodec_receive_frame(avctx, frame);
  472. if (ret < 0) {
  473. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  474. ret = 0;
  475. goto finish;
  476. }
  477. if (frame != avci->compat_decode_frame) {
  478. if (!avctx->refcounted_frames) {
  479. ret = unrefcount_frame(avci, frame);
  480. if (ret < 0)
  481. goto finish;
  482. }
  483. *got_frame = 1;
  484. frame = avci->compat_decode_frame;
  485. } else {
  486. if (!avci->compat_decode_warned) {
  487. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  488. "API cannot return all the frames for this decoder. "
  489. "Some frames will be dropped. Update your code to the "
  490. "new decoding API to fix this.\n");
  491. avci->compat_decode_warned = 1;
  492. }
  493. }
  494. if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
  495. break;
  496. }
  497. finish:
  498. if (ret == 0) {
  499. /* if there are any bsfs then assume full packet is always consumed */
  500. if (avctx->codec->bsfs)
  501. ret = pkt->size;
  502. else
  503. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  504. }
  505. avci->compat_decode_consumed = 0;
  506. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  507. return ret;
  508. }
  509. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  510. int *got_picture_ptr,
  511. AVPacket *avpkt)
  512. {
  513. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  514. }
  515. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  516. AVFrame *frame,
  517. int *got_frame_ptr,
  518. AVPacket *avpkt)
  519. {
  520. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  521. }
  522. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  523. int *got_sub_ptr,
  524. AVPacket *avpkt)
  525. {
  526. int ret;
  527. ret = extract_packet_props(avctx->internal, avpkt);
  528. if (ret < 0)
  529. return ret;
  530. *got_sub_ptr = 0;
  531. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  532. if (*got_sub_ptr)
  533. avctx->frame_number++;
  534. return ret;
  535. }
  536. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
  537. const enum AVPixelFormat *fmt)
  538. {
  539. const AVPixFmtDescriptor *desc;
  540. const AVCodecHWConfig *config;
  541. int i, n;
  542. // If a device was supplied when the codec was opened, assume that the
  543. // user wants to use it.
  544. if (avctx->hw_device_ctx && avctx->codec->hw_configs) {
  545. AVHWDeviceContext *device_ctx =
  546. (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  547. for (i = 0;; i++) {
  548. config = &avctx->codec->hw_configs[i]->public;
  549. if (!config)
  550. break;
  551. if (!(config->methods &
  552. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  553. continue;
  554. if (device_ctx->type != config->device_type)
  555. continue;
  556. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
  557. if (config->pix_fmt == fmt[n])
  558. return fmt[n];
  559. }
  560. }
  561. }
  562. // No device or other setup, so we have to choose from things which
  563. // don't any other external information.
  564. // If the last element of the list is a software format, choose it
  565. // (this should be best software format if any exist).
  566. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
  567. desc = av_pix_fmt_desc_get(fmt[n - 1]);
  568. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  569. return fmt[n - 1];
  570. // Finally, traverse the list in order and choose the first entry
  571. // with no external dependencies (if there is no hardware configuration
  572. // information available then this just picks the first entry).
  573. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
  574. for (i = 0;; i++) {
  575. config = avcodec_get_hw_config(avctx->codec, i);
  576. if (!config)
  577. break;
  578. if (config->pix_fmt == fmt[n])
  579. break;
  580. }
  581. if (!config) {
  582. // No specific config available, so the decoder must be able
  583. // to handle this format without any additional setup.
  584. return fmt[n];
  585. }
  586. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
  587. // Usable with only internal setup.
  588. return fmt[n];
  589. }
  590. }
  591. // Nothing is usable, give up.
  592. return AV_PIX_FMT_NONE;
  593. }
  594. int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
  595. enum AVHWDeviceType dev_type)
  596. {
  597. AVHWDeviceContext *device_ctx;
  598. AVHWFramesContext *frames_ctx;
  599. int ret;
  600. if (!avctx->hwaccel)
  601. return AVERROR(ENOSYS);
  602. if (avctx->hw_frames_ctx)
  603. return 0;
  604. if (!avctx->hw_device_ctx) {
  605. av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
  606. "required for hardware accelerated decoding.\n");
  607. return AVERROR(EINVAL);
  608. }
  609. device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
  610. if (device_ctx->type != dev_type) {
  611. av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
  612. "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
  613. av_hwdevice_get_type_name(device_ctx->type));
  614. return AVERROR(EINVAL);
  615. }
  616. ret = avcodec_get_hw_frames_parameters(avctx,
  617. avctx->hw_device_ctx,
  618. avctx->hwaccel->pix_fmt,
  619. &avctx->hw_frames_ctx);
  620. if (ret < 0)
  621. return ret;
  622. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  623. if (frames_ctx->initial_pool_size) {
  624. // We guarantee 4 base work surfaces. The function above guarantees 1
  625. // (the absolute minimum), so add the missing count.
  626. frames_ctx->initial_pool_size += 3;
  627. }
  628. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  629. if (ret < 0) {
  630. av_buffer_unref(&avctx->hw_frames_ctx);
  631. return ret;
  632. }
  633. return 0;
  634. }
  635. int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
  636. AVBufferRef *device_ref,
  637. enum AVPixelFormat hw_pix_fmt,
  638. AVBufferRef **out_frames_ref)
  639. {
  640. AVBufferRef *frames_ref = NULL;
  641. const AVCodecHWConfigInternal *hw_config;
  642. const AVHWAccel *hwa;
  643. int i, ret;
  644. for (i = 0;; i++) {
  645. hw_config = avctx->codec->hw_configs[i];
  646. if (!hw_config)
  647. return AVERROR(ENOENT);
  648. if (hw_config->public.pix_fmt == hw_pix_fmt)
  649. break;
  650. }
  651. hwa = hw_config->hwaccel;
  652. if (!hwa || !hwa->frame_params)
  653. return AVERROR(ENOENT);
  654. frames_ref = av_hwframe_ctx_alloc(device_ref);
  655. if (!frames_ref)
  656. return AVERROR(ENOMEM);
  657. ret = hwa->frame_params(avctx, frames_ref);
  658. if (ret >= 0) {
  659. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
  660. if (frames_ctx->initial_pool_size) {
  661. // If the user has requested that extra output surfaces be
  662. // available then add them here.
  663. if (avctx->extra_hw_frames > 0)
  664. frames_ctx->initial_pool_size += avctx->extra_hw_frames;
  665. // If frame threading is enabled then an extra surface per thread
  666. // is also required.
  667. if (avctx->active_thread_type & FF_THREAD_FRAME)
  668. frames_ctx->initial_pool_size += avctx->thread_count;
  669. }
  670. *out_frames_ref = frames_ref;
  671. } else {
  672. av_buffer_unref(&frames_ref);
  673. }
  674. return ret;
  675. }
  676. static int hwaccel_init(AVCodecContext *avctx,
  677. const AVCodecHWConfigInternal *hw_config)
  678. {
  679. const AVHWAccel *hwaccel;
  680. int err;
  681. hwaccel = hw_config->hwaccel;
  682. if (hwaccel->priv_data_size) {
  683. avctx->internal->hwaccel_priv_data =
  684. av_mallocz(hwaccel->priv_data_size);
  685. if (!avctx->internal->hwaccel_priv_data)
  686. return AVERROR(ENOMEM);
  687. }
  688. avctx->hwaccel = hwaccel;
  689. err = hwaccel->init(avctx);
  690. if (err < 0) {
  691. av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
  692. "hwaccel initialisation returned error.\n",
  693. av_get_pix_fmt_name(hw_config->public.pix_fmt));
  694. av_freep(&avctx->internal->hwaccel_priv_data);
  695. avctx->hwaccel = NULL;
  696. return err;
  697. }
  698. return 0;
  699. }
  700. static void hwaccel_uninit(AVCodecContext *avctx)
  701. {
  702. if (avctx->hwaccel && avctx->hwaccel->uninit)
  703. avctx->hwaccel->uninit(avctx);
  704. av_freep(&avctx->internal->hwaccel_priv_data);
  705. avctx->hwaccel = NULL;
  706. av_buffer_unref(&avctx->hw_frames_ctx);
  707. }
  708. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  709. {
  710. const AVPixFmtDescriptor *desc;
  711. enum AVPixelFormat *choices;
  712. enum AVPixelFormat ret, user_choice;
  713. const AVCodecHWConfigInternal *hw_config;
  714. const AVCodecHWConfig *config;
  715. int i, n, err;
  716. // Find end of list.
  717. for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
  718. // Must contain at least one entry.
  719. av_assert0(n >= 1);
  720. // If a software format is available, it must be the last entry.
  721. desc = av_pix_fmt_desc_get(fmt[n - 1]);
  722. if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
  723. // No software format is available.
  724. } else {
  725. avctx->sw_pix_fmt = fmt[n - 1];
  726. }
  727. choices = av_malloc_array(n + 1, sizeof(*choices));
  728. if (!choices)
  729. return AV_PIX_FMT_NONE;
  730. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  731. for (;;) {
  732. // Remove the previous hwaccel, if there was one.
  733. hwaccel_uninit(avctx);
  734. user_choice = avctx->get_format(avctx, choices);
  735. if (user_choice == AV_PIX_FMT_NONE) {
  736. // Explicitly chose nothing, give up.
  737. ret = AV_PIX_FMT_NONE;
  738. break;
  739. }
  740. desc = av_pix_fmt_desc_get(user_choice);
  741. if (!desc) {
  742. av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
  743. "get_format() callback.\n");
  744. ret = AV_PIX_FMT_NONE;
  745. break;
  746. }
  747. av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
  748. desc->name);
  749. for (i = 0; i < n; i++) {
  750. if (choices[i] == user_choice)
  751. break;
  752. }
  753. if (i == n) {
  754. av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
  755. "%s not in possible list.\n", desc->name);
  756. break;
  757. }
  758. if (avctx->codec->hw_configs) {
  759. for (i = 0;; i++) {
  760. hw_config = avctx->codec->hw_configs[i];
  761. if (!hw_config)
  762. break;
  763. if (hw_config->public.pix_fmt == user_choice)
  764. break;
  765. }
  766. } else {
  767. hw_config = NULL;
  768. }
  769. if (!hw_config) {
  770. // No config available, so no extra setup required.
  771. ret = user_choice;
  772. break;
  773. }
  774. config = &hw_config->public;
  775. if (config->methods &
  776. AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
  777. avctx->hw_frames_ctx) {
  778. const AVHWFramesContext *frames_ctx =
  779. (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  780. if (frames_ctx->format != user_choice) {
  781. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  782. "does not match the format of the provided frames "
  783. "context.\n", desc->name);
  784. goto try_again;
  785. }
  786. } else if (config->methods &
  787. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  788. avctx->hw_device_ctx) {
  789. const AVHWDeviceContext *device_ctx =
  790. (AVHWDeviceContext*)avctx->hw_device_ctx->data;
  791. if (device_ctx->type != config->device_type) {
  792. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  793. "does not match the type of the provided device "
  794. "context.\n", desc->name);
  795. goto try_again;
  796. }
  797. } else if (config->methods &
  798. AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
  799. // Internal-only setup, no additional configuration.
  800. } else if (config->methods &
  801. AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
  802. // Some ad-hoc configuration we can't see and can't check.
  803. } else {
  804. av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
  805. "missing configuration.\n", desc->name);
  806. goto try_again;
  807. }
  808. if (hw_config->hwaccel) {
  809. av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
  810. "initialisation.\n", desc->name);
  811. err = hwaccel_init(avctx, hw_config);
  812. if (err < 0)
  813. goto try_again;
  814. }
  815. ret = user_choice;
  816. break;
  817. try_again:
  818. av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
  819. "get_format() without it.\n", desc->name);
  820. for (i = 0; i < n; i++) {
  821. if (choices[i] == user_choice)
  822. break;
  823. }
  824. for (; i + 1 < n; i++)
  825. choices[i] = choices[i + 1];
  826. --n;
  827. }
  828. av_freep(&choices);
  829. return ret;
  830. }
  831. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  832. {
  833. FramePool *pool = avctx->internal->pool;
  834. int i, ret;
  835. switch (avctx->codec_type) {
  836. case AVMEDIA_TYPE_VIDEO: {
  837. uint8_t *data[4];
  838. int linesize[4];
  839. int size[4] = { 0 };
  840. int w = frame->width;
  841. int h = frame->height;
  842. int tmpsize, unaligned;
  843. if (pool->format == frame->format &&
  844. pool->width == frame->width && pool->height == frame->height)
  845. return 0;
  846. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  847. do {
  848. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  849. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  850. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  851. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  852. w += w & ~(w - 1);
  853. unaligned = 0;
  854. for (i = 0; i < 4; i++)
  855. unaligned |= linesize[i] % pool->stride_align[i];
  856. } while (unaligned);
  857. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  858. NULL, linesize);
  859. if (tmpsize < 0)
  860. return -1;
  861. for (i = 0; i < 3 && data[i + 1]; i++)
  862. size[i] = data[i + 1] - data[i];
  863. size[i] = tmpsize - (data[i] - data[0]);
  864. for (i = 0; i < 4; i++) {
  865. av_buffer_pool_uninit(&pool->pools[i]);
  866. pool->linesize[i] = linesize[i];
  867. if (size[i]) {
  868. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  869. if (!pool->pools[i]) {
  870. ret = AVERROR(ENOMEM);
  871. goto fail;
  872. }
  873. }
  874. }
  875. pool->format = frame->format;
  876. pool->width = frame->width;
  877. pool->height = frame->height;
  878. break;
  879. }
  880. case AVMEDIA_TYPE_AUDIO: {
  881. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  882. int planar = av_sample_fmt_is_planar(frame->format);
  883. int planes = planar ? ch : 1;
  884. if (pool->format == frame->format && pool->planes == planes &&
  885. pool->channels == ch && frame->nb_samples == pool->samples)
  886. return 0;
  887. av_buffer_pool_uninit(&pool->pools[0]);
  888. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  889. frame->nb_samples, frame->format, 0);
  890. if (ret < 0)
  891. goto fail;
  892. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  893. if (!pool->pools[0]) {
  894. ret = AVERROR(ENOMEM);
  895. goto fail;
  896. }
  897. pool->format = frame->format;
  898. pool->planes = planes;
  899. pool->channels = ch;
  900. pool->samples = frame->nb_samples;
  901. break;
  902. }
  903. default: av_assert0(0);
  904. }
  905. return 0;
  906. fail:
  907. for (i = 0; i < 4; i++)
  908. av_buffer_pool_uninit(&pool->pools[i]);
  909. pool->format = -1;
  910. pool->planes = pool->channels = pool->samples = 0;
  911. pool->width = pool->height = 0;
  912. return ret;
  913. }
  914. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  915. {
  916. FramePool *pool = avctx->internal->pool;
  917. int planes = pool->planes;
  918. int i;
  919. frame->linesize[0] = pool->linesize[0];
  920. if (planes > AV_NUM_DATA_POINTERS) {
  921. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  922. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  923. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  924. sizeof(*frame->extended_buf));
  925. if (!frame->extended_data || !frame->extended_buf) {
  926. av_freep(&frame->extended_data);
  927. av_freep(&frame->extended_buf);
  928. return AVERROR(ENOMEM);
  929. }
  930. } else
  931. frame->extended_data = frame->data;
  932. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  933. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  934. if (!frame->buf[i])
  935. goto fail;
  936. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  937. }
  938. for (i = 0; i < frame->nb_extended_buf; i++) {
  939. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  940. if (!frame->extended_buf[i])
  941. goto fail;
  942. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  943. }
  944. if (avctx->debug & FF_DEBUG_BUFFERS)
  945. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  946. return 0;
  947. fail:
  948. av_frame_unref(frame);
  949. return AVERROR(ENOMEM);
  950. }
  951. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  952. {
  953. FramePool *pool = s->internal->pool;
  954. int i;
  955. if (pic->data[0]) {
  956. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  957. return -1;
  958. }
  959. memset(pic->data, 0, sizeof(pic->data));
  960. pic->extended_data = pic->data;
  961. for (i = 0; i < 4 && pool->pools[i]; i++) {
  962. pic->linesize[i] = pool->linesize[i];
  963. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  964. if (!pic->buf[i])
  965. goto fail;
  966. pic->data[i] = pic->buf[i]->data;
  967. }
  968. for (; i < AV_NUM_DATA_POINTERS; i++) {
  969. pic->data[i] = NULL;
  970. pic->linesize[i] = 0;
  971. }
  972. if (pic->data[1] && !pic->data[2])
  973. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  974. if (s->debug & FF_DEBUG_BUFFERS)
  975. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  976. return 0;
  977. fail:
  978. av_frame_unref(pic);
  979. return AVERROR(ENOMEM);
  980. }
  981. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  982. {
  983. int ret;
  984. if (avctx->hw_frames_ctx) {
  985. ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  986. frame->width = avctx->coded_width;
  987. frame->height = avctx->coded_height;
  988. return ret;
  989. }
  990. if ((ret = update_frame_pool(avctx, frame)) < 0)
  991. return ret;
  992. switch (avctx->codec_type) {
  993. case AVMEDIA_TYPE_VIDEO:
  994. return video_get_buffer(avctx, frame);
  995. case AVMEDIA_TYPE_AUDIO:
  996. return audio_get_buffer(avctx, frame);
  997. default:
  998. return -1;
  999. }
  1000. }
  1001. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  1002. {
  1003. AVPacket *pkt = avctx->internal->last_pkt_props;
  1004. int i;
  1005. struct {
  1006. enum AVPacketSideDataType packet;
  1007. enum AVFrameSideDataType frame;
  1008. } sd[] = {
  1009. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  1010. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  1011. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  1012. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  1013. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  1014. };
  1015. frame->color_primaries = avctx->color_primaries;
  1016. frame->color_trc = avctx->color_trc;
  1017. frame->colorspace = avctx->colorspace;
  1018. frame->color_range = avctx->color_range;
  1019. frame->chroma_location = avctx->chroma_sample_location;
  1020. frame->reordered_opaque = avctx->reordered_opaque;
  1021. #if FF_API_PKT_PTS
  1022. FF_DISABLE_DEPRECATION_WARNINGS
  1023. frame->pkt_pts = pkt->pts;
  1024. FF_ENABLE_DEPRECATION_WARNINGS
  1025. #endif
  1026. frame->pts = pkt->pts;
  1027. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  1028. int size;
  1029. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  1030. if (packet_sd) {
  1031. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  1032. sd[i].frame,
  1033. size);
  1034. if (!frame_sd)
  1035. return AVERROR(ENOMEM);
  1036. memcpy(frame_sd->data, packet_sd, size);
  1037. }
  1038. }
  1039. return 0;
  1040. }
  1041. static void decode_data_free(void *opaque, uint8_t *data)
  1042. {
  1043. FrameDecodeData *fdd = (FrameDecodeData*)data;
  1044. av_buffer_unref(&fdd->user_opaque_ref);
  1045. if (fdd->post_process_opaque_free)
  1046. fdd->post_process_opaque_free(fdd->post_process_opaque);
  1047. if (fdd->hwaccel_priv_free)
  1048. fdd->hwaccel_priv_free(fdd->hwaccel_priv);
  1049. av_freep(&fdd);
  1050. }
  1051. static int attach_decode_data(AVFrame *frame)
  1052. {
  1053. AVBufferRef *fdd_buf;
  1054. FrameDecodeData *fdd;
  1055. fdd = av_mallocz(sizeof(*fdd));
  1056. if (!fdd)
  1057. return AVERROR(ENOMEM);
  1058. fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
  1059. NULL, AV_BUFFER_FLAG_READONLY);
  1060. if (!fdd_buf) {
  1061. av_freep(&fdd);
  1062. return AVERROR(ENOMEM);
  1063. }
  1064. fdd->user_opaque_ref = frame->opaque_ref;
  1065. frame->opaque_ref = fdd_buf;
  1066. return 0;
  1067. }
  1068. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  1069. {
  1070. const AVHWAccel *hwaccel = avctx->hwaccel;
  1071. int override_dimensions = 1;
  1072. int ret;
  1073. switch (avctx->codec_type) {
  1074. case AVMEDIA_TYPE_VIDEO:
  1075. if (frame->width <= 0 || frame->height <= 0) {
  1076. frame->width = FFMAX(avctx->width, avctx->coded_width);
  1077. frame->height = FFMAX(avctx->height, avctx->coded_height);
  1078. override_dimensions = 0;
  1079. }
  1080. if (frame->format < 0)
  1081. frame->format = avctx->pix_fmt;
  1082. if (!frame->sample_aspect_ratio.num)
  1083. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1084. if (av_image_check_sar(frame->width, frame->height,
  1085. frame->sample_aspect_ratio) < 0) {
  1086. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  1087. frame->sample_aspect_ratio.num,
  1088. frame->sample_aspect_ratio.den);
  1089. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  1090. }
  1091. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  1092. return ret;
  1093. break;
  1094. case AVMEDIA_TYPE_AUDIO:
  1095. if (!frame->sample_rate)
  1096. frame->sample_rate = avctx->sample_rate;
  1097. if (frame->format < 0)
  1098. frame->format = avctx->sample_fmt;
  1099. if (!frame->channel_layout) {
  1100. if (avctx->channel_layout) {
  1101. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  1102. avctx->channels) {
  1103. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  1104. "configuration.\n");
  1105. return AVERROR(EINVAL);
  1106. }
  1107. frame->channel_layout = avctx->channel_layout;
  1108. } else {
  1109. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1110. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  1111. avctx->channels);
  1112. return AVERROR(ENOSYS);
  1113. }
  1114. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  1115. if (!frame->channel_layout)
  1116. frame->channel_layout = (1ULL << avctx->channels) - 1;
  1117. }
  1118. }
  1119. break;
  1120. default: return AVERROR(EINVAL);
  1121. }
  1122. ret = ff_decode_frame_props(avctx, frame);
  1123. if (ret < 0)
  1124. return ret;
  1125. if (hwaccel) {
  1126. if (hwaccel->alloc_frame) {
  1127. ret = hwaccel->alloc_frame(avctx, frame);
  1128. goto end;
  1129. }
  1130. } else
  1131. avctx->sw_pix_fmt = avctx->pix_fmt;
  1132. ret = avctx->get_buffer2(avctx, frame, flags);
  1133. if (ret < 0)
  1134. goto end;
  1135. ret = attach_decode_data(frame);
  1136. if (ret < 0)
  1137. goto end;
  1138. end:
  1139. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
  1140. !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
  1141. frame->width = avctx->width;
  1142. frame->height = avctx->height;
  1143. }
  1144. if (ret < 0)
  1145. av_frame_unref(frame);
  1146. return ret;
  1147. }
  1148. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  1149. {
  1150. AVFrame *tmp;
  1151. int ret;
  1152. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  1153. if (!frame->data[0])
  1154. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1155. if (av_frame_is_writable(frame))
  1156. return ff_decode_frame_props(avctx, frame);
  1157. tmp = av_frame_alloc();
  1158. if (!tmp)
  1159. return AVERROR(ENOMEM);
  1160. av_frame_move_ref(tmp, frame);
  1161. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  1162. if (ret < 0) {
  1163. av_frame_free(&tmp);
  1164. return ret;
  1165. }
  1166. av_frame_copy(frame, tmp);
  1167. av_frame_free(&tmp);
  1168. return 0;
  1169. }
  1170. static void bsfs_flush(AVCodecContext *avctx)
  1171. {
  1172. DecodeFilterContext *s = &avctx->internal->filter;
  1173. for (int i = 0; i < s->nb_bsfs; i++)
  1174. av_bsf_flush(s->bsfs[i]);
  1175. }
  1176. void avcodec_flush_buffers(AVCodecContext *avctx)
  1177. {
  1178. avctx->internal->draining = 0;
  1179. avctx->internal->draining_done = 0;
  1180. av_frame_unref(avctx->internal->buffer_frame);
  1181. av_frame_unref(avctx->internal->compat_decode_frame);
  1182. av_packet_unref(avctx->internal->buffer_pkt);
  1183. avctx->internal->buffer_pkt_valid = 0;
  1184. av_packet_unref(avctx->internal->ds.in_pkt);
  1185. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1186. ff_thread_flush(avctx);
  1187. else if (avctx->codec->flush)
  1188. avctx->codec->flush(avctx);
  1189. bsfs_flush(avctx);
  1190. if (!avctx->refcounted_frames)
  1191. av_frame_unref(avctx->internal->to_free);
  1192. }
  1193. void ff_decode_bsfs_uninit(AVCodecContext *avctx)
  1194. {
  1195. DecodeFilterContext *s = &avctx->internal->filter;
  1196. int i;
  1197. for (i = 0; i < s->nb_bsfs; i++)
  1198. av_bsf_free(&s->bsfs[i]);
  1199. av_freep(&s->bsfs);
  1200. s->nb_bsfs = 0;
  1201. }