2002-09-01 21:07:56 +03:00
|
|
|
/*
|
|
|
|
* Ogg bitstream support
|
|
|
|
* Mark Hills <mark@pogo.org.uk>
|
|
|
|
*
|
|
|
|
* Uses libogg, but requires libvorbisenc to construct correct headers
|
|
|
|
* when containing Vorbis stream -- currently the only format supported
|
2006-09-10 17:02:42 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-09-10 17:02:42 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-09-10 17:02:42 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-09-10 17:02:42 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-09-10 17:02:42 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-09-01 21:07:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ogg/ogg.h>
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
|
2004-04-04 17:39:20 +03:00
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
2002-11-22 09:27:13 +02:00
|
|
|
#define DECODER_BUFFER_SIZE 4096
|
|
|
|
|
2002-09-01 21:07:56 +03:00
|
|
|
|
|
|
|
typedef struct OggContext {
|
2002-11-22 09:27:13 +02:00
|
|
|
/* output */
|
2002-09-01 21:07:56 +03:00
|
|
|
ogg_stream_state os ;
|
2002-11-22 09:27:13 +02:00
|
|
|
int header_handled ;
|
2004-04-04 17:39:20 +03:00
|
|
|
ogg_packet op;
|
2002-11-22 09:27:13 +02:00
|
|
|
|
|
|
|
/* input */
|
|
|
|
ogg_sync_state oy ;
|
2002-09-01 21:07:56 +03:00
|
|
|
} OggContext ;
|
|
|
|
|
|
|
|
|
2005-09-23 03:25:41 +03:00
|
|
|
#ifdef CONFIG_MUXERS
|
2005-12-17 20:14:38 +02:00
|
|
|
static int ogg_write_header(AVFormatContext *avfcontext)
|
2003-01-25 15:05:39 +02:00
|
|
|
{
|
|
|
|
OggContext *context = avfcontext->priv_data;
|
2005-12-17 20:14:38 +02:00
|
|
|
ogg_packet *op= &context->op;
|
2005-09-22 02:09:16 +03:00
|
|
|
int n;
|
2004-04-04 17:39:20 +03:00
|
|
|
|
2004-04-04 04:10:50 +03:00
|
|
|
ogg_stream_init(&context->os, 31415);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2002-09-01 21:07:56 +03:00
|
|
|
for(n = 0 ; n < avfcontext->nb_streams ; n++) {
|
2005-07-18 01:24:36 +03:00
|
|
|
AVCodecContext *codec = avfcontext->streams[n]->codec;
|
2005-08-15 23:42:56 +03:00
|
|
|
uint8_t *headers = codec->extradata;
|
|
|
|
int headers_len = codec->extradata_size;
|
|
|
|
uint8_t *header_start[3];
|
|
|
|
int header_len[3];
|
2005-09-22 02:09:16 +03:00
|
|
|
int i, j;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2004-05-21 23:43:21 +03:00
|
|
|
av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
|
|
|
|
|
2005-08-15 23:42:56 +03:00
|
|
|
for(j=1,i=0;i<2;++i, ++j) {
|
|
|
|
header_len[i]=0;
|
|
|
|
while(j<headers_len && headers[j]==0xff) {
|
|
|
|
header_len[i]+=0xff;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
header_len[i]+=headers[j];
|
|
|
|
}
|
|
|
|
header_len[2]=headers_len-header_len[0]-header_len[1]-j;
|
|
|
|
headers+=j;
|
|
|
|
header_start[0] = headers;
|
|
|
|
header_start[1] = header_start[0] + header_len[0];
|
|
|
|
header_start[2] = header_start[1] + header_len[1];
|
|
|
|
|
|
|
|
for(i=0; i < 3; ++i){
|
|
|
|
op->bytes = header_len[i];
|
2004-04-04 18:19:20 +03:00
|
|
|
|
2005-08-15 23:42:56 +03:00
|
|
|
op->packet= header_start[i];
|
2004-04-04 18:19:20 +03:00
|
|
|
op->b_o_s= op->packetno==0;
|
|
|
|
|
|
|
|
ogg_stream_packetin(&context->os, op);
|
|
|
|
|
|
|
|
op->packetno++; //FIXME multiple streams
|
|
|
|
}
|
2002-09-01 21:07:56 +03:00
|
|
|
|
2005-12-22 03:10:11 +02:00
|
|
|
context->header_handled = 0 ;
|
2002-09-01 21:07:56 +03:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2002-09-01 21:07:56 +03:00
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2004-05-29 05:06:32 +03:00
|
|
|
static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
|
2002-09-01 21:07:56 +03:00
|
|
|
{
|
|
|
|
OggContext *context = avfcontext->priv_data ;
|
2005-07-18 01:24:36 +03:00
|
|
|
AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
|
2004-04-04 17:39:20 +03:00
|
|
|
ogg_packet *op= &context->op;
|
2002-09-01 21:07:56 +03:00
|
|
|
ogg_page og ;
|
2004-05-29 05:06:32 +03:00
|
|
|
int64_t pts;
|
2004-04-04 17:39:20 +03:00
|
|
|
|
2004-05-29 05:06:32 +03:00
|
|
|
pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
|
2004-04-04 17:39:20 +03:00
|
|
|
|
|
|
|
// av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
|
|
|
|
|
2002-09-01 21:07:56 +03:00
|
|
|
/* flush header packets so audio starts on a new page */
|
|
|
|
|
2002-11-22 09:27:13 +02:00
|
|
|
if(!context->header_handled) {
|
2005-12-22 03:10:11 +02:00
|
|
|
while(ogg_stream_flush(&context->os, &og)) {
|
|
|
|
put_buffer(&avfcontext->pb, og.header, og.header_len) ;
|
|
|
|
put_buffer(&avfcontext->pb, og.body, og.body_len) ;
|
|
|
|
put_flush_packet(&avfcontext->pb);
|
|
|
|
}
|
|
|
|
context->header_handled = 1 ;
|
2002-09-01 21:07:56 +03:00
|
|
|
}
|
|
|
|
|
2004-05-29 05:06:32 +03:00
|
|
|
op->packet = (uint8_t*) pkt->data;
|
|
|
|
op->bytes = pkt->size;
|
2004-04-04 17:39:20 +03:00
|
|
|
op->b_o_s = op->packetno == 0;
|
|
|
|
op->granulepos= pts;
|
|
|
|
|
|
|
|
/* correct the fields in the packet -- essential for streaming */
|
2005-12-17 20:14:38 +02:00
|
|
|
|
|
|
|
ogg_stream_packetin(&context->os, op);
|
|
|
|
|
2004-04-04 17:39:20 +03:00
|
|
|
while(ogg_stream_pageout(&context->os, &og)) {
|
|
|
|
put_buffer(&avfcontext->pb, og.header, og.header_len);
|
2005-12-22 03:10:11 +02:00
|
|
|
put_buffer(&avfcontext->pb, og.body, og.body_len);
|
|
|
|
put_flush_packet(&avfcontext->pb);
|
2005-12-17 20:14:38 +02:00
|
|
|
}
|
2004-04-04 17:39:20 +03:00
|
|
|
op->packetno++;
|
2002-09-01 21:07:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ogg_write_trailer(AVFormatContext *avfcontext) {
|
|
|
|
OggContext *context = avfcontext->priv_data ;
|
|
|
|
ogg_page og ;
|
|
|
|
|
|
|
|
while(ogg_stream_flush(&context->os, &og)) {
|
2005-12-22 03:10:11 +02:00
|
|
|
put_buffer(&avfcontext->pb, og.header, og.header_len) ;
|
|
|
|
put_buffer(&avfcontext->pb, og.body, og.body_len) ;
|
|
|
|
put_flush_packet(&avfcontext->pb);
|
2002-09-01 21:07:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ogg_stream_clear(&context->os) ;
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-11 00:14:37 +03:00
|
|
|
AVOutputFormat ogg_muxer = {
|
2002-09-01 21:07:56 +03:00
|
|
|
"ogg",
|
2007-02-14 01:20:35 +02:00
|
|
|
"Ogg format",
|
|
|
|
"application/ogg",
|
2002-09-01 21:07:56 +03:00
|
|
|
"ogg",
|
|
|
|
sizeof(OggContext),
|
|
|
|
CODEC_ID_VORBIS,
|
|
|
|
0,
|
|
|
|
ogg_write_header,
|
|
|
|
ogg_write_packet,
|
|
|
|
ogg_write_trailer,
|
2002-11-22 09:27:13 +02:00
|
|
|
} ;
|
2005-09-23 03:25:41 +03:00
|
|
|
#endif //CONFIG_MUXERS
|
2002-11-22 09:27:13 +02:00
|
|
|
|
2005-04-09 18:32:58 +03:00
|
|
|
#if 0
|
2002-11-22 09:27:13 +02:00
|
|
|
static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
|
|
|
|
OggContext *context = avfcontext->priv_data ;
|
|
|
|
ogg_page og ;
|
|
|
|
char *buf ;
|
|
|
|
|
|
|
|
while(ogg_stream_packetout(&context->os, op) != 1) {
|
|
|
|
|
2005-12-22 03:10:11 +02:00
|
|
|
/* while no pages are available, read in more data to the sync */
|
|
|
|
while(ogg_sync_pageout(&context->oy, &og) != 1) {
|
|
|
|
buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
|
|
|
|
if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
|
|
|
|
return 1 ;
|
|
|
|
ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* got a page. Feed it into the stream and get the packet */
|
|
|
|
if(ogg_stream_pagein(&context->os, &og) != 0)
|
|
|
|
return 1 ;
|
2002-11-22 09:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
|
|
|
|
{
|
2003-01-25 15:05:39 +02:00
|
|
|
OggContext *context = avfcontext->priv_data;
|
2005-12-17 20:14:38 +02:00
|
|
|
ogg_packet op ;
|
2002-11-22 09:27:13 +02:00
|
|
|
char *buf ;
|
|
|
|
ogg_page og ;
|
|
|
|
AVStream *ast ;
|
2004-04-04 20:55:59 +03:00
|
|
|
AVCodecContext *codec;
|
|
|
|
uint8_t *p;
|
|
|
|
int i;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2002-11-22 09:27:13 +02:00
|
|
|
ogg_sync_init(&context->oy) ;
|
|
|
|
buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
|
|
|
|
|
|
|
|
if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
|
2005-12-22 03:10:11 +02:00
|
|
|
return AVERROR_IO ;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
|
|
|
ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
|
2002-11-22 09:27:13 +02:00
|
|
|
ogg_sync_pageout(&context->oy, &og) ;
|
|
|
|
ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
|
|
|
|
ogg_stream_pagein(&context->os, &og) ;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2002-11-22 09:27:13 +02:00
|
|
|
/* currently only one vorbis stream supported */
|
|
|
|
|
|
|
|
ast = av_new_stream(avfcontext, 0) ;
|
|
|
|
if(!ast)
|
2005-12-22 03:10:11 +02:00
|
|
|
return AVERROR_NOMEM ;
|
2004-05-21 23:43:21 +03:00
|
|
|
av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
|
2002-11-22 09:27:13 +02:00
|
|
|
|
2004-04-04 20:55:59 +03:00
|
|
|
codec= &ast->codec;
|
|
|
|
codec->codec_type = CODEC_TYPE_AUDIO;
|
|
|
|
codec->codec_id = CODEC_ID_VORBIS;
|
|
|
|
for(i=0; i<3; i++){
|
|
|
|
if(next_packet(avfcontext, &op)){
|
|
|
|
return -1;
|
|
|
|
}
|
2005-01-08 16:21:33 +02:00
|
|
|
if(op.bytes >= (1<<16) || op.bytes < 0)
|
|
|
|
return -1;
|
2004-04-04 20:55:59 +03:00
|
|
|
codec->extradata_size+= 2 + op.bytes;
|
2004-11-13 03:27:35 +02:00
|
|
|
codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
2005-12-17 19:57:03 +02:00
|
|
|
memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
|
2004-04-04 20:55:59 +03:00
|
|
|
p= codec->extradata + codec->extradata_size - 2 - op.bytes;
|
|
|
|
*(p++)= op.bytes>>8;
|
|
|
|
*(p++)= op.bytes&0xFF;
|
|
|
|
memcpy(p, op.packet, op.bytes);
|
|
|
|
}
|
|
|
|
|
2002-11-22 09:27:13 +02:00
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
|
|
|
|
ogg_packet op ;
|
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
if(next_packet(avfcontext, &op))
|
2005-12-22 03:10:11 +02:00
|
|
|
return AVERROR_IO ;
|
2004-04-04 05:07:15 +03:00
|
|
|
if(av_new_packet(pkt, op.bytes) < 0)
|
2005-12-22 03:10:11 +02:00
|
|
|
return AVERROR_IO ;
|
2002-11-22 09:27:13 +02:00
|
|
|
pkt->stream_index = 0 ;
|
2004-04-04 05:07:15 +03:00
|
|
|
memcpy(pkt->data, op.packet, op.bytes);
|
2004-04-27 06:35:14 +03:00
|
|
|
if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
|
|
|
|
pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
|
2006-11-02 00:39:58 +02:00
|
|
|
// printf("%"PRId64" %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
|
2002-11-22 09:27:13 +02:00
|
|
|
|
2004-04-04 05:07:15 +03:00
|
|
|
return op.bytes;
|
2002-11-22 09:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ogg_read_close(AVFormatContext *avfcontext) {
|
|
|
|
OggContext *context = avfcontext->priv_data ;
|
|
|
|
|
|
|
|
ogg_stream_clear(&context->os) ;
|
|
|
|
ogg_sync_clear(&context->oy) ;
|
|
|
|
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-11 00:14:37 +03:00
|
|
|
static AVInputFormat ogg_iformat = {
|
2002-11-22 09:27:13 +02:00
|
|
|
"ogg",
|
|
|
|
"Ogg Vorbis",
|
|
|
|
sizeof(OggContext),
|
|
|
|
NULL,
|
|
|
|
ogg_read_header,
|
|
|
|
ogg_read_packet,
|
|
|
|
ogg_read_close,
|
|
|
|
.extensions = "ogg",
|
|
|
|
} ;
|
2005-04-09 18:32:58 +03:00
|
|
|
#endif
|