[FFmpeg-devel] [PATCH] examples/muxing: merge add_audio_stream() and add_video_stream()
Stefano Sabatini
stefasab at gmail.com
Fri Aug 3 18:20:37 CEST 2012
Factorize.
---
doc/examples/muxing.c | 130 +++++++++++++++++++-----------------------------
1 files changed, 52 insertions(+), 78 deletions(-)
diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c
index a5d5cf5..c462035 100644
--- a/doc/examples/muxing.c
+++ b/doc/examples/muxing.c
@@ -47,23 +47,14 @@
static int sws_flags = SWS_BICUBIC;
-/**************************************************************/
-/* audio output */
-
-static float t, tincr, tincr2;
-static int16_t *samples;
-static int audio_input_frame_size;
-
-/*
- * add an audio output stream
- */
-static AVStream *add_audio_stream(AVFormatContext *oc, enum CodecID codec_id)
+/* Add an output stream. */
+static AVStream *add_stream(AVFormatContext *oc, enum CodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
AVCodec *codec;
- /* find the audio encoder */
+ /* find the encoder */
codec = avcodec_find_encoder(codec_id);
if (!codec) {
fprintf(stderr, "codec not found\n");
@@ -75,24 +66,64 @@ static AVStream *add_audio_stream(AVFormatContext *oc, enum CodecID codec_id)
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
- st->id = 1;
c = st->codec;
+ avcodec_get_context_defaults3(c, codec);
+ c->codec_id = codec_id;
c->codec = codec;
- /* put sample parameters */
- c->sample_fmt = AV_SAMPLE_FMT_S16;
- c->bit_rate = 64000;
- c->sample_rate = 44100;
- c->channels = 2;
+ switch (codec->type) {
+ case AVMEDIA_TYPE_AUDIO:
+ st->id = 1;
+ c->sample_fmt = AV_SAMPLE_FMT_S16;
+ c->bit_rate = 64000;
+ c->sample_rate = 44100;
+ c->channels = 2;
+ break;
+
+ case AVMEDIA_TYPE_VIDEO:
+ c->bit_rate = 400000;
+ /* Resolution must be a multiple of two. */
+ c->width = 352;
+ c->height = 288;
+ /* timebase: This is the fundamental unit of time (in seconds) in terms
+ * of which frame timestamps are represented. For fixed-fps content,
+ * timebase should be 1/framerate and timestamp increments should be
+ * identical to 1. */
+ c->time_base.den = STREAM_FRAME_RATE;
+ c->time_base.num = 1;
+ c->gop_size = 12; /* emit one intra frame every twelve frames at most */
+ c->pix_fmt = STREAM_PIX_FMT;
+ if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
+ /* just for testing, we also add B frames */
+ c->max_b_frames = 2;
+ }
+ if (c->codec_id == CODEC_ID_MPEG1VIDEO) {
+ /* Needed to avoid using macroblocks in which some coeffs overflow.
+ * This does not happen with normal video, it just happens here as
+ * the motion of the chroma plane does not match the luma plane. */
+ c->mb_decision = 2;
+ }
+ break;
+
+ default:
+ break;
+ }
- // some formats want stream headers to be separate
+ /* Some formats want stream headers to be separate. */
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
+/**************************************************************/
+/* audio output */
+
+static float t, tincr, tincr2;
+static int16_t *samples;
+static int audio_input_frame_size;
+
static void open_audio(AVFormatContext *oc, AVStream *st)
{
AVCodecContext *c;
@@ -182,63 +213,6 @@ static AVFrame *picture, *tmp_picture;
static uint8_t *video_outbuf;
static int frame_count, video_outbuf_size;
-/* Add a video output stream. */
-static AVStream *add_video_stream(AVFormatContext *oc, enum CodecID codec_id)
-{
- AVCodecContext *c;
- AVStream *st;
- AVCodec *codec;
-
- /* find the video encoder */
- codec = avcodec_find_encoder(codec_id);
- if (!codec) {
- fprintf(stderr, "codec not found\n");
- exit(1);
- }
-
- st = avformat_new_stream(oc, codec);
- if (!st) {
- fprintf(stderr, "Could not alloc stream\n");
- exit(1);
- }
-
- c = st->codec;
-
- avcodec_get_context_defaults3(c, codec);
-
- c->codec_id = codec_id;
- c->codec = codec;
-
- /* Put sample parameters. */
- c->bit_rate = 400000;
- /* Resolution must be a multiple of two. */
- c->width = 352;
- c->height = 288;
- /* timebase: This is the fundamental unit of time (in seconds) in terms
- * of which frame timestamps are represented. For fixed-fps content,
- * timebase should be 1/framerate and timestamp increments should be
- * identical to 1. */
- c->time_base.den = STREAM_FRAME_RATE;
- c->time_base.num = 1;
- c->gop_size = 12; /* emit one intra frame every twelve frames at most */
- c->pix_fmt = STREAM_PIX_FMT;
- if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
- /* just for testing, we also add B frames */
- c->max_b_frames = 2;
- }
- if (c->codec_id == CODEC_ID_MPEG1VIDEO) {
- /* Needed to avoid using macroblocks in which some coeffs overflow.
- * This does not happen with normal video, it just happens here as
- * the motion of the chroma plane does not match the luma plane. */
- c->mb_decision = 2;
- }
- /* Some formats want stream headers to be separate. */
- if (oc->oformat->flags & AVFMT_GLOBALHEADER)
- c->flags |= CODEC_FLAG_GLOBAL_HEADER;
-
- return st;
-}
-
static AVFrame *alloc_picture(enum PixelFormat pix_fmt, int width, int height)
{
AVFrame *picture = avcodec_alloc_frame();
@@ -447,10 +421,10 @@ int main(int argc, char **argv)
video_st = NULL;
audio_st = NULL;
if (fmt->video_codec != CODEC_ID_NONE) {
- video_st = add_video_stream(oc, fmt->video_codec);
+ video_st = add_stream(oc, fmt->video_codec);
}
if (fmt->audio_codec != CODEC_ID_NONE) {
- audio_st = add_audio_stream(oc, fmt->audio_codec);
+ audio_st = add_stream(oc, fmt->audio_codec);
}
/* Now that all the parameters are set, we can open the audio and
--
1.7.5.4
More information about the ffmpeg-devel
mailing list