annotate fifo.c @ 116:d76a36742464 libavutil

Change license headers to say 'FFmpeg' instead of 'this program/this library' and fix GPL/LGPL version mismatches.
author diego
date Sat, 07 Oct 2006 15:30:46 +0000
parents 813b7366ac3f
children a8a9454b0874
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
1 /*
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
2 * A very simple circular buffer FIFO implementation
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
4 * Copyright (c) 2006 Roman Shaposhnik
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
5 *
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 110
diff changeset
6 * This file is part of FFmpeg.
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 110
diff changeset
7 *
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 110
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
110
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 110
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
110
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
12 *
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 110
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
110
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
16 * Lesser General Public License for more details.
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
17 *
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
116
d76a36742464 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 110
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
110
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
21 */
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
22 #include "common.h"
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
23 #include "fifo.h"
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
24
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
25 int av_fifo_init(AVFifoBuffer *f, int size)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
26 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
27 f->buffer = av_malloc(size);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
28 if (!f->buffer)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
29 return -1;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
30 f->end = f->buffer + size;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
31 f->wptr = f->rptr = f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
32 return 0;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
33 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
34
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
35 void av_fifo_free(AVFifoBuffer *f)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
36 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
37 av_free(f->buffer);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
38 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
39
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
40 int av_fifo_size(AVFifoBuffer *f)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
41 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
42 int size = f->wptr - f->rptr;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
43 if (size < 0)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
44 size += f->end - f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
45 return size;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
46 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
47
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
48 /**
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
49 * Get data from the fifo (returns -1 if not enough data).
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
50 */
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
51 int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
52 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
53 int len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
54 int size = f->wptr - f->rptr;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
55 if (size < 0)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
56 size += f->end - f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
57
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
58 if (size < buf_size)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
59 return -1;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
60 while (buf_size > 0) {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
61 len = FFMIN(f->end - f->rptr, buf_size);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
62 memcpy(buf, f->rptr, len);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
63 buf += len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
64 f->rptr += len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
65 if (f->rptr >= f->end)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
66 f->rptr = f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
67 buf_size -= len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
68 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
69 return 0;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
70 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
71
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
72 /**
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
73 * Resizes a FIFO.
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
74 */
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
75 void av_fifo_realloc(AVFifoBuffer *f, unsigned int new_size) {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
76 unsigned int old_size= f->end - f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
77
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
78 if(old_size < new_size){
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
79 uint8_t *old= f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
80
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
81 f->buffer= av_realloc(f->buffer, new_size);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
82
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
83 f->rptr += f->buffer - old;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
84 f->wptr += f->buffer - old;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
85
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
86 if(f->wptr < f->rptr){
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
87 memmove(f->rptr + new_size - old_size, f->rptr, f->buffer + old_size - f->rptr);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
88 f->rptr += new_size - old_size;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
89 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
90 f->end= f->buffer + new_size;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
91 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
92 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
93
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
94 void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
95 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
96 int len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
97
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
98 while (size > 0) {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
99 len = FFMIN(f->end - f->wptr, size);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
100 memcpy(f->wptr, buf, len);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
101 f->wptr += len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
102 if (f->wptr >= f->end)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
103 f->wptr = f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
104 buf += len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
105 size -= len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
106 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
107 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
108
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
109
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
110 /* get data from the fifo (return -1 if not enough data) */
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
111 int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
112 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
113 int len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
114 int size = f->wptr - f->rptr;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
115 if (size < 0)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
116 size += f->end - f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
117
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
118 if (size < buf_size)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
119 return -1;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
120 while (buf_size > 0) {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
121 len = FFMIN(f->end - f->rptr, buf_size);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
122 func(dest, f->rptr, len);
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
123 f->rptr += len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
124 if (f->rptr >= f->end)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
125 f->rptr = f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
126 buf_size -= len;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
127 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
128 return 0;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
129 }
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
130
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
131 /* discard data from the fifo */
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
132 void av_fifo_drain(AVFifoBuffer *f, int size)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
133 {
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
134 f->rptr += size;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
135 if (f->rptr >= f->end)
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
136 f->rptr -= f->end - f->buffer;
813b7366ac3f * Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
diff changeset
137 }