| 1 |
/* |
| 2 |
sampleadjust: gapless sample offset math |
| 3 |
|
| 4 |
copyright 1995-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
|
| 7 |
This is no stand-alone header, precisely to be able to fool it into using fake handle types for testing the math. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "../common/debug.h" |
| 11 |
|
| 12 |
#ifdef GAPLESS |
| 13 |
/* From internal sample number to external. */ |
| 14 |
static int64_t sample_adjust(mpg123_handle *mh, int64_t x) |
| 15 |
{ |
| 16 |
int64_t s; |
| 17 |
if(mh->p.flags & MPG123_GAPLESS) |
| 18 |
{ |
| 19 |
/* It's a bit tricky to do this computation for the padding samples. |
| 20 |
They are not there on the outside. */ |
| 21 |
if(x > mh->end_os) |
| 22 |
{ |
| 23 |
if(x < mh->fullend_os) |
| 24 |
s = mh->end_os - mh->begin_os; |
| 25 |
else |
| 26 |
s = x - (mh->fullend_os - mh->end_os + mh->begin_os); |
| 27 |
} |
| 28 |
else |
| 29 |
s = x - mh->begin_os; |
| 30 |
} |
| 31 |
else |
| 32 |
s = x; |
| 33 |
|
| 34 |
return s; |
| 35 |
} |
| 36 |
|
| 37 |
/* from external samples to internal */ |
| 38 |
static int64_t sample_unadjust(mpg123_handle *mh, int64_t x) |
| 39 |
{ |
| 40 |
int64_t s; |
| 41 |
if(mh->p.flags & MPG123_GAPLESS) |
| 42 |
{ |
| 43 |
s = x + mh->begin_os; |
| 44 |
/* There is a hole; we don't create sample positions in there. |
| 45 |
Jump from the end of the gapless track directly to after the padding. */ |
| 46 |
if(s >= mh->end_os) |
| 47 |
s += mh->fullend_os - mh->end_os; |
| 48 |
} |
| 49 |
else s = x; |
| 50 |
|
| 51 |
return s; |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
Take the buffer after a frame decode (strictly: it is the data from frame fr->num!) and cut samples out. |
| 56 |
fr->buffer.fill may then be smaller than before... |
| 57 |
*/ |
| 58 |
static void frame_buffercheck(mpg123_handle *fr) |
| 59 |
{ |
| 60 |
/* When we have no accurate position, gapless code does not make sense. */ |
| 61 |
if(!(fr->state_flags & FRAME_ACCURATE)) return; |
| 62 |
|
| 63 |
/* Get a grip on dirty streams that start with a gapless header. |
| 64 |
Simply accept all data from frames that are too much, |
| 65 |
they are supposedly attached to the stream after the fact. */ |
| 66 |
if(fr->gapless_frames > 0 && fr->num >= fr->gapless_frames) return; |
| 67 |
|
| 68 |
/* Important: We first cut samples from the end, then cut from beginning (including left-shift of the buffer). |
| 69 |
This order works also for the case where firstframe == lastframe. */ |
| 70 |
|
| 71 |
/* The last interesting (planned) frame: Only use some leading samples. |
| 72 |
Note a difference from the below: The last frame and offset are unchanges by seeks. |
| 73 |
The lastoff keeps being valid. */ |
| 74 |
if(fr->lastframe > -1 && fr->num >= fr->lastframe) |
| 75 |
{ |
| 76 |
/* There can be more than one frame of padding at the end, so we ignore the whole frame if we are beyond lastframe. */ |
| 77 |
int64_t byteoff = (fr->num == fr->lastframe) ? INT123_samples_to_bytes(fr, fr->lastoff) : 0; |
| 78 |
if((int64_t)fr->buffer.fill > byteoff) |
| 79 |
{ |
| 80 |
fr->buffer.fill = byteoff; |
| 81 |
} |
| 82 |
if(VERBOSE3) |
| 83 |
fprintf(stderr, "\nNote: Cut frame %" PRIi64 " buffer on end of stream to %" |
| 84 |
PRIi64 " samples, fill now %zu bytes.\n" |
| 85 |
, fr->num, (fr->num == fr->lastframe ? fr->lastoff : 0), fr->buffer.fill); |
| 86 |
} |
| 87 |
|
| 88 |
/* The first interesting frame: Skip some leading samples. */ |
| 89 |
if(fr->firstoff && fr->num == fr->firstframe) |
| 90 |
{ |
| 91 |
int64_t byteoff = INT123_samples_to_bytes(fr, fr->firstoff); |
| 92 |
if((int64_t)fr->buffer.fill > byteoff) |
| 93 |
{ |
| 94 |
fr->buffer.fill -= byteoff; |
| 95 |
/* buffer.p != buffer.data only for own buffer */ |
| 96 |
debug6("cutting %" PRIi64 " samples/%" PRIi64 |
| 97 |
" bytes on begin, own_buffer=%i at %p=%p, buf[1]=%i" |
| 98 |
, fr->firstoff, byteoff, fr->own_buffer |
| 99 |
, (void*)fr->buffer.p, (void*)fr->buffer.data, ((short*)fr->buffer.p)[2]); |
| 100 |
if(fr->own_buffer) fr->buffer.p = fr->buffer.data + byteoff; |
| 101 |
else memmove(fr->buffer.data, fr->buffer.data + byteoff, fr->buffer.fill); |
| 102 |
debug3("done cutting, buffer at %p =? %p, buf[1]=%i", |
| 103 |
(void*)fr->buffer.p, (void*)fr->buffer.data, ((short*)fr->buffer.p)[2]); |
| 104 |
} |
| 105 |
else fr->buffer.fill = 0; |
| 106 |
|
| 107 |
if(VERBOSE3) |
| 108 |
fprintf(stderr, "\nNote: Cut frame %" PRIi64 |
| 109 |
" buffer on beginning of stream by %" PRIi64 " samples, fill now %zu bytes.\n" |
| 110 |
, fr->num, fr->firstoff, fr->buffer.fill); |
| 111 |
/* We can only reach this frame again by seeking. And on seeking, firstoff will be recomputed. |
| 112 |
So it is safe to null it here (and it makes the if() decision abort earlier). */ |
| 113 |
fr->firstoff = 0; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#define SAMPLE_ADJUST(mh,x) sample_adjust(mh,x) |
| 118 |
#define SAMPLE_UNADJUST(mh,x) sample_unadjust(mh,x) |
| 119 |
#define FRAME_BUFFERCHECK(mh) frame_buffercheck(mh) |
| 120 |
|
| 121 |
#else /* no gapless code included */ |
| 122 |
|
| 123 |
#define SAMPLE_ADJUST(mh,x) (x) |
| 124 |
#define SAMPLE_UNADJUST(mh,x) (x) |
| 125 |
#define FRAME_BUFFERCHECK(mh) |
| 126 |
|
| 127 |
#endif |