Skip to content

Commit

Permalink
Use the buffered AVIOContext in more places to reduce actual IO
Browse files Browse the repository at this point in the history
  • Loading branch information
andoma committed Apr 30, 2011
1 parent 9ccedb2 commit dcefaae
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 105 deletions.
33 changes: 14 additions & 19 deletions src/api/opensubtitles.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,31 +319,26 @@ opensub_add_subtitles(prop_t *node, htsmsg_t *query)
* http:https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes
*/
int
opensub_compute_hash(struct fa_handle *fh, uint64_t *hashp)
opensub_compute_hash(AVIOContext *avio, uint64_t *hashp)
{
int i;
uint64_t *buf = malloc(65536);
uint64_t hash = fa_fsize(fh);

fa_seek(fh, 0, SEEK_SET);
uint64_t n, hash, size = avio_size(avio);

if(size < 65536)
return -1;

if(fa_read(fh, buf, 65536) == 65536) {
hash = size;

for(i = 0; i < 8192; i++)
hash += buf[i];
n = avio_seek(avio, 0, SEEK_SET);

fa_seek(fh, -65536, SEEK_END);
for(i = 0; i < 8192; i++)
hash += avio_rl64(avio);

if(fa_read(fh, buf, 65536) == 65536) {
n = avio_seek(avio, size-65536, SEEK_SET);

for(i = 0; i < 8192; i++)
hash += buf[i];
for(i = 0; i < 8192; i++)
hash += avio_rl64(avio);

*hashp = hash;
free(buf);
return 0;
}
}
free(buf);
return -1;
*hashp = hash;
return 0;
}
4 changes: 2 additions & 2 deletions src/api/opensubtitles.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#ifndef OPENSUBTITLES_H__
#define OPENSUBTITLES_H__

#include <libavformat/avio.h>
#include "htsmsg/htsmsg.h"

struct fa_handle;
struct prop;

void opensub_init(void);

int opensub_compute_hash(struct fa_handle *fh, uint64_t *hashp);
int opensub_compute_hash(AVIOContext *avio, uint64_t *hashp);

