1 /*
2 * Copyright (c) 2014 Martin Storsjo
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23
28
30 {
31 fprintf(stderr, "%s -out foo.mpd file1\n", argv0);
33 }
34
46 };
47
53 };
54
56 {
63 }
64 break;
66 snprintf(str, size,
"mp4a.40");
// 0x40 is the mp4 object type for AAC
69 if (aot == 31)
72 }
73 break;
74 }
75 }
76
78 const char *file)
79 {
80 int err = 0;
82 int i;
83
85 goto fail;
86
90
93 if (size < 8)
94 break;
95 if (tag ==
MKBETAG(
's',
'i',
'd',
'x')) {
96 for (i = start_index; i < tracks->
nb_tracks; i++) {
103 }
104 }
105 }
106 if (
avio_seek(f, pos + size, SEEK_SET) != pos + size)
107 break;
108 }
109
110 fail:
111 if (f)
113 return err;
114 }
115
117 {
119 int err = 0, i, orig_tracks = tracks->
nb_tracks;
120 char errbuf[50], *ptr;
122
124 if (err < 0) {
126 fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
127 return 1;
128 }
129
131 if (err < 0) {
133 fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
134 goto fail;
135 }
136
138 fprintf(stderr, "No streams found in %s\n", file);
139 goto fail;
140 }
143
147
149 fprintf(stderr, "Skipping track %d in %s as it has zero bitrate\n",
151 continue;
152 }
153
155 if (!track) {
157 goto fail;
158 }
161 if (!temp) {
164 goto fail;
165 }
168
170 if ((ptr = strrchr(file, '/')))
171 track->
name = ptr + 1;
172
179
181 fprintf(stderr,
182 "Track %d in %s is neither video nor audio, skipping\n",
185 continue;
186 }
187
191
195 }
199 }
201
203 }
204
206
207 err =
find_sidx(tracks, orig_tracks, file);
208
209 fail:
210 if (ctx)
212 return err;
213 }
214
216 {
219 int minutes = seconds / 60;
220 int hours = minutes / 60;
222 seconds %= 60;
223 minutes %= 60;
224 fprintf(out, "PT");
225 if (hours)
226 fprintf(out, "%dH", hours);
227 if (hours || minutes)
228 fprintf(out, "%dM", minutes);
229 fprintf(out, "%d.%0*dS", seconds, decimals, fractions);
230 }
231
233 {
236 struct Track **adaptation_sets_buf[2] = {
NULL };
237 struct Track ***adaptation_sets;
238 int nb_tracks_buf[2] = { 0 };
239 int *nb_tracks;
240 int set, nb_sets;
241
243 adaptation_sets = adaptation_sets_buf;
244 nb_tracks = nb_tracks_buf;
245 nb_sets = 2;
246 for (i = 0; i < 2; i++) {
248 if (!adaptation_sets[i]) {
250 goto err;
251 }
252 }
253 for (i = 0; i < tracks->
nb_tracks; i++) {
254 int set_index = -1;
256 set_index = 0;
258 set_index = 1;
259 else
260 continue;
261 adaptation_sets[set_index][nb_tracks[set_index]++] = tracks->
tracks[i];
262 }
263 } else {
264 adaptation_sets = &tracks->
tracks;
266 nb_sets = 1;
267 }
268
269 out = fopen(filename, "w");
270 if (!out) {
272 perror(filename);
274 }
275 fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
276 fprintf(out, "<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
277 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
278 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
279 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
280 "\tprofiles=\"urn:mpeg:dash:profile:isoff-on-demand:2011\"\n"
281 "\ttype=\"static\"\n");
282 fprintf(out, "\tmediaPresentationDuration=\"");
284 fprintf(out, "\"\n");
285 fprintf(out, "\tminBufferTime=\"PT5S\">\n");
286
287 fprintf(out, "\t<Period start=\"PT0.0S\">\n");
288
289 for (set = 0; set < nb_sets; set++) {
290 if (nb_tracks[set] == 0)
291 continue;
292 fprintf(out, "\t\t<AdaptationSet segmentAlignment=\"true\">\n");
293 if (nb_sets == 1) {
294 for (i = 0; i < nb_tracks[set]; i++) {
295 struct Track *track = adaptation_sets[
set][i];
296 if (strcmp(track->
name, adaptation_sets[
set][0]->
name))
297 break;
298 fprintf(out,
"\t\t\t<ContentComponent id=\"%d\" contentType=\"%s\" />\n", track->
track_id, track->
is_audio ?
"audio" :
"video");
299 }
300 }
301
302 for (i = 0; i < nb_tracks[set]; ) {
303 struct Track *first_track = adaptation_sets[
set][i];
305 fprintf(out, "\t\t\t<Representation id=\"%d\" codecs=\"", i);
306 for (j = i; j < nb_tracks[set]; j++) {
307 struct Track *track = adaptation_sets[
set][j];
308 if (strcmp(track->
name, first_track->
name))
309 break;
313 }
315 width = track->
width;
317 }
319 if (j > i)
320 fprintf(out, ",");
322 }
323 fprintf(out, "\" mimeType=\"%s/mp4\" bandwidth=\"%d\"",
324 width ?
"video" :
"audio",
bitrate);
325 if (width > 0 &&
height > 0)
326 fprintf(out,
" width=\"%d\" height=\"%d\"", width,
height);
328 fprintf(out,
" audioSamplingRate=\"%d\"",
sample_rate);
329 fprintf(out, ">\n");
331 fprintf(out,
"\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n",
channels);
332 fprintf(out,
"\t\t\t\t<BaseURL>%s</BaseURL>\n", first_track->
name);
333 fprintf(out,
"\t\t\t\t<SegmentBase indexRange=\"%"PRId64
"-%"PRId64
"\" />\n", first_track->
sidx_start, first_track->
sidx_start + first_track->
sidx_length - 1);
334 fprintf(out, "\t\t\t</Representation>\n");
335 i = j;
336 }
337 fprintf(out, "\t\t</AdaptationSet>\n");
338 }
339 fprintf(out, "\t</Period>\n");
340 fprintf(out, "</MPD>\n");
341
342 fclose(out);
343 err:
344 for (i = 0; i < 2; i++)
345 av_free(adaptation_sets_buf[i]);
347 }
348
350 {
351 int i;
352 for (i = 0; i < tracks->
nb_tracks; i++) {
354 }
357 }
358
359 int main(
int argc,
char **argv)
360 {
362 struct Tracks tracks = { 0 };
363 int i;
364
366
367 for (i = 1; i < argc; i++) {
368 if (!strcmp(argv[i], "-out")) {
369 out = argv[i + 1];
370 i++;
371 } else if (argv[i][0] == '-') {
372 return usage(argv[0], 1);
373 } else {
375 return 1;
376 }
377 }
379 return usage(argv[0], 1);
380
382
384
385 return 0;
386 }