libavfilter/audio.c
0b45334a
 /*
01590329
  * Copyright (c) Stefano Sabatini | stefasab at gmail.com
  * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
0b45334a
  *
01590329
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
0b45334a
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
01590329
  * FFmpeg is distributed in the hope that it will be useful,
0b45334a
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
01590329
  * License along with FFmpeg; if not, write to the Free Software
0b45334a
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
01590329
 #include "libavutil/avassert.h"
a903f8f0
 #include "libavutil/channel_layout.h"
1d9c2dc8
 #include "libavutil/common.h"
0b45334a
 
 #include "audio.h"
 #include "avfilter.h"
 #include "internal.h"
 
e1f49712
 #define BUFFER_ALIGN 0
 
 
7e350379
 AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
0b45334a
 {
7e350379
     return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
0b45334a
 }
 
7e350379
 AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
0b45334a
 {
e1f49712
     AVFrame *frame = NULL;
a05a44e2
     int channels = link->channels;
7e350379
 
a05a44e2
     av_assert0(channels == av_get_channel_layout_nb_channels(link->channel_layout) || !av_get_channel_layout_nb_channels(link->channel_layout));
 
e1f49712
     if (!link->frame_pool) {
         link->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
                                                     nb_samples, link->format, BUFFER_ALIGN);
         if (!link->frame_pool)
             return NULL;
     } else {
         int pool_channels = 0;
         int pool_nb_samples = 0;
         int pool_align = 0;
         enum AVSampleFormat pool_format = AV_SAMPLE_FMT_NONE;
 
         if (ff_frame_pool_get_audio_config(link->frame_pool,
                                            &pool_channels, &pool_nb_samples,
                                            &pool_format, &pool_align) < 0) {
             return NULL;
         }
 
         if (pool_channels != channels || pool_nb_samples < nb_samples ||
             pool_format != link->format || pool_align != BUFFER_ALIGN) {
 
             ff_frame_pool_uninit((FFFramePool **)&link->frame_pool);
             link->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
                                                         nb_samples, link->format, BUFFER_ALIGN);
             if (!link->frame_pool)
                 return NULL;
         }
     }
 
     frame = ff_frame_pool_get(link->frame_pool);
7e350379
     if (!frame)
         return NULL;
 
e1f49712
     frame->nb_samples = nb_samples;
7e350379
     frame->channel_layout = link->channel_layout;
e1f49712
     frame->sample_rate = link->sample_rate;
c262ddb8
 
e1f49712
     av_samples_set_silence(frame->extended_data, 0, nb_samples, channels, link->format);
0b45334a
 
c9c7bc44
     return frame;
0b45334a
 }
 
7e350379
 AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
0b45334a
 {
7e350379
     AVFrame *ret = NULL;
0b45334a
 
     if (link->dstpad->get_audio_buffer)
7e350379
         ret = link->dstpad->get_audio_buffer(link, nb_samples);
0b45334a
 
     if (!ret)
7e350379
         ret = ff_default_get_audio_buffer(link, nb_samples);
0b45334a
 
     return ret;
 }