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.

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