61
61
struct ogg *ogg = s->priv_data;
62
62
struct ogg_state *ost =
63
av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
63
av_malloc(sizeof(*ost) + (ogg->nstreams - 1) * sizeof(*ogg->streams));
65
ost->pos = avio_tell (s->pb);
66
ost->curidx = ogg->curidx;
67
ost->next = ogg->state;
65
ost->pos = avio_tell(s->pb);
66
ost->curidx = ogg->curidx;
67
ost->next = ogg->state;
68
68
ost->nstreams = ogg->nstreams;
69
69
memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
71
for (i = 0; i < ogg->nstreams; i++){
71
for (i = 0; i < ogg->nstreams; i++) {
72
72
struct ogg_stream *os = ogg->streams + i;
73
os->buf = av_mallocz (os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
74
memcpy (os->buf, ost->streams[i].buf, os->bufpos);
73
os->buf = av_mallocz(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
74
memcpy(os->buf, ost->streams[i].buf, os->bufpos);
92
92
ogg->state = ost->next;
95
95
struct ogg_stream *old_streams = ogg->streams;
97
97
for (i = 0; i < ogg->nstreams; i++)
98
av_free (ogg->streams[i].buf);
98
av_free(ogg->streams[i].buf);
100
avio_seek (bc, ost->pos, SEEK_SET);
101
ogg->curidx = ost->curidx;
100
avio_seek(bc, ost->pos, SEEK_SET);
101
ogg->curidx = ost->curidx;
102
102
ogg->nstreams = ost->nstreams;
103
ogg->streams = av_realloc (ogg->streams,
104
ogg->nstreams * sizeof (*ogg->streams));
103
ogg->streams = av_realloc(ogg->streams,
104
ogg->nstreams * sizeof(*ogg->streams));
106
106
if (ogg->streams) {
107
107
memcpy(ogg->streams, ost->streams,
124
for (i = 0; i < ogg->nstreams; i++){
124
for (i = 0; i < ogg->nstreams; i++) {
125
125
struct ogg_stream *os = ogg->streams + i;
130
os->lastpts = AV_NOPTS_VALUE;
131
os->lastdts = AV_NOPTS_VALUE;
130
os->lastpts = AV_NOPTS_VALUE;
131
os->lastdts = AV_NOPTS_VALUE;
136
136
os->incomplete = 0;
148
148
for (i = 0; ogg_codecs[i]; i++)
149
149
if (size >= ogg_codecs[i]->magicsize &&
150
!memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
150
!memcmp(buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
151
151
return ogg_codecs[i];
156
156
static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
159
158
struct ogg *ogg = s->priv_data;
160
int idx = ogg->nstreams++;
159
int idx = ogg->nstreams++;
162
161
struct ogg_stream *os;
164
os = av_realloc (ogg->streams, ogg->nstreams * sizeof (*ogg->streams));
163
os = av_realloc(ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
167
166
return AVERROR(ENOMEM);
169
168
ogg->streams = os;
171
memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
172
os = ogg->streams + idx;
174
os->bufsize = DECODER_BUFFER_SIZE;
175
os->buf = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
170
memset(ogg->streams + idx, 0, sizeof(*ogg->streams));
172
os = ogg->streams + idx;
174
os->bufsize = DECODER_BUFFER_SIZE;
175
os->buf = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
177
os->start_granule = OGG_NOGRANULE_VALUE;
178
179
if (new_avstream) {
179
180
st = avformat_new_stream(s, NULL);
192
193
struct ogg_stream *os = ogg->streams + idx;
193
194
uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
194
195
int size = os->bufpos - os->pstart;
196
198
memcpy(nb, os->buf + os->pstart, size);
197
199
av_free(os->buf);
200
203
os->bufpos = size;
232
236
if (bc->eof_reached)
233
237
return AVERROR_EOF;
234
239
sync[sp++ & 3] = c;
235
}while (i++ < MAX_PAGE_SIZE);
240
} while (i++ < MAX_PAGE_SIZE);
237
if (i >= MAX_PAGE_SIZE){
238
av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
242
if (i >= MAX_PAGE_SIZE) {
243
av_log(s, AV_LOG_INFO, "cannot find sync word\n");
239
244
return AVERROR_INVALIDDATA;
242
247
if (avio_r8(bc) != 0) /* version */
243
248
return AVERROR_INVALIDDATA;
247
serial = avio_rl32 (bc);
252
serial = avio_rl32(bc);
248
253
avio_skip(bc, 8); /* seq, crc */
251
idx = ogg_find_stream (ogg, serial);
256
idx = ogg_find_stream(ogg, serial);
253
258
if (ogg->headers) {
256
261
for (n = 0; n < ogg->nstreams; n++) {
257
262
av_freep(&ogg->streams[n].buf);
258
if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)
264
ogg->state->streams[n].private != ogg->streams[n].private)
259
265
av_freep(&ogg->streams[n].private);
261
268
ogg->curidx = -1;
262
269
ogg->nstreams = 0;
263
271
idx = ogg_new_stream(s, serial, 0);
265
273
idx = ogg_new_stream(s, serial, 1);
279
287
return ret < 0 ? ret : AVERROR_EOF;
281
289
os->nsegs = nsegs;
285
293
for (i = 0; i < nsegs; i++)
286
294
size += os->segments[i];
288
if (flags & OGG_FLAG_CONT || os->incomplete){
290
while (os->segp < os->nsegs){
296
if (flags & OGG_FLAG_CONT || os->incomplete) {
298
while (os->segp < os->nsegs) {
291
299
int seg = os->segments[os->segp++];
292
300
os->pstart += seg;
296
304
os->sync_pos = os->page_pos;
300
308
os->sync_pos = os->page_pos;
303
if (os->bufsize - os->bufpos < size){
304
uint8_t *nb = av_malloc ((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
311
if (os->bufsize - os->bufpos < size) {
312
uint8_t *nb = av_malloc((os->bufsize *= 2) + FF_INPUT_BUFFER_PADDING_SIZE);
306
314
return AVERROR(ENOMEM);
307
memcpy (nb, os->buf, os->bufpos);
315
memcpy(nb, os->buf, os->bufpos);
349
357
av_dlog(s, "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
350
358
idx, os->pstart, os->psize, os->segp, os->nsegs);
354
os->codec = ogg_find_codec (os->buf, os->bufpos);
361
if (os->header < 0) {
362
os->codec = ogg_find_codec(os->buf, os->bufpos);
356
364
av_log(s, AV_LOG_WARNING, "Codec not found\n");
366
374
psize = os->psize;
368
while (os->segp < os->nsegs){
376
while (os->segp < os->nsegs) {
369
377
int ss = os->segments[os->segp++];
377
if (!complete && os->segp == os->nsegs){
385
if (!complete && os->segp == os->nsegs) {
379
387
os->incomplete = 1;
383
391
av_dlog(s, "ogg_packet: idx %i, frame size %i, start %i\n",
384
392
idx, os->psize, os->pstart);
386
394
if (os->granule == -1)
387
av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
395
av_log(s, AV_LOG_WARNING,
396
"Page at %"PRId64" is missing granule\n",
390
400
os->incomplete = 0;
392
402
if (os->header) {
393
os->header = os->codec->header (s, idx);
403
os->header = os->codec->header(s, idx);
396
406
os->psize = psize;
398
408
// We have reached the first non-header packet in this stream.
412
423
if (cur_os->incomplete)
413
424
s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
416
428
os->pstart += os->psize;
421
433
os->pduration = 0;
422
434
if (os->codec && os->codec->packet)
423
os->codec->packet (s, idx);
435
os->codec->packet(s, idx);
452
464
static int ogg_get_headers(AVFormatContext *s)
454
466
struct ogg *ogg = s->priv_data;
458
470
ret = ogg_packet(s, NULL, NULL, NULL, NULL);
461
}while (!ogg->headers);
473
} while (!ogg->headers);
475
for (i = 0; i < ogg->nstreams; i++) {
476
struct ogg_stream *os = ogg->streams + i;
478
if (os->codec && os->codec->nb_header &&
479
os->nb_header < os->codec->nb_header) {
480
av_log(s, AV_LOG_ERROR,
481
"Headers mismatch for stream %d\n", i);
482
return AVERROR_INVALIDDATA;
484
if (os->start_granule != OGG_NOGRANULE_VALUE)
485
os->lastpts = s->streams[i]->start_time =
486
ogg_gptopts(s, i, os->start_granule, NULL);
463
488
av_dlog(s, "found headers\n");
481
506
size = avio_size(s->pb);
484
end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
487
avio_seek (s->pb, end, SEEK_SET);
489
while (!ogg_read_page (s, &i)){
509
end = size > MAX_PAGE_SIZE ? size - MAX_PAGE_SIZE : 0;
512
avio_seek(s->pb, end, SEEK_SET);
514
while (!ogg_read_page(s, &i)) {
490
515
if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
491
516
ogg->streams[i].codec) {
492
517
s->streams[i]->duration =
493
ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
518
ogg_gptopts(s, i, ogg->streams[i].granule, NULL);
494
519
if (s->streams[i]->start_time != AV_NOPTS_VALUE)
495
520
s->streams[i]->duration -= s->streams[i]->start_time;
544
569
static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
546
struct ogg *ogg = s->priv_data;
571
struct ogg *ogg = s->priv_data;
547
572
struct ogg_stream *os = ogg->streams + idx;
548
int64_t pts = AV_NOPTS_VALUE;
573
int64_t pts = AV_NOPTS_VALUE;
551
576
*dts = AV_NOPTS_VALUE;
553
578
if (os->lastpts != AV_NOPTS_VALUE) {
555
580
os->lastpts = AV_NOPTS_VALUE;
557
582
if (os->lastdts != AV_NOPTS_VALUE) {
582
607
//Get an ogg packet
585
610
ret = ogg_packet(s, &idx, &pstart, &psize, &fpos);
588
}while (idx < 0 || !s->streams[idx]);
613
} while (idx < 0 || !s->streams[idx]);
590
615
ogg = s->priv_data;
591
os = ogg->streams + idx;
616
os = ogg->streams + idx;
593
618
// pflags might not be set until after this
594
619
pts = ogg_calc_pts(s, idx, &dts);
604
629
pkt->stream_index = idx;
605
memcpy (pkt->data, os->buf + pstart, psize);
630
memcpy(pkt->data, os->buf + pstart, psize);
609
pkt->flags = os->pflags;
634
pkt->flags = os->pflags;
610
635
pkt->duration = os->pduration;
619
644
struct ogg *ogg = s->priv_data;
620
645
AVIOContext *bc = s->pb;
621
int64_t pts = AV_NOPTS_VALUE;
646
int64_t pts = AV_NOPTS_VALUE;
623
648
avio_seek(bc, *pos_arg, SEEK_SET);
626
while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
651
while (avio_tell(bc) < pos_limit &&
652
!ogg_packet(s, &i, NULL, NULL, pos_arg)) {
627
653
if (i == stream_index) {
628
654
struct ogg_stream *os = ogg->streams + stream_index;
629
655
pts = ogg_calc_pts(s, i, NULL);
640
666
static int ogg_read_seek(AVFormatContext *s, int stream_index,
641
667
int64_t timestamp, int flags)
643
struct ogg *ogg = s->priv_data;
669
struct ogg *ogg = s->priv_data;
644
670
struct ogg_stream *os = ogg->streams + stream_index;
651
677
os->keyframe_seek = 1;
653
679
ret = ff_seek_frame_binary(s, stream_index, timestamp, flags);
654
os = ogg->streams + stream_index;
680
os = ogg->streams + stream_index;
656
682
os->keyframe_seek = 0;