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.

2205 lines
74KB

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