00001
00002
00003
00004
00005
00006
00007
00032 #include <stdio.h>
00033 #include "oggdec.h"
00034 #include "avformat.h"
00035 #include "vorbiscomment.h"
00036
00037 #define MAX_PAGE_SIZE 65307
00038 #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
00039
00040 static const struct ogg_codec * const ogg_codecs[] = {
00041 &ff_skeleton_codec,
00042 &ff_dirac_codec,
00043 &ff_speex_codec,
00044 &ff_vorbis_codec,
00045 &ff_theora_codec,
00046 &ff_flac_codec,
00047 &ff_celt_codec,
00048 &ff_old_dirac_codec,
00049 &ff_old_flac_codec,
00050 &ff_ogm_video_codec,
00051 &ff_ogm_audio_codec,
00052 &ff_ogm_text_codec,
00053 &ff_ogm_old_codec,
00054 NULL
00055 };
00056
00057
00058 static int ogg_save(AVFormatContext *s)
00059 {
00060 struct ogg *ogg = s->priv_data;
00061 struct ogg_state *ost =
00062 av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
00063 int i;
00064 ost->pos = avio_tell (s->pb);
00065 ost->curidx = ogg->curidx;
00066 ost->next = ogg->state;
00067 ost->nstreams = ogg->nstreams;
00068 memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
00069
00070 for (i = 0; i < ogg->nstreams; i++){
00071 struct ogg_stream *os = ogg->streams + i;
00072 os->buf = av_malloc (os->bufsize);
00073 memset (os->buf, 0, os->bufsize);
00074 memcpy (os->buf, ost->streams[i].buf, os->bufpos);
00075 }
00076
00077 ogg->state = ost;
00078
00079 return 0;
00080 }
00081
00082 static int ogg_restore(AVFormatContext *s, int discard)
00083 {
00084 struct ogg *ogg = s->priv_data;
00085 AVIOContext *bc = s->pb;
00086 struct ogg_state *ost = ogg->state;
00087 int i;
00088
00089 if (!ost)
00090 return 0;
00091
00092 ogg->state = ost->next;
00093
00094 if (!discard){
00095 struct ogg_stream *old_streams = ogg->streams;
00096
00097 for (i = 0; i < ogg->nstreams; i++)
00098 av_free (ogg->streams[i].buf);
00099
00100 avio_seek (bc, ost->pos, SEEK_SET);
00101 ogg->curidx = ost->curidx;
00102 ogg->nstreams = ost->nstreams;
00103 ogg->streams = av_realloc (ogg->streams,
00104 ogg->nstreams * sizeof (*ogg->streams));
00105
00106 if (ogg->streams) {
00107 memcpy(ogg->streams, ost->streams,
00108 ost->nstreams * sizeof(*ogg->streams));
00109 } else {
00110 av_free(old_streams);
00111 ogg->nstreams = 0;
00112 }
00113 }
00114
00115 av_free (ost);
00116
00117 return 0;
00118 }
00119
00120 static int ogg_reset(struct ogg *ogg)
00121 {
00122 int i;
00123
00124 for (i = 0; i < ogg->nstreams; i++){
00125 struct ogg_stream *os = ogg->streams + i;
00126 os->bufpos = 0;
00127 os->pstart = 0;
00128 os->psize = 0;
00129 os->granule = -1;
00130 os->lastpts = AV_NOPTS_VALUE;
00131 os->lastdts = AV_NOPTS_VALUE;
00132 os->sync_pos = -1;
00133 os->page_pos = 0;
00134 os->nsegs = 0;
00135 os->segp = 0;
00136 os->incomplete = 0;
00137 }
00138
00139 ogg->curidx = -1;
00140
00141 return 0;
00142 }
00143
00144 static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
00145 {
00146 int i;
00147
00148 for (i = 0; ogg_codecs[i]; i++)
00149 if (size >= ogg_codecs[i]->magicsize &&
00150 !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
00151 return ogg_codecs[i];
00152
00153 return NULL;
00154 }
00155
00156 static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
00157 {
00158
00159 struct ogg *ogg = s->priv_data;
00160 int idx = ogg->nstreams++;
00161 AVStream *st;
00162 struct ogg_stream *os;
00163
00164 ogg->streams = av_realloc (ogg->streams,
00165 ogg->nstreams * sizeof (*ogg->streams));
00166 memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
00167 os = ogg->streams + idx;
00168 os->serial = serial;
00169 os->bufsize = DECODER_BUFFER_SIZE;
00170 os->buf = av_malloc(os->bufsize);
00171 os->header = -1;
00172
00173 if (new_avstream) {
00174 st = av_new_stream(s, idx);
00175 if (!st)
00176 return AVERROR(ENOMEM);
00177
00178 av_set_pts_info(st, 64, 1, 1000000);
00179 }
00180
00181 return idx;
00182 }
00183
00184 static int ogg_new_buf(struct ogg *ogg, int idx)
00185 {
00186 struct ogg_stream *os = ogg->streams + idx;
00187 uint8_t *nb = av_malloc(os->bufsize);
00188 int size = os->bufpos - os->pstart;
00189 if(os->buf){
00190 memcpy(nb, os->buf + os->pstart, size);
00191 av_free(os->buf);
00192 }
00193 os->buf = nb;
00194 os->bufpos = size;
00195 os->pstart = 0;
00196
00197 return 0;
00198 }
00199
00200 static int ogg_read_page(AVFormatContext *s, int *str)
00201 {
00202 AVIOContext *bc = s->pb;
00203 struct ogg *ogg = s->priv_data;
00204 struct ogg_stream *os;
00205 int ret, i = 0;
00206 int flags, nsegs;
00207 uint64_t gp;
00208 uint32_t serial;
00209 int size, idx;
00210 uint8_t sync[4];
00211 int sp = 0;
00212
00213 ret = avio_read (bc, sync, 4);
00214 if (ret < 4)
00215 return ret < 0 ? ret : AVERROR_EOF;
00216
00217 do{
00218 int c;
00219
00220 if (sync[sp & 3] == 'O' &&
00221 sync[(sp + 1) & 3] == 'g' &&
00222 sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
00223 break;
00224
00225 c = avio_r8(bc);
00226 if (url_feof(bc))
00227 return AVERROR_EOF;
00228 sync[sp++ & 3] = c;
00229 }while (i++ < MAX_PAGE_SIZE);
00230
00231 if (i >= MAX_PAGE_SIZE){
00232 av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
00233 return AVERROR_INVALIDDATA;
00234 }
00235
00236 if (avio_r8(bc) != 0)
00237 return AVERROR_INVALIDDATA;
00238
00239 flags = avio_r8(bc);
00240 gp = avio_rl64 (bc);
00241 serial = avio_rl32 (bc);
00242 avio_skip(bc, 8);
00243 nsegs = avio_r8(bc);
00244
00245 idx = ogg_find_stream (ogg, serial);
00246 if (idx < 0){
00247 if (ogg->headers) {
00248 int n;
00249
00250 for (n = 0; n < ogg->nstreams; n++) {
00251 av_freep(&ogg->streams[n].buf);
00252 if (!ogg->state || ogg->state->streams[n].private != ogg->streams[n].private)
00253 av_freep(&ogg->streams[n].private);
00254 }
00255 ogg->curidx = -1;
00256 ogg->nstreams = 0;
00257 idx = ogg_new_stream(s, serial, 0);
00258 } else {
00259 idx = ogg_new_stream(s, serial, 1);
00260 }
00261 if (idx < 0)
00262 return idx;
00263 }
00264
00265 os = ogg->streams + idx;
00266 os->page_pos = avio_tell(bc) - 27;
00267
00268 if(os->psize > 0)
00269 ogg_new_buf(ogg, idx);
00270
00271 ret = avio_read (bc, os->segments, nsegs);
00272 if (ret < nsegs)
00273 return ret < 0 ? ret : AVERROR_EOF;
00274
00275 os->nsegs = nsegs;
00276 os->segp = 0;
00277
00278 size = 0;
00279 for (i = 0; i < nsegs; i++)
00280 size += os->segments[i];
00281
00282 if (flags & OGG_FLAG_CONT || os->incomplete){
00283 if (!os->psize){
00284 while (os->segp < os->nsegs){
00285 int seg = os->segments[os->segp++];
00286 os->pstart += seg;
00287 if (seg < 255)
00288 break;
00289 }
00290 os->sync_pos = os->page_pos;
00291 }
00292 }else{
00293 os->psize = 0;
00294 os->sync_pos = os->page_pos;
00295 }
00296
00297 if (os->bufsize - os->bufpos < size){
00298 uint8_t *nb = av_malloc (os->bufsize *= 2);
00299 memcpy (nb, os->buf, os->bufpos);
00300 av_free (os->buf);
00301 os->buf = nb;
00302 }
00303
00304 ret = avio_read (bc, os->buf + os->bufpos, size);
00305 if (ret < size)
00306 return ret < 0 ? ret : AVERROR_EOF;
00307
00308 os->bufpos += size;
00309 os->granule = gp;
00310 os->flags = flags;
00311
00312 if (str)
00313 *str = idx;
00314
00315 return 0;
00316 }
00317
00318 static int ogg_packet(AVFormatContext *s, int *str, int *dstart, int *dsize,
00319 int64_t *fpos)
00320 {
00321 struct ogg *ogg = s->priv_data;
00322 int idx, i, ret;
00323 struct ogg_stream *os;
00324 int complete = 0;
00325 int segp = 0, psize = 0;
00326
00327 av_dlog(s, "ogg_packet: curidx=%i\n", ogg->curidx);
00328
00329 do{
00330 idx = ogg->curidx;
00331
00332 while (idx < 0){
00333 ret = ogg_read_page (s, &idx);
00334 if (ret < 0)
00335 return ret;
00336 }
00337
00338 os = ogg->streams + idx;
00339
00340 av_dlog(s, "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
00341 idx, os->pstart, os->psize, os->segp, os->nsegs);
00342
00343 if (!os->codec){
00344 if (os->header < 0){
00345 os->codec = ogg_find_codec (os->buf, os->bufpos);
00346 if (!os->codec){
00347 av_log(s, AV_LOG_WARNING, "Codec not found\n");
00348 os->header = 0;
00349 return 0;
00350 }
00351 }else{
00352 return 0;
00353 }
00354 }
00355
00356 segp = os->segp;
00357 psize = os->psize;
00358
00359 while (os->segp < os->nsegs){
00360 int ss = os->segments[os->segp++];
00361 os->psize += ss;
00362 if (ss < 255){
00363 complete = 1;
00364 break;
00365 }
00366 }
00367
00368 if (!complete && os->segp == os->nsegs){
00369 ogg->curidx = -1;
00370 os->incomplete = 1;
00371 }
00372 }while (!complete);
00373
00374
00375 if (os->granule == -1)
00376 av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
00377
00378 ogg->curidx = idx;
00379 os->incomplete = 0;
00380
00381 if (os->header) {
00382 os->header = os->codec->header (s, idx);
00383 if (!os->header){
00384 os->segp = segp;
00385 os->psize = psize;
00386
00387
00388
00389
00390 ogg->headers = 1;
00391
00392
00393
00394 if (!s->data_offset)
00395 s->data_offset = os->sync_pos;
00396 for (i = 0; i < ogg->nstreams; i++) {
00397 struct ogg_stream *cur_os = ogg->streams + i;
00398
00399
00400
00401 if (cur_os->incomplete)
00402 s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
00403 }
00404 }else{
00405 os->pstart += os->psize;
00406 os->psize = 0;
00407 }
00408 } else {
00409 os->pflags = 0;
00410 os->pduration = 0;
00411 if (os->codec && os->codec->packet)
00412 os->codec->packet (s, idx);
00413 if (str)
00414 *str = idx;
00415 if (dstart)
00416 *dstart = os->pstart;
00417 if (dsize)
00418 *dsize = os->psize;
00419 if (fpos)
00420 *fpos = os->sync_pos;
00421 os->pstart += os->psize;
00422 os->psize = 0;
00423 if(os->pstart == os->bufpos)
00424 os->bufpos = os->pstart = 0;
00425 os->sync_pos = os->page_pos;
00426 }
00427
00428
00429
00430 os->page_end = 1;
00431 for (i = os->segp; i < os->nsegs; i++)
00432 if (os->segments[i] < 255) {
00433 os->page_end = 0;
00434 break;
00435 }
00436
00437 if (os->segp == os->nsegs)
00438 ogg->curidx = -1;
00439
00440 return 0;
00441 }
00442
00443 static int ogg_get_headers(AVFormatContext *s)
00444 {
00445 struct ogg *ogg = s->priv_data;
00446 int ret;
00447
00448 do{
00449 ret = ogg_packet (s, NULL, NULL, NULL, NULL);
00450 if (ret < 0)
00451 return ret;
00452 }while (!ogg->headers);
00453
00454 av_dlog(s, "found headers\n");
00455
00456 return 0;
00457 }
00458
00459 static int ogg_get_length(AVFormatContext *s)
00460 {
00461 struct ogg *ogg = s->priv_data;
00462 int i;
00463 int64_t size, end;
00464
00465 if(!s->pb->seekable)
00466 return 0;
00467
00468
00469 if (s->duration != AV_NOPTS_VALUE)
00470 return 0;
00471
00472 size = avio_size(s->pb);
00473 if(size < 0)
00474 return 0;
00475 end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
00476
00477 ogg_save (s);
00478 avio_seek (s->pb, end, SEEK_SET);
00479
00480 while (!ogg_read_page (s, &i)){
00481 if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
00482 ogg->streams[i].codec) {
00483 s->streams[i]->duration =
00484 ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
00485 if (s->streams[i]->start_time != AV_NOPTS_VALUE)
00486 s->streams[i]->duration -= s->streams[i]->start_time;
00487 }
00488 }
00489
00490 ogg_restore (s, 0);
00491
00492 ogg_save (s);
00493 avio_seek (s->pb, 0, SEEK_SET);
00494 while (!ogg_read_page (s, &i)){
00495 if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
00496 ogg->streams[i].codec) {
00497 s->streams[i]->duration -=
00498 ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
00499 break;
00500 }
00501 }
00502 ogg_restore (s, 0);
00503
00504 return 0;
00505 }
00506
00507 static int ogg_read_header(AVFormatContext *s, AVFormatParameters *ap)
00508 {
00509 struct ogg *ogg = s->priv_data;
00510 int ret, i;
00511 ogg->curidx = -1;
00512
00513 ret = ogg_get_headers (s);
00514 if (ret < 0){
00515 return ret;
00516 }
00517
00518 for (i = 0; i < ogg->nstreams; i++)
00519 if (ogg->streams[i].header < 0)
00520 ogg->streams[i].codec = NULL;
00521
00522
00523 ogg_get_length (s);
00524
00525
00526 return 0;
00527 }
00528
00529 static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
00530 {
00531 struct ogg *ogg = s->priv_data;
00532 struct ogg_stream *os = ogg->streams + idx;
00533 int64_t pts = AV_NOPTS_VALUE;
00534
00535 if (dts)
00536 *dts = AV_NOPTS_VALUE;
00537
00538 if (os->lastpts != AV_NOPTS_VALUE) {
00539 pts = os->lastpts;
00540 os->lastpts = AV_NOPTS_VALUE;
00541 }
00542 if (os->lastdts != AV_NOPTS_VALUE) {
00543 if (dts)
00544 *dts = os->lastdts;
00545 os->lastdts = AV_NOPTS_VALUE;
00546 }
00547 if (os->page_end) {
00548 if (os->granule != -1LL) {
00549 if (os->codec && os->codec->granule_is_start)
00550 pts = ogg_gptopts(s, idx, os->granule, dts);
00551 else
00552 os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
00553 os->granule = -1LL;
00554 }
00555 }
00556 return pts;
00557 }
00558
00559 static int ogg_read_packet(AVFormatContext *s, AVPacket *pkt)
00560 {
00561 struct ogg *ogg;
00562 struct ogg_stream *os;
00563 int idx = -1;
00564 int pstart, psize;
00565 int64_t fpos, pts, dts;
00566
00567
00568 retry:
00569 do{
00570 if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
00571 return AVERROR(EIO);
00572 }while (idx < 0 || !s->streams[idx]);
00573
00574 ogg = s->priv_data;
00575 os = ogg->streams + idx;
00576
00577
00578 pts = ogg_calc_pts(s, idx, &dts);
00579
00580 if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
00581 goto retry;
00582 os->keyframe_seek = 0;
00583
00584
00585 if (av_new_packet (pkt, psize) < 0)
00586 return AVERROR(EIO);
00587 pkt->stream_index = idx;
00588 memcpy (pkt->data, os->buf + pstart, psize);
00589
00590 pkt->pts = pts;
00591 pkt->dts = dts;
00592 pkt->flags = os->pflags;
00593 pkt->duration = os->pduration;
00594 pkt->pos = fpos;
00595
00596 return psize;
00597 }
00598
00599 static int ogg_read_close(AVFormatContext *s)
00600 {
00601 struct ogg *ogg = s->priv_data;
00602 int i;
00603
00604 for (i = 0; i < ogg->nstreams; i++){
00605 av_free (ogg->streams[i].buf);
00606 av_free (ogg->streams[i].private);
00607 }
00608 av_free (ogg->streams);
00609 return 0;
00610 }
00611
00612 static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
00613 int64_t *pos_arg, int64_t pos_limit)
00614 {
00615 struct ogg *ogg = s->priv_data;
00616 AVIOContext *bc = s->pb;
00617 int64_t pts = AV_NOPTS_VALUE;
00618 int i = -1;
00619 avio_seek(bc, *pos_arg, SEEK_SET);
00620 ogg_reset(ogg);
00621
00622 while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
00623 if (i == stream_index) {
00624 struct ogg_stream *os = ogg->streams + stream_index;
00625 pts = ogg_calc_pts(s, i, NULL);
00626 if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
00627 pts = AV_NOPTS_VALUE;
00628 }
00629 if (pts != AV_NOPTS_VALUE)
00630 break;
00631 }
00632 ogg_reset(ogg);
00633 return pts;
00634 }
00635
00636 static int ogg_read_seek(AVFormatContext *s, int stream_index,
00637 int64_t timestamp, int flags)
00638 {
00639 struct ogg *ogg = s->priv_data;
00640 struct ogg_stream *os = ogg->streams + stream_index;
00641 int ret;
00642
00643
00644
00645 if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
00646 && !(flags & AVSEEK_FLAG_ANY))
00647 os->keyframe_seek = 1;
00648
00649 ret = av_seek_frame_binary(s, stream_index, timestamp, flags);
00650 os = ogg->streams + stream_index;
00651 if (ret < 0)
00652 os->keyframe_seek = 0;
00653 return ret;
00654 }
00655
00656 static int ogg_probe(AVProbeData *p)
00657 {
00658 if (!memcmp("OggS", p->buf, 5) && p->buf[5] <= 0x7)
00659 return AVPROBE_SCORE_MAX;
00660 return 0;
00661 }
00662
00663 AVInputFormat ff_ogg_demuxer = {
00664 .name = "ogg",
00665 .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
00666 .priv_data_size = sizeof(struct ogg),
00667 .read_probe = ogg_probe,
00668 .read_header = ogg_read_header,
00669 .read_packet = ogg_read_packet,
00670 .read_close = ogg_read_close,
00671 .read_seek = ogg_read_seek,
00672 .read_timestamp = ogg_read_timestamp,
00673 .extensions = "ogg",
00674 .flags = AVFMT_GENERIC_INDEX,
00675 };