File: xineendanalyzer.cpp

package info (click to toggle)
strigi 0.7.8-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,556 kB
  • ctags: 6,064
  • sloc: cpp: 41,963; ansic: 1,199; perl: 483; java: 367; python: 345; xml: 177; sh: 150; makefile: 27
file content (315 lines) | stat: -rw-r--r-- 10,183 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2009 Evgeny Egorochkin <phreedom.stdin@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#define STRIGI_IMPORT_API
#include <strigi/analyzerplugin.h>
#include <strigi/streamendanalyzer.h>
#include <strigi/analysisresult.h>
#include <strigi/fieldtypes.h>
#include <strigi/textutils.h>
#include <strigi/rdfnamespaces.h>
#include <xine.h>
#include <cstring>
#include <iostream>
using namespace Strigi;
using namespace std;

class XineEndAnalyzerFactory;

class STRIGI_PLUGIN_API XineEndAnalyzer : public StreamEndAnalyzer {
private:
    AnalysisResult* result;
    const XineEndAnalyzerFactory* factory;
    xine_t *engine;
    xine_audio_port_t *audiodrv;
    xine_video_port_t *videodrv;

public:
    XineEndAnalyzer(const XineEndAnalyzerFactory* f) :factory(f) {
      if ((engine = xine_new())) {
        xine_init(engine);

        audiodrv = xine_open_audio_driver(engine, NULL, NULL);
        videodrv = xine_open_video_driver(engine, NULL, XINE_VISUAL_TYPE_NONE, NULL);

        if (!audiodrv || !videodrv)
          xine_exit (engine);
      }
    }
    
    ~XineEndAnalyzer() {
      if (engine) {
        if (audiodrv)
          xine_close_audio_driver(engine, audiodrv);
        if (videodrv)
          xine_close_video_driver(engine, videodrv);
        xine_exit(engine);
      }
    }
    
    const char* name() const {
        return "XineEndAnalyzer";
    }
    bool checkHeader(const char* header, int32_t headersize) const;
    signed char analyze(AnalysisResult& idx, ::InputStream* in);
};

class STRIGI_PLUGIN_API XineEndAnalyzerFactory : public StreamEndAnalyzerFactory {
friend class XineEndAnalyzer;
private:
    StreamEndAnalyzer* newInstance() const {
        return new XineEndAnalyzer(this);
    }
    const char* name() const {
        return "XineEndAnalyzer";
    }
    void registerFields(FieldRegister& );

    const RegisteredField* durationProperty;
    const RegisteredField* widthProperty;
    const RegisteredField* heightProperty;
    const RegisteredField* frameRateProperty;
    const RegisteredField* codecProperty;
    const RegisteredField* bitrateProperty;
    const RegisteredField* channelsProperty;
    const RegisteredField* samplerateProperty;
    const RegisteredField* titleProperty;
    const RegisteredField* commentProperty;
    const RegisteredField* typeProperty;
};

const string
  videoClassName = 
    NFO "Video",
  audioClassName = 
    NFO "Audio",
  musicPieceClassName = 
    NMM_DRAFT "MusicPiece",

  titlePropertyName = 
    NIE "title",
  commentPropertyName = 
    NIE "comment",
    
  sampleratePropertyName = 
    NFO "sampleRate",
  codecPropertyName = 
    NFO "codec",
  channelsPropertyName = 
    NFO "channels",
  bitratePropertyName = 
    NFO "averageBitrate",
  durationPropertyName = 
    NFO "duration",
  widthPropertyName = 
    NFO "width",
  heightPropertyName = 
    NFO "height",
  frameRatePropertyName = 
    NFO "frameRate";

void
XineEndAnalyzerFactory::registerFields(FieldRegister& r) {
  durationProperty = r.registerField(durationPropertyName);
  widthProperty = r.registerField(widthPropertyName);
  heightProperty = r.registerField(heightPropertyName);
  frameRateProperty = r.registerField(frameRatePropertyName);
  codecProperty = r.registerField(codecPropertyName);
  bitrateProperty = r.registerField(bitratePropertyName);
  typeProperty = r.typeField;
  channelsProperty = r.registerField(channelsPropertyName);
  samplerateProperty = r.registerField(sampleratePropertyName);
  titleProperty = r.registerField(titlePropertyName);
  commentProperty = r.registerField(commentPropertyName);
}

