libtags: opus: fix duration on truncated files

This commit is contained in:
Sigrid Solveig Haflínudóttir 2021-09-28 01:17:10 +00:00
parent df25039bb3
commit bd63aeb60d

View file

@ -8,8 +8,7 @@ tagopus(Tagctx *ctx)
int sz, numtags, i, npages; int sz, numtags, i, npages;
d = (uchar*)ctx->buf; d = (uchar*)ctx->buf;
/* need to find vorbis frame with type=3 */ for(npages = 0; npages < 2; npages++){
for(npages = 0; npages < 2; npages++){ /* vorbis comment is the second header */
int nsegs; int nsegs;
if(ctx->read(ctx, d, 27) != 27) if(ctx->read(ctx, d, 27) != 27)
return -1; return -1;
@ -76,7 +75,7 @@ tagopus(Tagctx *ctx)
break; break;
for(; v != nil && v < ctx->buf+sz;){ for(; v != nil && v < ctx->buf+sz;){
v = memchr(v, 'O', ctx->buf+sz - v - 14); v = memchr(v, 'O', ctx->buf+sz - v - 14);
if(v != nil && v[1] == 'g' && v[2] == 'g' && v[3] == 'S' && (v[5] & 4) == 4){ /* last page */ if(v != nil && v[1] == 'g' && v[2] == 'g' && v[3] == 'S'){
uvlong g = leuint(v+6) | (uvlong)leuint(v+10)<<32; uvlong g = leuint(v+6) | (uvlong)leuint(v+10)<<32;
ctx->duration = g * 1000 / 48000; /* granule positions are always 48KHz */ ctx->duration = g * 1000 / 48000; /* granule positions are always 48KHz */
return 0; return 0;