43 #define INBUF_SIZE 4096
44 #define AUDIO_INBUF_SIZE 20480
45 #define AUDIO_REFILL_THRESH 4096
64 int best_samplerate = 0;
71 best_samplerate =
FFMAX(*p, best_samplerate);
74 return best_samplerate;
81 uint64_t best_ch_layout = 0;
82 int best_nb_channels = 0;
91 if (nb_channels > best_nb_channels) {
97 return best_ch_layout;
109 int i, j, k, ret, got_output;
115 printf(
"Encode audio file %s\n", filename);
120 fprintf(stderr,
"Codec not found\n");
126 fprintf(stderr,
"Could not allocate audio codec context\n");
136 fprintf(stderr,
"Encoder does not support sample format %s",
148 fprintf(stderr,
"Could not open codec\n");
152 f = fopen(filename,
"wb");
154 fprintf(stderr,
"Could not open %s\n", filename);
161 fprintf(stderr,
"Could not allocate audio frame\n");
173 if (buffer_size < 0) {
174 fprintf(stderr,
"Could not get sample buffer size\n");
179 fprintf(stderr,
"Could not allocate %d bytes for samples buffer\n",
185 (
const uint8_t*)samples, buffer_size, 0);
187 fprintf(stderr,
"Could not setup audio frame\n");
194 for (i = 0; i < 200; i++) {
200 samples[2*j] = (
int)(sin(t) * 10000);
203 samples[2*j + k] = samples[2*j];
209 fprintf(stderr,
"Error encoding audio frame\n");
219 for (got_output = 1; got_output; i++) {
222 fprintf(stderr,
"Error encoding frame\n");
253 printf(
"Decode audio file %s to %s\n", filename, outfilename);
258 fprintf(stderr,
"Codec not found\n");
264 fprintf(stderr,
"Could not allocate audio codec context\n");
270 fprintf(stderr,
"Could not open codec\n");
274 f = fopen(filename,
"rb");
276 fprintf(stderr,
"Could not open %s\n", filename);
279 outfile = fopen(outfilename,
"wb");
289 while (avpkt.
size > 0) {
293 if (!decoded_frame) {
295 fprintf(stderr,
"Could not allocate audio frame\n");
302 fprintf(stderr,
"Error while decoding\n");
310 fprintf(stderr,
"Failed to calculate data size\n");
315 fwrite(decoded_frame->
data[ch] + data_size*i, 1, data_size, outfile);
326 memmove(inbuf, avpkt.
data, avpkt.
size);
328 len = fread(avpkt.
data + avpkt.
size, 1,
349 int i, ret, x, y, got_output;
353 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
355 printf(
"Encode video file %s\n", filename);
360 fprintf(stderr,
"Codec not found\n");
366 fprintf(stderr,
"Could not allocate video codec context\n");
392 fprintf(stderr,
"Could not open codec\n");
396 f = fopen(filename,
"wb");
398 fprintf(stderr,
"Could not open %s\n", filename);
404 fprintf(stderr,
"Could not allocate video frame\n");
416 fprintf(stderr,
"Could not allocate raw picture buffer\n");
421 for (i = 0; i < 25; i++) {
429 for (y = 0; y < c->
height; y++) {
430 for (x = 0; x < c->
width; x++) {
431 frame->
data[0][y * frame->
linesize[0] + x] = x + y + i * 3;
436 for (y = 0; y < c->
height/2; y++) {
437 for (x = 0; x < c->
width/2; x++) {
438 frame->
data[1][y * frame->
linesize[1] + x] = 128 + y + i * 2;
439 frame->
data[2][y * frame->
linesize[2] + x] = 64 + x + i * 5;
448 fprintf(stderr,
"Error encoding frame\n");
453 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
460 for (got_output = 1; got_output; i++) {
465 fprintf(stderr,
"Error encoding frame\n");
470 printf(
"Write frame %3d (size=%5d)\n", i, pkt.
size);
477 fwrite(endcode, 1,
sizeof(endcode), f);
496 f = fopen(filename,
"w");
497 fprintf(f,
"P5\n%d %d\n%d\n", xsize, ysize, 255);
498 for (i = 0; i < ysize; i++)
499 fwrite(buf + i * wrap, 1, xsize, f);
511 fprintf(stderr,
"Error while decoding frame %d\n", *frame_count);
515 printf(
"Saving %sframe %3d\n", last ?
"last " :
"", *frame_count);
519 snprintf(buf,
sizeof(buf), outfilename, *frame_count);
546 printf(
"Decode video file %s to %s\n", filename, outfilename);
551 fprintf(stderr,
"Codec not found\n");
557 fprintf(stderr,
"Could not allocate video codec context\n");
570 fprintf(stderr,
"Could not open codec\n");
574 f = fopen(filename,
"rb");
576 fprintf(stderr,
"Could not open %s\n", filename);
582 fprintf(stderr,
"Could not allocate video frame\n");
608 while (avpkt.
size > 0)
627 int main(
int argc,
char **argv)
629 const char *output_type;
635 printf(
"usage: %s output_type\n"
636 "API example program to decode/encode a media stream with libavcodec.\n"
637 "This program generates a synthetic stream and encodes it to a file\n"
638 "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
639 "The encoded stream is then decoded and written to a raw data output.\n"
640 "output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
644 output_type = argv[1];
646 if (!strcmp(output_type,
"h264")) {
648 }
else if (!strcmp(output_type,
"mp2")) {
651 }
else if (!strcmp(output_type,
"mpg")) {
655 fprintf(stderr,
"Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
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.
int64_t bit_rate
the average bitrate
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
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...
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
attribute_deprecated int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, const AVPacket *avpkt)
Decode the audio frame of size avpkt->size from avpkt->data into frame.
static void video_decode_example(const char *outfilename, const char *filename)
#define AV_CH_LAYOUT_STEREO
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align)
Fill AVFrame audio data and linesize pointers.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
static int select_sample_rate(AVCodec *codec)
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
int width
width and height of the video frame
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int capabilities
Codec capabilities.
int flags
AV_CODEC_FLAG_*.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
#define AUDIO_REFILL_THRESH
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
audio channel layout utility functions
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int width
picture width / height.
static void audio_encode_example(const char *filename)
static void video_encode_example(const char *filename, int 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.
static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
Libavcodec external API header.
attribute_deprecated int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
AVSampleFormat
Audio sample formats.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
int main(int argc, char **argv)
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Rational number (pair of numerator and denominator).
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
static void audio_decode_example(const char *outfilename, const char *filename)
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define AV_CODEC_CAP_TRUNCATED
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
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)
common internal and external API header
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int channels
number of audio channels
static int select_channel_layout(AVCodec *codec)
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0 ...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
This structure stores compressed data.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
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.