* Phase 1 for DV encoding support. Muxing/demuxing of DV streams is now
[libav.git] / output_example.c
CommitLineData
8de65d4f
FB
1/*
2 * Libavformat API example: Output a media file in any supported
3 * libavformat format. The default codecs are used.
4 *
5 * Copyright (c) 2003 Fabrice Bellard
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
25#include <stdlib.h>
26#include <stdio.h>
27#include <math.h>
28
29#include "avformat.h"
30
31/* 5 seconds stream duration */
32#define STREAM_DURATION 5.0
33
34
35/**************************************************************/
36/* audio output */
37
e70fcf07 38float t, tincr, tincr2;
8de65d4f
FB
39int16_t *samples;
40uint8_t *audio_outbuf;
41int audio_outbuf_size;
42int audio_input_frame_size;
43
44/*
45 * add an audio output stream
46 */
47AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
48{
8de65d4f
FB
49 AVCodecContext *c;
50 AVStream *st;
51
52 st = av_new_stream(oc, 1);
53 if (!st) {
54 fprintf(stderr, "Could not alloc stream\n");
55 exit(1);
56 }
57
8de65d4f 58 c = &st->codec;
e70fcf07 59 c->codec_id = codec_id;
8de65d4f
FB
60 c->codec_type = CODEC_TYPE_AUDIO;
61
62 /* put sample parameters */
63 c->bit_rate = 64000;
64 c->sample_rate = 44100;
65 c->channels = 2;
e70fcf07
FB
66 return st;
67}
68
69void open_audio(AVFormatContext *oc, AVStream *st)
70{
71 AVCodecContext *c;
72 AVCodec *codec;
73
74 c = &st->codec;
75
76 /* find the audio encoder */
77 codec = avcodec_find_encoder(c->codec_id);
78 if (!codec) {
79 fprintf(stderr, "codec not found\n");
80 exit(1);
81 }
8de65d4f
FB
82
83 /* open it */
84 if (avcodec_open(c, codec) < 0) {
85 fprintf(stderr, "could not open codec\n");
86 exit(1);
87 }
88
89 /* init signal generator */
90 t = 0;
e70fcf07
FB
91 tincr = 2 * M_PI * 110.0 / c->sample_rate;
92 /* increment frequency by 110 Hz per second */
93 tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
8de65d4f
FB
94
95 audio_outbuf_size = 10000;
96 audio_outbuf = malloc(audio_outbuf_size);
97
98 /* ugly hack for PCM codecs (will be removed ASAP with new PCM
99 support to compute the input frame size in samples */
100 if (c->frame_size <= 1) {
101 audio_input_frame_size = audio_outbuf_size / c->channels;
102 switch(st->codec.codec_id) {
103 case CODEC_ID_PCM_S16LE:
104 case CODEC_ID_PCM_S16BE:
105 case CODEC_ID_PCM_U16LE:
106 case CODEC_ID_PCM_U16BE:
107 audio_input_frame_size >>= 1;
108 break;
109 default:
110 break;
111 }
112 } else {
113 audio_input_frame_size = c->frame_size;
114 }
115 samples = malloc(audio_input_frame_size * 2 * c->channels);
8de65d4f
FB
116}
117
cb750e33
FB
118/* prepare a 16 bit dummy audio frame of 'frame_size' samples and
119 'nb_channels' channels */
120void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
121{
122 int j, i, v;
123 int16_t *q;
124
125 q = samples;
126 for(j=0;j<frame_size;j++) {
127 v = (int)(sin(t) * 10000);
128 for(i = 0; i < nb_channels; i++)
129 *q++ = v;
130 t += tincr;
131 tincr += tincr2;
132 }
133}
134
8de65d4f
FB
135void write_audio_frame(AVFormatContext *oc, AVStream *st)
136{
cb750e33 137 int out_size;
8de65d4f
FB
138 AVCodecContext *c;
139
140
141 c = &st->codec;
142
cb750e33
FB
143 get_audio_frame(samples, audio_input_frame_size, c->channels);
144
8de65d4f
FB
145 out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
146
147 /* write the compressed frame in the media file */
148 if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) {
149 fprintf(stderr, "Error while writing audio frame\n");
150 exit(1);
151 }
152}
153
e70fcf07
FB
154void close_audio(AVFormatContext *oc, AVStream *st)
155{
156 avcodec_close(&st->codec);
157
158 av_free(samples);
159 av_free(audio_outbuf);
160}
161
8de65d4f
FB
162/**************************************************************/
163/* video output */
164
e70fcf07 165AVFrame *picture, *tmp_picture;
8de65d4f
FB
166uint8_t *video_outbuf;
167int frame_count, video_outbuf_size;
168
169/* add a video output stream */
170AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
171{
8de65d4f
FB
172 AVCodecContext *c;
173 AVStream *st;
8de65d4f
FB
174
175 st = av_new_stream(oc, 0);
176 if (!st) {
177 fprintf(stderr, "Could not alloc stream\n");
178 exit(1);
179 }
180
8de65d4f 181 c = &st->codec;
e70fcf07 182 c->codec_id = codec_id;
8de65d4f
FB
183 c->codec_type = CODEC_TYPE_VIDEO;
184
185 /* put sample parameters */
186 c->bit_rate = 400000;
187 /* resolution must be a multiple of two */
188 c->width = 352;
189 c->height = 288;
190 /* frames per second */
191 c->frame_rate = 25;
192 c->frame_rate_base= 1;
193 c->gop_size = 12; /* emit one intra frame every twelve frames */
194
e70fcf07
FB
195 return st;
196}
197
198AVFrame *alloc_picture(int pix_fmt, int width, int height)
199{
200 AVFrame *picture;
201 uint8_t *picture_buf;
202 int size;
203
204 picture = avcodec_alloc_frame();
205 if (!picture)
206 return NULL;
207 size = avpicture_get_size(pix_fmt, width, height);
208 picture_buf = malloc(size);
209 if (!picture_buf) {
210 av_free(picture);
211 return NULL;
212 }
213 avpicture_fill((AVPicture *)picture, picture_buf,
214 pix_fmt, width, height);
215 return picture;
216}
217
218void open_video(AVFormatContext *oc, AVStream *st)
219{
220 AVCodec *codec;
221 AVCodecContext *c;
222
223 c = &st->codec;
224
225 /* find the video encoder */
226 codec = avcodec_find_encoder(c->codec_id);
227 if (!codec) {
228 fprintf(stderr, "codec not found\n");
229 exit(1);
230 }
231
232 /* open the codec */
8de65d4f
FB
233 if (avcodec_open(c, codec) < 0) {
234 fprintf(stderr, "could not open codec\n");
235 exit(1);
236 }
237
e70fcf07
FB
238 video_outbuf = NULL;
239 if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
240 /* allocate output buffer */
241 /* XXX: API change will be done */
242 video_outbuf_size = 200000;
243 video_outbuf = malloc(video_outbuf_size);
244 }
8de65d4f 245
e70fcf07
FB
246 /* allocate the encoded raw picture */
247 picture = alloc_picture(c->pix_fmt, c->width, c->height);
248 if (!picture) {
249 fprintf(stderr, "Could not allocate picture\n");
250 exit(1);
251 }
8de65d4f 252
e70fcf07
FB
253 /* if the output format is not YUV420P, then a temporary YUV420P
254 picture is needed too. It is then converted to the required
255 output format */
256 tmp_picture = NULL;
257 if (c->pix_fmt != PIX_FMT_YUV420P) {
258 tmp_picture = alloc_picture(PIX_FMT_YUV420P, c->width, c->height);
259 if (!tmp_picture) {
260 fprintf(stderr, "Could not allocate temporary picture\n");
261 exit(1);
262 }
263 }
264}
8de65d4f 265
e70fcf07
FB
266/* prepare a dummy image */
267void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
8de65d4f 268{
e70fcf07
FB
269 int x, y, i;
270
271 i = frame_index;
8de65d4f 272
8de65d4f 273 /* Y */
e70fcf07
FB
274 for(y=0;y<height;y++) {
275 for(x=0;x<width;x++) {
276 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
8de65d4f
FB
277 }
278 }
279
280 /* Cb and Cr */
e70fcf07
FB
281 for(y=0;y<height/2;y++) {
282 for(x=0;x<width/2;x++) {
283 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
284 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
8de65d4f
FB
285 }
286 }
e70fcf07 287}
8de65d4f 288
e70fcf07
FB
289void write_video_frame(AVFormatContext *oc, AVStream *st)
290{
291 int out_size, ret;
292 AVCodecContext *c;
8de65d4f 293
e70fcf07
FB
294 c = &st->codec;
295
296 if (c->pix_fmt != PIX_FMT_YUV420P) {
297 /* as we only generate a YUV420P picture, we must convert it
298 to the codec pixel format if needed */
299 fill_yuv_image(tmp_picture, frame_count, c->width, c->height);
300 img_convert((AVPicture *)picture, c->pix_fmt,
301 (AVPicture *)tmp_picture, PIX_FMT_YUV420P,
302 c->width, c->height);
303 } else {
304 fill_yuv_image(picture, frame_count, c->width, c->height);
305 }
306
307
308 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
309 /* raw video case. The API will change slightly in the near
310 futur for that */
311 ret = av_write_frame(oc, st->index,
312 (uint8_t *)picture, sizeof(AVPicture));
313 } else {
314 /* encode the image */
315 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
316
317 /* write the compressed frame in the media file */
318 ret = av_write_frame(oc, st->index, video_outbuf, out_size);
319 }
320 if (ret != 0) {
8de65d4f
FB
321 fprintf(stderr, "Error while writing video frame\n");
322 exit(1);
323 }
e70fcf07
FB
324 frame_count++;
325}
326
327void close_video(AVFormatContext *oc, AVStream *st)
328{
329 avcodec_close(&st->codec);
330 av_free(picture->data[0]);
331 av_free(picture);
332 if (tmp_picture) {
333 av_free(tmp_picture->data[0]);
334 av_free(tmp_picture);
335 }
336 av_free(video_outbuf);
8de65d4f
FB
337}
338
339/**************************************************************/
340/* media file output */
341
342int main(int argc, char **argv)
343{
344 const char *filename;
345 AVOutputFormat *fmt;
346 AVFormatContext *oc;
e70fcf07 347 AVStream *audio_st, *video_st;
8de65d4f 348 double audio_pts, video_pts;
e70fcf07
FB
349 int i;
350
8de65d4f
FB
351 /* initialize libavcodec, and register all codecs and formats */
352 av_register_all();
353
354 if (argc != 2) {
355 printf("usage: %s output_file\n"
e70fcf07
FB
356 "API example program to output a media file with libavformat.\n"
357 "The output format is automatically guessed according to the file extension.\n"
358 "Raw images can also be output by using '%%d' in the filename\n"
8de65d4f
FB
359 "\n", argv[0]);
360 exit(1);
361 }
362
363 filename = argv[1];
364
365 /* auto detect the output format from the name. default is
366 mpeg. */
367 fmt = guess_format(NULL, filename, NULL);
368 if (!fmt) {
369 printf("Could not deduce output format from file extension: using MPEG.\n");
370 fmt = guess_format("mpeg", NULL, NULL);
371 }
372 if (!fmt) {
373 fprintf(stderr, "Could not find suitable output format\n");
374 exit(1);
375 }
376
377 /* allocate the output media context */
378 oc = av_mallocz(sizeof(AVFormatContext));
379 if (!oc) {
380 fprintf(stderr, "Memory error\n");
381 exit(1);
382 }
383 oc->oformat = fmt;
e70fcf07 384 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
8de65d4f
FB
385
386 /* add the audio and video streams using the default format codecs
387 and initialize the codecs */
388 video_st = NULL;
389 audio_st = NULL;
390 if (fmt->video_codec != CODEC_ID_NONE) {
391 video_st = add_video_stream(oc, fmt->video_codec);
392 }
393 if (fmt->audio_codec != CODEC_ID_NONE) {
394 audio_st = add_audio_stream(oc, fmt->audio_codec);
395 }
396
e70fcf07
FB
397 /* set the output parameters (must be done even if no
398 parameters). */
399 if (av_set_parameters(oc, NULL) < 0) {
400 fprintf(stderr, "Invalid output format parameters\n");
401 exit(1);
402 }
403
8de65d4f
FB
404 dump_format(oc, 0, filename, 1);
405
e70fcf07
FB
406 /* now that all the parameters are set, we can open the audio and
407 video codecs and allocate the necessary encode buffers */
408 if (video_st)
409 open_video(oc, video_st);
410 if (audio_st)
411 open_audio(oc, audio_st);
412
8de65d4f
FB
413 /* open the output file, if needed */
414 if (!(fmt->flags & AVFMT_NOFILE)) {
415 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
416 fprintf(stderr, "Could not open '%s'\n", filename);
417 exit(1);
418 }
419 }
420
8de65d4f
FB
421 /* write the stream header, if any */
422 av_write_header(oc);
423
424 for(;;) {
425 /* compute current audio and video time */
426 if (audio_st)
427 audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
428 else
429 audio_pts = 0.0;
430
431 if (video_st)
432 video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
433 else
434 video_pts = 0.0;
435
436 if ((!audio_st || audio_pts >= STREAM_DURATION) &&
437 (!video_st || video_pts >= STREAM_DURATION))
438 break;
439
440 /* write interleaved audio and video frames */
e70fcf07 441 if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
8de65d4f
FB
442 write_audio_frame(oc, audio_st);
443 } else {
444 write_video_frame(oc, video_st);
445 }
446 }
447
448 /* close each codec */
e70fcf07
FB
449 if (video_st)
450 close_video(oc, video_st);
451 if (audio_st)
452 close_audio(oc, audio_st);
8de65d4f
FB
453
454 /* write the trailer, if any */
455 av_write_trailer(oc);
456
e70fcf07
FB
457 /* free the streams */
458 for(i = 0; i < oc->nb_streams; i++) {
459 av_freep(&oc->streams[i]);
460 }
461
8de65d4f
FB
462 if (!(fmt->flags & AVFMT_NOFILE)) {
463 /* close the output file */
464 url_fclose(&oc->pb);
465 }
466
467 /* free the stream */
468 av_free(oc);
469
470 return 0;
471}