+
+static const char *unknown_if_null(const char *str)
+{
+ return str ? str : "unknown";
+}
+
+void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
+{
+ const char *codec_type;
+ const char *codec_name;
+ const char *profile = NULL;
+ AVBPrint bprint;
+ int64_t bitrate;
+ int new_line = 0;
+ AVRational display_aspect_ratio;
+ const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
+ const char *str;
+
+ if (!buf || buf_size <= 0)
+ return;
+ av_bprint_init_for_buffer(&bprint, buf, buf_size);
+ codec_type = av_get_media_type_string(enc->codec_type);
+ codec_name = avcodec_get_name(enc->codec_id);
+ profile = avcodec_profile_name(enc->codec_id, enc->profile);
+
+ av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
+ codec_name);
+ buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
+
+ if (enc->codec && strcmp(enc->codec->name, codec_name))
+ av_bprintf(&bprint, " (%s)", enc->codec->name);
+
+ if (profile)
+ av_bprintf(&bprint, " (%s)", profile);
+ if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
+ && av_log_get_level() >= AV_LOG_VERBOSE
+ && enc->refs)
+ av_bprintf(&bprint, ", %d reference frame%s",
+ enc->refs, enc->refs > 1 ? "s" : "");
+
+ if (enc->codec_tag)
+ av_bprintf(&bprint, " (%s / 0x%04X)",
+ av_fourcc2str(enc->codec_tag), enc->codec_tag);
+
+ switch (enc->codec_type) {
+ case AVMEDIA_TYPE_VIDEO:
+ {
+ unsigned len;
+
+ av_bprintf(&bprint, "%s%s", separator,
+ enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
+ unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
+
+ av_bprint_chars(&bprint, '(', 1);
+ len = bprint.len;
+
+ /* The following check ensures that '(' has been written
+ * and therefore allows us to erase it if it turns out
+ * to be unnecessary. */
+ if (!av_bprint_is_complete(&bprint))
+ return;
+
+ if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
+ enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
+ av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
+ if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
+ (str = av_color_range_name(enc->color_range)))
+ av_bprintf(&bprint, "%s, ", str);
+
+ if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
+ enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
+ enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
+ const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
+ const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
+ const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
+ if (strcmp(col, pri) || strcmp(col, trc)) {
+ new_line = 1;
+ av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
+ } else
+ av_bprintf(&bprint, "%s, ", col);
+ }
+
+ if (enc->field_order != AV_FIELD_UNKNOWN) {
+ const char *field_order = "progressive";
+ if (enc->field_order == AV_FIELD_TT)
+ field_order = "top first";
+ else if (enc->field_order == AV_FIELD_BB)
+ field_order = "bottom first";
+ else if (enc->field_order == AV_FIELD_TB)
+ field_order = "top coded first (swapped)";
+ else if (enc->field_order == AV_FIELD_BT)
+ field_order = "bottom coded first (swapped)";
+
+ av_bprintf(&bprint, "%s, ", field_order);
+ }
+
+ if (av_log_get_level() >= AV_LOG_VERBOSE &&
+ enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
+ (str = av_chroma_location_name(enc->chroma_sample_location)))
+ av_bprintf(&bprint, "%s, ", str);
+
+ if (len == bprint.len) {
+ bprint.str[len - 1] = '\0';
+ bprint.len--;
+ } else {
+ if (bprint.len - 2 < bprint.size) {
+ /* Erase the last ", " */
+ bprint.len -= 2;
+ bprint.str[bprint.len] = '\0';
+ }
+ av_bprint_chars(&bprint, ')', 1);
+ }
+ }
+
+ if (enc->width) {
+ av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
+ enc->width, enc->height);
+
+ if (av_log_get_level() >= AV_LOG_VERBOSE &&
+ (enc->width != enc->coded_width ||
+ enc->height != enc->coded_height))
+ av_bprintf(&bprint, " (%dx%d)",
+ enc->coded_width, enc->coded_height);
+
+ if (enc->sample_aspect_ratio.num) {
+ av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
+ enc->width * (int64_t)enc->sample_aspect_ratio.num,
+ enc->height * (int64_t)enc->sample_aspect_ratio.den,
+ 1024 * 1024);
+ av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
+ enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
+ display_aspect_ratio.num, display_aspect_ratio.den);
+ }
+ if (av_log_get_level() >= AV_LOG_DEBUG) {
+ int g = av_gcd(enc->time_base.num, enc->time_base.den);
+ av_bprintf(&bprint, ", %d/%d",
+ enc->time_base.num / g, enc->time_base.den / g);
+ }
+ }
+ if (encode) {
+ av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
+ } else {
+ if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
+ av_bprintf(&bprint, ", Closed Captions");
+ if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
+ av_bprintf(&bprint, ", lossless");
+ }
+ break;
+ case AVMEDIA_TYPE_AUDIO:
+ av_bprintf(&bprint, "%s", separator);
+
+ if (enc->sample_rate) {
+ av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
+ }
+ av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout);
+ if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
+ (str = av_get_sample_fmt_name(enc->sample_fmt))) {
+ av_bprintf(&bprint, ", %s", str);
+ }
+ if ( enc->bits_per_raw_sample > 0
+ && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
+ av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
+ if (av_log_get_level() >= AV_LOG_VERBOSE) {
+ if (enc->initial_padding)
+ av_bprintf(&bprint, ", delay %d", enc->initial_padding);
+ if (enc->trailing_padding)
+ av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
+ }
+ break;
+ case AVMEDIA_TYPE_DATA:
+ if (av_log_get_level() >= AV_LOG_DEBUG) {
+ int g = av_gcd(enc->time_base.num, enc->time_base.den);
+ if (g)
+ av_bprintf(&bprint, ", %d/%d",
+ enc->time_base.num / g, enc->time_base.den / g);
+ }
+ break;
+ case AVMEDIA_TYPE_SUBTITLE:
+ if (enc->width)
+ av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
+ break;
+ default:
+ return;
+ }
+ if (encode) {
+ if (enc->flags & AV_CODEC_FLAG_PASS1)
+ av_bprintf(&bprint, ", pass 1");
+ if (enc->flags & AV_CODEC_FLAG_PASS2)
+ av_bprintf(&bprint, ", pass 2");
+ }
+ bitrate = get_bit_rate(enc);
+ if (bitrate != 0) {
+ av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
+ } else if (enc->rc_max_rate > 0) {
+ av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
+ }
+}
+
+int avcodec_is_open(AVCodecContext *s)
+{
+ return !!s->internal;
+}