37 #include <libavutil/opt.h>
38 #include <libavutil/mathematics.h>
39 #include <libavformat/avformat.h>
40 #include <libswscale/swscale.h>
41 #include <libswresample/swresample.h>
44 #define STREAM_DURATION 200.0
45 #define STREAM_FRAME_RATE 25
46 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
47 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
61 fprintf(stderr,
"Could not find encoder for '%s'\n",
68 fprintf(stderr,
"Could not allocate stream\n");
74 switch ((*codec)->type) {
146 fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
162 fprintf(stderr,
"Could not allocate source samples\n");
170 fprintf(stderr,
"Could not allocate resampler context\n");
183 if ((ret =
swr_init(swr_ctx)) < 0) {
184 fprintf(stderr,
"Failed to initialize the resampling context\n");
196 fprintf(stderr,
"Could not allocate destination samples\n");
212 v = (int)(sin(
t) * 10000);
225 int got_packet,
ret, dst_nb_samples;
253 fprintf(stderr,
"Error while converting\n");
267 fprintf(stderr,
"Error encoding audio frame: %s\n",
av_err2str(ret));
279 fprintf(stderr,
"Error while writing audio frame: %s\n",
308 fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
315 fprintf(stderr,
"Could not allocate video frame\n");
322 fprintf(stderr,
"Could not allocate picture: %s\n",
av_err2str(ret));
332 fprintf(stderr,
"Could not allocate temporary picture: %s\n",
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;
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;
384 "Could not initialize the conversion context\n");
416 fprintf(stderr,
"Error encoding video frame: %s\n",
av_err2str(ret));
421 if (!ret && got_packet && pkt.
size) {
431 fprintf(stderr,
"Error while writing video frame: %s\n",
av_err2str(ret));
448 int main(
int argc,
char **argv)
450 const char *filename;
454 AVCodec *audio_codec, *video_codec;
455 double audio_time, video_time;
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"
477 printf(
"Could not deduce output format from file extension: using MPEG.\n");
510 fprintf(stderr,
"Could not open '%s': %s\n", filename,
519 fprintf(stderr,
"Error occurred when opening output file: %s\n",
536 if (!video_st || (video_st && audio_st && audio_time < video_time)) {
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a samples buffer for nb_samples samples, and fill data pointers and linesize accordingly...
static void write_video_frame(AVFormatContext *oc, AVStream *st)
This structure describes decoded (raw) audio or video data.
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
#define CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding) av_const
Rescale a 64-bit integer with specified rounding.
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
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...
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
AVCodecContext * codec
Codec context associated with this stream.
int index
stream index in AVFormatContext
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Allocate a data pointers array, samples buffer for nb_samples samples, and fill data pointers and lin...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
struct SwrContext * swr_alloc(void)
Allocate SwrContext.
int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
Fill AVFrame audio data and linesize pointers.
enum AVSampleFormat sample_fmt
audio sample format
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int id
Format-specific stream ID.
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst...
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
static double av_q2d(AVRational a)
Convert rational to double.
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count)
Convert audio.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
static const uint8_t frame_size[4]
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
#define av_err2str(errnum)
static void close_audio(AVFormatContext *oc, AVStream *st)
AVCodecID
Identify the syntax and semantics of the bitstream.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static int src_samples_linesize
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int capabilities
Codec capabilities.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
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.
int flags
A combination of AV_PKT_FLAG values.
unsigned int nb_streams
A list of all streams in the file.
int bit_rate
the average bitrate
int width
picture width / height.
static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
static int src_nb_samples
int mb_decision
macroblock decision mode
preferred ID for MPEG-1/2 video decoding
static int max_dst_nb_samples
struct SwrContext * swr_ctx
int frame_size
Number of samples per channel in an audio frame.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
int sample_rate
samples per second
main external API structure.
static AVStream * add_stream(AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
static uint8_t ** src_samples_data
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.
AVIOContext * pb
I/O context.
BYTE int const BYTE int int int height
int avpicture_alloc(AVPicture *picture, enum AVPixelFormat pix_fmt, int width, int height)
Allocate memory for the pixels of a picture and setup the AVPicture fields for it.
static AVPicture src_picture
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.
static void fill_yuv_image(AVPicture *pict, int frame_index, int width, int height)
#define STREAM_FRAME_RATE
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
uint8_t ** dst_samples_data
struct AVOutputFormat * oformat
struct AVFrac pts
encoding: pts generation when outputting stream
int channels
number of audio channels
static AVPicture dst_picture
void avcodec_free_frame(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
int main(int argc, char **argv)
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)
This structure stores compressed data.
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
int nb_samples
number of audio samples (per channel) described by this frame
int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
static void close_video(AVFormatContext *oc, AVStream *st)