Statistics
| Branch: | Revision:

ffmpeg / libavformat / ogg.c @ 25f8db58

History | View | Annotate | Download (7.17 KB)

1 81e0d0b4 Mark Hills
/*
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
13
#include "avformat.h"
14
15 6d8f985e Michael Niedermayer
#undef NDEBUG
16
#include <assert.h>
17
18 3aca208a Mark Hills
#define DECODER_BUFFER_SIZE 4096
19
20 81e0d0b4 Mark Hills
21
typedef struct OggContext {
22 3aca208a Mark Hills
    /* output */
23 81e0d0b4 Mark Hills
    ogg_stream_state os ;
24 3aca208a Mark Hills
    int header_handled ;
25 6d8f985e Michael Niedermayer
    ogg_packet op;
26 3aca208a Mark Hills
27
    /* input */
28
    ogg_sync_state oy ;
29 81e0d0b4 Mark Hills
} OggContext ;
30
31
32 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
33 a48835b9 Fabrice Bellard
static int ogg_write_header(AVFormatContext *avfcontext) 
34
{
35
    OggContext *context = avfcontext->priv_data;
36 bbb77e7c Michael Niedermayer
    ogg_packet *op= &context->op;    
37 62ab0b78 Aurelien Jacobs
    int n;
38 6d8f985e Michael Niedermayer
39 8bfed902 Michael Niedermayer
    ogg_stream_init(&context->os, 31415);
40 81e0d0b4 Mark Hills
    
41
    for(n = 0 ; n < avfcontext->nb_streams ; n++) {
42 01f4895c Michael Niedermayer
        AVCodecContext *codec = avfcontext->streams[n]->codec;
43 30545b2f Alexander Strasser
        uint8_t *headers = codec->extradata;
44
        int headers_len = codec->extradata_size;
45
        uint8_t *header_start[3];
46
        int header_len[3];
47 62ab0b78 Aurelien Jacobs
        int i, j;
48 bbb77e7c Michael Niedermayer
        
49 9ee91c2f Michael Niedermayer
        av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
50
51 30545b2f Alexander Strasser
        for(j=1,i=0;i<2;++i, ++j) {
52
            header_len[i]=0;
53
            while(j<headers_len && headers[j]==0xff) {
54
                header_len[i]+=0xff;
55
                ++j;
56
            }
57
            header_len[i]+=headers[j];
58
        }
59
        header_len[2]=headers_len-header_len[0]-header_len[1]-j;
60
        headers+=j;
61
        header_start[0] = headers;
62
        header_start[1] = header_start[0] + header_len[0];
63
        header_start[2] = header_start[1] + header_len[1];
64
65
        for(i=0; i < 3; ++i){
66
            op->bytes = header_len[i];
67 bbb77e7c Michael Niedermayer
68 30545b2f Alexander Strasser
            op->packet= header_start[i];
69 bbb77e7c Michael Niedermayer
            op->b_o_s= op->packetno==0;
70
71
            ogg_stream_packetin(&context->os, op);
72
73
            op->packetno++; //FIXME multiple streams
74
        }
75 81e0d0b4 Mark Hills
76 3aca208a Mark Hills
        context->header_handled = 0 ;
77 81e0d0b4 Mark Hills
    }
78
    
79
    return 0 ;
80
}
81
82 e928649b Michael Niedermayer
static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
83 81e0d0b4 Mark Hills
{
84
    OggContext *context = avfcontext->priv_data ;
85 01f4895c Michael Niedermayer
    AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
86 6d8f985e Michael Niedermayer
    ogg_packet *op= &context->op;
87 81e0d0b4 Mark Hills
    ogg_page og ;
88 e928649b Michael Niedermayer
    int64_t pts;
89 6d8f985e Michael Niedermayer
90 e928649b Michael Niedermayer
    pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
91 6d8f985e Michael Niedermayer
92
//    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
93
94 81e0d0b4 Mark Hills
    /* flush header packets so audio starts on a new page */
95
96 3aca208a Mark Hills
    if(!context->header_handled) {
97 81e0d0b4 Mark Hills
        while(ogg_stream_flush(&context->os, &og)) {
98
            put_buffer(&avfcontext->pb, og.header, og.header_len) ;
99
            put_buffer(&avfcontext->pb, og.body, og.body_len) ;
100
            put_flush_packet(&avfcontext->pb);
101
        }
102 3aca208a Mark Hills
        context->header_handled = 1 ;
103 81e0d0b4 Mark Hills
    }
104
105 e928649b Michael Niedermayer
    op->packet = (uint8_t*) pkt->data;
106
    op->bytes  = pkt->size;
107 6d8f985e Michael Niedermayer
    op->b_o_s  = op->packetno == 0;
108
    op->granulepos= pts;
109
110
    /* correct the fields in the packet -- essential for streaming */
111
                                                        
112
    ogg_stream_packetin(&context->os, op);              
113
                                                        
114
    while(ogg_stream_pageout(&context->os, &og)) {
115
        put_buffer(&avfcontext->pb, og.header, og.header_len);
116
        put_buffer(&avfcontext->pb, og.body, og.body_len);     
117
        put_flush_packet(&avfcontext->pb);
118
    }                                                   
119
    op->packetno++;
120 81e0d0b4 Mark Hills
121
    return 0;
122
}
123
124
125
static int ogg_write_trailer(AVFormatContext *avfcontext) {
126
    OggContext *context = avfcontext->priv_data ;
127
    ogg_page og ;
128
129
    while(ogg_stream_flush(&context->os, &og)) {
130
        put_buffer(&avfcontext->pb, og.header, og.header_len) ;
131
        put_buffer(&avfcontext->pb, og.body, og.body_len) ;
132
        put_flush_packet(&avfcontext->pb);
133
    }
134
135
    ogg_stream_clear(&context->os) ;
136
    return 0 ;
137
}
138
139
140 c18a2692 Zdenek Kabelac
static AVOutputFormat ogg_oformat = {
141 81e0d0b4 Mark Hills
    "ogg",
142
    "Ogg Vorbis",
143
    "audio/x-vorbis",
144
    "ogg",
145
    sizeof(OggContext),
146
    CODEC_ID_VORBIS,
147
    0,
148
    ogg_write_header,
149
    ogg_write_packet,
150
    ogg_write_trailer,
151 3aca208a Mark Hills
} ;
152 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
153 3aca208a Mark Hills
154 9146ca37 Måns Rullgård
#if 0
155 3aca208a Mark Hills
static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
156
    OggContext *context = avfcontext->priv_data ;
