export parsers and their informations.
[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(".mov"),
64     LMS_STATIC_STRING_SIZE(".qt"),
65     LMS_STATIC_STRING_SIZE(".3gp")
66 };
67 static const char *_cats[] = {
68     "multimedia",
69     "audio",
70     "video",
71     NULL
72 };
73 static const char *_authors[] = {
74     "Andre Moreira Magalhaes",
75     NULL
76 };
77
78 static void *
79 _match(struct plugin *p, const char *path, int len, int base)
80 {
81     int i;
82
83     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
84     if (i < 0)
85       return NULL;
86     else
87       return (void*)(i + 1);
88 }
89
90 static int
91 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
92 {
93     struct mp4_info info = {{0}, {0}, {0}, {0}};
94     struct lms_audio_info audio_info = {0, {0}, {0}, {0}, {0}, 0, 0, 0};
95     struct lms_video_info video_info = {0, {0}, {0}};
96     int r, stream_type = STREAM_TYPE_AUDIO;
97     MP4FileHandle mp4_fh;
98     u_int32_t num_tracks;
99
100     mp4_fh = MP4Read(finfo->path, 0);
101     if (mp4_fh == MP4_INVALID_FILE_HANDLE) {
102         fprintf(stderr, "ERROR: cannot read mp4 file %s\n", finfo->path);
103         return -1;
104     }
105
106     /* check if the file contains a video track */
107     num_tracks = MP4GetNumberOfTracks(mp4_fh, MP4_VIDEO_TRACK_TYPE, 0);
108     if (num_tracks > 0)
109         stream_type = STREAM_TYPE_VIDEO;
110
111     MP4GetMetadataName(mp4_fh, &info.title.str);
112     if (info.title.str)
113         info.title.len = strlen(info.title.str);
114     MP4GetMetadataArtist(mp4_fh, &info.artist.str);
115     if (info.artist.str)
116         info.artist.len = strlen(info.artist.str);
117
118     if (stream_type == STREAM_TYPE_AUDIO) {
119         u_int16_t total_tracks;
120
121         MP4GetMetadataAlbum(mp4_fh, &info.album.str);
122         if (info.album.str)
123             info.album.len = strlen(info.album.str);
124         MP4GetMetadataGenre(mp4_fh, &info.genre.str);
125         if (info.genre.str)
126             info.genre.len = strlen(info.genre.str);
127
128         MP4GetMetadataTrack(mp4_fh, &info.trackno, &total_tracks);
129     }
130
131     lms_string_size_strip_and_free(&info.title);
132     lms_string_size_strip_and_free(&info.artist);
133     lms_string_size_strip_and_free(&info.album);
134     lms_string_size_strip_and_free(&info.genre);
135
136     if (!info.title.str) {
137         int ext_idx;
138         ext_idx = ((int)match) - 1;
139         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
140         info.title.str = malloc((info.title.len + 1) * sizeof(char));
141         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
142         info.title.str[info.title.len] = '\0';
143     }
144     lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
145
146     if (info.artist.str)
147         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
148     if (info.album.str)
149         lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
150     if (info.genre.str)
151         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
152
153 #if 0
154     fprintf(stderr, "file %s info\n", finfo->path);
155     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
156     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
157     fprintf(stderr, "\talbum='%s'\n", info.album.str);
158     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
159 #endif
160
161     if (stream_type == STREAM_TYPE_AUDIO) {
162         audio_info.id = finfo->id;
163         audio_info.title = info.title;
164         audio_info.artist = info.artist;
165         audio_info.album = info.album;
166         audio_info.genre = info.genre;
167         audio_info.trackno = info.trackno;
168         r = lms_db_audio_add(plugin->audio_db, &audio_info);
169     }
170     else {
171         video_info.id = finfo->id;
172         video_info.title = info.title;
173         video_info.artist = info.artist;
174         r = lms_db_video_add(plugin->video_db, &video_info);
175     }
176
177     MP4Close(mp4_fh);
178
179     if (info.title.str)
180         free(info.title.str);
181     if (info.artist.str)
182         free(info.artist.str);
183     if (info.album.str)
184         free(info.album.str);
185     if (info.genre.str)
186         free(info.genre.str);
187
188     return r;
189 }
190
191 static int
192 _setup(struct plugin *plugin, struct lms_context *ctxt)
193 {
194     plugin->audio_db = lms_db_audio_new(ctxt->db);
195     if (!plugin->audio_db)
196         return -1;
197     plugin->video_db = lms_db_video_new(ctxt->db);
198     if (!plugin->video_db)
199         return -1;
200
201     return 0;
202 }
203
204 static int
205 _start(struct plugin *plugin, struct lms_context *ctxt)
206 {
207     int r;
208     r = lms_db_audio_start(plugin->audio_db);
209     r |= lms_db_video_start(plugin->video_db);
210     return r;
211 }
212
213 static int
214 _finish(struct plugin *plugin, struct lms_context *ctxt)
215 {
216     if (plugin->audio_db)
217         lms_db_audio_free(plugin->audio_db);
218     if (plugin->video_db)
219         lms_db_video_free(plugin->video_db);
220
221     return 0;
222 }
223
224 static int
225 _close(struct plugin *plugin)
226 {
227     free(plugin);
228     return 0;
229 }
230
231 API struct lms_plugin *
232 lms_plugin_open(void)
233 {
234     struct plugin *plugin;
235
236     plugin = (struct plugin *)malloc(sizeof(*plugin));
237     plugin->plugin.name = _name;
238     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
239     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
240     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
241     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
242     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
243     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
244
245     return (struct lms_plugin *)plugin;
246 }
247
248 API struct lms_plugin_info *
249 lms_plugin_info(void)
250 {
251     static struct lms_plugin_info info = {
252         _name,
253         _cats,
254         "MP4 files (MP4, M4A, MOV, QT, 3GP)",
255         PV,
256         _authors,
257         "http://lms.garage.maemo.org"
258     };
259
260     return &info;
261 }