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.

1212 lines
36KB

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