Mercurial > libavformat.hg
annotate ogg.c @ 406:ea22a438ca79 libavformat
fix obnoxious ogg_packet passing from encoder to muxer
| author | michael |
|---|---|
| date | Sun, 04 Apr 2004 14:39:20 +0000 |
| parents | 04d7dda7ccd5 |
| children | b9d16c18ee18 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Ogg bitstream support | |
| 3 * Mark Hills <mark@pogo.org.uk> | |
| 4 * | |
| 5 * Uses libogg, but requires libvorbisenc to construct correct headers | |
| 6 * when containing Vorbis stream -- currently the only format supported | |
| 7 */ | |
| 8 | |
| 9 #include <stdio.h> | |
| 10 | |
| 11 #include <ogg/ogg.h> | |
| 12 #include <vorbis/vorbisenc.h> | |
| 13 | |
| 14 #include "avformat.h" | |
| 15 #include "oggvorbis.h" | |
| 16 | |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
17 #undef NDEBUG |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
18 #include <assert.h> |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
19 |
| 0 | 20 #define DECODER_BUFFER_SIZE 4096 |
| 21 | |
| 22 | |
| 23 typedef struct OggContext { | |
| 24 /* output */ | |
| 25 ogg_stream_state os ; | |
| 26 int header_handled ; | |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
27 ogg_packet op; |
| 0 | 28 |
| 29 /* input */ | |
| 30 ogg_sync_state oy ; | |
| 31 } OggContext ; | |
| 32 | |
| 33 | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
34 #ifdef CONFIG_ENCODERS |
| 35 | 35 static int ogg_write_header(AVFormatContext *avfcontext) |
| 36 { | |
| 37 OggContext *context = avfcontext->priv_data; | |
| 0 | 38 AVCodecContext *avccontext ; |
| 39 vorbis_info vi ; | |
| 40 vorbis_dsp_state vd ; | |
| 41 vorbis_comment vc ; | |
| 42 vorbis_block vb ; | |
| 43 ogg_packet header, header_comm, header_code ; | |
| 44 int n ; | |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
45 |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
46 av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
47 |
| 404 | 48 ogg_stream_init(&context->os, 31415); |
| 0 | 49 |
| 50 for(n = 0 ; n < avfcontext->nb_streams ; n++) { | |
| 51 avccontext = &avfcontext->streams[n]->codec ; | |
| 52 | |
| 53 /* begin vorbis specific code */ | |
| 54 | |
| 55 vorbis_info_init(&vi) ; | |
| 56 | |
| 57 /* code copied from libavcodec/oggvorbis.c */ | |
| 58 | |
| 59 if(oggvorbis_init_encoder(&vi, avccontext) < 0) { | |
| 60 fprintf(stderr, "ogg_write_header: init_encoder failed") ; | |
| 61 return -1 ; | |
| 62 } | |
| 63 | |
| 64 vorbis_analysis_init(&vd, &vi) ; | |
| 65 vorbis_block_init(&vd, &vb) ; | |
| 66 | |
| 67 vorbis_comment_init(&vc) ; | |
| 327 | 68 vorbis_comment_add_tag(&vc, "encoder", LIBAVFORMAT_IDENT) ; |
| 0 | 69 if(*avfcontext->title) |
| 70 vorbis_comment_add_tag(&vc, "title", avfcontext->title) ; | |
| 71 | |
| 72 vorbis_analysis_headerout(&vd, &vc, &header, | |
| 73 &header_comm, &header_code) ; | |
| 74 ogg_stream_packetin(&context->os, &header) ; | |
| 75 ogg_stream_packetin(&context->os, &header_comm) ; | |
| 76 ogg_stream_packetin(&context->os, &header_code) ; | |
| 77 | |
|
68
1e2f55eced38
ogg deallocate patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
35
diff
changeset
|
78 vorbis_block_clear(&vb) ; |
|
1e2f55eced38
ogg deallocate patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
35
diff
changeset
|
79 vorbis_dsp_clear(&vd) ; |
|
1e2f55eced38
ogg deallocate patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
35
diff
changeset
|
80 vorbis_info_clear(&vi) ; |
| 0 | 81 vorbis_comment_clear(&vc) ; |
|
68
1e2f55eced38
ogg deallocate patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
35
diff
changeset
|
82 |
| 0 | 83 /* end of vorbis specific code */ |
| 84 | |
| 85 context->header_handled = 0 ; | |
| 86 } | |
| 87 | |
| 88 return 0 ; | |
| 89 } | |
| 90 | |
| 91 | |
| 92 static int ogg_write_packet(AVFormatContext *avfcontext, | |
| 93 int stream_index, | |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
94 const uint8_t *buf, int size, int64_t pts) |
| 0 | 95 { |
| 96 OggContext *context = avfcontext->priv_data ; | |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
97 AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec; |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
98 ogg_packet *op= &context->op; |
| 0 | 99 ogg_page og ; |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
100 |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
101 pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
102 |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
103 if(!size){ |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
104 // av_log(avfcontext, AV_LOG_DEBUG, "zero packet\n"); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
105 return 0; |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
106 } |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
107 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
108 |
| 0 | 109 /* flush header packets so audio starts on a new page */ |
| 110 | |
| 111 if(!context->header_handled) { | |
| 112 while(ogg_stream_flush(&context->os, &og)) { | |
| 113 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
| 114 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
| 115 put_flush_packet(&avfcontext->pb); | |
| 116 } | |
| 117 context->header_handled = 1 ; | |
| 118 } | |
| 119 | |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
120 op->packet = (uint8_t*) buf; |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
121 op->bytes = size; |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
122 op->b_o_s = op->packetno == 0; |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
123 op->granulepos= pts; |
| 0 | 124 |
|
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
125 /* correct the fields in the packet -- essential for streaming */ |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
126 |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
127 ogg_stream_packetin(&context->os, op); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
128 |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
129 while(ogg_stream_pageout(&context->os, &og)) { |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
130 put_buffer(&avfcontext->pb, og.header, og.header_len); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
131 put_buffer(&avfcontext->pb, og.body, og.body_len); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
132 put_flush_packet(&avfcontext->pb); |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
133 } |
|
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
134 op->packetno++; |
| 0 | 135 |
| 136 return 0; | |
| 137 } | |
| 138 | |
| 139 | |
| 140 static int ogg_write_trailer(AVFormatContext *avfcontext) { | |
| 141 OggContext *context = avfcontext->priv_data ; | |
| 142 ogg_page og ; | |
| 143 | |
| 144 while(ogg_stream_flush(&context->os, &og)) { | |
| 145 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
| 146 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
| 147 put_flush_packet(&avfcontext->pb); | |
| 148 } | |
| 149 | |
| 150 ogg_stream_clear(&context->os) ; | |
| 151 return 0 ; | |
| 152 } | |
| 153 | |
| 154 | |
| 155 static AVOutputFormat ogg_oformat = { | |
| 156 "ogg", | |
| 157 "Ogg Vorbis", | |
| 158 "audio/x-vorbis", | |
| 159 "ogg", | |
| 160 sizeof(OggContext), | |
| 161 CODEC_ID_VORBIS, | |
| 162 0, | |
| 163 ogg_write_header, | |
| 164 ogg_write_packet, | |
| 165 ogg_write_trailer, | |
| 166 } ; | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
167 #endif //CONFIG_ENCODERS |
| 0 | 168 |
| 169 | |
| 170 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) { | |
| 171 OggContext *context = avfcontext->priv_data ; | |
| 172 ogg_page og ; | |
| 173 char *buf ; | |
| 174 | |
| 175 while(ogg_stream_packetout(&context->os, op) != 1) { | |
| 176 | |
| 177 /* while no pages are available, read in more data to the sync */ | |
| 178 while(ogg_sync_pageout(&context->oy, &og) != 1) { | |
| 179 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
| 180 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
| 181 return 1 ; | |
| 182 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
| 183 } | |
| 184 | |
| 185 /* got a page. Feed it into the stream and get the packet */ | |
| 186 if(ogg_stream_pagein(&context->os, &og) != 0) | |
| 187 return 1 ; | |
| 188 } | |
| 189 | |
| 190 return 0 ; | |
| 191 } | |
| 192 | |
| 193 | |
| 194 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap) | |
| 195 { | |
| 35 | 196 OggContext *context = avfcontext->priv_data; |
| 0 | 197 char *buf ; |
| 198 ogg_page og ; | |
| 199 AVStream *ast ; | |
| 404 | 200 |
| 201 avfcontext->ctx_flags |= AVFMTCTX_NOHEADER; | |
| 202 | |
| 0 | 203 ogg_sync_init(&context->oy) ; |
| 204 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
| 205 | |
| 206 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
| 207 return -EIO ; | |
| 208 | |
| 209 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
| 210 ogg_sync_pageout(&context->oy, &og) ; | |
| 211 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ; | |
| 212 ogg_stream_pagein(&context->os, &og) ; | |
| 213 | |
| 214 /* currently only one vorbis stream supported */ | |
| 215 | |
| 216 ast = av_new_stream(avfcontext, 0) ; | |
| 217 if(!ast) | |
| 218 return AVERROR_NOMEM ; | |
| 219 | |
| 220 ast->codec.codec_type = CODEC_TYPE_AUDIO ; | |
| 221 ast->codec.codec_id = CODEC_ID_VORBIS ; | |
| 222 | |
| 223 return 0 ; | |
| 224 } | |
| 225 | |
| 226 | |
| 227 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) { | |
| 228 ogg_packet op ; | |
| 229 | |
| 230 if(next_packet(avfcontext, &op)) | |
| 231 return -EIO ; | |
|
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
232 if(av_new_packet(pkt, op.bytes) < 0) |
| 0 | 233 return -EIO ; |
| 234 pkt->stream_index = 0 ; | |
|
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
235 memcpy(pkt->data, op.packet, op.bytes); |
| 0 | 236 |
|
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
237 return op.bytes; |
| 0 | 238 } |
| 239 | |
| 240 | |
| 241 static int ogg_read_close(AVFormatContext *avfcontext) { | |
| 242 OggContext *context = avfcontext->priv_data ; | |
| 243 | |
| 244 ogg_stream_clear(&context->os) ; | |
| 245 ogg_sync_clear(&context->oy) ; | |
| 246 | |
| 247 return 0 ; | |
| 248 } | |
| 249 | |
| 250 | |
| 251 static AVInputFormat ogg_iformat = { | |
| 252 "ogg", | |
| 253 "Ogg Vorbis", | |
| 254 sizeof(OggContext), | |
| 255 NULL, | |
| 256 ogg_read_header, | |
| 257 ogg_read_packet, | |
| 258 ogg_read_close, | |
| 259 .extensions = "ogg", | |
| 260 } ; | |
| 261 | |
| 262 | |
| 263 int ogg_init(void) { | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
264 #ifdef CONFIG_ENCODERS |
| 0 | 265 av_register_output_format(&ogg_oformat) ; |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
266 #endif |
| 0 | 267 av_register_input_format(&ogg_iformat); |
| 268 return 0 ; | |
| 269 } |
