Statistics
| Branch: | Revision:

ffmpeg / libavformat / ogg.c @ e0c850a8

History | View | Annotate | Download (7.72 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 04d7f601 Diego Biurrun
 *
8 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11 04d7f601 Diego Biurrun
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
14 04d7f601 Diego Biurrun
 *
15 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
16 04d7f601 Diego Biurrun
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
22 04d7f601 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 81e0d0b4 Mark Hills
 */
24
25
#include <stdio.h>
26
27
#include <ogg/ogg.h>
28
29
#include "avformat.h"
30
31 6d8f985e Michael Niedermayer
32 3aca208a Mark Hills
#define DECODER_BUFFER_SIZE 4096
33
34 81e0d0b4 Mark Hills
35
typedef struct OggContext {
36 3aca208a Mark Hills
    /* output */
37 81e0d0b4 Mark Hills
    ogg_stream_state os ;
38 3aca208a Mark Hills
    int header_handled ;
39 6d8f985e Michael Niedermayer
    ogg_packet op;
40 3aca208a Mark Hills
41
    /* input */
42
    ogg_sync_state oy ;
43 81e0d0b4 Mark Hills
} OggContext ;
44
45
46 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
47 115329f1 Diego Biurrun
static int ogg_write_header(AVFormatContext *avfcontext)
48 a48835b9 Fabrice Bellard
{
49
    OggContext *context = avfcontext->priv_data;
50 115329f1 Diego Biurrun
    ogg_packet *op= &context->op;
51 62ab0b78 Aurelien Jacobs
    int n;
52 6d8f985e Michael Niedermayer
53 8bfed902 Michael Niedermayer
    ogg_stream_init(&context->os, 31415);
54 115329f1 Diego Biurrun
55 81e0d0b4 Mark Hills
    for(n = 0 ; n < avfcontext->nb_streams ; n++) {
56 01f4895c Michael Niedermayer
        AVCodecContext *codec = avfcontext->streams[n]->codec;
57 30545b2f Alexander Strasser
        uint8_t *headers = codec->extradata;
58
        int headers_len = codec->extradata_size;
59
        uint8_t *header_start[3];
60
        int header_len[3];
61 62ab0b78 Aurelien Jacobs
        int i, j;
62 115329f1 Diego Biurrun
63 9ee91c2f Michael Niedermayer
        av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
64
65 30545b2f Alexander Strasser
        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 bbb77e7c Michael Niedermayer
82 30545b2f Alexander Strasser
            op->packet= header_start[i];
83 bbb77e7c Michael Niedermayer
            op->b_o_s= op->packetno==0;
84
85
            ogg_stream_packetin(&context->os, op);
86
87
            op->packetno++; //FIXME multiple streams
88
        }
89 81e0d0b4 Mark Hills
90 bb270c08 Diego Biurrun
        context->header_handled = 0 ;
91 81e0d0b4 Mark Hills
    }
92 115329f1 Diego Biurrun
93 81e0d0b4 Mark Hills
    return 0 ;
94
}
95
96 e928649b Michael Niedermayer
static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
97 81e0d0b4 Mark Hills
{
98
    OggContext *context = avfcontext->priv_data ;
99 01f4895c Michael Niedermayer
    AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
100 6d8f985e Michael Niedermayer
    ogg_packet *op= &context->op;
101 81e0d0b4 Mark Hills
    ogg_page og ;
102 e928649b Michael Niedermayer
    int64_t pts;
103 6d8f985e Michael Niedermayer
104 e928649b Michael Niedermayer
    pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
105 6d8f985e Michael Niedermayer
106
//    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
107
108 81e0d0b4 Mark Hills
    /* flush header packets so audio starts on a new page */
109
110 3aca208a Mark Hills
    if(!context->header_handled) {
111 bb270c08 Diego Biurrun
        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 81e0d0b4 Mark Hills
    }
118
119 e928649b Michael Niedermayer
    op->packet = (uint8_t*) pkt->data;
120
    op->bytes  = pkt->size;
121 6d8f985e Michael Niedermayer
    op->b_o_s  = op->packetno == 0;
122
    op->granulepos= pts;
123
124
    /* correct the fields in the packet -- essential for streaming */
125 115329f1 Diego Biurrun
126
    ogg_stream_packetin(&context->os, op);
127
128 6d8f985e Michael Niedermayer
    while(ogg_stream_pageout(&context->os, &og)) {
129
        put_buffer(&avfcontext->pb, og.header, og.header_len);
130 bb270c08 Diego Biurrun
        put_buffer(&avfcontext->pb, og.body, og.body_len);
131
        put_flush_packet(&avfcontext->pb);
132 115329f1 Diego Biurrun
    }
133 6d8f985e Michael Niedermayer
    op->packetno++;
134 81e0d0b4 Mark Hills
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 bb270c08 Diego Biurrun
        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 81e0d0b4 Mark Hills
    }
