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.

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