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