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.

2235 lines
75KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/bprint.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/crc.h"
  33. #include "libavutil/frame.h"
  34. #include "libavutil/hwcontext.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/mem_internal.h"
  38. #include "libavutil/pixdesc.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/samplefmt.h"
  41. #include "libavutil/dict.h"
  42. #include "libavutil/thread.h"
  43. #include "avcodec.h"
  44. #include "decode.h"
  45. #include "hwaccel.h"
  46. #include "libavutil/opt.h"
  47. #include "mpegvideo.h"
  48. #include "thread.h"
  49. #include "frame_thread_encoder.h"
  50. #include "internal.h"
  51. #include "raw.h"
  52. #include "bytestream.h"
  53. #include "version.h"
  54. #include <stdlib.h>
  55. #include <stdarg.h>
  56. #include <stdatomic.h>
  57. #include <limits.h>
  58. #include <float.h>
  59. #if CONFIG_ICONV
  60. # include <iconv.h>
  61. #endif
  62. #include "libavutil/ffversion.h"
  63. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  64. static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
  65. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  66. {
  67. uint8_t **p = ptr;
  68. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  69. av_freep(p);
  70. *size = 0;
  71. return;
  72. }
  73. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  74. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  75. }
  76. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. uint8_t **p = ptr;
  79. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  85. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  86. }
  87. int av_codec_is_encoder(const AVCodec *codec)
  88. {
  89. return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
  90. }
  91. int av_codec_is_decoder(const AVCodec *codec)
  92. {
  93. return codec && (codec->decode || codec->receive_frame);
  94. }
  95. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  96. {
  97. int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
  98. if (ret < 0)
  99. width = height = 0;
  100. s->coded_width = width;
  101. s->coded_height = height;
  102. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  103. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  104. return ret;
  105. }
  106. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  107. {
  108. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  109. if (ret < 0) {
  110. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  111. sar.num, sar.den);
  112. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  113. return ret;
  114. } else {
  115. avctx->sample_aspect_ratio = sar;
  116. }
  117. return 0;
  118. }
  119. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  120. enum AVMatrixEncoding matrix_encoding)
  121. {
  122. AVFrameSideData *side_data;
  123. enum AVMatrixEncoding *data;
  124. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  125. if (!side_data)
  126. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  127. sizeof(enum AVMatrixEncoding));
  128. if (!side_data)
  129. return AVERROR(ENOMEM);
  130. data = (enum AVMatrixEncoding*)side_data->data;
  131. *data = matrix_encoding;
  132. return 0;
  133. }
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align = 1;
  139. int h_align = 1;
  140. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  141. if (desc) {
  142. w_align = 1 << desc->log2_chroma_w;
  143. h_align = 1 << desc->log2_chroma_h;
  144. }
  145. switch (s->pix_fmt) {
  146. case AV_PIX_FMT_YUV420P:
  147. case AV_PIX_FMT_YUYV422:
  148. case AV_PIX_FMT_YVYU422:
  149. case AV_PIX_FMT_UYVY422:
  150. case AV_PIX_FMT_YUV422P:
  151. case AV_PIX_FMT_YUV440P:
  152. case AV_PIX_FMT_YUV444P:
  153. case AV_PIX_FMT_GBRP:
  154. case AV_PIX_FMT_GBRAP:
  155. case AV_PIX_FMT_GRAY8:
  156. case AV_PIX_FMT_GRAY16BE:
  157. case AV_PIX_FMT_GRAY16LE:
  158. case AV_PIX_FMT_YUVJ420P:
  159. case AV_PIX_FMT_YUVJ422P:
  160. case AV_PIX_FMT_YUVJ440P:
  161. case AV_PIX_FMT_YUVJ444P:
  162. case AV_PIX_FMT_YUVA420P:
  163. case AV_PIX_FMT_YUVA422P:
  164. case AV_PIX_FMT_YUVA444P:
  165. case AV_PIX_FMT_YUV420P9LE:
  166. case AV_PIX_FMT_YUV420P9BE:
  167. case AV_PIX_FMT_YUV420P10LE:
  168. case AV_PIX_FMT_YUV420P10BE:
  169. case AV_PIX_FMT_YUV420P12LE:
  170. case AV_PIX_FMT_YUV420P12BE:
  171. case AV_PIX_FMT_YUV420P14LE:
  172. case AV_PIX_FMT_YUV420P14BE:
  173. case AV_PIX_FMT_YUV420P16LE:
  174. case AV_PIX_FMT_YUV420P16BE:
  175. case AV_PIX_FMT_YUVA420P9LE:
  176. case AV_PIX_FMT_YUVA420P9BE:
  177. case AV_PIX_FMT_YUVA420P10LE:
  178. case AV_PIX_FMT_YUVA420P10BE:
  179. case AV_PIX_FMT_YUVA420P16LE:
  180. case AV_PIX_FMT_YUVA420P16BE:
  181. case AV_PIX_FMT_YUV422P9LE:
  182. case AV_PIX_FMT_YUV422P9BE:
  183. case AV_PIX_FMT_YUV422P10LE:
  184. case AV_PIX_FMT_YUV422P10BE:
  185. case AV_PIX_FMT_YUV422P12LE:
  186. case AV_PIX_FMT_YUV422P12BE:
  187. case AV_PIX_FMT_YUV422P14LE:
  188. case AV_PIX_FMT_YUV422P14BE:
  189. case AV_PIX_FMT_YUV422P16LE:
  190. case AV_PIX_FMT_YUV422P16BE:
  191. case AV_PIX_FMT_YUVA422P9LE:
  192. case AV_PIX_FMT_YUVA422P9BE:
  193. case AV_PIX_FMT_YUVA422P10LE:
  194. case AV_PIX_FMT_YUVA422P10BE:
  195. case AV_PIX_FMT_YUVA422P12LE:
  196. case AV_PIX_FMT_YUVA422P12BE:
  197. case AV_PIX_FMT_YUVA422P16LE:
  198. case AV_PIX_FMT_YUVA422P16BE:
  199. case AV_PIX_FMT_YUV440P10LE:
  200. case AV_PIX_FMT_YUV440P10BE:
  201. case AV_PIX_FMT_YUV440P12LE:
  202. case AV_PIX_FMT_YUV440P12BE:
  203. case AV_PIX_FMT_YUV444P9LE:
  204. case AV_PIX_FMT_YUV444P9BE:
  205. case AV_PIX_FMT_YUV444P10LE:
  206. case AV_PIX_FMT_YUV444P10BE:
  207. case AV_PIX_FMT_YUV444P12LE:
  208. case AV_PIX_FMT_YUV444P12BE:
  209. case AV_PIX_FMT_YUV444P14LE:
  210. case AV_PIX_FMT_YUV444P14BE:
  211. case AV_PIX_FMT_YUV444P16LE:
  212. case AV_PIX_FMT_YUV444P16BE:
  213. case AV_PIX_FMT_YUVA444P9LE:
  214. case AV_PIX_FMT_YUVA444P9BE:
  215. case AV_PIX_FMT_YUVA444P10LE:
  216. case AV_PIX_FMT_YUVA444P10BE:
  217. case AV_PIX_FMT_YUVA444P12LE:
  218. case AV_PIX_FMT_YUVA444P12BE:
  219. case AV_PIX_FMT_YUVA444P16LE:
  220. case AV_PIX_FMT_YUVA444P16BE:
  221. case AV_PIX_FMT_GBRP9LE:
  222. case AV_PIX_FMT_GBRP9BE:
  223. case AV_PIX_FMT_GBRP10LE:
  224. case AV_PIX_FMT_GBRP10BE:
  225. case AV_PIX_FMT_GBRP12LE:
  226. case AV_PIX_FMT_GBRP12BE:
  227. case AV_PIX_FMT_GBRP14LE:
  228. case AV_PIX_FMT_GBRP14BE:
  229. case AV_PIX_FMT_GBRP16LE:
  230. case AV_PIX_FMT_GBRP16BE:
  231. case AV_PIX_FMT_GBRAP12LE:
  232. case AV_PIX_FMT_GBRAP12BE:
  233. case AV_PIX_FMT_GBRAP16LE:
  234. case AV_PIX_FMT_GBRAP16BE:
  235. w_align = 16; //FIXME assume 16 pixel per macroblock
  236. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  237. break;
  238. case AV_PIX_FMT_YUV411P:
  239. case AV_PIX_FMT_YUVJ411P:
  240. case AV_PIX_FMT_UYYVYY411:
  241. w_align = 32;
  242. h_align = 16 * 2;
  243. break;
  244. case AV_PIX_FMT_YUV410P:
  245. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  246. w_align = 64;
  247. h_align = 64;
  248. }
  249. break;
  250. case AV_PIX_FMT_RGB555:
  251. if (s->codec_id == AV_CODEC_ID_RPZA) {
  252. w_align = 4;
  253. h_align = 4;
  254. }
  255. if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  256. w_align = 8;
  257. h_align = 8;
  258. }
  259. break;
  260. case AV_PIX_FMT_PAL8:
  261. case AV_PIX_FMT_BGR8:
  262. case AV_PIX_FMT_RGB8:
  263. if (s->codec_id == AV_CODEC_ID_SMC ||
  264. s->codec_id == AV_CODEC_ID_CINEPAK) {
  265. w_align = 4;
  266. h_align = 4;
  267. }
  268. if (s->codec_id == AV_CODEC_ID_JV ||
  269. s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  270. w_align = 8;
  271. h_align = 8;
  272. }
  273. break;
  274. case AV_PIX_FMT_BGR24:
  275. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  276. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  277. w_align = 4;
  278. h_align = 4;
  279. }
  280. break;
  281. case AV_PIX_FMT_RGB24:
  282. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  283. w_align = 4;
  284. h_align = 4;
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  291. w_align = FFMAX(w_align, 8);
  292. }
  293. *width = FFALIGN(*width, w_align);
  294. *height = FFALIGN(*height, h_align);
  295. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
  296. s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
  297. s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
  298. ) {
  299. // some of the optimized chroma MC reads one line too much
  300. // which is also done in mpeg decoders with lowres > 0
  301. *height += 2;
  302. // H.264 uses edge emulation for out of frame motion vectors, for this
  303. // it requires a temporary area large enough to hold a 21x21 block,
  304. // increasing witdth ensure that the temporary area is large enough,
  305. // the next rounded up width is 32
  306. *width = FFMAX(*width, 32);
  307. }
  308. for (i = 0; i < 4; i++)
  309. linesize_align[i] = STRIDE_ALIGN;
  310. }
  311. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  312. {
  313. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  314. int chroma_shift = desc->log2_chroma_w;
  315. int linesize_align[AV_NUM_DATA_POINTERS];
  316. int align;
  317. avcodec_align_dimensions2(s, width, height, linesize_align);
  318. align = FFMAX(linesize_align[0], linesize_align[3]);
  319. linesize_align[1] <<= chroma_shift;
  320. linesize_align[2] <<= chroma_shift;
  321. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  322. *width = FFALIGN(*width, align);
  323. }
  324. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  325. {
  326. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  327. return AVERROR(EINVAL);
  328. pos--;
  329. *xpos = (pos&1) * 128;
  330. *ypos = ((pos>>1)^(pos<4)) * 128;
  331. return 0;
  332. }
  333. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  334. {
  335. int pos, xout, yout;
  336. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  337. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  338. return pos;
  339. }
  340. return AVCHROMA_LOC_UNSPECIFIED;
  341. }
  342. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  343. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  344. int buf_size, int align)
  345. {
  346. int ch, planar, needed_size, ret = 0;
  347. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  348. frame->nb_samples, sample_fmt,
  349. align);
  350. if (buf_size < needed_size)
  351. return AVERROR(EINVAL);
  352. planar = av_sample_fmt_is_planar(sample_fmt);
  353. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  354. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  355. sizeof(*frame->extended_data))))
  356. return AVERROR(ENOMEM);
  357. } else {
  358. frame->extended_data = frame->data;
  359. }
  360. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  361. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  362. sample_fmt, align)) < 0) {
  363. if (frame->extended_data != frame->data)
  364. av_freep(&frame->extended_data);
  365. return ret;
  366. }
  367. if (frame->extended_data != frame->data) {
  368. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  369. frame->data[ch] = frame->extended_data[ch];
  370. }
  371. return ret;
  372. }
  373. void ff_color_frame(AVFrame *frame, const int c[4])
  374. {
  375. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  376. int p, y, x;
  377. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  378. for (p = 0; p<desc->nb_components; p++) {
  379. uint8_t *dst = frame->data[p];
  380. int is_chroma = p == 1 || p == 2;
  381. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  382. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  383. for (y = 0; y < height; y++) {
  384. if (desc->comp[0].depth >= 9) {
  385. for (x = 0; x<bytes; x++)
  386. ((uint16_t*)dst)[x] = c[p];
  387. }else
  388. memset(dst, c[p], bytes);
  389. dst += frame->linesize[p];
  390. }
  391. }
  392. }
  393. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  394. {
  395. int i;
  396. for (i = 0; i < count; i++) {
  397. int r = func(c, (char *)arg + i * size);
  398. if (ret)
  399. ret[i] = r;
  400. }
  401. emms_c();
  402. return 0;
  403. }
  404. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  405. {
  406. int i;
  407. for (i = 0; i < count; i++) {
  408. int r = func(c, arg, i, 0);
  409. if (ret)
  410. ret[i] = r;
  411. }
  412. emms_c();
  413. return 0;
  414. }
  415. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  416. unsigned int fourcc)
  417. {
  418. while (tags->pix_fmt >= 0) {
  419. if (tags->fourcc == fourcc)
  420. return tags->pix_fmt;
  421. tags++;
  422. }
  423. return AV_PIX_FMT_NONE;
  424. }
  425. #if FF_API_CODEC_GET_SET
  426. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  427. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  428. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  429. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  430. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  431. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  432. {
  433. return codec->properties;
  434. }
  435. int av_codec_get_max_lowres(const AVCodec *codec)
  436. {
  437. return codec->max_lowres;
  438. }
  439. #endif
  440. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  441. return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  442. }
  443. static int64_t get_bit_rate(AVCodecContext *ctx)
  444. {
  445. int64_t bit_rate;
  446. int bits_per_sample;
  447. switch (ctx->codec_type) {
  448. case AVMEDIA_TYPE_VIDEO:
  449. case AVMEDIA_TYPE_DATA:
  450. case AVMEDIA_TYPE_SUBTITLE:
  451. case AVMEDIA_TYPE_ATTACHMENT:
  452. bit_rate = ctx->bit_rate;
  453. break;
  454. case AVMEDIA_TYPE_AUDIO:
  455. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  456. bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
  457. break;
  458. default:
  459. bit_rate = 0;
  460. break;
  461. }
  462. return bit_rate;
  463. }
  464. static void ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  465. {
  466. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  467. ff_mutex_lock(&codec_mutex);
  468. }
  469. static void ff_unlock_avcodec(const AVCodec *codec)
  470. {
  471. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  472. ff_mutex_unlock(&codec_mutex);
  473. }
  474. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  475. {
  476. int ret = 0;
  477. ff_unlock_avcodec(codec);
  478. ret = avcodec_open2(avctx, codec, options);
  479. ff_lock_avcodec(avctx, codec);
  480. return ret;
  481. }
  482. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  483. {
  484. int ret = 0;
  485. int codec_init_ok = 0;
  486. AVDictionary *tmp = NULL;
  487. const AVPixFmtDescriptor *pixdesc;
  488. if (avcodec_is_open(avctx))
  489. return 0;
  490. if ((!codec && !avctx->codec)) {
  491. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  492. return AVERROR(EINVAL);
  493. }
  494. if ((codec && avctx->codec && codec != avctx->codec)) {
  495. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  496. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  497. return AVERROR(EINVAL);
  498. }
  499. if (!codec)
  500. codec = avctx->codec;
  501. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  502. return AVERROR(EINVAL);
  503. if (options)
  504. av_dict_copy(&tmp, *options, 0);
  505. ff_lock_avcodec(avctx, codec);
  506. avctx->internal = av_mallocz(sizeof(*avctx->internal));
  507. if (!avctx->internal) {
  508. ret = AVERROR(ENOMEM);
  509. goto end;
  510. }
  511. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  512. if (!avctx->internal->pool) {
  513. ret = AVERROR(ENOMEM);
  514. goto free_and_end;
  515. }
  516. avctx->internal->to_free = av_frame_alloc();
  517. if (!avctx->internal->to_free) {
  518. ret = AVERROR(ENOMEM);
  519. goto free_and_end;
  520. }
  521. avctx->internal->compat_decode_frame = av_frame_alloc();
  522. if (!avctx->internal->compat_decode_frame) {
  523. ret = AVERROR(ENOMEM);
  524. goto free_and_end;
  525. }
  526. avctx->internal->buffer_frame = av_frame_alloc();
  527. if (!avctx->internal->buffer_frame) {
  528. ret = AVERROR(ENOMEM);
  529. goto free_and_end;
  530. }
  531. avctx->internal->buffer_pkt = av_packet_alloc();
  532. if (!avctx->internal->buffer_pkt) {
  533. ret = AVERROR(ENOMEM);
  534. goto free_and_end;
  535. }
  536. avctx->internal->ds.in_pkt = av_packet_alloc();
  537. if (!avctx->internal->ds.in_pkt) {
  538. ret = AVERROR(ENOMEM);
  539. goto free_and_end;
  540. }
  541. avctx->internal->last_pkt_props = av_packet_alloc();
  542. if (!avctx->internal->last_pkt_props) {
  543. ret = AVERROR(ENOMEM);
  544. goto free_and_end;
  545. }
  546. avctx->internal->skip_samples_multiplier = 1;
  547. if (codec->priv_data_size > 0) {
  548. if (!avctx->priv_data) {
  549. avctx->priv_data = av_mallocz(codec->priv_data_size);
  550. if (!avctx->priv_data) {
  551. ret = AVERROR(ENOMEM);
  552. goto end;
  553. }
  554. if (codec->priv_class) {
  555. *(const AVClass **)avctx->priv_data = codec->priv_class;
  556. av_opt_set_defaults(avctx->priv_data);
  557. }
  558. }
  559. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  560. goto free_and_end;
  561. } else {
  562. avctx->priv_data = NULL;
  563. }
  564. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  565. goto free_and_end;
  566. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  567. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  568. ret = AVERROR(EINVAL);
  569. goto free_and_end;
  570. }
  571. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  572. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  573. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  574. if (avctx->coded_width && avctx->coded_height)
  575. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  576. else if (avctx->width && avctx->height)
  577. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  578. if (ret < 0)
  579. goto free_and_end;
  580. }
  581. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  582. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  583. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  584. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  585. ff_set_dimensions(avctx, 0, 0);
  586. }
  587. if (avctx->width > 0 && avctx->height > 0) {
  588. if (av_image_check_sar(avctx->width, avctx->height,
  589. avctx->sample_aspect_ratio) < 0) {
  590. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  591. avctx->sample_aspect_ratio.num,
  592. avctx->sample_aspect_ratio.den);
  593. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  594. }
  595. }
  596. /* if the decoder init function was already called previously,
  597. * free the already allocated subtitle_header before overwriting it */
  598. if (av_codec_is_decoder(codec))
  599. av_freep(&avctx->subtitle_header);
  600. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  601. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->channels);
  602. ret = AVERROR(EINVAL);
  603. goto free_and_end;
  604. }
  605. avctx->codec = codec;
  606. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  607. avctx->codec_id == AV_CODEC_ID_NONE) {
  608. avctx->codec_type = codec->type;
  609. avctx->codec_id = codec->id;
  610. }
  611. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  612. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  613. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  614. ret = AVERROR(EINVAL);
  615. goto free_and_end;
  616. }
  617. avctx->frame_number = 0;
  618. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  619. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  620. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  621. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  622. AVCodec *codec2;
  623. av_log(avctx, AV_LOG_ERROR,
  624. "The %s '%s' is experimental but experimental codecs are not enabled, "
  625. "add '-strict %d' if you want to use it.\n",
  626. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  627. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  628. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  629. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  630. codec_string, codec2->name);
  631. ret = AVERROR_EXPERIMENTAL;
  632. goto free_and_end;
  633. }
  634. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  635. (!avctx->time_base.num || !avctx->time_base.den)) {
  636. avctx->time_base.num = 1;
  637. avctx->time_base.den = avctx->sample_rate;
  638. }
  639. if (!HAVE_THREADS)
  640. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  641. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  642. ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  643. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  644. ff_lock_avcodec(avctx, codec);
  645. if (ret < 0)
  646. goto free_and_end;
  647. }
  648. if (av_codec_is_decoder(avctx->codec)) {
  649. ret = ff_decode_bsfs_init(avctx);
  650. if (ret < 0)
  651. goto free_and_end;
  652. }
  653. if (HAVE_THREADS
  654. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  655. ret = ff_thread_init(avctx);
  656. if (ret < 0) {
  657. goto free_and_end;
  658. }
  659. }
  660. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  661. avctx->thread_count = 1;
  662. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  663. av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
  664. avctx->codec->max_lowres);
  665. avctx->lowres = avctx->codec->max_lowres;
  666. }
  667. if (av_codec_is_encoder(avctx->codec)) {
  668. int i;
  669. #if FF_API_CODED_FRAME
  670. FF_DISABLE_DEPRECATION_WARNINGS
  671. avctx->coded_frame = av_frame_alloc();
  672. if (!avctx->coded_frame) {
  673. ret = AVERROR(ENOMEM);
  674. goto free_and_end;
  675. }
  676. FF_ENABLE_DEPRECATION_WARNINGS
  677. #endif
  678. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  679. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  680. ret = AVERROR(EINVAL);
  681. goto free_and_end;
  682. }
  683. if (avctx->codec->sample_fmts) {
  684. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  685. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  686. break;
  687. if (avctx->channels == 1 &&
  688. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  689. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  690. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  691. break;
  692. }
  693. }
  694. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  695. char buf[128];
  696. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  697. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  698. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  699. ret = AVERROR(EINVAL);
  700. goto free_and_end;
  701. }
  702. }
  703. if (avctx->codec->pix_fmts) {
  704. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  705. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  706. break;
  707. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  708. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  709. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  710. char buf[128];
  711. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  712. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  713. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  714. ret = AVERROR(EINVAL);
  715. goto free_and_end;
  716. }
  717. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  718. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  719. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  720. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  721. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  722. avctx->color_range = AVCOL_RANGE_JPEG;
  723. }
  724. if (avctx->codec->supported_samplerates) {
  725. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  726. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  727. break;
  728. if (avctx->codec->supported_samplerates[i] == 0) {
  729. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  730. avctx->sample_rate);
  731. ret = AVERROR(EINVAL);
  732. goto free_and_end;
  733. }
  734. }
  735. if (avctx->sample_rate < 0) {
  736. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  737. avctx->sample_rate);
  738. ret = AVERROR(EINVAL);
  739. goto free_and_end;
  740. }
  741. if (avctx->codec->channel_layouts) {
  742. if (!avctx->channel_layout) {
  743. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  744. } else {
  745. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  746. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  747. break;
  748. if (avctx->codec->channel_layouts[i] == 0) {
  749. char buf[512];
  750. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  751. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  752. ret = AVERROR(EINVAL);
  753. goto free_and_end;
  754. }
  755. }
  756. }
  757. if (avctx->channel_layout && avctx->channels) {
  758. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  759. if (channels != avctx->channels) {
  760. char buf[512];
  761. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  762. av_log(avctx, AV_LOG_ERROR,
  763. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  764. buf, channels, avctx->channels);
  765. ret = AVERROR(EINVAL);
  766. goto free_and_end;
  767. }
  768. } else if (avctx->channel_layout) {
  769. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  770. }
  771. if (avctx->channels < 0) {
  772. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  773. avctx->channels);
  774. ret = AVERROR(EINVAL);
  775. goto free_and_end;
  776. }
  777. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  778. pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
  779. if ( avctx->bits_per_raw_sample < 0
  780. || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
  781. av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
  782. avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
  783. avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
  784. }
  785. if (avctx->width <= 0 || avctx->height <= 0) {
  786. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  787. ret = AVERROR(EINVAL);
  788. goto free_and_end;
  789. }
  790. }
  791. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  792. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  793. av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
  794. }
  795. if (!avctx->rc_initial_buffer_occupancy)
  796. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
  797. if (avctx->ticks_per_frame && avctx->time_base.num &&
  798. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  799. av_log(avctx, AV_LOG_ERROR,
  800. "ticks_per_frame %d too large for the timebase %d/%d.",
  801. avctx->ticks_per_frame,
  802. avctx->time_base.num,
  803. avctx->time_base.den);
  804. goto free_and_end;
  805. }
  806. if (avctx->hw_frames_ctx) {
  807. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  808. if (frames_ctx->format != avctx->pix_fmt) {
  809. av_log(avctx, AV_LOG_ERROR,
  810. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  811. ret = AVERROR(EINVAL);
  812. goto free_and_end;
  813. }
  814. if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
  815. avctx->sw_pix_fmt != frames_ctx->sw_format) {
  816. av_log(avctx, AV_LOG_ERROR,
  817. "Mismatching AVCodecContext.sw_pix_fmt (%s) "
  818. "and AVHWFramesContext.sw_format (%s)\n",
  819. av_get_pix_fmt_name(avctx->sw_pix_fmt),
  820. av_get_pix_fmt_name(frames_ctx->sw_format));
  821. ret = AVERROR(EINVAL);
  822. goto free_and_end;
  823. }
  824. avctx->sw_pix_fmt = frames_ctx->sw_format;
  825. }
  826. }
  827. avctx->pts_correction_num_faulty_pts =
  828. avctx->pts_correction_num_faulty_dts = 0;
  829. avctx->pts_correction_last_pts =
  830. avctx->pts_correction_last_dts = INT64_MIN;
  831. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  832. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  833. av_log(avctx, AV_LOG_WARNING,
  834. "gray decoding requested but not enabled at configuration time\n");
  835. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  836. || avctx->internal->frame_thread_encoder)) {
  837. ret = avctx->codec->init(avctx);
  838. if (ret < 0) {
  839. goto free_and_end;
  840. }
  841. codec_init_ok = 1;
  842. }
  843. ret=0;
  844. if (av_codec_is_decoder(avctx->codec)) {
  845. if (!avctx->bit_rate)
  846. avctx->bit_rate = get_bit_rate(avctx);
  847. /* validate channel layout from the decoder */
  848. if (avctx->channel_layout) {
  849. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  850. if (!avctx->channels)
  851. avctx->channels = channels;
  852. else if (channels != avctx->channels) {
  853. char buf[512];
  854. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  855. av_log(avctx, AV_LOG_WARNING,
  856. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  857. "ignoring specified channel layout\n",
  858. buf, channels, avctx->channels);
  859. avctx->channel_layout = 0;
  860. }
  861. }
  862. if (avctx->channels && avctx->channels < 0 ||
  863. avctx->channels > FF_SANE_NB_CHANNELS) {
  864. ret = AVERROR(EINVAL);
  865. goto free_and_end;
  866. }
  867. if (avctx->bits_per_coded_sample < 0) {
  868. ret = AVERROR(EINVAL);
  869. goto free_and_end;
  870. }
  871. if (avctx->sub_charenc) {
  872. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  873. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  874. "supported with subtitles codecs\n");
  875. ret = AVERROR(EINVAL);
  876. goto free_and_end;
  877. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  878. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  879. "subtitles character encoding will be ignored\n",
  880. avctx->codec_descriptor->name);
  881. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  882. } else {
  883. /* input character encoding is set for a text based subtitle
  884. * codec at this point */
  885. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  886. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  887. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  888. #if CONFIG_ICONV
  889. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  890. if (cd == (iconv_t)-1) {
  891. ret = AVERROR(errno);
  892. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  893. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  894. goto free_and_end;
  895. }
  896. iconv_close(cd);
  897. #else
  898. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  899. "conversion needs a libavcodec built with iconv support "
  900. "for this codec\n");
  901. ret = AVERROR(ENOSYS);
  902. goto free_and_end;
  903. #endif
  904. }
  905. }
  906. }
  907. #if FF_API_AVCTX_TIMEBASE
  908. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  909. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  910. #endif
  911. }
  912. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  913. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  914. }
  915. end:
  916. ff_unlock_avcodec(codec);
  917. if (options) {
  918. av_dict_free(options);
  919. *options = tmp;
  920. }
  921. return ret;
  922. free_and_end:
  923. if (avctx->codec && avctx->codec->close &&
  924. (codec_init_ok ||
  925. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  926. avctx->codec->close(avctx);
  927. if (codec->priv_class && codec->priv_data_size)
  928. av_opt_free(avctx->priv_data);
  929. av_opt_free(avctx);
  930. #if FF_API_CODED_FRAME
  931. FF_DISABLE_DEPRECATION_WARNINGS
  932. av_frame_free(&avctx->coded_frame);
  933. FF_ENABLE_DEPRECATION_WARNINGS
  934. #endif
  935. av_dict_free(&tmp);
  936. av_freep(&avctx->priv_data);
  937. av_freep(&avctx->subtitle_header);
  938. if (avctx->internal) {
  939. av_frame_free(&avctx->internal->to_free);
  940. av_frame_free(&avctx->internal->compat_decode_frame);
  941. av_frame_free(&avctx->internal->buffer_frame);
  942. av_packet_free(&avctx->internal->buffer_pkt);
  943. av_packet_free(&avctx->internal->last_pkt_props);
  944. av_packet_free(&avctx->internal->ds.in_pkt);
  945. ff_decode_bsfs_uninit(avctx);
  946. av_freep(&avctx->internal->pool);
  947. }
  948. av_freep(&avctx->internal);
  949. avctx->codec = NULL;
  950. goto end;
  951. }
  952. void avsubtitle_free(AVSubtitle *sub)
  953. {
  954. int i;
  955. for (i = 0; i < sub->num_rects; i++) {
  956. av_freep(&sub->rects[i]->data[0]);
  957. av_freep(&sub->rects[i]->data[1]);
  958. av_freep(&sub->rects[i]->data[2]);
  959. av_freep(&sub->rects[i]->data[3]);
  960. av_freep(&sub->rects[i]->text);
  961. av_freep(&sub->rects[i]->ass);
  962. av_freep(&sub->rects[i]);
  963. }
  964. av_freep(&sub->rects);
  965. memset(sub, 0, sizeof(*sub));
  966. }
  967. av_cold int avcodec_close(AVCodecContext *avctx)
  968. {
  969. int i;
  970. if (!avctx)
  971. return 0;
  972. if (avcodec_is_open(avctx)) {
  973. FramePool *pool = avctx->internal->pool;
  974. if (CONFIG_FRAME_THREAD_ENCODER &&
  975. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  976. ff_frame_thread_encoder_free(avctx);
  977. }
  978. if (HAVE_THREADS && avctx->internal->thread_ctx)
  979. ff_thread_free(avctx);
  980. if (avctx->codec && avctx->codec->close)
  981. avctx->codec->close(avctx);
  982. avctx->internal->byte_buffer_size = 0;
  983. av_freep(&avctx->internal->byte_buffer);
  984. av_frame_free(&avctx->internal->to_free);
  985. av_frame_free(&avctx->internal->compat_decode_frame);
  986. av_frame_free(&avctx->internal->buffer_frame);
  987. av_packet_free(&avctx->internal->buffer_pkt);
  988. av_packet_free(&avctx->internal->last_pkt_props);
  989. av_packet_free(&avctx->internal->ds.in_pkt);
  990. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  991. av_buffer_pool_uninit(&pool->pools[i]);
  992. av_freep(&avctx->internal->pool);
  993. if (avctx->hwaccel && avctx->hwaccel->uninit)
  994. avctx->hwaccel->uninit(avctx);
  995. av_freep(&avctx->internal->hwaccel_priv_data);
  996. ff_decode_bsfs_uninit(avctx);
  997. av_freep(&avctx->internal);
  998. }
  999. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1000. av_freep(&avctx->coded_side_data[i].data);
  1001. av_freep(&avctx->coded_side_data);
  1002. avctx->nb_coded_side_data = 0;
  1003. av_buffer_unref(&avctx->hw_frames_ctx);
  1004. av_buffer_unref(&avctx->hw_device_ctx);
  1005. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1006. av_opt_free(avctx->priv_data);
  1007. av_opt_free(avctx);
  1008. av_freep(&avctx->priv_data);
  1009. if (av_codec_is_encoder(avctx->codec)) {
  1010. av_freep(&avctx->extradata);
  1011. #if FF_API_CODED_FRAME
  1012. FF_DISABLE_DEPRECATION_WARNINGS
  1013. av_frame_free(&avctx->coded_frame);
  1014. FF_ENABLE_DEPRECATION_WARNINGS
  1015. #endif
  1016. }
  1017. avctx->codec = NULL;
  1018. avctx->active_thread_type = 0;
  1019. return 0;
  1020. }
  1021. const char *avcodec_get_name(enum AVCodecID id)
  1022. {
  1023. const AVCodecDescriptor *cd;
  1024. AVCodec *codec;
  1025. if (id == AV_CODEC_ID_NONE)
  1026. return "none";
  1027. cd = avcodec_descriptor_get(id);
  1028. if (cd)
  1029. return cd->name;
  1030. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1031. codec = avcodec_find_decoder(id);
  1032. if (codec)
  1033. return codec->name;
  1034. codec = avcodec_find_encoder(id);
  1035. if (codec)
  1036. return codec->name;
  1037. return "unknown_codec";
  1038. }
  1039. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1040. {
  1041. int i, len, ret = 0;
  1042. #define TAG_PRINT(x) \
  1043. (((x) >= '0' && (x) <= '9') || \
  1044. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1045. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1046. for (i = 0; i < 4; i++) {
  1047. len = snprintf(buf, buf_size,
  1048. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1049. buf += len;
  1050. buf_size = buf_size > len ? buf_size - len : 0;
  1051. ret += len;
  1052. codec_tag >>= 8;
  1053. }
  1054. return ret;
  1055. }
  1056. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1057. {
  1058. const char *codec_type;
  1059. const char *codec_name;
  1060. const char *profile = NULL;
  1061. int64_t bitrate;
  1062. int new_line = 0;
  1063. AVRational display_aspect_ratio;
  1064. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  1065. if (!buf || buf_size <= 0)
  1066. return;
  1067. codec_type = av_get_media_type_string(enc->codec_type);
  1068. codec_name = avcodec_get_name(enc->codec_id);
  1069. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  1070. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  1071. codec_name);
  1072. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1073. if (enc->codec && strcmp(enc->codec->name, codec_name))
  1074. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  1075. if (profile)
  1076. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1077. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  1078. && av_log_get_level() >= AV_LOG_VERBOSE
  1079. && enc->refs)
  1080. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1081. ", %d reference frame%s",
  1082. enc->refs, enc->refs > 1 ? "s" : "");
  1083. if (enc->codec_tag)
  1084. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  1085. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  1086. switch (enc->codec_type) {
  1087. case AVMEDIA_TYPE_VIDEO:
  1088. {
  1089. char detail[256] = "(";
  1090. av_strlcat(buf, separator, buf_size);
  1091. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1092. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1093. av_get_pix_fmt_name(enc->pix_fmt));
  1094. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  1095. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  1096. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  1097. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1098. av_strlcatf(detail, sizeof(detail), "%s, ",
  1099. av_color_range_name(enc->color_range));
  1100. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1101. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1102. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1103. if (enc->colorspace != (int)enc->color_primaries ||
  1104. enc->colorspace != (int)enc->color_trc) {
  1105. new_line = 1;
  1106. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  1107. av_color_space_name(enc->colorspace),
  1108. av_color_primaries_name(enc->color_primaries),
  1109. av_color_transfer_name(enc->color_trc));
  1110. } else
  1111. av_strlcatf(detail, sizeof(detail), "%s, ",
  1112. av_get_colorspace_name(enc->colorspace));
  1113. }
  1114. if (enc->field_order != AV_FIELD_UNKNOWN) {
  1115. const char *field_order = "progressive";
  1116. if (enc->field_order == AV_FIELD_TT)
  1117. field_order = "top first";
  1118. else if (enc->field_order == AV_FIELD_BB)
  1119. field_order = "bottom first";
  1120. else if (enc->field_order == AV_FIELD_TB)
  1121. field_order = "top coded first (swapped)";
  1122. else if (enc->field_order == AV_FIELD_BT)
  1123. field_order = "bottom coded first (swapped)";
  1124. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  1125. }
  1126. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1127. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1128. av_strlcatf(detail, sizeof(detail), "%s, ",
  1129. av_chroma_location_name(enc->chroma_sample_location));
  1130. if (strlen(detail) > 1) {
  1131. detail[strlen(detail) - 2] = 0;
  1132. av_strlcatf(buf, buf_size, "%s)", detail);
  1133. }
  1134. }
  1135. if (enc->width) {
  1136. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  1137. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1138. "%dx%d",
  1139. enc->width, enc->height);
  1140. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1141. (enc->width != enc->coded_width ||
  1142. enc->height != enc->coded_height))
  1143. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1144. " (%dx%d)", enc->coded_width, enc->coded_height);
  1145. if (enc->sample_aspect_ratio.num) {
  1146. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1147. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  1148. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  1149. 1024 * 1024);
  1150. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1151. " [SAR %d:%d DAR %d:%d]",
  1152. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1153. display_aspect_ratio.num, display_aspect_ratio.den);
  1154. }
  1155. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1156. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1157. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1158. ", %d/%d",
  1159. enc->time_base.num / g, enc->time_base.den / g);
  1160. }
  1161. }
  1162. if (encode) {
  1163. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1164. ", q=%d-%d", enc->qmin, enc->qmax);
  1165. } else {
  1166. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  1167. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1168. ", Closed Captions");
  1169. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  1170. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1171. ", lossless");
  1172. }
  1173. break;
  1174. case AVMEDIA_TYPE_AUDIO:
  1175. av_strlcat(buf, separator, buf_size);
  1176. if (enc->sample_rate) {
  1177. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1178. "%d Hz, ", enc->sample_rate);
  1179. }
  1180. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1181. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1182. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1183. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1184. }
  1185. if ( enc->bits_per_raw_sample > 0
  1186. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  1187. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1188. " (%d bit)", enc->bits_per_raw_sample);
  1189. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  1190. if (enc->initial_padding)
  1191. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1192. ", delay %d", enc->initial_padding);
  1193. if (enc->trailing_padding)
  1194. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1195. ", padding %d", enc->trailing_padding);
  1196. }
  1197. break;
  1198. case AVMEDIA_TYPE_DATA:
  1199. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1200. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1201. if (g)
  1202. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1203. ", %d/%d",
  1204. enc->time_base.num / g, enc->time_base.den / g);
  1205. }
  1206. break;
  1207. case AVMEDIA_TYPE_SUBTITLE:
  1208. if (enc->width)
  1209. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1210. ", %dx%d", enc->width, enc->height);
  1211. break;
  1212. default:
  1213. return;
  1214. }
  1215. if (encode) {
  1216. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1217. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1218. ", pass 1");
  1219. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1220. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1221. ", pass 2");
  1222. }
  1223. bitrate = get_bit_rate(enc);
  1224. if (bitrate != 0) {
  1225. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1226. ", %"PRId64" kb/s", bitrate / 1000);
  1227. } else if (enc->rc_max_rate > 0) {
  1228. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1229. ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  1230. }
  1231. }
  1232. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1233. {
  1234. const AVProfile *p;
  1235. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1236. return NULL;
  1237. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1238. if (p->profile == profile)
  1239. return p->name;
  1240. return NULL;
  1241. }
  1242. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1243. {
  1244. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1245. const AVProfile *p;
  1246. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1247. return NULL;
  1248. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1249. if (p->profile == profile)
  1250. return p->name;
  1251. return NULL;
  1252. }
  1253. unsigned avcodec_version(void)
  1254. {
  1255. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1256. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1257. av_assert0(AV_CODEC_ID_SRT==94216);
  1258. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1259. return LIBAVCODEC_VERSION_INT;
  1260. }
  1261. const char *avcodec_configuration(void)
  1262. {
  1263. return FFMPEG_CONFIGURATION;
  1264. }
  1265. const char *avcodec_license(void)
  1266. {
  1267. #define LICENSE_PREFIX "libavcodec license: "
  1268. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1269. }
  1270. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1271. {
  1272. switch (codec_id) {
  1273. case AV_CODEC_ID_8SVX_EXP:
  1274. case AV_CODEC_ID_8SVX_FIB:
  1275. case AV_CODEC_ID_ADPCM_CT:
  1276. case AV_CODEC_ID_ADPCM_IMA_APC:
  1277. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1278. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1279. case AV_CODEC_ID_ADPCM_IMA_WS:
  1280. case AV_CODEC_ID_ADPCM_G722:
  1281. case AV_CODEC_ID_ADPCM_YAMAHA:
  1282. case AV_CODEC_ID_ADPCM_AICA:
  1283. return 4;
  1284. case AV_CODEC_ID_DSD_LSBF:
  1285. case AV_CODEC_ID_DSD_MSBF:
  1286. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  1287. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  1288. case AV_CODEC_ID_PCM_ALAW:
  1289. case AV_CODEC_ID_PCM_MULAW:
  1290. case AV_CODEC_ID_PCM_VIDC:
  1291. case AV_CODEC_ID_PCM_S8:
  1292. case AV_CODEC_ID_PCM_S8_PLANAR:
  1293. case AV_CODEC_ID_PCM_U8:
  1294. case AV_CODEC_ID_PCM_ZORK:
  1295. case AV_CODEC_ID_SDX2_DPCM:
  1296. return 8;
  1297. case AV_CODEC_ID_PCM_S16BE:
  1298. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1299. case AV_CODEC_ID_PCM_S16LE:
  1300. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1301. case AV_CODEC_ID_PCM_U16BE:
  1302. case AV_CODEC_ID_PCM_U16LE:
  1303. return 16;
  1304. case AV_CODEC_ID_PCM_S24DAUD:
  1305. case AV_CODEC_ID_PCM_S24BE:
  1306. case AV_CODEC_ID_PCM_S24LE:
  1307. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1308. case AV_CODEC_ID_PCM_U24BE:
  1309. case AV_CODEC_ID_PCM_U24LE:
  1310. return 24;
  1311. case AV_CODEC_ID_PCM_S32BE:
  1312. case AV_CODEC_ID_PCM_S32LE:
  1313. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1314. case AV_CODEC_ID_PCM_U32BE:
  1315. case AV_CODEC_ID_PCM_U32LE:
  1316. case AV_CODEC_ID_PCM_F32BE:
  1317. case AV_CODEC_ID_PCM_F32LE:
  1318. case AV_CODEC_ID_PCM_F24LE:
  1319. case AV_CODEC_ID_PCM_F16LE:
  1320. return 32;
  1321. case AV_CODEC_ID_PCM_F64BE:
  1322. case AV_CODEC_ID_PCM_F64LE:
  1323. case AV_CODEC_ID_PCM_S64BE:
  1324. case AV_CODEC_ID_PCM_S64LE:
  1325. return 64;
  1326. default:
  1327. return 0;
  1328. }
  1329. }
  1330. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1331. {
  1332. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  1333. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1334. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1335. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1336. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1337. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1338. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1339. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1340. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1341. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  1342. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1343. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1344. };
  1345. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1346. return AV_CODEC_ID_NONE;
  1347. if (be < 0 || be > 1)
  1348. be = AV_NE(1, 0);
  1349. return map[fmt][be];
  1350. }
  1351. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1352. {
  1353. switch (codec_id) {
  1354. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1355. return 2;
  1356. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1357. return 3;
  1358. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1359. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1360. case AV_CODEC_ID_ADPCM_IMA_QT:
  1361. case AV_CODEC_ID_ADPCM_SWF:
  1362. case AV_CODEC_ID_ADPCM_MS:
  1363. return 4;
  1364. default:
  1365. return av_get_exact_bits_per_sample(codec_id);
  1366. }
  1367. }
  1368. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1369. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  1370. uint8_t * extradata, int frame_size, int frame_bytes)
  1371. {
  1372. int bps = av_get_exact_bits_per_sample(id);
  1373. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  1374. /* codecs with an exact constant bits per sample */
  1375. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1376. return (frame_bytes * 8LL) / (bps * ch);
  1377. bps = bits_per_coded_sample;
  1378. /* codecs with a fixed packet duration */
  1379. switch (id) {
  1380. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1381. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1382. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1383. case AV_CODEC_ID_AMR_NB:
  1384. case AV_CODEC_ID_EVRC:
  1385. case AV_CODEC_ID_GSM:
  1386. case AV_CODEC_ID_QCELP:
  1387. case AV_CODEC_ID_RA_288: return 160;
  1388. case AV_CODEC_ID_AMR_WB:
  1389. case AV_CODEC_ID_GSM_MS: return 320;
  1390. case AV_CODEC_ID_MP1: return 384;
  1391. case AV_CODEC_ID_ATRAC1: return 512;
  1392. case AV_CODEC_ID_ATRAC9:
  1393. case AV_CODEC_ID_ATRAC3: return 1024 * framecount;
  1394. case AV_CODEC_ID_ATRAC3P: return 2048;
  1395. case AV_CODEC_ID_MP2:
  1396. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1397. case AV_CODEC_ID_AC3: return 1536;
  1398. }
  1399. if (sr > 0) {
  1400. /* calc from sample rate */
  1401. if (id == AV_CODEC_ID_TTA)
  1402. return 256 * sr / 245;
  1403. else if (id == AV_CODEC_ID_DST)
  1404. return 588 * sr / 44100;
  1405. if (ch > 0) {
  1406. /* calc from sample rate and channels */
  1407. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1408. return (480 << (sr / 22050)) / ch;
  1409. }
  1410. if (id == AV_CODEC_ID_MP3)
  1411. return sr <= 24000 ? 576 : 1152;
  1412. }
  1413. if (ba > 0) {
  1414. /* calc from block_align */
  1415. if (id == AV_CODEC_ID_SIPR) {
  1416. switch (ba) {
  1417. case 20: return 160;
  1418. case 19: return 144;
  1419. case 29: return 288;
  1420. case 37: return 480;
  1421. }
  1422. } else if (id == AV_CODEC_ID_ILBC) {
  1423. switch (ba) {
  1424. case 38: return 160;
  1425. case 50: return 240;
  1426. }
  1427. }
  1428. }
  1429. if (frame_bytes > 0) {
  1430. /* calc from frame_bytes only */
  1431. if (id == AV_CODEC_ID_TRUESPEECH)
  1432. return 240 * (frame_bytes / 32);
  1433. if (id == AV_CODEC_ID_NELLYMOSER)
  1434. return 256 * (frame_bytes / 64);
  1435. if (id == AV_CODEC_ID_RA_144)
  1436. return 160 * (frame_bytes / 20);
  1437. if (bps > 0) {
  1438. /* calc from frame_bytes and bits_per_coded_sample */
  1439. if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
  1440. return frame_bytes * 8 / bps;
  1441. }
  1442. if (ch > 0 && ch < INT_MAX/16) {
  1443. /* calc from frame_bytes and channels */
  1444. switch (id) {
  1445. case AV_CODEC_ID_ADPCM_AFC:
  1446. return frame_bytes / (9 * ch) * 16;
  1447. case AV_CODEC_ID_ADPCM_PSX:
  1448. case AV_CODEC_ID_ADPCM_DTK:
  1449. return frame_bytes / (16 * ch) * 28;
  1450. case AV_CODEC_ID_ADPCM_4XM:
  1451. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  1452. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1453. return (frame_bytes - 4 * ch) * 2 / ch;
  1454. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1455. return (frame_bytes - 4) * 2 / ch;
  1456. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1457. return (frame_bytes - 8) * 2 / ch;
  1458. case AV_CODEC_ID_ADPCM_THP:
  1459. case AV_CODEC_ID_ADPCM_THP_LE:
  1460. if (extradata)
  1461. return frame_bytes * 14 / (8 * ch);
  1462. break;
  1463. case AV_CODEC_ID_ADPCM_XA:
  1464. return (frame_bytes / 128) * 224 / ch;
  1465. case AV_CODEC_ID_INTERPLAY_DPCM:
  1466. return (frame_bytes - 6 - ch) / ch;
  1467. case AV_CODEC_ID_ROQ_DPCM:
  1468. return (frame_bytes - 8) / ch;
  1469. case AV_CODEC_ID_XAN_DPCM:
  1470. return (frame_bytes - 2 * ch) / ch;
  1471. case AV_CODEC_ID_MACE3:
  1472. return 3 * frame_bytes / ch;
  1473. case AV_CODEC_ID_MACE6:
  1474. return 6 * frame_bytes / ch;
  1475. case AV_CODEC_ID_PCM_LXF:
  1476. return 2 * (frame_bytes / (5 * ch));
  1477. case AV_CODEC_ID_IAC:
  1478. case AV_CODEC_ID_IMC:
  1479. return 4 * frame_bytes / ch;
  1480. }
  1481. if (tag) {
  1482. /* calc from frame_bytes, channels, and codec_tag */
  1483. if (id == AV_CODEC_ID_SOL_DPCM) {
  1484. if (tag == 3)
  1485. return frame_bytes / ch;
  1486. else
  1487. return frame_bytes * 2 / ch;
  1488. }
  1489. }
  1490. if (ba > 0) {
  1491. /* calc from frame_bytes, channels, and block_align */
  1492. int blocks = frame_bytes / ba;
  1493. switch (id) {
  1494. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1495. if (bps < 2 || bps > 5)
  1496. return 0;
  1497. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  1498. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1499. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1500. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1501. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1502. case AV_CODEC_ID_ADPCM_IMA_RAD:
  1503. return blocks * ((ba - 4 * ch) * 2 / ch);
  1504. case AV_CODEC_ID_ADPCM_MS:
  1505. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1506. case AV_CODEC_ID_ADPCM_MTAF:
  1507. return blocks * (ba - 16) * 2 / ch;
  1508. }
  1509. }
  1510. if (bps > 0) {
  1511. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1512. switch (id) {
  1513. case AV_CODEC_ID_PCM_DVD:
  1514. if(bps<4 || frame_bytes<3)
  1515. return 0;
  1516. return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
  1517. case AV_CODEC_ID_PCM_BLURAY:
  1518. if(bps<4 || frame_bytes<4)
  1519. return 0;
  1520. return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
  1521. case AV_CODEC_ID_S302M:
  1522. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1523. }
  1524. }
  1525. }
  1526. }
  1527. /* Fall back on using frame_size */
  1528. if (frame_size > 1 && frame_bytes)
  1529. return frame_size;
  1530. //For WMA we currently have no other means to calculate duration thus we
  1531. //do it here by assuming CBR, which is true for all known cases.
  1532. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  1533. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  1534. return (frame_bytes * 8LL * sr) / bitrate;
  1535. }
  1536. return 0;
  1537. }
  1538. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1539. {
  1540. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  1541. avctx->channels, avctx->block_align,
  1542. avctx->codec_tag, avctx->bits_per_coded_sample,
  1543. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  1544. frame_bytes);
  1545. }
  1546. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  1547. {
  1548. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  1549. par->channels, par->block_align,
  1550. par->codec_tag, par->bits_per_coded_sample,
  1551. par->bit_rate, par->extradata, par->frame_size,
  1552. frame_bytes);
  1553. }
  1554. #if !HAVE_THREADS
  1555. int ff_thread_init(AVCodecContext *s)
  1556. {
  1557. return -1;
  1558. }
  1559. #endif
  1560. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1561. {
  1562. unsigned int n = 0;
  1563. while (v >= 0xff) {
  1564. *s++ = 0xff;
  1565. v -= 0xff;
  1566. n++;
  1567. }
  1568. *s = v;
  1569. n++;
  1570. return n;
  1571. }
  1572. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1573. {
  1574. int i;
  1575. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1576. return i;
  1577. }
  1578. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
  1579. {
  1580. int i;
  1581. if (!codec->hw_configs || index < 0)
  1582. return NULL;
  1583. for (i = 0; i <= index; i++)
  1584. if (!codec->hw_configs[i])
  1585. return NULL;
  1586. return &codec->hw_configs[index]->public;
  1587. }
  1588. #if FF_API_USER_VISIBLE_AVHWACCEL
  1589. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1590. {
  1591. return NULL;
  1592. }
  1593. void av_register_hwaccel(AVHWAccel *hwaccel)
  1594. {
  1595. }
  1596. #endif
  1597. #if FF_API_LOCKMGR
  1598. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1599. {
  1600. return 0;
  1601. }
  1602. #endif
  1603. unsigned int avpriv_toupper4(unsigned int x)
  1604. {
  1605. return av_toupper(x & 0xFF) +
  1606. (av_toupper((x >> 8) & 0xFF) << 8) +
  1607. (av_toupper((x >> 16) & 0xFF) << 16) +
  1608. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  1609. }
  1610. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1611. {
  1612. int ret;
  1613. dst->owner[0] = src->owner[0];
  1614. dst->owner[1] = src->owner[1];
  1615. ret = av_frame_ref(dst->f, src->f);
  1616. if (ret < 0)
  1617. return ret;
  1618. av_assert0(!dst->progress);
  1619. if (src->progress &&
  1620. !(dst->progress = av_buffer_ref(src->progress))) {
  1621. ff_thread_release_buffer(dst->owner[0], dst);
  1622. return AVERROR(ENOMEM);
  1623. }
  1624. return 0;
  1625. }
  1626. #if !HAVE_THREADS
  1627. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1628. {
  1629. return ff_get_format(avctx, fmt);
  1630. }
  1631. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1632. {
  1633. f->owner[0] = f->owner[1] = avctx;
  1634. return ff_get_buffer(avctx, f->f, flags);
  1635. }
  1636. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1637. {
  1638. if (f->f)
  1639. av_frame_unref(f->f);
  1640. }
  1641. void ff_thread_finish_setup(AVCodecContext *avctx)
  1642. {
  1643. }
  1644. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1645. {
  1646. }
  1647. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1648. {
  1649. }
  1650. int ff_thread_can_start_frame(AVCodecContext *avctx)
  1651. {
  1652. return 1;
  1653. }
  1654. int ff_alloc_entries(AVCodecContext *avctx, int count)
  1655. {
  1656. return 0;
  1657. }
  1658. void ff_reset_entries(AVCodecContext *avctx)
  1659. {
  1660. }
  1661. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  1662. {
  1663. }
  1664. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  1665. {
  1666. }
  1667. #endif
  1668. int avcodec_is_open(AVCodecContext *s)
  1669. {
  1670. return !!s->internal;
  1671. }
  1672. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  1673. {
  1674. int ret;
  1675. char *str;
  1676. ret = av_bprint_finalize(buf, &str);
  1677. if (ret < 0)
  1678. return ret;
  1679. if (!av_bprint_is_complete(buf)) {
  1680. av_free(str);
  1681. return AVERROR(ENOMEM);
  1682. }
  1683. avctx->extradata = str;
  1684. /* Note: the string is NUL terminated (so extradata can be read as a
  1685. * string), but the ending character is not accounted in the size (in
  1686. * binary formats you are likely not supposed to mux that character). When
  1687. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  1688. * zeros. */
  1689. avctx->extradata_size = buf->len;
  1690. return 0;
  1691. }
  1692. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  1693. const uint8_t *end,
  1694. uint32_t *av_restrict state)
  1695. {
  1696. int i;
  1697. av_assert0(p <= end);
  1698. if (p >= end)
  1699. return end;
  1700. for (i = 0; i < 3; i++) {
  1701. uint32_t tmp = *state << 8;
  1702. *state = tmp + *(p++);
  1703. if (tmp == 0x100 || p == end)
  1704. return p;
  1705. }
  1706. while (p < end) {
  1707. if (p[-1] > 1 ) p += 3;
  1708. else if (p[-2] ) p += 2;
  1709. else if (p[-3]|(p[-1]-1)) p++;
  1710. else {
  1711. p++;
  1712. break;
  1713. }
  1714. }
  1715. p = FFMIN(p, end) - 4;
  1716. *state = AV_RB32(p);
  1717. return p + 4;
  1718. }
  1719. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  1720. {
  1721. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  1722. if (!props)
  1723. return NULL;
  1724. if (size)
  1725. *size = sizeof(*props);
  1726. props->vbv_delay = UINT64_MAX;
  1727. return props;
  1728. }
  1729. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  1730. {
  1731. AVPacketSideData *tmp;
  1732. AVCPBProperties *props;
  1733. size_t size;
  1734. props = av_cpb_properties_alloc(&size);
  1735. if (!props)
  1736. return NULL;
  1737. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  1738. if (!tmp) {
  1739. av_freep(&props);
  1740. return NULL;
  1741. }
  1742. avctx->coded_side_data = tmp;
  1743. avctx->nb_coded_side_data++;
  1744. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  1745. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  1746. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  1747. return props;
  1748. }
  1749. static void codec_parameters_reset(AVCodecParameters *par)
  1750. {
  1751. av_freep(&par->extradata);
  1752. memset(par, 0, sizeof(*par));
  1753. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  1754. par->codec_id = AV_CODEC_ID_NONE;
  1755. par->format = -1;
  1756. par->field_order = AV_FIELD_UNKNOWN;
  1757. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  1758. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  1759. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  1760. par->color_space = AVCOL_SPC_UNSPECIFIED;
  1761. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  1762. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  1763. par->profile = FF_PROFILE_UNKNOWN;
  1764. par->level = FF_LEVEL_UNKNOWN;
  1765. }
  1766. AVCodecParameters *avcodec_parameters_alloc(void)
  1767. {
  1768. AVCodecParameters *par = av_mallocz(sizeof(*par));
  1769. if (!par)
  1770. return NULL;
  1771. codec_parameters_reset(par);
  1772. return par;
  1773. }
  1774. void avcodec_parameters_free(AVCodecParameters **ppar)
  1775. {
  1776. AVCodecParameters *par = *ppar;
  1777. if (!par)
  1778. return;
  1779. codec_parameters_reset(par);
  1780. av_freep(ppar);
  1781. }
  1782. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  1783. {
  1784. codec_parameters_reset(dst);
  1785. memcpy(dst, src, sizeof(*dst));
  1786. dst->extradata = NULL;
  1787. dst->extradata_size = 0;
  1788. if (src->extradata) {
  1789. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1790. if (!dst->extradata)
  1791. return AVERROR(ENOMEM);
  1792. memcpy(dst->extradata, src->extradata, src->extradata_size);
  1793. dst->extradata_size = src->extradata_size;
  1794. }
  1795. return 0;
  1796. }
  1797. int avcodec_parameters_from_context(AVCodecParameters *par,
  1798. const AVCodecContext *codec)
  1799. {
  1800. codec_parameters_reset(par);
  1801. par->codec_type = codec->codec_type;
  1802. par->codec_id = codec->codec_id;
  1803. par->codec_tag = codec->codec_tag;
  1804. par->bit_rate = codec->bit_rate;
  1805. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  1806. par->bits_per_raw_sample = codec->bits_per_raw_sample;
  1807. par->profile = codec->profile;
  1808. par->level = codec->level;
  1809. switch (par->codec_type) {
  1810. case AVMEDIA_TYPE_VIDEO:
  1811. par->format = codec->pix_fmt;
  1812. par->width = codec->width;
  1813. par->height = codec->height;
  1814. par->field_order = codec->field_order;
  1815. par->color_range = codec->color_range;
  1816. par->color_primaries = codec->color_primaries;
  1817. par->color_trc = codec->color_trc;
  1818. par->color_space = codec->colorspace;
  1819. par->chroma_location = codec->chroma_sample_location;
  1820. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  1821. par->video_delay = codec->has_b_frames;
  1822. break;
  1823. case AVMEDIA_TYPE_AUDIO:
  1824. par->format = codec->sample_fmt;
  1825. par->channel_layout = codec->channel_layout;
  1826. par->channels = codec->channels;
  1827. par->sample_rate = codec->sample_rate;
  1828. par->block_align = codec->block_align;
  1829. par->frame_size = codec->frame_size;
  1830. par->initial_padding = codec->initial_padding;
  1831. par->trailing_padding = codec->trailing_padding;
  1832. par->seek_preroll = codec->seek_preroll;
  1833. break;
  1834. case AVMEDIA_TYPE_SUBTITLE:
  1835. par->width = codec->width;
  1836. par->height = codec->height;
  1837. break;
  1838. }
  1839. if (codec->extradata) {
  1840. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1841. if (!par->extradata)
  1842. return AVERROR(ENOMEM);
  1843. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  1844. par->extradata_size = codec->extradata_size;
  1845. }
  1846. return 0;
  1847. }
  1848. int avcodec_parameters_to_context(AVCodecContext *codec,
  1849. const AVCodecParameters *par)
  1850. {
  1851. codec->codec_type = par->codec_type;
  1852. codec->codec_id = par->codec_id;
  1853. codec->codec_tag = par->codec_tag;
  1854. codec->bit_rate = par->bit_rate;
  1855. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  1856. codec->bits_per_raw_sample = par->bits_per_raw_sample;
  1857. codec->profile = par->profile;
  1858. codec->level = par->level;
  1859. switch (par->codec_type) {
  1860. case AVMEDIA_TYPE_VIDEO:
  1861. codec->pix_fmt = par->format;
  1862. codec->width = par->width;
  1863. codec->height = par->height;
  1864. codec->field_order = par->field_order;
  1865. codec->color_range = par->color_range;
  1866. codec->color_primaries = par->color_primaries;
  1867. codec->color_trc = par->color_trc;
  1868. codec->colorspace = par->color_space;
  1869. codec->chroma_sample_location = par->chroma_location;
  1870. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  1871. codec->has_b_frames = par->video_delay;
  1872. break;
  1873. case AVMEDIA_TYPE_AUDIO:
  1874. codec->sample_fmt = par->format;
  1875. codec->channel_layout = par->channel_layout;
  1876. codec->channels = par->channels;
  1877. codec->sample_rate = par->sample_rate;
  1878. codec->block_align = par->block_align;
  1879. codec->frame_size = par->frame_size;
  1880. codec->delay =
  1881. codec->initial_padding = par->initial_padding;
  1882. codec->trailing_padding = par->trailing_padding;
  1883. codec->seek_preroll = par->seek_preroll;
  1884. break;
  1885. case AVMEDIA_TYPE_SUBTITLE:
  1886. codec->width = par->width;
  1887. codec->height = par->height;
  1888. break;
  1889. }
  1890. if (par->extradata) {
  1891. av_freep(&codec->extradata);
  1892. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1893. if (!codec->extradata)
  1894. return AVERROR(ENOMEM);
  1895. memcpy(codec->extradata, par->extradata, par->extradata_size);
  1896. codec->extradata_size = par->extradata_size;
  1897. }
  1898. return 0;
  1899. }
  1900. int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
  1901. void **data, size_t *sei_size)
  1902. {
  1903. AVFrameSideData *side_data = NULL;
  1904. uint8_t *sei_data;
  1905. if (frame)
  1906. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
  1907. if (!side_data) {
  1908. *data = NULL;
  1909. return 0;
  1910. }
  1911. *sei_size = side_data->size + 11;
  1912. *data = av_mallocz(*sei_size + prefix_len);
  1913. if (!*data)
  1914. return AVERROR(ENOMEM);
  1915. sei_data = (uint8_t*)*data + prefix_len;
  1916. // country code
  1917. sei_data[0] = 181;
  1918. sei_data[1] = 0;
  1919. sei_data[2] = 49;
  1920. /**
  1921. * 'GA94' is standard in North America for ATSC, but hard coding
  1922. * this style may not be the right thing to do -- other formats
  1923. * do exist. This information is not available in the side_data
  1924. * so we are going with this right now.
  1925. */
  1926. AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
  1927. sei_data[7] = 3;
  1928. sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
  1929. sei_data[9] = 0;
  1930. memcpy(sei_data + 10, side_data->data, side_data->size);
  1931. sei_data[side_data->size+10] = 255;
  1932. return 0;
  1933. }
  1934. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
  1935. {
  1936. AVRational framerate = avctx->framerate;
  1937. int bits_per_coded_sample = avctx->bits_per_coded_sample;
  1938. int64_t bitrate;
  1939. if (!(framerate.num && framerate.den))
  1940. framerate = av_inv_q(avctx->time_base);
  1941. if (!(framerate.num && framerate.den))
  1942. return 0;
  1943. if (!bits_per_coded_sample) {
  1944. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1945. bits_per_coded_sample = av_get_bits_per_pixel(desc);
  1946. }
  1947. bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
  1948. framerate.num / framerate.den;
  1949. return bitrate;
  1950. }
  1951. int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
  1952. const int * array_valid_values, int default_value)
  1953. {
  1954. int i = 0, ref_val;
  1955. while (1) {
  1956. ref_val = array_valid_values[i];
  1957. if (ref_val == INT_MAX)
  1958. break;
  1959. if (val == ref_val)
  1960. return val;
  1961. i++;
  1962. }
  1963. /* val is not a valid value */
  1964. av_log(ctx, AV_LOG_DEBUG,
  1965. "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
  1966. return default_value;
  1967. }