comparison utils.c @ 2829:51790e849ad3 libavformat

Simplify av_open_input_file
author reimar
date Mon, 17 Dec 2007 19:08:17 +0000
parents ff463ed64256
children d7b2ac8d796d
comparison
equal deleted inserted replaced
2828:ff463ed64256 2829:51790e849ad3
415 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, 415 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
416 AVInputFormat *fmt, 416 AVInputFormat *fmt,
417 int buf_size, 417 int buf_size,
418 AVFormatParameters *ap) 418 AVFormatParameters *ap)
419 { 419 {
420 int err, must_open_file, file_opened, probe_size; 420 int err, probe_size;
421 AVProbeData probe_data, *pd = &probe_data; 421 AVProbeData probe_data, *pd = &probe_data;
422 ByteIOContext *pb; 422 ByteIOContext *pb = NULL;
423 423
424 file_opened = 0;
425 pd->filename = ""; 424 pd->filename = "";
426 if (filename) 425 if (filename)
427 pd->filename = filename; 426 pd->filename = filename;
428 pd->buf = NULL; 427 pd->buf = NULL;
429 pd->buf_size = 0; 428 pd->buf_size = 0;
433 fmt = av_probe_input_format(pd, 0); 432 fmt = av_probe_input_format(pd, 0);
434 } 433 }
435 434
436 /* do not open file if the format does not need it. XXX: specific 435 /* do not open file if the format does not need it. XXX: specific
437 hack needed to handle RTSP/TCP */ 436 hack needed to handle RTSP/TCP */
438 must_open_file = 1; 437 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
439 if (fmt && (fmt->flags & AVFMT_NOFILE)) {
440 must_open_file = 0;
441 pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise it is uninitialized
442 }
443
444 if (!fmt || must_open_file) {
445 /* if no file needed do not try to open one */ 438 /* if no file needed do not try to open one */
446 if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) { 439 if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) {
447 goto fail; 440 goto fail;
448 } 441 }
449 file_opened = 1;
450 if (buf_size > 0) { 442 if (buf_size > 0) {
451 url_setbufsize(pb, buf_size); 443 url_setbufsize(pb, buf_size);
452 } 444 }
453 445
454 for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){ 446 for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){
458 pd->buf_size = get_buffer(pb, pd->buf, probe_size); 450 pd->buf_size = get_buffer(pb, pd->buf, probe_size);
459 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE); 451 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
460 if (url_fseek(pb, 0, SEEK_SET) < 0) { 452 if (url_fseek(pb, 0, SEEK_SET) < 0) {
461 url_fclose(pb); 453 url_fclose(pb);
462 if (url_fopen(&pb, filename, URL_RDONLY) < 0) { 454 if (url_fopen(&pb, filename, URL_RDONLY) < 0) {
463 file_opened = 0; 455 pb = NULL;
464 err = AVERROR(EIO); 456 err = AVERROR(EIO);
465 goto fail; 457 goto fail;
466 } 458 }
467 } 459 }
468 /* guess file format */ 460 /* guess file format */
488 if (err) 480 if (err)
489 goto fail; 481 goto fail;
490 return 0; 482 return 0;
491 fail: 483 fail:
492 av_freep(&pd->buf); 484 av_freep(&pd->buf);
493 if (file_opened) 485 if (pb)
494 url_fclose(pb); 486 url_fclose(pb);
495 *ic_ptr = NULL; 487 *ic_ptr = NULL;
496 return err; 488 return err;
497 489
498 } 490 }