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.

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