Statistics
| Branch: | Revision:

ffmpeg / libavformat / ogg.c @ d2062ff6

History | View | Annotate | Download (7.72 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
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24

    
25
#include <stdio.h>
26

    
27
#include <ogg/ogg.h>
28

    
29
#include "avformat.h"
30

    
31

    
32
#define DECODER_BUFFER_SIZE 4096
33

    
34

    
35
typedef struct OggContext {
36
    /* output */
37
    ogg_stream_state os ;
38
    int header_handled ;
39
    ogg_packet op;
40

    
41
    /* input */
42
    ogg_sync_state oy ;
43
} OggContext ;
44

    
45

    
46
#ifdef CONFIG_MUXERS
47
static int ogg_write_header(AVFormatContext *avfcontext)
48
{
49
    OggContext *context = avfcontext->priv_data;
50
    ogg_packet *op= &context->op;
51
    int n;
52

    
53
    ogg_stream_init(&context->os, 31415);
54

    
55
    for(n = 0 ; n < avfcontext->nb_streams ; n++) {
56
        AVCodecContext *codec = avfcontext->streams[n]->codec;
57
        uint8_t *headers = codec->extradata;
58
        int headers_len = codec->extradata_size;
59
        uint8_t *header_start[3];
60
        int header_len[3];
61
        int i, j;
62

    
63
        av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
64

    
65
        for(j=1,i=0;i<2;++i, ++j) {
66
            header_len[i]=0;
67
            while(j<headers_len && headers[j]==0xff) {
68
                header_len[i]+=0xff;
69
                ++j;
70
            }
71
            header_len[i]+=headers[j];
72
        }
73
        header_len[2]=headers_len-header_len[0]-header_len[1]-j;
74
        headers+=j;
75
        header_start[0] = headers;
76
        header_start[1] = header_start[0] + header_len[0];
77
        header_start[2] = header_start[1] + header_len[1];
78

    
79
        for(i=0; i < 3; ++i){
80
            op->bytes = header_len[i];
81

    
82
            op->packet= header_start[i];
83
            op->b_o_s= op->packetno==0;
84

    
85
            ogg_stream_packetin(&context->os, op);
86

    
87
            op->packetno++; //FIXME multiple streams
88
        }
89

    
90
        context->header_handled = 0 ;
91
    }
92

    
93
    return 0 ;
94
}
95

    
96
static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
97
{
98
    OggContext *context = avfcontext->priv_data ;
99
    AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
100
    ogg_packet *op= &context->op;
101
    ogg_page og ;
102
    int64_t pts;
103

    
104
    pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
105

    
106
//    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
107

    
108
    /* flush header packets so audio starts on a new page */
109

    
110
    if(!context->header_handled) {
111
        while(ogg_stream_flush(&context->os, &og)) {
112
            put_buffer(&avfcontext->pb, og.header, og.header_len) ;
113
            put_buffer(&avfcontext->pb, og.body, og.body_len) ;
114
            put_flush_packet(&avfcontext->pb);
115
        }
116
        context->header_handled = 1 ;
117
    }
118

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

    
124
    /* correct the fields in the packet -- essential for streaming */
125

    
126
    ogg_stream_packetin(&context->os, op);
127

    
128
    while(ogg_stream_pageout(&context->os, &og)) {
129
        put_buffer(&avfcontext->pb, og.header, og.header_len);
130
        put_buffer(&avfcontext->pb, og.body, og.body_len);
131
        put_flush_packet(&avfcontext->pb);
132
    }
133
    op->packetno++;
134

    
135
    return 0;
136
}
137

    
138

    
139
static int ogg_write_trailer(AVFormatContext *avfcontext) {
140
    OggContext *context = avfcontext->priv_data ;
141
    ogg_page og ;
142

    
143
    while(ogg_stream_flush(&context->os, &og)) {
144
        put_buffer(&avfcontext->pb, og.header, og.header_len) ;
145
        put_buffer(&avfcontext->pb, og.body, og.body_len) ;
146
        put_flush_packet(&avfcontext->pb);
147
    }
148

    
149
    ogg_stream_clear(&context->os) ;
150
    return 0 ;
151
}
152

    
153

    
154
AVOutputFormat ogg_muxer = {
155
    "ogg",
156
    "Ogg format",
157
    "application/ogg",
158
    "ogg",
159
    sizeof(OggContext),
160
    CODEC_ID_VORBIS,
161
    0,
162
    ogg_write_header,
163
    ogg_write_packet,
164
    ogg_write_trailer,
165
} ;
166
#endif //CONFIG_MUXERS
167

    
168
#if 0
169
static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
170
    OggContext *context = avfcontext->priv_data ;
171
    ogg_page og ;
172
    char *buf ;
173

174
    while(ogg_stream_packetout(&context->os, op) != 1) {
175

176
        /* while no pages are available, read in more data to the sync */
177
        while(ogg_sync_pageout(&context->oy, &og) != 1) {
178
            buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
179
            if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
180
                return 1 ;
181
            ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
182
        }
183

184
        /* got a page. Feed it into the stream and get the packet */
185
        if(ogg_stream_pagein(&context->os, &og) != 0)
186
            return 1 ;
187
    }
188

189
    return 0 ;
190
}
191

