Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_04 / synfig-core / src / modules / mod_libavcodec / libavformat / ogg.c
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 #include <time.h>
11
12 #include <ogg/ogg.h>
13 #include <vorbis/vorbisenc.h>
14
15 #include "avformat.h"
16 #include "oggvorbis.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_int64_t base_packet_no ;
26     ogg_int64_t base_granule_pos ;
27
28     /* input */
29     ogg_sync_state oy ;
30 } OggContext ;
31
32
33 static int ogg_write_header(AVFormatContext *avfcontext) 
34 {
35     OggContext *context = avfcontext->priv_data;
36     AVCodecContext *avccontext ;
37     vorbis_info vi ;
38     vorbis_dsp_state vd ;
39     vorbis_comment vc ;
40     vorbis_block vb ;
41     ogg_packet header, header_comm, header_code ; 
42     int n ;
43     
44     srand(time(NULL));
45     ogg_stream_init(&context->os, rand());
46     
47     for(n = 0 ; n < avfcontext->nb_streams ; n++) {
48         avccontext = &avfcontext->streams[n]->codec ;
49
50         /* begin vorbis specific code */
51                 
52         vorbis_info_init(&vi) ;
53
54         /* code copied from libavcodec/oggvorbis.c */
55
56         if(oggvorbis_init_encoder(&vi, avccontext) < 0) {
57             fprintf(stderr, "ogg_write_header: init_encoder failed") ;
58             return -1 ;
59         }
60
61         vorbis_analysis_init(&vd, &vi) ;
62         vorbis_block_init(&vd, &vb) ;
63         
64         vorbis_comment_init(&vc) ;
65         vorbis_comment_add_tag(&vc, "encoder", "ffmpeg") ;
66         if(*avfcontext->title)
67             vorbis_comment_add_tag(&vc, "title", avfcontext->title) ;
68
69         vorbis_analysis_headerout(&vd, &vc, &header,
70                                   &header_comm, &header_code) ;
71         ogg_stream_packetin(&context->os, &header) ;
72         ogg_stream_packetin(&context->os, &header_comm) ;
73         ogg_stream_packetin(&context->os, &header_code) ;  
74         
75         vorbis_block_clear(&vb) ;
76         vorbis_dsp_clear(&vd) ;
77         vorbis_info_clear(&vi) ;
78         vorbis_comment_clear(&vc) ;
79         
80         /* end of vorbis specific code */
81
82         context->header_handled = 0 ;
83         context->base_packet_no = 0 ;
84     }
85     
86     return 0 ;
87 }
88
89
90 static int ogg_write_packet(AVFormatContext *avfcontext,
91                             int stream_index,
92                             unsigned char *buf, int size, int force_pts)
93 {
94     OggContext *context = avfcontext->priv_data ;
95     ogg_packet *op ;
96     ogg_page og ;
97     int l = 0 ;
98     
99     /* flush header packets so audio starts on a new page */
100
101     if(!context->header_handled) {
102         while(ogg_stream_flush(&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         context->header_handled = 1 ;
108     }
109
110     while(l < size) {
111         op = (ogg_packet*)(buf + l) ;
112         op->packet = buf + l + sizeof(ogg_packet) ; /* fix data pointer */
113
114         if(!context->base_packet_no) { /* this is the first packet */
115             context->base_packet_no = op->packetno ; 
116             context->base_granule_pos = op->granulepos ;
117         }
118
119         /* correct the fields in the packet -- essential for streaming */
120
121         op->packetno -= context->base_packet_no ;
122         op->granulepos -= context->base_granule_pos ;
123
124         ogg_stream_packetin(&context->os, op) ;
125         l += sizeof(ogg_packet) + op->bytes ;
126
127         while(ogg_stream_pageout(&context->os, &og)) {
128             put_buffer(&avfcontext->pb, og.header, og.header_len) ;
129             put_buffer(&avfcontext->pb, og.body, og.body_len) ;
130             put_flush_packet(&avfcontext->pb);
131         }
132     }
133
134     return 0;
135 }
136
137
138 static int ogg_write_trailer(AVFormatContext *avfcontext) {
139     OggContext *context = avfcontext->priv_data ;
140     ogg_page og ;
141
142     while(ogg_stream_flush(&context->os, &og)) {
143         put_buffer(&avfcontext->pb, og.header, og.header_len) ;
144         put_buffer(&avfcontext->pb, og.body, og.body_len) ;
145         put_flush_packet(&avfcontext->pb);
146     }
147
148     ogg_stream_clear(&context->os) ;
149     return 0 ;
150 }
151
152
153 static AVOutputFormat ogg_oformat = {
154     "ogg",
155     "Ogg Vorbis",
156     "audio/x-vorbis",
157     "ogg",
158     sizeof(OggContext),
159     CODEC_ID_VORBIS,
160     0,
161     ogg_write_header,
162     ogg_write_packet,
163     ogg_write_trailer,
164 } ;
165
166
167 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
168     OggContext *context = avfcontext->priv_data ;
169     ogg_page og ;
170     char *buf ;
171
172     while(ogg_stream_packetout(&context->os, op) != 1) {
173
174         /* while no pages are available, read in more data to the sync */
175         while(ogg_sync_pageout(&context->oy, &og) != 1) {
176             buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
177             if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
178                 return 1 ;
179             ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; 
180         }       
181         
182         /* got a page. Feed it into the stream and get the packet */
183         if(ogg_stream_pagein(&context->os, &og) != 0)
184             return 1 ;
185     }
186
187     return 0 ;
188 }
189
190
191 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
192 {
193     OggContext *context = avfcontext->priv_data;
194     char *buf ;
195     ogg_page og ;
196     AVStream *ast ;
197     
198     ogg_sync_init(&context->oy) ;
199     buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
200
201     if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
202         return -EIO ;
203     
204     ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;   
205     ogg_sync_pageout(&context->oy, &og) ;
206     ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
207     ogg_stream_pagein(&context->os, &og) ;
208   
209     /* currently only one vorbis stream supported */
210
211     ast = av_new_stream(avfcontext, 0) ;
212     if(!ast)
213         return AVERROR_NOMEM ;
214
215     ast->codec.codec_type = CODEC_TYPE_AUDIO ;
216     ast->codec.codec_id = CODEC_ID_VORBIS ;
217     
218     return 0 ;
219 }
220
221
222 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
223     ogg_packet op ;
224
225     if(next_packet(avfcontext, &op)) 
226         return -EIO ;
227     if(av_new_packet(pkt, sizeof(ogg_packet) + op.bytes) < 0)
228         return -EIO ;
229     pkt->stream_index = 0 ;
230     memcpy(pkt->data, &op, sizeof(ogg_packet)) ;
231     memcpy(pkt->data + sizeof(ogg_packet), op.packet, op.bytes) ;
232
233     return sizeof(ogg_packet) + op.bytes ;
234 }
235
236
237 static int ogg_read_close(AVFormatContext *avfcontext) {
238     OggContext *context = avfcontext->priv_data ;
239
240     ogg_stream_clear(&context->os) ;
241     ogg_sync_clear(&context->oy) ;
242
243     return 0 ;
244 }
245
246
247 static AVInputFormat ogg_iformat = {
248     "ogg",
249     "Ogg Vorbis",
250     sizeof(OggContext),
251     NULL,
252     ogg_read_header,
253     ogg_read_packet,
254     ogg_read_close,
255     .extensions = "ogg",
256 } ;
257
258
259 int ogg_init(void) {
260     av_register_output_format(&ogg_oformat) ;
261     av_register_input_format(&ogg_iformat);
262     return 0 ;
263 }