[FFmpeg-devel] [PATCH] extract bit rate calculation into separate function
Robert Krüger
krueger
Tue Nov 10 15:14:02 CET 2009
Hi,
this patch extracts the bit rate calculation into a new function so
applications using libav like ffprobe do not have to duplicate this.
Regression tests passed. At least I think they did because they
completed without a final error message.
I hope this meets your requirements. If not, please let me know what I
should change.
Thanks and regards,
Robert
Index: libavcodec/utils.c
===================================================================
--- libavcodec/utils.c (revision 20498)
+++ libavcodec/utils.c (working copy)
@@ -741,6 +741,74 @@
return NULL;
}
+/**
+ * Calculates the bit rate of a stream
+ *
+ * @ctx AVCodecContext of the stream
+ * @return bit rate in bits per second
+ */
+int avcodec_get_bitrate(AVCodecContext *ctx)
+{
+ int bitrate;
+
+ switch(ctx->codec_type) {
+ case CODEC_TYPE_VIDEO:
+ bitrate = ctx->bit_rate;
+ break;
+ case CODEC_TYPE_AUDIO:
+ /* for PCM codecs, compute bitrate directly */
+ switch(ctx->codec_id) {
+ case CODEC_ID_PCM_F64BE:
+ case CODEC_ID_PCM_F64LE:
+ bitrate = ctx->sample_rate * ctx->channels * 64;
+ break;
+ case CODEC_ID_PCM_S32LE:
+ case CODEC_ID_PCM_S32BE:
+ case CODEC_ID_PCM_U32LE:
+ case CODEC_ID_PCM_U32BE:
+ case CODEC_ID_PCM_F32BE:
+ case CODEC_ID_PCM_F32LE:
+ bitrate = ctx->sample_rate * ctx->channels * 32;
+ break;
+ case CODEC_ID_PCM_S24LE:
+ case CODEC_ID_PCM_S24BE:
+ case CODEC_ID_PCM_U24LE:
+ case CODEC_ID_PCM_U24BE:
+ case CODEC_ID_PCM_S24DAUD:
+ bitrate = ctx->sample_rate * ctx->channels * 24;
+ break;
+ case CODEC_ID_PCM_S16LE:
+ case CODEC_ID_PCM_S16BE:
+ case CODEC_ID_PCM_S16LE_PLANAR:
+ case CODEC_ID_PCM_U16LE:
+ case CODEC_ID_PCM_U16BE:
+ bitrate = ctx->sample_rate * ctx->channels * 16;
+ break;
+ case CODEC_ID_PCM_S8:
+ case CODEC_ID_PCM_U8:
+ case CODEC_ID_PCM_ALAW:
+ case CODEC_ID_PCM_MULAW:
+ case CODEC_ID_PCM_ZORK:
+ bitrate = ctx->sample_rate * ctx->channels * 8;
+ break;
+ default:
+ bitrate = ctx->bit_rate;
+ break;
+ }
+ break;
+ case CODEC_TYPE_DATA:
+ bitrate = ctx->bit_rate;
+ break;
+ case CODEC_TYPE_SUBTITLE:
+ bitrate = ctx->bit_rate;
+ break;
+ case CODEC_TYPE_ATTACHMENT:
+ bitrate = ctx->bit_rate;
+ break;
+ }
+ return bitrate;
+}
+
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc,
int encode)
{
const char *codec_name;
@@ -813,7 +881,6 @@
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", q=%d-%d", enc->qmin, enc->qmax);
}
- bitrate = enc->bit_rate;
break;
case CODEC_TYPE_AUDIO:
snprintf(buf, buf_size,
@@ -830,57 +897,15 @@
", %s", avcodec_get_sample_fmt_name(enc-
>sample_fmt));
}
- /* for PCM codecs, compute bitrate directly */
- switch(enc->codec_id) {
- case CODEC_ID_PCM_F64BE:
- case CODEC_ID_PCM_F64LE:
- bitrate = enc->sample_rate * enc->channels * 64;
- break;
- case CODEC_ID_PCM_S32LE:
- case CODEC_ID_PCM_S32BE:
- case CODEC_ID_PCM_U32LE:
- case CODEC_ID_PCM_U32BE:
- case CODEC_ID_PCM_F32BE:
- case CODEC_ID_PCM_F32LE:
- bitrate = enc->sample_rate * enc->channels * 32;
- break;
- case CODEC_ID_PCM_S24LE:
- case CODEC_ID_PCM_S24BE:
- case CODEC_ID_PCM_U24LE:
- case CODEC_ID_PCM_U24BE:
- case CODEC_ID_PCM_S24DAUD:
- bitrate = enc->sample_rate * enc->channels * 24;
- break;
- case CODEC_ID_PCM_S16LE:
- case CODEC_ID_PCM_S16BE:
- case CODEC_ID_PCM_S16LE_PLANAR:
- case CODEC_ID_PCM_U16LE:
- case CODEC_ID_PCM_U16BE:
- bitrate = enc->sample_rate * enc->channels * 16;
- break;
- case CODEC_ID_PCM_S8:
- case CODEC_ID_PCM_U8:
- case CODEC_ID_PCM_ALAW:
- case CODEC_ID_PCM_MULAW:
- case CODEC_ID_PCM_ZORK:
- bitrate = enc->sample_rate * enc->channels * 8;
- break;
- default:
- bitrate = enc->bit_rate;
- break;
- }
break;
case CODEC_TYPE_DATA:
snprintf(buf, buf_size, "Data: %s", codec_name);
- bitrate = enc->bit_rate;
break;
case CODEC_TYPE_SUBTITLE:
snprintf(buf, buf_size, "Subtitle: %s", codec_name);
- bitrate = enc->bit_rate;
break;
case CODEC_TYPE_ATTACHMENT:
snprintf(buf, buf_size, "Attachment: %s", codec_name);
- bitrate = enc->bit_rate;
break;
default:
snprintf(buf, buf_size, "Invalid Codec type %d", enc-
>codec_type);
@@ -894,6 +919,7 @@
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", pass 2");
}
+ bitrate = avcodec_get_bitrate(enc);
if (bitrate != 0) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", %d kb/s", bitrate / 1000);
More information about the ffmpeg-devel
mailing list