Commit 281f5330 authored by conrad's avatar conrad

Update read position when handling buffered packets

in oggz_read with OGGZ_AUTO
parent 44ca0662
...@@ -40,6 +40,8 @@ ...@@ -40,6 +40,8 @@
#include <oggz/oggz_constants.h> #include <oggz/oggz_constants.h>
#include <oggz/oggz_off_t.h> #include <oggz/oggz_off_t.h>
#include "oggz/oggz_packet.h"
#include "oggz_macros.h" #include "oggz_macros.h"
#include "oggz_vector.h" #include "oggz_vector.h"
#include "oggz_dlist.h" #include "oggz_dlist.h"
...@@ -53,7 +55,7 @@ typedef struct _OggzReader OggzReader; ...@@ -53,7 +55,7 @@ typedef struct _OggzReader OggzReader;
typedef struct _OggzWriter OggzWriter; typedef struct _OggzWriter OggzWriter;
typedef int (*OggzReadPacket) (OGGZ * oggz, ogg_packet * op, long serialno, typedef int (*OggzReadPacket) (OGGZ * oggz, oggz_packet * op, long serialno,
void * user_data); void * user_data);
typedef int (*OggzReadPage) (OGGZ * oggz, const ogg_page * og, long serialno, typedef int (*OggzReadPage) (OGGZ * oggz, const ogg_page * og, long serialno,
void * user_data); void * user_data);
......
...@@ -189,6 +189,10 @@ oggz_read_get_next_page (OGGZ * oggz, ogg_page * og) ...@@ -189,6 +189,10 @@ oggz_read_get_next_page (OGGZ * oggz, ogg_page * og)
int found = 0; int found = 0;
/* Increment oggz->offset by length of the last page processed */ /* Increment oggz->offset by length of the last page processed */
#ifdef DEBUG_VERBOSE
printf ("%s: incrementing oggz->offset (0x%llx) by 0x%lx\n", __func__,
oggz->offset, reader->current_page_bytes);
#endif
oggz->offset += reader->current_page_bytes; oggz->offset += reader->current_page_bytes;
do { do {
...@@ -198,8 +202,9 @@ oggz_read_get_next_page (OGGZ * oggz, ogg_page * og) ...@@ -198,8 +202,9 @@ oggz_read_get_next_page (OGGZ * oggz, ogg_page * og)
/* No page available */ /* No page available */
return -2; return -2;
} else if (more < 0) { } else if (more < 0) {
page_offset += (-more);
#ifdef DEBUG_VERBOSE #ifdef DEBUG_VERBOSE
printf ("get_next_page: skipped %ld bytes\n", -more); printf ("%s: skipping; incrementing oggz->offset by 0x%lx bytes\n", __func__, -more);
#endif #endif
oggz->offset += (-more); oggz->offset += (-more);
} else { } else {
...@@ -216,7 +221,7 @@ oggz_read_get_next_page (OGGZ * oggz, ogg_page * og) ...@@ -216,7 +221,7 @@ oggz_read_get_next_page (OGGZ * oggz, ogg_page * og)
} }
typedef struct { typedef struct {
ogg_packet packet; oggz_packet zp;
ogg_int64_t calced_granulepos; ogg_int64_t calced_granulepos;
oggz_stream_t * stream; oggz_stream_t * stream;
OggzReader * reader; OggzReader * reader;
...@@ -225,16 +230,22 @@ typedef struct { ...@@ -225,16 +230,22 @@ typedef struct {
} OggzBufferedPacket; } OggzBufferedPacket;
OggzBufferedPacket * OggzBufferedPacket *
oggz_read_new_pbuffer_entry(OGGZ *oggz, ogg_packet *packet, oggz_read_new_pbuffer_entry(OGGZ *oggz, oggz_packet * zp,
ogg_int64_t granulepos, long serialno, oggz_stream_t * stream, ogg_int64_t granulepos, long serialno, oggz_stream_t * stream,
OggzReader *reader) { OggzReader *reader)
{
OggzBufferedPacket *p;
ogg_packet * op = &zp->op;
OggzBufferedPacket *p = oggz_malloc(sizeof(OggzBufferedPacket)); if ((p = oggz_malloc(sizeof(OggzBufferedPacket))) == NULL)
if (p == NULL) return NULL; return NULL;
memcpy(&(p->zp), zp, sizeof(oggz_packet));
memcpy(&(p->packet), packet, sizeof(ogg_packet)); if ((p->zp.op.packet = oggz_malloc(op->bytes)) == NULL) {
p->packet.packet = oggz_malloc(packet->bytes); oggz_free (p);
memcpy(p->packet.packet, packet->packet, packet->bytes); return NULL;
}
memcpy(p->zp.op.packet, op->packet, op->bytes);
p->calced_granulepos = granulepos; p->calced_granulepos = granulepos;
p->stream = stream; p->stream = stream;
...@@ -246,11 +257,10 @@ oggz_read_new_pbuffer_entry(OGGZ *oggz, ogg_packet *packet, ...@@ -246,11 +257,10 @@ oggz_read_new_pbuffer_entry(OGGZ *oggz, ogg_packet *packet,
} }
void void
oggz_read_free_pbuffer_entry(OggzBufferedPacket *p) { oggz_read_free_pbuffer_entry(OggzBufferedPacket *p)
{
oggz_free(p->packet.packet); oggz_free(p->zp.op.packet);
oggz_free(p); oggz_free(p);
} }
OggzDListIterResponse OggzDListIterResponse
...@@ -268,14 +278,14 @@ oggz_read_update_gp(void *elem) { ...@@ -268,14 +278,14 @@ oggz_read_update_gp(void *elem) {
p->calced_granulepos = p->calced_granulepos =
oggz_auto_calculate_gp_backwards(content, p->stream->last_granulepos, oggz_auto_calculate_gp_backwards(content, p->stream->last_granulepos,
p->stream, &(p->packet), p->stream->last_packet); p->stream, &(p->zp.op),
p->stream->last_packet);
p->stream->last_granulepos = p->calced_granulepos; p->stream->last_granulepos = p->calced_granulepos;
p->stream->last_packet = &(p->packet); p->stream->last_packet = &(p->zp.op);
} }
return DLIST_ITER_CONTINUE; return DLIST_ITER_CONTINUE;
} }
OggzDListIterResponse OggzDListIterResponse
...@@ -298,11 +308,11 @@ oggz_read_deliver_packet(void *elem) { ...@@ -298,11 +308,11 @@ oggz_read_deliver_packet(void *elem) {
oggz_get_unit (p->oggz, p->serialno, p->calced_granulepos); oggz_get_unit (p->oggz, p->serialno, p->calced_granulepos);
if (p->stream->read_packet) { if (p->stream->read_packet) {
p->stream->read_packet(p->oggz, &(p->packet), p->serialno, p->stream->read_packet(p->oggz, &(p->zp), p->serialno,
p->stream->read_user_data); p->stream->read_user_data);
} else if (p->reader->read_packet) { } else if (p->reader->read_packet) {
p->reader->read_packet(p->oggz, &(p->packet), p->serialno, p->reader->read_packet(p->oggz, &(p->zp), p->serialno,
p->reader->read_user_data); p->reader->read_user_data);
} }
p->reader->current_granulepos = gp_stored; p->reader->current_granulepos = gp_stored;
...@@ -374,14 +384,16 @@ oggz_read_sync (OGGZ * oggz) ...@@ -374,14 +384,16 @@ oggz_read_sync (OGGZ * oggz)
* so we silently swallow the notification and reget the packet. */ * so we silently swallow the notification and reget the packet. */
result = ogg_stream_packetout(os, op); result = ogg_stream_packetout(os, op);
if (result == -1) { if (result == -1) {
/* If the result is *still* -1 then something strange is /* If the result is *still* -1 then something strange is happening. */
* happening.
*/
#ifdef DEBUG #ifdef DEBUG
printf ("Multiple holes in data!"); printf ("Multiple holes in data!");
#endif #endif
return OGGZ_ERR_HOLE_IN_DATA; return OGGZ_ERR_HOLE_IN_DATA;
} }
/* Reset the position of the next page. */
reader->current_packet_pages = 1;
reader->current_packet_begin_page_offset = oggz->offset;
} }
if(result > 0){ if(result > 0){
...@@ -427,16 +439,28 @@ oggz_read_sync (OGGZ * oggz) ...@@ -427,16 +439,28 @@ oggz_read_sync (OGGZ * oggz)
oggz_auto_read_comments (oggz, stream, serialno, op); oggz_auto_read_comments (oggz, stream, serialno, op);
} }
/* Fill in position information. */
pos->calc_granulepos = reader->current_granulepos;
pos->begin_page_offset = reader->current_packet_begin_page_offset;
pos->end_page_offset = oggz->offset;
pos->pages = reader->current_packet_pages;
/* Handle reverse buffering */
if (oggz->flags & OGGZ_AUTO) { if (oggz->flags & OGGZ_AUTO) {
/* While we are getting invalid granulepos values, store the /* While we are getting invalid granulepos values, store the
* incoming packets in a dlist */ * incoming packets in a dlist */
if (reader->current_granulepos == -1) { if (reader->current_granulepos == -1) {
OggzBufferedPacket *p = oggz_read_new_pbuffer_entry( OggzBufferedPacket *p;
oggz, &packet, reader->current_granulepos,
serialno, stream, reader);
p = oggz_read_new_pbuffer_entry (oggz, &packet,
reader->current_granulepos,
serialno, stream, reader);
oggz_dlist_append(oggz->packet_buffer, p); oggz_dlist_append(oggz->packet_buffer, p);
/* Prepare the position of the next page. */
reader->current_packet_pages = 1;
reader->current_packet_begin_page_offset = oggz->offset;
continue; continue;
} else if (!oggz_dlist_is_empty(oggz->packet_buffer)) { } else if (!oggz_dlist_is_empty(oggz->packet_buffer)) {
/* Move backward through the list assigning gp values based upon /* Move backward through the list assigning gp values based upon
...@@ -445,7 +469,7 @@ oggz_read_sync (OGGZ * oggz) ...@@ -445,7 +469,7 @@ oggz_read_sync (OGGZ * oggz)
* gp values. * gp values.
*/ */
ogg_int64_t gp_stored = stream->last_granulepos; ogg_int64_t gp_stored = stream->last_granulepos;
stream->last_packet = &packet; stream->last_packet = op;
oggz_dlist_reverse_iter(oggz->packet_buffer, oggz_read_update_gp); oggz_dlist_reverse_iter(oggz->packet_buffer, oggz_read_update_gp);
oggz_dlist_deliter(oggz->packet_buffer, oggz_read_deliver_packet); oggz_dlist_deliter(oggz->packet_buffer, oggz_read_deliver_packet);
...@@ -453,21 +477,26 @@ oggz_read_sync (OGGZ * oggz) ...@@ -453,21 +477,26 @@ oggz_read_sync (OGGZ * oggz)
stream->last_granulepos = gp_stored; stream->last_granulepos = gp_stored;
if (!oggz_dlist_is_empty(oggz->packet_buffer)) { if (!oggz_dlist_is_empty(oggz->packet_buffer)) {
OggzBufferedPacket *p = oggz_read_new_pbuffer_entry( OggzBufferedPacket *p;
oggz, &packet, reader->current_granulepos,
serialno, stream, reader); p = oggz_read_new_pbuffer_entry(oggz, &packet,
reader->current_granulepos,
serialno, stream, reader);
oggz_dlist_append(oggz->packet_buffer, p); oggz_dlist_append(oggz->packet_buffer, p);
/* Prepare the position of the next page. */
reader->current_packet_pages = 1;
reader->current_packet_begin_page_offset = oggz->offset;
continue; continue;
} }
} }
} }
/* Fill in position information. */ #ifdef DEBUG_VERBOSE
pos->calc_granulepos = reader->current_granulepos; printf ("%s: set begin_page to %llx, calling read_packet\n", __func__, pos->begin_page_offset);
pos->begin_page_offset = reader->current_packet_begin_page_offset; #endif
pos->end_page_offset = oggz->offset;
pos->pages = reader->current_packet_pages;
if (stream->read_packet) { if (stream->read_packet) {
cb_ret = cb_ret =
...@@ -477,6 +506,10 @@ oggz_read_sync (OGGZ * oggz) ...@@ -477,6 +506,10 @@ oggz_read_sync (OGGZ * oggz)
reader->read_packet (oggz, &packet, serialno, reader->read_user_data); reader->read_packet (oggz, &packet, serialno, reader->read_user_data);
} }
#ifdef DEBUG_VERBOSE
fprintf (stdout, "%s: Done packet, setting next begin_page to 0x%llx\n", __func__, oggz->offset);
#endif
/* Prepare the position of the next page. */ /* Prepare the position of the next page. */
reader->current_packet_pages = 1; reader->current_packet_pages = 1;
reader->current_packet_begin_page_offset = oggz->offset; reader->current_packet_begin_page_offset = oggz->offset;
...@@ -550,6 +583,9 @@ oggz_read_sync (OGGZ * oggz) ...@@ -550,6 +583,9 @@ oggz_read_sync (OGGZ * oggz)
if (reader->current_packet_pages != -1) if (reader->current_packet_pages != -1)
reader->current_packet_pages++; reader->current_packet_pages++;
} else { } else {
#ifdef DEBUG_VERBOSE
fprintf (stdout, "%s: New non-cont page, setting next begin_page to 0x%llx\n", __func__, oggz->offset);
#endif
/* Prepare the position of the next page */ /* Prepare the position of the next page */
reader->current_packet_pages = 1; reader->current_packet_pages = 1;
reader->current_packet_begin_page_offset = oggz->offset; reader->current_packet_begin_page_offset = oggz->offset;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment