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.

2265 lines
76KB

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