aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/riff_extractor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/riff_extractor.c')
-rw-r--r--src/plugins/riff_extractor.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/plugins/riff_extractor.c b/src/plugins/riff_extractor.c
new file mode 100644
index 0000000..b9cb5b3
--- /dev/null
+++ b/src/plugins/riff_extractor.c
@@ -0,0 +1,157 @@
1/*
2 This file is part of libextractor.
3 (C) 2004, 2009, 2012 Vidyut Samanta and Christian Grothoff
4
5 libextractor is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 libextractor is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libextractor; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20 This code was based on AVInfo 1.0 alpha 11
21 (c) George Shuklin, gs]AT[shounen.ru, 2002-2004
22 http://shounen.ru/soft/avinfo/
23
24 and bitcollider 0.6.0
25 (PD) 2004 The Bitzi Corporation
26 http://bitzi.com/
27 */
28/**
29 * @file plugins/riff_extractor.c
30 * @brief plugin to support RIFF files (ms-video)
31 * @author Christian Grothoff
32 */
33#include "platform.h"
34#include "extractor.h"
35#include <math.h>
36
37
38/**
39 * Read an uint32_t as a little-endian (least
40 * significant byte first) integer from 'data'
41 *
42 * @param data input data
43 * @return integer read
44 */
45static uint32_t
46fread_le (const char *data)
47{
48 unsigned int x;
49 uint32_t result = 0;
50
51 for (x = 0; x < 4; x++)
52 result |= ((unsigned char) data[x]) << (x * 8);
53 return result;
54}
55
56
57/**
58 * We implement our own rounding function, because the availability of
59 * C99's round(), nearbyint(), rint(), etc. seems to be spotty, whereas
60 * floor() is available in math.h on all C compilers.
61 *
62 * @param num value to round
63 * @return rounded-to-nearest value
64 */
65static double
66round_double (double num)
67{
68 return floor (num + 0.5);
69}
70
71
72/**
73 * Pass the given UTF-8 string to the 'proc' callback using
74 * the given type. Uses 'return' if 'proc' returns non-0.
75 *
76 * @param s 0-terminated UTF8 string value with the meta data
77 * @param t libextractor type for the meta data
78 */
79#define ADD(s,t) do { if (0 != ec->proc (ec->cls, "riff", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen (s) + 1)) return; } while (0)
80
81
82/**
83 * Main entry method for the 'video/x-msvideo' extraction plugin.
84 *
85 * @param ec extraction context provided to the plugin
86 */
87void
88EXTRACTOR_riff_extract_method (struct EXTRACTOR_ExtractContext *ec)
89{
90 ssize_t xsize;
91 void *data;
92 char *xdata;
93 uint32_t blockLen;
94 unsigned int fps;
95 unsigned int duration;
96 uint64_t pos;
97 uint32_t width;
98 uint32_t height;
99 char codec[5];
100 char format[256];
101
102 /* read header */
103 if (72 > (xsize = ec->read (ec->cls, &data, 72)))
104 return;
105 xdata = data;
106
107 /* check magic values */
108 if ( (0 != memcmp (&xdata[0],
109 "RIFF", 4)) ||
110 (0 != memcmp (&xdata[8], "AVI ", 4)) ||
111 (0 != memcmp (&xdata[12], "LIST", 4)) ||
112 (0 != memcmp (&xdata[20], "hdrlavih", 8)) )
113 return;
114
115 blockLen = fread_le (&xdata[28]);
116
117 /* begin of AVI header at 32 */
118 fps = (unsigned int) round_double ((double) 1.0e6 / fread_le (&xdata[32]));
119 duration = (unsigned int) round_double ((double) fread_le (&xdata[48])
120 * 1000 / fps);
121 width = fread_le (&xdata[64]);
122 height = fread_le (&xdata[68]);
123
124 /* pos: begin of video stream header */
125 pos = blockLen + 32;
126
127 if (pos !=
128 ec->seek (ec->cls, pos, SEEK_SET))
129 return;
130 if (32 > ec->read (ec->cls, &data, 32))
131 return;
132 xdata = data;
133
134 /* check magic */
135 if ( (0 != memcmp (xdata, "LIST", 4)) ||
136 (0 != memcmp (&xdata[8], "strlstrh", 8)) ||
137 (0 != memcmp (&xdata[20], "vids", 4)) )
138 return;
139
140 /* pos + 24: video stream header with codec */
141 memcpy (codec, &xdata[24], 4);
142 codec[4] = '\0';
143 snprintf (format,
144 sizeof (format),
145 _("codec: %s, %u fps, %u ms"),
146 codec, fps, duration);
147 ADD (format, EXTRACTOR_METATYPE_FORMAT);
148 snprintf (format,
149 sizeof (format),
150 "%ux%u",
151 (unsigned int) width,
152 (unsigned int) height);
153 ADD (format, EXTRACTOR_METATYPE_IMAGE_DIMENSIONS);
154 ADD ("video/x-msvideo", EXTRACTOR_METATYPE_MIMETYPE);
155}
156
157/* end of riff_extractor.c */