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.

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