comparison ffmdec.c @ 4756:d426504e401b libavformat

do not write ffm write index by default, detect if file is being written and return EOF
author bcoudurier
date Fri, 20 Mar 2009 01:23:33 +0000
parents 241532434509
children 02a9d3d1345b
comparison
equal deleted inserted replaced
4755:e8d5fa636916 4756:d426504e401b
64 64
65 len = ffm->packet_end - ffm->packet_ptr; 65 len = ffm->packet_end - ffm->packet_ptr;
66 if (size <= len) 66 if (size <= len)
67 return 1; 67 return 1;
68 pos = url_ftell(s->pb); 68 pos = url_ftell(s->pb);
69 if (!ffm->write_index) {
70 if (pos == ffm->file_size);
71 return AVERROR_EOF;
72 avail_size = ffm->file_size - pos;
73 } else {
69 if (pos == ffm->write_index) { 74 if (pos == ffm->write_index) {
70 /* exactly at the end of stream */ 75 /* exactly at the end of stream */
71 return 0; 76 return AVERROR(EAGAIN);
72 } else if (pos < ffm->write_index) { 77 } else if (pos < ffm->write_index) {
73 avail_size = ffm->write_index - pos; 78 avail_size = ffm->write_index - pos;
74 } else { 79 } else {
75 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE); 80 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
76 } 81 }
82 }
77 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len; 83 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
78 if (size <= avail_size) 84 if (size <= avail_size)
79 return 1; 85 return 1;
80 else 86 else
81 return 0; 87 return AVERROR(EAGAIN);
82 } 88 }
83 89
84 /* first is true if we read the frame header */ 90 /* first is true if we read the frame header */
85 static int ffm_read_data(AVFormatContext *s, 91 static int ffm_read_data(AVFormatContext *s,
86 uint8_t *buf, int size, int header) 92 uint8_t *buf, int size, int header)
249 goto fail; 255 goto fail;
250 ffm->write_index = get_be64(pb); 256 ffm->write_index = get_be64(pb);
251 /* get also filesize */ 257 /* get also filesize */
252 if (!url_is_streamed(pb)) { 258 if (!url_is_streamed(pb)) {
253 ffm->file_size = url_fsize(pb); 259 ffm->file_size = url_fsize(pb);
254 adjust_write_index(s); 260 if (ffm->write_index)
261 adjust_write_index(s);
255 } else { 262 } else {
256 ffm->file_size = (UINT64_C(1) << 63) - 1; 263 ffm->file_size = (UINT64_C(1) << 63) - 1;
257 } 264 }
258 265
259 nb_streams = get_be32(pb); 266 nb_streams = get_be32(pb);
358 /* return < 0 if eof */ 365 /* return < 0 if eof */
359 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt) 366 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
360 { 367 {
361 int size; 368 int size;
362 FFMContext *ffm = s->priv_data; 369 FFMContext *ffm = s->priv_data;
363 int duration; 370 int duration, ret;
364
365 if (url_fsize(s->pb) == FFM_PACKET_SIZE)
366 return -1;
367 371
368 switch(ffm->read_state) { 372 switch(ffm->read_state) {
369 case READ_HEADER: 373 case READ_HEADER:
370 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) { 374 if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
371 return AVERROR(EAGAIN); 375 return ret;
372 } 376
373 dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n", 377 dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
374 url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size); 378 url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
375 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != 379 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
376 FRAME_HEADER_SIZE) 380 FRAME_HEADER_SIZE)
377 return AVERROR(EAGAIN); 381 return -1;
378 if (ffm->header[1] & FLAG_DTS) 382 if (ffm->header[1] & FLAG_DTS)
379 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4) 383 if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
380 return AVERROR(EAGAIN); 384 return -1;
381 #if 0 385 #if 0
382 av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE); 386 av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
383 #endif 387 #endif
384 ffm->read_state = READ_DATA; 388 ffm->read_state = READ_DATA;
385 /* fall thru */ 389 /* fall thru */
386 case READ_DATA: 390 case READ_DATA:
387 size = AV_RB24(ffm->header + 2); 391 size = AV_RB24(ffm->header + 2);
388 if (!ffm_is_avail_data(s, size)) { 392 if ((ret = ffm_is_avail_data(s, size)) < 0)
389 return AVERROR(EAGAIN); 393 return ret;
390 }
391 394
392 duration = AV_RB24(ffm->header + 5); 395 duration = AV_RB24(ffm->header + 5);
393 396
394 av_new_packet(pkt, size); 397 av_new_packet(pkt, size);
395 pkt->stream_index = ffm->header[0]; 398 pkt->stream_index = ffm->header[0];
396 if ((unsigned)pkt->stream_index >= s->nb_streams) { 399 if ((unsigned)pkt->stream_index >= s->nb_streams) {
397 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index); 400 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
398 av_free_packet(pkt); 401 av_free_packet(pkt);
399 ffm->read_state = READ_HEADER; 402 ffm->read_state = READ_HEADER;
400 return AVERROR(EAGAIN); 403 return -1;
401 } 404 }
402 pkt->pos = url_ftell(s->pb); 405 pkt->pos = url_ftell(s->pb);
403 if (ffm->header[1] & FLAG_KEY_FRAME) 406 if (ffm->header[1] & FLAG_KEY_FRAME)
404 pkt->flags |= PKT_FLAG_KEY; 407 pkt->flags |= PKT_FLAG_KEY;
405 408
406 ffm->read_state = READ_HEADER; 409 ffm->read_state = READ_HEADER;
407 if (ffm_read_data(s, pkt->data, size, 0) != size) { 410 if (ffm_read_data(s, pkt->data, size, 0) != size) {
408 /* bad case: desynchronized packet. we cancel all the packet loading */ 411 /* bad case: desynchronized packet. we cancel all the packet loading */
409 av_free_packet(pkt); 412 av_free_packet(pkt);
410 return AVERROR(EAGAIN); 413 return -1;
411 } 414 }
412 pkt->pts = AV_RB64(ffm->header+8); 415 pkt->pts = AV_RB64(ffm->header+8);
413 if (ffm->header[1] & FLAG_DTS) 416 if (ffm->header[1] & FLAG_DTS)
414 pkt->dts = pkt->pts - AV_RB32(ffm->header+16); 417 pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
415 else 418 else