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.

1103 lines
32KB

  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 "avcodec.h"
  30. #include "bytestream.h"
  31. #include "decode.h"
  32. #include "internal.h"
  33. #include "thread.h"
  34. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  35. {
  36. int size = 0, ret;
  37. const uint8_t *data;
  38. uint32_t flags;
  39. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  40. if (!data)
  41. return 0;
  42. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  43. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  44. "changes, but PARAM_CHANGE side data was sent to it.\n");
  45. ret = AVERROR(EINVAL);
  46. goto fail2;
  47. }
  48. if (size < 4)
  49. goto fail;
  50. flags = bytestream_get_le32(&data);
  51. size -= 4;
  52. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  53. if (size < 4)
  54. goto fail;
  55. avctx->channels = bytestream_get_le32(&data);
  56. size -= 4;
  57. }
  58. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  59. if (size < 8)
  60. goto fail;
  61. avctx->channel_layout = bytestream_get_le64(&data);
  62. size -= 8;
  63. }
  64. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  65. if (size < 4)
  66. goto fail;
  67. avctx->sample_rate = bytestream_get_le32(&data);
  68. size -= 4;
  69. }
  70. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  71. if (size < 8)
  72. goto fail;
  73. avctx->width = bytestream_get_le32(&data);
  74. avctx->height = bytestream_get_le32(&data);
  75. size -= 8;
  76. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  77. if (ret < 0)
  78. goto fail2;
  79. }
  80. return 0;
  81. fail:
  82. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  83. ret = AVERROR_INVALIDDATA;
  84. fail2:
  85. if (ret < 0) {
  86. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  87. if (avctx->err_recognition & AV_EF_EXPLODE)
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
  93. {
  94. av_packet_unref(avci->last_pkt_props);
  95. if (pkt)
  96. return av_packet_copy_props(avci->last_pkt_props, pkt);
  97. return 0;
  98. }
  99. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  100. {
  101. int ret;
  102. /* move the original frame to our backup */
  103. av_frame_unref(avci->to_free);
  104. av_frame_move_ref(avci->to_free, frame);
  105. /* now copy everything except the AVBufferRefs back
  106. * note that we make a COPY of the side data, so calling av_frame_free() on
  107. * the caller's frame will work properly */
  108. ret = av_frame_copy_props(frame, avci->to_free);
  109. if (ret < 0)
  110. return ret;
  111. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  112. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  113. if (avci->to_free->extended_data != avci->to_free->data) {
  114. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  115. int size = planes * sizeof(*frame->extended_data);
  116. if (!size) {
  117. av_frame_unref(frame);
  118. return AVERROR_BUG;
  119. }
  120. frame->extended_data = av_malloc(size);
  121. if (!frame->extended_data) {
  122. av_frame_unref(frame);
  123. return AVERROR(ENOMEM);
  124. }
  125. memcpy(frame->extended_data, avci->to_free->extended_data,
  126. size);
  127. } else
  128. frame->extended_data = frame->data;
  129. frame->format = avci->to_free->format;
  130. frame->width = avci->to_free->width;
  131. frame->height = avci->to_free->height;
  132. frame->channel_layout = avci->to_free->channel_layout;
  133. frame->nb_samples = avci->to_free->nb_samples;
  134. return 0;
  135. }
  136. static int bsfs_init(AVCodecContext *avctx)
  137. {
  138. AVCodecInternal *avci = avctx->internal;
  139. DecodeFilterContext *s = &avci->filter;
  140. const char *bsfs_str;
  141. int ret;
  142. if (s->nb_bsfs)
  143. return 0;
  144. bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
  145. while (bsfs_str && *bsfs_str) {
  146. AVBSFContext **tmp;
  147. const AVBitStreamFilter *filter;
  148. char *bsf;
  149. bsf = av_get_token(&bsfs_str, ",");
  150. if (!bsf) {
  151. ret = AVERROR(ENOMEM);
  152. goto fail;
  153. }
  154. filter = av_bsf_get_by_name(bsf);
  155. if (!filter) {
  156. av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
  157. "requested by a decoder. This is a bug, please report it.\n",
  158. bsf);
  159. ret = AVERROR_BUG;
  160. av_freep(&bsf);
  161. goto fail;
  162. }
  163. av_freep(&bsf);
  164. tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
  165. if (!tmp) {
  166. ret = AVERROR(ENOMEM);
  167. goto fail;
  168. }
  169. s->bsfs = tmp;
  170. s->nb_bsfs++;
  171. ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
  172. if (ret < 0)
  173. goto fail;
  174. if (s->nb_bsfs == 1) {
  175. /* We do not currently have an API for passing the input timebase into decoders,
  176. * but no filters used here should actually need it.
  177. * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
  178. s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
  179. ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
  180. avctx);
  181. } else {
  182. s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
  183. ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
  184. s->bsfs[s->nb_bsfs - 2]->par_out);
  185. }
  186. if (ret < 0)
  187. goto fail;
  188. ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
  189. if (ret < 0)
  190. goto fail;
  191. }
  192. return 0;
  193. fail:
  194. ff_decode_bsfs_uninit(avctx);
  195. return ret;
  196. }
  197. /* try to get one output packet from the filter chain */
  198. static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
  199. {
  200. DecodeFilterContext *s = &avctx->internal->filter;
  201. int idx, ret;
  202. /* start with the last filter in the chain */
  203. idx = s->nb_bsfs - 1;
  204. while (idx >= 0) {
  205. /* request a packet from the currently selected filter */
  206. ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
  207. if (ret == AVERROR(EAGAIN)) {
  208. /* no packets available, try the next filter up the chain */
  209. ret = 0;
  210. idx--;
  211. continue;
  212. } else if (ret < 0 && ret != AVERROR_EOF) {
  213. return ret;
  214. }
  215. /* got a packet or EOF -- pass it to the caller or to the next filter
  216. * down the chain */
  217. if (idx == s->nb_bsfs - 1) {
  218. return ret;
  219. } else {
  220. idx++;
  221. ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
  222. if (ret < 0) {
  223. av_log(avctx, AV_LOG_ERROR,
  224. "Error pre-processing a packet before decoding\n");
  225. av_packet_unref(pkt);
  226. return ret;
  227. }
  228. }
  229. }
  230. return AVERROR(EAGAIN);
  231. }
  232. int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
  233. {
  234. AVCodecInternal *avci = avctx->internal;
  235. int ret;
  236. if (avci->draining)
  237. return AVERROR_EOF;
  238. ret = bsfs_poll(avctx, pkt);
  239. if (ret == AVERROR_EOF)
  240. avci->draining = 1;
  241. if (ret < 0)
  242. return ret;
  243. ret = extract_packet_props(avctx->internal, pkt);
  244. if (ret < 0)
  245. goto finish;
  246. ret = apply_param_change(avctx, pkt);
  247. if (ret < 0)
  248. goto finish;
  249. if (avctx->codec->receive_frame)
  250. avci->compat_decode_consumed += pkt->size;
  251. return 0;
  252. finish:
  253. av_packet_unref(pkt);
  254. return ret;
  255. }
  256. /*
  257. * The core of the receive_frame_wrapper for the decoders implementing
  258. * the simple API. Certain decoders might consume partial packets without
  259. * returning any output, so this function needs to be called in a loop until it
  260. * returns EAGAIN.
  261. **/
  262. static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
  263. {
  264. AVCodecInternal *avci = avctx->internal;
  265. DecodeSimpleContext *ds = &avci->ds;
  266. AVPacket *pkt = ds->in_pkt;
  267. int got_frame;
  268. int ret;
  269. if (!pkt->data && !avci->draining) {
  270. av_packet_unref(pkt);
  271. ret = ff_decode_get_packet(avctx, pkt);
  272. if (ret < 0 && ret != AVERROR_EOF)
  273. return ret;
  274. }
  275. // Some codecs (at least wma lossless) will crash when feeding drain packets
  276. // after EOF was signaled.
  277. if (avci->draining_done)
  278. return AVERROR_EOF;
  279. if (!pkt->data &&
  280. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
  281. avctx->active_thread_type & FF_THREAD_FRAME))
  282. return AVERROR_EOF;
  283. got_frame = 0;
  284. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
  285. ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
  286. } else {
  287. ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
  288. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  289. frame->pkt_dts = pkt->dts;
  290. /* get_buffer is supposed to set frame parameters */
  291. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  292. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  293. frame->width = avctx->width;
  294. frame->height = avctx->height;
  295. frame->format = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
  296. avctx->pix_fmt : avctx->sample_fmt;
  297. }
  298. }
  299. emms_c();
  300. if (!got_frame)
  301. av_frame_unref(frame);
  302. if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
  303. ret = pkt->size;
  304. #if FF_API_AVCTX_TIMEBASE
  305. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  306. avctx->time_base = av_inv_q(avctx->framerate);
  307. #endif
  308. if (avctx->internal->draining && !got_frame)
  309. avci->draining_done = 1;
  310. avci->compat_decode_consumed += ret;
  311. if (ret >= pkt->size || ret < 0) {
  312. av_packet_unref(pkt);
  313. } else {
  314. int consumed = ret;
  315. pkt->data += consumed;
  316. pkt->size -= consumed;
  317. pkt->pts = AV_NOPTS_VALUE;
  318. pkt->dts = AV_NOPTS_VALUE;
  319. avci->last_pkt_props->pts = AV_NOPTS_VALUE;
  320. avci->last_pkt_props->dts = AV_NOPTS_VALUE;
  321. }
  322. if (got_frame)
  323. av_assert0(frame->buf[0]);
  324. return ret < 0 ? ret : 0;
  325. }
  326. static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  327. {
  328. int ret;
  329. while (!frame->buf[0]) {
  330. ret = decode_simple_internal(avctx, frame);
  331. if (ret < 0)
  332. return ret;
  333. }
  334. return 0;
  335. }
  336. static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
  337. {
  338. AVCodecInternal *avci = avctx->internal;
  339. int ret;
  340. av_assert0(!frame->buf[0]);
  341. if (avctx->codec->receive_frame)
  342. ret = avctx->codec->receive_frame(avctx, frame);
  343. else
  344. ret = decode_simple_receive_frame(avctx, frame);
  345. if (ret == AVERROR_EOF)
  346. avci->draining_done = 1;
  347. return ret;
  348. }
  349. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  350. {
  351. AVCodecInternal *avci = avctx->internal;
  352. int ret = 0;
  353. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  354. return AVERROR(EINVAL);
  355. if (avctx->internal->draining)
  356. return AVERROR_EOF;
  357. ret = bsfs_init(avctx);
  358. if (ret < 0)
  359. return ret;
  360. av_packet_unref(avci->buffer_pkt);
  361. if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
  362. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  363. if (ret < 0)
  364. return ret;
  365. }
  366. ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
  367. if (ret < 0) {
  368. av_packet_unref(avci->buffer_pkt);
  369. return ret;
  370. }
  371. if (!avci->buffer_frame->buf[0]) {
  372. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  373. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  374. return ret;
  375. }
  376. return 0;
  377. }
  378. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  379. {
  380. AVCodecInternal *avci = avctx->internal;
  381. int ret;
  382. av_frame_unref(frame);
  383. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  384. return AVERROR(EINVAL);
  385. ret = bsfs_init(avctx);
  386. if (ret < 0)
  387. return ret;
  388. if (avci->buffer_frame->buf[0]) {
  389. av_frame_move_ref(frame, avci->buffer_frame);
  390. } else {
  391. ret = decode_receive_frame_internal(avctx, frame);
  392. if (ret < 0)
  393. return ret;
  394. }
  395. avctx->frame_number++;
  396. return 0;
  397. }
  398. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  399. int *got_frame, AVPacket *pkt)
  400. {
  401. AVCodecInternal *avci = avctx->internal;
  402. int ret;
  403. av_assert0(avci->compat_decode_consumed == 0);
  404. *got_frame = 0;
  405. avci->compat_decode = 1;
  406. if (avci->compat_decode_partial_size > 0 &&
  407. avci->compat_decode_partial_size != pkt->size) {
  408. av_log(avctx, AV_LOG_ERROR,
  409. "Got unexpected packet size after a partial decode\n");
  410. ret = AVERROR(EINVAL);
  411. goto finish;
  412. }
  413. if (!avci->compat_decode_partial_size) {
  414. ret = avcodec_send_packet(avctx, pkt);
  415. if (ret == AVERROR_EOF)
  416. ret = 0;
  417. else if (ret == AVERROR(EAGAIN)) {
  418. /* we fully drain all the output in each decode call, so this should not
  419. * ever happen */
  420. ret = AVERROR_BUG;
  421. goto finish;
  422. } else if (ret < 0)
  423. goto finish;
  424. }
  425. while (ret >= 0) {
  426. ret = avcodec_receive_frame(avctx, frame);
  427. if (ret < 0) {
  428. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  429. ret = 0;
  430. goto finish;
  431. }
  432. if (frame != avci->compat_decode_frame) {
  433. if (!avctx->refcounted_frames) {
  434. ret = unrefcount_frame(avci, frame);
  435. if (ret < 0)
  436. goto finish;
  437. }
  438. *got_frame = 1;
  439. frame = avci->compat_decode_frame;
  440. } else {
  441. if (!avci->compat_decode_warned) {
  442. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  443. "API cannot return all the frames for this decoder. "
  444. "Some frames will be dropped. Update your code to the "
  445. "new decoding API to fix this.\n");
  446. avci->compat_decode_warned = 1;
  447. }
  448. }
  449. if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
  450. break;
  451. }
  452. finish:
  453. if (ret == 0) {
  454. /* if there are any bsfs then assume full packet is always consumed */
  455. if (avctx->codec->bsfs)
  456. ret = pkt->size;
  457. else
  458. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  459. }
  460. avci->compat_decode_consumed = 0;
  461. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  462. return ret;
  463. }
  464. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  465. int *got_picture_ptr,
  466. AVPacket *avpkt)
  467. {
  468. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  469. }
  470. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  471. AVFrame *frame,
  472. int *got_frame_ptr,
  473. AVPacket *avpkt)
  474. {
  475. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  476. }
  477. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  478. int *got_sub_ptr,
  479. AVPacket *avpkt)
  480. {
  481. int ret;
  482. ret = extract_packet_props(avctx->internal, avpkt);
  483. if (ret < 0)
  484. return ret;
  485. *got_sub_ptr = 0;
  486. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  487. if (*got_sub_ptr)
  488. avctx->frame_number++;
  489. return ret;
  490. }
  491. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  492. {
  493. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  494. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  495. }
  496. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  497. {
  498. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  499. ++fmt;
  500. return fmt[0];
  501. }
  502. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  503. enum AVPixelFormat pix_fmt)
  504. {
  505. AVHWAccel *hwaccel = NULL;
  506. while ((hwaccel = av_hwaccel_next(hwaccel)))
  507. if (hwaccel->id == codec_id
  508. && hwaccel->pix_fmt == pix_fmt)
  509. return hwaccel;
  510. return NULL;
  511. }
  512. static int setup_hwaccel(AVCodecContext *avctx,
  513. const enum AVPixelFormat fmt,
  514. const char *name)
  515. {
  516. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  517. int ret = 0;
  518. if (!hwa) {
  519. av_log(avctx, AV_LOG_ERROR,
  520. "Could not find an AVHWAccel for the pixel format: %s",
  521. name);
  522. return AVERROR(ENOENT);
  523. }
  524. if (hwa->priv_data_size) {
  525. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  526. if (!avctx->internal->hwaccel_priv_data)
  527. return AVERROR(ENOMEM);
  528. }
  529. if (hwa->init) {
  530. ret = hwa->init(avctx);
  531. if (ret < 0) {
  532. av_freep(&avctx->internal->hwaccel_priv_data);
  533. return ret;
  534. }
  535. }
  536. avctx->hwaccel = hwa;
  537. return 0;
  538. }
  539. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  540. {
  541. const AVPixFmtDescriptor *desc;
  542. enum AVPixelFormat *choices;
  543. enum AVPixelFormat ret;
  544. unsigned n = 0;
  545. while (fmt[n] != AV_PIX_FMT_NONE)
  546. ++n;
  547. av_assert0(n >= 1);
  548. avctx->sw_pix_fmt = fmt[n - 1];
  549. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  550. choices = av_malloc_array(n + 1, sizeof(*choices));
  551. if (!choices)
  552. return AV_PIX_FMT_NONE;
  553. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  554. for (;;) {
  555. if (avctx->hwaccel && avctx->hwaccel->uninit)
  556. avctx->hwaccel->uninit(avctx);
  557. av_freep(&avctx->internal->hwaccel_priv_data);
  558. avctx->hwaccel = NULL;
  559. av_buffer_unref(&avctx->hw_frames_ctx);
  560. ret = avctx->get_format(avctx, choices);
  561. desc = av_pix_fmt_desc_get(ret);
  562. if (!desc) {
  563. ret = AV_PIX_FMT_NONE;
  564. break;
  565. }
  566. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  567. break;
  568. if (avctx->hw_frames_ctx) {
  569. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  570. if (hw_frames_ctx->format != ret) {
  571. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  572. "does not match the format of provided AVHWFramesContext\n");
  573. ret = AV_PIX_FMT_NONE;
  574. break;
  575. }
  576. }
  577. if (!setup_hwaccel(avctx, ret, desc->name))
  578. break;
  579. /* Remove failed hwaccel from choices */
  580. for (n = 0; choices[n] != ret; n++)
  581. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  582. do
  583. choices[n] = choices[n + 1];
  584. while (choices[n++] != AV_PIX_FMT_NONE);
  585. }
  586. av_freep(&choices);
  587. return ret;
  588. }
  589. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  590. {
  591. FramePool *pool = avctx->internal->pool;
  592. int i, ret;
  593. switch (avctx->codec_type) {
  594. case AVMEDIA_TYPE_VIDEO: {
  595. uint8_t *data[4];
  596. int linesize[4];
  597. int size[4] = { 0 };
  598. int w = frame->width;
  599. int h = frame->height;
  600. int tmpsize, unaligned;
  601. if (pool->format == frame->format &&
  602. pool->width == frame->width && pool->height == frame->height)
  603. return 0;
  604. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  605. do {
  606. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  607. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  608. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  609. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  610. w += w & ~(w - 1);
  611. unaligned = 0;
  612. for (i = 0; i < 4; i++)
  613. unaligned |= linesize[i] % pool->stride_align[i];
  614. } while (unaligned);
  615. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  616. NULL, linesize);
  617. if (tmpsize < 0)
  618. return -1;
  619. for (i = 0; i < 3 && data[i + 1]; i++)
  620. size[i] = data[i + 1] - data[i];
  621. size[i] = tmpsize - (data[i] - data[0]);
  622. for (i = 0; i < 4; i++) {
  623. av_buffer_pool_uninit(&pool->pools[i]);
  624. pool->linesize[i] = linesize[i];
  625. if (size[i]) {
  626. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  627. if (!pool->pools[i]) {
  628. ret = AVERROR(ENOMEM);
  629. goto fail;
  630. }
  631. }
  632. }
  633. pool->format = frame->format;
  634. pool->width = frame->width;
  635. pool->height = frame->height;
  636. break;
  637. }
  638. case AVMEDIA_TYPE_AUDIO: {
  639. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  640. int planar = av_sample_fmt_is_planar(frame->format);
  641. int planes = planar ? ch : 1;
  642. if (pool->format == frame->format && pool->planes == planes &&
  643. pool->channels == ch && frame->nb_samples == pool->samples)
  644. return 0;
  645. av_buffer_pool_uninit(&pool->pools[0]);
  646. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  647. frame->nb_samples, frame->format, 0);
  648. if (ret < 0)
  649. goto fail;
  650. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  651. if (!pool->pools[0]) {
  652. ret = AVERROR(ENOMEM);
  653. goto fail;
  654. }
  655. pool->format = frame->format;
  656. pool->planes = planes;
  657. pool->channels = ch;
  658. pool->samples = frame->nb_samples;
  659. break;
  660. }
  661. default: av_assert0(0);
  662. }
  663. return 0;
  664. fail:
  665. for (i = 0; i < 4; i++)
  666. av_buffer_pool_uninit(&pool->pools[i]);
  667. pool->format = -1;
  668. pool->planes = pool->channels = pool->samples = 0;
  669. pool->width = pool->height = 0;
  670. return ret;
  671. }
  672. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  673. {
  674. FramePool *pool = avctx->internal->pool;
  675. int planes = pool->planes;
  676. int i;
  677. frame->linesize[0] = pool->linesize[0];
  678. if (planes > AV_NUM_DATA_POINTERS) {
  679. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  680. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  681. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  682. sizeof(*frame->extended_buf));
  683. if (!frame->extended_data || !frame->extended_buf) {
  684. av_freep(&frame->extended_data);
  685. av_freep(&frame->extended_buf);
  686. return AVERROR(ENOMEM);
  687. }
  688. } else
  689. frame->extended_data = frame->data;
  690. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  691. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  692. if (!frame->buf[i])
  693. goto fail;
  694. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  695. }
  696. for (i = 0; i < frame->nb_extended_buf; i++) {
  697. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  698. if (!frame->extended_buf[i])
  699. goto fail;
  700. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  701. }
  702. if (avctx->debug & FF_DEBUG_BUFFERS)
  703. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  704. return 0;
  705. fail:
  706. av_frame_unref(frame);
  707. return AVERROR(ENOMEM);
  708. }
  709. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  710. {
  711. FramePool *pool = s->internal->pool;
  712. int i;
  713. if (pic->data[0]) {
  714. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  715. return -1;
  716. }
  717. memset(pic->data, 0, sizeof(pic->data));
  718. pic->extended_data = pic->data;
  719. for (i = 0; i < 4 && pool->pools[i]; i++) {
  720. pic->linesize[i] = pool->linesize[i];
  721. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  722. if (!pic->buf[i])
  723. goto fail;
  724. pic->data[i] = pic->buf[i]->data;
  725. }
  726. for (; i < AV_NUM_DATA_POINTERS; i++) {
  727. pic->data[i] = NULL;
  728. pic->linesize[i] = 0;
  729. }
  730. if (pic->data[1] && !pic->data[2])
  731. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  732. if (s->debug & FF_DEBUG_BUFFERS)
  733. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  734. return 0;
  735. fail:
  736. av_frame_unref(pic);
  737. return AVERROR(ENOMEM);
  738. }
  739. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  740. {
  741. int ret;
  742. if (avctx->hw_frames_ctx)
  743. return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  744. if ((ret = update_frame_pool(avctx, frame)) < 0)
  745. return ret;
  746. switch (avctx->codec_type) {
  747. case AVMEDIA_TYPE_VIDEO:
  748. return video_get_buffer(avctx, frame);
  749. case AVMEDIA_TYPE_AUDIO:
  750. return audio_get_buffer(avctx, frame);
  751. default:
  752. return -1;
  753. }
  754. }
  755. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  756. {
  757. AVPacket *pkt = avctx->internal->last_pkt_props;
  758. int i;
  759. struct {
  760. enum AVPacketSideDataType packet;
  761. enum AVFrameSideDataType frame;
  762. } sd[] = {
  763. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  764. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  765. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  766. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  767. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  768. };
  769. frame->color_primaries = avctx->color_primaries;
  770. frame->color_trc = avctx->color_trc;
  771. frame->colorspace = avctx->colorspace;
  772. frame->color_range = avctx->color_range;
  773. frame->chroma_location = avctx->chroma_sample_location;
  774. frame->reordered_opaque = avctx->reordered_opaque;
  775. #if FF_API_PKT_PTS
  776. FF_DISABLE_DEPRECATION_WARNINGS
  777. frame->pkt_pts = pkt->pts;
  778. FF_ENABLE_DEPRECATION_WARNINGS
  779. #endif
  780. frame->pts = pkt->pts;
  781. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  782. int size;
  783. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  784. if (packet_sd) {
  785. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  786. sd[i].frame,
  787. size);
  788. if (!frame_sd)
  789. return AVERROR(ENOMEM);
  790. memcpy(frame_sd->data, packet_sd, size);
  791. }
  792. }
  793. return 0;
  794. }
  795. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  796. {
  797. const AVHWAccel *hwaccel = avctx->hwaccel;
  798. int override_dimensions = 1;
  799. int ret;
  800. switch (avctx->codec_type) {
  801. case AVMEDIA_TYPE_VIDEO:
  802. if (frame->width <= 0 || frame->height <= 0) {
  803. frame->width = FFMAX(avctx->width, avctx->coded_width);
  804. frame->height = FFMAX(avctx->height, avctx->coded_height);
  805. override_dimensions = 0;
  806. }
  807. if (frame->format < 0)
  808. frame->format = avctx->pix_fmt;
  809. if (!frame->sample_aspect_ratio.num)
  810. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  811. if (av_image_check_sar(frame->width, frame->height,
  812. frame->sample_aspect_ratio) < 0) {
  813. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  814. frame->sample_aspect_ratio.num,
  815. frame->sample_aspect_ratio.den);
  816. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  817. }
  818. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  819. return ret;
  820. break;
  821. case AVMEDIA_TYPE_AUDIO:
  822. if (!frame->sample_rate)
  823. frame->sample_rate = avctx->sample_rate;
  824. if (frame->format < 0)
  825. frame->format = avctx->sample_fmt;
  826. if (!frame->channel_layout) {
  827. if (avctx->channel_layout) {
  828. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  829. avctx->channels) {
  830. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  831. "configuration.\n");
  832. return AVERROR(EINVAL);
  833. }
  834. frame->channel_layout = avctx->channel_layout;
  835. } else {
  836. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  837. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  838. avctx->channels);
  839. return AVERROR(ENOSYS);
  840. }
  841. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  842. if (!frame->channel_layout)
  843. frame->channel_layout = (1ULL << avctx->channels) - 1;
  844. }
  845. }
  846. break;
  847. default: return AVERROR(EINVAL);
  848. }
  849. ret = ff_decode_frame_props(avctx, frame);
  850. if (ret < 0)
  851. return ret;
  852. if (hwaccel) {
  853. if (hwaccel->alloc_frame) {
  854. ret = hwaccel->alloc_frame(avctx, frame);
  855. goto end;
  856. }
  857. } else
  858. avctx->sw_pix_fmt = avctx->pix_fmt;
  859. ret = avctx->get_buffer2(avctx, frame, flags);
  860. end:
  861. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  862. frame->width = avctx->width;
  863. frame->height = avctx->height;
  864. }
  865. return ret;
  866. }
  867. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  868. {
  869. AVFrame *tmp;
  870. int ret;
  871. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  872. if (!frame->data[0])
  873. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  874. if (av_frame_is_writable(frame))
  875. return ff_decode_frame_props(avctx, frame);
  876. tmp = av_frame_alloc();
  877. if (!tmp)
  878. return AVERROR(ENOMEM);
  879. av_frame_move_ref(tmp, frame);
  880. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  881. if (ret < 0) {
  882. av_frame_free(&tmp);
  883. return ret;
  884. }
  885. av_frame_copy(frame, tmp);
  886. av_frame_free(&tmp);
  887. return 0;
  888. }
  889. void avcodec_flush_buffers(AVCodecContext *avctx)
  890. {
  891. avctx->internal->draining = 0;
  892. avctx->internal->draining_done = 0;
  893. av_frame_unref(avctx->internal->buffer_frame);
  894. av_frame_unref(avctx->internal->compat_decode_frame);
  895. av_packet_unref(avctx->internal->buffer_pkt);
  896. avctx->internal->buffer_pkt_valid = 0;
  897. av_packet_unref(avctx->internal->ds.in_pkt);
  898. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  899. ff_thread_flush(avctx);
  900. else if (avctx->codec->flush)
  901. avctx->codec->flush(avctx);
  902. ff_decode_bsfs_uninit(avctx);
  903. if (!avctx->refcounted_frames)
  904. av_frame_unref(avctx->internal->to_free);
  905. }
  906. void ff_decode_bsfs_uninit(AVCodecContext *avctx)
  907. {
  908. DecodeFilterContext *s = &avctx->internal->filter;
  909. int i;
  910. for (i = 0; i < s->nb_bsfs; i++)
  911. av_bsf_free(&s->bsfs[i]);
  912. av_freep(&s->bsfs);
  913. s->nb_bsfs = 0;
  914. }