decoding_encoding.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001 Fabrice Bellard
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 /**
24  * @file
25  * libavcodec API use example.
26  *
27  * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...),
28  * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the
29  * format handling
30  * @example doc/examples/decoding_encoding.c
31  */
32 
33 #include <math.h>
34 
35 #include <libavutil/opt.h>
36 #include <libavcodec/avcodec.h>
38 #include <libavutil/common.h>
39 #include <libavutil/imgutils.h>
40 #include <libavutil/mathematics.h>
41 #include <libavutil/samplefmt.h>
42 
43 #define INBUF_SIZE 4096
44 #define AUDIO_INBUF_SIZE 20480
45 #define AUDIO_REFILL_THRESH 4096
46 
47 /* check that a given sample format is supported by the encoder */
48 static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
49 {
50  const enum AVSampleFormat *p = codec->sample_fmts;
51 
52  while (*p != AV_SAMPLE_FMT_NONE) {
53  if (*p == sample_fmt)
54  return 1;
55  p++;
56  }
57  return 0;
58 }
59 
60 /* just pick the highest supported samplerate */
61 static int select_sample_rate(AVCodec *codec)
62 {
63  const int *p;
64  int best_samplerate = 0;
65 
66  if (!codec->supported_samplerates)
67  return 44100;
68 
69  p = codec->supported_samplerates;
70  while (*p) {
71  best_samplerate = FFMAX(*p, best_samplerate);
72  p++;
73  }
74  return best_samplerate;
75 }
76 
77 /* select layout with the highest channel count */
78 static int select_channel_layout(AVCodec *codec)
79 {
80  const uint64_t *p;
81  uint64_t best_ch_layout = 0;
82  int best_nb_channels = 0;
83 
84  if (!codec->channel_layouts)
85  return AV_CH_LAYOUT_STEREO;
86 
87  p = codec->channel_layouts;
88  while (*p) {
90 
91  if (nb_channels > best_nb_channels) {
92  best_ch_layout = *p;
93  best_nb_channels = nb_channels;
94  }
95  p++;
96  }
97  return best_ch_layout;
98 }
99 
100 /*
101  * Audio encoding example
102  */
103 static void audio_encode_example(const char *filename)
104 {
105  AVCodec *codec;
107  AVFrame *frame;
108  AVPacket pkt;
109  int i, j, k, ret, got_output;
110  int buffer_size;
111  FILE *f;
112  uint16_t *samples;
113  float t, tincr;
114 
115  printf("Encode audio file %s\n", filename);
116 
117  /* find the MP2 encoder */
119  if (!codec) {
120  fprintf(stderr, "Codec not found\n");
121  exit(1);
122  }
123 
124  c = avcodec_alloc_context3(codec);
125  if (!c) {
126  fprintf(stderr, "Could not allocate audio codec context\n");
127  exit(1);
128  }
129 
130  /* put sample parameters */
131  c->bit_rate = 64000;
132 
133  /* check that the encoder supports s16 pcm input */
135  if (!check_sample_fmt(codec, c->sample_fmt)) {
136  fprintf(stderr, "Encoder does not support sample format %s",
138  exit(1);
139  }
140 
141  /* select other audio parameters supported by the encoder */
142  c->sample_rate = select_sample_rate(codec);
145 
146  /* open it */
147  if (avcodec_open2(c, codec, NULL) < 0) {
148  fprintf(stderr, "Could not open codec\n");
149  exit(1);
150  }
151 
152  f = fopen(filename, "wb");
153  if (!f) {
154  fprintf(stderr, "Could not open %s\n", filename);
155  exit(1);
156  }
157 
158  /* frame containing input raw audio */
159  frame = avcodec_alloc_frame();
160  if (!frame) {
161  fprintf(stderr, "Could not allocate audio frame\n");
162  exit(1);
163  }
164 
165  frame->nb_samples = c->frame_size;
166  frame->format = c->sample_fmt;
167  frame->channel_layout = c->channel_layout;
168 
169  /* the codec gives us the frame size, in samples,
170  * we calculate the size of the samples buffer in bytes */
171  buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
172  c->sample_fmt, 0);
173  samples = av_malloc(buffer_size);
174  if (!samples) {
175  fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
176  buffer_size);
177  exit(1);
178  }
179  /* setup the data pointers in the AVFrame */
180  ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
181  (const uint8_t*)samples, buffer_size, 0);
182  if (ret < 0) {
183  fprintf(stderr, "Could not setup audio frame\n");
184  exit(1);
185  }
186 
187  /* encode a single tone sound */
188  t = 0;
189  tincr = 2 * M_PI * 440.0 / c->sample_rate;
190  for(i=0;i<200;i++) {
191  av_init_packet(&pkt);
192  pkt.data = NULL; // packet data will be allocated by the encoder
193  pkt.size = 0;
194 
195  for (j = 0; j < c->frame_size; j++) {
196  samples[2*j] = (int)(sin(t) * 10000);
197 
198  for (k = 1; k < c->channels; k++)
199  samples[2*j + k] = samples[2*j];
200  t += tincr;
201  }
202  /* encode the samples */
203  ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
204  if (ret < 0) {
205  fprintf(stderr, "Error encoding audio frame\n");
206  exit(1);
207  }
208  if (got_output) {
209  fwrite(pkt.data, 1, pkt.size, f);
210  av_free_packet(&pkt);
211  }
212  }
213 
214  /* get the delayed frames */
215  for (got_output = 1; got_output; i++) {
216  ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
217  if (ret < 0) {
218  fprintf(stderr, "Error encoding frame\n");
219  exit(1);
220  }
221 
222  if (got_output) {
223  fwrite(pkt.data, 1, pkt.size, f);
224  av_free_packet(&pkt);
225  }
226  }
227  fclose(f);
228 
229  av_freep(&samples);
230  avcodec_free_frame(&frame);
231  avcodec_close(c);
232  av_free(c);
233 }
234 
235 /*
236  * Audio decoding.
237  */
238 static void audio_decode_example(const char *outfilename, const char *filename)
239 {
240  AVCodec *codec;
242  int len;
243  FILE *f, *outfile;
245  AVPacket avpkt;
246  AVFrame *decoded_frame = NULL;
247 
248  av_init_packet(&avpkt);
249 
250  printf("Decode audio file %s to %s\n", filename, outfilename);
251 
252  /* find the mpeg audio decoder */
254  if (!codec) {
255  fprintf(stderr, "Codec not found\n");
256  exit(1);
257  }
258 
259  c = avcodec_alloc_context3(codec);
260  if (!c) {
261  fprintf(stderr, "Could not allocate audio codec context\n");
262  exit(1);
263  }
264 
265  /* open it */
266  if (avcodec_open2(c, codec, NULL) < 0) {
267  fprintf(stderr, "Could not open codec\n");
268  exit(1);
269  }
270 
271  f = fopen(filename, "rb");
272  if (!f) {
273  fprintf(stderr, "Could not open %s\n", filename);
274  exit(1);
275  }
276  outfile = fopen(outfilename, "wb");
277  if (!outfile) {
278  av_free(c);
279  exit(1);
280  }
281 
282  /* decode until eof */
283  avpkt.data = inbuf;
284  avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
285 
286  while (avpkt.size > 0) {
287  int got_frame = 0;
288 
289  if (!decoded_frame) {
290  if (!(decoded_frame = avcodec_alloc_frame())) {
291  fprintf(stderr, "Could not allocate audio frame\n");
292  exit(1);
293  }
294  } else
295  avcodec_get_frame_defaults(decoded_frame);
296 
297  len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
298  if (len < 0) {
299  fprintf(stderr, "Error while decoding\n");
300  exit(1);
301  }
302  if (got_frame) {
303  /* if a frame has been decoded, output it */
304  int data_size = av_samples_get_buffer_size(NULL, c->channels,
305  decoded_frame->nb_samples,
306  c->sample_fmt, 1);
307  fwrite(decoded_frame->data[0], 1, data_size, outfile);
308  }
309  avpkt.size -= len;
310  avpkt.data += len;
311  avpkt.dts =
312  avpkt.pts = AV_NOPTS_VALUE;
313  if (avpkt.size < AUDIO_REFILL_THRESH) {
314  /* Refill the input buffer, to avoid trying to decode
315  * incomplete frames. Instead of this, one could also use
316  * a parser, or use a proper container format through
317  * libavformat. */
318  memmove(inbuf, avpkt.data, avpkt.size);
319  avpkt.data = inbuf;
320  len = fread(avpkt.data + avpkt.size, 1,
321  AUDIO_INBUF_SIZE - avpkt.size, f);
322  if (len > 0)
323  avpkt.size += len;
324  }
325  }
326 
327  fclose(outfile);
328  fclose(f);
329 
330  avcodec_close(c);
331  av_free(c);
332  avcodec_free_frame(&decoded_frame);
333 }
334 
335 /*
336  * Video encoding example
337  */
338 static void video_encode_example(const char *filename, int codec_id)
339 {
340  AVCodec *codec;
342  int i, ret, x, y, got_output;
343  FILE *f;
344  AVFrame *frame;
345  AVPacket pkt;
346  uint8_t endcode[] = { 0, 0, 1, 0xb7 };
347 
348  printf("Encode video file %s\n", filename);
349 
350  /* find the mpeg1 video encoder */
351  codec = avcodec_find_encoder(codec_id);
352  if (!codec) {
353  fprintf(stderr, "Codec not found\n");
354  exit(1);
355  }
356 
357  c = avcodec_alloc_context3(codec);
358  if (!c) {
359  fprintf(stderr, "Could not allocate video codec context\n");
360  exit(1);
361  }
362 
363  /* put sample parameters */
364  c->bit_rate = 400000;
365  /* resolution must be a multiple of two */
366  c->width = 352;
367  c->height = 288;
368  /* frames per second */
369  c->time_base= (AVRational){1,25};
370  c->gop_size = 10; /* emit one intra frame every ten frames */
371  c->max_b_frames=1;
373 
374  if(codec_id == AV_CODEC_ID_H264)
375  av_opt_set(c->priv_data, "preset", "slow", 0);
376 
377  /* open it */
378  if (avcodec_open2(c, codec, NULL) < 0) {
379  fprintf(stderr, "Could not open codec\n");
380  exit(1);
381  }
382 
383  f = fopen(filename, "wb");
384  if (!f) {
385  fprintf(stderr, "Could not open %s\n", filename);
386  exit(1);
387  }
388 
389  frame = avcodec_alloc_frame();
390  if (!frame) {
391  fprintf(stderr, "Could not allocate video frame\n");
392  exit(1);
393  }
394  frame->format = c->pix_fmt;
395  frame->width = c->width;
396  frame->height = c->height;
397 
398  /* the image can be allocated by any means and av_image_alloc() is
399  * just the most convenient way if av_malloc() is to be used */
400  ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
401  c->pix_fmt, 32);
402  if (ret < 0) {
403  fprintf(stderr, "Could not allocate raw picture buffer\n");
404  exit(1);
405  }
406 
407  /* encode 1 second of video */
408  for(i=0;i<25;i++) {
409  av_init_packet(&pkt);
410  pkt.data = NULL; // packet data will be allocated by the encoder
411  pkt.size = 0;
412 
413  fflush(stdout);
414  /* prepare a dummy image */
415  /* Y */
416  for(y=0;y<c->height;y++) {
417  for(x=0;x<c->width;x++) {
418  frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
419  }
420  }
421 
422  /* Cb and Cr */
423  for(y=0;y<c->height/2;y++) {
424  for(x=0;x<c->width/2;x++) {
425  frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
426  frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
427  }
428  }
429 
430  frame->pts = i;
431 
432  /* encode the image */
433  ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
434  if (ret < 0) {
435  fprintf(stderr, "Error encoding frame\n");
436  exit(1);
437  }
438 
439  if (got_output) {
440  printf("Write frame %3d (size=%5d)\n", i, pkt.size);
441  fwrite(pkt.data, 1, pkt.size, f);
442  av_free_packet(&pkt);
443  }
444  }
445 
446  /* get the delayed frames */
447  for (got_output = 1; got_output; i++) {
448  fflush(stdout);
449 
450  ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
451  if (ret < 0) {
452  fprintf(stderr, "Error encoding frame\n");
453  exit(1);
454  }
455 
456  if (got_output) {
457  printf("Write frame %3d (size=%5d)\n", i, pkt.size);
458  fwrite(pkt.data, 1, pkt.size, f);
459  av_free_packet(&pkt);
460  }
461  }
462 
463  /* add sequence end code to have a real mpeg file */
464  fwrite(endcode, 1, sizeof(endcode), f);
465  fclose(f);
466 
467  avcodec_close(c);
468  av_free(c);
469  av_freep(&frame->data[0]);
470  avcodec_free_frame(&frame);
471  printf("\n");
472 }
473 
474 /*
475  * Video decoding example
476  */
477 
478 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
479  char *filename)
480 {
481  FILE *f;
482  int i;
483 
484  f=fopen(filename,"w");
485  fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
486  for(i=0;i<ysize;i++)
487  fwrite(buf + i * wrap,1,xsize,f);
488  fclose(f);
489 }
490 
491 static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
492  AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
493 {
494  int len, got_frame;
495  char buf[1024];
496 
497  len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
498  if (len < 0) {
499  fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
500  return len;
501  }
502  if (got_frame) {
503  printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
504  fflush(stdout);
505 
506  /* the picture is allocated by the decoder, no need to free it */
507  snprintf(buf, sizeof(buf), outfilename, *frame_count);
508  pgm_save(frame->data[0], frame->linesize[0],
509  avctx->width, avctx->height, buf);
510  (*frame_count)++;
511  }
512  if (pkt->data) {
513  pkt->size -= len;
514  pkt->data += len;
515  }
516  return 0;
517 }
518 
519 static void video_decode_example(const char *outfilename, const char *filename)
520 {
521  AVCodec *codec;
523  int frame_count;
524  FILE *f;
525  AVFrame *frame;
527  AVPacket avpkt;
528 
529  av_init_packet(&avpkt);
530 
531  /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
532  memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
533 
534  printf("Decode video file %s to %s\n", filename, outfilename);
535 
536  /* find the mpeg1 video decoder */
538  if (!codec) {
539  fprintf(stderr, "Codec not found\n");
540  exit(1);
541  }
542 
543  c = avcodec_alloc_context3(codec);
544  if (!c) {
545  fprintf(stderr, "Could not allocate video codec context\n");
546  exit(1);
547  }
548 
550  c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
551 
552  /* For some codecs, such as msmpeg4 and mpeg4, width and height
553  MUST be initialized there because this information is not
554  available in the bitstream. */
555 
556  /* open it */
557  if (avcodec_open2(c, codec, NULL) < 0) {
558  fprintf(stderr, "Could not open codec\n");
559  exit(1);
560  }
561 
562  f = fopen(filename, "rb");
563  if (!f) {
564  fprintf(stderr, "Could not open %s\n", filename);
565  exit(1);
566  }
567 
568  frame = avcodec_alloc_frame();
569  if (!frame) {
570  fprintf(stderr, "Could not allocate video frame\n");
571  exit(1);
572  }
573 
574  frame_count = 0;
575  for(;;) {
576  avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
577  if (avpkt.size == 0)
578  break;
579 
580  /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
581  and this is the only method to use them because you cannot
582  know the compressed data size before analysing it.
583 
584  BUT some other codecs (msmpeg4, mpeg4) are inherently frame
585  based, so you must call them with all the data for one
586  frame exactly. You must also initialize 'width' and
587  'height' before initializing them. */
588 
589  /* NOTE2: some codecs allow the raw parameters (frame size,
590  sample rate) to be changed at any frame. We handle this, so
591  you should also take care of it */
592 
593  /* here, we use a stream based decoder (mpeg1video), so we
594  feed decoder and see if it could decode a frame */
595  avpkt.data = inbuf;
596  while (avpkt.size > 0)
597  if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
598  exit(1);
599  }
600 
601  /* some codecs, such as MPEG, transmit the I and P frame with a
602  latency of one frame. You must do the following to have a
603  chance to get the last frame of the video */
604  avpkt.data = NULL;
605  avpkt.size = 0;
606  decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
607 
608  fclose(f);
609 
610  avcodec_close(c);
611  av_free(c);
612  avcodec_free_frame(&frame);
613  printf("\n");
614 }
615 
616 int main(int argc, char **argv)
617 {
618  const char *output_type;
619 
620  /* register all the codecs */
622 
623  if (argc < 2) {
624  printf("usage: %s output_type\n"
625  "API example program to decode/encode a media stream with libavcodec.\n"
626  "This program generates a synthetic stream and encodes it to a file\n"
627  "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
628  "The encoded stream is then decoded and written to a raw data output.\n"
629  "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n",
630  argv[0]);
631  return 1;
632  }
633  output_type = argv[1];
634 
635  if (!strcmp(output_type, "h264")) {
637  } else if (!strcmp(output_type, "mp2")) {
638  audio_encode_example("test.mp2");
639  audio_decode_example("test.sw", "test.mp2");
640  } else if (!strcmp(output_type, "mpg")) {
642  video_decode_example("test%02d.pgm", "test.mpg");
643  } else {
644  fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
645  output_type);
646  return 1;
647  }
648 
649  return 0;
650 }
static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
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.
Definition: samplefmt.c:125
misc image utilities
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...
Definition: imgutils.c:190
#define CODEC_CAP_TRUNCATED
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...
Sinusoidal phase f
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define wrap(func)
Definition: w64xmmtest.h:70
static void video_decode_example(const char *outfilename, const char *filename)
int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
#define AV_CH_LAYOUT_STEREO
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
Definition: allcodecs.c:67
signed 16 bits
Definition: samplefmt.h:52
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 ...
Definition: mem.c:198
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
uint8_t
AVOptions.
static AVPacket pkt
Definition: demuxing.c:56
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
uint8_t * data
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
frame
Definition: stft.m:14
Discrete Time axis x
static int select_sample_rate(AVCodec *codec)
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
#define CODEC_FLAG_TRUNCATED
int width
width and height of the video frame
Definition: frame.h:122
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
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.
int capabilities
Codec capabilities.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int flags
CODEC_FLAG_*.
enum AVCodecID codec_id
Definition: mov_chan.c:433
#define FFMAX(a, b)
Definition: common.h:56
#define AUDIO_REFILL_THRESH
external API header
uint64_t channel_layout
Audio channel layout.
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
struct AVRational AVRational
rational number numerator/denominator
int bit_rate
the average bitrate
audio channel layout utility functions
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
ret
Definition: avfilter.c:821
int width
picture width / height.
t
Definition: genspecsines3.m:6
static void audio_encode_example(const char *filename)
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.
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.
Definition: samplefmt.c:47
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...
Definition: frame.h:134
for k
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)
NULL
Definition: eval.c:55
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.
Definition: frame.h:101
Close file fclose(fid)
main external API structure.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
static float tincr
Definition: muxing.c:52
void * buf
Definition: avisynth_c.h:594
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
synthesis window for stochastic i
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 snprintf
Definition: snprintf.h:34
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
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)
Definition: pixfmt.h:68
static int frame_count
Definition: muxing.c:234
common internal and external API header
static double c[64]
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
function y
Definition: D.m:1
#define INBUF_SIZE
int len
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 ...
printf("static const uint8_t my_array[100] = {\n")
#define AUDIO_INBUF_SIZE
void avcodec_free_frame(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int height
Definition: frame.h:122
Filter the word “frame” indicates either a video frame or a group of audio samples
#define M_PI
Definition: mathematics.h:46
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
int nb_channels
This structure stores compressed data.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:252
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
FILE * outfile
Definition: audiogen.c:96
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190