FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  * @example decoding_encoding.c
28  * Note that libavcodec only handles codecs (MPEG, MPEG-4, etc...),
29  * not file formats (AVI, VOB, MP4, MOV, MKV, MXF, FLV, MPEG-TS, MPEG-PS, etc...).
30  * See library 'libavformat' for the format handling
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 = av_frame_alloc();
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  if (buffer_size < 0) {
174  fprintf(stderr, "Could not get sample buffer size\n");
175  exit(1);
176  }
177  samples = av_malloc(buffer_size);
178  if (!samples) {
179  fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
180  buffer_size);
181  exit(1);
182  }
183  /* setup the data pointers in the AVFrame */
184  ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
185  (const uint8_t*)samples, buffer_size, 0);
186  if (ret < 0) {
187  fprintf(stderr, "Could not setup audio frame\n");
188  exit(1);
189  }
190 
191  /* encode a single tone sound */
192  t = 0;
193  tincr = 2 * M_PI * 440.0 / c->sample_rate;
194  for (i = 0; i < 200; i++) {
195  av_init_packet(&pkt);
196  pkt.data = NULL; // packet data will be allocated by the encoder
197  pkt.size = 0;
198 
199  for (j = 0; j < c->frame_size; j++) {
200  samples[2*j] = (int)(sin(t) * 10000);
201 
202  for (k = 1; k < c->channels; k++)
203  samples[2*j + k] = samples[2*j];
204  t += tincr;
205  }
206  /* encode the samples */
207  ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
208  if (ret < 0) {
209  fprintf(stderr, "Error encoding audio frame\n");
210  exit(1);
211  }
212  if (got_output) {
213  fwrite(pkt.data, 1, pkt.size, f);
214  av_packet_unref(&pkt);
215  }
216  }
217 
218  /* get the delayed frames */
219  for (got_output = 1; got_output; i++) {
220  ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
221  if (ret < 0) {
222  fprintf(stderr, "Error encoding frame\n");
223  exit(1);
224  }
225 
226  if (got_output) {
227  fwrite(pkt.data, 1, pkt.size, f);
228  av_packet_unref(&pkt);
229  }
230  }
231  fclose(f);
232 
233  av_freep(&samples);
234  av_frame_free(&frame);
236 }
237 
238 /*
239  * Audio decoding.
240  */
241 static void audio_decode_example(const char *outfilename, const char *filename)
242 {
243  AVCodec *codec;
245  int len;
246  FILE *f, *outfile;
248  AVPacket avpkt;
249  AVFrame *decoded_frame = NULL;
250 
251  av_init_packet(&avpkt);
252 
253  printf("Decode audio file %s to %s\n", filename, outfilename);
254 
255  /* find the MPEG audio decoder */
257  if (!codec) {
258  fprintf(stderr, "Codec not found\n");
259  exit(1);
260  }
261 
262  c = avcodec_alloc_context3(codec);
263  if (!c) {
264  fprintf(stderr, "Could not allocate audio codec context\n");
265  exit(1);
266  }
267 
268  /* open it */
269  if (avcodec_open2(c, codec, NULL) < 0) {
270  fprintf(stderr, "Could not open codec\n");
271  exit(1);
272  }
273 
274  f = fopen(filename, "rb");
275  if (!f) {
276  fprintf(stderr, "Could not open %s\n", filename);
277  exit(1);
278  }
279  outfile = fopen(outfilename, "wb");
280  if (!outfile) {
281  av_free(c);
282  exit(1);
283  }
284 
285  /* decode until eof */
286  avpkt.data = inbuf;
287  avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
288 
289  while (avpkt.size > 0) {
290  int i, ch;
291  int got_frame = 0;
292 
293  if (!decoded_frame) {
294  if (!(decoded_frame = av_frame_alloc())) {
295  fprintf(stderr, "Could not allocate audio frame\n");
296  exit(1);
297  }
298  }
299 
300  len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
301  if (len < 0) {
302  fprintf(stderr, "Error while decoding\n");
303  exit(1);
304  }
305  if (got_frame) {
306  /* if a frame has been decoded, output it */
307  int data_size = av_get_bytes_per_sample(c->sample_fmt);
308  if (data_size < 0) {
309  /* This should not occur, checking just for paranoia */
310  fprintf(stderr, "Failed to calculate data size\n");
311  exit(1);
312  }
313  for (i=0; i<decoded_frame->nb_samples; i++)
314  for (ch=0; ch<c->channels; ch++)
315  fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
316  }
317  avpkt.size -= len;
318  avpkt.data += len;
319  avpkt.dts =
320  avpkt.pts = AV_NOPTS_VALUE;
321  if (avpkt.size < AUDIO_REFILL_THRESH) {
322  /* Refill the input buffer, to avoid trying to decode
323  * incomplete frames. Instead of this, one could also use
324  * a parser, or use a proper container format through
325  * libavformat. */
326  memmove(inbuf, avpkt.data, avpkt.size);
327  avpkt.data = inbuf;
328  len = fread(avpkt.data + avpkt.size, 1,
329  AUDIO_INBUF_SIZE - avpkt.size, f);
330  if (len > 0)
331  avpkt.size += len;
332  }
333  }
334 
335  fclose(outfile);
336  fclose(f);
337 
339  av_frame_free(&decoded_frame);
340 }
341 
342 /*
343  * Video encoding example
344  */
345 static void video_encode_example(const char *filename, int codec_id)
346 {
347  AVCodec *codec;
349  int i, ret, x, y, got_output;
350  FILE *f;
351  AVFrame *frame;
352  AVPacket pkt;
353  uint8_t endcode[] = { 0, 0, 1, 0xb7 };
354 
355  printf("Encode video file %s\n", filename);
356 
357  /* find the video encoder */
358  codec = avcodec_find_encoder(codec_id);
359  if (!codec) {
360  fprintf(stderr, "Codec not found\n");
361  exit(1);
362  }
363 
364  c = avcodec_alloc_context3(codec);
365  if (!c) {
366  fprintf(stderr, "Could not allocate video codec context\n");
367  exit(1);
368  }
369 
370  /* put sample parameters */
371  c->bit_rate = 400000;
372  /* resolution must be a multiple of two */
373  c->width = 352;
374  c->height = 288;
375  /* frames per second */
376  c->time_base = (AVRational){1,25};
377  /* emit one intra frame every ten frames
378  * check frame pict_type before passing frame
379  * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
380  * then gop_size is ignored and the output of encoder
381  * will always be I frame irrespective to gop_size
382  */
383  c->gop_size = 10;
384  c->max_b_frames = 1;
386 
387  if (codec_id == AV_CODEC_ID_H264)
388  av_opt_set(c->priv_data, "preset", "slow", 0);
389 
390  /* open it */
391  if (avcodec_open2(c, codec, NULL) < 0) {
392  fprintf(stderr, "Could not open codec\n");
393  exit(1);
394  }
395 
396  f = fopen(filename, "wb");
397  if (!f) {
398  fprintf(stderr, "Could not open %s\n", filename);
399  exit(1);
400  }
401 
402  frame = av_frame_alloc();
403  if (!frame) {
404  fprintf(stderr, "Could not allocate video frame\n");
405  exit(1);
406  }
407  frame->format = c->pix_fmt;
408  frame->width = c->width;
409  frame->height = c->height;
410 
411  /* the image can be allocated by any means and av_image_alloc() is
412  * just the most convenient way if av_malloc() is to be used */
413  ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
414  c->pix_fmt, 32);
415  if (ret < 0) {
416  fprintf(stderr, "Could not allocate raw picture buffer\n");
417  exit(1);
418  }
419 
420  /* encode 1 second of video */
421  for (i = 0; i < 25; i++) {
422  av_init_packet(&pkt);
423  pkt.data = NULL; // packet data will be allocated by the encoder
424  pkt.size = 0;
425 
426  fflush(stdout);
427  /* prepare a dummy image */
428  /* Y */
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;
432  }
433  }
434 
435  /* Cb and Cr */
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;
440  }
441  }
442 
443  frame->pts = i;
444 
445  /* encode the image */
446  ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
447  if (ret < 0) {
448  fprintf(stderr, "Error encoding frame\n");
449  exit(1);
450  }
451 
452  if (got_output) {
453  printf("Write frame %3d (size=%5d)\n", i, pkt.size);
454  fwrite(pkt.data, 1, pkt.size, f);
455  av_packet_unref(&pkt);
456  }
457  }
458 
459  /* get the delayed frames */
460  for (got_output = 1; got_output; i++) {
461  fflush(stdout);
462 
463  ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
464  if (ret < 0) {
465  fprintf(stderr, "Error encoding frame\n");
466  exit(1);
467  }
468 
469  if (got_output) {
470  printf("Write frame %3d (size=%5d)\n", i, pkt.size);
471  fwrite(pkt.data, 1, pkt.size, f);
472  av_packet_unref(&pkt);
473  }
474  }
475 
476  /* add sequence end code to have a real MPEG file */
477  fwrite(endcode, 1, sizeof(endcode), f);
478  fclose(f);
479 
481  av_freep(&frame->data[0]);
482  av_frame_free(&frame);
483  printf("\n");
484 }
485 
486 /*
487  * Video decoding example
488  */
489 
490 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
491  char *filename)
492 {
493  FILE *f;
494  int i;
495 
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);
500  fclose(f);
501 }
502 
503 static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
504  AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
505 {
506  int len, got_frame;
507  char buf[1024];
508 
509  len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
510  if (len < 0) {
511  fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
512  return len;
513  }
514  if (got_frame) {
515  printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
516  fflush(stdout);
517 
518  /* the picture is allocated by the decoder, no need to free it */
519  snprintf(buf, sizeof(buf), outfilename, *frame_count);
520  pgm_save(frame->data[0], frame->linesize[0],
521  frame->width, frame->height, buf);
522  (*frame_count)++;
523  }
524  if (pkt->data) {
525  pkt->size -= len;
526  pkt->data += len;
527  }
528  return 0;
529 }
530 
531 static void video_decode_example(const char *outfilename, const char *filename)
532 {
533  AVCodec *codec;
535  int frame_count;
536  FILE *f;
537  AVFrame *frame;
539  AVPacket avpkt;
540 
541  av_init_packet(&avpkt);
542 
543  /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
544  memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
545 
546  printf("Decode video file %s to %s\n", filename, outfilename);
547 
548  /* find the MPEG-1 video decoder */
550  if (!codec) {
551  fprintf(stderr, "Codec not found\n");
552  exit(1);
553  }
554 
555  c = avcodec_alloc_context3(codec);
556  if (!c) {
557  fprintf(stderr, "Could not allocate video codec context\n");
558  exit(1);
559  }
560 
562  c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
563 
564  /* For some codecs, such as msmpeg4 and mpeg4, width and height
565  MUST be initialized there because this information is not
566  available in the bitstream. */
567 
568  /* open it */
569  if (avcodec_open2(c, codec, NULL) < 0) {
570  fprintf(stderr, "Could not open codec\n");
571  exit(1);
572  }
573 
574  f = fopen(filename, "rb");
575  if (!f) {
576  fprintf(stderr, "Could not open %s\n", filename);
577  exit(1);
578  }
579 
580  frame = av_frame_alloc();
581  if (!frame) {
582  fprintf(stderr, "Could not allocate video frame\n");
583  exit(1);
584  }
585 
586  frame_count = 0;
587  for (;;) {
588  avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
589  if (avpkt.size == 0)
590  break;
591 
592  /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
593  and this is the only method to use them because you cannot
594  know the compressed data size before analysing it.
595 
596  BUT some other codecs (msmpeg4, mpeg4) are inherently frame
597  based, so you must call them with all the data for one
598  frame exactly. You must also initialize 'width' and
599  'height' before initializing them. */
600 
601  /* NOTE2: some codecs allow the raw parameters (frame size,
602  sample rate) to be changed at any frame. We handle this, so
603  you should also take care of it */
604 
605  /* here, we use a stream based decoder (mpeg1video), so we
606  feed decoder and see if it could decode a frame */
607  avpkt.data = inbuf;
608  while (avpkt.size > 0)
609  if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
610  exit(1);
611  }
612 
613  /* Some codecs, such as MPEG, transmit the I- and P-frame with a
614  latency of one frame. You must do the following to have a
615  chance to get the last frame of the video. */
616  avpkt.data = NULL;
617  avpkt.size = 0;
618  decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
619 
620  fclose(f);
621 
623  av_frame_free(&frame);
624  printf("\n");
625 }
626 
627 int main(int argc, char **argv)
628 {
629  const char *output_type;
630 
631  /* register all the codecs */
633 
634  if (argc < 2) {
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",
641  argv[0]);
642  return 1;
643  }
644  output_type = argv[1];
645 
646  if (!strcmp(output_type, "h264")) {
648  } else if (!strcmp(output_type, "mp2")) {
649  audio_encode_example("test.mp2");
650  audio_decode_example("test.pcm", "test.mp2");
651  } else if (!strcmp(output_type, "mpg")) {
653  video_decode_example("test%02d.pgm", "test.mpg");
654  } else {
655  fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
656  output_type);
657  return 1;
658  }
659 
660  return 0;
661 }
static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:190
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:3117
misc image utilities
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1756
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:191
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...
Definition: avcodec.h:1977
int size
Definition: avcodec.h:1617
attribute_deprecated int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of audio.
Definition: utils.c:1814
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1919
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.
Definition: utils.c:2322
static void video_decode_example(const char *outfilename, const char *filename)
static AVPacket pkt
#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:61
AVCodec.
Definition: avcodec.h:3646
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1828
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.
Definition: utils.c:475
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2461
uint8_t
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:145
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:274
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1616
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
Definition: avcodec.h:3670
int width
width and height of the video frame
Definition: frame.h:242
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
int capabilities
Codec capabilities.
Definition: avcodec.h:3665
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1786
#define wrap(func)
Definition: neontest.h:65
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:49
#define FFMAX(a, b)
Definition: common.h:94
#define AUDIO_REFILL_THRESH
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2504
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:359
audio channel layout utility functions
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:156
int width
picture width / height.
Definition: avcodec.h:1878
static void audio_encode_example(const char *filename)
enum AVCodecID codec_id
Definition: vaapi_decode.c:234
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:254
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2473
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.
Definition: utils.c:2219
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:171
int main(int argc, char **argv)
int sample_rate
samples per second
Definition: avcodec.h:2453
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:221
main external API structure.
Definition: avcodec.h:1691
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:3136
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:571
void * buf
Definition: avisynth_c.h:690
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:119
Rational number (pair of numerator and denominator).
Definition: rational.h:58
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: utils.c:1960
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.
Definition: utils.c:1247
#define snprintf
Definition: snprintf.h:34
#define AV_CODEC_CAP_TRUNCATED
Definition: avcodec.h:969
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:204
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1904
int
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
common internal and external API header
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
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
Definition: audioconvert.c:56
#define INBUF_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:743
void * priv_data
Definition: avcodec.h:1733
#define av_free(p)
int len
int channels
number of audio channels
Definition: avcodec.h:2454
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 ...
Definition: avcodec.h:3668
#define AUDIO_INBUF_SIZE
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1615
#define AV_CODEC_FLAG_TRUNCATED
Input bitstream might be truncated at a random location instead of only at frame boundaries.
Definition: avcodec.h:879
int height
Definition: frame.h:242
#define av_freep(p)
#define M_PI
Definition: mathematics.h:52
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3669
int nb_channels
This structure stores compressed data.
Definition: avcodec.h:1593
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:451
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:247
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1609
FILE * outfile
Definition: audiogen.c:96
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248