Statistics
| Branch: | Revision:

ffmpeg / libavformat / ogg.c @ a03cbe5f

History | View | Annotate | Download (6.33 KB)

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

    
13
#include "avformat.h"
14

    
15
#undef NDEBUG
16
#include <assert.h>
17

    
18
#define DECODER_BUFFER_SIZE 4096
19

    
20

    
21
typedef struct OggContext {
22
    /* output */
23
    ogg_stream_state os ;
24
    int header_handled ;
25
    ogg_packet op;
26

    
27
    /* input */
28
    ogg_sync_state oy ;
29
} OggContext ;
30

    
31

    
32
#ifdef CONFIG_ENCODERS
33
static int ogg_write_header(AVFormatContext *avfcontext) 
34
{
35
    OggContext *context = avfcontext->priv_data;
36
    ogg_packet *op= &context->op;    
37
    int n, i;
38

    
39
    av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE);
40

    
41
    ogg_stream_init(&context->os, 31415);
42
    
43
    for(n = 0 ; n < avfcontext->nb_streams ; n++) {
44
        AVCodecContext *codec = &avfcontext->streams[n]->codec;
45
        uint8_t *p= codec->extradata;
46
        
47
        for(i=0; i < codec->extradata_size; i+= op->bytes){
48
            op->bytes = p[i++]<<8;
49
            op->bytes+= p[i++];
50

    
51
            op->packet= &p[i];
52
            op->b_o_s= op->packetno==0;
53

    
54
            ogg_stream_packetin(&context->os, op);
55

    
56
            op->packetno++; //FIXME multiple streams
57
        }
58

    
59
        context->header_handled = 0 ;
60
    }
61
    
62
    return 0 ;
63
}
64

    
65
static int ogg_write_packet(AVFormatContext *avfcontext,
66
                            int stream_index,
67
                            const uint8_t *buf, int size, int64_t pts)
68
{
69
    OggContext *context = avfcontext->priv_data ;
70
    AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec;
71
    ogg_packet *op= &context->op;
72
    ogg_page og ;
73

    
74
    pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE);
75

    
76
    if(!size){
77
//        av_log(avfcontext, AV_LOG_DEBUG, "zero packet\n");
78
        return 0;
79
    }
80
//    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
81

    
82
    /* flush header packets so audio starts on a new page */
83

    
84
    if(!context->header_handled) {
85
        while(ogg_stream_flush(&context->os, &og)) {
86
            put_buffer(&avfcontext->pb, og.header, og.header_len) ;
87
            put_buffer(&avfcontext->pb, og.body, og.body_len) ;
88
            put_flush_packet(&avfcontext->pb);
89
        }
90
        context->header_handled = 1 ;
91
    }
92

    
93
    op->packet = (uint8_t*) buf;
94
    op->bytes  = size;
95
    op->b_o_s  = op->packetno == 0;
96
    op->granulepos= pts;
97

    
98
    /* correct the fields in the packet -- essential for streaming */
99
                                                        
100
    ogg_stream_packetin(&context->os, op);              
101
                                                        
102
    while(ogg_stream_pageout(&context->os, &og)) {
103
        put_buffer(&avfcontext->pb, og.header, og.header_len);
104
        put_buffer(&avfcontext->pb, og.body, og.body_len);     
105
        put_flush_packet(&avfcontext->pb);
106
    }                                                   
107
    op->packetno++;
108

    
109
    return 0;
110
}
111

    
112

    
113
static int ogg_write_trailer(AVFormatContext *avfcontext) {
114
    OggContext *context = avfcontext->priv_data ;
115
    ogg_page og ;
116

    
117
    while(ogg_stream_flush(&context->os, &og)) {
118
        put_buffer(&avfcontext->pb, og.header, og.header_len) ;
119
        put_buffer(&avfcontext->pb, og.body, og.body_len) ;
120
        put_flush_packet(&avfcontext->pb);
121
    }
122

    
123
    ogg_stream_clear(&context->os) ;
124
    return 0 ;
125
}
126

    
127

    
128
static AVOutputFormat ogg_oformat = {
129
    "ogg",
130
    "Ogg Vorbis",
131
    "audio/x-vorbis",
132
    "ogg",
133
    sizeof(OggContext),
134
    CODEC_ID_VORBIS,
135
    0,
136
    ogg_write_header,
137
    ogg_write_packet,
138
    ogg_write_trailer,
139
} ;
140
#endif //CONFIG_ENCODERS
141

    
142

    
143
static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
144
    OggContext *context = avfcontext->priv_data ;
