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