//Have to detect here all supported formats because there's no way to pass this to xine
bool
XineEndAnalyzer::checkHeader(const char* header, int32_t headersize) const {
    return headersize>=12 && (
           !strncmp(header, "FLV", 3) // FLV
        || readLittleEndianUInt32(header) == 0x75b22630 // ASF/WMA/WMV
        || (!strncmp(header, "RIFF", 4) && !strncmp(header+8, "AVI ", 4)) // AVI
        || readLittleEndianUInt32(header) == 0xa3df451a // Matroska/MKV/MKA
        || !strncmp(header+4, "ftyp3gp", 7) // 3GPP
        || !strncmp(header+4, "ftypisom", 8) || !strncmp(header+4, "ftypmp42", 8) // MOV
        || !strncmp(header+4, "ftypMSNV", 8) || !strncmp(header+4, "ftypM4", 6) // MOV
        || (!strncmp(header, "OggS", 4) && strncmp(header+29, "vorbis", 6)) // Any ogg apart from Vorbis, which is handled by an internal analyzer
        || readLittleEndianUInt32(header) == 0x10ff3f47 // MPG
        || readLittleEndianUInt32(header) == 0xb3010000 // MPG
        || readLittleEndianUInt32(header) == 0xba010000 // MPG
        );
}

/*
Left unused:
  XINE_STREAM_INFO_SEEKABLE
  XINE_STREAM_INFO_VIDEO_RATIO
  XINE_STREAM_INFO_VIDEO_CHANNELS
  XINE_STREAM_INFO_VIDEO_STREAMS
  XINE_STREAM_INFO_VIDEO_FOURCC
  XINE_STREAM_INFO_VIDEO_HANDLED
  XINE_STREAM_INFO_AUDIO_BITS
  XINE_STREAM_INFO_AUDIO_FOURCC
  XINE_STREAM_INFO_AUDIO_HANDLED
  XINE_STREAM_INFO_HAS_CHAPTERS
  XINE_STREAM_INFO_IGNORE_VIDEO
  XINE_STREAM_INFO_IGNORE_AUDIO
  XINE_STREAM_INFO_IGNORE_SPU
  XINE_STREAM_INFO_VIDEO_HAS_STILL
  XINE_STREAM_INFO_MAX_AUDIO_CHANNEL
  XINE_STREAM_INFO_MAX_SPU_CHANNEL
  XINE_STREAM_INFO_AUDIO_MODE
  XINE_STREAM_INFO_SKIPPED_FRAMES
  XINE_STREAM_INFO_DISCARDED_FRAMES
  XINE_STREAM_INFO_VIDEO_AFD
  XINE_STREAM_INFO_DVD_TITLE_NUMBER
  XINE_STREAM_INFO_DVD_TITLE_COUNT
  XINE_STREAM_INFO_DVD_CHAPTER_NUMBER
  XINE_STREAM_INFO_DVD_CHAPTER_COUNT
  XINE_STREAM_INFO_DVD_ANGLE_NUMBER
  XINE_STREAM_INFO_DVD_ANGLE_COUNT

  XINE_META_INFO_ARTIST
  XINE_META_INFO_GENRE
  XINE_META_INFO_ALBUM
  XINE_META_INFO_YEAR
  XINE_META_INFO_SYSTEMLAYER
  XINE_META_INFO_INPUT_PLUGIN
  XINE_META_INFO_CDINDEX_DISCID
  XINE_META_INFO_TRACK_NUMBER
*/