145
    ogg_page og ;
146
    char *buf ;
147

    
148
    while(ogg_stream_packetout(&context->os, op) != 1) {
149

    
150
        /* while no pages are available, read in more data to the sync */
151
        while(ogg_sync_pageout(&context->oy, &og) != 1) {
152
            buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
153
            if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
154
                return 1 ;
155
            ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; 
156
        }        
157
        
158
        /* got a page. Feed it into the stream and get the packet */
159
        if(ogg_stream_pagein(&context->os, &og) != 0)
160
            return 1 ;
161
    }
162

    
163
    return 0 ;
164
}
165

    
166

    
167
static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
168
{
169
    OggContext *context = avfcontext->priv_data;
170
    ogg_packet op ;    
171
    char *buf ;
172
    ogg_page og ;
173
    AVStream *ast ;
174
    AVCodecContext *codec;
175
    uint8_t *p;
176
    int i;
177
     
178
    avfcontext->ctx_flags |= AVFMTCTX_NOHEADER;
179
     
180
    ogg_sync_init(&context->oy) ;
181
    buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
182

    
183
    if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
184
        return -EIO ;
185
    
186
    ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;   
187
    ogg_sync_pageout(&context->oy, &og) ;
188
    ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
189
    ogg_stream_pagein(&context->os, &og) ;
190
    
191
    /* currently only one vorbis stream supported */
192

    
193
    ast = av_new_stream(avfcontext, 0) ;
194
    if(!ast)
195
        return AVERROR_NOMEM ;
196

    
197
    codec= &ast->codec;
198
    codec->codec_type = CODEC_TYPE_AUDIO;
199
    codec->codec_id = CODEC_ID_VORBIS;
200
    for(i=0; i<3; i++){
201
        if(next_packet(avfcontext, &op)){
202
            return -1;
203
        }
204
        codec->extradata_size+= 2 + op.bytes;
205
        codec->extradata= av_realloc(codec->extradata, codec->extradata_size);
206
        p= codec->extradata + codec->extradata_size - 2 - op.bytes;
207
        *(p++)= op.bytes>>8;
208
        *(p++)= op.bytes&0xFF;
209
        memcpy(p, op.packet, op.bytes);
210
    }
211

    
212
    return 0 ;
213
}
214

    
215

    
216
static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
217
    ogg_packet op ;
218

    
219
    if(next_packet(avfcontext, &op)) 
220
        return -EIO ;
221
    if(av_new_packet(pkt, op.bytes) < 0)
222
        return -EIO ;
223
    pkt->stream_index = 0 ;
224
    memcpy(pkt->data, op.packet, op.bytes);
225

    
226
    return op.bytes;
227
}
228

    
229

    
230
static int ogg_read_close(AVFormatContext *avfcontext) {
231
    OggContext *context = avfcontext->priv_data ;
232

    
233
    ogg_stream_clear(&context->os) ;
234
    ogg_sync_clear(&context->oy) ;
235
    av_freep(&avfcontext->streams[0]->codec.extradata);
236

    
237
    return 0 ;
238
}
239

    
240

    
241
static AVInputFormat ogg_iformat = {
242
    "ogg",
243
    "Ogg Vorbis",
244
    sizeof(OggContext),
245
    NULL,
246
    ogg_read_header,
247
    ogg_read_packet,
248
    ogg_read_close,
249
    .extensions = "ogg",
250
} ;
251

    
252

    
253
int ogg_init(void) {
254
#ifdef CONFIG_ENCODERS
255
    av_register_output_format(&ogg_oformat) ;
256
#endif
257
    av_register_input_format(&ogg_iformat);
258
    return 0 ;
259
}