148
149
    ogg_stream_clear(&context->os) ;
150
    return 0 ;
151
}
152
153
154 ff70e601 Måns Rullgård
AVOutputFormat ogg_muxer = {
155 81e0d0b4 Mark Hills
    "ogg",
156 1367cffd Diego Biurrun
    "Ogg format",
157
    "application/ogg",
158 81e0d0b4 Mark Hills
    "ogg",
159
    sizeof(OggContext),
160
    CODEC_ID_VORBIS,
161
    0,
162
    ogg_write_header,
163
    ogg_write_packet,
164
    ogg_write_trailer,
165 3aca208a Mark Hills
} ;
166 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
167 3aca208a Mark Hills
168 9146ca37 Måns Rullgård
#if 0
169 3aca208a Mark Hills
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 bb270c08 Diego Biurrun
        /* 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 3aca208a Mark Hills
    }
188

189
    return 0 ;
190
}
191

192

193
static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
194
{
195 a48835b9 Fabrice Bellard
    OggContext *context = avfcontext->priv_data;
196 115329f1 Diego Biurrun
    ogg_packet op ;
197 3aca208a Mark Hills
    char *buf ;
198
    ogg_page og ;
199
    AVStream *ast ;
200 a03cbe5f Michael Niedermayer
    AVCodecContext *codec;
201
    uint8_t *p;
202
    int i;
203 115329f1 Diego Biurrun

204 3aca208a Mark Hills
    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 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO) ;
209 115329f1 Diego Biurrun

210
    ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
211 3aca208a Mark Hills
    ogg_sync_pageout(&context->oy, &og) ;
212
    ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
213
    ogg_stream_pagein(&context->os, &og) ;
214 115329f1 Diego Biurrun

215 3aca208a Mark Hills
    /* currently only one vorbis stream supported */
216

217
    ast = av_new_stream(avfcontext, 0) ;
218
    if(!ast)
219 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM) ;
220 9ee91c2f Michael Niedermayer
    av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
221 3aca208a Mark Hills

222 a03cbe5f Michael Niedermayer
    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 568e18b1 Michael Niedermayer
        if(op.bytes >= (1<<16) || op.bytes < 0)
230
            return -1;
231 a03cbe5f Michael Niedermayer
        codec->extradata_size+= 2 + op.bytes;
232 3129cd01 Michael Niedermayer
        codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
233 d76319b1 Jindřich Makovička
        memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
234 a03cbe5f Michael Niedermayer
        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 3aca208a Mark Hills
    return 0 ;
241
}
242

243

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

247 115329f1 Diego Biurrun
    if(next_packet(avfcontext, &op))
248 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO) ;
249 d76f581f Michael Niedermayer
    if(av_new_packet(pkt, op.bytes) < 0)
250 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO) ;
251 3aca208a Mark Hills
    pkt->stream_index = 0 ;
252 d76f581f Michael Niedermayer
    memcpy(pkt->data, op.packet, op.bytes);
253 c0c37848 Michael Niedermayer
    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 949b1a13 Steve L'Homme
//    printf("%"PRId64" %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
256 3aca208a Mark Hills

257 d76f581f Michael Niedermayer
    return op.bytes;
258 3aca208a Mark Hills
}
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 ff70e601 Måns Rullgård
static AVInputFormat ogg_iformat = {
272 3aca208a Mark Hills
    "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 9146ca37 Måns Rullgård
#endif