FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
transcoding.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Nicolas George
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2014 Andrey Utkin
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /**
26  * @file
27  * API example for demuxing, decoding, filtering, encoding and muxing
28  * @example transcoding.c
29  */
30 
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
34 #include <libavfilter/buffersink.h>
35 #include <libavfilter/buffersrc.h>
36 #include <libavutil/opt.h>
37 #include <libavutil/pixdesc.h>
38 
41 typedef struct FilteringContext {
47 
48 static int open_input_file(const char *filename)
49 {
50  int ret;
51  unsigned int i;
52 
53  ifmt_ctx = NULL;
54  if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
55  av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
56  return ret;
57  }
58 
59  if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
60  av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
61  return ret;
62  }
63 
64  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
65  AVStream *stream;
66  AVCodecContext *codec_ctx;
67  stream = ifmt_ctx->streams[i];
68  codec_ctx = stream->codec;
69  /* Reencode video & audio and remux subtitles etc. */
70  if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
71  || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
72  /* Open decoder */
73  ret = avcodec_open2(codec_ctx,
74  avcodec_find_decoder(codec_ctx->codec_id), NULL);
75  if (ret < 0) {
76  av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
77  return ret;
78  }
79  }
80  }
81 
82  av_dump_format(ifmt_ctx, 0, filename, 0);
83  return 0;
84 }
85 
86 static int open_output_file(const char *filename)
87 {
88  AVStream *out_stream;
89  AVStream *in_stream;
90  AVCodecContext *dec_ctx, *enc_ctx;
91  AVCodec *encoder;
92  int ret;
93  unsigned int i;
94 
95  ofmt_ctx = NULL;
96  avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
97  if (!ofmt_ctx) {
98  av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
99  return AVERROR_UNKNOWN;
100  }
101 
102 
103  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
104  out_stream = avformat_new_stream(ofmt_ctx, NULL);
105  if (!out_stream) {
106  av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
107  return AVERROR_UNKNOWN;
108  }
109 
110  in_stream = ifmt_ctx->streams[i];
111  dec_ctx = in_stream->codec;
112  enc_ctx = out_stream->codec;
113 
114  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
115  || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
116  /* in this example, we choose transcoding to same codec */
117  encoder = avcodec_find_encoder(dec_ctx->codec_id);
118  if (!encoder) {
119  av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
120  return AVERROR_INVALIDDATA;
121  }
122 
123  /* In this example, we transcode to same properties (picture size,
124  * sample rate etc.). These properties can be changed for output
125  * streams easily using filters */
126  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
127  enc_ctx->height = dec_ctx->height;
128  enc_ctx->width = dec_ctx->width;
129  enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
130  /* take first format from list of supported formats */
131  if (encoder->pix_fmts)
132  enc_ctx->pix_fmt = encoder->pix_fmts[0];
133  else
134  enc_ctx->pix_fmt = dec_ctx->pix_fmt;
135  /* video time_base can be set to whatever is handy and supported by encoder */
136  enc_ctx->time_base = dec_ctx->time_base;
137  } else {
138  enc_ctx->sample_rate = dec_ctx->sample_rate;
139  enc_ctx->channel_layout = dec_ctx->channel_layout;
141  /* take first format from list of supported formats */
142  enc_ctx->sample_fmt = encoder->sample_fmts[0];
143  enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
144  }
145 
146  /* Third parameter can be used to pass settings to encoder */
147  ret = avcodec_open2(enc_ctx, encoder, NULL);
148  if (ret < 0) {
149  av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
150  return ret;
151  }
152  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
153  av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
154  return AVERROR_INVALIDDATA;
155  } else {
156  /* if this stream must be remuxed */
157  ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
158  ifmt_ctx->streams[i]->codec);
159  if (ret < 0) {
160  av_log(NULL, AV_LOG_ERROR, "Copying stream context failed\n");
161  return ret;
162  }
163  }
164 
165  if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
167 
168  }
169  av_dump_format(ofmt_ctx, 0, filename, 1);
170 
171  if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
172  ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
173  if (ret < 0) {
174  av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
175  return ret;
176  }
177  }
178 
179  /* init muxer, write output file header */
180  ret = avformat_write_header(ofmt_ctx, NULL);
181  if (ret < 0) {
182  av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
183  return ret;
184  }
185 
186  return 0;
187 }
188 
190  AVCodecContext *enc_ctx, const char *filter_spec)
191 {
192  char args[512];
193  int ret = 0;
194  AVFilter *buffersrc = NULL;
195  AVFilter *buffersink = NULL;
201 
202  if (!outputs || !inputs || !filter_graph) {
203  ret = AVERROR(ENOMEM);
204  goto end;
205  }
206 
207  if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
208  buffersrc = avfilter_get_by_name("buffer");
209  buffersink = avfilter_get_by_name("buffersink");
210  if (!buffersrc || !buffersink) {
211  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
212  ret = AVERROR_UNKNOWN;
213  goto end;
214  }
215 
216  snprintf(args, sizeof(args),
217  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
218  dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
219  dec_ctx->time_base.num, dec_ctx->time_base.den,
220  dec_ctx->sample_aspect_ratio.num,
221  dec_ctx->sample_aspect_ratio.den);
222 
223  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
224  args, NULL, filter_graph);
225  if (ret < 0) {
226  av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
227  goto end;
228  }
229 
230  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
231  NULL, NULL, filter_graph);
232  if (ret < 0) {
233  av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
234  goto end;
235  }
236 
237  ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
238  (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
240  if (ret < 0) {
241  av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
242  goto end;
243  }
244  } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
245  buffersrc = avfilter_get_by_name("abuffer");
246  buffersink = avfilter_get_by_name("abuffersink");
247  if (!buffersrc || !buffersink) {
248  av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
249  ret = AVERROR_UNKNOWN;
250  goto end;
251  }
252 
253  if (!dec_ctx->channel_layout)
254  dec_ctx->channel_layout =
256  snprintf(args, sizeof(args),
257  "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
258  dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
260  dec_ctx->channel_layout);
261  ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
262  args, NULL, filter_graph);
263  if (ret < 0) {
264  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
265  goto end;
266  }
267 
268  ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
269  NULL, NULL, filter_graph);
270  if (ret < 0) {
271  av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
272  goto end;
273  }
274 
275  ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
276  (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
278  if (ret < 0) {
279  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
280  goto end;
281  }
282 
283  ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
284  (uint8_t*)&enc_ctx->channel_layout,
285  sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
286  if (ret < 0) {
287  av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
288  goto end;
289  }
290 
291  ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
292  (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
294  if (ret < 0) {
295  av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
296  goto end;
297  }
298  } else {
299  ret = AVERROR_UNKNOWN;
300  goto end;
301  }
302 
303  /* Endpoints for the filter graph. */
304  outputs->name = av_strdup("in");
305  outputs->filter_ctx = buffersrc_ctx;
306  outputs->pad_idx = 0;
307  outputs->next = NULL;
308 
309  inputs->name = av_strdup("out");
310  inputs->filter_ctx = buffersink_ctx;
311  inputs->pad_idx = 0;
312  inputs->next = NULL;
313 
314  if (!outputs->name || !inputs->name) {
315  ret = AVERROR(ENOMEM);
316  goto end;
317  }
318 
319  if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
320  &inputs, &outputs, NULL)) < 0)
321  goto end;
322 
323  if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
324  goto end;
325 
326  /* Fill FilteringContext */
329  fctx->filter_graph = filter_graph;
330 
331 end:
332  avfilter_inout_free(&inputs);
333  avfilter_inout_free(&outputs);
334 
335  return ret;
336 }
337 
338 static int init_filters(void)
339 {
340  const char *filter_spec;
341  unsigned int i;
342  int ret;
343  filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
344  if (!filter_ctx)
345  return AVERROR(ENOMEM);
346 
347  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
348  filter_ctx[i].buffersrc_ctx = NULL;
349  filter_ctx[i].buffersink_ctx = NULL;
350  filter_ctx[i].filter_graph = NULL;
351  if (!(ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO
352  || ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
353  continue;
354 
355 
356  if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
357  filter_spec = "null"; /* passthrough (dummy) filter for video */
358  else
359  filter_spec = "anull"; /* passthrough (dummy) filter for audio */
360  ret = init_filter(&filter_ctx[i], ifmt_ctx->streams[i]->codec,
361  ofmt_ctx->streams[i]->codec, filter_spec);
362  if (ret)
363  return ret;
364  }
365  return 0;
366 }
367 
368 static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
369  int ret;
370  int got_frame_local;
371  AVPacket enc_pkt;
372  int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
373  (ifmt_ctx->streams[stream_index]->codec->codec_type ==
375 
376  if (!got_frame)
377  got_frame = &got_frame_local;
378 
379  av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
380  /* encode filtered frame */
381  enc_pkt.data = NULL;
382  enc_pkt.size = 0;
383  av_init_packet(&enc_pkt);
384  ret = enc_func(ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
385  filt_frame, got_frame);
386  av_frame_free(&filt_frame);
387  if (ret < 0)
388  return ret;
389  if (!(*got_frame))
390  return 0;
391 
392  /* prepare packet for muxing */
393  enc_pkt.stream_index = stream_index;
394  av_packet_rescale_ts(&enc_pkt,
395  ofmt_ctx->streams[stream_index]->codec->time_base,
396  ofmt_ctx->streams[stream_index]->time_base);
397 
398  av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
399  /* mux encoded frame */
400  ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
401  return ret;
402 }
403 
404 static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
405 {
406  int ret;
407  AVFrame *filt_frame;
408 
409  av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
410  /* push the decoded frame into the filtergraph */
411  ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
412  frame, 0);
413  if (ret < 0) {
414  av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
415  return ret;
416  }
417 
418  /* pull filtered frames from the filtergraph */
419  while (1) {
420  filt_frame = av_frame_alloc();
421  if (!filt_frame) {
422  ret = AVERROR(ENOMEM);
423  break;
424  }
425  av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
426  ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
427  filt_frame);
428  if (ret < 0) {
429  /* if no more frames for output - returns AVERROR(EAGAIN)
430  * if flushed and no more frames for output - returns AVERROR_EOF
431  * rewrite retcode to 0 to show it as normal procedure completion
432  */
433  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
434  ret = 0;
435  av_frame_free(&filt_frame);
436  break;
437  }
438 
439  filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
440  ret = encode_write_frame(filt_frame, stream_index, NULL);
441  if (ret < 0)
442  break;
443  }
444 
445  return ret;
446 }
447 
448 static int flush_encoder(unsigned int stream_index)
449 {
450  int ret;
451  int got_frame;
452 
453  if (!(ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
455  return 0;
456 
457  while (1) {
458  av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
459  ret = encode_write_frame(NULL, stream_index, &got_frame);
460  if (ret < 0)
461  break;
462  if (!got_frame)
463  return 0;
464  }
465  return ret;
466 }
467 
468 int main(int argc, char **argv)
469 {
470  int ret;
471  AVPacket packet = { .data = NULL, .size = 0 };
472  AVFrame *frame = NULL;
473  enum AVMediaType type;
474  unsigned int stream_index;
475  unsigned int i;
476  int got_frame;
477  int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
478 
479  if (argc != 3) {
480  av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
481  return 1;
482  }
483 
484  av_register_all();
486 
487  if ((ret = open_input_file(argv[1])) < 0)
488  goto end;
489  if ((ret = open_output_file(argv[2])) < 0)
490  goto end;
491  if ((ret = init_filters()) < 0)
492  goto end;
493 
494  /* read all packets */
495  while (1) {
496  if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
497  break;
498  stream_index = packet.stream_index;
499  type = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
500  av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
501  stream_index);
502 
503  if (filter_ctx[stream_index].filter_graph) {
504  av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
505  frame = av_frame_alloc();
506  if (!frame) {
507  ret = AVERROR(ENOMEM);
508  break;
509  }
510  av_packet_rescale_ts(&packet,
511  ifmt_ctx->streams[stream_index]->time_base,
512  ifmt_ctx->streams[stream_index]->codec->time_base);
513  dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
515  ret = dec_func(ifmt_ctx->streams[stream_index]->codec, frame,
516  &got_frame, &packet);
517  if (ret < 0) {
518  av_frame_free(&frame);
519  av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
520  break;
521  }
522 
523  if (got_frame) {
524  frame->pts = av_frame_get_best_effort_timestamp(frame);
525  ret = filter_encode_write_frame(frame, stream_index);
526  av_frame_free(&frame);
527  if (ret < 0)
528  goto end;
529  } else {
530  av_frame_free(&frame);
531  }
532  } else {
533  /* remux this frame without reencoding */
534  av_packet_rescale_ts(&packet,
535  ifmt_ctx->streams[stream_index]->time_base,
536  ofmt_ctx->streams[stream_index]->time_base);
537 
538  ret = av_interleaved_write_frame(ofmt_ctx, &packet);
539  if (ret < 0)
540  goto end;
541  }
542  av_packet_unref(&packet);
543  }
544 
545  /* flush filters and encoders */
546  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
547  /* flush filter */
548  if (!filter_ctx[i].filter_graph)
549  continue;
551  if (ret < 0) {
552  av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
553  goto end;
554  }
555 
556  /* flush encoder */
557  ret = flush_encoder(i);
558  if (ret < 0) {
559  av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
560  goto end;
561  }
562  }
563 
564  av_write_trailer(ofmt_ctx);
565 end:
566  av_packet_unref(&packet);
567  av_frame_free(&frame);
568  for (i = 0; i < ifmt_ctx->nb_streams; i++) {
569  avcodec_close(ifmt_ctx->streams[i]->codec);
570  if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
571  avcodec_close(ofmt_ctx->streams[i]->codec);
572  if (filter_ctx && filter_ctx[i].filter_graph)
573  avfilter_graph_free(&filter_ctx[i].filter_graph);
574  }
575  av_free(filter_ctx);
576  avformat_close_input(&ifmt_ctx);
577  if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
578  avio_closep(&ofmt_ctx->pb);
579  avformat_free_context(ofmt_ctx);
580 
581  if (ret < 0)
582  av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
583 
584  return ret ? 1 : 0;
585 }
int avio_open(AVIOContext **s, const char *url, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1033
AVFilterGraph * filter_graph
Definition: transcoding.c:44
#define NULL
Definition: coverity.c:32
AVFilterContext * buffersink_ctx
static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame)
Definition: transcoding.c:368
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
Definition: utils.c:3108
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
Definition: mux.c:1225
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:76
Memory buffer source API.
AVFilterGraph * filter_graph
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
Definition: graphparser.c:187
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
Definition: avfilter.h:972
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1602
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:607
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:2087
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
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:1808
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
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:2316
int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int search_flags)
Definition: opt.c:554
AVCodec.
Definition: avcodec.h:3600
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1813
Undefined.
Definition: avutil.h:267
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Format I/O context.
Definition: avformat.h:1338
static int open_input_file(const char *filename)
Definition: transcoding.c:48
memory buffer sink API for audio and video
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:984
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2446
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT, AVFMT_TS_NEGATIVE
Definition: avformat.h:543
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:145
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4193
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1406
void avfilter_register_all(void)
Initialize the filter system.
Definition: allfilters.c:40
static AVFrame * frame
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx)
Create and add a filter instance into an existing graph.
uint8_t * data
Definition: avcodec.h:1601
#define AVERROR_EOF
End of file.
Definition: error.h:55
int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame)
Accessors for some AVFrame fields.
#define av_log(a,...)
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1357
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
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, streams, container, programs, metadata, side data, codec and time base.
Definition: dump.c:511
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:3023
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int flush_encoder(unsigned int stream_index)
Definition: transcoding.c:448
static AVFormatContext * ifmt_ctx
Definition: transcoding.c:39
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...
Definition: avpacket.c:630
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1771
AVFilterContext * buffersrc_ctx
static FilteringContext * filter_ctx
Definition: transcoding.c:46
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
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2489
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:519
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1394
AVFilterContext * buffersrc_ctx
Definition: transcoding.c:43
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3621
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
static int init_filter(FilteringContext *fctx, AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, const char *filter_spec)
Definition: transcoding.c:189
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.
Definition: mux.c:527
attribute_deprecated int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:190
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:556
int width
picture width / height.
Definition: avcodec.h:1863
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:485
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:194
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
AVFilterContext * filter_ctx
filter context associated to this input/output
Definition: avfilter.h:966
static int init_filters(void)
Definition: transcoding.c:338
Stream structure.
Definition: avformat.h:889
A linked-list of the inputs/outputs of the filter chain.
Definition: avfilter.h:961
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
Libavcodec external API header.
enum AVMediaType codec_type
Definition: avcodec.h:1684
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:2213
enum AVCodecID codec_id
Definition: avcodec.h:1693
int sample_rate
samples per second
Definition: avcodec.h:2438
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
main external API structure.
Definition: avcodec.h:1676
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:3127
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:567
GLint GLenum type
Definition: opengl_enc.c:105
Filter definition.
Definition: avfilter.h:144
int pad_idx
index of the filt_ctx pad to use for linking
Definition: avfilter.h:969
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:1954
static int open_output_file(const char *filename)
Definition: transcoding.c:86
AVMediaType
Definition: avutil.h:193
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1241
#define snprintf
Definition: snprintf.h:34
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4129
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1676
char * name
unique name for this input/output in the list
Definition: avfilter.h:963
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:882
Main libavformat public API header.
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
Definition: graphparser.c:182
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:478
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3336
static AVCodecContext * dec_ctx
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
int den
Denominator.
Definition: rational.h:60
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
Definition: transcoding.c:404
int attribute_align_arg av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
Add a frame to the buffer source.
Definition: buffersrc.c:152
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4165
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:2439
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:503
int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
Add a graph described by a string to a graph.
Definition: graphparser.c:522
An instance of a filter.
Definition: avfilter.h:307
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1287
int main(int argc, char **argv)
Definition: transcoding.c:468
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
#define av_malloc_array(a, b)
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: avcodec.h:3623
AVFilterContext * buffersink_ctx
Definition: transcoding.c:42
int stream_index
Definition: avcodec.h:1603
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
Get a frame with filtered data from sink and put it in frame.
Definition: buffersink.c:123
This structure stores compressed data.
Definition: avcodec.h:1578
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:44
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1092
static AVFormatContext * ofmt_ctx
Definition: transcoding.c:40