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. The default
28 * codecs are used.
29 * @example muxing.c
30 */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36
45
46 #define STREAM_DURATION 10.0
47 #define STREAM_FRAME_RATE 25 /* 25 images/s */
48 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
49
50 #define SCALE_FLAGS SWS_BICUBIC
51
52 // a wrapper around a single output AVStream
55
56 /* pts of the next frame that will be generated */
59
62
64
68
70 {
72
73 printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
78 }
79
81 {
82 /* rescale output packet timestamp values from codec to stream timebase */
85
86 /* Write the compressed frame to the media file. */
89 }
90
91 /* Add an output stream. */
95 {
97 int i;
98
99 /* find the encoder */
101 if (!(*codec)) {
102 fprintf(stderr, "Could not find encoder for '%s'\n",
104 exit(1);
105 }
106
109 fprintf(stderr, "Could not allocate stream\n");
110 exit(1);
111 }
114
115 switch ((*codec)->type) {
121 if ((*codec)->supported_samplerates) {
122 c->
sample_rate = (*codec)->supported_samplerates[0];
123 for (i = 0; (*codec)->supported_samplerates[i]; i++) {
124 if ((*codec)->supported_samplerates[i] == 44100)
126 }
127 }
130 if ((*codec)->channel_layouts) {
132 for (i = 0; (*codec)->channel_layouts[i]; i++) {
135 }
136 }
139 break;
140
143
145 /* Resolution must be a multiple of two. */
148 /* timebase: This is the fundamental unit of time (in seconds) in terms
149 * of which frame timestamps are represented. For fixed-fps content,
150 * timebase should be 1/framerate and timestamp increments should be
151 * identical to 1. */
154
155 c->
gop_size = 12;
/* emit one intra frame every twelve frames at most */
158 /* just for testing, we also add B frames */
160 }
162 /* Needed to avoid using macroblocks in which some coeffs overflow.
163 * This does not happen with normal video, it just happens here as
164 * the motion of the chroma plane does not match the luma plane. */
166 }
167 break;
168
169 default:
170 break;
171 }
172
173 /* Some formats want stream headers to be separate. */
176 }
177
178 /**************************************************************/
179 /* audio output */
180
182 uint64_t channel_layout,
184 {
186 int ret;
187
188 if (!frame) {
189 fprintf(stderr, "Error allocating an audio frame\n");
190 exit(1);
191 }
192
193 frame->
format = sample_fmt;
197
198 if (nb_samples) {
200 if (ret < 0) {
201 fprintf(stderr, "Error allocating an audio buffer\n");
202 exit(1);
203 }
204 }
205
207 }
208
210 {
212 int nb_samples;
213 int ret;
215
217
218 /* open it */
222 if (ret < 0) {
223 fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
224 exit(1);
225 }
226
227 /* init signal generator */
230 /* increment frequency by 110 Hz per second */
232
234 nb_samples = 10000;
235 else
237
242
243 /* create resampler context */
246 fprintf(stderr, "Could not allocate resampler context\n");
247 exit(1);
248 }
249
250 /* set options */
257
258 /* initialize the resampling context */
260 fprintf(stderr, "Failed to initialize the resampling context\n");
261 exit(1);
262 }
263 }
264
265 /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
266 * 'nb_channels' channels. */
268 {
270 int j, i, v;
271 int16_t *q = (int16_t*)frame->
data[0];
272
273 /* check if we want to generate more frames */
277
279 v = (int)(sin(ost->t) * 10000);
280 for (i = 0; i < ost->st->codec->channels; i++)
281 *q++ = v;
282 ost->t += ost->tincr;
283 ost->tincr += ost->tincr2;
284 }
285
288
290 }
291
292 /*
293 * encode one audio frame and send it to the muxer
294 * return 1 when encoding is finished, 0 otherwise
295 */
297 {
301 int ret;
302 int got_packet;
303 int dst_nb_samples;
304
307
309
310 if (frame) {
311 /* convert samples from native format to destination codec format, using the resampler */
312 /* compute destination number of samples */
316
317 /* when we pass a frame to the encoder, it may keep a reference to it
318 * internally;
319 * make sure we do not overwrite it here
320 */
322 if (ret < 0)
323 exit(1);
324
325 /* convert to destination format */
329 if (ret < 0) {
330 fprintf(stderr, "Error while converting\n");
331 exit(1);
332 }
334
337 }
338
340 if (ret < 0) {
341 fprintf(stderr,
"Error encoding audio frame: %s\n",
av_err2str(ret));
342 exit(1);
343 }
344
345 if (got_packet) {
347 if (ret < 0) {
348 fprintf(stderr, "Error while writing audio frame: %s\n",
350 exit(1);
351 }
352 }
353
354 return (
frame || got_packet) ? 0 : 1;
355 }
356
357 /**************************************************************/
358 /* video output */
359
361 {
363 int ret;
364
366 if (!picture)
368
372
373 /* allocate the buffers for the frame data */
375 if (ret < 0) {
376 fprintf(stderr, "Could not allocate frame data.\n");
377 exit(1);
378 }
379
380 return picture;
381 }
382
384 {
385 int ret;
388
390
391 /* open the codec */
394 if (ret < 0) {
395 fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
396 exit(1);
397 }
398
399 /* allocate and init a re-usable frame */
402 fprintf(stderr, "Could not allocate video frame\n");
403 exit(1);
404 }
405
406 /* If the output format is not YUV420P, then a temporary YUV420P
407 * picture is needed too. It is then converted to the required
408 * output format. */
413 fprintf(stderr, "Could not allocate temporary picture\n");
414 exit(1);
415 }
416 }
417 }
418
419 /* Prepare a dummy image. */
422 {
423 int x, y, i, ret;
424
425 /* when we pass a frame to the encoder, it may keep a reference to it
426 * internally;
427 * make sure we do not overwrite it here
428 */
430 if (ret < 0)
431 exit(1);
432
433 i = frame_index;
434
435 /* Y */
436 for (y = 0; y <
height; y++)
437 for (x = 0; x <
width; x++)
438 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
439
440 /* Cb and Cr */
441 for (y = 0; y < height / 2; y++) {
442 for (x = 0; x < width / 2; x++) {
443 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
444 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
445 }
446 }
447 }
448
450 {
452
453 /* check if we want to generate more frames */
457
459 /* as we only generate a YUV420P picture, we must convert it
460 * to the codec pixel format if needed */
461 if (!ost->sws_ctx) {
467 if (!ost->sws_ctx) {
468 fprintf(stderr,
469 "Could not initialize the conversion context\n");
470 exit(1);
471 }
472 }
475 (
const uint8_t *
const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
476 0,
c->height, ost->frame->data, ost->frame->linesize);
477 } else {
479 }
480
481 ost->frame->pts = ost->next_pts++;
482
483 return ost->frame;
484 }
485
486 /*
487 * encode one video frame and send it to the muxer
488 * return 1 when encoding is finished, 0 otherwise
489 */
491 {
492 int ret;
495 int got_packet = 0;
497
499
501
503
504 /* encode the image */
506 if (ret < 0) {
507 fprintf(stderr,
"Error encoding video frame: %s\n",
av_err2str(ret));
508 exit(1);
509 }
510
511 if (got_packet) {
513 } else {
514 ret = 0;
515 }
516
517 if (ret < 0) {
518 fprintf(stderr,
"Error while writing video frame: %s\n",
av_err2str(ret));
519 exit(1);
520 }
521
522 return (frame || got_packet) ? 0 : 1;
523 }
524
526 {
532 }
533
534 /**************************************************************/
535 /* media file output */
536
537 int main(
int argc,
char **argv)
538 {
540 const char *filename;
543 AVCodec *audio_codec, *video_codec;
544 int ret;
545 int have_video = 0, have_audio = 0;
546 int encode_video = 0, encode_audio = 0;
548
549 /* Initialize libavcodec, and register all codecs and formats. */
551
552 if (argc < 2) {
553 printf("usage: %s output_file\n"
554 "API example program to output a media file with libavformat.\n"
555 "This program generates a synthetic audio and video stream, encodes and\n"
556 "muxes them into a file named output_file.\n"
557 "The output format is automatically guessed according to the file extension.\n"
558 "Raw images can also be output by using '%%d' in the filename.\n"
559 "\n", argv[0]);
560 return 1;
561 }
562
563 filename = argv[1];
564 if (argc > 3 && !strcmp(argv[2], "-flags")) {
566 }
567
568 /* allocate the output media context */
570 if (!oc) {
571 printf("Could not deduce output format from file extension: using MPEG.\n");
573 }
574 if (!oc)
575 return 1;
576
578
579 /* Add the audio and video streams using the default format codecs
580 * and initialize the codecs. */
583 have_video = 1;
584 encode_video = 1;
585 }
588 have_audio = 1;
589 encode_audio = 1;
590 }
591
592 /* Now that all the parameters are set, we can open the audio and
593 * video codecs and allocate the necessary encode buffers. */
594 if (have_video)
596
597 if (have_audio)
599
601
602 /* open the output file, if needed */
605 if (ret < 0) {
606 fprintf(stderr, "Could not open '%s': %s\n", filename,
608 return 1;
609 }
610 }
611
612 /* Write the stream header, if any. */
614 if (ret < 0) {
615 fprintf(stderr, "Error occurred when opening output file: %s\n",
617 return 1;
618 }
619
620 while (encode_video || encode_audio) {
621 /* select the stream to encode */
622 if (encode_video &&
626 } else {
628 }
629 }
630
631 /* Write the trailer, if any. The trailer must be written before you
632 * close the CodecContexts open when you wrote the header; otherwise
633 * av_write_trailer() may try to use memory that was freed on
634 * av_codec_close(). */
636
637 /* Close each codec. */
638 if (have_video)
640 if (have_audio)
642
644 /* Close the output file. */
646
647 /* free the stream */
649
650 return 0;
651 }
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
const struct AVCodec * codec
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
int main(int argc, char **argv)
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
int64_t bit_rate
the average bitrate
static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
static AVFormatContext * fmt_ctx
int index
stream index in AVFormatContext
#define AVIO_FLAG_WRITE
write-only
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
#define AV_CH_LAYOUT_STEREO
static void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static void close_stream(AVFormatContext *oc, OutputStream *ost)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
enum AVSampleFormat sample_fmt
audio sample format
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
timestamp utils, mostly useful for debugging/logging purposes
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int id
Format-specific stream ID.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
A list of all streams in the file.
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
struct SwrContext * swr_ctx
struct AVOutputFormat * oformat
The output container format.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate, streams, container, programs, metadata, side data, codec and time base.
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
libswresample public header
AVCodecID
Identify the syntax and semantics of the bitstream.
int width
width and height of the video frame
void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
static AVFrame * alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
The libswresample context.
int capabilities
Codec capabilities.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int flags
AV_CODEC_FLAG_*.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
simple assert() macros that are a bit more flexible than ISO C assert().
int64_t swr_get_delay(struct SwrContext *s, int64_t base)
Gets the delay the next input sample will experience relative to the next output sample.
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
uint64_t channel_layout
Audio channel layout.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
AVCodecContext * codec
Codec context associated with this stream.
uint64_t channel_layout
Channel layout of the audio data.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
audio channel layout utility functions
static AVFrame * alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples)
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
int width
picture width / height.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
int mb_decision
macroblock decision mode
preferred ID for MPEG-1/2 video decoding
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
int frame_size
Number of samples per channel in an audio frame.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
AVIOContext * pb
I/O context.
main external API structure.
static AVFrame * get_video_frame(OutputStream *ost)
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
BYTE int const BYTE int int int height
int sample_rate
Sample rate of the audio data.
rational number numerator/denominator
#define STREAM_FRAME_RATE
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count, const uint8_t *in_arg[SWR_CH_MAX], int in_count)
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int channels
number of audio channels
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
AVPixelFormat
Pixel format.
This structure stores compressed data.
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
struct SwsContext * sws_ctx
static AVFrame * get_audio_frame(OutputStream *ost)
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.