/libavcodec/utils.c

http://github.com/FFmpeg/FFmpeg · C · 2232 lines · 1905 code · 263 blank · 64 comment · 541 complexity · 893341df1c0bea69e93f31cb293fae31 MD5 · raw file

Large files are truncated click here to view the full file

  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 "hwconfig.h"
  46. #include "libavutil/opt.h"
  47. #include "mpegvideo.h"
  48. #include "thread.h"
  49. #include "frame_thread_encoder.h"
  50. #include "internal.h"
  51. #include "raw.h"
  52. #include "bytestream.h"
  53. #include "version.h"
  54. #include <stdlib.h>
  55. #include <stdarg.h>
  56. #include <stdatomic.h>
  57. #include <limits.h>
  58. #include <float.h>
  59. #if CONFIG_ICONV
  60. # include <iconv.h>
  61. #endif
  62. #include "libavutil/ffversion.h"
  63. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  64. static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
  65. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  66. {
  67. uint8_t **p = ptr;
  68. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  69. av_freep(p);
  70. *size = 0;
  71. return;
  72. }
  73. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  74. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  75. }
  76. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. uint8_t **p = ptr;
  79. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  85. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  86. }
  87. int av_codec_is_encoder(const AVCodec *codec)
  88. {
  89. return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
  90. }
  91. int av_codec_is_decoder(const AVCodec *codec)
  92. {
  93. return codec && (codec->decode || codec->receive_frame);
  94. }
  95. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  96. {
  97. int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
  98. if (ret < 0)
  99. width = height = 0;
  100. s->coded_width = width;
  101. s->coded_height = height;
  102. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  103. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  104. return ret;
  105. }
  106. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  107. {
  108. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  109. if (ret < 0) {
  110. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  111. sar.num, sar.den);
  112. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  113. return ret;
  114. } else {
  115. avctx->sample_aspect_ratio = sar;
  116. }
  117. return 0;
  118. }
  119. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  120. enum AVMatrixEncoding matrix_encoding)
  121. {
  122. AVFrameSideData *side_data;
  123. enum AVMatrixEncoding *data;
  124. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  125. if (!side_data)
  126. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  127. sizeof(enum AVMatrixEncoding));
  128. if (!side_data)
  129. return AVERROR(ENOMEM);
  130. data = (enum AVMatrixEncoding*)side_data->data;
  131. *data = matrix_encoding;
  132. return 0;
  133. }
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align = 1;
  139. int h_align = 1;
  140. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  141. if (desc) {
  142. w_align = 1 << desc->log2_chroma_w;
  143. h_align = 1 << desc->log2_chroma_h;
  144. }
  145. switch (s->pix_fmt) {
  146. case AV_PIX_FMT_YUV420P:
  147. case AV_PIX_FMT_YUYV422:
  148. case AV_PIX_FMT_YVYU422:
  149. case AV_PIX_FMT_UYVY422:
  150. case AV_PIX_FMT_YUV422P:
  151. case AV_PIX_FMT_YUV440P:
  152. case AV_PIX_FMT_YUV444P:
  153. case AV_PIX_FMT_GBRP:
  154. case AV_PIX_FMT_GBRAP:
  155. case AV_PIX_FMT_GRAY8:
  156. case AV_PIX_FMT_GRAY16BE:
  157. case AV_PIX_FMT_GRAY16LE:
  158. case AV_PIX_FMT_YUVJ420P:
  159. case AV_PIX_FMT_YUVJ422P:
  160. case AV_PIX_FMT_YUVJ440P:
  161. case AV_PIX_FMT_YUVJ444P:
  162. case AV_PIX_FMT_YUVA420P:
  163. case AV_PIX_FMT_YUVA422P:
  164. case AV_PIX_FMT_YUVA444P:
  165. case AV_PIX_FMT_YUV420P9LE:
  166. case AV_PIX_FMT_YUV420P9BE:
  167. case AV_PIX_FMT_YUV420P10LE:
  168. case AV_PIX_FMT_YUV420P10BE:
  169. case AV_PIX_FMT_YUV420P12LE:
  170. case AV_PIX_FMT_YUV420P12BE:
  171. case AV_PIX_FMT_YUV420P14LE:
  172. case AV_PIX_FMT_YUV420P14BE:
  173. case AV_PIX_FMT_YUV420P16LE:
  174. case AV_PIX_FMT_YUV420P16BE:
  175. case AV_PIX_FMT_YUVA420P9LE:
  176. case AV_PIX_FMT_YUVA420P9BE:
  177. case AV_PIX_FMT_YUVA420P10LE:
  178. case AV_PIX_FMT_YUVA420P10BE:
  179. case AV_PIX_FMT_YUVA420P16LE:
  180. case AV_PIX_FMT_YUVA420P16BE:
  181. case AV_PIX_FMT_YUV422P9LE:
  182. case AV_PIX_FMT_YUV422P9BE:
  183. case AV_PIX_FMT_YUV422P10LE:
  184. case AV_PIX_FMT_YUV422P10BE:
  185. case AV_PIX_FMT_YUV422P12LE:
  186. case AV_PIX_FMT_YUV422P12BE:
  187. case AV_PIX_FMT_YUV422P14LE:
  188. case AV_PIX_FMT_YUV422P14BE:
  189. case AV_PIX_FMT_YUV422P16LE:
  190. case AV_PIX_FMT_YUV422P16BE:
  191. case AV_PIX_FMT_YUVA422P9LE:
  192. case AV_PIX_FMT_YUVA422P9BE:
  193. case AV_PIX_FMT_YUVA422P10LE:
  194. case AV_PIX_FMT_YUVA422P10BE:
  195. case AV_PIX_FMT_YUVA422P12LE:
  196. case AV_PIX_FMT_YUVA422P12BE:
  197. case AV_PIX_FMT_YUVA422P16LE:
  198. case AV_PIX_FMT_YUVA422P16BE:
  199. case AV_PIX_FMT_YUV440P10LE:
  200. case AV_PIX_FMT_YUV440P10BE:
  201. case AV_PIX_FMT_YUV440P12LE:
  202. case AV_PIX_FMT_YUV440P12BE:
  203. case AV_PIX_FMT_YUV444P9LE:
  204. case AV_PIX_FMT_YUV444P9BE:
  205. case AV_PIX_FMT_YUV444P10LE:
  206. case AV_PIX_FMT_YUV444P10BE:
  207. case AV_PIX_FMT_YUV444P12LE:
  208. case AV_PIX_FMT_YUV444P12BE:
  209. case AV_PIX_FMT_YUV444P14LE:
  210. case AV_PIX_FMT_YUV444P14BE:
  211. case AV_PIX_FMT_YUV444P16LE:
  212. case AV_PIX_FMT_YUV444P16BE:
  213. case AV_PIX_FMT_YUVA444P9LE:
  214. case AV_PIX_FMT_YUVA444P9BE:
  215. case AV_PIX_FMT_YUVA444P10LE:
  216. case AV_PIX_FMT_YUVA444P10BE:
  217. case AV_PIX_FMT_YUVA444P12LE:
  218. case AV_PIX_FMT_YUVA444P12BE:
  219. case AV_PIX_FMT_YUVA444P16LE:
  220. case AV_PIX_FMT_YUVA444P16BE:
  221. case AV_PIX_FMT_GBRP9LE:
  222. case AV_PIX_FMT_GBRP9BE:
  223. case AV_PIX_FMT_GBRP10LE:
  224. case AV_PIX_FMT_GBRP10BE:
  225. case AV_PIX_FMT_GBRP12LE:
  226. case AV_PIX_FMT_GBRP12BE:
  227. case AV_PIX_FMT_GBRP14LE:
  228. case AV_PIX_FMT_GBRP14BE:
  229. case AV_PIX_FMT_GBRP16LE:
  230. case AV_PIX_FMT_GBRP16BE:
  231. case AV_PIX_FMT_GBRAP12LE:
  232. case AV_PIX_FMT_GBRAP12BE:
  233. case AV_PIX_FMT_GBRAP16LE:
  234. case AV_PIX_FMT_GBRAP16BE:
  235. w_align = 16; //FIXME assume 16 pixel per macroblock
  236. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  237. break;
  238. case AV_PIX_FMT_YUV411P:
  239. case AV_PIX_FMT_YUVJ411P:
  240. case AV_PIX_FMT_UYYVYY411:
  241. w_align = 32;
  242. h_align = 16 * 2;
  243. break;
  244. case AV_PIX_FMT_YUV410P:
  245. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  246. w_align = 64;
  247. h_align = 64;
  248. }
  249. break;
  250. case AV_PIX_FMT_RGB555:
  251. if (s->codec_id == AV_CODEC_ID_RPZA) {
  252. w_align = 4;
  253. h_align = 4;
  254. }
  255. if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  256. w_align = 8;
  257. h_align = 8;
  258. }
  259. break;
  260. case AV_PIX_FMT_PAL8:
  261. case AV_PIX_FMT_BGR8:
  262. case AV_PIX_FMT_RGB8:
  263. if (s->codec_id == AV_CODEC_ID_SMC ||
  264. s->codec_id == AV_CODEC_ID_CINEPAK) {
  265. w_align = 4;
  266. h_align = 4;
  267. }
  268. if (s->codec_id == AV_CODEC_ID_JV ||
  269. s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  270. w_align = 8;
  271. h_align = 8;
  272. }
  273. break;
  274. case AV_PIX_FMT_BGR24:
  275. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  276. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  277. w_align = 4;
  278. h_align = 4;
  279. }
  280. break;
  281. case AV_PIX_FMT_RGB24:
  282. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  283. w_align = 4;
  284. h_align = 4;
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  291. w_align = FFMAX(w_align, 8);
  292. }
  293. *width = FFALIGN(*width, w_align);
  294. *height = FFALIGN(*height, h_align);
  295. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
  296. s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
  297. s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
  298. ) {
  299. // some of the optimized chroma MC reads one line too much
  300. // which is also done in mpeg decoders with lowres > 0
  301. *height += 2;
  302. // H.264 uses edge emulation for out of frame motion vectors, for this
  303. // it requires a temporary area large enough to hold a 21x21 block,
  304. // increasing witdth ensure that the temporary area is large enough,
  305. // the next rounded up width is 32
  306. *width = FFMAX(*width, 32);
  307. }
  308. for (i = 0; i < 4; i++)
  309. linesize_align[i] = STRIDE_ALIGN;
  310. }
  311. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  312. {
  313. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  314. int chroma_shift = desc->log2_chroma_w;
  315. int linesize_align[AV_NUM_DATA_POINTERS];
  316. int align;
  317. avcodec_align_dimensions2(s, width, height, linesize_align);
  318. align = FFMAX(linesize_align[0], linesize_align[3]);
  319. linesize_align[1] <<= chroma_shift;
  320. linesize_align[2] <<= chroma_shift;
  321. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  322. *width = FFALIGN(*width, align);
  323. }
  324. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  325. {
  326. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  327. return AVERROR(EINVAL);
  328. pos--;
  329. *xpos = (pos&1) * 128;
  330. *ypos = ((pos>>1)^(pos<4)) * 128;
  331. return 0;
  332. }
  333. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  334. {
  335. int pos, xout, yout;
  336. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  337. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  338. return pos;
  339. }
  340. return AVCHROMA_LOC_UNSPECIFIED;
  341. }
  342. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  343. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  344. int buf_size, int align)
  345. {
  346. int ch, planar, needed_size, ret = 0;
  347. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  348. frame->nb_samples, sample_fmt,
  349. align);
  350. if (buf_size < needed_size)
  351. return AVERROR(EINVAL);
  352. planar = av_sample_fmt_is_planar(sample_fmt);
  353. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  354. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  355. sizeof(*frame->extended_data))))
  356. return AVERROR(ENOMEM);
  357. } else {
  358. frame->extended_data = frame->data;
  359. }
  360. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  361. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  362. sample_fmt, align)) < 0) {
  363. if (frame->extended_data != frame->data)
  364. av_freep(&frame->extended_data);
  365. return ret;
  366. }
  367. if (frame->extended_data != frame->data) {
  368. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  369. frame->data[ch] = frame->extended_data[ch];
  370. }
  371. return ret;
  372. }
  373. void ff_color_frame(AVFrame *frame, const int c[4])
  374. {
  375. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  376. int p, y;
  377. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  378. for (p = 0; p<desc->nb_components; p++) {
  379. uint8_t *dst = frame->data[p];
  380. int is_chroma = p == 1 || p == 2;
  381. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  382. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  383. if (desc->comp[0].depth >= 9) {
  384. ((uint16_t*)dst)[0] = c[p];
  385. av_memcpy_backptr(dst + 2, 2, bytes - 2);
  386. dst += frame->linesize[p];
  387. for (y = 1; y < height; y++) {
  388. memcpy(dst, frame->data[p], 2*bytes);
  389. dst += frame->linesize[p];
  390. }
  391. } else {
  392. for (y = 0; y < height; y++) {
  393. memset(dst, c[p], bytes);
  394. dst += frame->linesize[p];
  395. }
  396. }
  397. }
  398. }
  399. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  400. {
  401. int i;
  402. for (i = 0; i < count; i++) {
  403. int r = func(c, (char *)arg + i * size);
  404. if (ret)
  405. ret[i] = r;
  406. }
  407. emms_c();
  408. return 0;
  409. }
  410. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  411. {
  412. int i;
  413. for (i = 0; i < count; i++) {
  414. int r = func(c, arg, i, 0);
  415. if (ret)
  416. ret[i] = r;
  417. }
  418. emms_c();
  419. return 0;
  420. }
  421. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  422. unsigned int fourcc)
  423. {
  424. while (tags->pix_fmt >= 0) {
  425. if (tags->fourcc == fourcc)
  426. return tags->pix_fmt;
  427. tags++;
  428. }
  429. return AV_PIX_FMT_NONE;
  430. }
  431. #if FF_API_CODEC_GET_SET
  432. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  433. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  434. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  435. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  436. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  437. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  438. {
  439. return codec->properties;
  440. }
  441. int av_codec_get_max_lowres(const AVCodec *codec)
  442. {
  443. return codec->max_lowres;
  444. }
  445. #endif
  446. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  447. return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  448. }
  449. static int64_t get_bit_rate(AVCodecContext *ctx)
  450. {
  451. int64_t bit_rate;
  452. int bits_per_sample;
  453. switch (ctx->codec_type) {
  454. case AVMEDIA_TYPE_VIDEO:
  455. case AVMEDIA_TYPE_DATA:
  456. case AVMEDIA_TYPE_SUBTITLE:
  457. case AVMEDIA_TYPE_ATTACHMENT:
  458. bit_rate = ctx->bit_rate;
  459. break;
  460. case AVMEDIA_TYPE_AUDIO:
  461. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  462. bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
  463. break;
  464. default:
  465. bit_rate = 0;
  466. break;
  467. }
  468. return bit_rate;
  469. }
  470. static void ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  471. {
  472. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  473. ff_mutex_lock(&codec_mutex);
  474. }
  475. static void ff_unlock_avcodec(const AVCodec *codec)
  476. {
  477. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  478. ff_mutex_unlock(&codec_mutex);
  479. }
  480. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  481. {
  482. int ret = 0;
  483. ff_unlock_avcodec(codec);
  484. ret = avcodec_open2(avctx, codec, options);
  485. ff_lock_avcodec(avctx, codec);
  486. return ret;
  487. }
  488. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  489. {
  490. int ret = 0;
  491. int codec_init_ok = 0;
  492. AVDictionary *tmp = NULL;
  493. const AVPixFmtDescriptor *pixdesc;
  494. AVCodecInternal *avci;
  495. if (avcodec_is_open(avctx))
  496. return 0;
  497. if (!codec && !avctx->codec) {
  498. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  499. return AVERROR(EINVAL);
  500. }
  501. if (codec && avctx->codec && codec != avctx->codec) {
  502. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  503. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  504. return AVERROR(EINVAL);
  505. }
  506. if (!codec)
  507. codec = avctx->codec;
  508. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  509. return AVERROR(EINVAL);
  510. if (options)
  511. av_dict_copy(&tmp, *options, 0);
  512. ff_lock_avcodec(avctx, codec);
  513. avci = av_mallocz(sizeof(*avci));
  514. if (!avci) {
  515. ret = AVERROR(ENOMEM);
  516. goto end;
  517. }
  518. avctx->internal = avci;
  519. avci->to_free = av_frame_alloc();
  520. avci->compat_decode_frame = av_frame_alloc();
  521. avci->buffer_frame = av_frame_alloc();
  522. avci->buffer_pkt = av_packet_alloc();
  523. avci->ds.in_pkt = av_packet_alloc();
  524. avci->last_pkt_props = av_packet_alloc();
  525. if (!avci->to_free || !avci->compat_decode_frame ||
  526. !avci->buffer_frame || !avci->buffer_pkt ||
  527. !avci->ds.in_pkt || !avci->last_pkt_props) {
  528. ret = AVERROR(ENOMEM);
  529. goto free_and_end;
  530. }
  531. avci->skip_samples_multiplier = 1;
  532. if (codec->priv_data_size > 0) {
  533. if (!avctx->priv_data) {
  534. avctx->priv_data = av_mallocz(codec->priv_data_size);
  535. if (!avctx->priv_data) {
  536. ret = AVERROR(ENOMEM);
  537. goto end;
  538. }
  539. if (codec->priv_class) {
  540. *(const AVClass **)avctx->priv_data = codec->priv_class;
  541. av_opt_set_defaults(avctx->priv_data);
  542. }
  543. }
  544. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  545. goto free_and_end;
  546. } else {
  547. avctx->priv_data = NULL;
  548. }
  549. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  550. goto free_and_end;
  551. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  552. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  553. ret = AVERROR(EINVAL);
  554. goto free_and_end;
  555. }
  556. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  557. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  558. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  559. if (avctx->coded_width && avctx->coded_height)
  560. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  561. else if (avctx->width && avctx->height)
  562. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  563. if (ret < 0)
  564. goto free_and_end;
  565. }
  566. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  567. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  568. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  569. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  570. ff_set_dimensions(avctx, 0, 0);
  571. }
  572. if (avctx->width > 0 && avctx->height > 0) {
  573. if (av_image_check_sar(avctx->width, avctx->height,
  574. avctx->sample_aspect_ratio) < 0) {
  575. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  576. avctx->sample_aspect_ratio.num,
  577. avctx->sample_aspect_ratio.den);
  578. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  579. }
  580. }
  581. /* if the decoder init function was already called previously,
  582. * free the already allocated subtitle_header before overwriting it */
  583. if (av_codec_is_decoder(codec))
  584. av_freep(&avctx->subtitle_header);
  585. if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
  586. av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
  587. ret = AVERROR(EINVAL);
  588. goto free_and_end;
  589. }
  590. if (avctx->sample_rate < 0) {
  591. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
  592. ret = AVERROR(EINVAL);
  593. goto free_and_end;
  594. }
  595. if (avctx->block_align < 0) {
  596. av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
  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 (av_codec_is_decoder(avctx->codec)) {
  644. ret = ff_decode_bsfs_init(avctx);
  645. if (ret < 0)
  646. goto free_and_end;
  647. }
  648. if (HAVE_THREADS
  649. && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  650. ret = ff_thread_init(avctx);
  651. if (ret < 0) {
  652. goto free_and_end;
  653. }
  654. }
  655. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  656. avctx->thread_count = 1;
  657. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  658. av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
  659. avctx->codec->max_lowres);
  660. avctx->lowres = avctx->codec->max_lowres;
  661. }
  662. if (av_codec_is_encoder(avctx->codec)) {
  663. int i;
  664. #if FF_API_CODED_FRAME
  665. FF_DISABLE_DEPRECATION_WARNINGS
  666. avctx->coded_frame = av_frame_alloc();
  667. if (!avctx->coded_frame) {
  668. ret = AVERROR(ENOMEM);
  669. goto free_and_end;
  670. }
  671. FF_ENABLE_DEPRECATION_WARNINGS
  672. #endif
  673. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  674. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  675. ret = AVERROR(EINVAL);
  676. goto free_and_end;
  677. }
  678. if (avctx->codec->sample_fmts) {
  679. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  680. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  681. break;
  682. if (avctx->channels == 1 &&
  683. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  684. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  685. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  686. break;
  687. }
  688. }
  689. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  690. char buf[128];
  691. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  692. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  693. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  694. ret = AVERROR(EINVAL);
  695. goto free_and_end;
  696. }
  697. }
  698. if (avctx->codec->pix_fmts) {
  699. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  700. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  701. break;
  702. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  703. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  704. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  705. char buf[128];
  706. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  707. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  708. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  709. ret = AVERROR(EINVAL);
  710. goto free_and_end;
  711. }
  712. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  713. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  714. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  715. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  716. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  717. avctx->color_range = AVCOL_RANGE_JPEG;
  718. }
  719. if (avctx->codec->supported_samplerates) {
  720. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  721. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  722. break;
  723. if (avctx->codec->supported_samplerates[i] == 0) {
  724. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  725. avctx->sample_rate);
  726. ret = AVERROR(EINVAL);
  727. goto free_and_end;
  728. }
  729. }
  730. if (avctx->sample_rate < 0) {
  731. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  732. avctx->sample_rate);
  733. ret = AVERROR(EINVAL);
  734. goto free_and_end;
  735. }
  736. if (avctx->codec->channel_layouts) {
  737. if (!avctx->channel_layout) {
  738. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  739. } else {
  740. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  741. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  742. break;
  743. if (avctx->codec->channel_layouts[i] == 0) {
  744. char buf[512];
  745. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  746. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  747. ret = AVERROR(EINVAL);
  748. goto free_and_end;
  749. }
  750. }
  751. }
  752. if (avctx->channel_layout && avctx->channels) {
  753. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  754. if (channels != avctx->channels) {
  755. char buf[512];
  756. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  757. av_log(avctx, AV_LOG_ERROR,
  758. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  759. buf, channels, avctx->channels);
  760. ret = AVERROR(EINVAL);
  761. goto free_and_end;
  762. }
  763. } else if (avctx->channel_layout) {
  764. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  765. }
  766. if (avctx->channels < 0) {
  767. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  768. avctx->channels);
  769. ret = AVERROR(EINVAL);
  770. goto free_and_end;
  771. }
  772. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  773. pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
  774. if ( avctx->bits_per_raw_sample < 0
  775. || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
  776. av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
  777. avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
  778. avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
  779. }
  780. if (avctx->width <= 0 || avctx->height <= 0) {
  781. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  782. ret = AVERROR(EINVAL);
  783. goto free_and_end;
  784. }
  785. }
  786. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  787. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  788. av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
  789. }
  790. if (!avctx->rc_initial_buffer_occupancy)
  791. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
  792. if (avctx->ticks_per_frame && avctx->time_base.num &&
  793. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  794. av_log(avctx, AV_LOG_ERROR,
  795. "ticks_per_frame %d too large for the timebase %d/%d.",
  796. avctx->ticks_per_frame,
  797. avctx->time_base.num,
  798. avctx->time_base.den);
  799. goto free_and_end;
  800. }
  801. if (avctx->hw_frames_ctx) {
  802. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  803. if (frames_ctx->format != avctx->pix_fmt) {
  804. av_log(avctx, AV_LOG_ERROR,
  805. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  806. ret = AVERROR(EINVAL);
  807. goto free_and_end;
  808. }
  809. if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
  810. avctx->sw_pix_fmt != frames_ctx->sw_format) {
  811. av_log(avctx, AV_LOG_ERROR,
  812. "Mismatching AVCodecContext.sw_pix_fmt (%s) "
  813. "and AVHWFramesContext.sw_format (%s)\n",
  814. av_get_pix_fmt_name(avctx->sw_pix_fmt),
  815. av_get_pix_fmt_name(frames_ctx->sw_format));
  816. ret = AVERROR(EINVAL);
  817. goto free_and_end;
  818. }
  819. avctx->sw_pix_fmt = frames_ctx->sw_format;
  820. }
  821. }
  822. avctx->pts_correction_num_faulty_pts =
  823. avctx->pts_correction_num_faulty_dts = 0;
  824. avctx->pts_correction_last_pts =
  825. avctx->pts_correction_last_dts = INT64_MIN;
  826. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  827. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  828. av_log(avctx, AV_LOG_WARNING,
  829. "gray decoding requested but not enabled at configuration time\n");
  830. if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
  831. avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
  832. }
  833. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  834. || avci->frame_thread_encoder)) {
  835. ret = avctx->codec->init(avctx);
  836. if (ret < 0) {
  837. goto free_and_end;
  838. }
  839. codec_init_ok = 1;
  840. }
  841. ret=0;
  842. if (av_codec_is_decoder(avctx->codec)) {
  843. if (!avctx->bit_rate)
  844. avctx->bit_rate = get_bit_rate(avctx);
  845. /* validate channel layout from the decoder */
  846. if (avctx->channel_layout) {
  847. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  848. if (!avctx->channels)
  849. avctx->channels = channels;
  850. else if (channels != avctx->channels) {
  851. char buf[512];
  852. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  853. av_log(avctx, AV_LOG_WARNING,
  854. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  855. "ignoring specified channel layout\n",
  856. buf, channels, avctx->channels);
  857. avctx->channel_layout = 0;
  858. }
  859. }
  860. if (avctx->channels && avctx->channels < 0 ||
  861. avctx->channels > FF_SANE_NB_CHANNELS) {
  862. ret = AVERROR(EINVAL);
  863. goto free_and_end;
  864. }
  865. if (avctx->bits_per_coded_sample < 0) {
  866. ret = AVERROR(EINVAL);
  867. goto free_and_end;
  868. }
  869. if (avctx->sub_charenc) {
  870. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  871. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  872. "supported with subtitles codecs\n");
  873. ret = AVERROR(EINVAL);
  874. goto free_and_end;
  875. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  876. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  877. "subtitles character encoding will be ignored\n",
  878. avctx->codec_descriptor->name);
  879. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  880. } else {
  881. /* input character encoding is set for a text based subtitle
  882. * codec at this point */
  883. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  884. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  885. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  886. #if CONFIG_ICONV
  887. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  888. if (cd == (iconv_t)-1) {
  889. ret = AVERROR(errno);
  890. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  891. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  892. goto free_and_end;
  893. }
  894. iconv_close(cd);
  895. #else
  896. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  897. "conversion needs a libavcodec built with iconv support "
  898. "for this codec\n");
  899. ret = AVERROR(ENOSYS);
  900. goto free_and_end;
  901. #endif
  902. }
  903. }
  904. }
  905. #if FF_API_AVCTX_TIMEBASE
  906. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  907. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  908. #endif
  909. }
  910. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  911. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  912. }
  913. end:
  914. ff_unlock_avcodec(codec);
  915. if (options) {
  916. av_dict_free(options);
  917. *options = tmp;
  918. }
  919. return ret;
  920. free_and_end:
  921. if (avctx->codec && avctx->codec->close &&
  922. (codec_init_ok ||
  923. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  924. avctx->codec->close(avctx);
  925. if (HAVE_THREADS && avci->thread_ctx)
  926. ff_thread_free(avctx);
  927. if (codec->priv_class && codec->priv_data_size)
  928. av_opt_free(avctx->priv_data);
  929. av_opt_free(avctx);
  930. #if FF_API_CODED_FRAME
  931. FF_DISABLE_DEPRECATION_WARNINGS
  932. av_frame_free(&avctx->coded_frame);
  933. FF_ENABLE_DEPRECATION_WARNINGS
  934. #endif
  935. av_dict_free(&tmp);
  936. av_freep(&avctx->priv_data);
  937. av_freep(&avctx->subtitle_header);
  938. if (avci) {
  939. av_frame_free(&avci->to_free);
  940. av_frame_free(&avci->compat_decode_frame);
  941. av_frame_free(&avci->buffer_frame);
  942. av_packet_free(&avci->buffer_pkt);
  943. av_packet_free(&avci->last_pkt_props);
  944. av_packet_free(&avci->ds.in_pkt);
  945. ff_decode_bsfs_uninit(avctx);
  946. av_buffer_unref(&avci->pool);
  947. }
  948. av_freep(&avci);
  949. avctx->internal = NULL;
  950. avctx->codec = NULL;
  951. goto end;
  952. }
  953. void avsubtitle_free(AVSubtitle *sub)
  954. {
  955. int i;
  956. for (i = 0; i < sub->num_rects; i++) {
  957. av_freep(&sub->rects[i]->data[0]);
  958. av_freep(&sub->rects[i]->data[1]);
  959. av_freep(&sub->rects[i]->data[2]);
  960. av_freep(&sub->rects[i]->data[3]);
  961. av_freep(&sub->rects[i]->text);
  962. av_freep(&sub->rects[i]->ass);
  963. av_freep(&sub->rects[i]);
  964. }
  965. av_freep(&sub->rects);
  966. memset(sub, 0, sizeof(*sub));
  967. }
  968. av_cold int avcodec_close(AVCodecContext *avctx)
  969. {
  970. int i;
  971. if (!avctx)
  972. return 0;
  973. if (avcodec_is_open(avctx)) {
  974. if (CONFIG_FRAME_THREAD_ENCODER &&
  975. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  976. ff_frame_thread_encoder_free(avctx);
  977. }
  978. if (HAVE_THREADS && avctx->internal->thread_ctx)
  979. ff_thread_free(avctx);
  980. if (avctx->codec && avctx->codec->close)
  981. avctx->codec->close(avctx);
  982. avctx->internal->byte_buffer_size = 0;
  983. av_freep(&avctx->internal->byte_buffer);
  984. av_frame_free(&avctx->internal->to_free);
  985. av_frame_free(&avctx->internal->compat_decode_frame);
  986. av_frame_free(&avctx->internal->buffer_frame);
  987. av_packet_free(&avctx->internal->buffer_pkt);
  988. av_packet_free(&avctx->internal->last_pkt_props);
  989. av_packet_free(&avctx->internal->ds.in_pkt);
  990. av_buffer_unref(&avctx->internal->pool);
  991. if (avctx->hwaccel && avctx->hwaccel->uninit)
  992. avctx->hwaccel->uninit(avctx);
  993. av_freep(&avctx->internal->hwaccel_priv_data);
  994. ff_decode_bsfs_uninit(avctx);
  995. av_freep(&avctx->internal);
  996. }
  997. for (i = 0; i < avctx->nb_coded_side_data; i++)
  998. av_freep(&avctx->coded_side_data[i].data);
  999. av_freep(&avctx->coded_side_data);
  1000. avctx->nb_coded_side_data = 0;
  1001. av_buffer_unref(&avctx->hw_frames_ctx);
  1002. av_buffer_unref(&avctx->hw_device_ctx);
  1003. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1004. av_opt_free(avctx->priv_data);
  1005. av_opt_free(avctx);
  1006. av_freep(&avctx->priv_data);
  1007. if (av_codec_is_encoder(avctx->codec)) {
  1008. av_freep(&avctx->extradata);
  1009. #if FF_API_CODED_FRAME
  1010. FF_DISABLE_DEPRECATION_WARNINGS
  1011. av_frame_free(&avctx->coded_frame);
  1012. FF_ENABLE_DEPRECATION_WARNINGS
  1013. #endif
  1014. }
  1015. avctx->codec = NULL;
  1016. avctx->active_thread_type = 0;
  1017. return 0;
  1018. }
  1019. const char *avcodec_get_name(enum AVCodecID id)
  1020. {
  1021. const AVCodecDescriptor *cd;
  1022. AVCodec *codec;
  1023. if (id == AV_CODEC_ID_NONE)
  1024. return "none";
  1025. cd = avcodec_descriptor_get(id);
  1026. if (cd)
  1027. return cd->name;
  1028. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1029. codec = avcodec_find_decoder(id);
  1030. if (codec)
  1031. return codec->name;
  1032. codec = avcodec_find_encoder(id);
  1033. if (codec)
  1034. return codec->name;
  1035. return "unknown_codec";
  1036. }
  1037. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1038. {
  1039. int i, len, ret = 0;
  1040. #define TAG_PRINT(x) \
  1041. (((x) >= '0' && (x) <= '9') || \
  1042. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1043. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1044. for (i = 0; i < 4; i++) {
  1045. len = snprintf(buf, buf_size,
  1046. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1047. buf += len;
  1048. buf_size = buf_size > len ? buf_size - len : 0;
  1049. ret += len;
  1050. codec_tag >>= 8;
  1051. }
  1052. return ret;
  1053. }
  1054. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1055. {
  1056. const char *codec_type;
  1057. const char *codec_name;
  1058. const char *profile = NULL;
  1059. int64_t bitrate;
  1060. int new_line = 0;
  1061. AVRational display_aspect_ratio;
  1062. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  1063. if (!buf || buf_size <= 0)
  1064. return;
  1065. codec_type = av_get_media_type_string(enc->codec_type);
  1066. codec_name = avcodec_get_name(enc->codec_id);
  1067. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  1068. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  1069. codec_name);
  1070. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1071. if (enc->codec && strcmp(enc->codec->name, codec_name))
  1072. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  1073. if (profile)
  1074. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1075. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  1076. && av_log_get_level() >= AV_LOG_VERBOSE
  1077. && enc->refs)
  1078. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1079. ", %d reference frame%s",
  1080. enc->refs, enc->refs > 1 ? "s" : "");
  1081. if (enc->codec_tag)
  1082. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  1083. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  1084. switch (enc->codec_type) {
  1085. case AVMEDIA_TYPE_VIDEO:
  1086. {
  1087. char detail[256] = "(";
  1088. av_strlcat(buf, separator, buf_size);
  1089. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1090. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1091. av_get_pix_fmt_name(enc->pix_fmt));
  1092. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  1093. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  1094. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  1095. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1096. av_strlcatf(detail, sizeof(detail), "%s, ",
  1097. av_color_range_name(enc->color_range));
  1098. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1099. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1100. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1101. if (enc->colorspace != (int)enc->color_primaries ||
  1102. enc->colorspace != (int)enc->color_trc) {
  1103. new_line = 1;
  1104. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  1105. av_color_space_name(enc->colorspace),
  1106. av_color_primaries_name(enc->color_primaries),
  1107. av_color_transfer_name(enc->color_trc));
  1108. } else
  1109. av_strlcatf(detail, sizeof(detail), "%s, ",
  1110. av_get_colorspace_name(enc->colorspace));
  1111. }
  1112. if (enc->field_order != AV_FIELD_UNKNOWN) {
  1113. const char *field_order = "progressive";
  1114. if (enc->field_order == AV_FIELD_TT)
  1115. field_order = "top first";
  1116. else if (enc->field_order == AV_FIELD_BB)
  1117. field_order = "bottom first";
  1118. else if (enc->field_order == AV_FIELD_TB)
  1119. field_order = "top coded first (swapped)";
  1120. else if (enc->field_order == AV_FIELD_BT)
  1121. field_order = "bottom coded first (swapped)";
  1122. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  1123. }
  1124. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1125. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1126. av_strlcatf(detail, sizeof(detail), "%s, ",
  1127. av_chroma_location_name(enc->chroma_sample_location));
  1128. if (strlen(detail) > 1) {
  1129. detail[strlen(detail) - 2] = 0;
  1130. av_strlcatf(buf, buf_size, "%s)", detail);
  1131. }
  1132. }
  1133. if (enc->width) {
  1134. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  1135. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1136. "%dx%d",
  1137. enc->width, enc->height);
  1138. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1139. (enc->width != enc->coded_width ||
  1140. enc->height != enc->coded_height))
  1141. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1142. " (%dx%d)", enc->coded_width, enc->coded_height);
  1143. if (enc->sample_aspect_ratio.num) {
  1144. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1145. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  1146. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  1147. 1024 * 1024);
  1148. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1149. " [SAR %d:%d DAR %d:%d]",
  1150. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1151. display_aspect_ratio.num, display_aspect_ratio.den);
  1152. }
  1153. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1154. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1155. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1156. ", %d/%d",
  1157. enc->time_base.num / g, enc->time_base.den / g);
  1158. }
  1159. }
  1160. if (encode) {
  1161. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1162. ", q=%d-%d", enc->qmin, enc->qmax);
  1163. } else {
  1164. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  1165. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1166. ", Closed Captions");
  1167. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  1168. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1169. ", lossless");
  1170. }
  1171. break;
  1172. case AVMEDIA_TYPE_AUDIO:
  1173. av_strlcat(buf, separator, buf_size);
  1174. if (enc->sample_rate) {
  1175. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1176. "%d Hz, ", enc->sample_rate);
  1177. }
  1178. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1179. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1180. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1181. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1182. }
  1183. if ( enc->bits_per_raw_sample > 0
  1184. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  1185. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1186. " (%d bit)", enc->bits_per_raw_sample);
  1187. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  1188. if (enc->initial_padding)
  1189. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1190. ", delay %d", enc->initial_padding);
  1191. if (enc->trailing_padding)
  1192. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1193. ", padding %d", enc->trailing_padding);
  1194. }
  1195. break;
  1196. case AVMEDIA_TYPE_DATA:
  1197. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1198. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1199. if (g)
  1200. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1201. ", %d/%d",
  1202. enc->time_base.num / g, enc->time_base.den / g);
  1203. }
  1204. break;
  1205. case AVMEDIA_TYPE_SUBTITLE:
  1206. if (enc->width)
  1207. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1208. ", %dx%d", enc->width, enc->height);
  1209. break;
  1210. default:
  1211. return;
  1212. }
  1213. if (encode) {
  1214. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1215. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1216. ", pass 1");
  1217. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1218. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1219. ", pass 2");
  1220. }
  1221. bitrate = get_bit_rate(enc);
  1222. if (bitrate != 0) {
  1223. snprintf(buf + st