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.

846 lines
30KB

  1. /*
  2. * AVCodecContext functions for libavcodec
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * AVCodecContext functions for libavcodec
  23. */
  24. #include "config.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/mem.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/thread.h"
  31. #include "avcodec.h"
  32. #include "decode.h"
  33. #include "encode.h"
  34. #include "frame_thread_encoder.h"
  35. #include "internal.h"
  36. #include "thread.h"
  37. #if CONFIG_ICONV
  38. # include <iconv.h>
  39. #endif
  40. #include "libavutil/ffversion.h"
  41. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  42. unsigned avcodec_version(void)
  43. {
  44. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  45. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  46. av_assert0(AV_CODEC_ID_SRT==94216);
  47. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  48. return LIBAVCODEC_VERSION_INT;
  49. }
  50. const char *avcodec_configuration(void)
  51. {
  52. return FFMPEG_CONFIGURATION;
  53. }
  54. const char *avcodec_license(void)
  55. {
  56. #define LICENSE_PREFIX "libavcodec license: "
  57. return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
  58. }
  59. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  60. {
  61. int i;
  62. for (i = 0; i < count; i++) {
  63. int r = func(c, (char *)arg + i * size);
  64. if (ret)
  65. ret[i] = r;
  66. }
  67. emms_c();
  68. return 0;
  69. }
  70. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  71. {
  72. int i;
  73. for (i = 0; i < count; i++) {
  74. int r = func(c, arg, i, 0);
  75. if (ret)
  76. ret[i] = r;
  77. }
  78. emms_c();
  79. return 0;
  80. }
  81. static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
  82. static void lock_avcodec(const AVCodec *codec)
  83. {
  84. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  85. ff_mutex_lock(&codec_mutex);
  86. }
  87. static void unlock_avcodec(const AVCodec *codec)
  88. {
  89. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  90. ff_mutex_unlock(&codec_mutex);
  91. }
  92. #if FF_API_LOCKMGR
  93. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  94. {
  95. return 0;
  96. }
  97. #endif
  98. static int64_t get_bit_rate(AVCodecContext *ctx)
  99. {
  100. int64_t bit_rate;
  101. int bits_per_sample;
  102. switch (ctx->codec_type) {
  103. case AVMEDIA_TYPE_VIDEO:
  104. case AVMEDIA_TYPE_DATA:
  105. case AVMEDIA_TYPE_SUBTITLE:
  106. case AVMEDIA_TYPE_ATTACHMENT:
  107. bit_rate = ctx->bit_rate;
  108. break;
  109. case AVMEDIA_TYPE_AUDIO:
  110. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  111. if (bits_per_sample) {
  112. bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
  113. if (bit_rate > INT64_MAX / bits_per_sample) {
  114. bit_rate = 0;
  115. } else
  116. bit_rate *= bits_per_sample;
  117. } else
  118. bit_rate = ctx->bit_rate;
  119. break;
  120. default:
  121. bit_rate = 0;
  122. break;
  123. }
  124. return bit_rate;
  125. }
  126. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  127. {
  128. int ret = 0;
  129. int codec_init_ok = 0;
  130. AVDictionary *tmp = NULL;
  131. AVCodecInternal *avci;
  132. if (avcodec_is_open(avctx))
  133. return 0;
  134. if (!codec && !avctx->codec) {
  135. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  136. return AVERROR(EINVAL);
  137. }
  138. if (codec && avctx->codec && codec != avctx->codec) {
  139. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  140. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  141. return AVERROR(EINVAL);
  142. }
  143. if (!codec)
  144. codec = avctx->codec;
  145. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  146. avctx->codec_id == AV_CODEC_ID_NONE) {
  147. avctx->codec_type = codec->type;
  148. avctx->codec_id = codec->id;
  149. }
  150. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
  151. avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  152. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  153. return AVERROR(EINVAL);
  154. }
  155. avctx->codec = codec;
  156. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  157. return AVERROR(EINVAL);
  158. if (options)
  159. av_dict_copy(&tmp, *options, 0);
  160. lock_avcodec(codec);
  161. avci = av_mallocz(sizeof(*avci));
  162. if (!avci) {
  163. ret = AVERROR(ENOMEM);
  164. goto end;
  165. }
  166. avctx->internal = avci;
  167. #if FF_API_OLD_ENCDEC
  168. avci->to_free = av_frame_alloc();
  169. avci->compat_decode_frame = av_frame_alloc();
  170. avci->compat_encode_packet = av_packet_alloc();
  171. if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
  172. ret = AVERROR(ENOMEM);
  173. goto free_and_end;
  174. }
  175. #endif
  176. avci->buffer_frame = av_frame_alloc();
  177. avci->buffer_pkt = av_packet_alloc();
  178. avci->es.in_frame = av_frame_alloc();
  179. avci->ds.in_pkt = av_packet_alloc();
  180. avci->last_pkt_props = av_packet_alloc();
  181. avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
  182. if (!avci->buffer_frame || !avci->buffer_pkt ||
  183. !avci->es.in_frame || !avci->ds.in_pkt ||
  184. !avci->last_pkt_props || !avci->pkt_props) {
  185. ret = AVERROR(ENOMEM);
  186. goto free_and_end;
  187. }
  188. avci->skip_samples_multiplier = 1;
  189. if (codec->priv_data_size > 0) {
  190. if (!avctx->priv_data) {
  191. avctx->priv_data = av_mallocz(codec->priv_data_size);
  192. if (!avctx->priv_data) {
  193. ret = AVERROR(ENOMEM);
  194. goto free_and_end;
  195. }
  196. if (codec->priv_class) {
  197. *(const AVClass **)avctx->priv_data = codec->priv_class;
  198. av_opt_set_defaults(avctx->priv_data);
  199. }
  200. }
  201. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  202. goto free_and_end;
  203. } else {
  204. avctx->priv_data = NULL;
  205. }
  206. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  207. goto free_and_end;
  208. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  209. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  210. ret = AVERROR(EINVAL);
  211. goto free_and_end;
  212. }
  213. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  214. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  215. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  216. if (avctx->coded_width && avctx->coded_height)
  217. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  218. else if (avctx->width && avctx->height)
  219. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  220. if (ret < 0)
  221. goto free_and_end;
  222. }
  223. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  224. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  225. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  226. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  227. ff_set_dimensions(avctx, 0, 0);
  228. }
  229. if (avctx->width > 0 && avctx->height > 0) {
  230. if (av_image_check_sar(avctx->width, avctx->height,
  231. avctx->sample_aspect_ratio) < 0) {
  232. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  233. avctx->sample_aspect_ratio.num,
  234. avctx->sample_aspect_ratio.den);
  235. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  236. }
  237. }
  238. if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
  239. av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
  240. ret = AVERROR(EINVAL);
  241. goto free_and_end;
  242. }
  243. if (av_codec_is_decoder(codec) &&
  244. codec->type == AVMEDIA_TYPE_AUDIO &&
  245. !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF) &&
  246. avctx->channels == 0) {
  247. av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
  248. ret = AVERROR(EINVAL);
  249. goto free_and_end;
  250. }
  251. if (avctx->sample_rate < 0) {
  252. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
  253. ret = AVERROR(EINVAL);
  254. goto free_and_end;
  255. }
  256. if (avctx->block_align < 0) {
  257. av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
  258. ret = AVERROR(EINVAL);
  259. goto free_and_end;
  260. }
  261. avctx->frame_number = 0;
  262. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  263. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  264. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  265. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  266. const AVCodec *codec2;
  267. av_log(avctx, AV_LOG_ERROR,
  268. "The %s '%s' is experimental but experimental codecs are not enabled, "
  269. "add '-strict %d' if you want to use it.\n",
  270. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  271. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  272. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  273. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  274. codec_string, codec2->name);
  275. ret = AVERROR_EXPERIMENTAL;
  276. goto free_and_end;
  277. }
  278. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  279. (!avctx->time_base.num || !avctx->time_base.den)) {
  280. avctx->time_base.num = 1;
  281. avctx->time_base.den = avctx->sample_rate;
  282. }
  283. if (!HAVE_THREADS)
  284. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  285. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  286. unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  287. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  288. lock_avcodec(codec);
  289. if (ret < 0)
  290. goto free_and_end;
  291. }
  292. if (HAVE_THREADS
  293. && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  294. ret = ff_thread_init(avctx);
  295. if (ret < 0) {
  296. goto free_and_end;
  297. }
  298. }
  299. if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
  300. avctx->thread_count = 1;
  301. if (av_codec_is_encoder(avctx->codec))
  302. ret = ff_encode_preinit(avctx);
  303. else
  304. ret = ff_decode_preinit(avctx);
  305. if (ret < 0)
  306. goto free_and_end;
  307. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  308. || avci->frame_thread_encoder)) {
  309. ret = avctx->codec->init(avctx);
  310. if (ret < 0) {
  311. codec_init_ok = -1;
  312. goto free_and_end;
  313. }
  314. codec_init_ok = 1;
  315. }
  316. ret=0;
  317. if (av_codec_is_decoder(avctx->codec)) {
  318. if (!avctx->bit_rate)
  319. avctx->bit_rate = get_bit_rate(avctx);
  320. /* validate channel layout from the decoder */
  321. if (avctx->channel_layout) {
  322. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  323. if (!avctx->channels)
  324. avctx->channels = channels;
  325. else if (channels != avctx->channels) {
  326. char buf[512];
  327. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  328. av_log(avctx, AV_LOG_WARNING,
  329. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  330. "ignoring specified channel layout\n",
  331. buf, channels, avctx->channels);
  332. avctx->channel_layout = 0;
  333. }
  334. }
  335. if (avctx->channels && avctx->channels < 0 ||
  336. avctx->channels > FF_SANE_NB_CHANNELS) {
  337. ret = AVERROR(EINVAL);
  338. goto free_and_end;
  339. }
  340. if (avctx->bits_per_coded_sample < 0) {
  341. ret = AVERROR(EINVAL);
  342. goto free_and_end;
  343. }
  344. if (avctx->sub_charenc) {
  345. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  346. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  347. "supported with subtitles codecs\n");
  348. ret = AVERROR(EINVAL);
  349. goto free_and_end;
  350. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  351. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  352. "subtitles character encoding will be ignored\n",
  353. avctx->codec_descriptor->name);
  354. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  355. } else {
  356. /* input character encoding is set for a text based subtitle
  357. * codec at this point */
  358. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  359. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  360. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  361. #if CONFIG_ICONV
  362. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  363. if (cd == (iconv_t)-1) {
  364. ret = AVERROR(errno);
  365. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  366. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  367. goto free_and_end;
  368. }
  369. iconv_close(cd);
  370. #else
  371. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  372. "conversion needs a libavcodec built with iconv support "
  373. "for this codec\n");
  374. ret = AVERROR(ENOSYS);
  375. goto free_and_end;
  376. #endif
  377. }
  378. }
  379. }
  380. #if FF_API_AVCTX_TIMEBASE
  381. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  382. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  383. #endif
  384. }
  385. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  386. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  387. }
  388. end:
  389. unlock_avcodec(codec);
  390. if (options) {
  391. av_dict_free(options);
  392. *options = tmp;
  393. }
  394. return ret;
  395. free_and_end:
  396. if (avctx->codec && avctx->codec->close &&
  397. (codec_init_ok > 0 || (codec_init_ok < 0 &&
  398. avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  399. avctx->codec->close(avctx);
  400. if (HAVE_THREADS && avci->thread_ctx)
  401. ff_thread_free(avctx);
  402. if (codec->priv_class && avctx->priv_data)
  403. av_opt_free(avctx->priv_data);
  404. av_opt_free(avctx);
  405. if (av_codec_is_encoder(avctx->codec)) {
  406. #if FF_API_CODED_FRAME
  407. FF_DISABLE_DEPRECATION_WARNINGS
  408. av_frame_free(&avctx->coded_frame);
  409. FF_ENABLE_DEPRECATION_WARNINGS
  410. #endif
  411. av_freep(&avctx->extradata);
  412. avctx->extradata_size = 0;
  413. }
  414. av_dict_free(&tmp);
  415. av_freep(&avctx->priv_data);
  416. av_freep(&avctx->subtitle_header);
  417. #if FF_API_OLD_ENCDEC
  418. av_frame_free(&avci->to_free);
  419. av_frame_free(&avci->compat_decode_frame);
  420. av_packet_free(&avci->compat_encode_packet);
  421. #endif
  422. av_frame_free(&avci->buffer_frame);
  423. av_packet_free(&avci->buffer_pkt);
  424. av_packet_free(&avci->last_pkt_props);
  425. av_fifo_freep(&avci->pkt_props);
  426. av_packet_free(&avci->ds.in_pkt);
  427. av_frame_free(&avci->es.in_frame);
  428. av_bsf_free(&avci->bsf);
  429. av_buffer_unref(&avci->pool);
  430. av_freep(&avci);
  431. avctx->internal = NULL;
  432. avctx->codec = NULL;
  433. goto end;
  434. }
  435. void avcodec_flush_buffers(AVCodecContext *avctx)
  436. {
  437. AVCodecInternal *avci = avctx->internal;
  438. if (av_codec_is_encoder(avctx->codec)) {
  439. int caps = avctx->codec->capabilities;
  440. if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
  441. // Only encoders that explicitly declare support for it can be
  442. // flushed. Otherwise, this is a no-op.
  443. av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
  444. "that doesn't support it\n");
  445. return;
  446. }
  447. // We haven't implemented flushing for frame-threaded encoders.
  448. av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
  449. }
  450. avci->draining = 0;
  451. avci->draining_done = 0;
  452. avci->nb_draining_errors = 0;
  453. av_frame_unref(avci->buffer_frame);
  454. #if FF_API_OLD_ENCDEC
  455. av_frame_unref(avci->compat_decode_frame);
  456. av_packet_unref(avci->compat_encode_packet);
  457. #endif
  458. av_packet_unref(avci->buffer_pkt);
  459. av_packet_unref(avci->last_pkt_props);
  460. while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
  461. av_fifo_generic_read(avci->pkt_props,
  462. avci->last_pkt_props, sizeof(*avci->last_pkt_props),
  463. NULL);
  464. av_packet_unref(avci->last_pkt_props);
  465. }
  466. av_fifo_reset(avci->pkt_props);
  467. av_frame_unref(avci->es.in_frame);
  468. av_packet_unref(avci->ds.in_pkt);
  469. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  470. ff_thread_flush(avctx);
  471. else if (avctx->codec->flush)
  472. avctx->codec->flush(avctx);
  473. avctx->pts_correction_last_pts =
  474. avctx->pts_correction_last_dts = INT64_MIN;
  475. if (av_codec_is_decoder(avctx->codec))
  476. av_bsf_flush(avci->bsf);
  477. #if FF_API_OLD_ENCDEC
  478. FF_DISABLE_DEPRECATION_WARNINGS
  479. if (!avctx->refcounted_frames)
  480. av_frame_unref(avci->to_free);
  481. FF_ENABLE_DEPRECATION_WARNINGS
  482. #endif
  483. }
  484. void avsubtitle_free(AVSubtitle *sub)
  485. {
  486. int i;
  487. for (i = 0; i < sub->num_rects; i++) {
  488. av_freep(&sub->rects[i]->data[0]);
  489. av_freep(&sub->rects[i]->data[1]);
  490. av_freep(&sub->rects[i]->data[2]);
  491. av_freep(&sub->rects[i]->data[3]);
  492. av_freep(&sub->rects[i]->text);
  493. av_freep(&sub->rects[i]->ass);
  494. av_freep(&sub->rects[i]);
  495. }
  496. av_freep(&sub->rects);
  497. memset(sub, 0, sizeof(*sub));
  498. }
  499. av_cold int avcodec_close(AVCodecContext *avctx)
  500. {
  501. int i;
  502. if (!avctx)
  503. return 0;
  504. if (avcodec_is_open(avctx)) {
  505. if (CONFIG_FRAME_THREAD_ENCODER &&
  506. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  507. ff_frame_thread_encoder_free(avctx);
  508. }
  509. if (HAVE_THREADS && avctx->internal->thread_ctx)
  510. ff_thread_free(avctx);
  511. if (avctx->codec && avctx->codec->close)
  512. avctx->codec->close(avctx);
  513. avctx->internal->byte_buffer_size = 0;
  514. av_freep(&avctx->internal->byte_buffer);
  515. #if FF_API_OLD_ENCDEC
  516. av_frame_free(&avctx->internal->to_free);
  517. av_frame_free(&avctx->internal->compat_decode_frame);
  518. av_packet_free(&avctx->internal->compat_encode_packet);
  519. #endif
  520. av_frame_free(&avctx->internal->buffer_frame);
  521. av_packet_free(&avctx->internal->buffer_pkt);
  522. av_packet_unref(avctx->internal->last_pkt_props);
  523. while (av_fifo_size(avctx->internal->pkt_props) >=
  524. sizeof(*avctx->internal->last_pkt_props)) {
  525. av_fifo_generic_read(avctx->internal->pkt_props,
  526. avctx->internal->last_pkt_props,
  527. sizeof(*avctx->internal->last_pkt_props),
  528. NULL);
  529. av_packet_unref(avctx->internal->last_pkt_props);
  530. }
  531. av_packet_free(&avctx->internal->last_pkt_props);
  532. av_fifo_freep(&avctx->internal->pkt_props);
  533. av_packet_free(&avctx->internal->ds.in_pkt);
  534. av_frame_free(&avctx->internal->es.in_frame);
  535. av_buffer_unref(&avctx->internal->pool);
  536. if (avctx->hwaccel && avctx->hwaccel->uninit)
  537. avctx->hwaccel->uninit(avctx);
  538. av_freep(&avctx->internal->hwaccel_priv_data);
  539. av_bsf_free(&avctx->internal->bsf);
  540. av_freep(&avctx->internal);
  541. }
  542. for (i = 0; i < avctx->nb_coded_side_data; i++)
  543. av_freep(&avctx->coded_side_data[i].data);
  544. av_freep(&avctx->coded_side_data);
  545. avctx->nb_coded_side_data = 0;
  546. av_buffer_unref(&avctx->hw_frames_ctx);
  547. av_buffer_unref(&avctx->hw_device_ctx);
  548. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  549. av_opt_free(avctx->priv_data);
  550. av_opt_free(avctx);
  551. av_freep(&avctx->priv_data);
  552. if (av_codec_is_encoder(avctx->codec)) {
  553. av_freep(&avctx->extradata);
  554. #if FF_API_CODED_FRAME
  555. FF_DISABLE_DEPRECATION_WARNINGS
  556. av_frame_free(&avctx->coded_frame);
  557. FF_ENABLE_DEPRECATION_WARNINGS
  558. #endif
  559. }
  560. avctx->codec = NULL;
  561. avctx->active_thread_type = 0;
  562. return 0;
  563. }
  564. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  565. {
  566. const char *codec_type;
  567. const char *codec_name;
  568. const char *profile = NULL;
  569. int64_t bitrate;
  570. int new_line = 0;
  571. AVRational display_aspect_ratio;
  572. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  573. if (!buf || buf_size <= 0)
  574. return;
  575. codec_type = av_get_media_type_string(enc->codec_type);
  576. codec_name = avcodec_get_name(enc->codec_id);
  577. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  578. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  579. codec_name);
  580. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  581. if (enc->codec && strcmp(enc->codec->name, codec_name))
  582. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  583. if (profile)
  584. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  585. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  586. && av_log_get_level() >= AV_LOG_VERBOSE
  587. && enc->refs)
  588. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  589. ", %d reference frame%s",
  590. enc->refs, enc->refs > 1 ? "s" : "");
  591. if (enc->codec_tag)
  592. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  593. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  594. switch (enc->codec_type) {
  595. case AVMEDIA_TYPE_VIDEO:
  596. {
  597. char detail[256] = "(";
  598. av_strlcat(buf, separator, buf_size);
  599. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  600. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  601. av_get_pix_fmt_name(enc->pix_fmt));
  602. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  603. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  604. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  605. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  606. av_strlcatf(detail, sizeof(detail), "%s, ",
  607. av_color_range_name(enc->color_range));
  608. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  609. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  610. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  611. if (enc->colorspace != (int)enc->color_primaries ||
  612. enc->colorspace != (int)enc->color_trc) {
  613. new_line = 1;
  614. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  615. av_color_space_name(enc->colorspace),
  616. av_color_primaries_name(enc->color_primaries),
  617. av_color_transfer_name(enc->color_trc));
  618. } else
  619. av_strlcatf(detail, sizeof(detail), "%s, ",
  620. av_get_colorspace_name(enc->colorspace));
  621. }
  622. if (enc->field_order != AV_FIELD_UNKNOWN) {
  623. const char *field_order = "progressive";
  624. if (enc->field_order == AV_FIELD_TT)
  625. field_order = "top first";
  626. else if (enc->field_order == AV_FIELD_BB)
  627. field_order = "bottom first";
  628. else if (enc->field_order == AV_FIELD_TB)
  629. field_order = "top coded first (swapped)";
  630. else if (enc->field_order == AV_FIELD_BT)
  631. field_order = "bottom coded first (swapped)";
  632. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  633. }
  634. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  635. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  636. av_strlcatf(detail, sizeof(detail), "%s, ",
  637. av_chroma_location_name(enc->chroma_sample_location));
  638. if (strlen(detail) > 1) {
  639. detail[strlen(detail) - 2] = 0;
  640. av_strlcatf(buf, buf_size, "%s)", detail);
  641. }
  642. }
  643. if (enc->width) {
  644. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  645. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  646. "%dx%d",
  647. enc->width, enc->height);
  648. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  649. (enc->width != enc->coded_width ||
  650. enc->height != enc->coded_height))
  651. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  652. " (%dx%d)", enc->coded_width, enc->coded_height);
  653. if (enc->sample_aspect_ratio.num) {
  654. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  655. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  656. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  657. 1024 * 1024);
  658. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  659. " [SAR %d:%d DAR %d:%d]",
  660. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  661. display_aspect_ratio.num, display_aspect_ratio.den);
  662. }
  663. if (av_log_get_level() >= AV_LOG_DEBUG) {
  664. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  665. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  666. ", %d/%d",
  667. enc->time_base.num / g, enc->time_base.den / g);
  668. }
  669. }
  670. if (encode) {
  671. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  672. ", q=%d-%d", enc->qmin, enc->qmax);
  673. } else {
  674. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  675. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  676. ", Closed Captions");
  677. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  678. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  679. ", lossless");
  680. }
  681. break;
  682. case AVMEDIA_TYPE_AUDIO:
  683. av_strlcat(buf, separator, buf_size);
  684. if (enc->sample_rate) {
  685. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  686. "%d Hz, ", enc->sample_rate);
  687. }
  688. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  689. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  690. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  691. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  692. }
  693. if ( enc->bits_per_raw_sample > 0
  694. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  695. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  696. " (%d bit)", enc->bits_per_raw_sample);
  697. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  698. if (enc->initial_padding)
  699. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  700. ", delay %d", enc->initial_padding);
  701. if (enc->trailing_padding)
  702. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  703. ", padding %d", enc->trailing_padding);
  704. }
  705. break;
  706. case AVMEDIA_TYPE_DATA:
  707. if (av_log_get_level() >= AV_LOG_DEBUG) {
  708. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  709. if (g)
  710. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  711. ", %d/%d",
  712. enc->time_base.num / g, enc->time_base.den / g);
  713. }
  714. break;
  715. case AVMEDIA_TYPE_SUBTITLE:
  716. if (enc->width)
  717. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  718. ", %dx%d", enc->width, enc->height);
  719. break;
  720. default:
  721. return;
  722. }
  723. if (encode) {
  724. if (enc->flags & AV_CODEC_FLAG_PASS1)
  725. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  726. ", pass 1");
  727. if (enc->flags & AV_CODEC_FLAG_PASS2)
  728. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  729. ", pass 2");
  730. }
  731. bitrate = get_bit_rate(enc);
  732. if (bitrate != 0) {
  733. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  734. ", %"PRId64" kb/s", bitrate / 1000);
  735. } else if (enc->rc_max_rate > 0) {
  736. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  737. ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  738. }
  739. }
  740. int avcodec_is_open(AVCodecContext *s)
  741. {
  742. return !!s->internal;
  743. }