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.

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