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.

2884 lines
87KB

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