diff options
author | Mark Hills <mark@pogo.org.uk> | 2002-09-01 18:07:56 +0000 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2002-09-01 18:07:56 +0000 |
commit | 81e0d0b412e7d3d4ee8cc3a9ac940f60a7b1ae3b (patch) | |
tree | f4429f345bf66ef382cc5207aac0ff7c15a8b752 /libav | |
parent | ad324c93515ca4acb43f5973ba67861213ff584d (diff) | |
download | ffmpeg-81e0d0b412e7d3d4ee8cc3a9ac940f60a7b1ae3b.tar.gz |
oggvorbis support patch by (Mark Hills <mark at pogo dot org dot uk>)
Originally committed as revision 896 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libav')
-rw-r--r-- | libav/Makefile | 4 | ||||
-rw-r--r-- | libav/allformats.c | 4 | ||||
-rw-r--r-- | libav/avformat.h | 3 | ||||
-rw-r--r-- | libav/ffm.c | 2 | ||||
-rw-r--r-- | libav/ogg.c | 168 |
5 files changed, 181 insertions, 0 deletions
diff --git a/libav/Makefile b/libav/Makefile index eb11986c25..1a535ffb96 100644 --- a/libav/Makefile +++ b/libav/Makefile @@ -33,6 +33,10 @@ ifeq ($(CONFIG_NETWORK),yes) OBJS+= udp.o tcp.o http.o rtsp.o rtp.o rtpproto.o endif +ifeq ($(CONFIG_VORBIS),yes) +OBJS+= ogg.o +endif + LIB= libavformat.a all: $(LIB) diff --git a/libav/allformats.c b/libav/allformats.c index ff40001bd9..3ccc7dc4d7 100644 --- a/libav/allformats.c +++ b/libav/allformats.c @@ -45,6 +45,10 @@ void av_register_all(void) mov_init(); jpeg_init(); +#ifdef CONFIG_VORBIS + ogg_init(); +#endif + #ifndef CONFIG_WIN32 ffm_init(); #endif diff --git a/libav/avformat.h b/libav/avformat.h index 20a3cabbd8..b9a618d35b 100644 --- a/libav/avformat.h +++ b/libav/avformat.h @@ -213,6 +213,9 @@ int wav_init(void); /* raw.c */ int raw_init(void); +/* ogg.c */ +int ogg_init(void); + /* ffm.c */ int ffm_init(void); diff --git a/libav/ffm.c b/libav/ffm.c index c6033c5bfc..205626fe67 100644 --- a/libav/ffm.c +++ b/libav/ffm.c @@ -151,6 +151,7 @@ static int ffm_write_header(AVFormatContext *s) put_be32(pb, codec->codec_id); put_byte(pb, codec->codec_type); put_be32(pb, codec->bit_rate); + put_be32(pb, codec->quality); put_be32(pb, codec->flags); /* specific info */ switch(codec->codec_type) { @@ -393,6 +394,7 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap) st->codec.codec_id = get_be32(pb); st->codec.codec_type = get_byte(pb); /* codec_type */ codec->bit_rate = get_be32(pb); + codec->quality = get_be32(pb); codec->flags = get_be32(pb); /* specific info */ switch(codec->codec_type) { diff --git a/libav/ogg.c b/libav/ogg.c new file mode 100644 index 0000000000..61d8ebb4e5 --- /dev/null +++ b/libav/ogg.c @@ -0,0 +1,168 @@ +/* + * 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 + */ + +#include <stdio.h> +#include <time.h> + +#include <ogg/ogg.h> +#include <vorbis/vorbisenc.h> + +#include "avformat.h" +#include "oggvorbis.h" + + +typedef struct OggContext { + ogg_stream_state os ; + int header_written ; + ogg_int64_t base_packet_no ; + ogg_int64_t base_granule_pos ; +} OggContext ; + + +static int ogg_write_header(AVFormatContext *avfcontext) { + OggContext *context ; + AVCodecContext *avccontext ; + vorbis_info vi ; + vorbis_dsp_state vd ; + vorbis_comment vc ; + vorbis_block vb ; + ogg_packet header, header_comm, header_code ; + int n ; + + fprintf(stderr, "ogg_write_header\n") ; + + if(!(context = malloc(sizeof(OggContext)))) + return -1 ; + avfcontext->priv_data = context ; + + srand(time(NULL)); + ogg_stream_init(&context->os, rand()); + + for(n = 0 ; n < avfcontext->nb_streams ; n++) { + avccontext = &avfcontext->streams[n]->codec ; + + /* begin vorbis specific code */ + + vorbis_info_init(&vi) ; + + /* code copied from libavcodec/oggvorbis.c */ + + if(oggvorbis_init_encoder(&vi, avccontext) < 0) { + fprintf(stderr, "ogg_write_header: init_encoder failed") ; + return -1 ; + } + + vorbis_analysis_init(&vd, &vi) ; + vorbis_block_init(&vd, &vb) ; + + vorbis_comment_init(&vc) ; + vorbis_comment_add_tag(&vc, "encoder", "ffmpeg") ; + if(*avfcontext->title) + vorbis_comment_add_tag(&vc, "title", avfcontext->title) ; + + vorbis_analysis_headerout(&vd, &vc, &header, + &header_comm, &header_code) ; + ogg_stream_packetin(&context->os, &header) ; + ogg_stream_packetin(&context->os, &header_comm) ; + ogg_stream_packetin(&context->os, &header_code) ; + + vorbis_comment_clear(&vc) ; + + /* end of vorbis specific code */ + + context->header_written = 0 ; + context->base_packet_no = 0 ; + } + + return 0 ; +} + + +static int ogg_write_packet(AVFormatContext *avfcontext, + int stream_index, + unsigned char *buf, int size, int force_pts) +{ + OggContext *context = avfcontext->priv_data ; + ogg_packet *op ; + ogg_page og ; + int l = 0 ; + + /* flush header packets so audio starts on a new page */ + + if(!context->header_written) { + 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_written = 1 ; + } + + while(l < size) { + op = (ogg_packet*)(buf + l) ; + op->packet = buf + l + sizeof(ogg_packet) ; /* fix data pointer */ + + if(!context->base_packet_no) { /* this is the first packet */ + context->base_packet_no = op->packetno ; + context->base_granule_pos = op->granulepos ; + } + + /* correct the fields in the packet -- essential for streaming */ + + op->packetno -= context->base_packet_no ; + op->granulepos -= context->base_granule_pos ; + + ogg_stream_packetin(&context->os, op) ; + l += sizeof(ogg_packet) + op->bytes ; + + while(ogg_stream_pageout(&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); + } + } + + return 0; +} + + +static int ogg_write_trailer(AVFormatContext *avfcontext) { + OggContext *context = avfcontext->priv_data ; + ogg_page og ; + + fprintf(stderr, "ogg_write_trailer\n") ; + + 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); + } + + ogg_stream_clear(&context->os) ; + return 0 ; +} + + +AVOutputFormat ogg_oformat = { + "ogg", + "Ogg Vorbis", + "audio/x-vorbis", + "ogg", + sizeof(OggContext), + CODEC_ID_VORBIS, + 0, + ogg_write_header, + ogg_write_packet, + ogg_write_trailer, +}; + + +int ogg_init(void) { + av_register_output_format(&ogg_oformat) ; + return 0 ; +} |