annotate ogg.c @ 1341:884baacb7f7e libavformat

use a data definition uls table to fetch codec type
author bcoudurier
date Fri, 29 Sep 2006 12:23:38 +0000
parents 8bf9be9bb107
children 0899bfe4105c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * Ogg bitstream support
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Mark Hills <mark@pogo.org.uk>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * Uses libogg, but requires libvorbisenc to construct correct headers
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * when containing Vorbis stream -- currently the only format supported
1306
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
7 *
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
8 * This library is free software; you can redistribute it and/or
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
10 * License as published by the Free Software Foundation; either
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
11 * version 2 of the License, or (at your option) any later version.
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
12 *
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
13 * This library is distributed in the hope that it will be useful,
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
16 * Lesser General Public License for more details.
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
17 *
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
19 * License along with this library; if not, write to the Free Software
8bf9be9bb107 Add official LGPL license headers to the files that were missing them.
diego
parents: 1169
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #include <stdio.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 #include <ogg/ogg.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
29 #undef NDEBUG
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
30 #include <assert.h>
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
31
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 #define DECODER_BUFFER_SIZE 4096
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 typedef struct OggContext {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 /* output */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 ogg_stream_state os ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 int header_handled ;
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
39 ogg_packet op;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 /* input */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 ogg_sync_state oy ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 } OggContext ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 857
diff changeset
46 #ifdef CONFIG_MUXERS
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
47 static int ogg_write_header(AVFormatContext *avfcontext)
35
a16ccdf4a87c removed unneeded mallocs
bellard
parents: 0
diff changeset
48 {
a16ccdf4a87c removed unneeded mallocs
bellard
parents: 0
diff changeset
49 OggContext *context = avfcontext->priv_data;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
50 ogg_packet *op= &context->op;
857
26aaf3eed638 fix unused variable warnings
aurel
parents: 843
diff changeset
51 int n;
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
52
404
6579fe3ade40 remove spyware from ogg*.c
michael
parents: 327
diff changeset
53 ogg_stream_init(&context->os, 31415);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
54
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 for(n = 0 ; n < avfcontext->nb_streams ; n++) {
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 726
diff changeset
56 AVCodecContext *codec = avfcontext->streams[n]->codec;
843
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
57 uint8_t *headers = codec->extradata;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
58 int headers_len = codec->extradata_size;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
59 uint8_t *header_start[3];
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
60 int header_len[3];
857
26aaf3eed638 fix unused variable warnings
aurel
parents: 843
diff changeset
61 int i, j;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
62
462
b69898ffc92a move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents: 454
diff changeset
63 av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
b69898ffc92a move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents: 454
diff changeset
64
843
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
65 for(j=1,i=0;i<2;++i, ++j) {
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
66 header_len[i]=0;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
67 while(j<headers_len && headers[j]==0xff) {
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
68 header_len[i]+=0xff;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
69 ++j;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
70 }
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
71 header_len[i]+=headers[j];
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
72 }
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
73 header_len[2]=headers_len-header_len[0]-header_len[1]-j;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
74 headers+=j;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
75 header_start[0] = headers;
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
76 header_start[1] = header_start[0] + header_len[0];
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
77 header_start[2] = header_start[1] + header_len[1];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78
843
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
79 for(i=0; i < 3; ++i){
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
80 op->bytes = header_len[i];
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
81
dcf6e35ddb72 Make ogg `muxer' expect new vorbis extradata format.
al
parents: 820
diff changeset
82 op->packet= header_start[i];
407
b9d16c18ee18 remove function call from muxer->encoder and cleanly pass global headers
michael
parents: 406
diff changeset
83 op->b_o_s= op->packetno==0;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84
407
b9d16c18ee18 remove function call from muxer->encoder and cleanly pass global headers
michael
parents: 406
diff changeset
85 ogg_stream_packetin(&context->os, op);
b9d16c18ee18 remove function call from muxer->encoder and cleanly pass global headers
michael
parents: 406
diff changeset
86
b9d16c18ee18 remove function call from muxer->encoder and cleanly pass global headers
michael
parents: 406
diff changeset
87 op->packetno++; //FIXME multiple streams
b9d16c18ee18 remove function call from muxer->encoder and cleanly pass global headers
michael
parents: 406
diff changeset
88 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
90 context->header_handled = 0 ;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
92
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 return 0 ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
96 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 OggContext *context = avfcontext->priv_data ;
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 726
diff changeset
99 AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
100 ogg_packet *op= &context->op;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 ogg_page og ;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
102 int64_t pts;
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
103
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
104 pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
105
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
106 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
107
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 /* flush header packets so audio starts on a new page */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 if(!context->header_handled) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
111 while(ogg_stream_flush(&context->os, &og)) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
112 put_buffer(&avfcontext->pb, og.header, og.header_len) ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
113 put_buffer(&avfcontext->pb, og.body, og.body_len) ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
114 put_flush_packet(&avfcontext->pb);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
115 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
116 context->header_handled = 1 ;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
119 op->packet = (uint8_t*) pkt->data;
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 462
diff changeset
120 op->bytes = pkt->size;
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
121 op->b_o_s = op->packetno == 0;
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
122 op->granulepos= pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
124 /* correct the fields in the packet -- essential for streaming */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
125
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
126 ogg_stream_packetin(&context->os, op);
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
127
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
128 while(ogg_stream_pageout(&context->os, &og)) {
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
129 put_buffer(&avfcontext->pb, og.header, og.header_len);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
130 put_buffer(&avfcontext->pb, og.body, og.body_len);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
131 put_flush_packet(&avfcontext->pb);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
132 }
406
ea22a438ca79 fix obnoxious ogg_packet passing from encoder to muxer
michael
parents: 405
diff changeset
133 op->packetno++;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 static int ogg_write_trailer(AVFormatContext *avfcontext) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 OggContext *context = avfcontext->priv_data ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 ogg_page og ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 while(ogg_stream_flush(&context->os, &og)) {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
144 put_buffer(&avfcontext->pb, og.header, og.header_len) ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
145 put_buffer(&avfcontext->pb, og.body, og.body_len) ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
146 put_flush_packet(&avfcontext->pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 ogg_stream_clear(&context->os) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 return 0 ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
154 AVOutputFormat ogg_muxer = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 "ogg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 "Ogg Vorbis",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 "audio/x-vorbis",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 "ogg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 sizeof(OggContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 CODEC_ID_VORBIS,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 ogg_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 ogg_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 ogg_write_trailer,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 } ;
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 857
diff changeset
166 #endif //CONFIG_MUXERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167
726
17178af951b4 Ogg demuxer ported from tcvp by Luca Barbato <lu_zero at gentoo dot org>,
mru
parents: 639
diff changeset
168 #if 0
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 OggContext *context = avfcontext->priv_data ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 ogg_page og ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 char *buf ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 while(ogg_stream_packetout(&context->os, op) != 1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
176 /* while no pages are available, read in more data to the sync */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
177 while(ogg_sync_pageout(&context->oy, &og) != 1) {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
178 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
179 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
180 return 1 ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
181 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
182 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
183
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
184 /* got a page. Feed it into the stream and get the packet */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
185 if(ogg_stream_pagein(&context->os, &og) != 0)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
186 return 1 ;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 return 0 ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 {
35
a16ccdf4a87c removed unneeded mallocs
bellard
parents: 0
diff changeset
195 OggContext *context = avfcontext->priv_data;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
196 ogg_packet op ;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 char *buf ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 ogg_page og ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 AVStream *ast ;
408
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
200 AVCodecContext *codec;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
201 uint8_t *p;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
202 int i;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
203
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 ogg_sync_init(&context->oy) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
208 return AVERROR_IO ;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
209
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
210 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 ogg_sync_pageout(&context->oy, &og) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 ogg_stream_pagein(&context->os, &og) ;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
214
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 /* currently only one vorbis stream supported */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 ast = av_new_stream(avfcontext, 0) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 if(!ast)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
219 return AVERROR_NOMEM ;
462
b69898ffc92a move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents: 454
diff changeset
220 av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221
408
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
222 codec= &ast->codec;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
223 codec->codec_type = CODEC_TYPE_AUDIO;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
224 codec->codec_id = CODEC_ID_VORBIS;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
225 for(i=0; i<3; i++){
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
226 if(next_packet(avfcontext, &op)){
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
227 return -1;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
228 }
639
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
229 if(op.bytes >= (1<<16) || op.bytes < 0)
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
230 return -1;
408
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
231 codec->extradata_size+= 2 + op.bytes;
587
fe24632a577b allocate a few bytes more for extradata so the bitstream reader if its used by the codec for parsing extardata, doesnt read over the end
michael
parents: 486
diff changeset
232 codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
884
2ece9c9dd94c malloc padding to avoid reading past the malloc()ed area.
henry
parents: 858
diff changeset
233 memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
408
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
234 p= codec->extradata + codec->extradata_size - 2 - op.bytes;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
235 *(p++)= op.bytes>>8;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
236 *(p++)= op.bytes&0xFF;
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
237 memcpy(p, op.packet, op.bytes);
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
238 }
237eeeb50fb8 fix global header passing from demuxer to decoder
michael
parents: 407
diff changeset
239
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 return 0 ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 ogg_packet op ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 884
diff changeset
247 if(next_packet(avfcontext, &op))
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
248 return AVERROR_IO ;
405
04d7dda7ccd5 kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents: 404
diff changeset
249 if(av_new_packet(pkt, op.bytes) < 0)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
250 return AVERROR_IO ;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 pkt->stream_index = 0 ;
405
04d7dda7ccd5 kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents: 404
diff changeset
252 memcpy(pkt->data, op.packet, op.bytes);
454
7fa377b2f533 export pts if available
michael
parents: 409
diff changeset
253 if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
7fa377b2f533 export pts if available
michael
parents: 409
diff changeset
254 pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
7fa377b2f533 export pts if available
michael
parents: 409
diff changeset
255 // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256
405
04d7dda7ccd5 kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents: 404
diff changeset
257 return op.bytes;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 static int ogg_read_close(AVFormatContext *avfcontext) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 OggContext *context = avfcontext->priv_data ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 ogg_stream_clear(&context->os) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 ogg_sync_clear(&context->oy) ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 return 0 ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
271 static AVInputFormat ogg_iformat = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 "ogg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 "Ogg Vorbis",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 sizeof(OggContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 NULL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 ogg_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 ogg_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 ogg_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 .extensions = "ogg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 } ;
726
17178af951b4 Ogg demuxer ported from tcvp by Luca Barbato <lu_zero at gentoo dot org>,
mru
parents: 639
diff changeset
281 #endif