44 #define STREAM_DURATION 5.0
45 #define STREAM_FRAME_RATE 25
46 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
47 #define STREAM_PIX_FMT PIX_FMT_YUV420P
68 st = av_new_stream(oc, 1);
70 fprintf(stderr,
"Could not alloc stream\n");
101 fprintf(stderr,
"codec not found\n");
106 if (avcodec_open(c, codec) < 0) {
107 fprintf(stderr,
"could not open codec\n");
148 for(j=0;j<frame_size;j++) {
149 v = (int)(sin(
t) * 10000);
177 fprintf(stderr,
"Error while writing audio frame\n");
205 fprintf(stderr,
"Could not alloc stream\n");
246 uint8_t *picture_buf;
259 pix_fmt, width, height);
273 fprintf(stderr,
"codec not found\n");
278 if (avcodec_open(c, codec) < 0) {
279 fprintf(stderr,
"could not open codec\n");
298 fprintf(stderr,
"Could not allocate picture\n");
309 fprintf(stderr,
"Could not allocate temporary picture\n");
324 for(x=0;x<
width;x++) {
325 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
330 for(y=0;y<height/2;y++) {
331 for(x=0;x<width/2;x++) {
332 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
333 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
354 if (img_convert_ctx ==
NULL) {
360 if (img_convert_ctx ==
NULL) {
361 fprintf(stderr,
"Cannot initialize the conversion context\n");
382 pkt.
data= (uint8_t *)picture;
409 fprintf(stderr,
"Error while writing video frame\n");
430 int main(
int argc,
char **argv)
432 const char *filename;
436 double audio_pts, video_pts;
443 printf(
"usage: %s output_file\n"
444 "API example program to output a media file with libavformat.\n"
445 "The output format is automatically guessed according to the file extension.\n"
446 "Raw images can also be output by using '%%d' in the filename\n"
457 printf(
"Could not deduce output format from file extension: using MPEG.\n");
461 fprintf(stderr,
"Could not find suitable output format\n");
468 fprintf(stderr,
"Memory error\n");
487 if (av_set_parameters(oc,
NULL) < 0) {
488 fprintf(stderr,
"Invalid output format parameters\n");
504 fprintf(stderr,
"Could not open '%s'\n", filename);
529 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.
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
static AVFrame * alloc_picture(enum PixelFormat pix_fmt, int width, int height)
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
AVCodec * avcodec_find_encoder(enum CodecID id)
Find a registered encoder with a matching 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
#define AVIO_FLAG_WRITE
write-only
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
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 ...
static uint8_t * audio_outbuf
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
enum AVSampleFormat sample_fmt
audio sample format
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
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
static void write_video_frame(AVFormatContext *oc, AVStream *st)
int avpicture_get_size(enum PixelFormat 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...
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
struct AVOutputFormat * oformat
preferred ID for MPEG-1/2 video decoding
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)
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.
struct SwsContext * sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
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
unsigned int nb_streams
A list of all streams in the file.
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
int bit_rate
the average bitrate
char filename[1024]
input or output filename
static AVStream * add_audio_stream(AVFormatContext *oc, enum CodecID codec_id)
int width
picture width / height.
static int audio_input_frame_size
static void close_audio(AVFormatContext *oc, AVStream *st)
#define STREAM_FRAME_RATE
static AVStream * add_video_stream(AVFormatContext *oc, enum CodecID codec_id)
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
static int video_outbuf_size
int main(int argc, char **argv)
int frame_size
Samples per packet, initialized when calling 'init'.
enum AVMediaType codec_type
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.
static void open_video(AVFormatContext *oc, AVStream *st)
main external API structure.
struct AVPicture AVPicture
four components are given, that's all.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int avpicture_fill(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt, int width, int height)
Fill in the AVPicture fields.
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict)
Encode a video frame from pict into buf.
static int audio_outbuf_size
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
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
CodecID
Identify the syntax and semantics of the bitstream.
struct AVFrac pts
encoding: pts generation when outputting stream
int channels
number of audio channels
int key_frame
1 -> keyframe, 0-> not
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...
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
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.