1
/***************************************************************************
2
* Copyright (C) 2004 by Jean-Baptiste Mardelle *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
23
#include "kfile_theora.h"
27
#include <kgenericfactory.h>
29
#include "theora/theora.h"
30
#include "vorbis/codec.h"
32
ogg_stream_state t_stream_state;
33
ogg_stream_state v_stream_state;
38
static int queue_page(ogg_page *page)
41
ogg_stream_pagein(&t_stream_state,page);
43
ogg_stream_pagein(&v_stream_state,page);
47
static int buffer_data(FILE *in,ogg_sync_state *oy)
49
char *buffer=ogg_sync_buffer(oy,4096);
50
int bytes=fread(buffer,1,4096,in);
51
ogg_sync_wrote(oy,bytes);
55
typedef KGenericFactory<theoraPlugin> theoraFactory;
57
K_EXPORT_COMPONENT_FACTORY(kfile_theora, theoraFactory( "kfile_theora" ))
59
theoraPlugin::theoraPlugin(QObject *parent, const char *name,
60
const QStringList &args)
61
: KFilePlugin(parent, name, args)
63
// kdDebug(7034) << "theora plugin\n";
65
KFileMimeTypeInfo* info = addMimeTypeInfo( "video/x-theora" );
67
KFileMimeTypeInfo::GroupInfo* group = 0;
68
KFileMimeTypeInfo::ItemInfo* item;
72
group = addGroupInfo(info, "Video", i18n("Video Details"));
73
setAttributes(group, 0);
74
item = addItemInfo(group, "Length", i18n("Length"), QVariant::Int);
75
setUnit(item, KFileMimeTypeInfo::Seconds);
76
setHint(item, KFileMimeTypeInfo::Length);
77
item = addItemInfo(group, "Resolution", i18n("Resolution"), QVariant::Size);
78
setHint(item, KFileMimeTypeInfo::Size);
79
setUnit(item, KFileMimeTypeInfo::Pixels);
80
item = addItemInfo(group, "FrameRate", i18n("Frame Rate"), QVariant::Int);
81
setUnit(item, KFileMimeTypeInfo::FramesPerSecond);
82
item = addItemInfo(group, "TargetBitrate", i18n("Target Bitrate"), QVariant::Int);
83
setUnit(item, KFileMimeTypeInfo::Bitrate);
84
item = addItemInfo(group, "Quality", i18n("Quality"), QVariant::Int);
88
group = addGroupInfo(info, "Audio", i18n("Audio Details"));
89
setAttributes(group, 0);
90
addItemInfo(group, "Channels", i18n("Channels"), QVariant::Int);
92
item = addItemInfo(group, "SampleRate", i18n("Sample Rate"), QVariant::Int);
93
setUnit(item, KFileMimeTypeInfo::Hertz);
96
bool theoraPlugin::readInfo( KFileMetaInfo& info, uint what)
98
// most of the ogg stuff was borrowed from libtheora/examples/player_example.c
101
ogg_sync_state o_sync_state;
106
theora_comment t_comment;
107
theora_state t_state;
109
vorbis_comment v_comment;
116
ogg_int64_t duration=0;
118
// libtheora is still a bit unstable and sadly the init_ functions don't
119
// take care of things the way one would expect. So, let's do some explicit
120
// clearing of these fields.
122
memset(&t_info, 0, sizeof(theora_info));
123
memset(&t_comment, 0, sizeof(theora_comment));
124
memset(&t_state, 0, sizeof(theora_state));
126
bool readTech = false;
128
if (what & (KFileMetaInfo::Fastest |
129
KFileMetaInfo::DontCare |
130
KFileMetaInfo::TechnicalInfo))
133
if ( info.path().isEmpty() ) // remote file
136
fp = fopen(QFile::encodeName(info.path()),"rb");
139
kdDebug(7034) << "Unable to open " << QFile::encodeName(info.path()) << endl;
143
ogg_sync_init(&o_sync_state);
145
/* init supporting Vorbis structures needed in header parsing */
146
vorbis_info_init(&v_info);
147
vorbis_comment_init(&v_comment);
149
/* init supporting Theora structures needed in header parsing */
150
theora_comment_init(&t_comment);
151
theora_info_init(&t_info);
153
while(!stateflag && buffer_data(fp,&o_sync_state)!=0)
155
while (ogg_sync_pageout(&o_sync_state,&o_page)>0)
157
ogg_stream_state stream_test;
158
/* is this a mandated initial header? If not, stop parsing */
159
if(!ogg_page_bos(&o_page))
166
ogg_stream_init(&stream_test,ogg_page_serialno(&o_page));
167
ogg_stream_pagein(&stream_test,&o_page);
168
ogg_stream_packetout(&stream_test,&o_packet);
170
/* identify the codec: try theora */
171
if(!theora_p && theora_decode_header(&t_info,&t_comment,&o_packet)>=0)
174
memcpy(&t_stream_state,&stream_test,sizeof(stream_test));
175
theora_serial=ogg_page_serialno(&o_page);
178
else if(!vorbis_p && vorbis_synthesis_headerin(&v_info,&v_comment,&o_packet)>=0)
181
memcpy(&v_stream_state,&stream_test,sizeof(stream_test));
186
/* whatever it is, we don't care about it */
187
ogg_stream_clear(&stream_test);
192
/* we're expecting more header packets. */
193
bool corruptedHeaders=false;
195
while((theora_p && theora_p<3) || (vorbis_p && vorbis_p<3))
198
/* look for further theora headers */
199
while(theora_p && (theora_p<3) && (ret=ogg_stream_packetout(&t_stream_state,&o_packet)))
203
kdDebug(7034)<<"Error parsing Theora stream headers; corrupt stream?\n"<<endl;
204
corruptedHeaders=true;
206
if(theora_decode_header(&t_info,&t_comment,&o_packet))
208
kdDebug(7034)<<"Error parsing Theora stream headers; corrupt stream?"<<endl;
209
corruptedHeaders=true;
216
/* look for more vorbis header packets */
217
while(vorbis_p && (vorbis_p<3) && (ret=ogg_stream_packetout(&v_stream_state,&o_packet)))
221
kdDebug(7034)<<"Error parsing Vorbis stream headers; corrupt stream"<<endl;
222
corruptedHeaders=true;
224
if(vorbis_synthesis_headerin(&v_info,&v_comment,&o_packet))
226
kdDebug(7034)<<"Error parsing Vorbis stream headers; corrupt stream?"<<endl;
227
corruptedHeaders=true;
233
/* The header pages/packets will arrive before anything else we
234
care about, or the stream is not obeying spec */
236
if(ogg_sync_pageout(&o_sync_state,&o_page)>0)
239
/* demux into the appropriate stream */
243
int ret=buffer_data(fp,&o_sync_state); /* someone needs more data */
246
kdDebug(7034)<<"End of file while searching for codec headers."<<endl;
247
corruptedHeaders=true;
252
/* and now we have it all. initialize decoders */
253
if(theora_p && !corruptedHeaders)
255
theora_decode_init(&t_state,&t_info);
259
/* tear down the partial theora setup */
260
theora_info_clear(&t_info);
261
theora_comment_clear(&t_comment);
263
vorbis_info_clear(&v_info);
264
vorbis_comment_clear(&v_comment);
265
ogg_sync_clear(&o_sync_state);
269
//queue_page(&o_page);
271
while (buffer_data(fp,&o_sync_state))
273
while (ogg_sync_pageout(&o_sync_state,&o_page)>0)
275
// The following line was commented out by Scott Wheeler <wheeler@kde.org>
276
// We don't actually need to store all of the pages / packets in memory since
277
// (a) libtheora doesn't use them anyway in the one call that we make after this
278
// that usese t_state and (b) it basically buffers the entire file to memory if
279
// we queue them up like this and that sucks where a typical file size is a few
282
// queue_page(&o_page);
284
if (theora_serial==ogg_page_serialno(&o_page))
285
duration=(ogg_int64_t) theora_granule_time(&t_state,ogg_page_granulepos(&o_page));
291
if (t_info.fps_denominator!=0)
292
stream_fps=t_info.fps_numerator/t_info.fps_denominator;
293
KFileMetaInfoGroup videogroup = appendGroup(info, "Video");
294
appendItem(videogroup, "Length", int (duration));
295
appendItem(videogroup, "Resolution", QSize(t_info.frame_width,t_info.frame_height));
296
appendItem(videogroup, "FrameRate", stream_fps);
297
appendItem(videogroup, "Quality", (int) t_info.quality);
299
KFileMetaInfoGroup audiogroup = appendGroup(info, "Audio");
300
appendItem(audiogroup, "Channels", v_info.channels);
301
appendItem(audiogroup, "SampleRate", int(v_info.rate));
307
ogg_stream_clear(&v_stream_state);
308
vorbis_comment_clear(&v_comment);
309
vorbis_info_clear(&v_info);
312
ogg_stream_clear(&t_stream_state);
313
theora_clear(&t_state);
314
theora_comment_clear(&t_comment);
315
theora_info_clear(&t_info);
316
ogg_sync_clear(&o_sync_state);
321
#include "kfile_theora.moc"