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");
193 fprintf(stderr,
"codec not found\n");
199 fprintf(stderr,
"Could not alloc stream\n");
251 pix_fmt, width, height);
263 fprintf(stderr,
"could not open codec\n");
282 fprintf(stderr,
"Could not allocate picture\n");
293 fprintf(stderr,
"Could not allocate temporary picture\n");
308 for (y = 0; y <
height; y++)
309 for (x = 0; x <
width; x++)
310 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
313 for (y = 0; y < height / 2; y++) {
314 for (x = 0; x < width / 2; x++) {
315 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
316 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
337 if (img_convert_ctx ==
NULL) {
343 if (img_convert_ctx ==
NULL) {
345 "Cannot initialize the conversion context\n");
394 fprintf(stderr,
"Error while writing video frame\n");
415 int main(
int argc,
char **argv)
417 const char *filename;
421 double audio_pts, video_pts;
428 printf(
"usage: %s output_file\n"
429 "API example program to output a media file with libavformat.\n"
430 "The output format is automatically guessed according to the file extension.\n"
431 "Raw images can also be output by using '%%d' in the filename\n"
441 printf(
"Could not deduce output format from file extension: using MPEG.\n");
445 fprintf(stderr,
"Could not find suitable output format\n");
452 fprintf(stderr,
"Memory error\n");
481 fprintf(stderr,
"Could not open '%s'\n", filename);
507 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.
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_audio_stream(AVFormatContext *oc, enum AVCodecID codec_id)
AVFrame * coded_frame
the picture in the bitstream
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 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...
int index
stream index in AVFormatContext
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.
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
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
static AVFrame * tmp_picture
static uint8_t * video_outbuf
static void close_video(AVFormatContext *oc, AVStream *st)
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
external api for the swscale stuff
#define AVFMT_GLOBALHEADER
static void write_video_frame(AVFormatContext *oc, AVStream *st)
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
struct AVOutputFormat * oformat
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...
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.
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 void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
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.
struct AVPicture AVPicture
four components are given, that's all.
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
unsigned int nb_streams
A list of all streams in the file.
int bit_rate
the average bitrate
char filename[1024]
input or output filename
int width
picture width / height.
static int audio_input_frame_size
static void close_audio(AVFormatContext *oc, AVStream *st)
#define STREAM_FRAME_RATE
static AVFrame * alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
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
static int video_outbuf_size
int main(int argc, char **argv)
preferred ID for MPEG-1/2 video decoding
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.
static void open_audio(AVFormatContext *oc, AVStream *st)
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
AVIOContext * pb
I/O context.
static void open_video(AVFormatContext *oc, AVStream *st)
main external API structure.
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
static AVStream * add_video_stream(AVFormatContext *oc, enum AVCodecID codec_id)
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define CODEC_FLAG_GLOBAL_HEADER
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
#define CODEC_CAP_VARIABLE_FRAME_SIZE
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.
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...
int key_frame
1 -> keyframe, 0-> not
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.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
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...
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)