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