Commit | Line | Data |
---|---|---|
2ba45a60 DM |
1 | /* |
2 | * FFM (ffserver live feed) demuxer | |
3 | * Copyright (c) 2001 Fabrice Bellard | |
4 | * | |
5 | * This file is part of FFmpeg. | |
6 | * | |
7 | * FFmpeg is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
11 | * | |
12 | * FFmpeg is distributed in the hope that it will be useful, | |
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 | |
18 | * License along with FFmpeg; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <stdint.h> | |
23 | ||
24 | #include "libavutil/intreadwrite.h" | |
25 | #include "libavutil/intfloat.h" | |
f6fa7814 DM |
26 | #include "libavutil/opt.h" |
27 | #include "libavutil/avassert.h" | |
28 | #include "libavutil/avstring.h" | |
2ba45a60 DM |
29 | #include "avformat.h" |
30 | #include "internal.h" | |
31 | #include "ffm.h" | |
32 | #include "avio_internal.h" | |
33 | ||
34 | static int ffm_is_avail_data(AVFormatContext *s, int size) | |
35 | { | |
36 | FFMContext *ffm = s->priv_data; | |
37 | int64_t pos, avail_size; | |
38 | int len; | |
39 | ||
40 | len = ffm->packet_end - ffm->packet_ptr; | |
41 | if (size <= len) | |
42 | return 1; | |
43 | pos = avio_tell(s->pb); | |
44 | if (!ffm->write_index) { | |
45 | if (pos == ffm->file_size) | |
46 | return AVERROR_EOF; | |
47 | avail_size = ffm->file_size - pos; | |
48 | } else { | |
49 | if (pos == ffm->write_index) { | |
50 | /* exactly at the end of stream */ | |
51 | return AVERROR(EAGAIN); | |
52 | } else if (pos < ffm->write_index) { | |
53 | avail_size = ffm->write_index - pos; | |
54 | } else { | |
55 | avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE); | |
56 | } | |
57 | } | |
58 | avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len; | |
59 | if (size <= avail_size) | |
60 | return 1; | |
61 | else | |
62 | return AVERROR(EAGAIN); | |
63 | } | |
64 | ||
65 | static int ffm_resync(AVFormatContext *s, int state) | |
66 | { | |
67 | av_log(s, AV_LOG_ERROR, "resyncing\n"); | |
68 | while (state != PACKET_ID) { | |
69 | if (avio_feof(s->pb)) { | |
70 | av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n"); | |
71 | return -1; | |
72 | } | |
73 | state = (state << 8) | avio_r8(s->pb); | |
74 | } | |
75 | return 0; | |
76 | } | |
77 | ||
78 | /* first is true if we read the frame header */ | |
79 | static int ffm_read_data(AVFormatContext *s, | |
80 | uint8_t *buf, int size, int header) | |
81 | { | |
82 | FFMContext *ffm = s->priv_data; | |
83 | AVIOContext *pb = s->pb; | |
84 | int len, fill_size, size1, frame_offset, id; | |
85 | ||
86 | size1 = size; | |
87 | while (size > 0) { | |
88 | redo: | |
89 | len = ffm->packet_end - ffm->packet_ptr; | |
90 | if (len < 0) | |
91 | return -1; | |
92 | if (len > size) | |
93 | len = size; | |
94 | if (len == 0) { | |
95 | if (avio_tell(pb) == ffm->file_size) | |
96 | avio_seek(pb, ffm->packet_size, SEEK_SET); | |
97 | retry_read: | |
98 | if (pb->buffer_size != ffm->packet_size) { | |
99 | int64_t tell = avio_tell(pb); | |
100 | ffio_set_buf_size(pb, ffm->packet_size); | |
101 | avio_seek(pb, tell, SEEK_SET); | |
102 | } | |
103 | id = avio_rb16(pb); /* PACKET_ID */ | |
104 | if (id != PACKET_ID) | |
105 | if (ffm_resync(s, id) < 0) | |
106 | return -1; | |
107 | fill_size = avio_rb16(pb); | |
108 | ffm->dts = avio_rb64(pb); | |
109 | frame_offset = avio_rb16(pb); | |
110 | avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE); | |
111 | ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size); | |
112 | if (ffm->packet_end < ffm->packet || frame_offset < 0) | |
113 | return -1; | |
114 | /* if first packet or resynchronization packet, we must | |
115 | handle it specifically */ | |
116 | if (ffm->first_packet || (frame_offset & 0x8000)) { | |
117 | if (!frame_offset) { | |
118 | /* This packet has no frame headers in it */ | |
119 | if (avio_tell(pb) >= ffm->packet_size * 3LL) { | |
120 | avio_seek(pb, -ffm->packet_size * 2LL, SEEK_CUR); | |
121 | goto retry_read; | |
122 | } | |
123 | /* This is bad, we cannot find a valid frame header */ | |
124 | return 0; | |
125 | } | |
126 | ffm->first_packet = 0; | |
127 | if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) | |
128 | return -1; | |
129 | ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE; | |
130 | if (!header) | |
131 | break; | |
132 | } else { | |
133 | ffm->packet_ptr = ffm->packet; | |
134 | } | |
135 | goto redo; | |
136 | } | |
137 | memcpy(buf, ffm->packet_ptr, len); | |
138 | buf += len; | |
139 | ffm->packet_ptr += len; | |
140 | size -= len; | |
141 | header = 0; | |
142 | } | |
143 | return size1 - size; | |
144 | } | |
145 | ||
146 | /* ensure that acutal seeking happens between FFM_PACKET_SIZE | |
147 | and file_size - FFM_PACKET_SIZE */ | |
148 | static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1) | |
149 | { | |
150 | FFMContext *ffm = s->priv_data; | |
151 | AVIOContext *pb = s->pb; | |
152 | int64_t pos; | |
153 | ||
154 | pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE); | |
155 | pos = FFMAX(pos, FFM_PACKET_SIZE); | |
156 | av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos); | |
157 | return avio_seek(pb, pos, SEEK_SET); | |
158 | } | |
159 | ||
160 | static int64_t get_dts(AVFormatContext *s, int64_t pos) | |
161 | { | |
162 | AVIOContext *pb = s->pb; | |
163 | int64_t dts; | |
164 | ||
165 | ffm_seek1(s, pos); | |
166 | avio_skip(pb, 4); | |
167 | dts = avio_rb64(pb); | |
168 | av_dlog(s, "dts=%0.6f\n", dts / 1000000.0); | |
169 | return dts; | |
170 | } | |
171 | ||
172 | static void adjust_write_index(AVFormatContext *s) | |
173 | { | |
174 | FFMContext *ffm = s->priv_data; | |
175 | AVIOContext *pb = s->pb; | |
176 | int64_t pts; | |
177 | //int64_t orig_write_index = ffm->write_index; | |
178 | int64_t pos_min, pos_max; | |
179 | int64_t pts_start; | |
180 | int64_t ptr = avio_tell(pb); | |
181 | ||
182 | ||
183 | pos_min = 0; | |
184 | pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE; | |
185 | ||
186 | pts_start = get_dts(s, pos_min); | |
187 | ||
188 | pts = get_dts(s, pos_max); | |
189 | ||
190 | if (pts - 100000 > pts_start) | |
191 | goto end; | |
192 | ||
193 | ffm->write_index = FFM_PACKET_SIZE; | |
194 | ||
195 | pts_start = get_dts(s, pos_min); | |
196 | ||
197 | pts = get_dts(s, pos_max); | |
198 | ||
199 | if (pts - 100000 <= pts_start) { | |
200 | while (1) { | |
201 | int64_t newpos; | |
202 | int64_t newpts; | |
203 | ||
204 | newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE; | |
205 | ||
206 | if (newpos == pos_min) | |
207 | break; | |
208 | ||
209 | newpts = get_dts(s, newpos); | |
210 | ||
211 | if (newpts - 100000 <= pts) { | |
212 | pos_max = newpos; | |
213 | pts = newpts; | |
214 | } else { | |
215 | pos_min = newpos; | |
216 | } | |
217 | } | |
218 | ffm->write_index += pos_max; | |
219 | } | |
220 | ||
221 | end: | |
222 | avio_seek(pb, ptr, SEEK_SET); | |
223 | } | |
224 | ||
225 | ||
226 | static int ffm_close(AVFormatContext *s) | |
227 | { | |
228 | int i; | |
229 | ||
230 | for (i = 0; i < s->nb_streams; i++) | |
231 | av_freep(&s->streams[i]->codec->rc_eq); | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
f6fa7814 DM |
236 | static int ffm_append_recommended_configuration(AVStream *st, char **conf) |
237 | { | |
238 | int ret; | |
239 | size_t newsize; | |
240 | av_assert0(conf && st); | |
241 | if (!*conf) | |
242 | return 0; | |
243 | if (!st->recommended_encoder_configuration) { | |
244 | st->recommended_encoder_configuration = *conf; | |
245 | *conf = 0; | |
246 | return 0; | |
247 | } | |
248 | newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2; | |
249 | if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0) | |
250 | return ret; | |
251 | av_strlcat(st->recommended_encoder_configuration, ",", newsize); | |
252 | av_strlcat(st->recommended_encoder_configuration, *conf, newsize); | |
253 | av_freep(conf); | |
254 | return 0; | |
255 | } | |
256 | ||
2ba45a60 DM |
257 | static int ffm2_read_header(AVFormatContext *s) |
258 | { | |
259 | FFMContext *ffm = s->priv_data; | |
260 | AVStream *st; | |
261 | AVIOContext *pb = s->pb; | |
262 | AVCodecContext *codec; | |
263 | int ret; | |
f6fa7814 DM |
264 | int f_main = 0, f_cprv, f_stvi, f_stau; |
265 | AVCodec *enc; | |
266 | char *buffer; | |
2ba45a60 DM |
267 | |
268 | ffm->packet_size = avio_rb32(pb); | |
269 | if (ffm->packet_size != FFM_PACKET_SIZE) { | |
270 | av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n", | |
271 | ffm->packet_size, FFM_PACKET_SIZE); | |
272 | ret = AVERROR_INVALIDDATA; | |
273 | goto fail; | |
274 | } | |
275 | ||
276 | ffm->write_index = avio_rb64(pb); | |
277 | /* get also filesize */ | |
278 | if (pb->seekable) { | |
279 | ffm->file_size = avio_size(pb); | |
280 | if (ffm->write_index && 0) | |
281 | adjust_write_index(s); | |
282 | } else { | |
283 | ffm->file_size = (UINT64_C(1) << 63) - 1; | |
284 | } | |
285 | ||
286 | while(!avio_feof(pb)) { | |
287 | unsigned id = avio_rb32(pb); | |
288 | unsigned size = avio_rb32(pb); | |
289 | int64_t next = avio_tell(pb) + size; | |
290 | char rc_eq_buf[128]; | |
291 | ||
292 | if(!id) | |
293 | break; | |
294 | ||
295 | switch(id) { | |
296 | case MKBETAG('M', 'A', 'I', 'N'): | |
f6fa7814 DM |
297 | if (f_main++) { |
298 | ret = AVERROR(EINVAL); | |
299 | goto fail; | |
300 | } | |
2ba45a60 DM |
301 | avio_rb32(pb); /* nb_streams */ |
302 | avio_rb32(pb); /* total bitrate */ | |
303 | break; | |
304 | case MKBETAG('C', 'O', 'M', 'M'): | |
f6fa7814 | 305 | f_cprv = f_stvi = f_stau = 0; |
2ba45a60 DM |
306 | st = avformat_new_stream(s, NULL); |
307 | if (!st) { | |
308 | ret = AVERROR(ENOMEM); | |
309 | goto fail; | |
310 | } | |
311 | ||
312 | avpriv_set_pts_info(st, 64, 1, 1000000); | |
313 | ||
314 | codec = st->codec; | |
315 | /* generic info */ | |
316 | codec->codec_id = avio_rb32(pb); | |
317 | codec->codec_type = avio_r8(pb); | |
318 | codec->bit_rate = avio_rb32(pb); | |
319 | codec->flags = avio_rb32(pb); | |
320 | codec->flags2 = avio_rb32(pb); | |
321 | codec->debug = avio_rb32(pb); | |
322 | if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) { | |
323 | if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0) | |
324 | return AVERROR(ENOMEM); | |
325 | } | |
f6fa7814 DM |
326 | break; |
327 | case MKBETAG('S', 'T', 'V', 'I'): | |
328 | if (f_stvi++) { | |
329 | ret = AVERROR(EINVAL); | |
330 | goto fail; | |
331 | } | |
332 | codec->time_base.num = avio_rb32(pb); | |
333 | codec->time_base.den = avio_rb32(pb); | |
334 | codec->width = avio_rb16(pb); | |
335 | codec->height = avio_rb16(pb); | |
336 | codec->gop_size = avio_rb16(pb); | |
337 | codec->pix_fmt = avio_rb32(pb); | |
338 | codec->qmin = avio_r8(pb); | |
339 | codec->qmax = avio_r8(pb); | |
340 | codec->max_qdiff = avio_r8(pb); | |
341 | codec->qcompress = avio_rb16(pb) / 10000.0; | |
342 | codec->qblur = avio_rb16(pb) / 10000.0; | |
343 | codec->bit_rate_tolerance = avio_rb32(pb); | |
344 | avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf)); | |
345 | codec->rc_eq = av_strdup(rc_eq_buf); | |
346 | codec->rc_max_rate = avio_rb32(pb); | |
347 | codec->rc_min_rate = avio_rb32(pb); | |
348 | codec->rc_buffer_size = avio_rb32(pb); | |
349 | codec->i_quant_factor = av_int2double(avio_rb64(pb)); | |
350 | codec->b_quant_factor = av_int2double(avio_rb64(pb)); | |
351 | codec->i_quant_offset = av_int2double(avio_rb64(pb)); | |
352 | codec->b_quant_offset = av_int2double(avio_rb64(pb)); | |
353 | codec->dct_algo = avio_rb32(pb); | |
354 | codec->strict_std_compliance = avio_rb32(pb); | |
355 | codec->max_b_frames = avio_rb32(pb); | |
356 | codec->mpeg_quant = avio_rb32(pb); | |
357 | codec->intra_dc_precision = avio_rb32(pb); | |
358 | codec->me_method = avio_rb32(pb); | |
359 | codec->mb_decision = avio_rb32(pb); | |
360 | codec->nsse_weight = avio_rb32(pb); | |
361 | codec->frame_skip_cmp = avio_rb32(pb); | |
362 | codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb)); | |
363 | codec->codec_tag = avio_rb32(pb); | |
364 | codec->thread_count = avio_r8(pb); | |
365 | codec->coder_type = avio_rb32(pb); | |
366 | codec->me_cmp = avio_rb32(pb); | |
367 | codec->me_subpel_quality = avio_rb32(pb); | |
368 | codec->me_range = avio_rb32(pb); | |
369 | codec->keyint_min = avio_rb32(pb); | |
370 | codec->scenechange_threshold = avio_rb32(pb); | |
371 | codec->b_frame_strategy = avio_rb32(pb); | |
372 | codec->qcompress = av_int2double(avio_rb64(pb)); | |
373 | codec->qblur = av_int2double(avio_rb64(pb)); | |
374 | codec->max_qdiff = avio_rb32(pb); | |
375 | codec->refs = avio_rb32(pb); | |
376 | break; | |
377 | case MKBETAG('S', 'T', 'A', 'U'): | |
378 | if (f_stau++) { | |
379 | ret = AVERROR(EINVAL); | |
380 | goto fail; | |
381 | } | |
382 | codec->sample_rate = avio_rb32(pb); | |
383 | codec->channels = avio_rl16(pb); | |
384 | codec->frame_size = avio_rl16(pb); | |
385 | break; | |
386 | case MKBETAG('C', 'P', 'R', 'V'): | |
387 | if (f_cprv++) { | |
388 | ret = AVERROR(EINVAL); | |
389 | goto fail; | |
390 | } | |
391 | enc = avcodec_find_encoder(codec->codec_id); | |
392 | if (enc && enc->priv_data_size && enc->priv_class) { | |
393 | buffer = av_malloc(size + 1); | |
394 | if (!buffer) { | |
395 | ret = AVERROR(ENOMEM); | |
396 | goto fail; | |
397 | } | |
398 | avio_get_str(pb, size, buffer, size + 1); | |
399 | if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0) | |
400 | goto fail; | |
401 | } | |
402 | break; | |
403 | case MKBETAG('S', '2', 'V', 'I'): | |
404 | if (f_stvi++) { | |
405 | ret = AVERROR(EINVAL); | |
406 | goto fail; | |
407 | } | |
408 | buffer = av_malloc(size); | |
409 | if (!buffer) { | |
410 | ret = AVERROR(ENOMEM); | |
411 | goto fail; | |
412 | } | |
413 | avio_get_str(pb, INT_MAX, buffer, size); | |
414 | av_set_options_string(codec, buffer, "=", ","); | |
415 | if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0) | |
416 | goto fail; | |
417 | break; | |
418 | case MKBETAG('S', '2', 'A', 'U'): | |
419 | if (f_stau++) { | |
420 | ret = AVERROR(EINVAL); | |
421 | goto fail; | |
422 | } | |
423 | buffer = av_malloc(size); | |
424 | if (!buffer) { | |
425 | ret = AVERROR(ENOMEM); | |
426 | goto fail; | |
2ba45a60 | 427 | } |
f6fa7814 DM |
428 | avio_get_str(pb, INT_MAX, buffer, size); |
429 | av_set_options_string(codec, buffer, "=", ","); | |
430 | ffm_append_recommended_configuration(st, &buffer); | |
2ba45a60 DM |
431 | break; |
432 | } | |
433 | avio_seek(pb, next, SEEK_SET); | |
434 | } | |
435 | ||
436 | /* get until end of block reached */ | |
437 | while ((avio_tell(pb) % ffm->packet_size) != 0) | |
438 | avio_r8(pb); | |
439 | ||
440 | /* init packet demux */ | |
441 | ffm->packet_ptr = ffm->packet; | |
442 | ffm->packet_end = ffm->packet; | |
443 | ffm->frame_offset = 0; | |
444 | ffm->dts = 0; | |
445 | ffm->read_state = READ_HEADER; | |
446 | ffm->first_packet = 1; | |
447 | return 0; | |
448 | fail: | |
449 | ffm_close(s); | |
450 | return ret; | |
451 | } | |
452 | ||
453 | static int ffm_read_header(AVFormatContext *s) | |
454 | { | |
455 | FFMContext *ffm = s->priv_data; | |
456 | AVStream *st; | |
457 | AVIOContext *pb = s->pb; | |
458 | AVCodecContext *codec; | |
459 | int i, nb_streams; | |
460 | uint32_t tag; | |
461 | ||
462 | /* header */ | |
463 | tag = avio_rl32(pb); | |
464 | if (tag == MKTAG('F', 'F', 'M', '2')) | |
465 | return ffm2_read_header(s); | |
466 | if (tag != MKTAG('F', 'F', 'M', '1')) | |
467 | goto fail; | |
468 | ffm->packet_size = avio_rb32(pb); | |
469 | if (ffm->packet_size != FFM_PACKET_SIZE) | |
470 | goto fail; | |
471 | ffm->write_index = avio_rb64(pb); | |
472 | /* get also filesize */ | |
473 | if (pb->seekable) { | |
474 | ffm->file_size = avio_size(pb); | |
475 | if (ffm->write_index && 0) | |
476 | adjust_write_index(s); | |
477 | } else { | |
478 | ffm->file_size = (UINT64_C(1) << 63) - 1; | |
479 | } | |
480 | ||
481 | nb_streams = avio_rb32(pb); | |
482 | avio_rb32(pb); /* total bitrate */ | |
483 | /* read each stream */ | |
484 | for(i=0;i<nb_streams;i++) { | |
485 | char rc_eq_buf[128]; | |
486 | ||
487 | st = avformat_new_stream(s, NULL); | |
488 | if (!st) | |
489 | goto fail; | |
490 | ||
491 | avpriv_set_pts_info(st, 64, 1, 1000000); | |
492 | ||
493 | codec = st->codec; | |
494 | /* generic info */ | |
495 | codec->codec_id = avio_rb32(pb); | |
496 | codec->codec_type = avio_r8(pb); /* codec_type */ | |
497 | codec->bit_rate = avio_rb32(pb); | |
498 | codec->flags = avio_rb32(pb); | |
499 | codec->flags2 = avio_rb32(pb); | |
500 | codec->debug = avio_rb32(pb); | |
501 | /* specific info */ | |
502 | switch(codec->codec_type) { | |
503 | case AVMEDIA_TYPE_VIDEO: | |
504 | codec->time_base.num = avio_rb32(pb); | |
505 | codec->time_base.den = avio_rb32(pb); | |
506 | codec->width = avio_rb16(pb); | |
507 | codec->height = avio_rb16(pb); | |
508 | codec->gop_size = avio_rb16(pb); | |
509 | codec->pix_fmt = avio_rb32(pb); | |
510 | codec->qmin = avio_r8(pb); | |
511 | codec->qmax = avio_r8(pb); | |
512 | codec->max_qdiff = avio_r8(pb); | |
513 | codec->qcompress = avio_rb16(pb) / 10000.0; | |
514 | codec->qblur = avio_rb16(pb) / 10000.0; | |
515 | codec->bit_rate_tolerance = avio_rb32(pb); | |
516 | avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf)); | |
517 | codec->rc_eq = av_strdup(rc_eq_buf); | |
518 | codec->rc_max_rate = avio_rb32(pb); | |
519 | codec->rc_min_rate = avio_rb32(pb); | |
520 | codec->rc_buffer_size = avio_rb32(pb); | |
521 | codec->i_quant_factor = av_int2double(avio_rb64(pb)); | |
522 | codec->b_quant_factor = av_int2double(avio_rb64(pb)); | |
523 | codec->i_quant_offset = av_int2double(avio_rb64(pb)); | |
524 | codec->b_quant_offset = av_int2double(avio_rb64(pb)); | |
525 | codec->dct_algo = avio_rb32(pb); | |
526 | codec->strict_std_compliance = avio_rb32(pb); | |
527 | codec->max_b_frames = avio_rb32(pb); | |
528 | codec->mpeg_quant = avio_rb32(pb); | |
529 | codec->intra_dc_precision = avio_rb32(pb); | |
530 | codec->me_method = avio_rb32(pb); | |
531 | codec->mb_decision = avio_rb32(pb); | |
532 | codec->nsse_weight = avio_rb32(pb); | |
533 | codec->frame_skip_cmp = avio_rb32(pb); | |
534 | codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb)); | |
535 | codec->codec_tag = avio_rb32(pb); | |
536 | codec->thread_count = avio_r8(pb); | |
537 | codec->coder_type = avio_rb32(pb); | |
538 | codec->me_cmp = avio_rb32(pb); | |
539 | codec->me_subpel_quality = avio_rb32(pb); | |
540 | codec->me_range = avio_rb32(pb); | |
541 | codec->keyint_min = avio_rb32(pb); | |
542 | codec->scenechange_threshold = avio_rb32(pb); | |
543 | codec->b_frame_strategy = avio_rb32(pb); | |
544 | codec->qcompress = av_int2double(avio_rb64(pb)); | |
545 | codec->qblur = av_int2double(avio_rb64(pb)); | |
546 | codec->max_qdiff = avio_rb32(pb); | |
547 | codec->refs = avio_rb32(pb); | |
548 | break; | |
549 | case AVMEDIA_TYPE_AUDIO: | |
550 | codec->sample_rate = avio_rb32(pb); | |
551 | codec->channels = avio_rl16(pb); | |
552 | codec->frame_size = avio_rl16(pb); | |
553 | break; | |
554 | default: | |
555 | goto fail; | |
556 | } | |
557 | if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) { | |
558 | if (ff_get_extradata(codec, pb, avio_rb32(pb)) < 0) | |
559 | return AVERROR(ENOMEM); | |
560 | } | |
561 | } | |
562 | ||
563 | /* get until end of block reached */ | |
564 | while ((avio_tell(pb) % ffm->packet_size) != 0) | |
565 | avio_r8(pb); | |
566 | ||
567 | /* init packet demux */ | |
568 | ffm->packet_ptr = ffm->packet; | |
569 | ffm->packet_end = ffm->packet; | |
570 | ffm->frame_offset = 0; | |
571 | ffm->dts = 0; | |
572 | ffm->read_state = READ_HEADER; | |
573 | ffm->first_packet = 1; | |
574 | return 0; | |
575 | fail: | |
576 | ffm_close(s); | |
577 | return -1; | |
578 | } | |
579 | ||
580 | /* return < 0 if eof */ | |
581 | static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt) | |
582 | { | |
583 | int size; | |
584 | FFMContext *ffm = s->priv_data; | |
585 | int duration, ret; | |
586 | ||
587 | switch(ffm->read_state) { | |
588 | case READ_HEADER: | |
589 | if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0) | |
590 | return ret; | |
591 | ||
592 | av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n", | |
593 | avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size); | |
594 | if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != | |
595 | FRAME_HEADER_SIZE) | |
596 | return -1; | |
597 | if (ffm->header[1] & FLAG_DTS) | |
598 | if (ffm_read_data(s, ffm->header+16, 4, 1) != 4) | |
599 | return -1; | |
600 | ffm->read_state = READ_DATA; | |
601 | /* fall through */ | |
602 | case READ_DATA: | |
603 | size = AV_RB24(ffm->header + 2); | |
604 | if ((ret = ffm_is_avail_data(s, size)) < 0) | |
605 | return ret; | |
606 | ||
607 | duration = AV_RB24(ffm->header + 5); | |
608 | ||
609 | if (av_new_packet(pkt, size) < 0) { | |
610 | return AVERROR(ENOMEM); | |
611 | } | |
612 | pkt->stream_index = ffm->header[0]; | |
613 | if ((unsigned)pkt->stream_index >= s->nb_streams) { | |
614 | av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index); | |
615 | av_free_packet(pkt); | |
616 | ffm->read_state = READ_HEADER; | |
617 | return -1; | |
618 | } | |
619 | pkt->pos = avio_tell(s->pb); | |
620 | if (ffm->header[1] & FLAG_KEY_FRAME) | |
621 | pkt->flags |= AV_PKT_FLAG_KEY; | |
622 | ||
623 | ffm->read_state = READ_HEADER; | |
624 | if (ffm_read_data(s, pkt->data, size, 0) != size) { | |
625 | /* bad case: desynchronized packet. we cancel all the packet loading */ | |
626 | av_free_packet(pkt); | |
627 | return -1; | |
628 | } | |
629 | pkt->pts = AV_RB64(ffm->header+8); | |
630 | if (ffm->header[1] & FLAG_DTS) | |
631 | pkt->dts = pkt->pts - AV_RB32(ffm->header+16); | |
632 | else | |
633 | pkt->dts = pkt->pts; | |
634 | pkt->duration = duration; | |
635 | break; | |
636 | } | |
637 | return 0; | |
638 | } | |
639 | ||
640 | /* seek to a given time in the file. The file read pointer is | |
641 | positioned at or before pts. XXX: the following code is quite | |
642 | approximative */ | |
643 | static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags) | |
644 | { | |
645 | FFMContext *ffm = s->priv_data; | |
646 | int64_t pos_min, pos_max, pos; | |
647 | int64_t pts_min, pts_max, pts; | |
648 | double pos1; | |
649 | ||
650 | av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0); | |
651 | /* find the position using linear interpolation (better than | |
652 | dichotomy in typical cases) */ | |
653 | if (ffm->write_index && ffm->write_index < ffm->file_size) { | |
654 | if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) { | |
655 | pos_min = FFM_PACKET_SIZE; | |
656 | pos_max = ffm->write_index - FFM_PACKET_SIZE; | |
657 | } else { | |
658 | pos_min = ffm->write_index; | |
659 | pos_max = ffm->file_size - FFM_PACKET_SIZE; | |
660 | } | |
661 | } else { | |
662 | pos_min = FFM_PACKET_SIZE; | |
663 | pos_max = ffm->file_size - FFM_PACKET_SIZE; | |
664 | } | |
665 | while (pos_min <= pos_max) { | |
666 | pts_min = get_dts(s, pos_min); | |
667 | pts_max = get_dts(s, pos_max); | |
668 | if (pts_min > wanted_pts || pts_max <= wanted_pts) { | |
669 | pos = pts_min > wanted_pts ? pos_min : pos_max; | |
670 | goto found; | |
671 | } | |
672 | /* linear interpolation */ | |
673 | pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) / | |
674 | (double)(pts_max - pts_min); | |
675 | pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE; | |
676 | if (pos <= pos_min) | |
677 | pos = pos_min; | |
678 | else if (pos >= pos_max) | |
679 | pos = pos_max; | |
680 | pts = get_dts(s, pos); | |
681 | /* check if we are lucky */ | |
682 | if (pts == wanted_pts) { | |
683 | goto found; | |
684 | } else if (pts > wanted_pts) { | |
685 | pos_max = pos - FFM_PACKET_SIZE; | |
686 | } else { | |
687 | pos_min = pos + FFM_PACKET_SIZE; | |
688 | } | |
689 | } | |
690 | pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max; | |
691 | ||
692 | found: | |
693 | if (ffm_seek1(s, pos) < 0) | |
694 | return -1; | |
695 | ||
696 | /* reset read state */ | |
697 | ffm->read_state = READ_HEADER; | |
698 | ffm->packet_ptr = ffm->packet; | |
699 | ffm->packet_end = ffm->packet; | |
700 | ffm->first_packet = 1; | |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
705 | static int ffm_probe(AVProbeData *p) | |
706 | { | |
707 | if ( | |
708 | p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' && | |
709 | (p->buf[3] == '1' || p->buf[3] == '2')) | |
710 | return AVPROBE_SCORE_MAX + 1; | |
711 | return 0; | |
712 | } | |
713 | ||
714 | AVInputFormat ff_ffm_demuxer = { | |
715 | .name = "ffm", | |
716 | .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"), | |
717 | .priv_data_size = sizeof(FFMContext), | |
718 | .read_probe = ffm_probe, | |
719 | .read_header = ffm_read_header, | |
720 | .read_packet = ffm_read_packet, | |
721 | .read_close = ffm_close, | |
722 | .read_seek = ffm_seek, | |
723 | }; |