1 /*
2 * Copyright (c) 2003 Fabrice Bellard
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file
25 * libavformat API example.
26 *
27 * Output a media file in any supported libavformat format.
28 * The default codecs are used.
29 * @example doc/examples/muxing.c
30 */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36
42
43 /* 5 seconds stream duration */
44 #define STREAM_DURATION 200.0
45 #define STREAM_FRAME_RATE 25 /* 25 images/s */
46 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
47 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
48
50
51 /* Add an output stream. */
54 {
57
58 /* find the encoder */
60 if (!(*codec)) {
61 fprintf(stderr, "Could not find encoder for '%s'\n",
63 exit(1);
64 }
65
67 if (!st) {
68 fprintf(stderr, "Could not allocate stream\n");
69 exit(1);
70 }
73
74 switch ((*codec)->type) {
80 break;
81
84
86 /* Resolution must be a multiple of two. */
89 /* timebase: This is the fundamental unit of time (in seconds) in terms
90 * of which frame timestamps are represented. For fixed-fps content,
91 * timebase should be 1/framerate and timestamp increments should be
92 * identical to 1. */
95 c->
gop_size = 12;
/* emit one intra frame every twelve frames at most */
98 /* just for testing, we also add B frames */
100 }
102 /* Needed to avoid using macroblocks in which some coeffs overflow.
103 * This does not happen with normal video, it just happens here as
104 * the motion of the chroma plane does not match the luma plane. */
106 }
107 break;
108
109 default:
110 break;
111 }
112
113 /* Some formats want stream headers to be separate. */
116
117 return st;
118 }
119
120 /**************************************************************/
121 /* audio output */
122
124
128
133
135
137 {
140
142
143 /* open it */
145 if (ret < 0) {
146 fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
147 exit(1);
148 }
149
150 /* init signal generator */
153 /* increment frequency by 110 Hz per second */
155
158
161 if (ret < 0) {
162 fprintf(stderr, "Could not allocate source samples\n");
163 exit(1);
164 }
165
166 /* create resampler context */
169 if (!swr_ctx) {
170 fprintf(stderr, "Could not allocate resampler context\n");
171 exit(1);
172 }
173
174 /* set options */
181
182 /* initialize the resampling context */
183 if ((ret =
swr_init(swr_ctx)) < 0) {
184 fprintf(stderr, "Failed to initialize the resampling context\n");
185 exit(1);
186 }
187 }
188
189 /* compute the number of converted samples: buffering is avoided
190 * ensuring that the output buffer will contain at least all the
191 * converted input samples */
195 if (ret < 0) {
196 fprintf(stderr, "Could not allocate destination samples\n");
197 exit(1);
198 }
201 }
202
203 /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
204 * 'nb_channels' channels. */
206 {
208 int16_t *q;
209
210 q = samples;
212 v = (int)(sin(
t) * 10000);
214 *q++ = v;
217 }
218 }
219
221 {
225 int got_packet,
ret, dst_nb_samples;
226
229
231
232 /* convert samples from native format to destination codec format, using the resampler */
233 if (swr_ctx) {
234 /* compute destination number of samples */
241 if (ret < 0)
242 exit(1);
246 }
247
248 /* convert to destination format */
252 if (ret < 0) {
253 fprintf(stderr, "Error while converting\n");
254 exit(1);
255 }
256 } else {
259 }
260
264
266 if (ret < 0) {
267 fprintf(stderr,
"Error encoding audio frame: %s\n",
av_err2str(ret));
268 exit(1);
269 }
270
271 if (!got_packet)
272 return;
273
275
276 /* Write the compressed frame to the media file. */
278 if (ret != 0) {
279 fprintf(stderr, "Error while writing audio frame: %s\n",
281 exit(1);
282 }
284 }
285
287 {
291 }
292
293 /**************************************************************/
294 /* video output */
295
299
301 {
304
305 /* open the codec */
307 if (ret < 0) {
308 fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
309 exit(1);
310 }
311
312 /* allocate and init a re-usable frame */
314 if (!frame) {
315 fprintf(stderr, "Could not allocate video frame\n");
316 exit(1);
317 }
318
319 /* Allocate the encoded raw picture. */
321 if (ret < 0) {
322 fprintf(stderr,
"Could not allocate picture: %s\n",
av_err2str(ret));
323 exit(1);
324 }
325
326 /* If the output format is not YUV420P, then a temporary YUV420P
327 * picture is needed too. It is then converted to the required
328 * output format. */
331 if (ret < 0) {
332 fprintf(stderr, "Could not allocate temporary picture: %s\n",
334 exit(1);
335 }
336 }
337
338 /* copy data and linesize picture pointers to frame */
340 }
341
342 /* Prepare a dummy image. */
345 {
347
348 i = frame_index;
349
350 /* Y */
351 for (y = 0; y <
height; y++)
352 for (x = 0; x <
width; x++)
353 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
354
355 /* Cb and Cr */
356 for (y = 0; y < height / 2; y++) {
357 for (x = 0; x < width / 2; x++) {
358 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
359 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
360 }
361 }
362 }
363
365 {
369
371 /* No more frames to compress. The codec has a latency of a few
372 * frames if using B-frames, so we get the last frames by
373 * passing the same picture again. */
374 } else {
376 /* as we only generate a YUV420P picture, we must convert it
377 * to the codec pixel format if needed */
378 if (!sws_ctx) {
382 if (!sws_ctx) {
383 fprintf(stderr,
384 "Could not initialize the conversion context\n");
385 exit(1);
386 }
387 }
392 } else {
394 }
395 }
396
398 /* Raw video case - directly store the picture in the packet */
401
406
408 } else {
410 int got_packet;
412
413 /* encode the image */
415 if (ret < 0) {
416 fprintf(stderr,
"Error encoding video frame: %s\n",
av_err2str(ret));
417 exit(1);
418 }
419 /* If size is zero, it means the image was buffered. */
420
421 if (!ret && got_packet && pkt.
size) {
423
424 /* Write the compressed frame to the media file. */
426 } else {
427 ret = 0;
428 }
429 }
430 if (ret != 0) {
431 fprintf(stderr,
"Error while writing video frame: %s\n",
av_err2str(ret));
432 exit(1);
433 }
435 }
436
438 {
443 }
444
445 /**************************************************************/
446 /* media file output */
447
448 int main(
int argc,
char **argv)
449 {
450 const char *filename;
454 AVCodec *audio_codec, *video_codec;
455 double audio_time, video_time;
457
458 /* Initialize libavcodec, and register all codecs and formats. */
460
461 if (argc != 2) {
462 printf("usage: %s output_file\n"
463 "API example program to output a media file with libavformat.\n"
464 "This program generates a synthetic audio and video stream, encodes and\n"
465 "muxes them into a file named output_file.\n"
466 "The output format is automatically guessed according to the file extension.\n"
467 "Raw images can also be output by using '%%d' in the filename.\n"
468 "\n", argv[0]);
469 return 1;
470 }
471
472 filename = argv[1];
473
474 /* allocate the output media context */
476 if (!oc) {
477 printf("Could not deduce output format from file extension: using MPEG.\n");
479 }
480 if (!oc) {
481 return 1;
482 }
484
485 /* Add the audio and video streams using the default format codecs
486 * and initialize the codecs. */
487 video_st = NULL;
488 audio_st = NULL;
489
492 }
495 }
496
497 /* Now that all the parameters are set, we can open the audio and
498 * video codecs and allocate the necessary encode buffers. */
499 if (video_st)
501 if (audio_st)
503
505
506 /* open the output file, if needed */
509 if (ret < 0) {
510 fprintf(stderr, "Could not open '%s': %s\n", filename,
512 return 1;
513 }
514 }
515
516 /* Write the stream header, if any. */
518 if (ret < 0) {
519 fprintf(stderr, "Error occurred when opening output file: %s\n",
521 return 1;
522 }
523
524 if (frame)
526 for (;;) {
527 /* Compute current audio and video time. */
530
533 break;
534
535 /* write interleaved audio and video frames */
536 if (!video_st || (video_st && audio_st && audio_time < video_time)) {
538 } else {
541 }
542 }
543
544 /* Write the trailer, if any. The trailer must be written before you
545 * close the CodecContexts open when you wrote the header; otherwise
546 * av_write_trailer() may try to use memory that was freed on
547 * av_codec_close(). */
549
550 /* Close each codec. */
551 if (video_st)
553 if (audio_st)
555
557 /* Close the output file. */
559
560 /* free the stream */
562
563 return 0;
564 }