avcodec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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/bprint.h"
  28. #include "libavutil/channel_layout.h"
  29. #include "libavutil/emms.h"
  30. #include "libavutil/fifo.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/mem.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/thread.h"
  35. #include "avcodec.h"
  36. #include "avcodec_internal.h"
  37. #include "bsf.h"
  38. #include "codec_internal.h"
  39. #include "decode.h"
  40. #include "encode.h"
  41. #include "frame_thread_encoder.h"
  42. #include "hwconfig.h"
  43. #include "internal.h"
  44. #include "thread.h"
  45. /**
  46. * Maximum size in bytes of extradata.
  47. * This value was chosen such that every bit of the buffer is
  48. * addressable by a 32-bit signed integer as used by get_bits.
  49. */
  50. #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
  51. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  52. {
  53. size_t i;
  54. for (i = 0; i < count; i++) {
  55. size_t offset = i * size;
  56. int r = func(c, FF_PTR_ADD((char *)arg, offset));
  57. if (ret)
  58. ret[i] = r;
  59. }
  60. emms_c();
  61. return 0;
  62. }
  63. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  64. {
  65. int i;
  66. for (i = 0; i < count; i++) {
  67. int r = func(c, arg, i, 0);
  68. if (ret)
  69. ret[i] = r;
  70. }
  71. emms_c();
  72. return 0;
  73. }
  74. static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
  75. static void lock_avcodec(const FFCodec *codec)
  76. {
  77. if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
  78. ff_mutex_lock(&codec_mutex);
  79. }
  80. static void unlock_avcodec(const FFCodec *codec)
  81. {
  82. if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
  83. ff_mutex_unlock(&codec_mutex);
  84. }
  85. static int64_t get_bit_rate(AVCodecContext *ctx)
  86. {
  87. int64_t bit_rate;
  88. int bits_per_sample;
  89. switch (ctx->codec_type) {
  90. case AVMEDIA_TYPE_VIDEO:
  91. case AVMEDIA_TYPE_DATA:
  92. case AVMEDIA_TYPE_SUBTITLE:
  93. case AVMEDIA_TYPE_ATTACHMENT:
  94. bit_rate = ctx->bit_rate;
  95. break;
  96. case AVMEDIA_TYPE_AUDIO:
  97. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  98. if (bits_per_sample) {
  99. bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
  100. if (bit_rate > INT64_MAX / bits_per_sample) {
  101. bit_rate = 0;
  102. } else
  103. bit_rate *= bits_per_sample;
  104. } else
  105. bit_rate = ctx->bit_rate;
  106. break;
  107. default:
  108. bit_rate = 0;
  109. break;
  110. }
  111. return bit_rate;
  112. }
  113. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  114. {
  115. int ret = 0;
  116. AVCodecInternal *avci;
  117. const FFCodec *codec2;
  118. if (avcodec_is_open(avctx))
  119. return 0;
  120. if (!codec && !avctx->codec) {
  121. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  122. return AVERROR(EINVAL);
  123. }
  124. if (codec && avctx->codec && codec != avctx->codec) {
  125. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  126. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  127. return AVERROR(EINVAL);
  128. }
  129. if (!codec)
  130. codec = avctx->codec;
  131. codec2 = ffcodec(codec);
  132. if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
  133. (avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec->id)) {
  134. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  135. return AVERROR(EINVAL);
  136. }
  137. avctx->codec_type = codec->type;
  138. avctx->codec_id = codec->id;
  139. avctx->codec = codec;
  140. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  141. return AVERROR(EINVAL);
  142. avci = av_codec_is_decoder(codec) ?
  143. ff_decode_internal_alloc() :
  144. ff_encode_internal_alloc();
  145. if (!avci) {
  146. ret = AVERROR(ENOMEM);
  147. goto end;
  148. }
  149. avctx->internal = avci;
  150. avci->buffer_frame = av_frame_alloc();
  151. avci->buffer_pkt = av_packet_alloc();
  152. if (!avci->buffer_frame || !avci->buffer_pkt) {
  153. ret = AVERROR(ENOMEM);
  154. goto free_and_end;
  155. }
  156. if (codec2->priv_data_size > 0) {
  157. if (!avctx->priv_data) {
  158. avctx->priv_data = av_mallocz(codec2->priv_data_size);
  159. if (!avctx->priv_data) {
  160. ret = AVERROR(ENOMEM);
  161. goto free_and_end;
  162. }
  163. if (codec->priv_class) {
  164. *(const AVClass **)avctx->priv_data = codec->priv_class;
  165. av_opt_set_defaults(avctx->priv_data);
  166. }
  167. }
  168. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, options)) < 0)
  169. goto free_and_end;
  170. } else {
  171. avctx->priv_data = NULL;
  172. }
  173. if ((ret = av_opt_set_dict(avctx, options)) < 0)
  174. goto free_and_end;
  175. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  176. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  177. ret = AVERROR(EINVAL);
  178. goto free_and_end;
  179. }
  180. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  181. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  182. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  183. if (avctx->coded_width && avctx->coded_height)
  184. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  185. else if (avctx->width && avctx->height)
  186. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  187. if (ret < 0)
  188. goto free_and_end;
  189. }
  190. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  191. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  192. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  193. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  194. ff_set_dimensions(avctx, 0, 0);
  195. }
  196. if (avctx->width > 0 && avctx->height > 0) {
  197. if (av_image_check_sar(avctx->width, avctx->height,
  198. avctx->sample_aspect_ratio) < 0) {
  199. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  200. avctx->sample_aspect_ratio.num,
  201. avctx->sample_aspect_ratio.den);
  202. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  203. }
  204. }
  205. if (avctx->sample_rate < 0) {
  206. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
  207. ret = AVERROR(EINVAL);
  208. goto free_and_end;
  209. }
  210. if (avctx->block_align < 0) {
  211. av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
  212. ret = AVERROR(EINVAL);
  213. goto free_and_end;
  214. }
  215. #if FF_API_OLD_CHANNEL_LAYOUT
  216. FF_DISABLE_DEPRECATION_WARNINGS
  217. /* compat wrapper for old-style callers */
  218. if (avctx->channel_layout && !avctx->channels)
  219. avctx->channels = av_popcount64(avctx->channel_layout);
  220. if ((avctx->channels && avctx->ch_layout.nb_channels != avctx->channels) ||
  221. (avctx->channel_layout && (avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
  222. avctx->ch_layout.u.mask != avctx->channel_layout))) {
  223. av_channel_layout_uninit(&avctx->ch_layout);
  224. if (avctx->channel_layout) {
  225. av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout);
  226. } else {
  227. avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  228. }
  229. avctx->ch_layout.nb_channels = avctx->channels;
  230. }
  231. FF_ENABLE_DEPRECATION_WARNINGS
  232. #endif
  233. /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
  234. * in particular checks that nb_channels is set for all audio encoders. */
  235. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
  236. && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
  237. av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n",
  238. av_codec_is_decoder(codec) ? "Decoder" : "Encoder");
  239. ret = AVERROR(EINVAL);
  240. goto free_and_end;
  241. }
  242. if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) {
  243. av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
  244. ret = AVERROR(EINVAL);
  245. goto free_and_end;
  246. }
  247. if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
  248. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
  249. ret = AVERROR(EINVAL);
  250. goto free_and_end;
  251. }
  252. avctx->frame_num = 0;
  253. #if FF_API_AVCTX_FRAME_NUMBER
  254. FF_DISABLE_DEPRECATION_WARNINGS
  255. avctx->frame_number = avctx->frame_num;
  256. FF_ENABLE_DEPRECATION_WARNINGS
  257. #endif
  258. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  259. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  260. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  261. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  262. const AVCodec *codec2;
  263. av_log(avctx, AV_LOG_ERROR,
  264. "The %s '%s' is experimental but experimental codecs are not enabled, "
  265. "add '-strict %d' if you want to use it.\n",
  266. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  267. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  268. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  269. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  270. codec_string, codec2->name);
  271. ret = AVERROR_EXPERIMENTAL;
  272. goto free_and_end;
  273. }
  274. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  275. (!avctx->time_base.num || !avctx->time_base.den)) {
  276. avctx->time_base.num = 1;
  277. avctx->time_base.den = avctx->sample_rate;
  278. }
  279. if (av_codec_is_encoder(avctx->codec))
  280. ret = ff_encode_preinit(avctx);
  281. else
  282. ret = ff_decode_preinit(avctx);
  283. if (ret < 0)
  284. goto free_and_end;
  285. if (HAVE_THREADS && !avci->frame_thread_encoder) {
  286. /* Frame-threaded decoders call FFCodec.init for their child contexts. */
  287. lock_avcodec(codec2);
  288. ret = ff_thread_init(avctx);
  289. unlock_avcodec(codec2);
  290. if (ret < 0) {
  291. goto free_and_end;
  292. }
  293. }
  294. if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
  295. avctx->thread_count = 1;
  296. if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
  297. avci->frame_thread_encoder) {
  298. if (codec2->init) {
  299. lock_avcodec(codec2);
  300. ret = codec2->init(avctx);
  301. unlock_avcodec(codec2);
  302. if (ret < 0) {
  303. avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
  304. goto free_and_end;
  305. }
  306. }
  307. avci->needs_close = 1;
  308. }
  309. ret=0;
  310. if (av_codec_is_decoder(avctx->codec)) {
  311. if (!avctx->bit_rate)
  312. avctx->bit_rate = get_bit_rate(avctx);
  313. #if FF_API_OLD_CHANNEL_LAYOUT
  314. FF_DISABLE_DEPRECATION_WARNINGS
  315. /* update the deprecated fields for old-style callers */
  316. avctx->channels = avctx->ch_layout.nb_channels;
  317. avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
  318. avctx->ch_layout.u.mask : 0;
  319. FF_ENABLE_DEPRECATION_WARNINGS
  320. #endif
  321. /* validate channel layout from the decoder */
  322. if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
  323. avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
  324. ret = AVERROR(EINVAL);
  325. goto free_and_end;
  326. }
  327. if (avctx->bits_per_coded_sample < 0) {
  328. ret = AVERROR(EINVAL);
  329. goto free_and_end;
  330. }
  331. }
  332. if (codec->priv_class)
  333. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  334. end:
  335. return ret;
  336. free_and_end:
  337. avcodec_close(avctx);
  338. goto end;
  339. }
  340. void avcodec_flush_buffers(AVCodecContext *avctx)
  341. {
  342. AVCodecInternal *avci = avctx->internal;
  343. if (av_codec_is_encoder(avctx->codec)) {
  344. int caps = avctx->codec->capabilities;
  345. if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
  346. // Only encoders that explicitly declare support for it can be
  347. // flushed. Otherwise, this is a no-op.
  348. av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
  349. "that doesn't support it\n");
  350. return;
  351. }
  352. ff_encode_flush_buffers(avctx);
  353. } else
  354. ff_decode_flush_buffers(avctx);
  355. avci->draining = 0;
  356. avci->draining_done = 0;
  357. av_frame_unref(avci->buffer_frame);
  358. av_packet_unref(avci->buffer_pkt);
  359. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  360. ff_thread_flush(avctx);
  361. else if (ffcodec(avctx->codec)->flush)
  362. ffcodec(avctx->codec)->flush(avctx);
  363. }
  364. void avsubtitle_free(AVSubtitle *sub)
  365. {
  366. int i;
  367. for (i = 0; i < sub->num_rects; i++) {
  368. AVSubtitleRect *const rect = sub->rects[i];
  369. av_freep(&rect->data[0]);
  370. av_freep(&rect->data[1]);
  371. av_freep(&rect->data[2]);
  372. av_freep(&rect->data[3]);
  373. av_freep(&rect->text);
  374. av_freep(&rect->ass);
  375. av_freep(&sub->rects[i]);
  376. }
  377. av_freep(&sub->rects);
  378. memset(sub, 0, sizeof(*sub));
  379. }
  380. av_cold int avcodec_close(AVCodecContext *avctx)
  381. {
  382. int i;
  383. if (!avctx)
  384. return 0;
  385. if (avcodec_is_open(avctx)) {
  386. AVCodecInternal *avci = avctx->internal;
  387. if (CONFIG_FRAME_THREAD_ENCODER &&
  388. avci->frame_thread_encoder && avctx->thread_count > 1) {
  389. ff_frame_thread_encoder_free(avctx);
  390. }
  391. if (HAVE_THREADS && avci->thread_ctx)
  392. ff_thread_free(avctx);
  393. if (avci->needs_close && ffcodec(avctx->codec)->close)
  394. ffcodec(avctx->codec)->close(avctx);
  395. avci->byte_buffer_size = 0;
  396. av_freep(&avci->byte_buffer);
  397. av_frame_free(&avci->buffer_frame);
  398. av_packet_free(&avci->buffer_pkt);
  399. av_packet_free(&avci->last_pkt_props);
  400. av_packet_free(&avci->in_pkt);
  401. av_frame_free(&avci->in_frame);
  402. av_frame_free(&avci->recon_frame);
  403. av_buffer_unref(&avci->pool);
  404. ff_hwaccel_uninit(avctx);
  405. av_bsf_free(&avci->bsf);
  406. #if FF_API_DROPCHANGED
  407. av_channel_layout_uninit(&avci->initial_ch_layout);
  408. #endif
  409. #if CONFIG_LCMS2
  410. ff_icc_context_uninit(&avci->icc);
  411. #endif
  412. av_freep(&avctx->internal);
  413. }
  414. for (i = 0; i < avctx->nb_coded_side_data; i++)
  415. av_freep(&avctx->coded_side_data[i].data);
  416. av_freep(&avctx->coded_side_data);
  417. avctx->nb_coded_side_data = 0;
  418. av_buffer_unref(&avctx->hw_frames_ctx);
  419. av_buffer_unref(&avctx->hw_device_ctx);
  420. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  421. av_opt_free(avctx->priv_data);
  422. av_opt_free(avctx);
  423. av_freep(&avctx->priv_data);
  424. if (av_codec_is_encoder(avctx->codec)) {
  425. av_freep(&avctx->extradata);
  426. avctx->extradata_size = 0;
  427. } else if (av_codec_is_decoder(avctx->codec))
  428. av_freep(&avctx->subtitle_header);
  429. avctx->codec = NULL;
  430. avctx->active_thread_type = 0;
  431. return 0;
  432. }
  433. static const char *unknown_if_null(const char *str)
  434. {
  435. return str ? str : "unknown";
  436. }
  437. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  438. {
  439. const char *codec_type;
  440. const char *codec_name;
  441. const char *profile = NULL;
  442. AVBPrint bprint;
  443. int64_t bitrate;
  444. int new_line = 0;
  445. AVRational display_aspect_ratio;
  446. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  447. const char *str;
  448. if (!buf || buf_size <= 0)
  449. return;
  450. av_bprint_init_for_buffer(&bprint, buf, buf_size);
  451. codec_type = av_get_media_type_string(enc->codec_type);
  452. codec_name = avcodec_get_name(enc->codec_id);
  453. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  454. av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
  455. codec_name);
  456. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  457. if (enc->codec && strcmp(enc->codec->name, codec_name))
  458. av_bprintf(&bprint, " (%s)", enc->codec->name);
  459. if (profile)
  460. av_bprintf(&bprint, " (%s)", profile);
  461. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  462. && av_log_get_level() >= AV_LOG_VERBOSE
  463. && enc->refs)
  464. av_bprintf(&bprint, ", %d reference frame%s",
  465. enc->refs, enc->refs > 1 ? "s" : "");
  466. if (enc->codec_tag)
  467. av_bprintf(&bprint, " (%s / 0x%04X)",
  468. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  469. switch (enc->codec_type) {
  470. case AVMEDIA_TYPE_VIDEO:
  471. {
  472. unsigned len;
  473. av_bprintf(&bprint, "%s%s", separator,
  474. enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  475. unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
  476. av_bprint_chars(&bprint, '(', 1);
  477. len = bprint.len;
  478. /* The following check ensures that '(' has been written
  479. * and therefore allows us to erase it if it turns out
  480. * to be unnecessary. */
  481. if (!av_bprint_is_complete(&bprint))
  482. return;
  483. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  484. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  485. av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
  486. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
  487. (str = av_color_range_name(enc->color_range)))
  488. av_bprintf(&bprint, "%s, ", str);
  489. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  490. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  491. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  492. const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
  493. const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
  494. const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
  495. if (strcmp(col, pri) || strcmp(col, trc)) {
  496. new_line = 1;
  497. av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
  498. } else
  499. av_bprintf(&bprint, "%s, ", col);
  500. }
  501. if (enc->field_order != AV_FIELD_UNKNOWN) {
  502. const char *field_order = "progressive";
  503. if (enc->field_order == AV_FIELD_TT)
  504. field_order = "top first";
  505. else if (enc->field_order == AV_FIELD_BB)
  506. field_order = "bottom first";
  507. else if (enc->field_order == AV_FIELD_TB)
  508. field_order = "top coded first (swapped)";
  509. else if (enc->field_order == AV_FIELD_BT)
  510. field_order = "bottom coded first (swapped)";
  511. av_bprintf(&bprint, "%s, ", field_order);
  512. }
  513. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  514. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
  515. (str = av_chroma_location_name(enc->chroma_sample_location)))
  516. av_bprintf(&bprint, "%s, ", str);
  517. if (len == bprint.len) {
  518. bprint.str[len - 1] = '\0';
  519. bprint.len--;
  520. } else {
  521. if (bprint.len - 2 < bprint.size) {
  522. /* Erase the last ", " */
  523. bprint.len -= 2;
  524. bprint.str[bprint.len] = '\0';
  525. }
  526. av_bprint_chars(&bprint, ')', 1);
  527. }
  528. }
  529. if (enc->width) {
  530. av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
  531. enc->width, enc->height);
  532. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  533. (enc->width != enc->coded_width ||
  534. enc->height != enc->coded_height))
  535. av_bprintf(&bprint, " (%dx%d)",
  536. enc->coded_width, enc->coded_height);
  537. if (enc->sample_aspect_ratio.num) {
  538. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  539. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  540. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  541. 1024 * 1024);
  542. av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
  543. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  544. display_aspect_ratio.num, display_aspect_ratio.den);
  545. }
  546. if (av_log_get_level() >= AV_LOG_DEBUG) {
  547. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  548. av_bprintf(&bprint, ", %d/%d",
  549. enc->time_base.num / g, enc->time_base.den / g);
  550. }
  551. }
  552. if (encode) {
  553. av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
  554. } else {
  555. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  556. av_bprintf(&bprint, ", Closed Captions");
  557. if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
  558. av_bprintf(&bprint, ", Film Grain");
  559. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  560. av_bprintf(&bprint, ", lossless");
  561. }
  562. break;
  563. case AVMEDIA_TYPE_AUDIO:
  564. av_bprintf(&bprint, "%s", separator);
  565. if (enc->sample_rate) {
  566. av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
  567. }
  568. {
  569. char buf[512];
  570. int ret = av_channel_layout_describe(&enc->ch_layout, buf, sizeof(buf));
  571. if (ret >= 0)
  572. av_bprintf(&bprint, "%s", buf);
  573. }
  574. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
  575. (str = av_get_sample_fmt_name(enc->sample_fmt))) {
  576. av_bprintf(&bprint, ", %s", str);
  577. }
  578. if ( enc->bits_per_raw_sample > 0
  579. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  580. av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
  581. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  582. if (enc->initial_padding)
  583. av_bprintf(&bprint, ", delay %d", enc->initial_padding);
  584. if (enc->trailing_padding)
  585. av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
  586. }
  587. break;
  588. case AVMEDIA_TYPE_DATA:
  589. if (av_log_get_level() >= AV_LOG_DEBUG) {
  590. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  591. if (g)
  592. av_bprintf(&bprint, ", %d/%d",
  593. enc->time_base.num / g, enc->time_base.den / g);
  594. }
  595. break;
  596. case AVMEDIA_TYPE_SUBTITLE:
  597. if (enc->width)
  598. av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
  599. break;
  600. default:
  601. return;
  602. }
  603. if (encode) {
  604. if (enc->flags & AV_CODEC_FLAG_PASS1)
  605. av_bprintf(&bprint, ", pass 1");
  606. if (enc->flags & AV_CODEC_FLAG_PASS2)
  607. av_bprintf(&bprint, ", pass 2");
  608. }
  609. bitrate = get_bit_rate(enc);
  610. if (bitrate != 0) {
  611. av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
  612. } else if (enc->rc_max_rate > 0) {
  613. av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  614. }
  615. }
  616. int avcodec_is_open(AVCodecContext *s)
  617. {
  618. return !!s->internal;
  619. }
  620. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  621. {
  622. av_frame_unref(frame);
  623. if (av_codec_is_decoder(avctx->codec))
  624. return ff_decode_receive_frame(avctx, frame);
  625. return ff_encode_receive_frame(avctx, frame);
  626. }