removed unnecessary codec
[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
38float t, tincr;
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{
49 AVCodec *codec;
50 AVCodecContext *c;
51 AVStream *st;
52
53 st = av_new_stream(oc, 1);
54 if (!st) {
55 fprintf(stderr, "Could not alloc stream\n");
56 exit(1);
57 }
58
59 /* find the MP2 encoder */
60 codec = avcodec_find_encoder(codec_id);
61 if (!codec) {
62 fprintf(stderr, "codec not found\n");
63 exit(1);
64 }
65 c = &st->codec;
8de65d4f
FB
66 c->codec_type = CODEC_TYPE_AUDIO;
67
68 /* put sample parameters */
69 c->bit_rate = 64000;
70 c->sample_rate = 44100;
71 c->channels = 2;
72
73 /* open it */
74 if (avcodec_open(c, codec) < 0) {
75 fprintf(stderr, "could not open codec\n");
76 exit(1);
77 }
78
79 /* init signal generator */
80 t = 0;
81 tincr = 2 * M_PI * 440.0 / c->sample_rate;
82
83 audio_outbuf_size = 10000;
84 audio_outbuf = malloc(audio_outbuf_size);
85
86 /* ugly hack for PCM codecs (will be removed ASAP with new PCM
87 support to compute the input frame size in samples */
88 if (c->frame_size <= 1) {
89 audio_input_frame_size = audio_outbuf_size / c->channels;
90 switch(st->codec.codec_id) {
91 case CODEC_ID_PCM_S16LE:
92 case CODEC_ID_PCM_S16BE:
93 case CODEC_ID_PCM_U16LE:
94 case CODEC_ID_PCM_U16BE:
95 audio_input_frame_size >>= 1;
96 break;
97 default:
98 break;
99 }
100 } else {
101 audio_input_frame_size = c->frame_size;
102 }
103 samples = malloc(audio_input_frame_size * 2 * c->channels);
104
105 return st;
106}
107
108void write_audio_frame(AVFormatContext *oc, AVStream *st)
109{
110 int j, out_size;
111 AVCodecContext *c;
112
113
114 c = &st->codec;
115
116 for(j=0;j<audio_input_frame_size;j++) {
117 samples[2*j] = (int)(sin(t) * 10000);
118 samples[2*j+1] = samples[2*j];
119 t += tincr;
120 }
121
122 out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
123
124 /* write the compressed frame in the media file */
125 if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) {
126 fprintf(stderr, "Error while writing audio frame\n");
127 exit(1);
128 }
129}
130
131/**************************************************************/
132/* video output */
133
134AVFrame *picture;
135uint8_t *video_outbuf;
136int frame_count, video_outbuf_size;
137
138/* add a video output stream */
139AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
140{
141 AVCodec *codec;
142 AVCodecContext *c;
143 AVStream *st;
144 uint8_t *picture_buf;
145 int size;
146
147 st = av_new_stream(oc, 0);
148 if (!st) {
149 fprintf(stderr, "Could not alloc stream\n");
150 exit(1);
151 }
152
153 /* find the mpeg1 video encoder */
154 codec = avcodec_find_encoder(codec_id);
155 if (!codec) {
156 fprintf(stderr, "codec not found\n");
157 exit(1);
158 }
159
160 c = &st->codec;
8de65d4f
FB
161 c->codec_type = CODEC_TYPE_VIDEO;
162
163 /* put sample parameters */
164 c->bit_rate = 400000;
165 /* resolution must be a multiple of two */
166 c->width = 352;
167 c->height = 288;
168 /* frames per second */
169 c->frame_rate = 25;
170 c->frame_rate_base= 1;
171 c->gop_size = 12; /* emit one intra frame every twelve frames */
172
173 /* open it */
174 if (avcodec_open(c, codec) < 0) {
175 fprintf(stderr, "could not open codec\n");
176 exit(1);
177 }
178
179 /* alloc various buffers */
180 picture= avcodec_alloc_frame();
181 video_outbuf_size = 100000;
182 video_outbuf = malloc(video_outbuf_size);
183
184 size = c->width * c->height;
185 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
186
187 picture->data[0] = picture_buf;
188 picture->data[1] = picture->data[0] + size;
189 picture->data[2] = picture->data[1] + size / 4;
190 picture->linesize[0] = c->width;
191 picture->linesize[1] = c->width / 2;
192 picture->linesize[2] = c->width / 2;
193
194 return st;
195}
196
197void write_video_frame(AVFormatContext *oc, AVStream *st)
198{
199 int x, y, i, out_size;
200 AVCodecContext *c;
201
202 c = &st->codec;
203
204 /* prepare a dummy image */
205 /* Y */
206 i = frame_count++;
207 for(y=0;y<c->height;y++) {
208 for(x=0;x<c->width;x++) {
209 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
210 }
211 }
212
213 /* Cb and Cr */
214 for(y=0;y<c->height/2;y++) {
215 for(x=0;x<c->width/2;x++) {
216 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
217 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
218 }
219 }
220
221 /* encode the image */
222 out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
223
224 /* write the compressed frame in the media file */
225 if (av_write_frame(oc, st->index, video_outbuf, out_size) != 0) {
226 fprintf(stderr, "Error while writing video frame\n");
227 exit(1);
228 }
229}
230
231/**************************************************************/
232/* media file output */
233
234int main(int argc, char **argv)
235{
236 const char *filename;
237 AVOutputFormat *fmt;
238 AVFormatContext *oc;
239 AVStream *st, *audio_st, *video_st;
240 int i;
241 double audio_pts, video_pts;
242
243 /* initialize libavcodec, and register all codecs and formats */
244 av_register_all();
245
246 if (argc != 2) {
247 printf("usage: %s output_file\n"
248 "API example program for to output media file with libavformat\n"
249 "\n", argv[0]);
250 exit(1);
251 }
252
253 filename = argv[1];
254
255 /* auto detect the output format from the name. default is
256 mpeg. */
257 fmt = guess_format(NULL, filename, NULL);
258 if (!fmt) {
259 printf("Could not deduce output format from file extension: using MPEG.\n");
260 fmt = guess_format("mpeg", NULL, NULL);
261 }
262 if (!fmt) {
263 fprintf(stderr, "Could not find suitable output format\n");
264 exit(1);
265 }
266
267 /* allocate the output media context */
268 oc = av_mallocz(sizeof(AVFormatContext));
269 if (!oc) {
270 fprintf(stderr, "Memory error\n");
271 exit(1);
272 }
273 oc->oformat = fmt;
274
275 /* add the audio and video streams using the default format codecs
276 and initialize the codecs */
277 video_st = NULL;
278 audio_st = NULL;
279 if (fmt->video_codec != CODEC_ID_NONE) {
280 video_st = add_video_stream(oc, fmt->video_codec);
281 }
282 if (fmt->audio_codec != CODEC_ID_NONE) {
283 audio_st = add_audio_stream(oc, fmt->audio_codec);
284 }
285
286 dump_format(oc, 0, filename, 1);
287
288 /* open the output file, if needed */
289 if (!(fmt->flags & AVFMT_NOFILE)) {
290 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
291 fprintf(stderr, "Could not open '%s'\n", filename);
292 exit(1);
293 }
294 }
295
296 /* set the output parameters (must be done even if no parameters) */
297 av_set_parameters(oc, NULL);
298
299 /* write the stream header, if any */
300 av_write_header(oc);
301
302 for(;;) {
303 /* compute current audio and video time */
304 if (audio_st)
305 audio_pts = (double)audio_st->pts.val * oc->pts_num / oc->pts_den;
306 else
307 audio_pts = 0.0;
308
309 if (video_st)
310 video_pts = (double)video_st->pts.val * oc->pts_num / oc->pts_den;
311 else
312 video_pts = 0.0;
313
314 if ((!audio_st || audio_pts >= STREAM_DURATION) &&
315 (!video_st || video_pts >= STREAM_DURATION))
316 break;
317
318 /* write interleaved audio and video frames */
319 if (!video_st || (video_st && audio_pts < video_pts)) {
320 write_audio_frame(oc, audio_st);
321 } else {
322 write_video_frame(oc, video_st);
323 }
324 }
325
326 /* close each codec */
327 for(i = 0;i < oc->nb_streams; i++) {
328 st = oc->streams[i];
329 avcodec_close(&st->codec);
330 }
331
332 /* write the trailer, if any */
333 av_write_trailer(oc);
334
335 if (!(fmt->flags & AVFMT_NOFILE)) {
336 /* close the output file */
337 url_fclose(&oc->pb);
338 }
339
340 /* free the stream */
341 av_free(oc);
342
343 return 0;
344}