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.

1198 lines
35KB

  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. user_opaque_ref = fdd->user_opaque_ref;
  355. fdd->user_opaque_ref = NULL;
  356. av_buffer_unref(&frame->opaque_ref);
  357. frame->opaque_ref = user_opaque_ref;
  358. }
  359. }
  360. return ret;
  361. }
  362. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  363. {
  364. AVCodecInternal *avci = avctx->internal;
  365. int ret = 0;
  366. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  367. return AVERROR(EINVAL);
  368. if (avctx->internal->draining)
  369. return AVERROR_EOF;
  370. ret = bsfs_init(avctx);
  371. if (ret < 0)
  372. return ret;
  373. av_packet_unref(avci->buffer_pkt);
  374. if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
  375. ret = av_packet_ref(avci->buffer_pkt, avpkt);
  376. if (ret < 0)
  377. return ret;
  378. }
  379. ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
  380. if (ret < 0) {
  381. av_packet_unref(avci->buffer_pkt);
  382. return ret;
  383. }
  384. if (!avci->buffer_frame->buf[0]) {
  385. ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
  386. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
  387. return ret;
  388. }
  389. return 0;
  390. }
  391. static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
  392. {
  393. /* make sure we are noisy about decoders returning invalid cropping data */
  394. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  395. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  396. (frame->crop_left + frame->crop_right) >= frame->width ||
  397. (frame->crop_top + frame->crop_bottom) >= frame->height) {
  398. av_log(avctx, AV_LOG_WARNING,
  399. "Invalid cropping information set by a decoder: %zu/%zu/%zu/%zu "
  400. "(frame size %dx%d). This is a bug, please report it\n",
  401. frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
  402. frame->width, frame->height);
  403. frame->crop_left = 0;
  404. frame->crop_right = 0;
  405. frame->crop_top = 0;
  406. frame->crop_bottom = 0;
  407. return 0;
  408. }
  409. if (!avctx->apply_cropping)
  410. return 0;
  411. return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
  412. AV_FRAME_CROP_UNALIGNED : 0);
  413. }
  414. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  415. {
  416. AVCodecInternal *avci = avctx->internal;
  417. int ret;
  418. av_frame_unref(frame);
  419. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  420. return AVERROR(EINVAL);
  421. ret = bsfs_init(avctx);
  422. if (ret < 0)
  423. return ret;
  424. if (avci->buffer_frame->buf[0]) {
  425. av_frame_move_ref(frame, avci->buffer_frame);
  426. } else {
  427. ret = decode_receive_frame_internal(avctx, frame);
  428. if (ret < 0)
  429. return ret;
  430. }
  431. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  432. ret = apply_cropping(avctx, frame);
  433. if (ret < 0) {
  434. av_frame_unref(frame);
  435. return ret;
  436. }
  437. }
  438. avctx->frame_number++;
  439. return 0;
  440. }
  441. static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
  442. int *got_frame, AVPacket *pkt)
  443. {
  444. AVCodecInternal *avci = avctx->internal;
  445. int ret = 0;
  446. av_assert0(avci->compat_decode_consumed == 0);
  447. *got_frame = 0;
  448. avci->compat_decode = 1;
  449. if (avci->compat_decode_partial_size > 0 &&
  450. avci->compat_decode_partial_size != pkt->size) {
  451. av_log(avctx, AV_LOG_ERROR,
  452. "Got unexpected packet size after a partial decode\n");
  453. ret = AVERROR(EINVAL);
  454. goto finish;
  455. }
  456. if (!avci->compat_decode_partial_size) {
  457. ret = avcodec_send_packet(avctx, pkt);
  458. if (ret == AVERROR_EOF)
  459. ret = 0;
  460. else if (ret == AVERROR(EAGAIN)) {
  461. /* we fully drain all the output in each decode call, so this should not
  462. * ever happen */
  463. ret = AVERROR_BUG;
  464. goto finish;
  465. } else if (ret < 0)
  466. goto finish;
  467. }
  468. while (ret >= 0) {
  469. ret = avcodec_receive_frame(avctx, frame);
  470. if (ret < 0) {
  471. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  472. ret = 0;
  473. goto finish;
  474. }
  475. if (frame != avci->compat_decode_frame) {
  476. if (!avctx->refcounted_frames) {
  477. ret = unrefcount_frame(avci, frame);
  478. if (ret < 0)
  479. goto finish;
  480. }
  481. *got_frame = 1;
  482. frame = avci->compat_decode_frame;
  483. } else {
  484. if (!avci->compat_decode_warned) {
  485. av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
  486. "API cannot return all the frames for this decoder. "
  487. "Some frames will be dropped. Update your code to the "
  488. "new decoding API to fix this.\n");
  489. avci->compat_decode_warned = 1;
  490. }
  491. }
  492. if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
  493. break;
  494. }
  495. finish:
  496. if (ret == 0) {
  497. /* if there are any bsfs then assume full packet is always consumed */
  498. if (avctx->codec->bsfs)
  499. ret = pkt->size;
  500. else
  501. ret = FFMIN(avci->compat_decode_consumed, pkt->size);
  502. }
  503. avci->compat_decode_consumed = 0;
  504. avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
  505. return ret;
  506. }
  507. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  508. int *got_picture_ptr,
  509. AVPacket *avpkt)
  510. {
  511. return compat_decode(avctx, picture, got_picture_ptr, avpkt);
  512. }
  513. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  514. AVFrame *frame,
  515. int *got_frame_ptr,
  516. AVPacket *avpkt)
  517. {
  518. return compat_decode(avctx, frame, got_frame_ptr, avpkt);
  519. }
  520. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  521. int *got_sub_ptr,
  522. AVPacket *avpkt)
  523. {
  524. int ret;
  525. ret = extract_packet_props(avctx->internal, avpkt);
  526. if (ret < 0)
  527. return ret;
  528. *got_sub_ptr = 0;
  529. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  530. if (*got_sub_ptr)
  531. avctx->frame_number++;
  532. return ret;
  533. }
  534. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  535. {
  536. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  537. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  538. }
  539. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  540. {
  541. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  542. ++fmt;
  543. return fmt[0];
  544. }
  545. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  546. enum AVPixelFormat pix_fmt)
  547. {
  548. AVHWAccel *hwaccel = NULL;
  549. while ((hwaccel = av_hwaccel_next(hwaccel)))
  550. if (hwaccel->id == codec_id
  551. && hwaccel->pix_fmt == pix_fmt)
  552. return hwaccel;
  553. return NULL;
  554. }
  555. static int setup_hwaccel(AVCodecContext *avctx,
  556. const enum AVPixelFormat fmt,
  557. const char *name)
  558. {
  559. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  560. int ret = 0;
  561. if (!hwa) {
  562. av_log(avctx, AV_LOG_ERROR,
  563. "Could not find an AVHWAccel for the pixel format: %s",
  564. name);
  565. return AVERROR(ENOENT);
  566. }
  567. if (hwa->priv_data_size) {
  568. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  569. if (!avctx->internal->hwaccel_priv_data)
  570. return AVERROR(ENOMEM);
  571. }
  572. avctx->hwaccel = hwa;
  573. if (hwa->init) {
  574. ret = hwa->init(avctx);
  575. if (ret < 0) {
  576. av_freep(&avctx->internal->hwaccel_priv_data);
  577. avctx->hwaccel = NULL;
  578. return ret;
  579. }
  580. }
  581. return 0;
  582. }
  583. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  584. {
  585. const AVPixFmtDescriptor *desc;
  586. enum AVPixelFormat *choices;
  587. enum AVPixelFormat ret;
  588. unsigned n = 0;
  589. while (fmt[n] != AV_PIX_FMT_NONE)
  590. ++n;
  591. av_assert0(n >= 1);
  592. avctx->sw_pix_fmt = fmt[n - 1];
  593. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  594. choices = av_malloc_array(n + 1, sizeof(*choices));
  595. if (!choices)
  596. return AV_PIX_FMT_NONE;
  597. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  598. for (;;) {
  599. if (avctx->hwaccel && avctx->hwaccel->uninit)
  600. avctx->hwaccel->uninit(avctx);
  601. av_freep(&avctx->internal->hwaccel_priv_data);
  602. avctx->hwaccel = NULL;
  603. av_buffer_unref(&avctx->hw_frames_ctx);
  604. ret = avctx->get_format(avctx, choices);
  605. desc = av_pix_fmt_desc_get(ret);
  606. if (!desc) {
  607. ret = AV_PIX_FMT_NONE;
  608. break;
  609. }
  610. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  611. break;
  612. if (avctx->hw_frames_ctx) {
  613. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  614. if (hw_frames_ctx->format != ret) {
  615. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  616. "does not match the format of provided AVHWFramesContext\n");
  617. ret = AV_PIX_FMT_NONE;
  618. break;
  619. }
  620. }
  621. if (!setup_hwaccel(avctx, ret, desc->name))
  622. break;
  623. /* Remove failed hwaccel from choices */
  624. for (n = 0; choices[n] != ret; n++)
  625. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  626. do
  627. choices[n] = choices[n + 1];
  628. while (choices[n++] != AV_PIX_FMT_NONE);
  629. }
  630. av_freep(&choices);
  631. return ret;
  632. }
  633. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  634. {
  635. FramePool *pool = avctx->internal->pool;
  636. int i, ret;
  637. switch (avctx->codec_type) {
  638. case AVMEDIA_TYPE_VIDEO: {
  639. uint8_t *data[4];
  640. int linesize[4];
  641. int size[4] = { 0 };
  642. int w = frame->width;
  643. int h = frame->height;
  644. int tmpsize, unaligned;
  645. if (pool->format == frame->format &&
  646. pool->width == frame->width && pool->height == frame->height)
  647. return 0;
  648. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  649. do {
  650. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  651. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  652. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  653. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  654. w += w & ~(w - 1);
  655. unaligned = 0;
  656. for (i = 0; i < 4; i++)
  657. unaligned |= linesize[i] % pool->stride_align[i];
  658. } while (unaligned);
  659. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  660. NULL, linesize);
  661. if (tmpsize < 0)
  662. return -1;
  663. for (i = 0; i < 3 && data[i + 1]; i++)
  664. size[i] = data[i + 1] - data[i];
  665. size[i] = tmpsize - (data[i] - data[0]);
  666. for (i = 0; i < 4; i++) {
  667. av_buffer_pool_uninit(&pool->pools[i]);
  668. pool->linesize[i] = linesize[i];
  669. if (size[i]) {
  670. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  671. if (!pool->pools[i]) {
  672. ret = AVERROR(ENOMEM);
  673. goto fail;
  674. }
  675. }
  676. }
  677. pool->format = frame->format;
  678. pool->width = frame->width;
  679. pool->height = frame->height;
  680. break;
  681. }
  682. case AVMEDIA_TYPE_AUDIO: {
  683. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  684. int planar = av_sample_fmt_is_planar(frame->format);
  685. int planes = planar ? ch : 1;
  686. if (pool->format == frame->format && pool->planes == planes &&
  687. pool->channels == ch && frame->nb_samples == pool->samples)
  688. return 0;
  689. av_buffer_pool_uninit(&pool->pools[0]);
  690. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  691. frame->nb_samples, frame->format, 0);
  692. if (ret < 0)
  693. goto fail;
  694. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  695. if (!pool->pools[0]) {
  696. ret = AVERROR(ENOMEM);
  697. goto fail;
  698. }
  699. pool->format = frame->format;
  700. pool->planes = planes;
  701. pool->channels = ch;
  702. pool->samples = frame->nb_samples;
  703. break;
  704. }
  705. default: av_assert0(0);
  706. }
  707. return 0;
  708. fail:
  709. for (i = 0; i < 4; i++)
  710. av_buffer_pool_uninit(&pool->pools[i]);
  711. pool->format = -1;
  712. pool->planes = pool->channels = pool->samples = 0;
  713. pool->width = pool->height = 0;
  714. return ret;
  715. }
  716. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  717. {
  718. FramePool *pool = avctx->internal->pool;
  719. int planes = pool->planes;
  720. int i;
  721. frame->linesize[0] = pool->linesize[0];
  722. if (planes > AV_NUM_DATA_POINTERS) {
  723. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  724. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  725. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  726. sizeof(*frame->extended_buf));
  727. if (!frame->extended_data || !frame->extended_buf) {
  728. av_freep(&frame->extended_data);
  729. av_freep(&frame->extended_buf);
  730. return AVERROR(ENOMEM);
  731. }
  732. } else
  733. frame->extended_data = frame->data;
  734. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  735. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  736. if (!frame->buf[i])
  737. goto fail;
  738. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  739. }
  740. for (i = 0; i < frame->nb_extended_buf; i++) {
  741. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  742. if (!frame->extended_buf[i])
  743. goto fail;
  744. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  745. }
  746. if (avctx->debug & FF_DEBUG_BUFFERS)
  747. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  748. return 0;
  749. fail:
  750. av_frame_unref(frame);
  751. return AVERROR(ENOMEM);
  752. }
  753. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  754. {
  755. FramePool *pool = s->internal->pool;
  756. int i;
  757. if (pic->data[0]) {
  758. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  759. return -1;
  760. }
  761. memset(pic->data, 0, sizeof(pic->data));
  762. pic->extended_data = pic->data;
  763. for (i = 0; i < 4 && pool->pools[i]; i++) {
  764. pic->linesize[i] = pool->linesize[i];
  765. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  766. if (!pic->buf[i])
  767. goto fail;
  768. pic->data[i] = pic->buf[i]->data;
  769. }
  770. for (; i < AV_NUM_DATA_POINTERS; i++) {
  771. pic->data[i] = NULL;
  772. pic->linesize[i] = 0;
  773. }
  774. if (pic->data[1] && !pic->data[2])
  775. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  776. if (s->debug & FF_DEBUG_BUFFERS)
  777. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  778. return 0;
  779. fail:
  780. av_frame_unref(pic);
  781. return AVERROR(ENOMEM);
  782. }
  783. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  784. {
  785. int ret;
  786. if (avctx->hw_frames_ctx) {
  787. ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  788. frame->width = avctx->coded_width;
  789. frame->height = avctx->coded_height;
  790. return ret;
  791. }
  792. if ((ret = update_frame_pool(avctx, frame)) < 0)
  793. return ret;
  794. switch (avctx->codec_type) {
  795. case AVMEDIA_TYPE_VIDEO:
  796. return video_get_buffer(avctx, frame);
  797. case AVMEDIA_TYPE_AUDIO:
  798. return audio_get_buffer(avctx, frame);
  799. default:
  800. return -1;
  801. }
  802. }
  803. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  804. {
  805. AVPacket *pkt = avctx->internal->last_pkt_props;
  806. int i;
  807. struct {
  808. enum AVPacketSideDataType packet;
  809. enum AVFrameSideDataType frame;
  810. } sd[] = {
  811. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  812. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  813. { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
  814. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  815. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  816. };
  817. frame->color_primaries = avctx->color_primaries;
  818. frame->color_trc = avctx->color_trc;
  819. frame->colorspace = avctx->colorspace;
  820. frame->color_range = avctx->color_range;
  821. frame->chroma_location = avctx->chroma_sample_location;
  822. frame->reordered_opaque = avctx->reordered_opaque;
  823. #if FF_API_PKT_PTS
  824. FF_DISABLE_DEPRECATION_WARNINGS
  825. frame->pkt_pts = pkt->pts;
  826. FF_ENABLE_DEPRECATION_WARNINGS
  827. #endif
  828. frame->pts = pkt->pts;
  829. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  830. int size;
  831. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  832. if (packet_sd) {
  833. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  834. sd[i].frame,
  835. size);
  836. if (!frame_sd)
  837. return AVERROR(ENOMEM);
  838. memcpy(frame_sd->data, packet_sd, size);
  839. }
  840. }
  841. return 0;
  842. }
  843. static void decode_data_free(void *opaque, uint8_t *data)
  844. {
  845. FrameDecodeData *fdd = (FrameDecodeData*)data;
  846. av_buffer_unref(&fdd->user_opaque_ref);
  847. av_freep(&fdd);
  848. }
  849. static int attach_decode_data(AVFrame *frame)
  850. {
  851. AVBufferRef *fdd_buf;
  852. FrameDecodeData *fdd;
  853. fdd = av_mallocz(sizeof(*fdd));
  854. if (!fdd)
  855. return AVERROR(ENOMEM);
  856. fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
  857. NULL, AV_BUFFER_FLAG_READONLY);
  858. if (!fdd_buf) {
  859. av_freep(&fdd);
  860. return AVERROR(ENOMEM);
  861. }
  862. fdd->user_opaque_ref = frame->opaque_ref;
  863. frame->opaque_ref = fdd_buf;
  864. return 0;
  865. }
  866. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  867. {
  868. const AVHWAccel *hwaccel = avctx->hwaccel;
  869. int override_dimensions = 1;
  870. int ret;
  871. switch (avctx->codec_type) {
  872. case AVMEDIA_TYPE_VIDEO:
  873. if (frame->width <= 0 || frame->height <= 0) {
  874. frame->width = FFMAX(avctx->width, avctx->coded_width);
  875. frame->height = FFMAX(avctx->height, avctx->coded_height);
  876. override_dimensions = 0;
  877. }
  878. if (frame->format < 0)
  879. frame->format = avctx->pix_fmt;
  880. if (!frame->sample_aspect_ratio.num)
  881. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  882. if (av_image_check_sar(frame->width, frame->height,
  883. frame->sample_aspect_ratio) < 0) {
  884. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  885. frame->sample_aspect_ratio.num,
  886. frame->sample_aspect_ratio.den);
  887. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  888. }
  889. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  890. return ret;
  891. break;
  892. case AVMEDIA_TYPE_AUDIO:
  893. if (!frame->sample_rate)
  894. frame->sample_rate = avctx->sample_rate;
  895. if (frame->format < 0)
  896. frame->format = avctx->sample_fmt;
  897. if (!frame->channel_layout) {
  898. if (avctx->channel_layout) {
  899. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  900. avctx->channels) {
  901. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  902. "configuration.\n");
  903. return AVERROR(EINVAL);
  904. }
  905. frame->channel_layout = avctx->channel_layout;
  906. } else {
  907. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  908. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  909. avctx->channels);
  910. return AVERROR(ENOSYS);
  911. }
  912. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  913. if (!frame->channel_layout)
  914. frame->channel_layout = (1ULL << avctx->channels) - 1;
  915. }
  916. }
  917. break;
  918. default: return AVERROR(EINVAL);
  919. }
  920. ret = ff_decode_frame_props(avctx, frame);
  921. if (ret < 0)
  922. return ret;
  923. if (hwaccel) {
  924. if (hwaccel->alloc_frame) {
  925. ret = hwaccel->alloc_frame(avctx, frame);
  926. goto end;
  927. }
  928. } else
  929. avctx->sw_pix_fmt = avctx->pix_fmt;
  930. ret = avctx->get_buffer2(avctx, frame, flags);
  931. if (ret < 0)
  932. goto end;
  933. ret = attach_decode_data(frame);
  934. if (ret < 0)
  935. goto end;
  936. end:
  937. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
  938. !(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
  939. frame->width = avctx->width;
  940. frame->height = avctx->height;
  941. }
  942. if (ret < 0)
  943. av_frame_unref(frame);
  944. return ret;
  945. }
  946. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  947. {
  948. AVFrame *tmp;
  949. int ret;
  950. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  951. if (!frame->data[0])
  952. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  953. if (av_frame_is_writable(frame))
  954. return ff_decode_frame_props(avctx, frame);
  955. tmp = av_frame_alloc();
  956. if (!tmp)
  957. return AVERROR(ENOMEM);
  958. av_frame_move_ref(tmp, frame);
  959. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  960. if (ret < 0) {
  961. av_frame_free(&tmp);
  962. return ret;
  963. }
  964. av_frame_copy(frame, tmp);
  965. av_frame_free(&tmp);
  966. return 0;
  967. }
  968. void avcodec_flush_buffers(AVCodecContext *avctx)
  969. {
  970. avctx->internal->draining = 0;
  971. avctx->internal->draining_done = 0;
  972. av_frame_unref(avctx->internal->buffer_frame);
  973. av_frame_unref(avctx->internal->compat_decode_frame);
  974. av_packet_unref(avctx->internal->buffer_pkt);
  975. avctx->internal->buffer_pkt_valid = 0;
  976. av_packet_unref(avctx->internal->ds.in_pkt);
  977. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  978. ff_thread_flush(avctx);
  979. else if (avctx->codec->flush)
  980. avctx->codec->flush(avctx);
  981. ff_decode_bsfs_uninit(avctx);
  982. if (!avctx->refcounted_frames)
  983. av_frame_unref(avctx->internal->to_free);
  984. }
  985. void ff_decode_bsfs_uninit(AVCodecContext *avctx)
  986. {
  987. DecodeFilterContext *s = &avctx->internal->filter;
  988. int i;
  989. for (i = 0; i < s->nb_bsfs; i++)
  990. av_bsf_free(&s->bsfs[i]);
  991. av_freep(&s->bsfs);
  992. s->nb_bsfs = 0;
  993. }