LCOV - code coverage report
Current view: top level - libavutil - audio_fifo.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 82 110 74.5 %
Date: 2017-10-18 21:45:51 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /*
       2             :  * Audio FIFO
       3             :  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
       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             : /**
      23             :  * @file
      24             :  * Audio FIFO
      25             :  */
      26             : 
      27             : #include "avutil.h"
      28             : #include "audio_fifo.h"
      29             : #include "common.h"
      30             : #include "fifo.h"
      31             : #include "mem.h"
      32             : #include "samplefmt.h"
      33             : 
      34             : struct AVAudioFifo {
      35             :     AVFifoBuffer **buf;             /**< single buffer for interleaved, per-channel buffers for planar */
      36             :     int nb_buffers;                 /**< number of buffers */
      37             :     int nb_samples;                 /**< number of samples currently in the FIFO */
      38             :     int allocated_samples;          /**< current allocated size, in samples */
      39             : 
      40             :     int channels;                   /**< number of channels */
      41             :     enum AVSampleFormat sample_fmt; /**< sample format */
      42             :     int sample_size;                /**< size, in bytes, of one sample in a buffer */
      43             : };
      44             : 
      45         101 : void av_audio_fifo_free(AVAudioFifo *af)
      46             : {
      47         101 :     if (af) {
      48          99 :         if (af->buf) {
      49             :             int i;
      50         267 :             for (i = 0; i < af->nb_buffers; i++) {
      51         168 :                 av_fifo_freep(&af->buf[i]);
      52             :             }
      53          99 :             av_freep(&af->buf);
      54             :         }
      55          99 :         av_free(af);
      56             :     }
      57         101 : }
      58             : 
      59          99 : AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
      60             :                                  int nb_samples)
      61             : {
      62             :     AVAudioFifo *af;
      63             :     int buf_size, i;
      64             : 
      65             :     /* get channel buffer size (also validates parameters) */
      66          99 :     if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0)
      67           0 :         return NULL;
      68             : 
      69          99 :     af = av_mallocz(sizeof(*af));
      70          99 :     if (!af)
      71           0 :         return NULL;
      72             : 
      73          99 :     af->channels    = channels;
      74          99 :     af->sample_fmt  = sample_fmt;
      75          99 :     af->sample_size = buf_size / nb_samples;
      76          99 :     af->nb_buffers  = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
      77             : 
      78          99 :     af->buf = av_mallocz_array(af->nb_buffers, sizeof(*af->buf));
      79          99 :     if (!af->buf)
      80           0 :         goto error;
      81             : 
      82         267 :     for (i = 0; i < af->nb_buffers; i++) {
      83         168 :         af->buf[i] = av_fifo_alloc(buf_size);
      84         168 :         if (!af->buf[i])
      85           0 :             goto error;
      86             :     }
      87          99 :     af->allocated_samples = nb_samples;
      88             : 
      89          99 :     return af;
      90             : 
      91           0 : error:
      92           0 :     av_audio_fifo_free(af);
      93           0 :     return NULL;
      94             : }
      95             : 
      96          10 : int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
      97             : {
      98             :     int i, ret, buf_size;
      99             : 
     100          10 :     if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
     101             :                                           af->sample_fmt, 1)) < 0)
     102           0 :         return ret;
     103             : 
     104          23 :     for (i = 0; i < af->nb_buffers; i++) {
     105          13 :         if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
     106           0 :             return ret;
     107             :     }
     108          10 :     af->allocated_samples = nb_samples;
     109          10 :     return 0;
     110             : }
     111             : 
     112        6894 : int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
     113             : {
     114             :     int i, ret, size;
     115             : 
     116             :     /* automatically reallocate buffers if needed */
     117        6894 :     if (av_audio_fifo_space(af) < nb_samples) {
     118           8 :         int current_size = av_audio_fifo_size(af);
     119             :         /* check for integer overflow in new size calculation */
     120           8 :         if (INT_MAX / 2 - current_size < nb_samples)
     121           0 :             return AVERROR(EINVAL);
     122             :         /* reallocate buffers */
     123           8 :         if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0)
     124           0 :             return ret;
     125             :     }
     126             : 
     127        6894 :     size = nb_samples * af->sample_size;
     128       18729 :     for (i = 0; i < af->nb_buffers; i++) {
     129       11835 :         ret = av_fifo_generic_write(af->buf[i], data[i], size, NULL);
     130       11835 :         if (ret != size)
     131           0 :             return AVERROR_BUG;
     132             :     }
     133        6894 :     af->nb_samples += nb_samples;
     134             : 
     135        6894 :     return nb_samples;
     136             : }
     137             : 
     138           6 : int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
     139             : {
     140             :     int i, ret, size;
     141             : 
     142           6 :     if (nb_samples < 0)
     143           0 :         return AVERROR(EINVAL);
     144           6 :     nb_samples = FFMIN(nb_samples, af->nb_samples);
     145           6 :     if (!nb_samples)
     146           0 :         return 0;
     147             : 
     148           6 :     size = nb_samples * af->sample_size;
     149          15 :     for (i = 0; i < af->nb_buffers; i++) {
     150           9 :         if ((ret = av_fifo_generic_peek(af->buf[i], data[i], size, NULL)) < 0)
     151           0 :             return AVERROR_BUG;
     152             :     }
     153             : 
     154           6 :     return nb_samples;
     155             : }
     156             : 
     157          54 : int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
     158             : {
     159             :     int i, ret, size;
     160             : 
     161          54 :     if (offset < 0 || offset >= af->nb_samples)
     162           0 :         return AVERROR(EINVAL);
     163          54 :     if (nb_samples < 0)
     164           0 :         return AVERROR(EINVAL);
     165          54 :     nb_samples = FFMIN(nb_samples, af->nb_samples);
     166          54 :     if (!nb_samples)
     167           0 :         return 0;
     168          54 :     if (offset > af->nb_samples - nb_samples)
     169           0 :         return AVERROR(EINVAL);
     170             : 
     171          54 :     offset *= af->sample_size;
     172          54 :     size = nb_samples * af->sample_size;
     173         126 :     for (i = 0; i < af->nb_buffers; i++) {
     174          72 :         if ((ret = av_fifo_generic_peek_at(af->buf[i], data[i], offset, size, NULL)) < 0)
     175           0 :             return AVERROR_BUG;
     176             :     }
     177             : 
     178          54 :     return nb_samples;
     179             : }
     180             : 
     181       28498 : int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
     182             : {
     183             :     int i, size;
     184             : 
     185       28498 :     if (nb_samples < 0)
     186           0 :         return AVERROR(EINVAL);
     187       28498 :     nb_samples = FFMIN(nb_samples, af->nb_samples);
     188       28498 :     if (!nb_samples)
     189       26196 :         return 0;
     190             : 
     191        2302 :     size = nb_samples * af->sample_size;
     192        4607 :     for (i = 0; i < af->nb_buffers; i++) {
     193        2305 :         if (av_fifo_generic_read(af->buf[i], data[i], size, NULL) < 0)
     194           0 :             return AVERROR_BUG;
     195             :     }
     196        2302 :     af->nb_samples -= nb_samples;
     197             : 
     198        2302 :     return nb_samples;
     199             : }
     200             : 
     201           6 : int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
     202             : {
     203             :     int i, size;
     204             : 
     205           6 :     if (nb_samples < 0)
     206           0 :         return AVERROR(EINVAL);
     207           6 :     nb_samples = FFMIN(nb_samples, af->nb_samples);
     208             : 
     209           6 :     if (nb_samples) {
     210           6 :         size = nb_samples * af->sample_size;
     211          15 :         for (i = 0; i < af->nb_buffers; i++)
     212           9 :             av_fifo_drain(af->buf[i], size);
     213           6 :         af->nb_samples -= nb_samples;
     214             :     }
     215           6 :     return 0;
     216             : }
     217             : 
     218           0 : void av_audio_fifo_reset(AVAudioFifo *af)
     219             : {
     220             :     int i;
     221             : 
     222           0 :     for (i = 0; i < af->nb_buffers; i++)
     223           0 :         av_fifo_reset(af->buf[i]);
     224             : 
     225           0 :     af->nb_samples = 0;
     226           0 : }
     227             : 
     228      115503 : int av_audio_fifo_size(AVAudioFifo *af)
     229             : {
     230      115503 :     return af->nb_samples;
     231             : }
     232             : 
     233        7412 : int av_audio_fifo_space(AVAudioFifo *af)
     234             : {
     235        7412 :     return af->allocated_samples - af->nb_samples;
     236             : }

Generated by: LCOV version 1.13