htsmsg_t *opensub_build_query(const char *lang, int64_t hash, int64_t movsize,
const char *imdb, const char *title);
Expand Down
24 changes: 8 additions & 16 deletions src/fileaccess/fa_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*
*/
static event_t *
openspc_play(media_pipe_t *mp, void *fh, char *errbuf, size_t errlen)
openspc_play(media_pipe_t *mp, AVIOContext *avio, char *errbuf, size_t errlen)
{
media_queue_t *mq = &mp->mp_audio;
size_t r, siz = fa_fsize(fh);
Expand Down Expand Up @@ -222,46 +222,38 @@ be_file_playaudio(const char *url, media_pipe_t *mp,
media_codec_t *cw;
event_t *e;
int lost_focus = 0;
fa_handle_t *fh;

mp_set_playstatus_by_hold(mp, hold, NULL);

if((fh = fa_open(url, errbuf, errlen)) == NULL)
if((avio = fa_libav_open(url, 32768, errbuf, errlen)) == NULL)
return NULL;


// First we need to check for a few other formats
#if ENABLE_LIBOPENSPC || ENABLE_LIBGME

char pb[128];
uint8_t pb[128];
size_t psiz;

psiz = fa_read(fh, pb, sizeof(pb));
psiz = avio_read(avio, pb, sizeof(pb));
if(psiz < sizeof(pb)) {
fa_close(fh);
fa_libav_close(avio);
snprintf(errbuf, errlen, "Fill too small");
return NULL;
}

#if ENABLE_LIBGME
if(*gme_identify_header(pb)) {
fa_seek(fh, 0, SEEK_SET);
e = fa_gme_playfile(mp, fh, errbuf, errlen, hold);
fa_close(fh);
return e;
}
if(*gme_identify_header(pb))
return fa_gme_playfile(mp, avio, errbuf, errlen, hold);
#endif

#if ENABLE_LIBOPENSPC
if(!memcmp(pb, "SNES-SPC700 Sound File Data", 27))
return openspc_play(mp, fh, errbuf, errlen);
return openspc_play(mp, avio, errbuf, errlen);
#endif

fa_seek(fh, 0, SEEK_SET);
#endif

avio = fa_libav_reopen(fh, 32768);

if((fctx = fa_libav_open_format(avio, url, errbuf, errlen)) == NULL) {
fa_libav_close(avio);
return NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/fileaccess/fa_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

#include "media.h"
struct backend;
struct fa_handle;

event_t *be_file_playaudio(const char *url, media_pipe_t *mp,
char *errbuf, size_t errlen, int hold);

#if ENABLE_LIBGME
event_t *fa_gme_playfile(media_pipe_t *mp, void *fh,
event_t *fa_gme_playfile(media_pipe_t *mp, AVIOContext *avio,
char *errbuf, size_t errlen, int hold);
#endif

Expand Down
42 changes: 20 additions & 22 deletions src/fileaccess/fa_gmefile.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "fileaccess.h"
#include "fa_proto.h"
#include "fa_audio.h"
#include "fa_libav.h"
#include "misc/string.h"
#include "media.h"

Expand Down Expand Up @@ -192,32 +193,18 @@ deltaseek(media_pipe_t *mp, media_buf_t **mbp, Music_Emu *emu, int delta)
*
*/
static event_t *
fa_gme_playfile_internal(media_pipe_t *mp, void *fh,
fa_gme_playfile_internal(media_pipe_t *mp, void *buf, size_t size,
char *errbuf, size_t errlen, int hold, int track)
{
media_queue_t *mq = &mp->mp_audio;
Music_Emu *emu;
gme_err_t err;
char *buf;
int lost_focus = 0;
size_t size, r;
int sample_rate = 48000;
media_buf_t *mb = NULL;
event_t *e;

size = fa_fsize(fh);

buf = malloc(size);
r = fa_read(fh, buf, size);

if(r != size) {
snprintf(errbuf, errlen, "Unable to read file");
free(buf);
return NULL;
}

err = gme_open_data(buf, size, &emu, sample_rate);
free(buf);
if(err != NULL) {
snprintf(errbuf, errlen, "Unable to load file -- %s", err);
return NULL;
Expand Down Expand Up @@ -339,11 +326,21 @@ fa_gme_playfile_internal(media_pipe_t *mp, void *fh,
*
*/
event_t *
fa_gme_playfile(media_pipe_t *mp, void *fh,
fa_gme_playfile(media_pipe_t *mp, AVIOContext *avio,
char *errbuf, size_t errlen, int hold)
{
return fa_gme_playfile_internal(mp, fh, errbuf, errlen, hold, 0);
uint8_t *mem;
size_t size;
event_t *e;

if((mem = fa_libav_load_and_close(avio, &size)) == NULL) {
snprintf(errbuf, errlen, "Unable to read data from file");
return NULL;
}

e = fa_gme_playfile_internal(mp, mem, size, errbuf, errlen, hold, 0);
free(mem);
return e;
}

/**
Expand All @@ -356,7 +353,8 @@ be_gmeplayer_play(const char *url0, media_pipe_t *mp,
event_t *e;
char *url, *p;
int track;
void *fh;
void *mem;
struct fa_stat fs;

url0 += strlen("gmeplayer:");

Expand All @@ -370,13 +368,13 @@ be_gmeplayer_play(const char *url0, media_pipe_t *mp,
*p++= 0;
track = atoi(p) - 1;

if((fh = fa_open(url, errbuf, errlen)) == NULL)
if((mem = fa_quickload(url, &fs, NULL, errbuf, errlen)) == NULL)
return NULL;

e = fa_gme_playfile_internal(mp, fh, errbuf, errlen, hold, track);
fa_close(fh);
e = fa_gme_playfile_internal(mp, mem, fs.fs_size,
errbuf, errlen, hold, track);
free(mem);
return e;

}


Expand Down
33 changes: 32 additions & 1 deletion src/fileaccess/fa_libav.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fa_libav_seek(void *opaque, int64_t offset, int whence)
fa_handle_t *fh = opaque;
if(whence == AVSEEK_SIZE)
return fa_fsize(fh);

return fa_seek(fh, offset, whence & ~AVSEEK_FORCE);
}

Expand All @@ -46,8 +47,13 @@ fa_libav_seek(void *opaque, int64_t offset, int whence)
*
*/
AVIOContext *
fa_libav_reopen(fa_handle_t *fh, int buf_size)
fa_libav_open(const char *url, int buf_size, char *errbuf, size_t errlen)
{
fa_handle_t *fh;

if((fh = fa_open(url, errbuf, errlen)) == NULL)
return NULL;

if(buf_size == 0)
buf_size = 32768;
void *buf = malloc(buf_size);
Expand Down Expand Up @@ -131,3 +137,28 @@ fa_libav_close_format(AVFormatContext *fctx)
av_close_input_stream(fctx);
fa_libav_close(avio);
}


/**
*
*/
uint8_t *
fa_libav_load_and_close(AVIOContext *avio, size_t *sizep)
{
size_t r, size = avio_size(avio);
uint8_t *mem = malloc(size+1);

avio_seek(avio, 0, SEEK_SET);
r = avio_read(avio, mem, size);
fa_libav_close(avio);

if(r != size) {
free(mem);
return NULL;
}

if(sizep != NULL)
*sizep = size;
mem[size] = 0;
return mem;
}
5 changes: 4 additions & 1 deletion src/fileaccess/fa_libav.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

#include "fileaccess.h"

AVIOContext *fa_libav_reopen(fa_handle_t *fh, int buf_size);
AVIOContext *fa_libav_open(const char *url, int buf_size,
char *errbuf, size_t errlen);

void fa_libav_close(AVIOContext *io);

AVFormatContext *fa_libav_open_format(AVIOContext *avio, const char *url,
char *errbuf, size_t errlen);

void fa_libav_close_format(AVFormatContext *fctx);

uint8_t *fa_libav_load_and_close(AVIOContext *avio, size_t *sizep);
20 changes: 8 additions & 12 deletions src/fileaccess/fa_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ fa_probe_header(metadata_t *md, const char *url, AVIOContext *avio)
* of data starting 0x8000 of start of file
*/
static int
fa_probe_iso0(metadata_t *md, char *pb)
fa_probe_iso0(metadata_t *md, uint8_t *pb)
{
char *p;
uint8_t *p;

if(memcmp(pb, isosig, 8))
return -1;
Expand All @@ -449,7 +449,7 @@ fa_probe_iso0(metadata_t *md, char *pb)
*p = 0;

if(md != NULL) {
md->md_title = rstr_alloc(pb + 40);
md->md_title = rstr_alloc((const char *)pb + 40);
md->md_type = CONTENT_DVD;
}
return 0;
Expand All @@ -462,14 +462,14 @@ fa_probe_iso0(metadata_t *md, char *pb)
* pb is guaranteed to point at 64k of data
*/
int
fa_probe_iso(metadata_t *md, fa_handle_t *fh)
fa_probe_iso(metadata_t *md, AVIOContext *avio)
{
char pb[128];
uint8_t pb[128];

if(fa_seek(fh, 0x8000, SEEK_SET) != 0x8000)
if(avio_seek(avio, 0x8000, SEEK_SET) != 0x8000)
return -1;

if(fa_read(fh, pb, sizeof(pb)) != sizeof(pb))
if(avio_read(avio, pb, sizeof(pb)) != sizeof(pb))
return -1;
return fa_probe_iso0(md, pb);
}
Expand Down Expand Up @@ -661,16 +661,12 @@ static int
fa_probe_fill_cache(metadata_t *md, const char *url, char *errbuf,
size_t errsize, struct fa_stat *fs)
{
fa_handle_t *fh;
AVFormatContext *fctx;
AVIOContext *avio;

if((fh = fa_open(url, errbuf, errsize)) == NULL)
if((avio = fa_libav_open(url, 32768, errbuf, errsize)) == NULL)
return -1;

avio = fa_libav_reopen(fh, 32768);


#if ENABLE_LIBGME
if(gme_probe(md, url, avio))
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/fileaccess/fa_probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ unsigned int fa_probe(prop_t *proproot, const char *url,

unsigned int fa_probe_dir(prop_t *proproot, const char *url);

int fa_probe_iso(struct metadata *md, fa_handle_t *fh);
int fa_probe_iso(struct metadata *md, AVIOContext *avio);

void fa_probe_load_metaprop(prop_t *p, AVFormatContext *fctx, const char *url);

Expand Down
Loading

0 comments on commit dcefaae

Please sign in to comment.