2 * Libavformat API example: Output a media file in any supported
3 * libavformat format. The default codecs are used.
5 * Copyright (c) 2003 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #define M_PI 3.1415926535897931
35 /* 5 seconds stream duration */
36 #define STREAM_DURATION 5.0
39 /**************************************************************/
42 float t, tincr, tincr2;
44 uint8_t *audio_outbuf;
45 int audio_outbuf_size;
46 int audio_input_frame_size;
49 * add an audio output stream
51 AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
56 st = av_new_stream(oc, 1);
58 fprintf(stderr, "Could not alloc stream\n");
63 c->codec_id = codec_id;
64 c->codec_type = CODEC_TYPE_AUDIO;
66 /* put sample parameters */
68 c->sample_rate = 44100;
73 void open_audio(AVFormatContext *oc, AVStream *st)
80 /* find the audio encoder */
81 codec = avcodec_find_encoder(c->codec_id);
83 fprintf(stderr, "codec not found\n");
88 if (avcodec_open(c, codec) < 0) {
89 fprintf(stderr, "could not open codec\n");
93 /* init signal generator */
95 tincr = 2 * M_PI * 110.0 / c->sample_rate;
96 /* increment frequency by 110 Hz per second */
97 tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
99 audio_outbuf_size = 10000;
100 audio_outbuf = malloc(audio_outbuf_size);
102 /* ugly hack for PCM codecs (will be removed ASAP with new PCM
103 support to compute the input frame size in samples */
104 if (c->frame_size <= 1) {
105 audio_input_frame_size = audio_outbuf_size / c->channels;
106 switch(st->codec.codec_id) {
107 case CODEC_ID_PCM_S16LE:
108 case CODEC_ID_PCM_S16BE:
109 case CODEC_ID_PCM_U16LE:
110 case CODEC_ID_PCM_U16BE:
111 audio_input_frame_size >>= 1;
117 audio_input_frame_size = c->frame_size;
119 samples = malloc(audio_input_frame_size * 2 * c->channels);
122 /* prepare a 16 bit dummy audio frame of 'frame_size' samples and
123 'nb_channels' channels */
124 void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
130 for(j=0;j<frame_size;j++) {
131 v = (int)(sin(t) * 10000);
132 for(i = 0; i < nb_channels; i++)
139 void write_audio_frame(AVFormatContext *oc, AVStream *st)
147 get_audio_frame(samples, audio_input_frame_size, c->channels);
149 out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
151 /* write the compressed frame in the media file */
152 if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) {
153 fprintf(stderr, "Error while writing audio frame\n");
158 void close_audio(AVFormatContext *oc, AVStream *st)
160 avcodec_close(&st->codec);
163 av_free(audio_outbuf);
166 /**************************************************************/
169 AVFrame *picture, *tmp_picture;
170 uint8_t *video_outbuf;
171 int frame_count, video_outbuf_size;
173 /* add a video output stream */
174 AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
179 st = av_new_stream(oc, 0);
181 fprintf(stderr, "Could not alloc stream\n");
186 c->codec_id = codec_id;
187 c->codec_type = CODEC_TYPE_VIDEO;
189 /* put sample parameters */
190 c->bit_rate = 400000;
191 /* resolution must be a multiple of two */
194 /* frames per second */
196 c->frame_rate_base= 1;
197 c->gop_size = 12; /* emit one intra frame every twelve frames */
202 AVFrame *alloc_picture(int pix_fmt, int width, int height)
205 uint8_t *picture_buf;
208 picture = avcodec_alloc_frame();
211 size = avpicture_get_size(pix_fmt, width, height);
212 picture_buf = malloc(size);
217 avpicture_fill((AVPicture *)picture, picture_buf,
218 pix_fmt, width, height);
222 void open_video(AVFormatContext *oc, AVStream *st)
229 /* find the video encoder */
230 codec = avcodec_find_encoder(c->codec_id);
232 fprintf(stderr, "codec not found\n");
237 if (avcodec_open(c, codec) < 0) {
238 fprintf(stderr, "could not open codec\n");
243 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
244 /* allocate output buffer */
245 /* XXX: API change will be done */
246 video_outbuf_size = 200000;
247 video_outbuf = malloc(video_outbuf_size);
250 /* allocate the encoded raw picture */
251 picture = alloc_picture(c->pix_fmt, c->width, c->height);
253 fprintf(stderr, "Could not allocate picture\n");
257 /* if the output format is not YUV420P, then a temporary YUV420P
258 picture is needed too. It is then converted to the required
261 if (c->pix_fmt != PIX_FMT_YUV420P) {
262 tmp_picture = alloc_picture(PIX_FMT_YUV420P, c->width, c->height);
264 fprintf(stderr, "Could not allocate temporary picture\n");
270 /* prepare a dummy image */
271 void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
278 for(y=0;y<height;y++) {
279 for(x=0;x<width;x++) {
280 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
285 for(y=0;y<height/2;y++) {
286 for(x=0;x<width/2;x++) {
287 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
288 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
293 void write_video_frame(AVFormatContext *oc, AVStream *st)
300 if (c->pix_fmt != PIX_FMT_YUV420P) {
301 /* as we only generate a YUV420P picture, we must convert it
302 to the codec pixel format if needed */
303 fill_yuv_image(tmp_picture, frame_count, c->width, c->height);
304 img_convert((AVPicture *)picture, c->pix_fmt,
305 (AVPicture *)tmp_picture, PIX_FMT_YUV420P,
306 c->width, c->height);
308 fill_yuv_image(picture, frame_count, c->width, c->height);
312 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
313 /* raw video case. The API will change slightly in the near
315 ret = av_write_frame(oc, st->index,
316 (uint8_t *)picture, sizeof(AVPicture));
318 /* encode the image */
319 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
321 /* write the compressed frame in the media file */
322 ret = av_write_frame(oc, st->index, video_outbuf, out_size);
325 fprintf(stderr, "Error while writing video frame\n");
331 void close_video(AVFormatContext *oc, AVStream *st)
333 avcodec_close(&st->codec);
334 av_free(picture->data[0]);
337 av_free(tmp_picture->data[0]);
338 av_free(tmp_picture);
340 av_free(video_outbuf);
343 /**************************************************************/
344 /* media file output */
346 int main(int argc, char **argv)
348 const char *filename;
351 AVStream *audio_st, *video_st;
352 double audio_pts, video_pts;
355 /* initialize libavcodec, and register all codecs and formats */
359 printf("usage: %s output_file\n"
360 "API example program to output a media file with libavformat.\n"
361 "The output format is automatically guessed according to the file extension.\n"
362 "Raw images can also be output by using '%%d' in the filename\n"
369 /* auto detect the output format from the name. default is
371 fmt = guess_format(NULL, filename, NULL);
373 printf("Could not deduce output format from file extension: using MPEG.\n");
374 fmt = guess_format("mpeg", NULL, NULL);
377 fprintf(stderr, "Could not find suitable output format\n");
381 /* allocate the output media context */
382 oc = av_mallocz(sizeof(AVFormatContext));
384 fprintf(stderr, "Memory error\n");
388 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
390 /* add the audio and video streams using the default format codecs
391 and initialize the codecs */
394 if (fmt->video_codec != CODEC_ID_NONE) {
395 video_st = add_video_stream(oc, fmt->video_codec);
397 if (fmt->audio_codec != CODEC_ID_NONE) {
398 audio_st = add_audio_stream(oc, fmt->audio_codec);
401 /* set the output parameters (must be done even if no
403 if (av_set_parameters(oc, NULL) < 0) {
404 fprintf(stderr, "Invalid output format parameters\n");
408 dump_format(oc, 0, filename, 1);
410 /* now that all the parameters are set, we can open the audio and
411 video codecs and allocate the necessary encode buffers */
413 open_video(oc, video_st);
415 open_audio(oc, audio_st);
417 /* open the output file, if needed */
418 if (!(fmt->flags & AVFMT_NOFILE)) {
419 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
420 fprintf(stderr, "Could not open '%s'\n", filename);
425 /* write the stream header, if any */
429 /* compute current audio and video time */
431 audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
436 video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
440 if ((!audio_st || audio_pts >= STREAM_DURATION) &&
441 (!video_st || video_pts >= STREAM_DURATION))
444 /* write interleaved audio and video frames */
445 if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
446 write_audio_frame(oc, audio_st);
448 write_video_frame(oc, video_st);
452 /* close each codec */
454 close_video(oc, video_st);
456 close_audio(oc, audio_st);
458 /* write the trailer, if any */
459 av_write_trailer(oc);
461 /* free the streams */
462 for(i = 0; i < oc->nb_streams; i++) {
463 av_freep(&oc->streams[i]);
466 if (!(fmt->flags & AVFMT_NOFILE)) {
467 /* close the output file */
471 /* free the stream */