Mercurial > libavformat.hg
annotate ffmdec.c @ 3348:4d492fccf79b libavformat
split ffm de/muxer
author | bcoudurier |
---|---|
date | Mon, 26 May 2008 03:44:31 +0000 |
parents | ffm.c@a6449316fad1 |
children | e092bf9b744f |
rev | line source |
---|---|
0 | 1 /* |
3348 | 2 * FFM (ffserver live feed) demuxer |
0 | 3 * Copyright (c) 2001 Fabrice Bellard. |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
3348 | 21 |
0 | 22 #include "avformat.h" |
3348 | 23 #include "ffm.h" |
0 | 24 #include <unistd.h> |
25 | |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
26 static int64_t get_pts(AVFormatContext *s, offset_t pos); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
27 |
0 | 28 static int ffm_is_avail_data(AVFormatContext *s, int size) |
29 { | |
30 FFMContext *ffm = s->priv_data; | |
31 offset_t pos, avail_size; | |
32 int len; | |
33 | |
34 len = ffm->packet_end - ffm->packet_ptr; | |
3347 | 35 if (size <= len) |
36 return 1; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
37 pos = url_ftell(s->pb); |
0 | 38 if (pos == ffm->write_index) { |
39 /* exactly at the end of stream */ | |
40 return 0; | |
41 } else if (pos < ffm->write_index) { | |
42 avail_size = ffm->write_index - pos; | |
43 } else { | |
44 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE); | |
45 } | |
46 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len; | |
47 if (size <= avail_size) | |
48 return 1; | |
49 else | |
50 return 0; | |
51 } | |
52 | |
53 /* first is true if we read the frame header */ | |
54 static int ffm_read_data(AVFormatContext *s, | |
65 | 55 uint8_t *buf, int size, int first) |
0 | 56 { |
57 FFMContext *ffm = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
58 ByteIOContext *pb = s->pb; |
0 | 59 int len, fill_size, size1, frame_offset; |
60 | |
61 size1 = size; | |
62 while (size > 0) { | |
63 redo: | |
64 len = ffm->packet_end - ffm->packet_ptr; | |
65 if (len > size) | |
66 len = size; | |
67 if (len == 0) { | |
68 if (url_ftell(pb) == ffm->file_size) | |
69 url_fseek(pb, ffm->packet_size, SEEK_SET); | |
70 retry_read: | |
71 get_be16(pb); /* PACKET_ID */ | |
72 fill_size = get_be16(pb); | |
73 ffm->pts = get_be64(pb); | |
901
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
74 ffm->first_frame_in_packet = 1; |
0 | 75 frame_offset = get_be16(pb); |
76 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE); | |
77 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size); | |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
78 if (ffm->packet_end < ffm->packet) |
537 | 79 return -1; |
0 | 80 /* if first packet or resynchronization packet, we must |
81 handle it specifically */ | |
82 if (ffm->first_packet || (frame_offset & 0x8000)) { | |
83 if (!frame_offset) { | |
84 /* This packet has no frame headers in it */ | |
85 if (url_ftell(pb) >= ffm->packet_size * 3) { | |
86 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR); | |
87 goto retry_read; | |
88 } | |
89 /* This is bad, we cannot find a valid frame header */ | |
90 return 0; | |
91 } | |
92 ffm->first_packet = 0; | |
93 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE) | |
537 | 94 return -1; |
0 | 95 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE; |
96 if (!first) | |
97 break; | |
98 } else { | |
99 ffm->packet_ptr = ffm->packet; | |
100 } | |
101 goto redo; | |
102 } | |
103 memcpy(buf, ffm->packet_ptr, len); | |
104 buf += len; | |
105 ffm->packet_ptr += len; | |
106 size -= len; | |
107 first = 0; | |
108 } | |
109 return size1 - size; | |
110 } | |
111 | |
112 | |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
113 static void adjust_write_index(AVFormatContext *s) |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
114 { |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
115 FFMContext *ffm = s->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
116 ByteIOContext *pb = s->pb; |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
117 int64_t pts; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
118 //offset_t orig_write_index = ffm->write_index; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
119 offset_t pos_min, pos_max; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
120 int64_t pts_start; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
121 offset_t ptr = url_ftell(pb); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
122 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
123 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
124 pos_min = 0; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
125 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
126 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
127 pts_start = get_pts(s, pos_min); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
128 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
129 pts = get_pts(s, pos_max); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
130 |
885 | 131 if (pts - 100000 > pts_start) |
390
3a40642dc4df
adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TELEFONICA dot COM dot AR>)
michael
parents:
318
diff
changeset
|
132 goto end; |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
133 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
134 ffm->write_index = FFM_PACKET_SIZE; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
135 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
136 pts_start = get_pts(s, pos_min); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
137 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
138 pts = get_pts(s, pos_max); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
139 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
140 if (pts - 100000 <= pts_start) { |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
141 while (1) { |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
142 offset_t newpos; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
143 int64_t newpts; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
144 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
145 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
146 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
147 if (newpos == pos_min) |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
148 break; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
149 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
150 newpts = get_pts(s, newpos); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
151 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
152 if (newpts - 100000 <= pts) { |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
153 pos_max = newpos; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
154 pts = newpts; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
155 } else { |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
156 pos_min = newpos; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
157 } |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
158 } |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
159 ffm->write_index += pos_max; |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
160 } |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
161 |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
162 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.); |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
163 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. ); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
164 |
390
3a40642dc4df
adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TELEFONICA dot COM dot AR>)
michael
parents:
318
diff
changeset
|
165 end: |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
166 url_fseek(pb, ptr, SEEK_SET); |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
167 } |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
168 |
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
169 |
0 | 170 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap) |
171 { | |
172 FFMContext *ffm = s->priv_data; | |
173 AVStream *st; | |
174 FFMStream *fst; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
175 ByteIOContext *pb = s->pb; |
0 | 176 AVCodecContext *codec; |
187 | 177 int i, nb_streams; |
65 | 178 uint32_t tag; |
0 | 179 |
180 /* header */ | |
181 tag = get_le32(pb); | |
182 if (tag != MKTAG('F', 'F', 'M', '1')) | |
183 goto fail; | |
184 ffm->packet_size = get_be32(pb); | |
185 if (ffm->packet_size != FFM_PACKET_SIZE) | |
186 goto fail; | |
187 ffm->write_index = get_be64(pb); | |
188 /* get also filesize */ | |
189 if (!url_is_streamed(pb)) { | |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
751
diff
changeset
|
190 ffm->file_size = url_fsize(pb); |
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
191 adjust_write_index(s); |
0 | 192 } else { |
1556 | 193 ffm->file_size = (UINT64_C(1) << 63) - 1; |
0 | 194 } |
195 | |
187 | 196 nb_streams = get_be32(pb); |
0 | 197 get_be32(pb); /* total bitrate */ |
198 /* read each stream */ | |
187 | 199 for(i=0;i<nb_streams;i++) { |
0 | 200 char rc_eq_buf[128]; |
201 | |
187 | 202 st = av_new_stream(s, 0); |
0 | 203 if (!st) |
204 goto fail; | |
205 fst = av_mallocz(sizeof(FFMStream)); | |
206 if (!fst) | |
207 goto fail; | |
862
aa0abab5e320
fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents:
858
diff
changeset
|
208 s->streams[i] = st; |
885 | 209 |
468 | 210 av_set_pts_info(st, 64, 1, 1000000); |
885 | 211 |
0 | 212 st->priv_data = fst; |
213 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
214 codec = st->codec; |
0 | 215 /* generic info */ |
862
aa0abab5e320
fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents:
858
diff
changeset
|
216 codec->codec_id = get_be32(pb); |
aa0abab5e320
fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents:
858
diff
changeset
|
217 codec->codec_type = get_byte(pb); /* codec_type */ |
0 | 218 codec->bit_rate = get_be32(pb); |
862
aa0abab5e320
fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents:
858
diff
changeset
|
219 st->quality = get_be32(pb); |
0 | 220 codec->flags = get_be32(pb); |
744
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
221 codec->flags2 = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
222 codec->debug = get_be32(pb); |
0 | 223 /* specific info */ |
224 switch(codec->codec_type) { | |
225 case CODEC_TYPE_VIDEO: | |
743 | 226 codec->time_base.num = get_be32(pb); |
227 codec->time_base.den = get_be32(pb); | |
0 | 228 codec->width = get_be16(pb); |
229 codec->height = get_be16(pb); | |
230 codec->gop_size = get_be16(pb); | |
744
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
231 codec->pix_fmt = get_be32(pb); |
0 | 232 codec->qmin = get_byte(pb); |
233 codec->qmax = get_byte(pb); | |
234 codec->max_qdiff = get_byte(pb); | |
235 codec->qcompress = get_be16(pb) / 10000.0; | |
236 codec->qblur = get_be16(pb) / 10000.0; | |
237 codec->bit_rate_tolerance = get_be32(pb); | |
34 | 238 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf))); |
0 | 239 codec->rc_max_rate = get_be32(pb); |
240 codec->rc_min_rate = get_be32(pb); | |
241 codec->rc_buffer_size = get_be32(pb); | |
823 | 242 codec->i_quant_factor = av_int2dbl(get_be64(pb)); |
243 codec->b_quant_factor = av_int2dbl(get_be64(pb)); | |
244 codec->i_quant_offset = av_int2dbl(get_be64(pb)); | |
245 codec->b_quant_offset = av_int2dbl(get_be64(pb)); | |
0 | 246 codec->dct_algo = get_be32(pb); |
744
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
247 codec->strict_std_compliance = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
248 codec->max_b_frames = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
249 codec->luma_elim_threshold = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
250 codec->chroma_elim_threshold = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
251 codec->mpeg_quant = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
252 codec->intra_dc_precision = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
253 codec->me_method = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
254 codec->mb_decision = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
255 codec->nsse_weight = get_be32(pb); |
da5b3b9e898e
Add in many fields that have been added to the Codec structure. This means
philipjsg
parents:
743
diff
changeset
|
256 codec->frame_skip_cmp = get_be32(pb); |
823 | 257 codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb)); |
1809
491581a2b9a7
codec_tag settable via VideoTag, and transmit codec_tag in ffm
alex
parents:
1787
diff
changeset
|
258 codec->codec_tag = get_be32(pb); |
0 | 259 break; |
260 case CODEC_TYPE_AUDIO: | |
261 codec->sample_rate = get_be32(pb); | |
262 codec->channels = get_le16(pb); | |
263 codec->frame_size = get_le16(pb); | |
264 break; | |
265 default: | |
266 goto fail; | |
267 } | |
268 | |
269 } | |
270 | |
271 /* get until end of block reached */ | |
272 while ((url_ftell(pb) % ffm->packet_size) != 0) | |
273 get_byte(pb); | |
274 | |
275 /* init packet demux */ | |
276 ffm->packet_ptr = ffm->packet; | |
277 ffm->packet_end = ffm->packet; | |
278 ffm->frame_offset = 0; | |
279 ffm->pts = 0; | |
280 ffm->read_state = READ_HEADER; | |
281 ffm->first_packet = 1; | |
282 return 0; | |
283 fail: | |
284 for(i=0;i<s->nb_streams;i++) { | |
285 st = s->streams[i]; | |
286 if (st) { | |
287 av_freep(&st->priv_data); | |
288 av_free(st); | |
289 } | |
290 } | |
291 return -1; | |
292 } | |
293 | |
294 /* return < 0 if eof */ | |
295 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
296 { | |
297 int size; | |
298 FFMContext *ffm = s->priv_data; | |
299 int duration; | |
300 | |
301 switch(ffm->read_state) { | |
302 case READ_HEADER: | |
303 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) { | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1556
diff
changeset
|
304 return AVERROR(EAGAIN); |
0 | 305 } |
306 #if 0 | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
307 printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n", |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
308 url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size); |
0 | 309 #endif |
885 | 310 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != |
0 | 311 FRAME_HEADER_SIZE) |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1556
diff
changeset
|
312 return AVERROR(EAGAIN); |
0 | 313 #if 0 |
314 { | |
315 int i; | |
316 for(i=0;i<FRAME_HEADER_SIZE;i++) | |
317 printf("%02x ", ffm->header[i]); | |
318 printf("\n"); | |
319 } | |
320 #endif | |
321 ffm->read_state = READ_DATA; | |
322 /* fall thru */ | |
323 case READ_DATA: | |
2222 | 324 size = AV_RB24(ffm->header + 2); |
0 | 325 if (!ffm_is_avail_data(s, size)) { |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1556
diff
changeset
|
326 return AVERROR(EAGAIN); |
0 | 327 } |
328 | |
2222 | 329 duration = AV_RB24(ffm->header + 5); |
0 | 330 |
331 av_new_packet(pkt, size); | |
332 pkt->stream_index = ffm->header[0]; | |
3307
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
333 if ((unsigned)pkt->stream_index >= s->nb_streams) { |
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
334 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index); |
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
335 av_free_packet(pkt); |
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
336 ffm->read_state = READ_HEADER; |
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
337 return AVERROR(EAGAIN); |
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
338 } |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
339 pkt->pos = url_ftell(s->pb); |
0 | 340 if (ffm->header[1] & FLAG_KEY_FRAME) |
341 pkt->flags |= PKT_FLAG_KEY; | |
342 | |
343 ffm->read_state = READ_HEADER; | |
344 if (ffm_read_data(s, pkt->data, size, 0) != size) { | |
345 /* bad case: desynchronized packet. we cancel all the packet loading */ | |
346 av_free_packet(pkt); | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1556
diff
changeset
|
347 return AVERROR(EAGAIN); |
0 | 348 } |
901
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
349 if (ffm->first_frame_in_packet) |
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
350 { |
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
351 pkt->pts = ffm->pts; |
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
352 ffm->first_frame_in_packet = 0; |
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
353 } |
0 | 354 pkt->duration = duration; |
355 break; | |
356 } | |
357 return 0; | |
358 } | |
359 | |
360 //#define DEBUG_SEEK | |
361 | |
362 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated | |
363 by the write position inside this function */ | |
364 static void ffm_seek1(AVFormatContext *s, offset_t pos1) | |
365 { | |
366 FFMContext *ffm = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
367 ByteIOContext *pb = s->pb; |
0 | 368 offset_t pos; |
369 | |
370 pos = pos1 + ffm->write_index; | |
371 if (pos >= ffm->file_size) | |
372 pos -= (ffm->file_size - FFM_PACKET_SIZE); | |
373 #ifdef DEBUG_SEEK | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
374 printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos); |
0 | 375 #endif |
376 url_fseek(pb, pos, SEEK_SET); | |
377 } | |
378 | |
65 | 379 static int64_t get_pts(AVFormatContext *s, offset_t pos) |
0 | 380 { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
381 ByteIOContext *pb = s->pb; |
65 | 382 int64_t pts; |
0 | 383 |
384 ffm_seek1(s, pos); | |
385 url_fskip(pb, 4); | |
386 pts = get_be64(pb); | |
387 #ifdef DEBUG_SEEK | |
388 printf("pts=%0.6f\n", pts / 1000000.0); | |
389 #endif | |
390 return pts; | |
391 } | |
392 | |
393 /* seek to a given time in the file. The file read pointer is | |
2915 | 394 positioned at or before pts. XXX: the following code is quite |
0 | 395 approximative */ |
558 | 396 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags) |
0 | 397 { |
398 FFMContext *ffm = s->priv_data; | |
399 offset_t pos_min, pos_max, pos; | |
65 | 400 int64_t pts_min, pts_max, pts; |
0 | 401 double pos1; |
402 | |
403 #ifdef DEBUG_SEEK | |
404 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0); | |
405 #endif | |
406 /* find the position using linear interpolation (better than | |
407 dichotomy in typical cases) */ | |
408 pos_min = 0; | |
409 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE; | |
410 while (pos_min <= pos_max) { | |
411 pts_min = get_pts(s, pos_min); | |
412 pts_max = get_pts(s, pos_max); | |
413 /* linear interpolation */ | |
414 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) / | |
415 (double)(pts_max - pts_min); | |
65 | 416 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE; |
0 | 417 if (pos <= pos_min) |
418 pos = pos_min; | |
419 else if (pos >= pos_max) | |
420 pos = pos_max; | |
421 pts = get_pts(s, pos); | |
422 /* check if we are lucky */ | |
423 if (pts == wanted_pts) { | |
424 goto found; | |
425 } else if (pts > wanted_pts) { | |
426 pos_max = pos - FFM_PACKET_SIZE; | |
427 } else { | |
428 pos_min = pos + FFM_PACKET_SIZE; | |
429 } | |
430 } | |
558 | 431 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max; |
0 | 432 if (pos > 0) |
433 pos -= FFM_PACKET_SIZE; | |
434 found: | |
435 ffm_seek1(s, pos); | |
436 return 0; | |
437 } | |
438 | |
905
dbc0145bbf11
Add --disable-protocols option to configure to disable I/O protocol from
diego
parents:
901
diff
changeset
|
439 #ifdef CONFIG_FFSERVER |
0 | 440 offset_t ffm_read_write_index(int fd) |
441 { | |
65 | 442 uint8_t buf[8]; |
0 | 443 |
444 lseek(fd, 8, SEEK_SET); | |
445 read(fd, buf, 8); | |
2222 | 446 return AV_RB64(buf); |
0 | 447 } |
448 | |
449 void ffm_write_write_index(int fd, offset_t pos) | |
450 { | |
65 | 451 uint8_t buf[8]; |
0 | 452 int i; |
453 | |
454 for(i=0;i<8;i++) | |
455 buf[i] = (pos >> (56 - i * 8)) & 0xff; | |
456 lseek(fd, 8, SEEK_SET); | |
457 write(fd, buf, 8); | |
458 } | |
459 | |
460 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size) | |
461 { | |
462 FFMContext *ffm = s->priv_data; | |
463 ffm->write_index = pos; | |
464 ffm->file_size = file_size; | |
465 } | |
905
dbc0145bbf11
Add --disable-protocols option to configure to disable I/O protocol from
diego
parents:
901
diff
changeset
|
466 #endif // CONFIG_FFSERVER |
0 | 467 |
468 static int ffm_read_close(AVFormatContext *s) | |
469 { | |
470 AVStream *st; | |
471 int i; | |
472 | |
473 for(i=0;i<s->nb_streams;i++) { | |
474 st = s->streams[i]; | |
475 av_freep(&st->priv_data); | |
476 } | |
477 return 0; | |
478 } | |
479 | |
480 static int ffm_probe(AVProbeData *p) | |
481 { | |
2001
1a3c9056982a
allocate 32 extra bytes at the end of the probe buffer and remove most probe buf_size checks
michael
parents:
1809
diff
changeset
|
482 if ( |
885 | 483 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' && |
0 | 484 p->buf[3] == '1') |
485 return AVPROBE_SCORE_MAX + 1; | |
486 return 0; | |
487 } | |
488 | |
1169 | 489 AVInputFormat ffm_demuxer = { |
0 | 490 "ffm", |
491 "ffm format", | |
492 sizeof(FFMContext), | |
493 ffm_probe, | |
494 ffm_read_header, | |
495 ffm_read_packet, | |
496 ffm_read_close, | |
497 ffm_seek, | |
498 }; |