Remove compiler warnings.
[platform/upstream/lightmediascanner.git] / src / plugins / mp4 / mp4.c
1 /**
2  * Copyright (C) 2008 by INdT
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * mp4 file parser.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 static const char PV[] = PACKAGE_VERSION; /* mp4.h screws PACKAGE_VERSION */
32
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_db.h>
35 #include <mp4.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 enum StreamTypes {
40     STREAM_TYPE_UNKNOWN = 0,
41     STREAM_TYPE_AUDIO,
42     STREAM_TYPE_VIDEO
43 };
44
45 struct mp4_info {
46     struct lms_string_size title;
47     struct lms_string_size artist;
48     struct lms_string_size album;
49     struct lms_string_size genre;
50     u_int16_t trackno;
51 };
52
53 struct plugin {
54     struct lms_plugin plugin;
55     lms_db_audio_t *audio_db;
56     lms_db_video_t *video_db;
57 };
58
59 static const char _name[] = "mp4";
60 static const struct lms_string_size _exts[] = {
61     LMS_STATIC_STRING_SIZE(".mp4"),
62     LMS_STATIC_STRING_SIZE(".m4a"),
63     LMS_STATIC_STRING_SIZE(".m4v"),
64     LMS_STATIC_STRING_SIZE(".mov"),
65     LMS_STATIC_STRING_SIZE(".qt"),
66     LMS_STATIC_STRING_SIZE(".3gp")
67 };
68 static const char *_cats[] = {
69     "multimedia",
70     "audio",
71     "video",
72     NULL
73 };
74 static const char *_authors[] = {
75     "Andre Moreira Magalhaes",
76     NULL
77 };
78
79 static void *
80 _match(struct plugin *p, const char *path, int len, int base)
81 {
82     long i;
83
84     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
85     if (i < 0)
86       return NULL;
87     else
88       return (void*)(i + 1);
89 }
90
91 static int
92 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
93 {
94     struct mp4_info info = {{0}, {0}, {0}, {0}};
95     struct lms_audio_info audio_info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
96     struct lms_video_info video_info = {0, {0}, {0}};
97     int r, stream_type = STREAM_TYPE_AUDIO;
98     MP4FileHandle mp4_fh;
99     u_int32_t num_tracks;
100
101     mp4_fh = MP4Read(finfo->path, 0);
102     if (mp4_fh == MP4_INVALID_FILE_HANDLE) {
103         fprintf(stderr, "ERROR: cannot read mp4 file %s\n", finfo->path);
104         return -1;
105     }
106
107     /* check if the file contains a video track */
108     num_tracks = MP4GetNumberOfTracks(mp4_fh, MP4_VIDEO_TRACK_TYPE, 0);
109     if (num_tracks > 0)
110         stream_type = STREAM_TYPE_VIDEO;
111
112     MP4GetMetadataName(mp4_fh, &info.title.str);
113     if (info.title.str)
114         info.title.len = strlen(info.title.str);
115     MP4GetMetadataArtist(mp4_fh, &info.artist.str);
116     if (info.artist.str)
117         info.artist.len = strlen(info.artist.str);
118
119     if (stream_type == STREAM_TYPE_AUDIO) {
120         u_int16_t total_tracks;
121
122         MP4GetMetadataAlbum(mp4_fh, &info.album.str);
123         if (info.album.str)
124             info.album.len = strlen(info.album.str);
125         MP4GetMetadataGenre(mp4_fh, &info.genre.str);
126         if (info.genre.str)
127             info.genre.len = strlen(info.genre.str);
128
129         MP4GetMetadataTrack(mp4_fh, &info.trackno, &total_tracks);
130     }
131
132     lms_string_size_strip_and_free(&info.title);
133     lms_string_size_strip_and_free(&info.artist);
134     lms_string_size_strip_and_free(&info.album);
135     lms_string_size_strip_and_free(&info.genre);
136
137     if (!info.title.str) {
138         long ext_idx;
139         ext_idx = ((long)match) - 1;
140         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
141         info.title.str = malloc((info.title.len + 1) * sizeof(char));
142         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
143         info.title.str[info.title.len] = '\0';
144     }
145     lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
146
147     if (info.artist.str)
148         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
149     if (info.album.str)
150         lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
151     if (info.genre.str)
152         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
153
154 #if 0
155     fprintf(stderr, "file %s info\n", finfo->path);
156     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
157     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
158     fprintf(stderr, "\talbum='%s'\n", info.album.str);
159     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
160 #endif
161
162     if (stream_type == STREAM_TYPE_AUDIO) {
163         audio_info.id = finfo->id;
164         audio_info.title = info.title;
165         audio_info.artist = info.artist;
166         audio_info.album = info.album;
167         audio_info.genre = info.genre;
168         audio_info.trackno = info.trackno;
169         r = lms_db_audio_add(plugin->audio_db, &audio_info);
170     }
171     else {
172         video_info.id = finfo->id;
173         video_info.title = info.title;
174         video_info.artist = info.artist;
175         r = lms_db_video_add(plugin->video_db, &video_info);
176     }
177
178     MP4Close(mp4_fh);
179
180     if (info.title.str)
181         free(info.title.str);
182     if (info.artist.str)
183         free(info.artist.str);
184     if (info.album.str)
185         free(info.album.str);
186     if (info.genre.str)
187         free(info.genre.str);
188
189     return r;
190 }
191
192 static int
193 _setup(struct plugin *plugin, struct lms_context *ctxt)
194 {
195     plugin->audio_db = lms_db_audio_new(ctxt->db);
196     if (!plugin->audio_db)
197         return -1;
198     plugin->video_db = lms_db_video_new(ctxt->db);
199     if (!plugin->video_db)
200         return -1;
201
202     return 0;
203 }
204
205 static int
206 _start(struct plugin *plugin, struct lms_context *ctxt)
207 {
208     int r;
209     r = lms_db_audio_start(plugin->audio_db);
210     r |= lms_db_video_start(plugin->video_db);
211     return r;
212 }
213
214 static int
215 _finish(struct plugin *plugin, struct lms_context *ctxt)
216 {
217     if (plugin->audio_db)
218         lms_db_audio_free(plugin->audio_db);
219     if (plugin->video_db)
220         lms_db_video_free(plugin->video_db);
221
222     return 0;
223 }
224
225 static int
226 _close(struct plugin *plugin)
227 {
228     free(plugin);
229     return 0;
230 }
231
232 API struct lms_plugin *
233 lms_plugin_open(void)
234 {
235     struct plugin *plugin;
236
237     plugin = (struct plugin *)malloc(sizeof(*plugin));
238     plugin->plugin.name = _name;
239     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
240     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
241     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
242     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
243     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
244     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
245
246     return (struct lms_plugin *)plugin;
247 }
248
249 API struct lms_plugin_info *
250 lms_plugin_info(void)
251 {
252     static struct lms_plugin_info info = {
253         _name,
254         _cats,
255         "MP4 files (MP4, M4A, MOV, QT, 3GP)",
256         PV,
257         _authors,
258         "http://lms.garage.maemo.org"
259     };
260
261     return &info;
262 }