Skip to content

Commit 2bb3b47

Browse files
Laurence BankLaurence Bank
Laurence Bank
authored and
Laurence Bank
committed
Fixed buffer getting stuck after a seek, but there's more work to do
1 parent 92b80df commit 2bb3b47

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

pl_mpeg.h

+29-16
Original file line numberDiff line numberDiff line change
@@ -1570,27 +1570,40 @@ uint32_t plm_buffer_tell(plm_buffer_t *self) {
15701570
}
15711571

15721572
void plm_buffer_discard_read_bytes(plm_buffer_t *self) {
1573+
#ifndef NEW_WAY
15731574
size_t byte_pos = self->bit_index >> 3;
15741575
if (byte_pos == self->length) {
15751576
self->bit_index = 0;
15761577
self->length = 0;
1577-
}
1578-
else if (byte_pos > 0) {
1578+
} else if (byte_pos > 0) {
15791579
// This normally moves only the last 4 bytes; calling memmove is a waste of time
1580-
if (self->length - byte_pos < 8) {
1580+
if (self->length - byte_pos < 16) {
15811581
uint8_t *d = self->bytes;
15821582
uint8_t *s = &self->bytes[byte_pos];
15831583
uint8_t *pEnd = &self->bytes[self->length];
15841584
while (s < pEnd) {
15851585
*d++ = *s++;
15861586
}
1587-
self->bit_index -= byte_pos << 3;
1588-
self->length -= byte_pos;
1589-
} else { // no need to move anything
1590-
byte_pos |= 0;
1591-
// memmove(self->bytes, self->bytes + byte_pos, self->length - byte_pos);
1587+
} else {
1588+
memmove(self->bytes, self->bytes + byte_pos, self->length - byte_pos);
15921589
}
1590+
self->bit_index -= byte_pos << 3;
1591+
self->length -= byte_pos;
15931592
}
1593+
#else
1594+
size_t byte_pos = self->bit_index >> 3;
1595+
if (byte_pos >= self->length)
1596+
{
1597+
self->bit_index = 0;
1598+
self->length = 0;
1599+
}
1600+
else if (byte_pos > 0)
1601+
{
1602+
memmove(self->bytes, self->bytes + byte_pos, self->length - byte_pos);
1603+
self->bit_index -= byte_pos << 3;
1604+
self->length -= byte_pos;
1605+
}
1606+
#endif
15941607
}
15951608

15961609
void plm_buffer_load_file_callback(plm_buffer_t *self, void *user) {
@@ -2785,7 +2798,7 @@ struct plm_video_t {
27852798
int macroblock_intra;
27862799

27872800
int dc_predictor[3];
2788-
2801+
27892802
plm_buffer_t *buffer;
27902803
int destroy_buffer_when_done;
27912804

@@ -3285,7 +3298,7 @@ void plm_video_decode_macroblock(plm_video_t *self) {
32853298
self->macroblock_address++;
32863299
}
32873300

3288-
self->mb_row = self->macroblock_address / self->mb_width;
3301+
self->mb_row = self->macroblock_address / self->mb_width;
32893302
self->mb_col = self->macroblock_address % self->mb_width;
32903303

32913304
if (self->mb_col >= self->mb_width || self->mb_row >= self->mb_height) {
@@ -3531,13 +3544,13 @@ void plm_video_process_macroblock(
35313544
case 3: // 2x2 average
35323545
{
35333546
int dest_scan = dw - block_size;
3534-
int a0, b0, a1, b1;
3547+
int a0, b0;
35353548
for (int y = 0; y < block_size; y++) {
3536-
a0 = s[si]; a1 = s[si+dw];
3549+
a0 = s[si] + s[si+dw];
35373550
for (int x = 0; x < block_size; x++) {
3538-
b0 = s[si+1]; b1 = s[si+dw+1];
3539-
d[di] = (a0+b0+a1+b1+2)>>2;
3540-
a0 = b0; a1 = b1;
3551+
b0 = s[si+1] + s[si+dw+1];
3552+
d[di] = (a0+b0+2)>>2;
3553+
a0 = b0;
35413554
si++; di++;
35423555
} // for x
35433556
si += dest_scan;
@@ -3697,7 +3710,7 @@ void plm_video_decode_block(plm_video_t *self, int block) {
36973710
}
36983711

36993712
n += run;
3700-
if (n < 0 || n >= 64) {
3713+
if (n >= 64) {
37013714
return; // invalid
37023715
}
37033716

0 commit comments

Comments
 (0)