42 #define STREAM_DURATION 5.0
43 #define STREAM_FRAME_RATE 25
44 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
45 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
68 fprintf(stderr,
"codec not found\n");
74 fprintf(stderr,
"Could not alloc stream\n");
101 fprintf(stderr,
"could not open codec\n");
129 v = (int)(sin(
t) * 10000);
163 fprintf(stderr,
"Error while writing audio frame\n");
192 fprintf(stderr,
"codec not found\n");
198 fprintf(stderr,
"Could not alloc stream\n");
250 pix_fmt, width, height);
262 fprintf(stderr,
"could not open codec\n");
269 fprintf(stderr,
"Could not allocate picture\n");
280 fprintf(stderr,
"Could not allocate temporary picture\n");
295 for (y = 0; y <
height; y++)
296 for (x = 0; x <
width; x++)
297 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
300 for (y = 0; y < height / 2; y++) {
301 for (x = 0; x < width / 2; x++) {
302 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
303 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
324 if (img_convert_ctx ==
NULL) {
330 if (img_convert_ctx ==
NULL) {
332 "Cannot initialize the conversion context\n");
364 if (!ret && got_packet && pkt.
size) {
382 fprintf(stderr,
"Error while writing video frame\n");
402 int main(
int argc,
char **argv)
404 const char *filename;
408 double audio_pts, video_pts;
415 printf(
"usage: %s output_file\n"
416 "API example program to output a media file with libavformat.\n"
417 "The output format is automatically guessed according to the file extension.\n"
418 "Raw images can also be output by using '%%d' in the filename\n"
428 printf(
"Could not deduce output format from file extension: using MPEG.\n");
432 fprintf(stderr,
"Could not find suitable output format\n");
439 fprintf(stderr,
"Memory error\n");
468 fprintf(stderr,
"Could not open '%s'\n", filename);
494 if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
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
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
This structure describes decoded (raw) audio or video data.
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.
#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.
int avpicture_fill(AVPicture *picture, uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
Fill in the AVPicture fields.
static AVStream * add_video_stream(AVFormatContext *oc, enum AVCodecID codec_id)
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 index
stream index in AVFormatContext
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
#define AVIO_FLAG_WRITE
write-only
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.
four components are given, that's all.
static void write_video_frame(AVFormatContext *oc, AVStream *st)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
Fill audio frame data and linesize.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
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.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
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.
external api for the swscale stuff
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
struct AVOutputFormat * oformat
The output container format.
static void open_audio(AVFormatContext *oc, AVStream *st)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static void close_audio(AVFormatContext *oc, AVStream *st)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
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...
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 AVStream * add_audio_stream(AVFormatContext *oc, enum AVCodecID codec_id)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
int capabilities
Codec capabilities.
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int bit_rate
the average bitrate
char filename[1024]
input or output filename
static AVFrame * tmp_picture
int width
picture width / height.
int main(int argc, char **argv)
static void close_video(AVFormatContext *oc, AVStream *st)
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
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...
int mb_decision
macroblock decision mode
enum AVPixelFormat pix_fmt
preferred ID for MPEG-1/2 video decoding
if(ac->has_optimized_func)
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
int frame_size
Number of samples per channel in an audio frame.
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.
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...
static AVFrame * alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void open_video(AVFormatContext *oc, AVStream *st)
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)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static int audio_input_frame_size
struct AVFrac pts
encoding: pts generation when outputting stream
int channels
number of audio channels
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
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
AVPixelFormat
Pixel format.
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
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.