00001
00002
00003
00004
00005
00006
00007
00033 #include <stdio.h>
00034 #include "oggdec.h"
00035 #include "avformat.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_speex_codec,
00042 &ff_vorbis_codec,
00043 &ff_theora_codec,
00044 &ff_flac_codec,
00045 &ff_old_flac_codec,
00046 &ff_ogm_video_codec,
00047 &ff_ogm_audio_codec,
00048 &ff_ogm_text_codec,
00049 &ff_ogm_old_codec,
00050 NULL
00051 };
00052
00053
00054 static int
00055 ogg_save (AVFormatContext * s)
00056 {
00057 struct ogg *ogg = s->priv_data;
00058 struct ogg_state *ost =
00059 av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
00060 int i;
00061 ost->pos = url_ftell (s->pb);
00062 ost->curidx = ogg->curidx;
00063 ost->next = ogg->state;
00064 ost->nstreams = ogg->nstreams;
00065 memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
00066
00067 for (i = 0; i < ogg->nstreams; i++){
00068 struct ogg_stream *os = ogg->streams + i;
00069 os->buf = av_malloc (os->bufsize);
00070 memset (os->buf, 0, os->bufsize);
00071 memcpy (os->buf, ost->streams[i].buf, os->bufpos);
00072 }
00073
00074 ogg->state = ost;
00075
00076 return 0;
00077 }
00078
00079 static int
00080 ogg_restore (AVFormatContext * s, int discard)
00081 {
00082 struct ogg *ogg = s->priv_data;
00083 ByteIOContext *bc = s->pb;
00084 struct ogg_state *ost = ogg->state;
00085 int i;
00086
00087 if (!ost)
00088 return 0;
00089
00090 ogg->state = ost->next;
00091
00092 if (!discard){
00093 struct ogg_stream *old_streams = ogg->streams;
00094
00095 for (i = 0; i < ogg->nstreams; i++)
00096 av_free (ogg->streams[i].buf);
00097
00098 url_fseek (bc, ost->pos, SEEK_SET);
00099 ogg->curidx = ost->curidx;
00100 ogg->nstreams = ost->nstreams;
00101 ogg->streams = av_realloc (ogg->streams,
00102 ogg->nstreams * sizeof (*ogg->streams));
00103
00104 if (ogg->streams) {
00105 memcpy(ogg->streams, ost->streams,
00106 ost->nstreams * sizeof(*ogg->streams));
00107 } else {
00108 av_free(old_streams);
00109 ogg->nstreams = 0;
00110 }
00111 }
00112
00113 av_free (ost);
00114
00115 return 0;
00116 }
00117
00118 static int
00119 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->lastgp = -1;
00130 os->nsegs = 0;
00131 os->segp = 0;
00132 }
00133
00134 ogg->curidx = -1;
00135
00136 return 0;
00137 }
00138
00139 static const struct ogg_codec *
00140 ogg_find_codec (uint8_t * buf, int size)
00141 {
00142 int i;
00143
00144 for (i = 0; ogg_codecs[i]; i++)
00145 if (size >= ogg_codecs[i]->magicsize &&
00146 !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
00147 return ogg_codecs[i];
00148
00149 return NULL;
00150 }
00151
00152 static int
00153 ogg_find_stream (struct ogg * ogg, int serial)
00154 {
00155 int i;
00156
00157 for (i = 0; i < ogg->nstreams; i++)
00158 if (ogg->streams[i].serial == serial)
00159 return i;
00160
00161 return -1;
00162 }
00163
00164 static int
00165 ogg_new_stream (AVFormatContext * s, uint32_t serial)
00166 {
00167
00168 struct ogg *ogg = s->priv_data;
00169 int idx = ogg->nstreams++;
00170 AVStream *st;
00171 struct ogg_stream *os;
00172
00173 ogg->streams = av_realloc (ogg->streams,
00174 ogg->nstreams * sizeof (*ogg->streams));
00175 memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
00176 os = ogg->streams + idx;
00177 os->serial = serial;
00178 os->bufsize = DECODER_BUFFER_SIZE;
00179 os->buf = av_malloc(os->bufsize);
00180 os->header = -1;
00181
00182 st = av_new_stream (s, idx);
00183 if (!st)
00184 return AVERROR(ENOMEM);
00185
00186 av_set_pts_info(st, 64, 1, 1000000);
00187
00188 return idx;
00189 }
00190
00191 static int
00192 ogg_new_buf(struct ogg *ogg, int idx)
00193 {
00194 struct ogg_stream *os = ogg->streams + idx;
00195 uint8_t *nb = av_malloc(os->bufsize);
00196 int size = os->bufpos - os->pstart;
00197 if(os->buf){
00198 memcpy(nb, os->buf + os->pstart, size);
00199 av_free(os->buf);
00200 }
00201 os->buf = nb;
00202 os->bufpos = size;
00203 os->pstart = 0;
00204
00205 return 0;
00206 }
00207
00208 static int
00209 ogg_read_page (AVFormatContext * s, int *str)
00210 {
00211 ByteIOContext *bc = s->pb;
00212 struct ogg *ogg = s->priv_data;
00213 struct ogg_stream *os;
00214 int i = 0;
00215 int flags, nsegs;
00216 uint64_t gp;
00217 uint32_t serial;
00218 uint32_t seq;
00219 uint32_t crc;
00220 int size, idx;
00221 uint8_t sync[4];
00222 int sp = 0;
00223
00224 if (get_buffer (bc, sync, 4) < 4)
00225 return -1;
00226
00227 do{
00228 int c;
00229
00230 if (sync[sp & 3] == 'O' &&
00231 sync[(sp + 1) & 3] == 'g' &&
00232 sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
00233 break;
00234
00235 c = url_fgetc (bc);
00236 if (c < 0)
00237 return -1;
00238 sync[sp++ & 3] = c;
00239 }while (i++ < MAX_PAGE_SIZE);
00240
00241 if (i >= MAX_PAGE_SIZE){
00242 av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
00243 return -1;
00244 }
00245
00246 if (url_fgetc (bc) != 0)
00247 return -1;
00248
00249 flags = url_fgetc (bc);
00250 gp = get_le64 (bc);
00251 serial = get_le32 (bc);
00252 seq = get_le32 (bc);
00253 crc = get_le32 (bc);
00254 nsegs = url_fgetc (bc);
00255
00256 idx = ogg_find_stream (ogg, serial);
00257 if (idx < 0){
00258 idx = ogg_new_stream (s, serial);
00259 if (idx < 0)
00260 return -1;
00261 }
00262
00263 os = ogg->streams + idx;
00264
00265 if(os->psize > 0)
00266 ogg_new_buf(ogg, idx);
00267
00268 if (get_buffer (bc, os->segments, nsegs) < nsegs)
00269 return -1;
00270
00271 os->nsegs = nsegs;
00272 os->segp = 0;
00273
00274 size = 0;
00275 for (i = 0; i < nsegs; i++)
00276 size += os->segments[i];
00277
00278 if (flags & OGG_FLAG_CONT){
00279 if (!os->psize){
00280 while (os->segp < os->nsegs){
00281 int seg = os->segments[os->segp++];
00282 os->pstart += seg;
00283 if (seg < 255)
00284 break;
00285 }
00286 }
00287 }else{
00288 os->psize = 0;
00289 }
00290
00291 if (os->bufsize - os->bufpos < size){
00292 uint8_t *nb = av_malloc (os->bufsize *= 2);
00293 memcpy (nb, os->buf, os->bufpos);
00294 av_free (os->buf);
00295 os->buf = nb;
00296 }
00297
00298 if (get_buffer (bc, os->buf + os->bufpos, size) < size)
00299 return -1;
00300
00301 os->lastgp = os->granule;
00302 os->bufpos += size;
00303 os->granule = gp;
00304 os->flags = flags;
00305
00306 if (str)
00307 *str = idx;
00308
00309 return 0;
00310 }
00311
00312 static int
00313 ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
00314 {
00315 struct ogg *ogg = s->priv_data;
00316 int idx;
00317 struct ogg_stream *os;
00318 int complete = 0;
00319 int segp = 0, psize = 0;
00320
00321 #if 0
00322 av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
00323 #endif
00324
00325 do{
00326 idx = ogg->curidx;
00327
00328 while (idx < 0){
00329 if (ogg_read_page (s, &idx) < 0)
00330 return -1;
00331 }
00332
00333 os = ogg->streams + idx;
00334
00335 #if 0
00336 av_log (s, AV_LOG_DEBUG,
00337 "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
00338 idx, os->pstart, os->psize, os->segp, os->nsegs);
00339 #endif
00340
00341 if (!os->codec){
00342 if (os->header < 0){
00343 os->codec = ogg_find_codec (os->buf, os->bufpos);
00344 if (!os->codec){
00345 os->header = 0;
00346 return 0;
00347 }
00348 }else{
00349 return 0;
00350 }
00351 }
00352
00353 segp = os->segp;
00354 psize = os->psize;
00355
00356 while (os->segp < os->nsegs){
00357 int ss = os->segments[os->segp++];
00358 os->psize += ss;
00359 if (ss < 255){
00360 complete = 1;
00361 break;
00362 }
00363 }
00364
00365 if (!complete && os->segp == os->nsegs){
00366 ogg->curidx = -1;
00367 }
00368 }while (!complete);
00369
00370 #if 0
00371 av_log (s, AV_LOG_DEBUG,
00372 "ogg_packet: idx %i, frame size %i, start %i\n",
00373 idx, os->psize, os->pstart);
00374 #endif
00375
00376 ogg->curidx = idx;
00377
00378 if (os->header < 0){
00379 int hdr = os->codec->header (s, idx);
00380 if (!hdr){
00381 os->header = os->seq;
00382 os->segp = segp;
00383 os->psize = psize;
00384 ogg->headers = 1;
00385 }else{
00386 os->pstart += os->psize;
00387 os->psize = 0;
00388 }
00389 }
00390
00391 if (os->header > -1 && os->seq > os->header){
00392 os->pflags = 0;
00393 if (os->codec && os->codec->packet)
00394 os->codec->packet (s, idx);
00395 if (str)
00396 *str = idx;
00397 if (dstart)
00398 *dstart = os->pstart;
00399 if (dsize)
00400 *dsize = os->psize;
00401 os->pstart += os->psize;
00402 os->psize = 0;
00403 }
00404
00405 os->seq++;
00406 if (os->segp == os->nsegs)
00407 ogg->curidx = -1;
00408
00409 return 0;
00410 }
00411
00412 static int
00413 ogg_get_headers (AVFormatContext * s)
00414 {
00415 struct ogg *ogg = s->priv_data;
00416
00417 do{
00418 if (ogg_packet (s, NULL, NULL, NULL) < 0)
00419 return -1;
00420 }while (!ogg->headers);
00421
00422 #if 0
00423 av_log (s, AV_LOG_DEBUG, "found headers\n");
00424 #endif
00425
00426 return 0;
00427 }
00428
00429 static uint64_t
00430 ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
00431 {
00432 struct ogg *ogg = s->priv_data;
00433 struct ogg_stream *os = ogg->streams + i;
00434 uint64_t pts = AV_NOPTS_VALUE;
00435
00436 if(os->codec->gptopts){
00437 pts = os->codec->gptopts(s, i, gp);
00438 } else {
00439 pts = gp;
00440 }
00441
00442 return pts;
00443 }
00444
00445
00446 static int
00447 ogg_get_length (AVFormatContext * s)
00448 {
00449 struct ogg *ogg = s->priv_data;
00450 int idx = -1, i;
00451 int64_t size, end;
00452
00453 if(url_is_streamed(s->pb))
00454 return 0;
00455
00456
00457 if (s->duration != AV_NOPTS_VALUE)
00458 return 0;
00459
00460 size = url_fsize(s->pb);
00461 if(size < 0)
00462 return 0;
00463 end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
00464
00465 ogg_save (s);
00466 url_fseek (s->pb, end, SEEK_SET);
00467
00468 while (!ogg_read_page (s, &i)){
00469 if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
00470 ogg->streams[i].codec)
00471 idx = i;
00472 }
00473
00474 if (idx != -1){
00475 s->streams[idx]->duration =
00476 ogg_gptopts (s, idx, ogg->streams[idx].granule);
00477 }
00478
00479 ogg->size = size;
00480 ogg_restore (s, 0);
00481
00482 return 0;
00483 }
00484
00485
00486 static int
00487 ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
00488 {
00489 struct ogg *ogg = s->priv_data;
00490 int i;
00491 ogg->curidx = -1;
00492
00493 if (ogg_get_headers (s) < 0){
00494 return -1;
00495 }
00496
00497 for (i = 0; i < ogg->nstreams; i++)
00498 if (ogg->streams[i].header < 0)
00499 ogg->streams[i].codec = NULL;
00500
00501
00502 ogg_get_length (s);
00503
00504
00505 return 0;
00506 }
00507
00508
00509 static int
00510 ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
00511 {
00512 struct ogg *ogg;
00513 struct ogg_stream *os;
00514 int idx = -1;
00515 int pstart, psize;
00516
00517
00518 do{
00519 if (ogg_packet (s, &idx, &pstart, &psize) < 0)
00520 return AVERROR(EIO);
00521 }while (idx < 0 || !s->streams[idx]);
00522
00523 ogg = s->priv_data;
00524 os = ogg->streams + idx;
00525
00526
00527 if (av_new_packet (pkt, psize) < 0)
00528 return AVERROR(EIO);
00529 pkt->stream_index = idx;
00530 memcpy (pkt->data, os->buf + pstart, psize);
00531 if (os->lastgp != -1LL){
00532 pkt->pts = ogg_gptopts (s, idx, os->lastgp);
00533 os->lastgp = -1;
00534 }
00535
00536 pkt->flags = os->pflags;
00537
00538 return psize;
00539 }
00540
00541
00542 static int
00543 ogg_read_close (AVFormatContext * s)
00544 {
00545 struct ogg *ogg = s->priv_data;
00546 int i;
00547
00548 for (i = 0; i < ogg->nstreams; i++){
00549 av_free (ogg->streams[i].buf);
00550 av_free (ogg->streams[i].private);
00551 }
00552 av_free (ogg->streams);
00553 return 0;
00554 }
00555
00556
00557 static int64_t
00558 ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
00559 int64_t pos_limit)
00560 {
00561 struct ogg *ogg = s->priv_data;
00562 ByteIOContext *bc = s->pb;
00563 int64_t pts = AV_NOPTS_VALUE;
00564 int i;
00565 url_fseek(bc, *pos_arg, SEEK_SET);
00566 while (url_ftell(bc) < pos_limit && !ogg_read_page (s, &i)) {
00567 if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
00568 ogg->streams[i].codec && i == stream_index) {
00569 pts = ogg_gptopts(s, i, ogg->streams[i].granule);
00570
00571
00572 *pos_arg = url_ftell(bc);
00573 break;
00574 }
00575 }
00576 ogg_reset(ogg);
00577 return pts;
00578 }
00579
00580 static int ogg_probe(AVProbeData *p)
00581 {
00582 if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
00583 p->buf[2] == 'g' && p->buf[3] == 'S' &&
00584 p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
00585 return AVPROBE_SCORE_MAX;
00586 else
00587 return 0;
00588 }
00589
00590 AVInputFormat ogg_demuxer = {
00591 "ogg",
00592 NULL_IF_CONFIG_SMALL("Ogg"),
00593 sizeof (struct ogg),
00594 ogg_probe,
00595 ogg_read_header,
00596 ogg_read_packet,
00597 ogg_read_close,
00598 NULL,
00599 ogg_read_timestamp,
00600 .extensions = "ogg",
00601 };