Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) Stefano Sabatini | stefasab at gmail.com
  3.  * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
  4.  *
  5.  * This file is part of FFmpeg.
  6.  *
  7.  * FFmpeg is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * FFmpeg is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with FFmpeg; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  */
  21.  
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/common.h"
  25. #include "libavcodec/avcodec.h"
  26.  
  27. #include "audio.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30.  
  31. #if FF_API_AVFILTERBUFFER
  32. FF_DISABLE_DEPRECATION_WARNINGS
  33. int avfilter_ref_get_channels(AVFilterBufferRef *ref)
  34. {
  35.     return ref->audio ? ref->audio->channels : 0;
  36. }
  37. FF_ENABLE_DEPRECATION_WARNINGS
  38. #endif
  39.  
  40. AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
  41. {
  42.     return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
  43. }
  44.  
  45. AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
  46. {
  47.     AVFrame *frame = av_frame_alloc();
  48.     int channels = link->channels;
  49.     int ret;
  50.  
  51.     av_assert0(channels == av_get_channel_layout_nb_channels(link->channel_layout) || !av_get_channel_layout_nb_channels(link->channel_layout));
  52.  
  53.     if (!frame)
  54.         return NULL;
  55.  
  56.     frame->nb_samples     = nb_samples;
  57.     frame->format         = link->format;
  58.     av_frame_set_channels(frame, link->channels);
  59.     frame->channel_layout = link->channel_layout;
  60.     frame->sample_rate    = link->sample_rate;
  61.     ret = av_frame_get_buffer(frame, 0);
  62.     if (ret < 0) {
  63.         av_frame_free(&frame);
  64.         return NULL;
  65.     }
  66.  
  67.     av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
  68.                            link->format);
  69.  
  70.  
  71.     return frame;
  72. }
  73.  
  74. AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
  75. {
  76.     AVFrame *ret = NULL;
  77.  
  78.     if (link->dstpad->get_audio_buffer)
  79.         ret = link->dstpad->get_audio_buffer(link, nb_samples);
  80.  
  81.     if (!ret)
  82.         ret = ff_default_get_audio_buffer(link, nb_samples);
  83.  
  84.     return ret;
  85. }
  86.  
  87. #if FF_API_AVFILTERBUFFER
  88. FF_DISABLE_DEPRECATION_WARNINGS
  89. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
  90.                                                                       int linesize,int perms,
  91.                                                                       int nb_samples,
  92.                                                                       enum AVSampleFormat sample_fmt,
  93.                                                                       int channels,
  94.                                                                       uint64_t channel_layout)
  95. {
  96.     int planes;
  97.     AVFilterBuffer    *samples    = av_mallocz(sizeof(*samples));
  98.     AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  99.  
  100.     if (!samples || !samplesref)
  101.         goto fail;
  102.  
  103.     av_assert0(channels);
  104.     av_assert0(channel_layout == 0 ||
  105.                channels == av_get_channel_layout_nb_channels(channel_layout));
  106.  
  107.     samplesref->buf         = samples;
  108.     samplesref->buf->free   = ff_avfilter_default_free_buffer;
  109.     if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  110.         goto fail;
  111.  
  112.     samplesref->audio->nb_samples     = nb_samples;
  113.     samplesref->audio->channel_layout = channel_layout;
  114.     samplesref->audio->channels       = channels;
  115.  
  116.     planes = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
  117.  
  118.     /* make sure the buffer gets read permission or it's useless for output */
  119.     samplesref->perms = perms | AV_PERM_READ;
  120.  
  121.     samples->refcount  = 1;
  122.     samplesref->type   = AVMEDIA_TYPE_AUDIO;
  123.     samplesref->format = sample_fmt;
  124.  
  125.     memcpy(samples->data, data,
  126.            FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  127.     memcpy(samplesref->data, samples->data, sizeof(samples->data));
  128.  
  129.     samples->linesize[0] = samplesref->linesize[0] = linesize;
  130.  
  131.     if (planes > FF_ARRAY_ELEMS(samples->data)) {
  132.         samples->   extended_data = av_mallocz_array(sizeof(*samples->extended_data),
  133.                                                planes);
  134.         samplesref->extended_data = av_mallocz_array(sizeof(*samplesref->extended_data),
  135.                                                planes);
  136.  
  137.         if (!samples->extended_data || !samplesref->extended_data)
  138.             goto fail;
  139.  
  140.         memcpy(samples->   extended_data, data, sizeof(*data)*planes);
  141.         memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  142.     } else {
  143.         samples->extended_data    = samples->data;
  144.         samplesref->extended_data = samplesref->data;
  145.     }
  146.  
  147.     samplesref->pts = AV_NOPTS_VALUE;
  148.  
  149.     return samplesref;
  150.  
  151. fail:
  152.     if (samples && samples->extended_data != samples->data)
  153.         av_freep(&samples->extended_data);
  154.     if (samplesref) {
  155.         av_freep(&samplesref->audio);
  156.         if (samplesref->extended_data != samplesref->data)
  157.             av_freep(&samplesref->extended_data);
  158.     }
  159.     av_freep(&samplesref);
  160.     av_freep(&samples);
  161.     return NULL;
  162. }
  163.  
  164. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  165.                                                              int linesize,int perms,
  166.                                                              int nb_samples,
  167.                                                              enum AVSampleFormat sample_fmt,
  168.                                                              uint64_t channel_layout)
  169. {
  170.     int channels = av_get_channel_layout_nb_channels(channel_layout);
  171.     return avfilter_get_audio_buffer_ref_from_arrays_channels(data, linesize, perms,
  172.                                                               nb_samples, sample_fmt,
  173.                                                               channels, channel_layout);
  174. }
  175. FF_ENABLE_DEPRECATION_WARNINGS
  176. #endif
  177.