Mercurial > libavformat.hg
comparison utils.c @ 2771:d52c718e83f9 libavformat
Use dynamically allocated ByteIOContext in AVFormatContext
patch by: Bj«Órn Axelsson, bjorn d axelsson a intinor d se
thread: [PATCH] Remove static ByteIOContexts, 06 nov 2007
author | andoma |
---|---|
date | Wed, 21 Nov 2007 07:41:00 +0000 |
parents | 1b2658f20244 |
children | d3b3cfb538d8 |
comparison
equal
deleted
inserted
replaced
2770:a7e42cf4b364 | 2771:d52c718e83f9 |
---|---|
359 if (!ic) { | 359 if (!ic) { |
360 err = AVERROR(ENOMEM); | 360 err = AVERROR(ENOMEM); |
361 goto fail; | 361 goto fail; |
362 } | 362 } |
363 ic->iformat = fmt; | 363 ic->iformat = fmt; |
364 if (pb) | 364 ic->pb = pb; |
365 ic->pb = *pb; | |
366 ic->duration = AV_NOPTS_VALUE; | 365 ic->duration = AV_NOPTS_VALUE; |
367 ic->start_time = AV_NOPTS_VALUE; | 366 ic->start_time = AV_NOPTS_VALUE; |
368 av_strlcpy(ic->filename, filename, sizeof(ic->filename)); | 367 av_strlcpy(ic->filename, filename, sizeof(ic->filename)); |
369 | 368 |
370 /* allocate private data */ | 369 /* allocate private data */ |
381 err = ic->iformat->read_header(ic, ap); | 380 err = ic->iformat->read_header(ic, ap); |
382 if (err < 0) | 381 if (err < 0) |
383 goto fail; | 382 goto fail; |
384 | 383 |
385 if (pb && !ic->data_offset) | 384 if (pb && !ic->data_offset) |
386 ic->data_offset = url_ftell(&ic->pb); | 385 ic->data_offset = url_ftell(ic->pb); |
387 | 386 |
388 *ic_ptr = ic; | 387 *ic_ptr = ic; |
389 return 0; | 388 return 0; |
390 fail: | 389 fail: |
391 if (ic) { | 390 if (ic) { |
405 int buf_size, | 404 int buf_size, |
406 AVFormatParameters *ap) | 405 AVFormatParameters *ap) |
407 { | 406 { |
408 int err, must_open_file, file_opened, probe_size; | 407 int err, must_open_file, file_opened, probe_size; |
409 AVProbeData probe_data, *pd = &probe_data; | 408 AVProbeData probe_data, *pd = &probe_data; |
410 ByteIOContext pb1, *pb = &pb1; | 409 ByteIOContext *pb; |
411 | 410 |
412 file_opened = 0; | 411 file_opened = 0; |
413 pd->filename = ""; | 412 pd->filename = ""; |
414 if (filename) | 413 if (filename) |
415 pd->filename = filename; | 414 pd->filename = filename; |
429 pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise it is uninitialized | 428 pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise it is uninitialized |
430 } | 429 } |
431 | 430 |
432 if (!fmt || must_open_file) { | 431 if (!fmt || must_open_file) { |
433 /* if no file needed do not try to open one */ | 432 /* if no file needed do not try to open one */ |
434 if ((err=url_fopen(pb, filename, URL_RDONLY)) < 0) { | 433 if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) { |
435 goto fail; | 434 goto fail; |
436 } | 435 } |
437 file_opened = 1; | 436 file_opened = 1; |
438 if (buf_size > 0) { | 437 if (buf_size > 0) { |
439 url_setbufsize(pb, buf_size); | 438 url_setbufsize(pb, buf_size); |
445 pd->buf= av_realloc(pd->buf, probe_size + AVPROBE_PADDING_SIZE); | 444 pd->buf= av_realloc(pd->buf, probe_size + AVPROBE_PADDING_SIZE); |
446 pd->buf_size = get_buffer(pb, pd->buf, probe_size); | 445 pd->buf_size = get_buffer(pb, pd->buf, probe_size); |
447 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE); | 446 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE); |
448 if (url_fseek(pb, 0, SEEK_SET) < 0) { | 447 if (url_fseek(pb, 0, SEEK_SET) < 0) { |
449 url_fclose(pb); | 448 url_fclose(pb); |
450 if (url_fopen(pb, filename, URL_RDONLY) < 0) { | 449 if (url_fopen(&pb, filename, URL_RDONLY) < 0) { |
451 file_opened = 0; | 450 file_opened = 0; |
452 err = AVERROR(EIO); | 451 err = AVERROR(EIO); |
453 goto fail; | 452 goto fail; |
454 } | 453 } |
455 } | 454 } |
1128 pos= av_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp); | 1127 pos= av_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp); |
1129 if(pos<0) | 1128 if(pos<0) |
1130 return -1; | 1129 return -1; |
1131 | 1130 |
1132 /* do the seek */ | 1131 /* do the seek */ |
1133 url_fseek(&s->pb, pos, SEEK_SET); | 1132 url_fseek(s->pb, pos, SEEK_SET); |
1134 | 1133 |
1135 av_update_cur_dts(s, st, ts); | 1134 av_update_cur_dts(s, st, ts); |
1136 | 1135 |
1137 return 0; | 1136 return 0; |
1138 } | 1137 } |
1153 return -1; | 1152 return -1; |
1154 } | 1153 } |
1155 | 1154 |
1156 if(ts_max == AV_NOPTS_VALUE){ | 1155 if(ts_max == AV_NOPTS_VALUE){ |
1157 int step= 1024; | 1156 int step= 1024; |
1158 filesize = url_fsize(&s->pb); | 1157 filesize = url_fsize(s->pb); |
1159 pos_max = filesize - 1; | 1158 pos_max = filesize - 1; |
1160 do{ | 1159 do{ |
1161 pos_max -= step; | 1160 pos_max -= step; |
1162 ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step); | 1161 ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step); |
1163 step += step; | 1162 step += step; |
1259 | 1258 |
1260 st= s->streams[stream_index]; | 1259 st= s->streams[stream_index]; |
1261 #endif | 1260 #endif |
1262 | 1261 |
1263 pos_min = s->data_offset; | 1262 pos_min = s->data_offset; |
1264 pos_max = url_fsize(&s->pb) - 1; | 1263 pos_max = url_fsize(s->pb) - 1; |
1265 | 1264 |
1266 if (pos < pos_min) pos= pos_min; | 1265 if (pos < pos_min) pos= pos_min; |
1267 else if(pos > pos_max) pos= pos_max; | 1266 else if(pos > pos_max) pos= pos_max; |
1268 | 1267 |
1269 url_fseek(&s->pb, pos, SEEK_SET); | 1268 url_fseek(s->pb, pos, SEEK_SET); |
1270 | 1269 |
1271 #if 0 | 1270 #if 0 |
1272 av_update_cur_dts(s, st, ts); | 1271 av_update_cur_dts(s, st, ts); |
1273 #endif | 1272 #endif |
1274 return 0; | 1273 return 0; |
1289 int i; | 1288 int i; |
1290 AVPacket pkt; | 1289 AVPacket pkt; |
1291 | 1290 |
1292 if(st->index_entries && st->nb_index_entries){ | 1291 if(st->index_entries && st->nb_index_entries){ |
1293 ie= &st->index_entries[st->nb_index_entries-1]; | 1292 ie= &st->index_entries[st->nb_index_entries-1]; |
1294 url_fseek(&s->pb, ie->pos, SEEK_SET); | 1293 url_fseek(s->pb, ie->pos, SEEK_SET); |
1295 av_update_cur_dts(s, st, ie->timestamp); | 1294 av_update_cur_dts(s, st, ie->timestamp); |
1296 }else | 1295 }else |
1297 url_fseek(&s->pb, 0, SEEK_SET); | 1296 url_fseek(s->pb, 0, SEEK_SET); |
1298 | 1297 |
1299 for(i=0;; i++) { | 1298 for(i=0;; i++) { |
1300 int ret = av_read_frame(s, &pkt); | 1299 int ret = av_read_frame(s, &pkt); |
1301 if(ret<0) | 1300 if(ret<0) |
1302 break; | 1301 break; |
1315 if (s->iformat->read_seek){ | 1314 if (s->iformat->read_seek){ |
1316 if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0) | 1315 if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0) |
1317 return 0; | 1316 return 0; |
1318 } | 1317 } |
1319 ie = &st->index_entries[index]; | 1318 ie = &st->index_entries[index]; |
1320 url_fseek(&s->pb, ie->pos, SEEK_SET); | 1319 url_fseek(s->pb, ie->pos, SEEK_SET); |
1321 | 1320 |
1322 av_update_cur_dts(s, st, ie->timestamp); | 1321 av_update_cur_dts(s, st, ie->timestamp); |
1323 | 1322 |
1324 return 0; | 1323 return 0; |
1325 } | 1324 } |
1507 } | 1506 } |
1508 } | 1507 } |
1509 | 1508 |
1510 /* we read the first packets to get the first PTS (not fully | 1509 /* we read the first packets to get the first PTS (not fully |
1511 accurate, but it is enough now) */ | 1510 accurate, but it is enough now) */ |
1512 url_fseek(&ic->pb, 0, SEEK_SET); | 1511 url_fseek(ic->pb, 0, SEEK_SET); |
1513 read_size = 0; | 1512 read_size = 0; |
1514 for(;;) { | 1513 for(;;) { |
1515 if (read_size >= DURATION_MAX_READ_SIZE) | 1514 if (read_size >= DURATION_MAX_READ_SIZE) |
1516 break; | 1515 break; |
1517 /* if all info is available, we can stop */ | 1516 /* if all info is available, we can stop */ |
1540 filesize = ic->file_size; | 1539 filesize = ic->file_size; |
1541 offset = filesize - DURATION_MAX_READ_SIZE; | 1540 offset = filesize - DURATION_MAX_READ_SIZE; |
1542 if (offset < 0) | 1541 if (offset < 0) |
1543 offset = 0; | 1542 offset = 0; |
1544 | 1543 |
1545 url_fseek(&ic->pb, offset, SEEK_SET); | 1544 url_fseek(ic->pb, offset, SEEK_SET); |
1546 read_size = 0; | 1545 read_size = 0; |
1547 for(;;) { | 1546 for(;;) { |
1548 if (read_size >= DURATION_MAX_READ_SIZE) | 1547 if (read_size >= DURATION_MAX_READ_SIZE) |
1549 break; | 1548 break; |
1550 | 1549 |
1566 av_free_packet(pkt); | 1565 av_free_packet(pkt); |
1567 } | 1566 } |
1568 | 1567 |
1569 fill_all_stream_timings(ic); | 1568 fill_all_stream_timings(ic); |
1570 | 1569 |
1571 url_fseek(&ic->pb, old_offset, SEEK_SET); | 1570 url_fseek(ic->pb, old_offset, SEEK_SET); |
1572 for(i=0; i<ic->nb_streams; i++){ | 1571 for(i=0; i<ic->nb_streams; i++){ |
1573 st= ic->streams[i]; | 1572 st= ic->streams[i]; |
1574 st->cur_dts= st->first_dts; | 1573 st->cur_dts= st->first_dts; |
1575 st->last_IP_pts = AV_NOPTS_VALUE; | 1574 st->last_IP_pts = AV_NOPTS_VALUE; |
1576 } | 1575 } |
1582 | 1581 |
1583 /* get the file size, if possible */ | 1582 /* get the file size, if possible */ |
1584 if (ic->iformat->flags & AVFMT_NOFILE) { | 1583 if (ic->iformat->flags & AVFMT_NOFILE) { |
1585 file_size = 0; | 1584 file_size = 0; |
1586 } else { | 1585 } else { |
1587 file_size = url_fsize(&ic->pb); | 1586 file_size = url_fsize(ic->pb); |
1588 if (file_size < 0) | 1587 if (file_size < 0) |
1589 file_size = 0; | 1588 file_size = 0; |
1590 } | 1589 } |
1591 ic->file_size = file_size; | 1590 ic->file_size = file_size; |
1592 | 1591 |
1593 if ((!strcmp(ic->iformat->name, "mpeg") || | 1592 if ((!strcmp(ic->iformat->name, "mpeg") || |
1594 !strcmp(ic->iformat->name, "mpegts")) && | 1593 !strcmp(ic->iformat->name, "mpegts")) && |
1595 file_size && !ic->pb.is_streamed) { | 1594 file_size && !ic->pb->is_streamed) { |
1596 /* get accurate estimate from the PTSes */ | 1595 /* get accurate estimate from the PTSes */ |
1597 av_estimate_timings_from_pts(ic, old_offset); | 1596 av_estimate_timings_from_pts(ic, old_offset); |
1598 } else if (av_has_duration(ic)) { | 1597 } else if (av_has_duration(ic)) { |
1599 /* at least one components has timings - we use them for all | 1598 /* at least one components has timings - we use them for all |
1600 the components */ | 1599 the components */ |
1755 AVStream *st; | 1754 AVStream *st; |
1756 AVPacket pkt1, *pkt; | 1755 AVPacket pkt1, *pkt; |
1757 int64_t last_dts[MAX_STREAMS]; | 1756 int64_t last_dts[MAX_STREAMS]; |
1758 int duration_count[MAX_STREAMS]={0}; | 1757 int duration_count[MAX_STREAMS]={0}; |
1759 double (*duration_error)[MAX_STD_TIMEBASES]; | 1758 double (*duration_error)[MAX_STD_TIMEBASES]; |
1760 offset_t old_offset = url_ftell(&ic->pb); | 1759 offset_t old_offset = url_ftell(ic->pb); |
1761 int64_t codec_info_duration[MAX_STREAMS]={0}; | 1760 int64_t codec_info_duration[MAX_STREAMS]={0}; |
1762 int codec_info_nb_frames[MAX_STREAMS]={0}; | 1761 int codec_info_nb_frames[MAX_STREAMS]={0}; |
1763 AVProbeData probe_data[MAX_STREAMS]; | 1762 AVProbeData probe_data[MAX_STREAMS]; |
1764 int codec_identified[MAX_STREAMS]={0}; | 1763 int codec_identified[MAX_STREAMS]={0}; |
1765 | 1764 |
1987 if (codec_identified[st->index]) { | 1986 if (codec_identified[st->index]) { |
1988 av_seek_frame(ic, st->index, 0.0, 0); | 1987 av_seek_frame(ic, st->index, 0.0, 0); |
1989 } | 1988 } |
1990 st->cur_dts= st->first_dts; | 1989 st->cur_dts= st->first_dts; |
1991 } | 1990 } |
1992 url_fseek(&ic->pb, ic->data_offset, SEEK_SET); | 1991 url_fseek(ic->pb, ic->data_offset, SEEK_SET); |
1993 } | 1992 } |
1994 | 1993 |
1995 #if 0 | 1994 #if 0 |
1996 /* correct DTS for b frame streams with no timestamps */ | 1995 /* correct DTS for b frame streams with no timestamps */ |
1997 for(i=0;i<ic->nb_streams;i++) { | 1996 for(i=0;i<ic->nb_streams;i++) { |
2073 must_open_file = 1; | 2072 must_open_file = 1; |
2074 if (s->iformat->flags & AVFMT_NOFILE) { | 2073 if (s->iformat->flags & AVFMT_NOFILE) { |
2075 must_open_file = 0; | 2074 must_open_file = 0; |
2076 } | 2075 } |
2077 if (must_open_file) { | 2076 if (must_open_file) { |
2078 url_fclose(&s->pb); | 2077 url_fclose(s->pb); |
2079 } | 2078 } |
2080 av_freep(&s->priv_data); | 2079 av_freep(&s->priv_data); |
2081 av_free(s); | 2080 av_free(s); |
2082 } | 2081 } |
2083 | 2082 |
2341 | 2340 |
2342 truncate_ts(s->streams[pkt->stream_index], pkt); | 2341 truncate_ts(s->streams[pkt->stream_index], pkt); |
2343 | 2342 |
2344 ret= s->oformat->write_packet(s, pkt); | 2343 ret= s->oformat->write_packet(s, pkt); |
2345 if(!ret) | 2344 if(!ret) |
2346 ret= url_ferror(&s->pb); | 2345 ret= url_ferror(s->pb); |
2347 return ret; | 2346 return ret; |
2348 } | 2347 } |
2349 | 2348 |
2350 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){ | 2349 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){ |
2351 AVPacketList *pktl, **next_point, *this_pktl; | 2350 AVPacketList *pktl, **next_point, *this_pktl; |
2442 av_free_packet(&opkt); | 2441 av_free_packet(&opkt); |
2443 pkt= NULL; | 2442 pkt= NULL; |
2444 | 2443 |
2445 if(ret<0) | 2444 if(ret<0) |
2446 return ret; | 2445 return ret; |
2447 if(url_ferror(&s->pb)) | 2446 if(url_ferror(s->pb)) |
2448 return url_ferror(&s->pb); | 2447 return url_ferror(s->pb); |
2449 } | 2448 } |
2450 } | 2449 } |
2451 | 2450 |
2452 int av_write_trailer(AVFormatContext *s) | 2451 int av_write_trailer(AVFormatContext *s) |
2453 { | 2452 { |
2466 | 2465 |
2467 av_free_packet(&pkt); | 2466 av_free_packet(&pkt); |
2468 | 2467 |
2469 if(ret<0) | 2468 if(ret<0) |
2470 goto fail; | 2469 goto fail; |
2471 if(url_ferror(&s->pb)) | 2470 if(url_ferror(s->pb)) |
2472 goto fail; | 2471 goto fail; |
2473 } | 2472 } |
2474 | 2473 |
2475 if(s->oformat->write_trailer) | 2474 if(s->oformat->write_trailer) |
2476 ret = s->oformat->write_trailer(s); | 2475 ret = s->oformat->write_trailer(s); |
2477 fail: | 2476 fail: |
2478 if(ret == 0) | 2477 if(ret == 0) |
2479 ret=url_ferror(&s->pb); | 2478 ret=url_ferror(s->pb); |
2480 for(i=0;i<s->nb_streams;i++) | 2479 for(i=0;i<s->nb_streams;i++) |
2481 av_freep(&s->streams[i]->priv_data); | 2480 av_freep(&s->streams[i]->priv_data); |
2482 av_freep(&s->priv_data); | 2481 av_freep(&s->priv_data); |
2483 return ret; | 2482 return ret; |
2484 } | 2483 } |