#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define STREAM_DURATION 10.0
#define STREAM_FRAME_RATE 25
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
#define SCALE_FLAGS SWS_BICUBIC
{
printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
}
{
int ret;
if (ret < 0) {
fprintf(stderr, "Error sending a frame to the encoder: %s\n",
exit(1);
}
while (ret >= 0) {
break;
else if (ret < 0) {
fprintf(stderr,
"Error encoding a frame: %s\n",
av_err2str(ret));
exit(1);
}
if (ret < 0) {
fprintf(stderr,
"Error while writing output packet: %s\n",
av_err2str(ret));
exit(1);
}
}
}
{
int i;
if (!(*codec)) {
fprintf(stderr, "Could not find encoder for '%s'\n",
exit(1);
}
fprintf(stderr, "Could not allocate AVPacket\n");
exit(1);
}
fprintf(stderr, "Could not allocate stream\n");
exit(1);
}
if (!c) {
fprintf(stderr, "Could not alloc an encoding context\n");
exit(1);
}
switch ((*codec)->type) {
if ((*codec)->supported_samplerates) {
for (i = 0; (*codec)->supported_samplerates[i]; i++) {
if ((*codec)->supported_samplerates[i] == 44100)
}
}
break;
}
}
break;
default:
break;
}
}
int sample_rate, int nb_samples)
{
int ret;
fprintf(stderr, "Error allocating an audio frame\n");
exit(1);
}
if (nb_samples) {
if (ret < 0) {
fprintf(stderr, "Error allocating an audio buffer\n");
exit(1);
}
}
}
{
int nb_samples;
int ret;
if (ret < 0) {
fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
exit(1);
}
nb_samples = 10000;
else
if (ret < 0) {
fprintf(stderr, "Could not copy the stream parameters\n");
exit(1);
}
fprintf(stderr, "Could not allocate resampler context\n");
exit(1);
}
fprintf(stderr, "Failed to initialize the resampling context\n");
exit(1);
}
}
{
int j, i, v;
return NULL;
v = (int)(sin(
ost->t) * 10000);
for (i = 0; i <
ost->enc->ch_layout.nb_channels; i++)
*q++ = v;
}
}
{
int ret;
int dst_nb_samples;
if (ret < 0)
exit(1);
ost->frame->data, dst_nb_samples,
if (ret < 0) {
fprintf(stderr, "Error while converting\n");
exit(1);
}
ost->samples_count += dst_nb_samples;
}
}
{
int ret;
if (!picture)
return NULL;
if (ret < 0) {
fprintf(stderr, "Could not allocate frame data.\n");
exit(1);
}
return picture;
}
{
int ret;
if (ret < 0) {
fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
exit(1);
}
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
fprintf(stderr, "Could not allocate temporary picture\n");
exit(1);
}
}
if (ret < 0) {
fprintf(stderr, "Could not copy the stream parameters\n");
exit(1);
}
}
{
int x, y, i;
i = frame_index;
for (x = 0; x <
width; x++)
for (y = 0; y <
height / 2; y++) {
for (x = 0; x <
width / 2; x++) {
}
}
}
{
return NULL;
exit(1);
fprintf(stderr,
"Could not initialize the conversion context\n");
exit(1);
}
}
} else {
}
ost->frame->pts =
ost->next_pts++;
}
{
}
{
}
int main(
int argc,
char **argv)
{
const char *filename;
const AVCodec *audio_codec, *video_codec;
int ret;
int have_video = 0, have_audio = 0;
int encode_video = 0, encode_audio = 0;
int i;
if (argc < 2) {
printf("usage: %s output_file\n"
"API example program to output a media file with libavformat.\n"
"This program generates a synthetic audio and video stream, encodes and\n"
"muxes them into a file named output_file.\n"
"The output format is automatically guessed according to the file extension.\n"
"Raw images can also be output by using '%%d' in the filename.\n"
"\n", argv[0]);
return 1;
}
filename = argv[1];
for (i = 2; i+1 < argc; i+=2) {
if (!strcmp(argv[i], "-flags") || !strcmp(argv[i], "-fflags"))
}
if (!oc) {
printf("Could not deduce output format from file extension: using MPEG.\n");
}
if (!oc)
return 1;
have_video = 1;
encode_video = 1;
}
have_audio = 1;
encode_audio = 1;
}
if (have_video)
if (have_audio)
if (ret < 0) {
fprintf(stderr, "Could not open '%s': %s\n", filename,
return 1;
}
}
if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file: %s\n",
return 1;
}
while (encode_video || encode_audio) {
if (encode_video &&
audio_st.next_pts, audio_st.enc->time_base) <= 0)) {
} else {
}
}
if (have_video)
if (have_audio)
return 0;
}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
#define AVIO_FLAG_WRITE
write-only
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
audio channel layout utility functions
static AVFormatContext * fmt_ctx
static enum AVPixelFormat pix_fmt
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE
Audio encoder supports receiving a different number of samples in each call.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
AVCodecID
Identify the syntax and semantics of the bitstream.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
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.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
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,...
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values.
struct AVDictionary AVDictionary
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
@ AV_SAMPLE_FMT_S16
signed 16 bits
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.
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
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.
struct SwrContext * swr_alloc(void)
Allocate SwrContext.
struct SwrContext SwrContext
The libswresample context.
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.
void swr_free(struct SwrContext **s)
Free the given SwrContext and set the pointer to NULL.
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count)
Convert audio.
int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *layout, int search_flags)
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
static void open_video(AVFormatContext *oc, const AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
static AVFrame * get_video_frame(OutputStream *ost)
static void add_stream(OutputStream *ost, AVFormatContext *oc, const AVCodec **codec, enum AVCodecID codec_id)
int main(int argc, char **argv)
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
static void open_audio(AVFormatContext *oc, const AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
static int write_frame(AVFormatContext *fmt_ctx, AVCodecContext *c, AVStream *st, AVFrame *frame, AVPacket *pkt)
static AVFrame * alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
static void close_stream(AVFormatContext *oc, OutputStream *ost)
static AVFrame * get_audio_frame(OutputStream *ost)
static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
static AVFrame * alloc_audio_frame(enum AVSampleFormat sample_fmt, const AVChannelLayout *channel_layout, int sample_rate, int nb_samples)
#define STREAM_FRAME_RATE
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
An AVChannelLayout holds information about the channel layout of audio data.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
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 mb_decision
macroblock decision mode
int64_t bit_rate
the average bitrate
const struct AVCodec * codec
int sample_rate
samples per second
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
int frame_size
Number of samples per channel in an audio frame.
int capabilities
Codec capabilities.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
AVIOContext * pb
I/O context.
const struct AVOutputFormat * oformat
The output container format.
AVStream ** streams
A list of all streams in the file.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
int sample_rate
Sample rate of the audio data.
AVChannelLayout ch_layout
Channel layout of the audio data.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Rational number (pair of numerator and denominator).
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int id
Format-specific stream ID.
int index
stream index in AVFormatContext
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
struct SwrContext * swr_ctx
struct SwsContext * sws_ctx
libswresample public header
timestamp utils, mostly useful for debugging/logging purposes
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...