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->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  146. return AVERROR(EINVAL);
  147. if (options)
  148. av_dict_copy(&tmp, *options, 0);
  149. lock_avcodec(codec);
  150. avci = av_mallocz(sizeof(*avci));
  151. if (!avci) {
  152. ret = AVERROR(ENOMEM);
  153. goto end;
  154. }
  155. avctx->internal = avci;
  156. #if FF_API_OLD_ENCDEC
  157. avci->to_free = av_frame_alloc();
  158. avci->compat_decode_frame = av_frame_alloc();
  159. avci->compat_encode_packet = av_packet_alloc();
  160. if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
  161. ret = AVERROR(ENOMEM);
  162. goto free_and_end;
  163. }
  164. #endif
  165. avci->buffer_frame = av_frame_alloc();
  166. avci->buffer_pkt = av_packet_alloc();
  167. avci->es.in_frame = av_frame_alloc();
  168. avci->ds.in_pkt = av_packet_alloc();
  169. avci->last_pkt_props = av_packet_alloc();
  170. avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
  171. if (!avci->buffer_frame || !avci->buffer_pkt ||
  172. !avci->es.in_frame || !avci->ds.in_pkt ||
  173. !avci->last_pkt_props || !avci->pkt_props) {
  174. ret = AVERROR(ENOMEM);
  175. goto free_and_end;
  176. }
  177. avci->skip_samples_multiplier = 1;
  178. if (codec->priv_data_size > 0) {
  179. if (!avctx->priv_data) {
  180. avctx->priv_data = av_mallocz(codec->priv_data_size);
  181. if (!avctx->priv_data) {
  182. ret = AVERROR(ENOMEM);
  183. goto free_and_end;
  184. }
  185. if (codec->priv_class) {
  186. *(const AVClass **)avctx->priv_data = codec->priv_class;
  187. av_opt_set_defaults(avctx->priv_data);
  188. }
  189. }
  190. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  191. goto free_and_end;
  192. } else {
  193. avctx->priv_data = NULL;
  194. }
  195. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  196. goto free_and_end;
  197. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  198. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  199. ret = AVERROR(EINVAL);
  200. goto free_and_end;
  201. }
  202. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  203. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  204. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  205. if (avctx->coded_width && avctx->coded_height)
  206. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  207. else if (avctx->width && avctx->height)
  208. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  209. if (ret < 0)
  210. goto free_and_end;
  211. }
  212. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  213. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  214. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  215. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  216. ff_set_dimensions(avctx, 0, 0);
  217. }
  218. if (avctx->width > 0 && avctx->height > 0) {
  219. if (av_image_check_sar(avctx->width, avctx->height,
  220. avctx->sample_aspect_ratio) < 0) {
  221. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  222. avctx->sample_aspect_ratio.num,
  223. avctx->sample_aspect_ratio.den);
  224. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  225. }
  226. }
  227. if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
  228. av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
  229. ret = AVERROR(EINVAL);
  230. goto free_and_end;
  231. }
  232. if (av_codec_is_decoder(codec) &&
  233. codec->type == AVMEDIA_TYPE_AUDIO &&
  234. !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF) &&
  235. avctx->channels == 0) {
  236. av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
  237. ret = AVERROR(EINVAL);
  238. goto free_and_end;
  239. }
  240. if (avctx->sample_rate < 0) {
  241. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
  242. ret = AVERROR(EINVAL);
  243. goto free_and_end;
  244. }
  245. if (avctx->block_align < 0) {
  246. av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
  247. ret = AVERROR(EINVAL);
  248. goto free_and_end;
  249. }
  250. avctx->codec = codec;
  251. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  252. avctx->codec_id == AV_CODEC_ID_NONE) {
  253. avctx->codec_type = codec->type;
  254. avctx->codec_id = codec->id;
  255. }
  256. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  257. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  258. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  259. ret = AVERROR(EINVAL);
  260. goto free_and_end;
  261. }
  262. avctx->frame_number = 0;
  263. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  264. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  265. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  266. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  267. const AVCodec *codec2;
  268. av_log(avctx, AV_LOG_ERROR,
  269. "The %s '%s' is experimental but experimental codecs are not enabled, "
  270. "add '-strict %d' if you want to use it.\n",
  271. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  272. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  273. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  274. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  275. codec_string, codec2->name);
  276. ret = AVERROR_EXPERIMENTAL;
  277. goto free_and_end;
  278. }
  279. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  280. (!avctx->time_base.num || !avctx->time_base.den)) {
  281. avctx->time_base.num = 1;
  282. avctx->time_base.den = avctx->sample_rate;
  283. }
  284. if (!HAVE_THREADS)
  285. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  286. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  287. unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  288. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  289. lock_avcodec(codec);
  290. if (ret < 0)
  291. goto free_and_end;
  292. }
  293. if (HAVE_THREADS
  294. && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  295. ret = ff_thread_init(avctx);
  296. if (ret < 0) {
  297. goto free_and_end;
  298. }
  299. }
  300. if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
  301. avctx->thread_count = 1;
  302. if (av_codec_is_encoder(avctx->codec))
  303. ret = ff_encode_preinit(avctx);
  304. else
  305. ret = ff_decode_preinit(avctx);
  306. if (ret < 0)
  307. goto free_and_end;
  308. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  309. || avci->frame_thread_encoder)) {
  310. ret = avctx->codec->init(avctx);
  311. if (ret < 0) {
  312. codec_init_ok = -1;
  313. goto free_and_end;
  314. }
  315. codec_init_ok = 1;
  316. }
  317. ret=0;
  318. if (av_codec_is_decoder(avctx->codec)) {
  319. if (!avctx->bit_rate)
  320. avctx->bit_rate = get_bit_rate(avctx);
  321. /* validate channel layout from the decoder */
  322. if (avctx->channel_layout) {
  323. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  324. if (!avctx->channels)
  325. avctx->channels = channels;
  326. else if (channels != avctx->channels) {
  327. char buf[512];
  328. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  329. av_log(avctx, AV_LOG_WARNING,
  330. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  331. "ignoring specified channel layout\n",
  332. buf, channels, avctx->channels);
  333. avctx->channel_layout = 0;
  334. }
  335. }
  336. if (avctx->channels && avctx->channels < 0 ||
  337. avctx->channels > FF_SANE_NB_CHANNELS) {
  338. ret = AVERROR(EINVAL);
  339. goto free_and_end;
  340. }
  341. if (avctx->bits_per_coded_sample < 0) {
  342. ret = AVERROR(EINVAL);
  343. goto free_and_end;
  344. }
  345. if (avctx->sub_charenc) {
  346. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  347. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  348. "supported with subtitles codecs\n");
  349. ret = AVERROR(EINVAL);
  350. goto free_and_end;
  351. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  352. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  353. "subtitles character encoding will be ignored\n",
  354. avctx->codec_descriptor->name);
  355. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  356. } else {
  357. /* input character encoding is set for a text based subtitle
  358. * codec at this point */
  359. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  360. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  361. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  362. #if CONFIG_ICONV
  363. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  364. if (cd == (iconv_t)-1) {
  365. ret = AVERROR(errno);
  366. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  367. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  368. goto free_and_end;
  369. }
  370. iconv_close(cd);
  371. #else
  372. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  373. "conversion needs a libavcodec built with iconv support "
  374. "for this codec\n");
  375. ret = AVERROR(ENOSYS);
  376. goto free_and_end;
  377. #endif
  378. }
  379. }
  380. }
  381. #if FF_API_AVCTX_TIMEBASE
  382. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  383. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  384. #endif
  385. }
  386. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  387. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  388. }
  389. end:
  390. unlock_avcodec(codec);
  391. if (options) {
  392. av_dict_free(options);
  393. *options = tmp;
  394. }
  395. return ret;
  396. free_and_end:
  397. if (avctx->codec && avctx->codec->close &&
  398. (codec_init_ok > 0 || (codec_init_ok < 0 &&
  399. avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  400. avctx->codec->close(avctx);
  401. if (HAVE_THREADS && avci->thread_ctx)
  402. ff_thread_free(avctx);
  403. if (codec->priv_class && avctx->priv_data)
  404. av_opt_free(avctx->priv_data);
  405. av_opt_free(avctx);
  406. if (av_codec_is_encoder(avctx->codec)) {
  407. #if FF_API_CODED_FRAME
  408. FF_DISABLE_DEPRECATION_WARNINGS
  409. av_frame_free(&avctx->coded_frame);
  410. FF_ENABLE_DEPRECATION_WARNINGS
  411. #endif
  412. av_freep(&avctx->extradata);
  413. avctx->extradata_size = 0;
  414. }
  415. av_dict_free(&tmp);
  416. av_freep(&avctx->priv_data);
  417. av_freep(&avctx->subtitle_header);
  418. #if FF_API_OLD_ENCDEC
  419. av_frame_free(&avci->to_free);
  420. av_frame_free(&avci->compat_decode_frame);
  421. av_packet_free(&avci->compat_encode_packet);
  422. #endif
  423. av_frame_free(&avci->buffer_frame);
  424. av_packet_free(&avci->buffer_pkt);
  425. av_packet_free(&avci->last_pkt_props);
  426. av_fifo_freep(&avci->pkt_props);
  427. av_packet_free(&avci->ds.in_pkt);
  428. av_frame_free(&avci->es.in_frame);
  429. av_bsf_free(&avci->bsf);
  430. av_buffer_unref(&avci->pool);
  431. av_freep(&avci);
  432. avctx->internal = NULL;
  433. avctx->codec = NULL;
  434. goto end;
  435. }
  436. void avcodec_flush_buffers(AVCodecContext *avctx)
  437. {
  438. AVCodecInternal *avci = avctx->internal;
  439. if (av_codec_is_encoder(avctx->codec)) {
  440. int caps = avctx->codec->capabilities;
  441. if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
  442. // Only encoders that explicitly declare support for it can be
  443. // flushed. Otherwise, this is a no-op.
  444. av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
  445. "that doesn't support it\n");
  446. return;
  447. }
  448. // We haven't implemented flushing for frame-threaded encoders.
  449. av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
  450. }
  451. avci->draining = 0;
  452. avci->draining_done = 0;
  453. avci->nb_draining_errors = 0;
  454. av_frame_unref(avci->buffer_frame);
  455. #if FF_API_OLD_ENCDEC
  456. av_frame_unref(avci->compat_decode_frame);
  457. av_packet_unref(avci->compat_encode_packet);
  458. #endif
  459. av_packet_unref(avci->buffer_pkt);
  460. av_packet_unref(avci->last_pkt_props);
  461. while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
  462. av_fifo_generic_read(avci->pkt_props,
  463. avci->last_pkt_props, sizeof(*avci->last_pkt_props),
  464. NULL);
  465. av_packet_unref(avci->last_pkt_props);
  466. }
  467. av_fifo_reset(avci->pkt_props);
  468. av_frame_unref(avci->es.in_frame);
  469. av_packet_unref(avci->ds.in_pkt);
  470. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  471. ff_thread_flush(avctx);
  472. else if (avctx->codec->flush)
  473. avctx->codec->flush(avctx);
  474. avctx->pts_correction_last_pts =
  475. avctx->pts_correction_last_dts = INT64_MIN;
  476. if (av_codec_is_decoder(avctx->codec))
  477. av_bsf_flush(avci->bsf);
  478. #if FF_API_OLD_ENCDEC
  479. FF_DISABLE_DEPRECATION_WARNINGS
  480. if (!avctx->refcounted_frames)
  481. av_frame_unref(avci->to_free);
  482. FF_ENABLE_DEPRECATION_WARNINGS
  483. #endif
  484. }
  485. void avsubtitle_free(AVSubtitle *sub)
  486. {
  487. int i;
  488. for (i = 0; i < sub->num_rects; i++) {
  489. av_freep(&sub->rects[i]->data[0]);
  490. av_freep(&sub->rects[i]->data[1]);
  491. av_freep(&sub->rects[i]->data[2]);
  492. av_freep(&sub->rects[i]->data[3]);
  493. av_freep(&sub->rects[i]->text);
  494. av_freep(&sub->rects[i]->ass);
  495. av_freep(&sub->rects[i]);
  496. }
  497. av_freep(&sub->rects);
  498. memset(sub, 0, sizeof(*sub));
  499. }
  500. av_cold int avcodec_close(AVCodecContext *avctx)
  501. {
  502. int i;
  503. if (!avctx)
  504. return 0;
  505. if (avcodec_is_open(avctx)) {
  506. if (CONFIG_FRAME_THREAD_ENCODER &&
  507. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  508. ff_frame_thread_encoder_free(avctx);
  509. }
  510. if (HAVE_THREADS && avctx->internal->thread_ctx)
  511. ff_thread_free(avctx);
  512. if (avctx->codec && avctx->codec->close)
  513. avctx->codec->close(avctx);
  514. avctx->internal->byte_buffer_size = 0;
  515. av_freep(&avctx->internal->byte_buffer);
  516. #if FF_API_OLD_ENCDEC
  517. av_frame_free(&avctx->internal->to_free);
  518. av_frame_free(&avctx->internal->compat_decode_frame);
  519. av_packet_free(&avctx->internal->compat_encode_packet);
  520. #endif
  521. av_frame_free(&avctx->internal->buffer_frame);
  522. av_packet_free(&avctx->internal->buffer_pkt);
  523. av_packet_unref(avctx->internal->last_pkt_props);
  524. while (av_fifo_size(avctx->internal->pkt_props) >=
  525. sizeof(*avctx->internal->last_pkt_props)) {
  526. av_fifo_generic_read(avctx->internal->pkt_props,
  527. avctx->internal->last_pkt_props,
  528. sizeof(*avctx->internal->last_pkt_props),
  529. NULL);
  530. av_packet_unref(avctx->internal->last_pkt_props);
  531. }
  532. av_packet_free(&avctx->internal->last_pkt_props);
  533. av_fifo_freep(&avctx->internal->pkt_props);
  534. av_packet_free(&avctx->internal->ds.in_pkt);
  535. av_frame_free(&avctx->internal->es.in_frame);
  536. av_buffer_unref(&avctx->internal->pool);
  537. if (avctx->hwaccel && avctx->hwaccel->uninit)
  538. avctx->hwaccel->uninit(avctx);
  539. av_freep(&avctx->internal->hwaccel_priv_data);
  540. av_bsf_free(&avctx->internal->bsf);
  541. av_freep(&avctx->internal);
  542. }
  543. for (i = 0; i < avctx->nb_coded_side_data; i++)
  544. av_freep(&avctx->coded_side_data[i].data);
  545. av_freep(&avctx->coded_side_data);
  546. avctx->nb_coded_side_data = 0;
  547. av_buffer_unref(&avctx->hw_frames_ctx);
  548. av_buffer_unref(&avctx->hw_device_ctx);
  549. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  550. av_opt_free(avctx->priv_data);
  551. av_opt_free(avctx);
  552. av_freep(&avctx->priv_data);
  553. if (av_codec_is_encoder(avctx->codec)) {
  554. av_freep(&avctx->extradata);
  555. #if FF_API_CODED_FRAME
  556. FF_DISABLE_DEPRECATION_WARNINGS
  557. av_frame_free(&avctx->coded_frame);
  558. FF_ENABLE_DEPRECATION_WARNINGS
  559. #endif
  560. }
  561. avctx->codec = NULL;
  562. avctx->active_thread_type = 0;
  563. return 0;
  564. }
  565. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  566. {
  567. const char *codec_type;
  568. const char *codec_name;
  569. const char *profile = NULL;
  570. int64_t bitrate;
  571. int new_line = 0;
  572. AVRational display_aspect_ratio;
  573. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  574. if (!buf || buf_size <= 0)
  575. return;
  576. codec_type = av_get_media_type_string(enc->codec_type);
  577. codec_name = avcodec_get_name(enc->codec_id);
  578. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  579. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  580. codec_name);
  581. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  582. if (enc->codec && strcmp(enc->codec->name, codec_name))
  583. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  584. if (profile)
  585. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  586. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  587. && av_log_get_level() >= AV_LOG_VERBOSE
  588. && enc->refs)
  589. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  590. ", %d reference frame%s",
  591. enc->refs, enc->refs > 1 ? "s" : "");
  592. if (enc->codec_tag)
  593. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  594. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  595. switch (enc->codec_type) {
  596. case AVMEDIA_TYPE_VIDEO:
  597. {
  598. char detail[256] = "(";
  599. av_strlcat(buf, separator, buf_size);
  600. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  601. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  602. av_get_pix_fmt_name(enc->pix_fmt));
  603. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  604. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  605. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  606. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  607. av_strlcatf(detail, sizeof(detail), "%s, ",
  608. av_color_range_name(enc->color_range));
  609. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  610. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  611. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  612. if (enc->colorspace != (int)enc->color_primaries ||
  613. enc->colorspace != (int)enc->color_trc) {
  614. new_line = 1;
  615. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  616. av_color_space_name(enc->colorspace),
  617. av_color_primaries_name(enc->color_primaries),
  618. av_color_transfer_name(enc->color_trc));
  619. } else
  620. av_strlcatf(detail, sizeof(detail), "%s, ",
  621. av_get_colorspace_name(enc->colorspace));
  622. }
  623. if (enc->field_order != AV_FIELD_UNKNOWN) {
  624. const char *field_order = "progressive";
  625. if (enc->field_order == AV_FIELD_TT)
  626. field_order = "top first";
  627. else if (enc->field_order == AV_FIELD_BB)
  628. field_order = "bottom first";
  629. else if (enc->field_order == AV_FIELD_TB)
  630. field_order = "top coded first (swapped)";
  631. else if (enc->field_order == AV_FIELD_BT)
  632. field_order = "bottom coded first (swapped)";
  633. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  634. }
  635. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  636. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  637. av_strlcatf(detail, sizeof(detail), "%s, ",
  638. av_chroma_location_name(enc->chroma_sample_location));
  639. if (strlen(detail) > 1) {
  640. detail[strlen(detail) - 2] = 0;
  641. av_strlcatf(buf, buf_size, "%s)", detail);
  642. }
  643. }
  644. if (enc->width) {
  645. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  646. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  647. "%dx%d",
  648. enc->width, enc->height);
  649. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  650. (enc->width != enc->coded_width ||
  651. enc->height != enc->coded_height))
  652. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  653. " (%dx%d)", enc->coded_width, enc->coded_height);
  654. if (enc->sample_aspect_ratio.num) {
  655. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  656. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  657. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  658. 1024 * 1024);
  659. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  660. " [SAR %d:%d DAR %d:%d]",
  661. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  662. display_aspect_ratio.num, display_aspect_ratio.den);
  663. }
  664. if (av_log_get_level() >= AV_LOG_DEBUG) {
  665. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  666. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  667. ", %d/%d",
  668. enc->time_base.num / g, enc->time_base.den / g);
  669. }
  670. }
  671. if (encode) {
  672. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  673. ", q=%d-%d", enc->qmin, enc->qmax);
  674. } else {
  675. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  676. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  677. ", Closed Captions");
  678. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  679. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  680. ", lossless");
  681. }
  682. break;
  683. case AVMEDIA_TYPE_AUDIO:
  684. av_strlcat(buf, separator, buf_size);
  685. if (enc->sample_rate) {
  686. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  687. "%d Hz, ", enc->sample_rate);
  688. }
  689. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  690. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  691. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  692. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  693. }
  694. if ( enc->bits_per_raw_sample > 0
  695. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  696. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  697. " (%d bit)", enc->bits_per_raw_sample);
  698. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  699. if (enc->initial_padding)
  700. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  701. ", delay %d", enc->initial_padding);
  702. if (enc->trailing_padding)
  703. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  704. ", padding %d", enc->trailing_padding);
  705. }
  706. break;
  707. case AVMEDIA_TYPE_DATA:
  708. if (av_log_get_level() >= AV_LOG_DEBUG) {
  709. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  710. if (g)
  711. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  712. ", %d/%d",
  713. enc->time_base.num / g, enc->time_base.den / g);
  714. }
  715. break;
  716. case AVMEDIA_TYPE_SUBTITLE:
  717. if (enc->width)
  718. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  719. ", %dx%d", enc->width, enc->height);
  720. break;
  721. default:
  722. return;
  723. }
  724. if (encode) {
  725. if (enc->flags & AV_CODEC_FLAG_PASS1)
  726. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  727. ", pass 1");
  728. if (enc->flags & AV_CODEC_FLAG_PASS2)
  729. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  730. ", pass 2");
  731. }
  732. bitrate = get_bit_rate(enc);
  733. if (bitrate != 0) {
  734. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  735. ", %"PRId64" kb/s", bitrate / 1000);
  736. } else if (enc->rc_max_rate > 0) {
  737. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  738. ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  739. }
  740. }
  741. int avcodec_is_open(AVCodecContext *s)
  742. {
  743. return !!s->internal;
  744. }