Mercurial > libavformat.hg
annotate ffmdec.c @ 3350:e092bf9b744f libavformat
move get_pts function to avoid useless declaration
| author | bcoudurier |
|---|---|
| date | Mon, 26 May 2008 03:47:07 +0000 |
| parents | 4d492fccf79b |
| children | 6063f3cf59e6 |
| 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 | |
|
3350
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
110 static int64_t get_pts(AVFormatContext *s, offset_t pos) |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
111 { |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
112 ByteIOContext *pb = s->pb; |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
113 int64_t pts; |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
114 |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
115 ffm_seek1(s, pos); |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
116 url_fskip(pb, 4); |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
117 pts = get_be64(pb); |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
118 #ifdef DEBUG_SEEK |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
119 printf("pts=%0.6f\n", pts / 1000000.0); |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
120 #endif |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
121 return pts; |
|
e092bf9b744f
move get_pts function to avoid useless declaration
bcoudurier
parents:
3348
diff
changeset
|
122 } |
| 0 | 123 |
|
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
124 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
|
125 { |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
126 FFMContext *ffm = s->priv_data; |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
127 ByteIOContext *pb = s->pb; |
|
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
128 int64_t pts; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
129 //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
|
130 offset_t pos_min, pos_max; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
131 int64_t pts_start; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
132 offset_t ptr = url_ftell(pb); |
|
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 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
135 pos_min = 0; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
136 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
|
137 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
138 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
|
139 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
140 pts = get_pts(s, pos_max); |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
141 |
| 885 | 142 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
|
143 goto end; |
|
318
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 ffm->write_index = 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 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
|
148 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
149 pts = get_pts(s, pos_max); |
|
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 if (pts - 100000 <= pts_start) { |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
152 while (1) { |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
153 offset_t newpos; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
154 int64_t newpts; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
155 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
156 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
|
157 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
158 if (newpos == pos_min) |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
159 break; |
|
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 newpts = get_pts(s, newpos); |
|
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 if (newpts - 100000 <= pts) { |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
164 pos_max = newpos; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
165 pts = newpts; |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
166 } else { |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
167 pos_min = newpos; |
|
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 } |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
170 ffm->write_index += pos_max; |
|
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 |
|
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
173 //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
|
174 //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
|
175 |
|
390
3a40642dc4df
adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TELEFONICA dot COM dot AR>)
michael
parents:
318
diff
changeset
|
176 end: |
|
318
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
177 url_fseek(pb, ptr, SEEK_SET); |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
178 } |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
179 |
|
54e915169d48
Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents:
277
diff
changeset
|
180 |
| 0 | 181 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap) |
| 182 { | |
| 183 FFMContext *ffm = s->priv_data; | |
| 184 AVStream *st; | |
| 185 FFMStream *fst; | |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
186 ByteIOContext *pb = s->pb; |
| 0 | 187 AVCodecContext *codec; |
| 187 | 188 int i, nb_streams; |
| 65 | 189 uint32_t tag; |
| 0 | 190 |
| 191 /* header */ | |
| 192 tag = get_le32(pb); | |
| 193 if (tag != MKTAG('F', 'F', 'M', '1')) | |
| 194 goto fail; | |
| 195 ffm->packet_size = get_be32(pb); | |
| 196 if (ffm->packet_size != FFM_PACKET_SIZE) | |
| 197 goto fail; | |
| 198 ffm->write_index = get_be64(pb); | |
| 199 /* get also filesize */ | |
| 200 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
|
201 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
|
202 adjust_write_index(s); |
| 0 | 203 } else { |
| 1556 | 204 ffm->file_size = (UINT64_C(1) << 63) - 1; |
| 0 | 205 } |
| 206 | |
| 187 | 207 nb_streams = get_be32(pb); |
| 0 | 208 get_be32(pb); /* total bitrate */ |
| 209 /* read each stream */ | |
| 187 | 210 for(i=0;i<nb_streams;i++) { |
| 0 | 211 char rc_eq_buf[128]; |
| 212 | |
| 187 | 213 st = av_new_stream(s, 0); |
| 0 | 214 if (!st) |
| 215 goto fail; | |
| 216 fst = av_mallocz(sizeof(FFMStream)); | |
| 217 if (!fst) | |
| 218 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
|
219 s->streams[i] = st; |
| 885 | 220 |
| 468 | 221 av_set_pts_info(st, 64, 1, 1000000); |
| 885 | 222 |
| 0 | 223 st->priv_data = fst; |
| 224 | |
|
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
225 codec = st->codec; |
| 0 | 226 /* 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
|
227 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
|
228 codec->codec_type = get_byte(pb); /* codec_type */ |
| 0 | 229 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
|
230 st->quality = get_be32(pb); |
| 0 | 231 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
|
232 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
|
233 codec->debug = get_be32(pb); |
| 0 | 234 /* specific info */ |
| 235 switch(codec->codec_type) { | |
| 236 case CODEC_TYPE_VIDEO: | |
| 743 | 237 codec->time_base.num = get_be32(pb); |
| 238 codec->time_base.den = get_be32(pb); | |
| 0 | 239 codec->width = get_be16(pb); |
| 240 codec->height = get_be16(pb); | |
| 241 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
|
242 codec->pix_fmt = get_be32(pb); |
| 0 | 243 codec->qmin = get_byte(pb); |
| 244 codec->qmax = get_byte(pb); | |
| 245 codec->max_qdiff = get_byte(pb); | |
| 246 codec->qcompress = get_be16(pb) / 10000.0; | |
| 247 codec->qblur = get_be16(pb) / 10000.0; | |
| 248 codec->bit_rate_tolerance = get_be32(pb); | |
| 34 | 249 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf))); |
| 0 | 250 codec->rc_max_rate = get_be32(pb); |
| 251 codec->rc_min_rate = get_be32(pb); | |
| 252 codec->rc_buffer_size = get_be32(pb); | |
| 823 | 253 codec->i_quant_factor = av_int2dbl(get_be64(pb)); |
| 254 codec->b_quant_factor = av_int2dbl(get_be64(pb)); | |
| 255 codec->i_quant_offset = av_int2dbl(get_be64(pb)); | |
| 256 codec->b_quant_offset = av_int2dbl(get_be64(pb)); | |
| 0 | 257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 codec->frame_skip_cmp = get_be32(pb); |
| 823 | 268 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
|
269 codec->codec_tag = get_be32(pb); |
| 0 | 270 break; |
| 271 case CODEC_TYPE_AUDIO: | |
| 272 codec->sample_rate = get_be32(pb); | |
| 273 codec->channels = get_le16(pb); | |
| 274 codec->frame_size = get_le16(pb); | |
| 275 break; | |
| 276 default: | |
| 277 goto fail; | |
| 278 } | |
| 279 | |
| 280 } | |
| 281 | |
| 282 /* get until end of block reached */ | |
| 283 while ((url_ftell(pb) % ffm->packet_size) != 0) | |
| 284 get_byte(pb); | |
| 285 | |
| 286 /* init packet demux */ | |
| 287 ffm->packet_ptr = ffm->packet; | |
| 288 ffm->packet_end = ffm->packet; | |
| 289 ffm->frame_offset = 0; | |
| 290 ffm->pts = 0; | |
| 291 ffm->read_state = READ_HEADER; | |
| 292 ffm->first_packet = 1; | |
| 293 return 0; | |
| 294 fail: | |
| 295 for(i=0;i<s->nb_streams;i++) { | |
| 296 st = s->streams[i]; | |
| 297 if (st) { | |
| 298 av_freep(&st->priv_data); | |
| 299 av_free(st); | |
| 300 } | |
| 301 } | |
| 302 return -1; | |
| 303 } | |
| 304 | |
| 305 /* return < 0 if eof */ | |
| 306 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 307 { | |
| 308 int size; | |
| 309 FFMContext *ffm = s->priv_data; | |
| 310 int duration; | |
| 311 | |
| 312 switch(ffm->read_state) { | |
| 313 case READ_HEADER: | |
| 314 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
|
315 return AVERROR(EAGAIN); |
| 0 | 316 } |
| 317 #if 0 | |
|
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1415
diff
changeset
|
318 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
|
319 url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size); |
| 0 | 320 #endif |
| 885 | 321 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != |
| 0 | 322 FRAME_HEADER_SIZE) |
|
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1556
diff
changeset
|
323 return AVERROR(EAGAIN); |
| 0 | 324 #if 0 |
| 325 { | |
| 326 int i; | |
| 327 for(i=0;i<FRAME_HEADER_SIZE;i++) | |
| 328 printf("%02x ", ffm->header[i]); | |
| 329 printf("\n"); | |
| 330 } | |
| 331 #endif | |
| 332 ffm->read_state = READ_DATA; | |
| 333 /* fall thru */ | |
| 334 case READ_DATA: | |
| 2222 | 335 size = AV_RB24(ffm->header + 2); |
| 0 | 336 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
|
337 return AVERROR(EAGAIN); |
| 0 | 338 } |
| 339 | |
| 2222 | 340 duration = AV_RB24(ffm->header + 5); |
| 0 | 341 |
| 342 av_new_packet(pkt, size); | |
| 343 pkt->stream_index = ffm->header[0]; | |
|
3307
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
344 if ((unsigned)pkt->stream_index >= s->nb_streams) { |
|
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
345 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
|
346 av_free_packet(pkt); |
|
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
347 ffm->read_state = READ_HEADER; |
|
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
348 return AVERROR(EAGAIN); |
|
e4ff879325c0
check pkt stream index before returning packet, prevent segfault
bcoudurier
parents:
2915
diff
changeset
|
349 } |
|
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2222
diff
changeset
|
350 pkt->pos = url_ftell(s->pb); |
| 0 | 351 if (ffm->header[1] & FLAG_KEY_FRAME) |
| 352 pkt->flags |= PKT_FLAG_KEY; | |
| 353 | |
| 354 ffm->read_state = READ_HEADER; | |
| 355 if (ffm_read_data(s, pkt->data, size, 0) != size) { | |
| 356 /* bad case: desynchronized packet. we cancel all the packet loading */ | |
| 357 av_free_packet(pkt); | |
|
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1556
diff
changeset
|
358 return AVERROR(EAGAIN); |
| 0 | 359 } |
|
901
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
360 if (ffm->first_frame_in_packet) |
|
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
361 { |
|
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
362 pkt->pts = ffm->pts; |
|
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
363 ffm->first_frame_in_packet = 0; |
|
c1a07d63a66d
pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents:
896
diff
changeset
|
364 } |
| 0 | 365 pkt->duration = duration; |
| 366 break; | |
| 367 } | |
| 368 return 0; | |
| 369 } | |
| 370 | |
| 371 //#define DEBUG_SEEK | |
| 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 }; |
