avcodec.c 25 KB

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