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.

2933 lines
89KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/hwcontext.h"
  34. #include "libavutil/internal.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/samplefmt.h"
  39. #include "libavutil/dict.h"
  40. #include "avcodec.h"
  41. #include "libavutil/opt.h"
  42. #include "me_cmp.h"
  43. #include "mpegvideo.h"
  44. #include "thread.h"
  45. #include "internal.h"
  46. #include "bytestream.h"
  47. #include "version.h"
  48. #include <stdlib.h>
  49. #include <stdarg.h>
  50. #include <limits.h>
  51. #include <float.h>
  52. static int volatile entangled_thread_counter = 0;
  53. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
  54. static void *codec_mutex;
  55. static void *avformat_mutex;
  56. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  57. {
  58. void **p = ptr;
  59. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  60. av_freep(p);
  61. *size = 0;
  62. return;
  63. }
  64. av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  65. if (*size)
  66. memset((uint8_t *)*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  67. }
  68. /* encoder management */
  69. static AVCodec *first_avcodec = NULL;
  70. AVCodec *av_codec_next(const AVCodec *c)
  71. {
  72. if (c)
  73. return c->next;
  74. else
  75. return first_avcodec;
  76. }
  77. static av_cold void avcodec_init(void)
  78. {
  79. static int initialized = 0;
  80. if (initialized != 0)
  81. return;
  82. initialized = 1;
  83. if (CONFIG_ME_CMP)
  84. ff_me_cmp_init_static();
  85. }
  86. int av_codec_is_encoder(const AVCodec *codec)
  87. {
  88. return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
  89. }
  90. int av_codec_is_decoder(const AVCodec *codec)
  91. {
  92. return codec && (codec->decode || codec->send_packet);
  93. }
  94. av_cold void avcodec_register(AVCodec *codec)
  95. {
  96. AVCodec **p;
  97. avcodec_init();
  98. p = &first_avcodec;
  99. while (*p)
  100. p = &(*p)->next;
  101. *p = codec;
  102. codec->next = NULL;
  103. if (codec->init_static_data)
  104. codec->init_static_data(codec);
  105. }
  106. #if FF_API_EMU_EDGE
  107. unsigned avcodec_get_edge_width(void)
  108. {
  109. return EDGE_WIDTH;
  110. }
  111. #endif
  112. #if FF_API_SET_DIMENSIONS
  113. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  114. {
  115. ff_set_dimensions(s, width, height);
  116. }
  117. #endif
  118. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  119. {
  120. int ret = av_image_check_size(width, height, 0, s);
  121. if (ret < 0)
  122. width = height = 0;
  123. s->width = s->coded_width = width;
  124. s->height = s->coded_height = height;
  125. return ret;
  126. }
  127. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  128. {
  129. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  130. if (ret < 0) {
  131. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  132. sar.num, sar.den);
  133. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  134. return ret;
  135. } else {
  136. avctx->sample_aspect_ratio = sar;
  137. }
  138. return 0;
  139. }
  140. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  141. enum AVMatrixEncoding matrix_encoding)
  142. {
  143. AVFrameSideData *side_data;
  144. enum AVMatrixEncoding *data;
  145. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  146. if (!side_data)
  147. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  148. sizeof(enum AVMatrixEncoding));
  149. if (!side_data)
  150. return AVERROR(ENOMEM);
  151. data = (enum AVMatrixEncoding*)side_data->data;
  152. *data = matrix_encoding;
  153. return 0;
  154. }
  155. #if HAVE_SIMD_ALIGN_32
  156. # define STRIDE_ALIGN 32
  157. #elif HAVE_SIMD_ALIGN_16
  158. # define STRIDE_ALIGN 16
  159. #else
  160. # define STRIDE_ALIGN 8
  161. #endif
  162. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  163. int linesize_align[AV_NUM_DATA_POINTERS])
  164. {
  165. int i;
  166. int w_align = 1;
  167. int h_align = 1;
  168. switch (s->pix_fmt) {
  169. case AV_PIX_FMT_YUV420P:
  170. case AV_PIX_FMT_YUYV422:
  171. case AV_PIX_FMT_YVYU422:
  172. case AV_PIX_FMT_UYVY422:
  173. case AV_PIX_FMT_YUV422P:
  174. case AV_PIX_FMT_YUV440P:
  175. case AV_PIX_FMT_YUV444P:
  176. case AV_PIX_FMT_GBRP:
  177. case AV_PIX_FMT_GBRAP:
  178. case AV_PIX_FMT_GRAY8:
  179. case AV_PIX_FMT_GRAY16BE:
  180. case AV_PIX_FMT_GRAY16LE:
  181. case AV_PIX_FMT_YUVJ420P:
  182. case AV_PIX_FMT_YUVJ422P:
  183. case AV_PIX_FMT_YUVJ440P:
  184. case AV_PIX_FMT_YUVJ444P:
  185. case AV_PIX_FMT_YUVA420P:
  186. case AV_PIX_FMT_YUVA422P:
  187. case AV_PIX_FMT_YUVA444P:
  188. case AV_PIX_FMT_YUV420P9LE:
  189. case AV_PIX_FMT_YUV420P9BE:
  190. case AV_PIX_FMT_YUV420P10LE:
  191. case AV_PIX_FMT_YUV420P10BE:
  192. case AV_PIX_FMT_YUV422P9LE:
  193. case AV_PIX_FMT_YUV422P9BE:
  194. case AV_PIX_FMT_YUV422P10LE:
  195. case AV_PIX_FMT_YUV422P10BE:
  196. case AV_PIX_FMT_YUVA422P10LE:
  197. case AV_PIX_FMT_YUVA422P10BE:
  198. case AV_PIX_FMT_YUV444P9LE:
  199. case AV_PIX_FMT_YUV444P9BE:
  200. case AV_PIX_FMT_YUV444P10LE:
  201. case AV_PIX_FMT_YUV444P10BE:
  202. case AV_PIX_FMT_YUVA444P10LE:
  203. case AV_PIX_FMT_YUVA444P10BE:
  204. case AV_PIX_FMT_GBRP9LE:
  205. case AV_PIX_FMT_GBRP9BE:
  206. case AV_PIX_FMT_GBRP10LE:
  207. case AV_PIX_FMT_GBRP10BE:
  208. case AV_PIX_FMT_GBRAP12LE:
  209. case AV_PIX_FMT_GBRAP12BE:
  210. w_align = 16; //FIXME assume 16 pixel per macroblock
  211. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  212. break;
  213. case AV_PIX_FMT_YUV411P:
  214. case AV_PIX_FMT_UYYVYY411:
  215. w_align = 32;
  216. h_align = 8;
  217. break;
  218. case AV_PIX_FMT_YUV410P:
  219. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  220. w_align = 64;
  221. h_align = 64;
  222. }
  223. case AV_PIX_FMT_RGB555:
  224. if (s->codec_id == AV_CODEC_ID_RPZA) {
  225. w_align = 4;
  226. h_align = 4;
  227. }
  228. case AV_PIX_FMT_PAL8:
  229. case AV_PIX_FMT_BGR8:
  230. case AV_PIX_FMT_RGB8:
  231. if (s->codec_id == AV_CODEC_ID_SMC) {
  232. w_align = 4;
  233. h_align = 4;
  234. }
  235. break;
  236. case AV_PIX_FMT_BGR24:
  237. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  238. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  239. w_align = 4;
  240. h_align = 4;
  241. }
  242. break;
  243. default:
  244. w_align = 1;
  245. h_align = 1;
  246. break;
  247. }
  248. *width = FFALIGN(*width, w_align);
  249. *height = FFALIGN(*height, h_align);
  250. if (s->codec_id == AV_CODEC_ID_H264)
  251. // some of the optimized chroma MC reads one line too much
  252. *height += 2;
  253. for (i = 0; i < 4; i++)
  254. linesize_align[i] = STRIDE_ALIGN;
  255. }
  256. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  257. {
  258. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  259. int chroma_shift = desc->log2_chroma_w;
  260. int linesize_align[AV_NUM_DATA_POINTERS];
  261. int align;
  262. avcodec_align_dimensions2(s, width, height, linesize_align);
  263. align = FFMAX(linesize_align[0], linesize_align[3]);
  264. linesize_align[1] <<= chroma_shift;
  265. linesize_align[2] <<= chroma_shift;
  266. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  267. *width = FFALIGN(*width, align);
  268. }
  269. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  270. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  271. int buf_size, int align)
  272. {
  273. int ch, planar, needed_size, ret = 0;
  274. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  275. frame->nb_samples, sample_fmt,
  276. align);
  277. if (buf_size < needed_size)
  278. return AVERROR(EINVAL);
  279. planar = av_sample_fmt_is_planar(sample_fmt);
  280. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  281. if (!(frame->extended_data = av_mallocz(nb_channels *
  282. sizeof(*frame->extended_data))))
  283. return AVERROR(ENOMEM);
  284. } else {
  285. frame->extended_data = frame->data;
  286. }
  287. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  288. buf, nb_channels, frame->nb_samples,
  289. sample_fmt, align)) < 0) {
  290. if (frame->extended_data != frame->data)
  291. av_free(frame->extended_data);
  292. return ret;
  293. }
  294. if (frame->extended_data != frame->data) {
  295. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  296. frame->data[ch] = frame->extended_data[ch];
  297. }
  298. return ret;
  299. }
  300. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  301. {
  302. FramePool *pool = avctx->internal->pool;
  303. int i, ret;
  304. switch (avctx->codec_type) {
  305. case AVMEDIA_TYPE_VIDEO: {
  306. uint8_t *data[4];
  307. int linesize[4];
  308. int size[4] = { 0 };
  309. int w = frame->width;
  310. int h = frame->height;
  311. int tmpsize, unaligned;
  312. if (pool->format == frame->format &&
  313. pool->width == frame->width && pool->height == frame->height)
  314. return 0;
  315. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  316. do {
  317. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  318. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  319. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  320. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  321. w += w & ~(w - 1);
  322. unaligned = 0;
  323. for (i = 0; i < 4; i++)
  324. unaligned |= linesize[i] % pool->stride_align[i];
  325. } while (unaligned);
  326. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  327. NULL, linesize);
  328. if (tmpsize < 0)
  329. return -1;
  330. for (i = 0; i < 3 && data[i + 1]; i++)
  331. size[i] = data[i + 1] - data[i];
  332. size[i] = tmpsize - (data[i] - data[0]);
  333. for (i = 0; i < 4; i++) {
  334. av_buffer_pool_uninit(&pool->pools[i]);
  335. pool->linesize[i] = linesize[i];
  336. if (size[i]) {
  337. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  338. if (!pool->pools[i]) {
  339. ret = AVERROR(ENOMEM);
  340. goto fail;
  341. }
  342. }
  343. }
  344. pool->format = frame->format;
  345. pool->width = frame->width;
  346. pool->height = frame->height;
  347. break;
  348. }
  349. case AVMEDIA_TYPE_AUDIO: {
  350. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  351. int planar = av_sample_fmt_is_planar(frame->format);
  352. int planes = planar ? ch : 1;
  353. if (pool->format == frame->format && pool->planes == planes &&
  354. pool->channels == ch && frame->nb_samples == pool->samples)
  355. return 0;
  356. av_buffer_pool_uninit(&pool->pools[0]);
  357. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  358. frame->nb_samples, frame->format, 0);
  359. if (ret < 0)
  360. goto fail;
  361. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  362. if (!pool->pools[0]) {
  363. ret = AVERROR(ENOMEM);
  364. goto fail;
  365. }
  366. pool->format = frame->format;
  367. pool->planes = planes;
  368. pool->channels = ch;
  369. pool->samples = frame->nb_samples;
  370. break;
  371. }
  372. default: av_assert0(0);
  373. }
  374. return 0;
  375. fail:
  376. for (i = 0; i < 4; i++)
  377. av_buffer_pool_uninit(&pool->pools[i]);
  378. pool->format = -1;
  379. pool->planes = pool->channels = pool->samples = 0;
  380. pool->width = pool->height = 0;
  381. return ret;
  382. }
  383. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  384. {
  385. FramePool *pool = avctx->internal->pool;
  386. int planes = pool->planes;
  387. int i;
  388. frame->linesize[0] = pool->linesize[0];
  389. if (planes > AV_NUM_DATA_POINTERS) {
  390. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  391. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  392. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  393. sizeof(*frame->extended_buf));
  394. if (!frame->extended_data || !frame->extended_buf) {
  395. av_freep(&frame->extended_data);
  396. av_freep(&frame->extended_buf);
  397. return AVERROR(ENOMEM);
  398. }
  399. } else
  400. frame->extended_data = frame->data;
  401. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  402. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  403. if (!frame->buf[i])
  404. goto fail;
  405. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  406. }
  407. for (i = 0; i < frame->nb_extended_buf; i++) {
  408. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  409. if (!frame->extended_buf[i])
  410. goto fail;
  411. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  412. }
  413. if (avctx->debug & FF_DEBUG_BUFFERS)
  414. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  415. return 0;
  416. fail:
  417. av_frame_unref(frame);
  418. return AVERROR(ENOMEM);
  419. }
  420. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  421. {
  422. FramePool *pool = s->internal->pool;
  423. int i;
  424. if (pic->data[0]) {
  425. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  426. return -1;
  427. }
  428. memset(pic->data, 0, sizeof(pic->data));
  429. pic->extended_data = pic->data;
  430. for (i = 0; i < 4 && pool->pools[i]; i++) {
  431. pic->linesize[i] = pool->linesize[i];
  432. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  433. if (!pic->buf[i])
  434. goto fail;
  435. pic->data[i] = pic->buf[i]->data;
  436. }
  437. for (; i < AV_NUM_DATA_POINTERS; i++) {
  438. pic->data[i] = NULL;
  439. pic->linesize[i] = 0;
  440. }
  441. if (pic->data[1] && !pic->data[2])
  442. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  443. if (s->debug & FF_DEBUG_BUFFERS)
  444. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  445. return 0;
  446. fail:
  447. av_frame_unref(pic);
  448. return AVERROR(ENOMEM);
  449. }
  450. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  451. {
  452. int ret;
  453. if (avctx->hw_frames_ctx)
  454. return av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
  455. if ((ret = update_frame_pool(avctx, frame)) < 0)
  456. return ret;
  457. switch (avctx->codec_type) {
  458. case AVMEDIA_TYPE_VIDEO:
  459. return video_get_buffer(avctx, frame);
  460. case AVMEDIA_TYPE_AUDIO:
  461. return audio_get_buffer(avctx, frame);
  462. default:
  463. return -1;
  464. }
  465. }
  466. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  467. {
  468. AVPacket *pkt = avctx->internal->pkt;
  469. int i;
  470. struct {
  471. enum AVPacketSideDataType packet;
  472. enum AVFrameSideDataType frame;
  473. } sd[] = {
  474. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  475. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  476. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  477. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  478. };
  479. frame->color_primaries = avctx->color_primaries;
  480. frame->color_trc = avctx->color_trc;
  481. frame->colorspace = avctx->colorspace;
  482. frame->color_range = avctx->color_range;
  483. frame->chroma_location = avctx->chroma_sample_location;
  484. frame->reordered_opaque = avctx->reordered_opaque;
  485. if (!pkt) {
  486. #if FF_API_PKT_PTS
  487. FF_DISABLE_DEPRECATION_WARNINGS
  488. frame->pkt_pts = AV_NOPTS_VALUE;
  489. FF_ENABLE_DEPRECATION_WARNINGS
  490. #endif
  491. frame->pts = AV_NOPTS_VALUE;
  492. return 0;
  493. }
  494. #if FF_API_PKT_PTS
  495. FF_DISABLE_DEPRECATION_WARNINGS
  496. frame->pkt_pts = pkt->pts;
  497. FF_ENABLE_DEPRECATION_WARNINGS
  498. #endif
  499. frame->pts = pkt->pts;
  500. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  501. int size;
  502. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  503. if (packet_sd) {
  504. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  505. sd[i].frame,
  506. size);
  507. if (!frame_sd)
  508. return AVERROR(ENOMEM);
  509. memcpy(frame_sd->data, packet_sd, size);
  510. }
  511. }
  512. return 0;
  513. }
  514. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  515. {
  516. const AVHWAccel *hwaccel = avctx->hwaccel;
  517. int override_dimensions = 1;
  518. int ret;
  519. switch (avctx->codec_type) {
  520. case AVMEDIA_TYPE_VIDEO:
  521. if (frame->width <= 0 || frame->height <= 0) {
  522. frame->width = FFMAX(avctx->width, avctx->coded_width);
  523. frame->height = FFMAX(avctx->height, avctx->coded_height);
  524. override_dimensions = 0;
  525. }
  526. if (frame->format < 0)
  527. frame->format = avctx->pix_fmt;
  528. if (!frame->sample_aspect_ratio.num)
  529. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  530. if (av_image_check_sar(frame->width, frame->height,
  531. frame->sample_aspect_ratio) < 0) {
  532. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  533. frame->sample_aspect_ratio.num,
  534. frame->sample_aspect_ratio.den);
  535. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  536. }
  537. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  538. return ret;
  539. break;
  540. case AVMEDIA_TYPE_AUDIO:
  541. if (!frame->sample_rate)
  542. frame->sample_rate = avctx->sample_rate;
  543. if (frame->format < 0)
  544. frame->format = avctx->sample_fmt;
  545. if (!frame->channel_layout) {
  546. if (avctx->channel_layout) {
  547. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  548. avctx->channels) {
  549. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  550. "configuration.\n");
  551. return AVERROR(EINVAL);
  552. }
  553. frame->channel_layout = avctx->channel_layout;
  554. } else {
  555. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  556. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  557. avctx->channels);
  558. return AVERROR(ENOSYS);
  559. }
  560. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  561. if (!frame->channel_layout)
  562. frame->channel_layout = (1ULL << avctx->channels) - 1;
  563. }
  564. }
  565. break;
  566. default: return AVERROR(EINVAL);
  567. }
  568. ret = ff_decode_frame_props(avctx, frame);
  569. if (ret < 0)
  570. return ret;
  571. if (hwaccel) {
  572. if (hwaccel->alloc_frame) {
  573. ret = hwaccel->alloc_frame(avctx, frame);
  574. goto end;
  575. }
  576. } else
  577. avctx->sw_pix_fmt = avctx->pix_fmt;
  578. ret = avctx->get_buffer2(avctx, frame, flags);
  579. end:
  580. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  581. frame->width = avctx->width;
  582. frame->height = avctx->height;
  583. }
  584. return ret;
  585. }
  586. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  587. {
  588. AVFrame *tmp;
  589. int ret;
  590. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  591. if (!frame->data[0])
  592. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  593. if (av_frame_is_writable(frame))
  594. return ff_decode_frame_props(avctx, frame);
  595. tmp = av_frame_alloc();
  596. if (!tmp)
  597. return AVERROR(ENOMEM);
  598. av_frame_move_ref(tmp, frame);
  599. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  600. if (ret < 0) {
  601. av_frame_free(&tmp);
  602. return ret;
  603. }
  604. av_frame_copy(frame, tmp);
  605. av_frame_free(&tmp);
  606. return 0;
  607. }
  608. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  609. {
  610. int i;
  611. for (i = 0; i < count; i++) {
  612. int r = func(c, (char *)arg + i * size);
  613. if (ret)
  614. ret[i] = r;
  615. }
  616. return 0;
  617. }
  618. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  619. {
  620. int i;
  621. for (i = 0; i < count; i++) {
  622. int r = func(c, arg, i, 0);
  623. if (ret)
  624. ret[i] = r;
  625. }
  626. return 0;
  627. }
  628. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  629. {
  630. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  631. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  632. }
  633. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  634. {
  635. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  636. ++fmt;
  637. return fmt[0];
  638. }
  639. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  640. enum AVPixelFormat pix_fmt)
  641. {
  642. AVHWAccel *hwaccel = NULL;
  643. while ((hwaccel = av_hwaccel_next(hwaccel)))
  644. if (hwaccel->id == codec_id
  645. && hwaccel->pix_fmt == pix_fmt)
  646. return hwaccel;
  647. return NULL;
  648. }
  649. static int setup_hwaccel(AVCodecContext *avctx,
  650. const enum AVPixelFormat fmt,
  651. const char *name)
  652. {
  653. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  654. int ret = 0;
  655. if (!hwa) {
  656. av_log(avctx, AV_LOG_ERROR,
  657. "Could not find an AVHWAccel for the pixel format: %s",
  658. name);
  659. return AVERROR(ENOENT);
  660. }
  661. if (hwa->priv_data_size) {
  662. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  663. if (!avctx->internal->hwaccel_priv_data)
  664. return AVERROR(ENOMEM);
  665. }
  666. if (hwa->init) {
  667. ret = hwa->init(avctx);
  668. if (ret < 0) {
  669. av_freep(&avctx->internal->hwaccel_priv_data);
  670. return ret;
  671. }
  672. }
  673. avctx->hwaccel = hwa;
  674. return 0;
  675. }
  676. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  677. {
  678. const AVPixFmtDescriptor *desc;
  679. enum AVPixelFormat *choices;
  680. enum AVPixelFormat ret;
  681. unsigned n = 0;
  682. while (fmt[n] != AV_PIX_FMT_NONE)
  683. ++n;
  684. av_assert0(n >= 1);
  685. avctx->sw_pix_fmt = fmt[n - 1];
  686. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  687. choices = av_malloc_array(n + 1, sizeof(*choices));
  688. if (!choices)
  689. return AV_PIX_FMT_NONE;
  690. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  691. for (;;) {
  692. if (avctx->hwaccel && avctx->hwaccel->uninit)
  693. avctx->hwaccel->uninit(avctx);
  694. av_freep(&avctx->internal->hwaccel_priv_data);
  695. avctx->hwaccel = NULL;
  696. av_buffer_unref(&avctx->hw_frames_ctx);
  697. ret = avctx->get_format(avctx, choices);
  698. desc = av_pix_fmt_desc_get(ret);
  699. if (!desc) {
  700. ret = AV_PIX_FMT_NONE;
  701. break;
  702. }
  703. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  704. break;
  705. if (avctx->hw_frames_ctx) {
  706. AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  707. if (hw_frames_ctx->format != ret) {
  708. av_log(avctx, AV_LOG_ERROR, "Format returned from get_buffer() "
  709. "does not match the format of provided AVHWFramesContext\n");
  710. ret = AV_PIX_FMT_NONE;
  711. break;
  712. }
  713. }
  714. if (!setup_hwaccel(avctx, ret, desc->name))
  715. break;
  716. /* Remove failed hwaccel from choices */
  717. for (n = 0; choices[n] != ret; n++)
  718. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  719. do
  720. choices[n] = choices[n + 1];
  721. while (choices[n++] != AV_PIX_FMT_NONE);
  722. }
  723. av_freep(&choices);
  724. return ret;
  725. }
  726. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  727. {
  728. int ret = 0;
  729. AVDictionary *tmp = NULL;
  730. if (avcodec_is_open(avctx))
  731. return 0;
  732. if ((!codec && !avctx->codec)) {
  733. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  734. return AVERROR(EINVAL);
  735. }
  736. if ((codec && avctx->codec && codec != avctx->codec)) {
  737. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  738. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  739. return AVERROR(EINVAL);
  740. }
  741. if (!codec)
  742. codec = avctx->codec;
  743. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  744. return AVERROR(EINVAL);
  745. if (options)
  746. av_dict_copy(&tmp, *options, 0);
  747. /* If there is a user-supplied mutex locking routine, call it. */
  748. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  749. if (lockmgr_cb) {
  750. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  751. return -1;
  752. }
  753. entangled_thread_counter++;
  754. if (entangled_thread_counter != 1) {
  755. av_log(avctx, AV_LOG_ERROR,
  756. "Insufficient thread locking. At least %d threads are "
  757. "calling avcodec_open2() at the same time right now.\n",
  758. entangled_thread_counter);
  759. ret = -1;
  760. goto end;
  761. }
  762. }
  763. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  764. if (!avctx->internal) {
  765. ret = AVERROR(ENOMEM);
  766. goto end;
  767. }
  768. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  769. if (!avctx->internal->pool) {
  770. ret = AVERROR(ENOMEM);
  771. goto free_and_end;
  772. }
  773. avctx->internal->to_free = av_frame_alloc();
  774. if (!avctx->internal->to_free) {
  775. ret = AVERROR(ENOMEM);
  776. goto free_and_end;
  777. }
  778. avctx->internal->buffer_frame = av_frame_alloc();
  779. if (!avctx->internal->buffer_frame) {
  780. ret = AVERROR(ENOMEM);
  781. goto free_and_end;
  782. }
  783. avctx->internal->buffer_pkt = av_packet_alloc();
  784. if (!avctx->internal->buffer_pkt) {
  785. ret = AVERROR(ENOMEM);
  786. goto free_and_end;
  787. }
  788. if (codec->priv_data_size > 0) {
  789. if (!avctx->priv_data) {
  790. avctx->priv_data = av_mallocz(codec->priv_data_size);
  791. if (!avctx->priv_data) {
  792. ret = AVERROR(ENOMEM);
  793. goto end;
  794. }
  795. if (codec->priv_class) {
  796. *(const AVClass **)avctx->priv_data = codec->priv_class;
  797. av_opt_set_defaults(avctx->priv_data);
  798. }
  799. }
  800. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  801. goto free_and_end;
  802. } else {
  803. avctx->priv_data = NULL;
  804. }
  805. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  806. goto free_and_end;
  807. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  808. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  809. else if (avctx->width && avctx->height)
  810. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  811. if (ret < 0)
  812. goto free_and_end;
  813. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  814. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  815. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  816. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  817. ff_set_dimensions(avctx, 0, 0);
  818. }
  819. if (avctx->width > 0 && avctx->height > 0) {
  820. if (av_image_check_sar(avctx->width, avctx->height,
  821. avctx->sample_aspect_ratio) < 0) {
  822. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  823. avctx->sample_aspect_ratio.num,
  824. avctx->sample_aspect_ratio.den);
  825. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  826. }
  827. }
  828. /* if the decoder init function was already called previously,
  829. * free the already allocated subtitle_header before overwriting it */
  830. if (av_codec_is_decoder(codec))
  831. av_freep(&avctx->subtitle_header);
  832. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  833. ret = AVERROR(EINVAL);
  834. goto free_and_end;
  835. }
  836. avctx->codec = codec;
  837. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  838. avctx->codec_id == AV_CODEC_ID_NONE) {
  839. avctx->codec_type = codec->type;
  840. avctx->codec_id = codec->id;
  841. }
  842. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  843. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  844. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  845. ret = AVERROR(EINVAL);
  846. goto free_and_end;
  847. }
  848. avctx->frame_number = 0;
  849. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  850. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  851. ret = AVERROR_EXPERIMENTAL;
  852. goto free_and_end;
  853. }
  854. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  855. (!avctx->time_base.num || !avctx->time_base.den)) {
  856. avctx->time_base.num = 1;
  857. avctx->time_base.den = avctx->sample_rate;
  858. }
  859. if (HAVE_THREADS) {
  860. ret = ff_thread_init(avctx);
  861. if (ret < 0) {
  862. goto free_and_end;
  863. }
  864. }
  865. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  866. avctx->thread_count = 1;
  867. if (av_codec_is_encoder(avctx->codec)) {
  868. int i;
  869. #if FF_API_CODED_FRAME
  870. FF_DISABLE_DEPRECATION_WARNINGS
  871. avctx->coded_frame = av_frame_alloc();
  872. if (!avctx->coded_frame) {
  873. ret = AVERROR(ENOMEM);
  874. goto free_and_end;
  875. }
  876. FF_ENABLE_DEPRECATION_WARNINGS
  877. #endif
  878. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  879. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  880. ret = AVERROR(EINVAL);
  881. goto free_and_end;
  882. }
  883. if (avctx->codec->sample_fmts) {
  884. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  885. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  886. break;
  887. if (avctx->channels == 1 &&
  888. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  889. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  890. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  891. break;
  892. }
  893. }
  894. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  895. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  896. ret = AVERROR(EINVAL);
  897. goto free_and_end;
  898. }
  899. }
  900. if (avctx->codec->pix_fmts) {
  901. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  902. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  903. break;
  904. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  905. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  906. ret = AVERROR(EINVAL);
  907. goto free_and_end;
  908. }
  909. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  910. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  911. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  912. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  913. avctx->color_range = AVCOL_RANGE_JPEG;
  914. }
  915. if (avctx->codec->supported_samplerates) {
  916. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  917. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  918. break;
  919. if (avctx->codec->supported_samplerates[i] == 0) {
  920. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  921. ret = AVERROR(EINVAL);
  922. goto free_and_end;
  923. }
  924. }
  925. if (avctx->codec->channel_layouts) {
  926. if (!avctx->channel_layout) {
  927. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  928. } else {
  929. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  930. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  931. break;
  932. if (avctx->codec->channel_layouts[i] == 0) {
  933. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  934. ret = AVERROR(EINVAL);
  935. goto free_and_end;
  936. }
  937. }
  938. }
  939. if (avctx->channel_layout && avctx->channels) {
  940. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  941. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  942. ret = AVERROR(EINVAL);
  943. goto free_and_end;
  944. }
  945. } else if (avctx->channel_layout) {
  946. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  947. }
  948. if (!avctx->rc_initial_buffer_occupancy)
  949. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  950. if (avctx->ticks_per_frame &&
  951. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  952. av_log(avctx, AV_LOG_ERROR,
  953. "ticks_per_frame %d too large for the timebase %d/%d.",
  954. avctx->ticks_per_frame,
  955. avctx->time_base.num,
  956. avctx->time_base.den);
  957. goto free_and_end;
  958. }
  959. if (avctx->hw_frames_ctx) {
  960. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  961. if (frames_ctx->format != avctx->pix_fmt) {
  962. av_log(avctx, AV_LOG_ERROR,
  963. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  964. ret = AVERROR(EINVAL);
  965. goto free_and_end;
  966. }
  967. if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
  968. avctx->sw_pix_fmt != frames_ctx->sw_format) {
  969. av_log(avctx, AV_LOG_ERROR,
  970. "Mismatching AVCodecContext.sw_pix_fmt (%s) "
  971. "and AVHWFramesContext.sw_format (%s)\n",
  972. av_get_pix_fmt_name(avctx->sw_pix_fmt),
  973. av_get_pix_fmt_name(frames_ctx->sw_format));
  974. ret = AVERROR(EINVAL);
  975. goto free_and_end;
  976. }
  977. avctx->sw_pix_fmt = frames_ctx->sw_format;
  978. }
  979. }
  980. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  981. ret = avctx->codec->init(avctx);
  982. if (ret < 0) {
  983. goto free_and_end;
  984. }
  985. }
  986. #if FF_API_AUDIOENC_DELAY
  987. if (av_codec_is_encoder(avctx->codec))
  988. avctx->delay = avctx->initial_padding;
  989. #endif
  990. if (av_codec_is_decoder(avctx->codec)) {
  991. /* validate channel layout from the decoder */
  992. if (avctx->channel_layout) {
  993. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  994. if (!avctx->channels)
  995. avctx->channels = channels;
  996. else if (channels != avctx->channels) {
  997. av_log(avctx, AV_LOG_WARNING,
  998. "channel layout does not match number of channels\n");
  999. avctx->channel_layout = 0;
  1000. }
  1001. }
  1002. if (avctx->channels && avctx->channels < 0 ||
  1003. avctx->channels > FF_SANE_NB_CHANNELS) {
  1004. ret = AVERROR(EINVAL);
  1005. goto free_and_end;
  1006. }
  1007. #if FF_API_AVCTX_TIMEBASE
  1008. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1009. avctx->time_base = av_inv_q(avctx->framerate);
  1010. #endif
  1011. }
  1012. end:
  1013. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  1014. entangled_thread_counter--;
  1015. /* Release any user-supplied mutex. */
  1016. if (lockmgr_cb) {
  1017. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1018. }
  1019. }
  1020. if (options) {
  1021. av_dict_free(options);
  1022. *options = tmp;
  1023. }
  1024. return ret;
  1025. free_and_end:
  1026. if (avctx->codec &&
  1027. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  1028. avctx->codec->close(avctx);
  1029. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1030. av_opt_free(avctx->priv_data);
  1031. av_opt_free(avctx);
  1032. #if FF_API_CODED_FRAME
  1033. FF_DISABLE_DEPRECATION_WARNINGS
  1034. av_frame_free(&avctx->coded_frame);
  1035. FF_ENABLE_DEPRECATION_WARNINGS
  1036. #endif
  1037. av_dict_free(&tmp);
  1038. av_freep(&avctx->priv_data);
  1039. if (avctx->internal) {
  1040. av_frame_free(&avctx->internal->to_free);
  1041. av_frame_free(&avctx->internal->buffer_frame);
  1042. av_packet_free(&avctx->internal->buffer_pkt);
  1043. av_freep(&avctx->internal->pool);
  1044. }
  1045. av_freep(&avctx->internal);
  1046. avctx->codec = NULL;
  1047. goto end;
  1048. }
  1049. int ff_alloc_packet(AVPacket *avpkt, int size)
  1050. {
  1051. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  1052. return AVERROR(EINVAL);
  1053. if (avpkt->data) {
  1054. AVBufferRef *buf = avpkt->buf;
  1055. if (avpkt->size < size)
  1056. return AVERROR(EINVAL);
  1057. av_init_packet(avpkt);
  1058. avpkt->buf = buf;
  1059. avpkt->size = size;
  1060. return 0;
  1061. } else {
  1062. return av_new_packet(avpkt, size);
  1063. }
  1064. }
  1065. /**
  1066. * Pad last frame with silence.
  1067. */
  1068. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1069. {
  1070. AVFrame *frame = NULL;
  1071. int ret;
  1072. if (!(frame = av_frame_alloc()))
  1073. return AVERROR(ENOMEM);
  1074. frame->format = src->format;
  1075. frame->channel_layout = src->channel_layout;
  1076. frame->nb_samples = s->frame_size;
  1077. ret = av_frame_get_buffer(frame, 32);
  1078. if (ret < 0)
  1079. goto fail;
  1080. ret = av_frame_copy_props(frame, src);
  1081. if (ret < 0)
  1082. goto fail;
  1083. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1084. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1085. goto fail;
  1086. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1087. frame->nb_samples - src->nb_samples,
  1088. s->channels, s->sample_fmt)) < 0)
  1089. goto fail;
  1090. *dst = frame;
  1091. return 0;
  1092. fail:
  1093. av_frame_free(&frame);
  1094. return ret;
  1095. }
  1096. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1097. AVPacket *avpkt,
  1098. const AVFrame *frame,
  1099. int *got_packet_ptr)
  1100. {
  1101. AVFrame tmp;
  1102. AVFrame *padded_frame = NULL;
  1103. int ret;
  1104. int user_packet = !!avpkt->data;
  1105. *got_packet_ptr = 0;
  1106. if (!avctx->codec->encode2) {
  1107. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  1108. return AVERROR(ENOSYS);
  1109. }
  1110. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1111. av_packet_unref(avpkt);
  1112. av_init_packet(avpkt);
  1113. return 0;
  1114. }
  1115. /* ensure that extended_data is properly set */
  1116. if (frame && !frame->extended_data) {
  1117. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1118. avctx->channels > AV_NUM_DATA_POINTERS) {
  1119. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1120. "with more than %d channels, but extended_data is not set.\n",
  1121. AV_NUM_DATA_POINTERS);
  1122. return AVERROR(EINVAL);
  1123. }
  1124. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1125. tmp = *frame;
  1126. tmp.extended_data = tmp.data;
  1127. frame = &tmp;
  1128. }
  1129. /* extract audio service type metadata */
  1130. if (frame) {
  1131. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1132. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1133. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1134. }
  1135. /* check for valid frame size */
  1136. if (frame) {
  1137. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1138. if (frame->nb_samples > avctx->frame_size)
  1139. return AVERROR(EINVAL);
  1140. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1141. if (frame->nb_samples < avctx->frame_size &&
  1142. !avctx->internal->last_audio_frame) {
  1143. ret = pad_last_frame(avctx, &padded_frame, frame);
  1144. if (ret < 0)
  1145. return ret;
  1146. frame = padded_frame;
  1147. avctx->internal->last_audio_frame = 1;
  1148. }
  1149. if (frame->nb_samples != avctx->frame_size) {
  1150. ret = AVERROR(EINVAL);
  1151. goto end;
  1152. }
  1153. }
  1154. }
  1155. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1156. if (!ret) {
  1157. if (*got_packet_ptr) {
  1158. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1159. if (avpkt->pts == AV_NOPTS_VALUE)
  1160. avpkt->pts = frame->pts;
  1161. if (!avpkt->duration)
  1162. avpkt->duration = ff_samples_to_time_base(avctx,
  1163. frame->nb_samples);
  1164. }
  1165. avpkt->dts = avpkt->pts;
  1166. } else {
  1167. avpkt->size = 0;
  1168. }
  1169. if (!user_packet && avpkt->size) {
  1170. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1171. if (ret >= 0)
  1172. avpkt->data = avpkt->buf->data;
  1173. }
  1174. avctx->frame_number++;
  1175. }
  1176. if (ret < 0 || !*got_packet_ptr) {
  1177. av_packet_unref(avpkt);
  1178. av_init_packet(avpkt);
  1179. goto end;
  1180. }
  1181. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1182. * this needs to be moved to the encoders, but for now we can do it
  1183. * here to simplify things */
  1184. avpkt->flags |= AV_PKT_FLAG_KEY;
  1185. end:
  1186. av_frame_free(&padded_frame);
  1187. #if FF_API_AUDIOENC_DELAY
  1188. avctx->delay = avctx->initial_padding;
  1189. #endif
  1190. return ret;
  1191. }
  1192. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1193. AVPacket *avpkt,
  1194. const AVFrame *frame,
  1195. int *got_packet_ptr)
  1196. {
  1197. int ret;
  1198. int user_packet = !!avpkt->data;
  1199. *got_packet_ptr = 0;
  1200. if (!avctx->codec->encode2) {
  1201. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  1202. return AVERROR(ENOSYS);
  1203. }
  1204. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1205. av_packet_unref(avpkt);
  1206. av_init_packet(avpkt);
  1207. avpkt->size = 0;
  1208. return 0;
  1209. }
  1210. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1211. return AVERROR(EINVAL);
  1212. av_assert0(avctx->codec->encode2);
  1213. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1214. if (!ret) {
  1215. if (!*got_packet_ptr)
  1216. avpkt->size = 0;
  1217. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1218. avpkt->pts = avpkt->dts = frame->pts;
  1219. if (!user_packet && avpkt->size) {
  1220. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1221. if (ret >= 0)
  1222. avpkt->data = avpkt->buf->data;
  1223. }
  1224. avctx->frame_number++;
  1225. }
  1226. if (ret < 0 || !*got_packet_ptr)
  1227. av_packet_unref(avpkt);
  1228. emms_c();
  1229. return ret;
  1230. }
  1231. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1232. const AVSubtitle *sub)
  1233. {
  1234. int ret;
  1235. if (sub->start_display_time) {
  1236. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1237. return -1;
  1238. }
  1239. if (sub->num_rects == 0 || !sub->rects)
  1240. return -1;
  1241. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1242. avctx->frame_number++;
  1243. return ret;
  1244. }
  1245. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1246. {
  1247. int size = 0, ret;
  1248. const uint8_t *data;
  1249. uint32_t flags;
  1250. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1251. if (!data)
  1252. return 0;
  1253. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1254. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1255. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1256. ret = AVERROR(EINVAL);
  1257. goto fail2;
  1258. }
  1259. if (size < 4)
  1260. goto fail;
  1261. flags = bytestream_get_le32(&data);
  1262. size -= 4;
  1263. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1264. if (size < 4)
  1265. goto fail;
  1266. avctx->channels = bytestream_get_le32(&data);
  1267. size -= 4;
  1268. }
  1269. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1270. if (size < 8)
  1271. goto fail;
  1272. avctx->channel_layout = bytestream_get_le64(&data);
  1273. size -= 8;
  1274. }
  1275. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1276. if (size < 4)
  1277. goto fail;
  1278. avctx->sample_rate = bytestream_get_le32(&data);
  1279. size -= 4;
  1280. }
  1281. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1282. if (size < 8)
  1283. goto fail;
  1284. avctx->width = bytestream_get_le32(&data);
  1285. avctx->height = bytestream_get_le32(&data);
  1286. size -= 8;
  1287. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1288. if (ret < 0)
  1289. goto fail2;
  1290. }
  1291. return 0;
  1292. fail:
  1293. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1294. ret = AVERROR_INVALIDDATA;
  1295. fail2:
  1296. if (ret < 0) {
  1297. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1298. if (avctx->err_recognition & AV_EF_EXPLODE)
  1299. return ret;
  1300. }
  1301. return 0;
  1302. }
  1303. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1304. {
  1305. int ret;
  1306. /* move the original frame to our backup */
  1307. av_frame_unref(avci->to_free);
  1308. av_frame_move_ref(avci->to_free, frame);
  1309. /* now copy everything except the AVBufferRefs back
  1310. * note that we make a COPY of the side data, so calling av_frame_free() on
  1311. * the caller's frame will work properly */
  1312. ret = av_frame_copy_props(frame, avci->to_free);
  1313. if (ret < 0)
  1314. return ret;
  1315. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1316. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1317. if (avci->to_free->extended_data != avci->to_free->data) {
  1318. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  1319. int size = planes * sizeof(*frame->extended_data);
  1320. if (!size) {
  1321. av_frame_unref(frame);
  1322. return AVERROR_BUG;
  1323. }
  1324. frame->extended_data = av_malloc(size);
  1325. if (!frame->extended_data) {
  1326. av_frame_unref(frame);
  1327. return AVERROR(ENOMEM);
  1328. }
  1329. memcpy(frame->extended_data, avci->to_free->extended_data,
  1330. size);
  1331. } else
  1332. frame->extended_data = frame->data;
  1333. frame->format = avci->to_free->format;
  1334. frame->width = avci->to_free->width;
  1335. frame->height = avci->to_free->height;
  1336. frame->channel_layout = avci->to_free->channel_layout;
  1337. frame->nb_samples = avci->to_free->nb_samples;
  1338. return 0;
  1339. }
  1340. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1341. int *got_picture_ptr,
  1342. AVPacket *avpkt)
  1343. {
  1344. AVCodecInternal *avci = avctx->internal;
  1345. int ret;
  1346. *got_picture_ptr = 0;
  1347. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1348. return -1;
  1349. if (!avctx->codec->decode) {
  1350. av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
  1351. return AVERROR(ENOSYS);
  1352. }
  1353. avctx->internal->pkt = avpkt;
  1354. ret = apply_param_change(avctx, avpkt);
  1355. if (ret < 0)
  1356. return ret;
  1357. av_frame_unref(picture);
  1358. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1359. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1360. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1361. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1362. avpkt);
  1363. else {
  1364. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1365. avpkt);
  1366. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1367. picture->pkt_dts = avpkt->dts;
  1368. /* get_buffer is supposed to set frame parameters */
  1369. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1370. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1371. picture->width = avctx->width;
  1372. picture->height = avctx->height;
  1373. picture->format = avctx->pix_fmt;
  1374. }
  1375. }
  1376. emms_c(); //needed to avoid an emms_c() call before every return;
  1377. if (*got_picture_ptr) {
  1378. if (!avctx->refcounted_frames) {
  1379. int err = unrefcount_frame(avci, picture);
  1380. if (err < 0)
  1381. return err;
  1382. }
  1383. avctx->frame_number++;
  1384. } else
  1385. av_frame_unref(picture);
  1386. } else
  1387. ret = 0;
  1388. #if FF_API_AVCTX_TIMEBASE
  1389. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1390. avctx->time_base = av_inv_q(avctx->framerate);
  1391. #endif
  1392. return ret;
  1393. }
  1394. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1395. AVFrame *frame,
  1396. int *got_frame_ptr,
  1397. AVPacket *avpkt)
  1398. {
  1399. AVCodecInternal *avci = avctx->internal;
  1400. int ret = 0;
  1401. *got_frame_ptr = 0;
  1402. if (!avctx->codec->decode) {
  1403. av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
  1404. return AVERROR(ENOSYS);
  1405. }
  1406. avctx->internal->pkt = avpkt;
  1407. if (!avpkt->data && avpkt->size) {
  1408. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1409. return AVERROR(EINVAL);
  1410. }
  1411. ret = apply_param_change(avctx, avpkt);
  1412. if (ret < 0)
  1413. return ret;
  1414. av_frame_unref(frame);
  1415. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  1416. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1417. if (ret >= 0 && *got_frame_ptr) {
  1418. avctx->frame_number++;
  1419. frame->pkt_dts = avpkt->dts;
  1420. if (frame->format == AV_SAMPLE_FMT_NONE)
  1421. frame->format = avctx->sample_fmt;
  1422. if (!avctx->refcounted_frames) {
  1423. int err = unrefcount_frame(avci, frame);
  1424. if (err < 0)
  1425. return err;
  1426. }
  1427. } else
  1428. av_frame_unref(frame);
  1429. }
  1430. return ret;
  1431. }
  1432. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1433. int *got_sub_ptr,
  1434. AVPacket *avpkt)
  1435. {
  1436. int ret;
  1437. avctx->internal->pkt = avpkt;
  1438. *got_sub_ptr = 0;
  1439. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1440. if (*got_sub_ptr)
  1441. avctx->frame_number++;
  1442. return ret;
  1443. }
  1444. void avsubtitle_free(AVSubtitle *sub)
  1445. {
  1446. int i;
  1447. for (i = 0; i < sub->num_rects; i++) {
  1448. av_freep(&sub->rects[i]->data[0]);
  1449. av_freep(&sub->rects[i]->data[1]);
  1450. av_freep(&sub->rects[i]->data[2]);
  1451. av_freep(&sub->rects[i]->data[3]);
  1452. av_freep(&sub->rects[i]->text);
  1453. av_freep(&sub->rects[i]->ass);
  1454. av_freep(&sub->rects[i]);
  1455. }
  1456. av_freep(&sub->rects);
  1457. memset(sub, 0, sizeof(AVSubtitle));
  1458. }
  1459. static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
  1460. {
  1461. int got_frame;
  1462. int ret;
  1463. av_assert0(!avctx->internal->buffer_frame->buf[0]);
  1464. if (!pkt)
  1465. pkt = avctx->internal->buffer_pkt;
  1466. // This is the lesser evil. The field is for compatibility with legacy users
  1467. // of the legacy API, and users using the new API should not be forced to
  1468. // even know about this field.
  1469. avctx->refcounted_frames = 1;
  1470. // Some codecs (at least wma lossless) will crash when feeding drain packets
  1471. // after EOF was signaled.
  1472. if (avctx->internal->draining_done)
  1473. return AVERROR_EOF;
  1474. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1475. ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
  1476. &got_frame, pkt);
  1477. if (ret >= 0)
  1478. ret = pkt->size;
  1479. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1480. ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
  1481. &got_frame, pkt);
  1482. } else {
  1483. ret = AVERROR(EINVAL);
  1484. }
  1485. if (ret < 0)
  1486. return ret;
  1487. if (avctx->internal->draining && !got_frame)
  1488. avctx->internal->draining_done = 1;
  1489. if (ret >= pkt->size) {
  1490. av_packet_unref(avctx->internal->buffer_pkt);
  1491. } else {
  1492. int consumed = ret;
  1493. if (pkt != avctx->internal->buffer_pkt) {
  1494. av_packet_unref(avctx->internal->buffer_pkt);
  1495. if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
  1496. return ret;
  1497. }
  1498. avctx->internal->buffer_pkt->data += consumed;
  1499. avctx->internal->buffer_pkt->size -= consumed;
  1500. avctx->internal->buffer_pkt->pts = AV_NOPTS_VALUE;
  1501. avctx->internal->buffer_pkt->dts = AV_NOPTS_VALUE;
  1502. }
  1503. if (got_frame)
  1504. av_assert0(avctx->internal->buffer_frame->buf[0]);
  1505. return 0;
  1506. }
  1507. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  1508. {
  1509. int ret;
  1510. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  1511. return AVERROR(EINVAL);
  1512. if (avctx->internal->draining)
  1513. return AVERROR_EOF;
  1514. if (!avpkt || !avpkt->size) {
  1515. avctx->internal->draining = 1;
  1516. avpkt = NULL;
  1517. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1518. return 0;
  1519. }
  1520. if (avctx->codec->send_packet) {
  1521. if (avpkt) {
  1522. ret = apply_param_change(avctx, (AVPacket *)avpkt);
  1523. if (ret < 0)
  1524. return ret;
  1525. }
  1526. return avctx->codec->send_packet(avctx, avpkt);
  1527. }
  1528. // Emulation via old API. Assume avpkt is likely not refcounted, while
  1529. // decoder output is always refcounted, and avoid copying.
  1530. if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
  1531. return AVERROR(EAGAIN);
  1532. // The goal is decoding the first frame of the packet without using memcpy,
  1533. // because the common case is having only 1 frame per packet (especially
  1534. // with video, but audio too). In other cases, it can't be avoided, unless
  1535. // the user is feeding refcounted packets.
  1536. return do_decode(avctx, (AVPacket *)avpkt);
  1537. }
  1538. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  1539. {
  1540. int ret;
  1541. av_frame_unref(frame);
  1542. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  1543. return AVERROR(EINVAL);
  1544. if (avctx->codec->receive_frame) {
  1545. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1546. return AVERROR_EOF;
  1547. return avctx->codec->receive_frame(avctx, frame);
  1548. }
  1549. // Emulation via old API.
  1550. if (!avctx->internal->buffer_frame->buf[0]) {
  1551. if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
  1552. return AVERROR(EAGAIN);
  1553. while (1) {
  1554. if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
  1555. av_packet_unref(avctx->internal->buffer_pkt);
  1556. return ret;
  1557. }
  1558. // Some audio decoders may consume partial data without returning
  1559. // a frame (fate-wmapro-2ch). There is no way to make the caller
  1560. // call avcodec_receive_frame() again without returning a frame,
  1561. // so try to decode more in these cases.
  1562. if (avctx->internal->buffer_frame->buf[0] ||
  1563. !avctx->internal->buffer_pkt->size)
  1564. break;
  1565. }
  1566. }
  1567. if (!avctx->internal->buffer_frame->buf[0])
  1568. return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
  1569. av_frame_move_ref(frame, avctx->internal->buffer_frame);
  1570. return 0;
  1571. }
  1572. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  1573. {
  1574. int ret;
  1575. *got_packet = 0;
  1576. av_packet_unref(avctx->internal->buffer_pkt);
  1577. avctx->internal->buffer_pkt_valid = 0;
  1578. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1579. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  1580. frame, got_packet);
  1581. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1582. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  1583. frame, got_packet);
  1584. } else {
  1585. ret = AVERROR(EINVAL);
  1586. }
  1587. if (ret >= 0 && *got_packet) {
  1588. // Encoders must always return ref-counted buffers.
  1589. // Side-data only packets have no data and can be not ref-counted.
  1590. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  1591. avctx->internal->buffer_pkt_valid = 1;
  1592. ret = 0;
  1593. } else {
  1594. av_packet_unref(avctx->internal->buffer_pkt);
  1595. }
  1596. return ret;
  1597. }
  1598. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  1599. {
  1600. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  1601. return AVERROR(EINVAL);
  1602. if (avctx->internal->draining)
  1603. return AVERROR_EOF;
  1604. if (!frame) {
  1605. avctx->internal->draining = 1;
  1606. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1607. return 0;
  1608. }
  1609. if (avctx->codec->send_frame)
  1610. return avctx->codec->send_frame(avctx, frame);
  1611. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  1612. // 1. if the AVFrame is not refcounted, the copying will be much more
  1613. // expensive than copying the packet data
  1614. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  1615. // needed
  1616. if (avctx->internal->buffer_pkt_valid)
  1617. return AVERROR(EAGAIN);
  1618. return do_encode(avctx, frame, &(int){0});
  1619. }
  1620. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  1621. {
  1622. av_packet_unref(avpkt);
  1623. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  1624. return AVERROR(EINVAL);
  1625. if (avctx->codec->receive_packet) {
  1626. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1627. return AVERROR_EOF;
  1628. return avctx->codec->receive_packet(avctx, avpkt);
  1629. }
  1630. // Emulation via old API.
  1631. if (!avctx->internal->buffer_pkt_valid) {
  1632. int got_packet;
  1633. int ret;
  1634. if (!avctx->internal->draining)
  1635. return AVERROR(EAGAIN);
  1636. ret = do_encode(avctx, NULL, &got_packet);
  1637. if (ret < 0)
  1638. return ret;
  1639. if (ret >= 0 && !got_packet)
  1640. return AVERROR_EOF;
  1641. }
  1642. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  1643. avctx->internal->buffer_pkt_valid = 0;
  1644. return 0;
  1645. }
  1646. av_cold int avcodec_close(AVCodecContext *avctx)
  1647. {
  1648. int i;
  1649. if (avcodec_is_open(avctx)) {
  1650. FramePool *pool = avctx->internal->pool;
  1651. if (HAVE_THREADS && avctx->internal->thread_ctx)
  1652. ff_thread_free(avctx);
  1653. if (avctx->codec && avctx->codec->close)
  1654. avctx->codec->close(avctx);
  1655. av_frame_free(&avctx->internal->to_free);
  1656. av_frame_free(&avctx->internal->buffer_frame);
  1657. av_packet_free(&avctx->internal->buffer_pkt);
  1658. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1659. av_buffer_pool_uninit(&pool->pools[i]);
  1660. av_freep(&avctx->internal->pool);
  1661. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1662. avctx->hwaccel->uninit(avctx);
  1663. av_freep(&avctx->internal->hwaccel_priv_data);
  1664. av_freep(&avctx->internal);
  1665. }
  1666. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1667. av_freep(&avctx->coded_side_data[i].data);
  1668. av_freep(&avctx->coded_side_data);
  1669. avctx->nb_coded_side_data = 0;
  1670. av_buffer_unref(&avctx->hw_frames_ctx);
  1671. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1672. av_opt_free(avctx->priv_data);
  1673. av_opt_free(avctx);
  1674. av_freep(&avctx->priv_data);
  1675. if (av_codec_is_encoder(avctx->codec)) {
  1676. av_freep(&avctx->extradata);
  1677. #if FF_API_CODED_FRAME
  1678. FF_DISABLE_DEPRECATION_WARNINGS
  1679. av_frame_free(&avctx->coded_frame);
  1680. FF_ENABLE_DEPRECATION_WARNINGS
  1681. #endif
  1682. }
  1683. avctx->codec = NULL;
  1684. avctx->active_thread_type = 0;
  1685. return 0;
  1686. }
  1687. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1688. {
  1689. AVCodec *p, *experimental = NULL;
  1690. p = first_avcodec;
  1691. while (p) {
  1692. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1693. p->id == id) {
  1694. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  1695. experimental = p;
  1696. } else
  1697. return p;
  1698. }
  1699. p = p->next;
  1700. }
  1701. return experimental;
  1702. }
  1703. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1704. {
  1705. return find_encdec(id, 1);
  1706. }
  1707. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1708. {
  1709. AVCodec *p;
  1710. if (!name)
  1711. return NULL;
  1712. p = first_avcodec;
  1713. while (p) {
  1714. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1715. return p;
  1716. p = p->next;
  1717. }
  1718. return NULL;
  1719. }
  1720. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1721. {
  1722. return find_encdec(id, 0);
  1723. }
  1724. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1725. {
  1726. AVCodec *p;
  1727. if (!name)
  1728. return NULL;
  1729. p = first_avcodec;
  1730. while (p) {
  1731. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1732. return p;
  1733. p = p->next;
  1734. }
  1735. return NULL;
  1736. }
  1737. static int get_bit_rate(AVCodecContext *ctx)
  1738. {
  1739. int bit_rate;
  1740. int bits_per_sample;
  1741. switch (ctx->codec_type) {
  1742. case AVMEDIA_TYPE_VIDEO:
  1743. case AVMEDIA_TYPE_DATA:
  1744. case AVMEDIA_TYPE_SUBTITLE:
  1745. case AVMEDIA_TYPE_ATTACHMENT:
  1746. bit_rate = ctx->bit_rate;
  1747. break;
  1748. case AVMEDIA_TYPE_AUDIO:
  1749. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1750. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1751. break;
  1752. default:
  1753. bit_rate = 0;
  1754. break;
  1755. }
  1756. return bit_rate;
  1757. }
  1758. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1759. {
  1760. int i, len, ret = 0;
  1761. #define TAG_PRINT(x) \
  1762. (((x) >= '0' && (x) <= '9') || \
  1763. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1764. ((x) == '.' || (x) == ' '))
  1765. for (i = 0; i < 4; i++) {
  1766. len = snprintf(buf, buf_size,
  1767. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1768. buf += len;
  1769. buf_size = buf_size > len ? buf_size - len : 0;
  1770. ret += len;
  1771. codec_tag >>= 8;
  1772. }
  1773. return ret;
  1774. }
  1775. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1776. {
  1777. const char *codec_name;
  1778. const char *profile = NULL;
  1779. char buf1[32];
  1780. int bitrate;
  1781. int new_line = 0;
  1782. AVRational display_aspect_ratio;
  1783. const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
  1784. if (desc) {
  1785. codec_name = desc->name;
  1786. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  1787. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1788. /* fake mpeg2 transport stream codec (currently not
  1789. * registered) */
  1790. codec_name = "mpeg2ts";
  1791. } else {
  1792. /* output avi tags */
  1793. char tag_buf[32];
  1794. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1795. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1796. codec_name = buf1;
  1797. }
  1798. switch (enc->codec_type) {
  1799. case AVMEDIA_TYPE_VIDEO:
  1800. snprintf(buf, buf_size,
  1801. "Video: %s%s",
  1802. codec_name, enc->mb_decision ? " (hq)" : "");
  1803. if (profile)
  1804. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1805. " (%s)", profile);
  1806. if (enc->codec_tag) {
  1807. char tag_buf[32];
  1808. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1809. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1810. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1811. }
  1812. av_strlcat(buf, "\n ", buf_size);
  1813. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1814. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1815. av_get_pix_fmt_name(enc->pix_fmt));
  1816. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1817. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1818. av_color_range_name(enc->color_range));
  1819. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1820. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1821. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1822. new_line = 1;
  1823. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
  1824. av_color_space_name(enc->colorspace),
  1825. av_color_primaries_name(enc->color_primaries),
  1826. av_color_transfer_name(enc->color_trc));
  1827. }
  1828. if (av_log_get_level() >= AV_LOG_DEBUG &&
  1829. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1830. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1831. av_chroma_location_name(enc->chroma_sample_location));
  1832. if (enc->width) {
  1833. av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
  1834. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1835. "%dx%d",
  1836. enc->width, enc->height);
  1837. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1838. (enc->width != enc->coded_width ||
  1839. enc->height != enc->coded_height))
  1840. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1841. " (%dx%d)", enc->coded_width, enc->coded_height);
  1842. if (enc->sample_aspect_ratio.num) {
  1843. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1844. enc->width * enc->sample_aspect_ratio.num,
  1845. enc->height * enc->sample_aspect_ratio.den,
  1846. 1024 * 1024);
  1847. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1848. " [PAR %d:%d DAR %d:%d]",
  1849. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1850. display_aspect_ratio.num, display_aspect_ratio.den);
  1851. }
  1852. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1853. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1854. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1855. ", %d/%d",
  1856. enc->time_base.num / g, enc->time_base.den / g);
  1857. }
  1858. }
  1859. if (encode) {
  1860. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1861. ", q=%d-%d", enc->qmin, enc->qmax);
  1862. }
  1863. break;
  1864. case AVMEDIA_TYPE_AUDIO:
  1865. snprintf(buf, buf_size,
  1866. "Audio: %s",
  1867. codec_name);
  1868. if (profile)
  1869. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1870. " (%s)", profile);
  1871. if (enc->codec_tag) {
  1872. char tag_buf[32];
  1873. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1874. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1875. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1876. }
  1877. av_strlcat(buf, "\n ", buf_size);
  1878. if (enc->sample_rate) {
  1879. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1880. "%d Hz, ", enc->sample_rate);
  1881. }
  1882. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1883. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1884. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1885. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1886. }
  1887. break;
  1888. case AVMEDIA_TYPE_DATA:
  1889. snprintf(buf, buf_size, "Data: %s", codec_name);
  1890. break;
  1891. case AVMEDIA_TYPE_SUBTITLE:
  1892. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1893. break;
  1894. case AVMEDIA_TYPE_ATTACHMENT:
  1895. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1896. break;
  1897. default:
  1898. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1899. return;
  1900. }
  1901. if (encode) {
  1902. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1903. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1904. ", pass 1");
  1905. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1906. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1907. ", pass 2");
  1908. }
  1909. bitrate = get_bit_rate(enc);
  1910. if (bitrate != 0) {
  1911. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1912. ", %d kb/s", bitrate / 1000);
  1913. }
  1914. }
  1915. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1916. {
  1917. const AVProfile *p;
  1918. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1919. return NULL;
  1920. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1921. if (p->profile == profile)
  1922. return p->name;
  1923. return NULL;
  1924. }
  1925. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1926. {
  1927. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1928. const AVProfile *p;
  1929. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1930. return NULL;
  1931. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1932. if (p->profile == profile)
  1933. return p->name;
  1934. return NULL;
  1935. }
  1936. unsigned avcodec_version(void)
  1937. {
  1938. return LIBAVCODEC_VERSION_INT;
  1939. }
  1940. const char *avcodec_configuration(void)
  1941. {
  1942. return LIBAV_CONFIGURATION;
  1943. }
  1944. const char *avcodec_license(void)
  1945. {
  1946. #define LICENSE_PREFIX "libavcodec license: "
  1947. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1948. }
  1949. void avcodec_flush_buffers(AVCodecContext *avctx)
  1950. {
  1951. avctx->internal->draining = 0;
  1952. avctx->internal->draining_done = 0;
  1953. av_frame_unref(avctx->internal->buffer_frame);
  1954. av_packet_unref(avctx->internal->buffer_pkt);
  1955. avctx->internal->buffer_pkt_valid = 0;
  1956. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1957. ff_thread_flush(avctx);
  1958. else if (avctx->codec->flush)
  1959. avctx->codec->flush(avctx);
  1960. if (!avctx->refcounted_frames)
  1961. av_frame_unref(avctx->internal->to_free);
  1962. }
  1963. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1964. {
  1965. switch (codec_id) {
  1966. case AV_CODEC_ID_ADPCM_CT:
  1967. case AV_CODEC_ID_ADPCM_IMA_APC:
  1968. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1969. case AV_CODEC_ID_ADPCM_IMA_WS:
  1970. case AV_CODEC_ID_ADPCM_G722:
  1971. case AV_CODEC_ID_ADPCM_YAMAHA:
  1972. return 4;
  1973. case AV_CODEC_ID_PCM_ALAW:
  1974. case AV_CODEC_ID_PCM_MULAW:
  1975. case AV_CODEC_ID_PCM_S8:
  1976. case AV_CODEC_ID_PCM_U8:
  1977. case AV_CODEC_ID_PCM_ZORK:
  1978. return 8;
  1979. case AV_CODEC_ID_PCM_S16BE:
  1980. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1981. case AV_CODEC_ID_PCM_S16LE:
  1982. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1983. case AV_CODEC_ID_PCM_U16BE:
  1984. case AV_CODEC_ID_PCM_U16LE:
  1985. return 16;
  1986. case AV_CODEC_ID_PCM_S24DAUD:
  1987. case AV_CODEC_ID_PCM_S24BE:
  1988. case AV_CODEC_ID_PCM_S24LE:
  1989. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1990. case AV_CODEC_ID_PCM_U24BE:
  1991. case AV_CODEC_ID_PCM_U24LE:
  1992. return 24;
  1993. case AV_CODEC_ID_PCM_S32BE:
  1994. case AV_CODEC_ID_PCM_S32LE:
  1995. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1996. case AV_CODEC_ID_PCM_U32BE:
  1997. case AV_CODEC_ID_PCM_U32LE:
  1998. case AV_CODEC_ID_PCM_F32BE:
  1999. case AV_CODEC_ID_PCM_F32LE:
  2000. return 32;
  2001. case AV_CODEC_ID_PCM_F64BE:
  2002. case AV_CODEC_ID_PCM_F64LE:
  2003. return 64;
  2004. default:
  2005. return 0;
  2006. }
  2007. }
  2008. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2009. {
  2010. switch (codec_id) {
  2011. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2012. return 2;
  2013. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2014. return 3;
  2015. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2016. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2017. case AV_CODEC_ID_ADPCM_IMA_QT:
  2018. case AV_CODEC_ID_ADPCM_SWF:
  2019. case AV_CODEC_ID_ADPCM_MS:
  2020. return 4;
  2021. default:
  2022. return av_get_exact_bits_per_sample(codec_id);
  2023. }
  2024. }
  2025. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  2026. uint32_t tag, int bits_per_coded_sample, int frame_bytes)
  2027. {
  2028. int bps = av_get_exact_bits_per_sample(id);
  2029. /* codecs with an exact constant bits per sample */
  2030. if (bps > 0 && ch > 0 && frame_bytes > 0)
  2031. return (frame_bytes * 8) / (bps * ch);
  2032. bps = bits_per_coded_sample;
  2033. /* codecs with a fixed packet duration */
  2034. switch (id) {
  2035. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2036. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2037. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2038. case AV_CODEC_ID_AMR_NB:
  2039. case AV_CODEC_ID_GSM:
  2040. case AV_CODEC_ID_QCELP:
  2041. case AV_CODEC_ID_RA_144:
  2042. case AV_CODEC_ID_RA_288: return 160;
  2043. case AV_CODEC_ID_IMC: return 256;
  2044. case AV_CODEC_ID_AMR_WB:
  2045. case AV_CODEC_ID_GSM_MS: return 320;
  2046. case AV_CODEC_ID_MP1: return 384;
  2047. case AV_CODEC_ID_ATRAC1: return 512;
  2048. case AV_CODEC_ID_ATRAC3: return 1024;
  2049. case AV_CODEC_ID_MP2:
  2050. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2051. case AV_CODEC_ID_AC3: return 1536;
  2052. }
  2053. if (sr > 0) {
  2054. /* calc from sample rate */
  2055. if (id == AV_CODEC_ID_TTA)
  2056. return 256 * sr / 245;
  2057. if (ch > 0) {
  2058. /* calc from sample rate and channels */
  2059. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2060. return (480 << (sr / 22050)) / ch;
  2061. }
  2062. }
  2063. if (ba > 0) {
  2064. /* calc from block_align */
  2065. if (id == AV_CODEC_ID_SIPR) {
  2066. switch (ba) {
  2067. case 20: return 160;
  2068. case 19: return 144;
  2069. case 29: return 288;
  2070. case 37: return 480;
  2071. }
  2072. } else if (id == AV_CODEC_ID_ILBC) {
  2073. switch (ba) {
  2074. case 38: return 160;
  2075. case 50: return 240;
  2076. }
  2077. }
  2078. }
  2079. if (frame_bytes > 0) {
  2080. /* calc from frame_bytes only */
  2081. if (id == AV_CODEC_ID_TRUESPEECH)
  2082. return 240 * (frame_bytes / 32);
  2083. if (id == AV_CODEC_ID_NELLYMOSER)
  2084. return 256 * (frame_bytes / 64);
  2085. if (bps > 0) {
  2086. /* calc from frame_bytes and bits_per_coded_sample */
  2087. if (id == AV_CODEC_ID_ADPCM_G726)
  2088. return frame_bytes * 8 / bps;
  2089. }
  2090. if (ch > 0) {
  2091. /* calc from frame_bytes and channels */
  2092. switch (id) {
  2093. case AV_CODEC_ID_ADPCM_4XM:
  2094. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2095. return (frame_bytes - 4 * ch) * 2 / ch;
  2096. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2097. return (frame_bytes - 4) * 2 / ch;
  2098. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2099. return (frame_bytes - 8) * 2 / ch;
  2100. case AV_CODEC_ID_ADPCM_XA:
  2101. return (frame_bytes / 128) * 224 / ch;
  2102. case AV_CODEC_ID_INTERPLAY_DPCM:
  2103. return (frame_bytes - 6 - ch) / ch;
  2104. case AV_CODEC_ID_ROQ_DPCM:
  2105. return (frame_bytes - 8) / ch;
  2106. case AV_CODEC_ID_XAN_DPCM:
  2107. return (frame_bytes - 2 * ch) / ch;
  2108. case AV_CODEC_ID_MACE3:
  2109. return 3 * frame_bytes / ch;
  2110. case AV_CODEC_ID_MACE6:
  2111. return 6 * frame_bytes / ch;
  2112. case AV_CODEC_ID_PCM_LXF:
  2113. return 2 * (frame_bytes / (5 * ch));
  2114. }
  2115. if (tag) {
  2116. /* calc from frame_bytes, channels, and codec_tag */
  2117. if (id == AV_CODEC_ID_SOL_DPCM) {
  2118. if (tag == 3)
  2119. return frame_bytes / ch;
  2120. else
  2121. return frame_bytes * 2 / ch;
  2122. }
  2123. }
  2124. if (ba > 0) {
  2125. /* calc from frame_bytes, channels, and block_align */
  2126. int blocks = frame_bytes / ba;
  2127. switch (id) {
  2128. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2129. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2130. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2131. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2132. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2133. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2134. case AV_CODEC_ID_ADPCM_MS:
  2135. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2136. }
  2137. }
  2138. if (bps > 0) {
  2139. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2140. switch (id) {
  2141. case AV_CODEC_ID_PCM_DVD:
  2142. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2143. case AV_CODEC_ID_PCM_BLURAY:
  2144. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2145. case AV_CODEC_ID_S302M:
  2146. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2147. }
  2148. }
  2149. }
  2150. }
  2151. return 0;
  2152. }
  2153. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2154. {
  2155. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  2156. avctx->channels, avctx->block_align,
  2157. avctx->codec_tag, avctx->bits_per_coded_sample,
  2158. frame_bytes);
  2159. }
  2160. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  2161. {
  2162. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  2163. par->channels, par->block_align,
  2164. par->codec_tag, par->bits_per_coded_sample,
  2165. frame_bytes);
  2166. }
  2167. #if !HAVE_THREADS
  2168. int ff_thread_init(AVCodecContext *s)
  2169. {
  2170. return -1;
  2171. }
  2172. #endif
  2173. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2174. {
  2175. unsigned int n = 0;
  2176. while (v >= 0xff) {
  2177. *s++ = 0xff;
  2178. v -= 0xff;
  2179. n++;
  2180. }
  2181. *s = v;
  2182. n++;
  2183. return n;
  2184. }
  2185. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2186. {
  2187. int i;
  2188. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2189. return i;
  2190. }
  2191. #if FF_API_MISSING_SAMPLE
  2192. FF_DISABLE_DEPRECATION_WARNINGS
  2193. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2194. {
  2195. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  2196. "version to the newest one from Git. If the problem still "
  2197. "occurs, it means that your file has a feature which has not "
  2198. "been implemented.\n", feature);
  2199. if(want_sample)
  2200. av_log_ask_for_sample(avc, NULL);
  2201. }
  2202. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2203. {
  2204. va_list argument_list;
  2205. va_start(argument_list, msg);
  2206. if (msg)
  2207. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2208. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2209. "of this file to ftp://upload.libav.org/incoming/ "
  2210. "and contact the libav-devel mailing list.\n");
  2211. va_end(argument_list);
  2212. }
  2213. FF_ENABLE_DEPRECATION_WARNINGS
  2214. #endif /* FF_API_MISSING_SAMPLE */
  2215. static AVHWAccel *first_hwaccel = NULL;
  2216. void av_register_hwaccel(AVHWAccel *hwaccel)
  2217. {
  2218. AVHWAccel **p = &first_hwaccel;
  2219. while (*p)
  2220. p = &(*p)->next;
  2221. *p = hwaccel;
  2222. hwaccel->next = NULL;
  2223. }
  2224. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  2225. {
  2226. return hwaccel ? hwaccel->next : first_hwaccel;
  2227. }
  2228. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2229. {
  2230. if (lockmgr_cb) {
  2231. // There is no good way to rollback a failure to destroy the
  2232. // mutex, so we ignore failures.
  2233. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  2234. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  2235. lockmgr_cb = NULL;
  2236. codec_mutex = NULL;
  2237. avformat_mutex = NULL;
  2238. }
  2239. if (cb) {
  2240. void *new_codec_mutex = NULL;
  2241. void *new_avformat_mutex = NULL;
  2242. int err;
  2243. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  2244. return err > 0 ? AVERROR_UNKNOWN : err;
  2245. }
  2246. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  2247. // Ignore failures to destroy the newly created mutex.
  2248. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  2249. return err > 0 ? AVERROR_UNKNOWN : err;
  2250. }
  2251. lockmgr_cb = cb;
  2252. codec_mutex = new_codec_mutex;
  2253. avformat_mutex = new_avformat_mutex;
  2254. }
  2255. return 0;
  2256. }
  2257. int avpriv_lock_avformat(void)
  2258. {
  2259. if (lockmgr_cb) {
  2260. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2261. return -1;
  2262. }
  2263. return 0;
  2264. }
  2265. int avpriv_unlock_avformat(void)
  2266. {
  2267. if (lockmgr_cb) {
  2268. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2269. return -1;
  2270. }
  2271. return 0;
  2272. }
  2273. unsigned int avpriv_toupper4(unsigned int x)
  2274. {
  2275. return av_toupper(x & 0xFF) +
  2276. (av_toupper((x >> 8) & 0xFF) << 8) +
  2277. (av_toupper((x >> 16) & 0xFF) << 16) +
  2278. (av_toupper((x >> 24) & 0xFF) << 24);
  2279. }
  2280. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2281. {
  2282. int ret;
  2283. dst->owner = src->owner;
  2284. ret = av_frame_ref(dst->f, src->f);
  2285. if (ret < 0)
  2286. return ret;
  2287. if (src->progress &&
  2288. !(dst->progress = av_buffer_ref(src->progress))) {
  2289. ff_thread_release_buffer(dst->owner, dst);
  2290. return AVERROR(ENOMEM);
  2291. }
  2292. return 0;
  2293. }
  2294. #if !HAVE_THREADS
  2295. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2296. {
  2297. f->owner = avctx;
  2298. return ff_get_buffer(avctx, f->f, flags);
  2299. }
  2300. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2301. {
  2302. if (f->f)
  2303. av_frame_unref(f->f);
  2304. }
  2305. void ff_thread_finish_setup(AVCodecContext *avctx)
  2306. {
  2307. }
  2308. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2309. {
  2310. }
  2311. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2312. {
  2313. }
  2314. #endif
  2315. int avcodec_is_open(AVCodecContext *s)
  2316. {
  2317. return !!s->internal;
  2318. }
  2319. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  2320. const uint8_t *end,
  2321. uint32_t * restrict state)
  2322. {
  2323. int i;
  2324. assert(p <= end);
  2325. if (p >= end)
  2326. return end;
  2327. for (i = 0; i < 3; i++) {
  2328. uint32_t tmp = *state << 8;
  2329. *state = tmp + *(p++);
  2330. if (tmp == 0x100 || p == end)
  2331. return p;
  2332. }
  2333. while (p < end) {
  2334. if (p[-1] > 1 ) p += 3;
  2335. else if (p[-2] ) p += 2;
  2336. else if (p[-3]|(p[-1]-1)) p++;
  2337. else {
  2338. p++;
  2339. break;
  2340. }
  2341. }
  2342. p = FFMIN(p, end) - 4;
  2343. *state = AV_RB32(p);
  2344. return p + 4;
  2345. }
  2346. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  2347. {
  2348. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  2349. if (!props)
  2350. return NULL;
  2351. if (size)
  2352. *size = sizeof(*props);
  2353. props->vbv_delay = UINT64_MAX;
  2354. return props;
  2355. }
  2356. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  2357. {
  2358. AVPacketSideData *tmp;
  2359. AVCPBProperties *props;
  2360. size_t size;
  2361. props = av_cpb_properties_alloc(&size);
  2362. if (!props)
  2363. return NULL;
  2364. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  2365. if (!tmp) {
  2366. av_freep(&props);
  2367. return NULL;
  2368. }
  2369. avctx->coded_side_data = tmp;
  2370. avctx->nb_coded_side_data++;
  2371. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  2372. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  2373. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  2374. return props;
  2375. }
  2376. static void codec_parameters_reset(AVCodecParameters *par)
  2377. {
  2378. av_freep(&par->extradata);
  2379. memset(par, 0, sizeof(*par));
  2380. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  2381. par->codec_id = AV_CODEC_ID_NONE;
  2382. par->format = -1;
  2383. par->field_order = AV_FIELD_UNKNOWN;
  2384. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  2385. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  2386. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  2387. par->color_space = AVCOL_SPC_UNSPECIFIED;
  2388. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  2389. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  2390. }
  2391. AVCodecParameters *avcodec_parameters_alloc(void)
  2392. {
  2393. AVCodecParameters *par = av_mallocz(sizeof(*par));
  2394. if (!par)
  2395. return NULL;
  2396. codec_parameters_reset(par);
  2397. return par;
  2398. }
  2399. void avcodec_parameters_free(AVCodecParameters **ppar)
  2400. {
  2401. AVCodecParameters *par = *ppar;
  2402. if (!par)
  2403. return;
  2404. codec_parameters_reset(par);
  2405. av_freep(ppar);
  2406. }
  2407. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  2408. {
  2409. codec_parameters_reset(dst);
  2410. memcpy(dst, src, sizeof(*dst));
  2411. dst->extradata = NULL;
  2412. dst->extradata_size = 0;
  2413. if (src->extradata) {
  2414. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  2415. if (!dst->extradata)
  2416. return AVERROR(ENOMEM);
  2417. memcpy(dst->extradata, src->extradata, src->extradata_size);
  2418. dst->extradata_size = src->extradata_size;
  2419. }
  2420. return 0;
  2421. }
  2422. int avcodec_parameters_from_context(AVCodecParameters *par,
  2423. const AVCodecContext *codec)
  2424. {
  2425. codec_parameters_reset(par);
  2426. par->codec_type = codec->codec_type;
  2427. par->codec_id = codec->codec_id;
  2428. par->codec_tag = codec->codec_tag;
  2429. par->bit_rate = codec->bit_rate;
  2430. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  2431. par->profile = codec->profile;
  2432. par->level = codec->level;
  2433. switch (par->codec_type) {
  2434. case AVMEDIA_TYPE_VIDEO:
  2435. par->format = codec->pix_fmt;
  2436. par->width = codec->width;
  2437. par->height = codec->height;
  2438. par->field_order = codec->field_order;
  2439. par->color_range = codec->color_range;
  2440. par->color_primaries = codec->color_primaries;
  2441. par->color_trc = codec->color_trc;
  2442. par->color_space = codec->colorspace;
  2443. par->chroma_location = codec->chroma_sample_location;
  2444. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  2445. break;
  2446. case AVMEDIA_TYPE_AUDIO:
  2447. par->format = codec->sample_fmt;
  2448. par->channel_layout = codec->channel_layout;
  2449. par->channels = codec->channels;
  2450. par->sample_rate = codec->sample_rate;
  2451. par->block_align = codec->block_align;
  2452. par->initial_padding = codec->initial_padding;
  2453. break;
  2454. }
  2455. if (codec->extradata) {
  2456. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  2457. if (!par->extradata)
  2458. return AVERROR(ENOMEM);
  2459. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  2460. par->extradata_size = codec->extradata_size;
  2461. }
  2462. return 0;
  2463. }
  2464. int avcodec_parameters_to_context(AVCodecContext *codec,
  2465. const AVCodecParameters *par)
  2466. {
  2467. codec->codec_type = par->codec_type;
  2468. codec->codec_id = par->codec_id;
  2469. codec->codec_tag = par->codec_tag;
  2470. codec->bit_rate = par->bit_rate;
  2471. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  2472. codec->profile = par->profile;
  2473. codec->level = par->level;
  2474. switch (par->codec_type) {
  2475. case AVMEDIA_TYPE_VIDEO:
  2476. codec->pix_fmt = par->format;
  2477. codec->width = par->width;
  2478. codec->height = par->height;
  2479. codec->field_order = par->field_order;
  2480. codec->color_range = par->color_range;
  2481. codec->color_primaries = par->color_primaries;
  2482. codec->color_trc = par->color_trc;
  2483. codec->colorspace = par->color_space;
  2484. codec->chroma_sample_location = par->chroma_location;
  2485. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  2486. break;
  2487. case AVMEDIA_TYPE_AUDIO:
  2488. codec->sample_fmt = par->format;
  2489. codec->channel_layout = par->channel_layout;
  2490. codec->channels = par->channels;
  2491. codec->sample_rate = par->sample_rate;
  2492. codec->block_align = par->block_align;
  2493. codec->initial_padding = par->initial_padding;
  2494. break;
  2495. }
  2496. if (par->extradata) {
  2497. av_freep(&codec->extradata);
  2498. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  2499. if (!codec->extradata)
  2500. return AVERROR(ENOMEM);
  2501. memcpy(codec->extradata, par->extradata, par->extradata_size);
  2502. codec->extradata_size = par->extradata_size;
  2503. }
  2504. return 0;
  2505. }