37 #include "libavutil/channel_layout.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/opt.h"
40 #include "libavformat/avformat.h"
41 #include "libavresample/avresample.h"
42 #include "libswscale/swscale.h"
45 #define STREAM_DURATION 5.0
46 #define STREAM_FRAME_RATE 25
47 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
48 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
50 #define SCALE_FLAGS SWS_BICUBIC
84 fprintf(stderr,
"codec not found\n");
90 fprintf(stderr,
"Could not alloc stream\n");
116 fprintf(stderr,
"Error allocating the resampling context\n");
129 fprintf(stderr,
"Error opening the resampling context\n");
135 uint64_t channel_layout,
136 int sample_rate,
int nb_samples)
142 fprintf(stderr,
"Error allocating an audio frame\n");
154 fprintf(stderr,
"Error allocating an audio buffer\n");
171 fprintf(stderr,
"could not open codec\n");
198 int16_t *q = (int16_t*)frame->
data[0];
207 v = (int)(sin(ost->
t) * 10000);
236 fprintf(stderr,
"Error while writing audio frame\n");
241 return (frame || got_packet) ? 0 : 1;
255 got_output |= !!frame;
263 fprintf(stderr,
"Error feeding audio data to the resampler\n");
292 fprintf(stderr,
"Error while resampling\n");
295 fprintf(stderr,
"Too few samples returned from lavr\n");
323 fprintf(stderr,
"codec not found\n");
329 fprintf(stderr,
"Could not alloc stream\n");
380 fprintf(stderr,
"Could not allocate frame data.\n");
395 fprintf(stderr,
"could not open codec\n");
402 fprintf(stderr,
"Could not allocate picture\n");
413 fprintf(stderr,
"Could not allocate temporary picture\n");
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;
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;
469 "Cannot initialize the conversion context\n");
524 fprintf(stderr,
"Error encoding a video frame\n");
537 fprintf(stderr,
"Error while writing video frame\n");
541 return (frame || got_packet) ? 0 : 1;
556 int main(
int argc,
char **argv)
559 const char *filename;
562 int have_video = 0, have_audio = 0;
563 int encode_video = 0, encode_audio = 0;
569 printf(
"usage: %s output_file\n"
570 "API example program to output a media file with libavformat.\n"
571 "The output format is automatically guessed according to the file extension.\n"
572 "Raw images can also be output by using '%%d' in the filename\n"
582 printf(
"Could not deduce output format from file extension: using MPEG.\n");
586 fprintf(stderr,
"Could not find suitable output format\n");
593 fprintf(stderr,
"Memory error\n");
624 fprintf(stderr,
"Could not open '%s'\n", filename);
632 while (encode_video || encode_audio) {
636 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
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.
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.
#define CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
static void close_stream(AVFormatContext *oc, OutputStream *ost)
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
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...
int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
Read samples from the output FIFO.
AVCodecContext * codec
Codec context associated with this stream.
int index
stream index in AVFormatContext
uint8_t ** extended_data
pointers to the data planes/channels.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
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
four components are given, that's all.
void avresample_free(AVAudioResampleContext **avr)
Free AVAudioResampleContext and associated AVOption values.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
enum AVSampleFormat sample_fmt
audio sample format
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
AVAudioResampleContext * avr
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.
static AVFrame * alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
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...
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 int encode_audio_frame(AVFormatContext *oc, OutputStream *ost, AVFrame *frame)
char filename[1024]
input or output filename
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int capabilities
Codec capabilities.
AVAudioResampleContext * avresample_alloc_context(void)
Allocate AVAudioResampleContext and set options.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
AVPixelFormat
Pixel format.
int flags
A combination of AV_PKT_FLAG values.
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.
uint64_t channel_layout
Channel layout of the audio data.
int bit_rate
the average bitrate
static AVFrame * get_video_frame(OutputStream *ost)
static int process_audio_stream(AVFormatContext *oc, OutputStream *ost)
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
int width
picture width / height.
static AVFrame * get_audio_frame(OutputStream *ost)
static void add_video_stream(OutputStream *ost, AVFormatContext *oc, enum AVCodecID codec_id)
int main(int argc, char **argv)
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
int mb_decision
macroblock decision mode
enum AVPixelFormat pix_fmt
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
preferred ID for MPEG-1/2 video decoding
if(ac->has_optimized_func)
static void add_audio_stream(OutputStream *ost, AVFormatContext *oc, enum AVCodecID codec_id)
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.
int avresample_available(AVAudioResampleContext *avr)
Return the number of available samples in the output FIFO.
AVSampleFormat
Audio Sample Formats.
int sample_rate
samples per second
main external API structure.
int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples)
Provide the upper bound on the number of samples the configured conversion would output.
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...
AVIOContext * pb
I/O context.
int sample_rate
Sample rate of the audio data.
rational number numerator/denominator
static AVFrame * alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
int avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t **input, int in_plane_size, int in_samples)
Convert input samples and write them to the output FIFO.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
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 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.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
static void open_video(AVFormatContext *oc, OutputStream *ost)
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
static void open_audio(AVFormatContext *oc, OutputStream *ost)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
const struct AVCodec * codec
struct AVOutputFormat * oformat
The output container format.
int channels
number of audio channels
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
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...
#define STREAM_FRAME_RATE
This structure stores compressed data.
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
int avresample_open(AVAudioResampleContext *avr)
Initialize AVAudioResampleContext.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
struct SwsContext * sws_ctx