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