Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 539 lines (481 sloc) 17.009 kB
de6d9b6 Initial revision
Fabrice Bellard authored
1 /*
85a6b01 @bcoudurier split ffm de/muxer
bcoudurier authored
2 * FFM (ffserver live feed) demuxer
406792e @DonDiego cosmetics: Remove pointless period after copyright statement non-sent…
DonDiego authored
3 * Copyright (c) 2001 Fabrice Bellard
de6d9b6 Initial revision
Fabrice Bellard authored
4 *
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
66d2ff2 fixed memory free (no free necessary for private data) - added better…
Fabrice Bellard authored
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
10 * version 2.1 of the License, or (at your option) any later version.
de6d9b6 Initial revision
Fabrice Bellard authored
11 *
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b6 Initial revision
Fabrice Bellard authored
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66d2ff2 fixed memory free (no free necessary for private data) - added better…
Fabrice Bellard authored
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b6 Initial revision
Fabrice Bellard authored
16 *
66d2ff2 fixed memory free (no free necessary for private data) - added better…
Fabrice Bellard authored
17 * You should have received a copy of the GNU Lesser General Public
b78e719 @DonDiego Change license headers to say 'FFmpeg' instead of 'this program/this …
DonDiego authored
18 * License along with FFmpeg; if not, write to the Free Software
5509bff @DonDiego Update licensing information: The FSF changed postal address.
DonDiego authored
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b6 Initial revision
Fabrice Bellard authored
20 */
85a6b01 @bcoudurier split ffm de/muxer
bcoudurier authored
21
6a5d31a @DonDiego Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
DonDiego authored
22 #include "libavutil/intreadwrite.h"
3383a53 @mansr lavu: replace int/float punning functions
mansr authored
23 #include "libavutil/intfloat.h"
de6d9b6 Initial revision
Fabrice Bellard authored
24 #include "avformat.h"
c3f9ebf @elenril lavf: make av_set_pts_info private.
elenril authored
25 #include "internal.h"
85a6b01 @bcoudurier split ffm de/muxer
bcoudurier authored
26 #include "ffm.h"
e37f161 @michaelni Merge remote-tracking branch 'qatar/master'
michaelni authored
27 #include "avio_internal.h"
b250f9c @aurelj Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurelj authored
28 #if CONFIG_FFSERVER
8be1c65 win32 fixes
Fabrice Bellard authored
29 #include <unistd.h>
de6d9b6 Initial revision
Fabrice Bellard authored
30
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
31 int64_t ffm_read_write_index(int fd)
278f987 @bcoudurier move ffserver specific functions up and only include unistd.h in this…
bcoudurier authored
32 {
33 uint8_t buf[8];
34
35 lseek(fd, 8, SEEK_SET);
2dc9188 @bcoudurier return error if read failed
bcoudurier authored
36 if (read(fd, buf, 8) != 8)
37 return AVERROR(EIO);
278f987 @bcoudurier move ffserver specific functions up and only include unistd.h in this…
bcoudurier authored
38 return AV_RB64(buf);
39 }
40
6fcce4f Change ffm_write_write_index to return int, and return if error occured.
Patrik Kullman authored
41 int ffm_write_write_index(int fd, int64_t pos)
278f987 @bcoudurier move ffserver specific functions up and only include unistd.h in this…
bcoudurier authored
42 {
43 uint8_t buf[8];
44 int i;
45
46 for(i=0;i<8;i++)
47 buf[i] = (pos >> (56 - i * 8)) & 0xff;
48 lseek(fd, 8, SEEK_SET);
6fcce4f Change ffm_write_write_index to return int, and return if error occured.
Patrik Kullman authored
49 if (write(fd, buf, 8) != 8)
50 return AVERROR(EIO);
51 return 8;
278f987 @bcoudurier move ffserver specific functions up and only include unistd.h in this…
bcoudurier authored
52 }
53
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
54 void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
278f987 @bcoudurier move ffserver specific functions up and only include unistd.h in this…
bcoudurier authored
55 {
56 FFMContext *ffm = s->priv_data;
57 ffm->write_index = pos;
58 ffm->file_size = file_size;
59 }
60 #endif // CONFIG_FFSERVER
61
de6d9b6 Initial revision
Fabrice Bellard authored
62 static int ffm_is_avail_data(AVFormatContext *s, int size)
63 {
64 FFMContext *ffm = s->priv_data;
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
65 int64_t pos, avail_size;
de6d9b6 Initial revision
Fabrice Bellard authored
66 int len;
67
68 len = ffm->packet_end - ffm->packet_ptr;
ba26712 @bcoudurier reenable test, data is available in this case
bcoudurier authored
69 if (size <= len)
70 return 1;
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
71 pos = avio_tell(s->pb);
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
72 if (!ffm->write_index) {
21c6438 @bcoudurier typo, fix eof check
bcoudurier authored
73 if (pos == ffm->file_size)
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
74 return AVERROR_EOF;
75 avail_size = ffm->file_size - pos;
76 } else {
de6d9b6 Initial revision
Fabrice Bellard authored
77 if (pos == ffm->write_index) {
78 /* exactly at the end of stream */
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
79 return AVERROR(EAGAIN);
de6d9b6 Initial revision
Fabrice Bellard authored
80 } else if (pos < ffm->write_index) {
81 avail_size = ffm->write_index - pos;
82 } else {
83 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
84 }
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
85 }
de6d9b6 Initial revision
Fabrice Bellard authored
86 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
87 if (size <= avail_size)
88 return 1;
89 else
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
90 return AVERROR(EAGAIN);
de6d9b6 Initial revision
Fabrice Bellard authored
91 }
92
7c45723 @bcoudurier resync ffm if error in stream
bcoudurier authored
93 static int ffm_resync(AVFormatContext *s, int state)
94 {
95 av_log(s, AV_LOG_ERROR, "resyncing\n");
96 while (state != PACKET_ID) {
97 if (url_feof(s->pb)) {
98 av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
99 return -1;
100 }
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
101 state = (state << 8) | avio_r8(s->pb);
7c45723 @bcoudurier resync ffm if error in stream
bcoudurier authored
102 }
103 return 0;
104 }
105
de6d9b6 Initial revision
Fabrice Bellard authored
106 /* first is true if we read the frame header */
4606ac8 * some minor modification by Philip Gladston
Zdenek Kabelac authored
107 static int ffm_read_data(AVFormatContext *s,
b30bb53 @bcoudurier cosmetics, rename first to header, more explicit name
bcoudurier authored
108 uint8_t *buf, int size, int header)
de6d9b6 Initial revision
Fabrice Bellard authored
109 {
110 FFMContext *ffm = s->priv_data;
471fe57 @elenril avio: rename ByteIOContext to AVIOContext.
elenril authored
111 AVIOContext *pb = s->pb;
7c45723 @bcoudurier resync ffm if error in stream
bcoudurier authored
112 int len, fill_size, size1, frame_offset, id;
de6d9b6 Initial revision
Fabrice Bellard authored
113
114 size1 = size;
115 while (size > 0) {
116 redo:
117 len = ffm->packet_end - ffm->packet_ptr;
e5ece18 @bcoudurier return error if len is negative, prevent segfault
bcoudurier authored
118 if (len < 0)
119 return -1;
de6d9b6 Initial revision
Fabrice Bellard authored
120 if (len > size)
121 len = size;
122 if (len == 0) {
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
123 if (avio_tell(pb) == ffm->file_size)
f59d8ff @elenril avio: avio_ prefix for url_fseek
elenril authored
124 avio_seek(pb, ffm->packet_size, SEEK_SET);
5e57424 Fix the WAIT_FEED problem. It turns out that when you open up an FFM
Philip Gladstone authored
125 retry_read:
8ba987b ffmdec: set avio buffer to ffm->packet_size, avoid dirty reads
Yan Jing authored
126 if (pb->buffer_size != ffm->packet_size) {
127 int64_t tell = avio_tell(pb);
e37f161 @michaelni Merge remote-tracking branch 'qatar/master'
michaelni authored
128 ffio_set_buf_size(pb, ffm->packet_size);
8ba987b ffmdec: set avio buffer to ffm->packet_size, avoid dirty reads
Yan Jing authored
129 avio_seek(pb, tell, SEEK_SET);
130 }
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
131 id = avio_rb16(pb); /* PACKET_ID */
7c45723 @bcoudurier resync ffm if error in stream
bcoudurier authored
132 if (id != PACKET_ID)
133 if (ffm_resync(s, id) < 0)
134 return -1;
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
135 fill_size = avio_rb16(pb);
136 ffm->dts = avio_rb64(pb);
137 frame_offset = avio_rb16(pb);
138 avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
de6d9b6 Initial revision
Fabrice Bellard authored
139 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
eea2f03 @bcoudurier return error if frame_offset is negative, prevent segfault
bcoudurier authored
140 if (ffm->packet_end < ffm->packet || frame_offset < 0)
71c32f1 @michaelni do not call (av_)abort()
michaelni authored
141 return -1;
de6d9b6 Initial revision
Fabrice Bellard authored
142 /* if first packet or resynchronization packet, we must
143 handle it specifically */
144 if (ffm->first_packet || (frame_offset & 0x8000)) {
5e57424 Fix the WAIT_FEED problem. It turns out that when you open up an FFM
Philip Gladstone authored
145 if (!frame_offset) {
146 /* This packet has no frame headers in it */
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
147 if (avio_tell(pb) >= ffm->packet_size * 3) {
f59d8ff @elenril avio: avio_ prefix for url_fseek
elenril authored
148 avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
5e57424 Fix the WAIT_FEED problem. It turns out that when you open up an FFM
Philip Gladstone authored
149 goto retry_read;
150 }
151 /* This is bad, we cannot find a valid frame header */
152 return 0;
153 }
de6d9b6 Initial revision
Fabrice Bellard authored
154 ffm->first_packet = 0;
a077f3b @bcoudurier typo
bcoudurier authored
155 if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
71c32f1 @michaelni do not call (av_)abort()
michaelni authored
156 return -1;
de6d9b6 Initial revision
Fabrice Bellard authored
157 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
b30bb53 @bcoudurier cosmetics, rename first to header, more explicit name
bcoudurier authored
158 if (!header)
de6d9b6 Initial revision
Fabrice Bellard authored
159 break;
160 } else {
161 ffm->packet_ptr = ffm->packet;
162 }
163 goto redo;
164 }
165 memcpy(buf, ffm->packet_ptr, len);
166 buf += len;
167 ffm->packet_ptr += len;
168 size -= len;
b30bb53 @bcoudurier cosmetics, rename first to header, more explicit name
bcoudurier authored
169 header = 0;
de6d9b6 Initial revision
Fabrice Bellard authored
170 }
171 return size1 - size;
172 }
173
92a0f33 @bcoudurier fix valid seeking range
bcoudurier authored
174 /* ensure that acutal seeking happens between FFM_PACKET_SIZE
175 and file_size - FFM_PACKET_SIZE */
928cfc7 @elupus [ffmdec] Check return value of avio_seek and avoid modifying state if…
elupus authored
176 static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
a1e0130 @bcoudurier move ffm_seek1 before get_pts function since func uses it
bcoudurier authored
177 {
178 FFMContext *ffm = s->priv_data;
471fe57 @elenril avio: rename ByteIOContext to AVIOContext.
elenril authored
179 AVIOContext *pb = s->pb;
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
180 int64_t pos;
a1e0130 @bcoudurier move ffm_seek1 before get_pts function since func uses it
bcoudurier authored
181
92a0f33 @bcoudurier fix valid seeking range
bcoudurier authored
182 pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
183 pos = FFMAX(pos, FFM_PACKET_SIZE);
919d7a3 @DonDiego Replace DEBUG_SEEK/DEBUG_SI + av_log combinations by av_dlog.
DonDiego authored
184 av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
928cfc7 @elupus [ffmdec] Check return value of avio_seek and avoid modifying state if…
elupus authored
185 return avio_seek(pb, pos, SEEK_SET);
a1e0130 @bcoudurier move ffm_seek1 before get_pts function since func uses it
bcoudurier authored
186 }
187
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
188 static int64_t get_dts(AVFormatContext *s, int64_t pos)
979b1a0 @bcoudurier move get_pts function to avoid useless declaration
bcoudurier authored
189 {
471fe57 @elenril avio: rename ByteIOContext to AVIOContext.
elenril authored
190 AVIOContext *pb = s->pb;
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
191 int64_t dts;
979b1a0 @bcoudurier move get_pts function to avoid useless declaration
bcoudurier authored
192
193 ffm_seek1(s, pos);
45a8a02 @elenril lavf: replace avio_seek(SEEK_CUR) with avio_skip where it makes sense
elenril authored
194 avio_skip(pb, 4);
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
195 dts = avio_rb64(pb);
919d7a3 @DonDiego Replace DEBUG_SEEK/DEBUG_SI + av_log combinations by av_dlog.
DonDiego authored
196 av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
197 return dts;
979b1a0 @bcoudurier move get_pts function to avoid useless declaration
bcoudurier authored
198 }
de6d9b6 Initial revision
Fabrice Bellard authored
199
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
200 static void adjust_write_index(AVFormatContext *s)
201 {
202 FFMContext *ffm = s->priv_data;
471fe57 @elenril avio: rename ByteIOContext to AVIOContext.
elenril authored
203 AVIOContext *pb = s->pb;
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
204 int64_t pts;
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
205 //int64_t orig_write_index = ffm->write_index;
206 int64_t pos_min, pos_max;
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
207 int64_t pts_start;
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
208 int64_t ptr = avio_tell(pb);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
209
210
211 pos_min = 0;
212 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
213
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
214 pts_start = get_dts(s, pos_min);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
215
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
216 pts = get_dts(s, pos_max);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
217
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
218 if (pts - 100000 > pts_start)
7929251 @michaelni adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TEL…
michaelni authored
219 goto end;
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
220
221 ffm->write_index = FFM_PACKET_SIZE;
222
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
223 pts_start = get_dts(s, pos_min);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
224
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
225 pts = get_dts(s, pos_max);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
226
227 if (pts - 100000 <= pts_start) {
228 while (1) {
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
229 int64_t newpos;
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
230 int64_t newpts;
231
232 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
233
234 if (newpos == pos_min)
235 break;
236
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
237 newpts = get_dts(s, newpos);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
238
239 if (newpts - 100000 <= pts) {
240 pos_max = newpos;
241 pts = newpts;
242 } else {
243 pos_min = newpos;
244 }
245 }
246 ffm->write_index += pos_max;
247 }
248
949b1a1 Replace most of the %lld and %llx by their (cleaner) PRI*64 counterpa…
Steve L'Homme authored
249 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
250 //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
251
7929251 @michaelni adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TEL…
michaelni authored
252 end:
f59d8ff @elenril avio: avio_ prefix for url_fseek
elenril authored
253 avio_seek(pb, ptr, SEEK_SET);
9162842 Add more resilience in reading ffm files. In particular, don't assume
Philip Gladstone authored
254 }
255
256
e36a390 Move ffm_close function up to avoid a forward declaration.
Reimar Döffinger authored
257 static int ffm_close(AVFormatContext *s)
258 {
259 int i;
260
261 for (i = 0; i < s->nb_streams; i++)
262 av_freep(&s->streams[i]->codec->rc_eq);
263
264 return 0;
265 }
266
527b46b ffmdec: Do not manually free streams on read_header error, this is al…
Reimar Döffinger authored
267
6e9651d @elenril lavf: remove AVFormatParameters from AVFormatContext.read_header sign…
elenril authored
268 static int ffm_read_header(AVFormatContext *s)
de6d9b6 Initial revision
Fabrice Bellard authored
269 {
c9a65ca converted to new API
Fabrice Bellard authored
270 FFMContext *ffm = s->priv_data;
de6d9b6 Initial revision
Fabrice Bellard authored
271 AVStream *st;
471fe57 @elenril avio: rename ByteIOContext to AVIOContext.
elenril authored
272 AVIOContext *pb = s->pb;
de6d9b6 Initial revision
Fabrice Bellard authored
273 AVCodecContext *codec;
fa26a29 add av_new_stream() usage
Fabrice Bellard authored
274 int i, nb_streams;
0c1a9ed * UINTX -> uintx_t INTX -> intx_t
Zdenek Kabelac authored
275 uint32_t tag;
de6d9b6 Initial revision
Fabrice Bellard authored
276
277 /* header */
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
278 tag = avio_rl32(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
279 if (tag != MKTAG('F', 'F', 'M', '1'))
280 goto fail;
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
281 ffm->packet_size = avio_rb32(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
282 if (ffm->packet_size != FFM_PACKET_SIZE)
283 goto fail;
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
284 ffm->write_index = avio_rb64(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
285 /* get also filesize */
8978fed @elenril avio: introduce an AVIOContext.seekable field
elenril authored
286 if (pb->seekable) {
db44ea9 @elenril avio: avio_ prefix for url_fsize
elenril authored
287 ffm->file_size = avio_size(pb);
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
288 if (ffm->write_index)
289 adjust_write_index(s);
de6d9b6 Initial revision
Fabrice Bellard authored
290 } else {
8da9266 @mansr use the standard INT64_C() macro for 64-bit constants
mansr authored
291 ffm->file_size = (UINT64_C(1) << 63) - 1;
de6d9b6 Initial revision
Fabrice Bellard authored
292 }
293
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
294 nb_streams = avio_rb32(pb);
295 avio_rb32(pb); /* total bitrate */
de6d9b6 Initial revision
Fabrice Bellard authored
296 /* read each stream */
fa26a29 add av_new_stream() usage
Fabrice Bellard authored
297 for(i=0;i<nb_streams;i++) {
75bdb98 Add the transfer of the new parameters from ffmpeg to ffserver and vi…
Philip Gladstone authored
298 char rc_eq_buf[128];
299
3b3bbdd @elenril lavf,lavd: replace av_new_stream->avformat_new_stream part I.
elenril authored
300 st = avformat_new_stream(s, NULL);
de6d9b6 Initial revision
Fabrice Bellard authored
301 if (!st)
302 goto fail;
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
303
c3f9ebf @elenril lavf: make av_set_pts_info private.
elenril authored
304 avpriv_set_pts_info(st, 64, 1, 1000000);
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
305
01f4895 @michaelni changing AVCodecContext codec -> *codec in AVStream so additions to A…
michaelni authored
306 codec = st->codec;
de6d9b6 Initial revision
Fabrice Bellard authored
307 /* generic info */
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
308 codec->codec_id = avio_rb32(pb);
309 codec->codec_type = avio_r8(pb); /* codec_type */
310 codec->bit_rate = avio_rb32(pb);
311 codec->flags = avio_rb32(pb);
312 codec->flags2 = avio_rb32(pb);
313 codec->debug = avio_rb32(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
314 /* specific info */
315 switch(codec->codec_type) {
72415b2 Define AVMediaType enum, and use it instead of enum CodecType, which
Stefano Sabatini authored
316 case AVMEDIA_TYPE_VIDEO:
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
317 codec->time_base.num = avio_rb32(pb);
318 codec->time_base.den = avio_rb32(pb);
319 codec->width = avio_rb16(pb);
320 codec->height = avio_rb16(pb);
321 codec->gop_size = avio_rb16(pb);
322 codec->pix_fmt = avio_rb32(pb);
323 codec->qmin = avio_r8(pb);
324 codec->qmax = avio_r8(pb);
325 codec->max_qdiff = avio_r8(pb);
326 codec->qcompress = avio_rb16(pb) / 10000.0;
327 codec->qblur = avio_rb16(pb) / 10000.0;
328 codec->bit_rate_tolerance = avio_rb32(pb);
6813450 @richardpl lavf: replace remaining use of deprecated get_strz()
richardpl authored
329 avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
330 codec->rc_eq = av_strdup(rc_eq_buf);
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
331 codec->rc_max_rate = avio_rb32(pb);
332 codec->rc_min_rate = avio_rb32(pb);
333 codec->rc_buffer_size = avio_rb32(pb);
3383a53 @mansr lavu: replace int/float punning functions
mansr authored
334 codec->i_quant_factor = av_int2double(avio_rb64(pb));
335 codec->b_quant_factor = av_int2double(avio_rb64(pb));
336 codec->i_quant_offset = av_int2double(avio_rb64(pb));
337 codec->b_quant_offset = av_int2double(avio_rb64(pb));
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
338 codec->dct_algo = avio_rb32(pb);
339 codec->strict_std_compliance = avio_rb32(pb);
340 codec->max_b_frames = avio_rb32(pb);
341 codec->mpeg_quant = avio_rb32(pb);
342 codec->intra_dc_precision = avio_rb32(pb);
343 codec->me_method = avio_rb32(pb);
344 codec->mb_decision = avio_rb32(pb);
345 codec->nsse_weight = avio_rb32(pb);
346 codec->frame_skip_cmp = avio_rb32(pb);
3383a53 @mansr lavu: replace int/float punning functions
mansr authored
347 codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
348 codec->codec_tag = avio_rb32(pb);
349 codec->thread_count = avio_r8(pb);
350 codec->coder_type = avio_rb32(pb);
351 codec->me_cmp = avio_rb32(pb);
352 codec->me_subpel_quality = avio_rb32(pb);
353 codec->me_range = avio_rb32(pb);
354 codec->keyint_min = avio_rb32(pb);
355 codec->scenechange_threshold = avio_rb32(pb);
356 codec->b_frame_strategy = avio_rb32(pb);
3383a53 @mansr lavu: replace int/float punning functions
mansr authored
357 codec->qcompress = av_int2double(avio_rb64(pb));
358 codec->qblur = av_int2double(avio_rb64(pb));
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
359 codec->max_qdiff = avio_rb32(pb);
360 codec->refs = avio_rb32(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
361 break;
72415b2 Define AVMediaType enum, and use it instead of enum CodecType, which
Stefano Sabatini authored
362 case AVMEDIA_TYPE_AUDIO:
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
363 codec->sample_rate = avio_rb32(pb);
364 codec->channels = avio_rl16(pb);
365 codec->frame_size = avio_rl16(pb);
366 codec->sample_fmt = (int16_t) avio_rl16(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
367 break;
20f0154 * Add the gop_size to the video parameters. Also the audio framesize.
Philip Gladstone authored
368 default:
75bdb98 Add the transfer of the new parameters from ffmpeg to ffserver and vi…
Philip Gladstone authored
369 goto fail;
de6d9b6 Initial revision
Fabrice Bellard authored
370 }
7080cbe @bcoudurier pass extradata through ffm
bcoudurier authored
371 if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
372 codec->extradata_size = avio_rb32(pb);
7080cbe @bcoudurier pass extradata through ffm
bcoudurier authored
373 codec->extradata = av_malloc(codec->extradata_size);
374 if (!codec->extradata)
375 return AVERROR(ENOMEM);
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
376 avio_read(pb, codec->extradata, codec->extradata_size);
7080cbe @bcoudurier pass extradata through ffm
bcoudurier authored
377 }
de6d9b6 Initial revision
Fabrice Bellard authored
378 }
379
380 /* get until end of block reached */
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
381 while ((avio_tell(pb) % ffm->packet_size) != 0)
e63a362 @elenril avio: avio_ prefixes for get_* functions
elenril authored
382 avio_r8(pb);
de6d9b6 Initial revision
Fabrice Bellard authored
383
384 /* init packet demux */
385 ffm->packet_ptr = ffm->packet;
386 ffm->packet_end = ffm->packet;
387 ffm->frame_offset = 0;
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
388 ffm->dts = 0;
de6d9b6 Initial revision
Fabrice Bellard authored
389 ffm->read_state = READ_HEADER;
390 ffm->first_packet = 1;
391 return 0;
392 fail:
527b46b ffmdec: Do not manually free streams on read_header error, this is al…
Reimar Döffinger authored
393 ffm_close(s);
de6d9b6 Initial revision
Fabrice Bellard authored
394 return -1;
395 }
396
397 /* return < 0 if eof */
398 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
399 {
400 int size;
401 FFMContext *ffm = s->priv_data;
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
402 int duration, ret;
eee99eb @bcoudurier return eof/error if only ffm header has been written, should fix #815
bcoudurier authored
403
de6d9b6 Initial revision
Fabrice Bellard authored
404 switch(ffm->read_state) {
405 case READ_HEADER:
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
406 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
407 return ret;
408
9ef5a9d @lu-zero Replace dprintf with av_dlog
lu-zero authored
409 av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
410 avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
411 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
66d2ff2 fixed memory free (no free necessary for private data) - added better…
Fabrice Bellard authored
412 FRAME_HEADER_SIZE)
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
413 return -1;
3c6a9f6 @bcoudurier pass dts and pts through ffm, should fix streaming b frames
bcoudurier authored
414 if (ffm->header[1] & FLAG_DTS)
415 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
416 return -1;
de6d9b6 Initial revision
Fabrice Bellard authored
417 ffm->read_state = READ_DATA;
418 /* fall thru */
419 case READ_DATA:
80fb823 @rbultje Use AV_RB* macros where appropriate.
rbultje authored
420 size = AV_RB24(ffm->header + 2);
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
421 if ((ret = ffm_is_avail_data(s, size)) < 0)
422 return ret;
de6d9b6 Initial revision
Fabrice Bellard authored
423
80fb823 @rbultje Use AV_RB* macros where appropriate.
rbultje authored
424 duration = AV_RB24(ffm->header + 5);
20f0154 * Add the gop_size to the video parameters. Also the audio framesize.
Philip Gladstone authored
425
de6d9b6 Initial revision
Fabrice Bellard authored
426 av_new_packet(pkt, size);
427 pkt->stream_index = ffm->header[0];
2ea2340 @bcoudurier check pkt stream index before returning packet, prevent segfault
bcoudurier authored
428 if ((unsigned)pkt->stream_index >= s->nb_streams) {
429 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
430 av_free_packet(pkt);
431 ffm->read_state = READ_HEADER;
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
432 return -1;
2ea2340 @bcoudurier check pkt stream index before returning packet, prevent segfault
bcoudurier authored
433 }
384c9c2 @elenril avio: add avio_tell macro as a replacement for url_ftell
elenril authored
434 pkt->pos = avio_tell(s->pb);
de6d9b6 Initial revision
Fabrice Bellard authored
435 if (ffm->header[1] & FLAG_KEY_FRAME)
cc947f0 Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
Jean-Daniel Dupas authored
436 pkt->flags |= AV_PKT_FLAG_KEY;
4606ac8 * some minor modification by Philip Gladston
Zdenek Kabelac authored
437
de6d9b6 Initial revision
Fabrice Bellard authored
438 ffm->read_state = READ_HEADER;
439 if (ffm_read_data(s, pkt->data, size, 0) != size) {
440 /* bad case: desynchronized packet. we cancel all the packet loading */
441 av_free_packet(pkt);
b9edbe9 @bcoudurier do not write ffm write index by default, detect if file is being writ…
bcoudurier authored
442 return -1;
de6d9b6 Initial revision
Fabrice Bellard authored
443 }
3c6a9f6 @bcoudurier pass dts and pts through ffm, should fix streaming b frames
bcoudurier authored
444 pkt->pts = AV_RB64(ffm->header+8);
445 if (ffm->header[1] & FLAG_DTS)
446 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
447 else
448 pkt->dts = pkt->pts;
20f0154 * Add the gop_size to the video parameters. Also the audio framesize.
Philip Gladstone authored
449 pkt->duration = duration;
de6d9b6 Initial revision
Fabrice Bellard authored
450 break;
451 }
452 return 0;
453 }
454
455 /* seek to a given time in the file. The file read pointer is
aa6b38c @DonDiego Fix random typos.
DonDiego authored
456 positioned at or before pts. XXX: the following code is quite
de6d9b6 Initial revision
Fabrice Bellard authored
457 approximative */
7b3c138 @michaelni 100l (forgoten seeking functions)
michaelni authored
458 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
de6d9b6 Initial revision
Fabrice Bellard authored
459 {
460 FFMContext *ffm = s->priv_data;
bc5c918 @DonDiego Remove offset_t typedef and use int64_t directly instead.
DonDiego authored
461 int64_t pos_min, pos_max, pos;
0c1a9ed * UINTX -> uintx_t INTX -> intx_t
Zdenek Kabelac authored
462 int64_t pts_min, pts_max, pts;
de6d9b6 Initial revision
Fabrice Bellard authored
463 double pos1;
464
919d7a3 @DonDiego Replace DEBUG_SEEK/DEBUG_SI + av_log combinations by av_dlog.
DonDiego authored
465 av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
de6d9b6 Initial revision
Fabrice Bellard authored
466 /* find the position using linear interpolation (better than
467 dichotomy in typical cases) */
0b99e85 @michaelni ffmdec: fix seeking for non indexed files
michaelni authored
468 if (ffm->write_index && ffm->write_index < ffm->file_size) {
59810f8 ffmdec: handle wrapped file in ffm_seek
Yan Jing authored
469 if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
470 pos_min = FFM_PACKET_SIZE;
471 pos_max = ffm->write_index - FFM_PACKET_SIZE;
472 } else {
473 pos_min = ffm->write_index;
474 pos_max = ffm->file_size - FFM_PACKET_SIZE;
475 }
476 } else {
477 pos_min = FFM_PACKET_SIZE;
478 pos_max = ffm->file_size - FFM_PACKET_SIZE;
479 }
de6d9b6 Initial revision
Fabrice Bellard authored
480 while (pos_min <= pos_max) {
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
481 pts_min = get_dts(s, pos_min);
482 pts_max = get_dts(s, pos_max);
59810f8 ffmdec: handle wrapped file in ffm_seek
Yan Jing authored
483 if (pts_min > wanted_pts || pts_max < wanted_pts) {
484 pos = pts_min > wanted_pts ? pos_min : pos_max;
485 goto found;
486 }
de6d9b6 Initial revision
Fabrice Bellard authored
487 /* linear interpolation */
4606ac8 * some minor modification by Philip Gladston
Zdenek Kabelac authored
488 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
de6d9b6 Initial revision
Fabrice Bellard authored
489 (double)(pts_max - pts_min);
0c1a9ed * UINTX -> uintx_t INTX -> intx_t
Zdenek Kabelac authored
490 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
de6d9b6 Initial revision
Fabrice Bellard authored
491 if (pos <= pos_min)
492 pos = pos_min;
493 else if (pos >= pos_max)
494 pos = pos_max;
fabb990 @bcoudurier cosmetics, pts->dts
bcoudurier authored
495 pts = get_dts(s, pos);
de6d9b6 Initial revision
Fabrice Bellard authored
496 /* check if we are lucky */
497 if (pts == wanted_pts) {
498 goto found;
499 } else if (pts > wanted_pts) {
500 pos_max = pos - FFM_PACKET_SIZE;
501 } else {
502 pos_min = pos + FFM_PACKET_SIZE;
503 }
504 }
7b3c138 @michaelni 100l (forgoten seeking functions)
michaelni authored
505 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
92a0f33 @bcoudurier fix valid seeking range
bcoudurier authored
506
de6d9b6 Initial revision
Fabrice Bellard authored
507 found:
928cfc7 @elupus [ffmdec] Check return value of avio_seek and avoid modifying state if…
elupus authored
508 if (ffm_seek1(s, pos) < 0)
509 return -1;
c07d64c @bcoudurier Reset reading state after successful seek.
bcoudurier authored
510
511 /* reset read state */
512 ffm->read_state = READ_HEADER;
513 ffm->packet_ptr = ffm->packet;
514 ffm->packet_end = ffm->packet;
515 ffm->first_packet = 1;
516
de6d9b6 Initial revision
Fabrice Bellard authored
517 return 0;
518 }
519
07c4ed8 * Add a probe function for FFM files
Philip Gladstone authored
520 static int ffm_probe(AVProbeData *p)
521 {
87e8788 @michaelni allocate 32 extra bytes at the end of the probe buffer and remove mos…
michaelni authored
522 if (
115329f @DonDiego COSMETICS: Remove all trailing whitespace.
DonDiego authored
523 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
66d2ff2 fixed memory free (no free necessary for private data) - added better…
Fabrice Bellard authored
524 p->buf[3] == '1')
07c4ed8 * Add a probe function for FFM files
Philip Gladstone authored
525 return AVPROBE_SCORE_MAX + 1;
526 return 0;
527 }
528
66355be @Flameeyes Prefix all _demuxer, _muxer, _protocol from libavformat and libavdevice.
Flameeyes authored
529 AVInputFormat ff_ffm_demuxer = {
dfc2c4d @elenril lavf: use designated initialisers for all (de)muxers.
elenril authored
530 .name = "ffm",
531 .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
532 .priv_data_size = sizeof(FFMContext),
533 .read_probe = ffm_probe,
534 .read_header = ffm_read_header,
535 .read_packet = ffm_read_packet,
536 .read_close = ffm_close,
537 .read_seek = ffm_seek,
c9a65ca converted to new API
Fabrice Bellard authored
538 };
Something went wrong with that request. Please try again.