LCOV - code coverage report
Current view: top level - src/libavfilter - bufferqueue.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 18 22 81.8 %
Date: 2017-06-24 07:01:58 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Generic buffer queue
       3             :  * Copyright (c) 2012 Nicolas George
       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             : #ifndef AVFILTER_BUFFERQUEUE_H
      23             : #define AVFILTER_BUFFERQUEUE_H
      24             : 
      25             : /**
      26             :  * FFBufQueue: simple AVFrame queue API
      27             :  *
      28             :  * Note: this API is not thread-safe. Concurrent access to the same queue
      29             :  * must be protected by a mutex or any synchronization mechanism.
      30             :  */
      31             : 
      32             : /**
      33             :  * Maximum size of the queue.
      34             :  *
      35             :  * This value can be overridden by definying it before including this
      36             :  * header.
      37             :  * Powers of 2 are recommended.
      38             :  */
      39             : #ifndef FF_BUFQUEUE_SIZE
      40             : #define FF_BUFQUEUE_SIZE 64
      41             : #endif
      42             : 
      43             : #include "avfilter.h"
      44             : #include "libavutil/avassert.h"
      45             : 
      46             : /**
      47             :  * Structure holding the queue
      48             :  */
      49             : struct FFBufQueue {
      50             :     AVFrame *queue[FF_BUFQUEUE_SIZE];
      51             :     unsigned short head;
      52             :     unsigned short available; /**< number of available buffers */
      53             : };
      54             : 
      55             : #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE]
      56             : 
      57             : /**
      58             :  * Test if a buffer queue is full.
      59             :  */
      60        7482 : static inline int ff_bufqueue_is_full(struct FFBufQueue *queue)
      61             : {
      62        7482 :     return queue->available == FF_BUFQUEUE_SIZE;
      63             : }
      64             : 
      65             : /**
      66             :  * Add a buffer to the queue.
      67             :  *
      68             :  * If the queue is already full, then the current last buffer is dropped
      69             :  * (and unrefed) with a warning before adding the new buffer.
      70             :  */
      71        7222 : static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue,
      72             :                                    AVFrame *buf)
      73             : {
      74        7222 :     if (ff_bufqueue_is_full(queue)) {
      75           0 :         av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
      76           0 :         av_frame_free(&BUCKET(--queue->available));
      77             :     }
      78        7222 :     BUCKET(queue->available++) = buf;
      79        7222 : }
      80             : 
      81             : /**
      82             :  * Get a buffer from the queue without altering it.
      83             :  *
      84             :  * Buffer with index 0 is the first buffer in the queue.
      85             :  * Return NULL if the queue has not enough buffers.
      86             :  */
      87        1822 : static inline AVFrame *ff_bufqueue_peek(struct FFBufQueue *queue,
      88             :                                         unsigned index)
      89             : {
      90        1822 :     return index < queue->available ? BUCKET(index) : NULL;
      91             : }
      92             : 
      93             : /**
      94             :  * Get the first buffer from the queue and remove it.
      95             :  *
      96             :  * Do not use on an empty queue.
      97             :  */
      98        7222 : static inline AVFrame *ff_bufqueue_get(struct FFBufQueue *queue)
      99             : {
     100        7222 :     AVFrame *ret = queue->queue[queue->head];
     101        7222 :     av_assert0(queue->available);
     102        7222 :     queue->available--;
     103        7222 :     queue->queue[queue->head] = NULL;
     104        7222 :     queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE;
     105        7222 :     return ret;
     106             : }
     107             : 
     108             : /**
     109             :  * Unref and remove all buffers from the queue.
     110             :  */
     111          66 : static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue)
     112             : {
     113         132 :     while (queue->available) {
     114           0 :         AVFrame *buf = ff_bufqueue_get(queue);
     115           0 :         av_frame_free(&buf);
     116             :     }
     117          66 : }
     118             : 
     119             : #undef BUCKET
     120             : 
     121             : #endif /* AVFILTER_BUFFERQUEUE_H */

Generated by: LCOV version 1.13