signed char
XineEndAnalyzer::analyze(AnalysisResult& ar, ::InputStream* in) {

  xine_stream_t *stream;

  int posstream, postime, lengthtime;
  
  string filename;
  
  if ((ar.depth()==0) && (ar.path().substr(0,7) == "file://")) 
    filename = ar.path().substr(7);
  else
    filename = ar.path();
  
  if (!(stream = xine_stream_new(engine, audiodrv, videodrv)))
    return -1;

  if (!xine_open(stream, filename.c_str())) {
    xine_dispose(stream);
    return 0;
  }

  bool audio = xine_get_stream_info(stream, XINE_STREAM_INFO_HAS_AUDIO);

  if (xine_get_pos_length(stream, &posstream, &postime, &lengthtime)) {
    if (lengthtime > 0)
      ar.addValue(factory->durationProperty, lengthtime / 1000); //duration in seconds
  }

  int bitrate = xine_get_stream_info (stream, XINE_STREAM_INFO_BITRATE);
  if (bitrate > 0)
    ar.addValue(factory->bitrateProperty, bitrate);

// video properties

  if ( xine_get_stream_info(stream, XINE_STREAM_INFO_HAS_VIDEO) ) {
    ar.addValue(factory->typeProperty, videoClassName);
    
    int width = xine_get_stream_info(stream, XINE_STREAM_INFO_VIDEO_WIDTH);
    int height  = xine_get_stream_info(stream, XINE_STREAM_INFO_VIDEO_HEIGHT);
    if (width > 0 && height > 0) {
      ar.addValue(factory->widthProperty, width);
      ar.addValue(factory->heightProperty, height);
    }
    
    int duration = xine_get_stream_info(stream, XINE_STREAM_INFO_FRAME_DURATION);
    if ( duration > 0)
      ar.addValue(factory->frameRateProperty, 90000 / duration);
    
    const char *videocodec = xine_get_meta_info(stream, XINE_META_INFO_VIDEOCODEC);
    if (videocodec)
      ar.addValue(factory->codecProperty, videocodec, strlen(videocodec));

    // Somehow bitrate always ends up being 0 :(
    int bitrate = xine_get_stream_info (stream, XINE_STREAM_INFO_VIDEO_BITRATE);
    if (bitrate > 0)
      ar.addValue(factory->bitrateProperty, bitrate);
  } else if (audio) {
    ar.addValue(factory->typeProperty, musicPieceClassName);
    ar.addValue(factory->typeProperty, audioClassName);
  }

// audio properties

  if (audio) {
    // Somehow bitrate always ends up being 0 :(
    int bitrate = xine_get_stream_info (stream, XINE_STREAM_INFO_AUDIO_BITRATE);
    if (bitrate > 0)
      ar.addValue(factory->bitrateProperty, bitrate);

    const char *audiocodec = xine_get_meta_info(stream, XINE_META_INFO_AUDIOCODEC);
    if (audiocodec)
      ar.addValue(factory->codecProperty, audiocodec, strlen(audiocodec));
    
    int channels = xine_get_stream_info(stream, XINE_STREAM_INFO_AUDIO_CHANNELS);
    if (channels>0)
      ar.addValue(factory->channelsProperty, channels);

    int samplerate = xine_get_stream_info(stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE);
    if (samplerate>0)
      ar.addValue(factory->samplerateProperty, samplerate);
  }

// tags

  const char *title = xine_get_meta_info(stream, XINE_META_INFO_TITLE);
  if (title)
    ar.addValue(factory->titleProperty, title);

  const char *comment = xine_get_meta_info(stream, XINE_META_INFO_COMMENT);
  if (comment)
    ar.addValue(factory->commentProperty, comment);
  
  xine_dispose(stream);
  return 0;
}

/*
 For plugins, we need to have a way to find out which plugins are defined in a
 plugin. One instance of AnalyzerFactoryFactory per plugin profides this
 information.
*/
class Factory : public AnalyzerFactoryFactory {
public:
    list<StreamEndAnalyzerFactory*>
    streamEndAnalyzerFactories() const {
        list<StreamEndAnalyzerFactory*> af;
        af.push_back(new XineEndAnalyzerFactory());
        return af;
    }
};

/*
 Register the AnalyzerFactoryFactory
*/
STRIGI_ANALYZER_FACTORY(Factory)