157
    ogg_page og ;
158
    char *buf ;
159

160
    while(ogg_stream_packetout(&context->os, op) != 1) {
161

162
        /* while no pages are available, read in more data to the sync */
163
        while(ogg_sync_pageout(&context->oy, &og) != 1) {
164
            buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
165
            if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
166
                return 1 ;
167
            ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; 
168
        }        
169
        
170
        /* got a page. Feed it into the stream and get the packet */
171
        if(ogg_stream_pagein(&context->os, &og) != 0)
172
            return 1 ;
173
    }
174

175
    return 0 ;
176
}
177

178

179
static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
180
{
181 a48835b9 Fabrice Bellard
    OggContext *context = avfcontext->priv_data;
182 a03cbe5f Michael Niedermayer
    ogg_packet op ;    
183 3aca208a Mark Hills
    char *buf ;
184
    ogg_page og ;
185
    AVStream *ast ;
186 a03cbe5f Michael Niedermayer
    AVCodecContext *codec;
187
    uint8_t *p;
188
    int i;
189 8bfed902 Michael Niedermayer
     
190 3aca208a Mark Hills
    ogg_sync_init(&context->oy) ;
191
    buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
192

193
    if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
194 0bd586c5 Mike Melanson
        return AVERROR_IO ;
195 3aca208a Mark Hills
    
196
    ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;   
197
    ogg_sync_pageout(&context->oy, &og) ;
198
    ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
199
    ogg_stream_pagein(&context->os, &og) ;
200 a03cbe5f Michael Niedermayer
    
201 3aca208a Mark Hills
    /* currently only one vorbis stream supported */
202

203
    ast = av_new_stream(avfcontext, 0) ;
204
    if(!ast)
205
        return AVERROR_NOMEM ;
206 9ee91c2f Michael Niedermayer
    av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
207 3aca208a Mark Hills

208 a03cbe5f Michael Niedermayer
    codec= &ast->codec;
209
    codec->codec_type = CODEC_TYPE_AUDIO;
210
    codec->codec_id = CODEC_ID_VORBIS;
211
    for(i=0; i<3; i++){
212
        if(next_packet(avfcontext, &op)){
213
            return -1;
214
        }
215 568e18b1 Michael Niedermayer
        if(op.bytes >= (1<<16) || op.bytes < 0)
216
            return -1;
217 a03cbe5f Michael Niedermayer
        codec->extradata_size+= 2 + op.bytes;
218 3129cd01 Michael Niedermayer
        codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
219 a03cbe5f Michael Niedermayer
        p= codec->extradata + codec->extradata_size - 2 - op.bytes;
220
        *(p++)= op.bytes>>8;
221
        *(p++)= op.bytes&0xFF;
222
        memcpy(p, op.packet, op.bytes);
223
    }
224

225 3aca208a Mark Hills
    return 0 ;
226
}
227

228

229
static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
230
    ogg_packet op ;
231

232
    if(next_packet(avfcontext, &op)) 
233 0bd586c5 Mike Melanson
        return AVERROR_IO ;
234 d76f581f Michael Niedermayer
    if(av_new_packet(pkt, op.bytes) < 0)
235 0bd586c5 Mike Melanson
        return AVERROR_IO ;
236 3aca208a Mark Hills
    pkt->stream_index = 0 ;
237 d76f581f Michael Niedermayer
    memcpy(pkt->data, op.packet, op.bytes);
238 c0c37848 Michael Niedermayer
    if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
239
        pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
240
//    printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
241 3aca208a Mark Hills

242 d76f581f Michael Niedermayer
    return op.bytes;
243 3aca208a Mark Hills
}
244

245

246
static int ogg_read_close(AVFormatContext *avfcontext) {
247
    OggContext *context = avfcontext->priv_data ;
248

249
    ogg_stream_clear(&context->os) ;
250
    ogg_sync_clear(&context->oy) ;
251 a03cbe5f Michael Niedermayer
    av_freep(&avfcontext->streams[0]->codec.extradata);
252 3aca208a Mark Hills

253
    return 0 ;
254
}
255

256

257
static AVInputFormat ogg_iformat = {
258
    "ogg",
259
    "Ogg Vorbis",
260
    sizeof(OggContext),
261
    NULL,
262
    ogg_read_header,
263
    ogg_read_packet,
264
    ogg_read_close,
265
    .extensions = "ogg",
266
} ;
267 9146ca37 Måns Rullgård
#endif
268 81e0d0b4 Mark Hills
269 9146ca37 Måns Rullgård
int libogg_init(void) {
270 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
271 81e0d0b4 Mark Hills
    av_register_output_format(&ogg_oformat) ;
272 764ef400 Mike Melanson
#endif
273 9146ca37 Måns Rullgård
/*     av_register_input_format(&ogg_iformat); */
274 81e0d0b4 Mark Hills
    return 0 ;
275
}