Statistics
| Branch: | Revision:

ffmpeg / libavformat / ogg.c @ 6d8f985e

History | View | Annotate | Download (6.47 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
#include <vorbis/vorbisenc.h>
13

    
14
#include "avformat.h"
15
#include "oggvorbis.h"
16

    
17
#undef NDEBUG
18
#include <assert.h>
19

    
20
#define DECODER_BUFFER_SIZE 4096
21

    
22

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

    
29
    /* input */
30
    ogg_sync_state oy ;
31
} OggContext ;
32

    
33

    
34
#ifdef CONFIG_ENCODERS
35
static int ogg_write_header(AVFormatContext *avfcontext) 
36
{
37
    OggContext *context = avfcontext->priv_data;
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 ;
45

    
46
    av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE);
47

    
48
    ogg_stream_init(&context->os, 31415);
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) ;
68
        vorbis_comment_add_tag(&vc, "encoder", LIBAVFORMAT_IDENT) ;
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
        
78
        vorbis_block_clear(&vb) ;
79
        vorbis_dsp_clear(&vd) ;
80
        vorbis_info_clear(&vi) ;
81
        vorbis_comment_clear(&vc) ;
82
        
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,
94
                            const uint8_t *buf, int size, int64_t pts)
95
{
96
    OggContext *context = avfcontext->priv_data ;
97
    AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec;
98
    ogg_packet *op= &context->op;
99
    ogg_page og ;
100

    
101
    pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE);
102

    
103
    if(!size){
104
//        av_log(avfcontext, AV_LOG_DEBUG, "zero packet\n");
105
        return 0;
106
    }
107
//    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
108

    
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

    
120
    op->packet = (uint8_t*) buf;
121
    op->bytes  = size;
122
    op->b_o_s  = op->packetno == 0;
123
    op->granulepos= pts;
124

    
125
    /* correct the fields in the packet -- essential for streaming */
126
                                                        
127
    ogg_stream_packetin(&context->os, op);              
128
                                                        
129
    while(ogg_stream_pageout(&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
    op->packetno++;
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
} ;
167
#endif //CONFIG_ENCODERS
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
{
196
    OggContext *context = avfcontext->priv_data;
197
    char *buf ;
198
    ogg_page og ;
199
    AVStream *ast ;
200
     
201
    avfcontext->ctx_flags |= AVFMTCTX_NOHEADER;
202
     
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 ;
232
    if(av_new_packet(pkt, op.bytes) < 0)
233
        return -EIO ;
234
    pkt->stream_index = 0 ;
235
    memcpy(pkt->data, op.packet, op.bytes);
236

    
237
    return op.bytes;
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) {
264
#ifdef CONFIG_ENCODERS
265
    av_register_output_format(&ogg_oformat) ;
266
#endif
267
    av_register_input_format(&ogg_iformat);
268
    return 0 ;
269
}