192

193
static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
194
{
195
    OggContext *context = avfcontext->priv_data;
196
    ogg_packet op ;
197
    char *buf ;
198
    ogg_page og ;
199
    AVStream *ast ;
200
    AVCodecContext *codec;
201
    uint8_t *p;
202
    int i;
203

204
    ogg_sync_init(&context->oy) ;
205
    buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
206

207
    if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
208
        return AVERROR(EIO) ;
209

210
    ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
211
    ogg_sync_pageout(&context->oy, &og) ;
212
    ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
213
    ogg_stream_pagein(&context->os, &og) ;
214

215
    /* currently only one vorbis stream supported */
216

217
    ast = av_new_stream(avfcontext, 0) ;
218
    if(!ast)
219
        return AVERROR(ENOMEM) ;
220
    av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
221

222
    codec= &ast->codec;
223
    codec->codec_type = CODEC_TYPE_AUDIO;
224
    codec->codec_id = CODEC_ID_VORBIS;
225
    for(i=0; i<3; i++){
226
        if(next_packet(avfcontext, &op)){
227
            return -1;
228
        }
229
        if(op.bytes >= (1<<16) || op.bytes < 0)
230
            return -1;
231
        codec->extradata_size+= 2 + op.bytes;
232
        codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
233
        memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
234
        p= codec->extradata + codec->extradata_size - 2 - op.bytes;
235
        *(p++)= op.bytes>>8;
236
        *(p++)= op.bytes&0xFF;
237
        memcpy(p, op.packet, op.bytes);
238
    }
239

240
    return 0 ;
241
}
242

243

244
static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
245
    ogg_packet op ;
246

247
    if(next_packet(avfcontext, &op))
248
        return AVERROR(EIO) ;
249
    if(av_new_packet(pkt, op.bytes) < 0)
250
        return AVERROR(EIO) ;
251
    pkt->stream_index = 0 ;
252
    memcpy(pkt->data, op.packet, op.bytes);
253
    if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
254
        pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
255
//    printf("%"PRId64" %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
256

257
    return op.bytes;
258
}
259

260

261
static int ogg_read_close(AVFormatContext *avfcontext) {
262
    OggContext *context = avfcontext->priv_data ;
263

264
    ogg_stream_clear(&context->os) ;
265
    ogg_sync_clear(&context->oy) ;
266

267
    return 0 ;
268
}
269

270

271
static AVInputFormat ogg_iformat = {
272
    "ogg",
273
    "Ogg Vorbis",
274
    sizeof(OggContext),
275
    NULL,
276
    ogg_read_header,
277
    ogg_read_packet,
278
    ogg_read_close,
279
    .extensions = "ogg",
280
} ;
281
#endif