Advertisement
Terbaddo

libav program - functions.cpp

Dec 31st, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. extern "C" {
  6. #include <libavutil/mathematics.h>
  7. #include <libavformat/avformat.h>
  8. #include <libswscale/swscale.h>
  9. #include <libavcodec/avcodec.h>
  10. size_t av_strlcpy(char *dst, const char *src, size_t size);
  11. void av_opt_set_defaults2(void *s, int mask, int flags);
  12. }
  13.  
  14. int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
  15.                                     const char *format, const char *filename)
  16.  {
  17.      AVFormatContext *s = avformat_alloc_context();
  18.      int ret = 0;
  19.  
  20.      *avctx = NULL;
  21.      if (!s)
  22.          goto nomem;
  23.  
  24.      if (!oformat) {
  25.          if (format) {
  26.              oformat = av_guess_format(format, NULL, NULL);
  27.              if (!oformat) {
  28.                  av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
  29.                  ret = AVERROR(EINVAL);
  30.                  goto error;
  31.              }
  32.          } else {
  33.              oformat = av_guess_format(NULL, filename, NULL);
  34.              if (!oformat) {
  35.                  ret = AVERROR(EINVAL);
  36.                  av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
  37.                         filename);
  38.                  goto error;
  39.              }
  40.          }
  41.      }
  42.  
  43.      s->oformat = oformat;
  44.      if (s->oformat->priv_data_size > 0) {
  45.          s->priv_data = av_mallocz(s->oformat->priv_data_size);
  46.          if (!s->priv_data)
  47.              goto nomem;
  48.          if (s->oformat->priv_class) {
  49.              *(const AVClass**)s->priv_data= s->oformat->priv_class;
  50.              av_opt_set_defaults2(s->priv_data, 0, 0);
  51.          }
  52.      } else
  53.          s->priv_data = NULL;
  54.  
  55.      if (filename)
  56.          av_strlcpy(s->filename, filename, sizeof(s->filename));
  57.      *avctx = s;
  58.      return 0;
  59.  nomem:
  60.      av_log(s, AV_LOG_ERROR, "Out of memory\n");
  61.      ret = AVERROR(ENOMEM);
  62.  error:
  63.      avformat_free_context(s);
  64.      return ret;
  65.  }
  66.  
  67. int select_channel_layout(AVCodec *codec)
  68. {
  69.     const uint64_t *p;
  70.     uint64_t best_ch_layout = 0;
  71.     int best_nb_channels   = 0;
  72.  
  73.     if (!codec->channel_layouts)
  74.         return AV_CH_LAYOUT_STEREO;
  75.  
  76.     p = codec->channel_layouts;
  77.     while (*p) {
  78.         int nb_channels = av_get_channel_layout_nb_channels(*p);
  79.  
  80.         if (nb_channels > best_nb_channels) {
  81.             best_ch_layout    = *p;
  82.             best_nb_channels = nb_channels;
  83.         }
  84.         p++;
  85.     }
  86.     return best_ch_layout;
  87. }
  88.  
  89. int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
  90. {
  91.     const enum AVSampleFormat *p = codec->sample_fmts;
  92.  
  93.     while (*p != AV_SAMPLE_FMT_NONE) {
  94.         if (*p == sample_fmt)
  95.             return 1;
  96.         p++;
  97.     }
  98.     return 0;
  99. }
  100. int select_sample_rate(AVCodec *codec)
  101. {
  102.     const int *p;
  103.     int best_samplerate = 0;
  104.  
  105.     if (!codec->supported_samplerates)
  106.         return 44100;
  107.  
  108.     p = codec->supported_samplerates;
  109.     while (*p) {
  110.         best_samplerate = FFMAX(*p, best_samplerate);
  111.         p++;
  112.     }
  113.     return best_samplerate;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement