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.

964 lines
28KB

  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. int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
  137. {
  138. AVCodecInternal *avci = avctx->internal;
  139. int ret;
  140. if (avci->draining)
  141. return AVERROR_EOF;
  142. if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data_elems)
  143. return AVERROR(EAGAIN);
  144. av_packet_move_ref(pkt, avci->buffer_pkt);
  145. ret = extract_packet_props(avctx->internal, pkt);
  146. if (ret < 0)
  147. goto finish;
  148. ret = apply_param_change(avctx, pkt);
  149. if (ret < 0)
  150. goto finish;
  151. if (avctx->codec->receive_frame)
  152. avci->compat_decode_consumed += pkt->size;
  153. return 0;
  154. finish:
  155. av_packet_unref(pkt);
  156. return ret;
  157. }
  158. /*
  159. * The core of the receive_frame_wrapper for the decoders implementing
  160. * the simple API. Certain decoders might consume partial packets without
  161. * returning any output, so this function needs to be called in a loop until it
  162. * returns EAGAIN.
  163. **/
  164. static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
  165. {
  166. AVCodecInternal *avci = avctx->internal;
  167. DecodeSimpleContext *ds = &avci->ds;
  168. AVPacket *pkt = ds->in_pkt;
  169. int got_frame;
  170. int ret;
  171. if (!pkt->data && !avci->draining) {
  172. av_packet_unref(pkt);
  173. ret = ff_decode_get_packet(avctx, pkt);
  174. if (ret < 0 && ret != AVERROR_EOF)
  175. return ret;
  176. }
  177. // Some codecs (at least wma lossless) will crash when feeding drain packets
  178. // after EOF was signaled.
  179. if (avci->draining_done)
  180. return AVERROR_EOF;
  181. if (!pkt->data &&
  182. !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
  183. avctx->active_thread_type & FF_THREAD_FRAME))
  184. return AVERROR_EOF;
  185. got_frame = 0;
  186. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
  187. ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
  188. } else {
  189. ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
  190. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  191. frame->pkt_dts = pkt->dts;
  192. /* get_buffer is supposed to set frame parameters */
  193. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  194. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  195. frame->width = avctx->width;
  196. frame->height = avctx->height;
  197. frame->format = avctx->codec->type == AVMEDIA_TYPE_VIDEO ?
  198. avctx->pix_fmt : avctx->sample_fmt;
  199. }
  200. }
  201. emms_c();
  202. if (!got_frame)
  203. av_frame_unref(frame);
  204. if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO)
  205. ret = pkt->size;
  206. #if FF_API_AVCTX_TIMEBASE
  207. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  208. avctx->time_base = av_inv_q(avctx->framerate);
  209. #endif
  210. if (avctx->internal->draining && !got_frame)
  211. avci->draining_done = 1;
  212. avci->compat_decode_consumed += ret;
  213. if (ret >= pkt->size || ret < 0) {
  214. av_packet_unref(pkt);
  215. } else {
  216. int consumed = ret;
  217. pkt->data += consumed;
  218. pkt->size -= consumed;
  219. pkt->pts = AV_NOPTS_VALUE;
  220. pkt->dts = AV_NOPTS_VALUE;
  221. avci->last_pkt_props->pts = AV_NOPTS_VALUE;
  222. avci->last_pkt_props->dts = AV_NOPTS_VALUE;
  223. }
  224. if (got_frame)
  225. av_assert0(frame->buf[0]);
  226. return ret < 0 ? ret : 0;
  227. }
  228. static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  229. {
  230. int ret;
  231. while (!frame->buf[0]) {
  232. ret = decode_simple_internal(avctx, frame);
  233. if (ret < 0)
  234. return ret;
  235. }
  236. return 0;
  237. }
  238. static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
  239. {
  240. AVCodecInternal *avci = avctx->internal;
  241. int ret;
  242. av_assert0(!frame->buf[0]);
  243. if (avctx->codec->receive_frame)
  244. ret = avctx->codec->receive_frame(avctx, frame);
  245. else
  246. ret = decode_simple_receive_frame(avctx, frame);
  247. if (ret == AVERROR_EOF)
  248. avci->draining_done = 1;
  249. return ret;
  250. }
  251. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  252. {
  253. AVCodecInternal *avci = avctx->internal;
  254. int ret = 0;
  255. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  256. return AVERROR(EINVAL);
  257. if (avctx->internal->draining)
  258. return AVERROR_EOF;
  259. if (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems)
  260. return AVERROR(EAGAIN);
  261. if (!avpkt || !avpkt->size) {
  262. avctx->internal->draining = 1;
  263. } else {
  264. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  265. if (ret < 0)
  266. return ret;
  267. }
  268. if (!avci->buffer_frame->buf[0]) {
  269. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  270. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  271. return ret;
  272. }
  273. return 0;
  274. }
  275. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  276. {
  277. AVCodecInternal *avci = avctx->internal;
  278. int ret;
  279. av_frame_unref(frame);
  280. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  281. return AVERROR(EINVAL);
  282. if (avci->buffer_frame->buf[0]) {
  283. av_frame_move_ref(frame, avci->buffer_frame);
  284. } else {
  285. ret = decode_receive_frame_internal(avctx, frame);
  286. if (ret < 0)
  287. return ret;
  288. }
  289. avctx->frame_number++;
  290. return 0;
  291. }
  292. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  293. int *got_frame, AVPacket *pkt)
  294. {
  295. AVCodecInternal *avci = avctx->internal;
  296. int ret;
  297. av_assert0(avci->compat_decode_consumed == 0);
  298. *got_frame = 0;
  299. avci->compat_decode = 1;
  300. if (avci->compat_decode_partial_size > 0 &&
  301. avci->compat_decode_partial_size != pkt->size) {
  302. av_log(avctx, AV_LOG_ERROR,
  303. "Got unexpected packet size after a partial decode\n");
  304. ret = AVERROR(EINVAL);
  305. goto finish;
  306. }
  307. if (!avci->compat_decode_partial_size) {
  308. ret = avcodec_send_packet(avctx, pkt);
  309. if (ret == AVERROR_EOF)
  310. ret = 0;
  311. else if (ret == AVERROR(EAGAIN)) {
  312. /* we fully drain all the output in each decode call, so this should not
  313. * ever happen */
  314. ret = AVERROR_BUG;
  315. goto finish;
  316. } else if (ret < 0)
  317. goto finish;
  318. }
  319. while (ret >= 0) {
  320. ret = avcodec_receive_frame(avctx, frame);
  321. if (ret < 0) {
  322. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  323. ret = 0;
  324. goto finish;
  325. }
  326. if (frame != avci->compat_decode_frame) {
  327. if (!avctx->refcounted_frames) {
  328. ret = unrefcount_frame(avci, frame);
  329. if (ret < 0)
  330. goto finish;
  331. }
  332. *got_frame = 1;
  333. frame = avci->compat_decode_frame;
  334. } else {
  335. if (!avci->compat_decode_warned) {
  336. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  337. "API cannot return all the frames for this decoder. "
  338. "Some frames will be dropped. Update your code to the "
  339. "new decoding API to fix this.\n");
  340. avci->compat_decode_warned = 1;
  341. }
  342. }
  343. if (avci->draining || avci->compat_decode_consumed < pkt->size)
  344. break;
  345. }
  346. finish:
  347. if (ret == 0)
  348. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  349. avci->compat_decode_consumed = 0;
  350. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  351. return ret;
  352. }
  353. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  354. int *got_picture_ptr,
  355. AVPacket *avpkt)
  356. {
  357. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  358. }
  359. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  360. AVFrame *frame,
  361. int *got_frame_ptr,
  362. AVPacket *avpkt)
  363. {
  364. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  365. }
  366. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  367. int *got_sub_ptr,
  368. AVPacket *avpkt)
  369. {
  370. int ret;
  371. ret = extract_packet_props(avctx->internal, avpkt);
  372. if (ret < 0)
  373. return ret;
  374. *got_sub_ptr = 0;
  375. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  376. if (*got_sub_ptr)
  377. avctx->frame_number++;
  378. return ret;
  379. }
  380. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  381. {
  382. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  383. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  384. }
  385. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  386. {
  387. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  388. ++fmt;
  389. return fmt[0];
  390. }
  391. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  392. enum AVPixelFormat pix_fmt)
  393. {
  394. AVHWAccel *hwaccel = NULL;
  395. while ((hwaccel = av_hwaccel_next(hwaccel)))
  396. if (hwaccel->id == codec_id
  397. && hwaccel->pix_fmt == pix_fmt)
  398. return hwaccel;
  399. return NULL;
  400. }
  401. static int setup_hwaccel(AVCodecContext *avctx,
  402. const enum AVPixelFormat fmt,
  403. const char *name)
  404. {
  405. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  406. int ret = 0;
  407. if (!hwa) {
  408. av_log(avctx, AV_LOG_ERROR,
  409. "Could not find an AVHWAccel for the pixel format: %s",
  410. name);
  411. return AVERROR(ENOENT);
  412. }
  413. if (hwa->priv_data_size) {
  414. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  415. if (!avctx->internal->hwaccel_priv_data)
  416. return AVERROR(ENOMEM);
  417. }
  418. if (hwa->init) {
  419. ret = hwa->init(avctx);
  420. if (ret < 0) {
  421. av_freep(&avctx->internal->hwaccel_priv_data);
  422. return ret;
  423. }
  424. }
  425. avctx->hwaccel = hwa;
  426. return 0;
  427. }
  428. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  429. {
  430. const AVPixFmtDescriptor *desc;
  431. enum AVPixelFormat *choices;
  432. enum AVPixelFormat ret;
  433. unsigned n = 0;
  434. while (fmt[n] != AV_PIX_FMT_NONE)
  435. ++n;
  436. av_assert0(n >= 1);
  437. avctx->sw_pix_fmt = fmt[n - 1];
  438. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  439. choices = av_malloc_array(n + 1, sizeof(*choices));
  440. if (!choices)
  441. return AV_PIX_FMT_NONE;
  442. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  443. for (;;) {
  444. if (avctx->hwaccel && avctx->hwaccel->uninit)
  445. avctx->hwaccel->uninit(avctx);
  446. av_freep(&avctx->internal->hwaccel_priv_data);
  447. avctx->hwaccel = NULL;
  448. av_buffer_unref(&avctx->hw_frames_ctx);
  449. ret = avctx->get_format(avctx, choices);
  450. desc = av_pix_fmt_desc_get(ret);
  451. if (!desc) {
  452. ret = AV_PIX_FMT_NONE;
  453. break;
  454. }
  455. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  456. break;
  457. if (avctx->hw_frames_ctx) {
  458. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  459. if (hw_frames_ctx->format != ret) {
  460. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  461. "does not match the format of provided AVHWFramesContext\n");
  462. ret = AV_PIX_FMT_NONE;
  463. break;
  464. }
  465. }
  466. if (!setup_hwaccel(avctx, ret, desc->name))
  467. break;
  468. /* Remove failed hwaccel from choices */
  469. for (n = 0; choices[n] != ret; n++)
  470. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  471. do
  472. choices[n] = choices[n + 1];
  473. while (choices[n++] != AV_PIX_FMT_NONE);
  474. }
  475. av_freep(&choices);
  476. return ret;
  477. }
  478. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  479. {
  480. FramePool *pool = avctx->internal->pool;
  481. int i, ret;
  482. switch (avctx->codec_type) {
  483. case AVMEDIA_TYPE_VIDEO: {
  484. uint8_t *data[4];
  485. int linesize[4];
  486. int size[4] = { 0 };
  487. int w = frame->width;
  488. int h = frame->height;
  489. int tmpsize, unaligned;
  490. if (pool->format == frame->format &&
  491. pool->width == frame->width && pool->height == frame->height)
  492. return 0;
  493. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  494. do {
  495. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  496. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  497. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  498. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  499. w += w & ~(w - 1);
  500. unaligned = 0;
  501. for (i = 0; i < 4; i++)
  502. unaligned |= linesize[i] % pool->stride_align[i];
  503. } while (unaligned);
  504. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  505. NULL, linesize);
  506. if (tmpsize < 0)
  507. return -1;
  508. for (i = 0; i < 3 && data[i + 1]; i++)
  509. size[i] = data[i + 1] - data[i];
  510. size[i] = tmpsize - (data[i] - data[0]);
  511. for (i = 0; i < 4; i++) {
  512. av_buffer_pool_uninit(&pool->pools[i]);
  513. pool->linesize[i] = linesize[i];
  514. if (size[i]) {
  515. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  516. if (!pool->pools[i]) {
  517. ret = AVERROR(ENOMEM);
  518. goto fail;
  519. }
  520. }
  521. }
  522. pool->format = frame->format;
  523. pool->width = frame->width;
  524. pool->height = frame->height;
  525. break;
  526. }
  527. case AVMEDIA_TYPE_AUDIO: {
  528. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  529. int planar = av_sample_fmt_is_planar(frame->format);
  530. int planes = planar ? ch : 1;
  531. if (pool->format == frame->format && pool->planes == planes &&
  532. pool->channels == ch && frame->nb_samples == pool->samples)
  533. return 0;
  534. av_buffer_pool_uninit(&pool->pools[0]);
  535. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  536. frame->nb_samples, frame->format, 0);
  537. if (ret < 0)
  538. goto fail;
  539. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  540. if (!pool->pools[0]) {
  541. ret = AVERROR(ENOMEM);
  542. goto fail;
  543. }
  544. pool->format = frame->format;
  545. pool->planes = planes;
  546. pool->channels = ch;
  547. pool->samples = frame->nb_samples;
  548. break;
  549. }
  550. default: av_assert0(0);
  551. }
  552. return 0;
  553. fail:
  554. for (i = 0; i < 4; i++)
  555. av_buffer_pool_uninit(&pool->pools[i]);
  556. pool->format = -1;
  557. pool->planes = pool->channels = pool->samples = 0;
  558. pool->width = pool->height = 0;
  559. return ret;
  560. }
  561. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  562. {
  563. FramePool *pool = avctx->internal->pool;
  564. int planes = pool->planes;
  565. int i;
  566. frame->linesize[0] = pool->linesize[0];
  567. if (planes > AV_NUM_DATA_POINTERS) {
  568. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  569. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  570. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  571. sizeof(*frame->extended_buf));
  572. if (!frame->extended_data || !frame->extended_buf) {
  573. av_freep(&frame->extended_data);
  574. av_freep(&frame->extended_buf);
  575. return AVERROR(ENOMEM);
  576. }
  577. } else
  578. frame->extended_data = frame->data;
  579. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  580. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  581. if (!frame->buf[i])
  582. goto fail;
  583. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  584. }
  585. for (i = 0; i < frame->nb_extended_buf; i++) {
  586. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  587. if (!frame->extended_buf[i])
  588. goto fail;
  589. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  590. }
  591. if (avctx->debug & FF_DEBUG_BUFFERS)
  592. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  593. return 0;
  594. fail:
  595. av_frame_unref(frame);
  596. return AVERROR(ENOMEM);
  597. }
  598. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  599. {
  600. FramePool *pool = s->internal->pool;
  601. int i;
  602. if (pic->data[0]) {
  603. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  604. return -1;
  605. }
  606. memset(pic->data, 0, sizeof(pic->data));
  607. pic->extended_data = pic->data;
  608. for (i = 0; i < 4 && pool->pools[i]; i++) {
  609. pic->linesize[i] = pool->linesize[i];
  610. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  611. if (!pic->buf[i])
  612. goto fail;
  613. pic->data[i] = pic->buf[i]->data;
  614. }
  615. for (; i < AV_NUM_DATA_POINTERS; i++) {
  616. pic->data[i] = NULL;
  617. pic->linesize[i] = 0;
  618. }
  619. if (pic->data[1] && !pic->data[2])
  620. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  621. if (s->debug & FF_DEBUG_BUFFERS)
  622. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  623. return 0;
  624. fail:
  625. av_frame_unref(pic);
  626. return AVERROR(ENOMEM);
  627. }
  628. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  629. {
  630. int ret;
  631. if (avctx->hw_frames_ctx)
  632. return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  633. if ((ret = update_frame_pool(avctx, frame)) < 0)
  634. return ret;
  635. switch (avctx->codec_type) {
  636. case AVMEDIA_TYPE_VIDEO:
  637. return video_get_buffer(avctx, frame);
  638. case AVMEDIA_TYPE_AUDIO:
  639. return audio_get_buffer(avctx, frame);
  640. default:
  641. return -1;
  642. }
  643. }
  644. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  645. {
  646. AVPacket *pkt = avctx->internal->last_pkt_props;
  647. int i;
  648. struct {
  649. enum AVPacketSideDataType packet;
  650. enum AVFrameSideDataType frame;
  651. } sd[] = {
  652. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  653. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  654. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  655. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  656. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  657. };
  658. frame->color_primaries = avctx->color_primaries;
  659. frame->color_trc = avctx->color_trc;
  660. frame->colorspace = avctx->colorspace;
  661. frame->color_range = avctx->color_range;
  662. frame->chroma_location = avctx->chroma_sample_location;
  663. frame->reordered_opaque = avctx->reordered_opaque;
  664. #if FF_API_PKT_PTS
  665. FF_DISABLE_DEPRECATION_WARNINGS
  666. frame->pkt_pts = pkt->pts;
  667. FF_ENABLE_DEPRECATION_WARNINGS
  668. #endif
  669. frame->pts = pkt->pts;
  670. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  671. int size;
  672. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  673. if (packet_sd) {
  674. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  675. sd[i].frame,
  676. size);
  677. if (!frame_sd)
  678. return AVERROR(ENOMEM);
  679. memcpy(frame_sd->data, packet_sd, size);
  680. }
  681. }
  682. return 0;
  683. }
  684. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  685. {
  686. const AVHWAccel *hwaccel = avctx->hwaccel;
  687. int override_dimensions = 1;
  688. int ret;
  689. switch (avctx->codec_type) {
  690. case AVMEDIA_TYPE_VIDEO:
  691. if (frame->width <= 0 || frame->height <= 0) {
  692. frame->width = FFMAX(avctx->width, avctx->coded_width);
  693. frame->height = FFMAX(avctx->height, avctx->coded_height);
  694. override_dimensions = 0;
  695. }
  696. if (frame->format < 0)
  697. frame->format = avctx->pix_fmt;
  698. if (!frame->sample_aspect_ratio.num)
  699. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  700. if (av_image_check_sar(frame->width, frame->height,
  701. frame->sample_aspect_ratio) < 0) {
  702. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  703. frame->sample_aspect_ratio.num,
  704. frame->sample_aspect_ratio.den);
  705. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  706. }
  707. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  708. return ret;
  709. break;
  710. case AVMEDIA_TYPE_AUDIO:
  711. if (!frame->sample_rate)
  712. frame->sample_rate = avctx->sample_rate;
  713. if (frame->format < 0)
  714. frame->format = avctx->sample_fmt;
  715. if (!frame->channel_layout) {
  716. if (avctx->channel_layout) {
  717. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  718. avctx->channels) {
  719. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  720. "configuration.\n");
  721. return AVERROR(EINVAL);
  722. }
  723. frame->channel_layout = avctx->channel_layout;
  724. } else {
  725. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  726. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  727. avctx->channels);
  728. return AVERROR(ENOSYS);
  729. }
  730. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  731. if (!frame->channel_layout)
  732. frame->channel_layout = (1ULL << avctx->channels) - 1;
  733. }
  734. }
  735. break;
  736. default: return AVERROR(EINVAL);
  737. }
  738. ret = ff_decode_frame_props(avctx, frame);
  739. if (ret < 0)
  740. return ret;
  741. if (hwaccel) {
  742. if (hwaccel->alloc_frame) {
  743. ret = hwaccel->alloc_frame(avctx, frame);
  744. goto end;
  745. }
  746. } else
  747. avctx->sw_pix_fmt = avctx->pix_fmt;
  748. ret = avctx->get_buffer2(avctx, frame, flags);
  749. end:
  750. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  751. frame->width = avctx->width;
  752. frame->height = avctx->height;
  753. }
  754. return ret;
  755. }
  756. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  757. {
  758. AVFrame *tmp;
  759. int ret;
  760. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  761. if (!frame->data[0])
  762. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  763. if (av_frame_is_writable(frame))
  764. return ff_decode_frame_props(avctx, frame);
  765. tmp = av_frame_alloc();
  766. if (!tmp)
  767. return AVERROR(ENOMEM);
  768. av_frame_move_ref(tmp, frame);
  769. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  770. if (ret < 0) {
  771. av_frame_free(&tmp);
  772. return ret;
  773. }
  774. av_frame_copy(frame, tmp);
  775. av_frame_free(&tmp);
  776. return 0;
  777. }
  778. void avcodec_flush_buffers(AVCodecContext *avctx)
  779. {
  780. avctx->internal->draining = 0;
  781. avctx->internal->draining_done = 0;
  782. av_frame_unref(avctx->internal->buffer_frame);
  783. av_frame_unref(avctx->internal->compat_decode_frame);
  784. av_packet_unref(avctx->internal->buffer_pkt);
  785. avctx->internal->buffer_pkt_valid = 0;
  786. av_packet_unref(avctx->internal->ds.in_pkt);
  787. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  788. ff_thread_flush(avctx);
  789. else if (avctx->codec->flush)
  790. avctx->codec->flush(avctx);
  791. if (!avctx->refcounted_frames)
  792. av_frame_unref(avctx->internal->to_free);
  793. }