Debian Codegraph @

gst/isomp4/atomsrecovery.c

raw symbols SCIP
1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2010 Thiago Santos <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 /*
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
22 * LICENSE).
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 * SOFTWARE.
41 */
42
43 /*
44 * This module contains functions for serializing partial information from
45 * a mux in progress (by qtmux elements). This enables reconstruction of the
46 * moov box if a crash happens and thus recovering the movie file.
47 *
48 * Usage:
49 * 1) pipeline: ...yourelements ! qtmux moov-recovery-file=path.mrf ! \
50 * filesink location=moovie.mov
51 *
52 * 2) CRASH!
53 *
54 * 3) gst-launch-1.0 qtmoovrecover recovery-input=path.mrf broken-input=moovie.mov \
55 fixed-output=recovered.mov
56 *
57 * 4) (Hopefully) enjoy recovered.mov.
58 *
59 * --- Recovery file layout ---
60 * 1) Version (a guint16)
61 * 2) Prefix atom (if present)
62 * 3) ftyp atom
63 * 4) MVHD atom (without timescale/duration set)
64 * 5) moovie timescale
65 * 6) number of traks
66 * 7) list of trak atoms (stbl data is ignored, except for the stsd atom)
67 * 8) Buffers metadata (metadata that is relevant to the container)
68 * Buffers metadata are stored in the order they are added to the mdat,
69 * each entre has a fixed size and is stored in BE. booleans are stored
70 * as a single byte where 0 means false, otherwise is true.
71 * Metadata:
72 * - guint32 track_id;
73 * - guint32 nsamples;
74 * - guint32 delta;
75 * - guint32 size;
76 * - guint64 chunk_offset;
77 * - gboolean sync;
78 * - gboolean do_pts;
79 * - guint64 pts_offset; (always present, ignored if do_pts is false)
80 *
81 * The mdat file might contain ftyp and then mdat, in case this is the faststart
82 * temporary file there is no ftyp and no mdat header, only the buffers data.
83 *
84 * Notes about recovery file layout: We still don't store tags nor EDTS data.
85 *
86 * IMPORTANT: this is still at a experimental state.
87 */
88
89 #include "atomsrecovery.h"
90
91 #define MAX_CHUNK_SIZE (1024 * 1024) /* 1MB */
92
93 #define ATOMS_RECOV_OUTPUT_WRITE_ERROR(err) \
94 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE, \
95 "Failed to write to output file: %s", g_strerror (errno))
96
97 static gboolean
98 atoms_recov_write_version (FILE * f)
99 {
100 guint8 data[2];
101 GST_WRITE_UINT16_BE (data, ATOMS_RECOV_FILE_VERSION);
102 return fwrite (data, 2, 1, f) == 1;
103 }
104
105 static gboolean
106 atoms_recov_write_ftyp_info (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix)
107 {
108 guint8 *data = NULL;
109 guint64 offset = 0;
110 guint64 size = 0;
111
112 if (prefix) {
113 GstMapInfo map;
114
115 if (!gst_buffer_map (prefix, &map, GST_MAP_READ)) {
116 return FALSE;
117 }
118 if (fwrite (map.data, 1, map.size, f) != map.size) {
119 gst_buffer_unmap (prefix, &map);
120 return FALSE;
121 }
122 gst_buffer_unmap (prefix, &map);
123 }
124 if (!atom_ftyp_copy_data (ftyp, &data, &size, &offset)) {
125 return FALSE;
126 }
127 if (fwrite (data, 1, offset, f) != offset) {
128 g_free (data);
129 return FALSE;
130 }
131 g_free (data);
132 return TRUE;
133 }
134
135 /*
136 * Writes important info on the 'moov' atom (non-trak related)
137 * to be able to recover the moov structure after a crash.
138 *
139 * Currently, it writes the MVHD atom.
140 */
141 static gboolean
142 atoms_recov_write_moov_info (FILE * f, AtomMOOV * moov)
143 {
144 guint8 *data;
145 guint64 size;
146 guint64 offset = 0;
147 guint64 atom_size = 0;
148 gint writen = 0;
149
150 /* likely enough */
151 size = 256;
152 data = g_malloc (size);
153 atom_size = atom_mvhd_copy_data (&moov->mvhd, &data, &size, &offset);
154 if (atom_size > 0)
155 writen = fwrite (data, 1, atom_size, f);
156 g_free (data);
157 return atom_size > 0 && writen == atom_size;
158 }
159
160 /*
161 * Writes the number of traks to the file.
162 * This simply writes a guint32 in BE.
163 */
164 static gboolean
165 atoms_recov_write_traks_number (FILE * f, guint32 traks)
166 {
167 guint8 data[4];
168 GST_WRITE_UINT32_BE (data, traks);
169 return fwrite (data, 4, 1, f) == 1;
170 }
171
172 /*
173 * Writes the moov's timescale to the file
174 * This simply writes a guint32 in BE.
175 */
176 static gboolean
177 atoms_recov_write_moov_timescale (FILE * f, guint32 timescale)
178 {
179 guint8 data[4];
180 GST_WRITE_UINT32_BE (data, timescale);
181 return fwrite (data, 4, 1, f) == 1;
182 }
183
184 /*
185 * Writes the trak atom to the file.
186 */
187 gboolean
188 atoms_recov_write_trak_info (FILE * f, AtomTRAK * trak)
189 {
190 guint8 *data;
191 guint64 size;
192 guint64 offset = 0;
193 guint64 atom_size = 0;
194 gint writen = 0;
195
196 /* buffer is realloced to a larger size if needed */
197 size = 4 * 1024;
198 data = g_malloc (size);
199 atom_size = atom_trak_copy_data (trak, &data, &size, &offset);
200 if (atom_size > 0)
201 writen = fwrite (data, atom_size, 1, f);
202 g_free (data);
203 return atom_size > 0 && writen == atom_size;
204 }
205
206 gboolean
207 atoms_recov_write_trak_samples (FILE * f, AtomTRAK * trak, guint32 nsamples,
208 guint32 delta, guint32 size, guint64 chunk_offset, gboolean sync,
209 gboolean do_pts, gint64 pts_offset)
210 {
211 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
212 /*
213 * We have to write a TrakBufferEntryInfo
214 */
215 GST_WRITE_UINT32_BE (data + 0, trak->tkhd.track_ID);
216 GST_WRITE_UINT32_BE (data + 4, nsamples);
217 GST_WRITE_UINT32_BE (data + 8, delta);
218 GST_WRITE_UINT32_BE (data + 12, size);
219 GST_WRITE_UINT64_BE (data + 16, chunk_offset);
220 if (sync)
221 GST_WRITE_UINT8 (data + 24, 1);
222 else
223 GST_WRITE_UINT8 (data + 24, 0);
224 if (do_pts) {
225 GST_WRITE_UINT8 (data + 25, 1);
226 GST_WRITE_UINT64_BE (data + 26, pts_offset);
227 } else {
228 GST_WRITE_UINT8 (data + 25, 0);
229 GST_WRITE_UINT64_BE (data + 26, 0);
230 }
231
232 return fwrite (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, f) ==
233 TRAK_BUFFER_ENTRY_INFO_SIZE;
234 }
235
236 gboolean
237 atoms_recov_write_headers (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix,
238 AtomMOOV * moov, guint32 timescale, guint32 traks_number)
239 {
240 if (!atoms_recov_write_version (f)) {
241 return FALSE;
242 }
243
244 if (!atoms_recov_write_ftyp_info (f, ftyp, prefix)) {
245 return FALSE;
246 }
247
248 if (!atoms_recov_write_moov_info (f, moov)) {
249 return FALSE;
250 }
251
252 if (!atoms_recov_write_moov_timescale (f, timescale)) {
253 return FALSE;
254 }
255
256 if (!atoms_recov_write_traks_number (f, traks_number)) {
257 return FALSE;
258 }
259
260 return TRUE;
261 }
262
263 static gboolean
264 read_atom_header (FILE * f, guint32 * fourcc, guint32 * size)
265 {
266 guint8 aux[8];
267
268 if (fread (aux, 1, 8, f) != 8)
269 return FALSE;
270 *size = GST_READ_UINT32_BE (aux);
271 *fourcc = GST_READ_UINT32_LE (aux + 4);
272 return TRUE;
273 }
274
275 static gboolean
276 moov_recov_file_parse_prefix (MoovRecovFile * moovrf)
277 {
278 guint32 fourcc;
279 guint32 size;
280 guint32 total_size = 0;
281 if (fseek (moovrf->file, 2, SEEK_SET) != 0)
282 return FALSE;
283 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
284 return FALSE;
285 }
286 if (size < 8)
287 return FALSE;
288
289 if (fourcc != FOURCC_ftyp) {
290 /* we might have a prefix here */
291 if (fseek (moovrf->file, size - 8, SEEK_CUR) != 0)
292 return FALSE;
293
294 total_size += size;
295
296 /* now read the ftyp */
297 if (!read_atom_header (moovrf->file, &fourcc, &size))
298 return FALSE;
299 if (size < 8)
300 return FALSE;
301 }
302
303 /* this has to be the ftyp */
304 if (fourcc != FOURCC_ftyp)
305 return FALSE;
306 total_size += size;
307 moovrf->prefix_size = total_size;
308 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
309 }
310
311 static gboolean
312 moov_recov_file_parse_mvhd (MoovRecovFile * moovrf)
313 {
314 guint32 fourcc;
315 guint32 size;
316 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
317 return FALSE;
318 }
319 /* check for sanity */
320 if (fourcc != FOURCC_mvhd)
321 return FALSE;
322 if (size < 8)
323 return FALSE;
324
325 moovrf->mvhd_size = size;
326 moovrf->mvhd_pos = ftell (moovrf->file) - 8;
327
328 guint8 version;
329 if (fread (&version, 1, 1, moovrf->file) != 1)
330 return FALSE;
331 if (version != 0) {
332 GST_WARNING ("Version %d mvhd not supported", version);
333 return FALSE;
334 }
335
336 if (size != 108)
337 return FALSE;
338
339 /* skip the remaining of the mvhd in the file */
340 return fseek (moovrf->file, size - 8 - 1, SEEK_CUR) == 0;
341 }
342
343 static gboolean
344 mdat_recov_file_parse_mdat_start (MdatRecovFile * mdatrf)
345 {
346 guint32 fourcc, size;
347
348 if (!read_atom_header (mdatrf->file, &fourcc, &size)) {
349 return FALSE;
350 }
351 if (size == 1) {
352 mdatrf->mdat_header_size = 16;
353 mdatrf->mdat_size = 16;
354 } else {
355 mdatrf->mdat_header_size = 8;
356 mdatrf->mdat_size = 8;
357 }
358 mdatrf->mdat_start = ftell (mdatrf->file) - 8;
359
360 return fourcc == FOURCC_mdat;
361 }
362
363 static gboolean
364 mdat_recov_file_find_mdat (FILE * file, GError ** err)
365 {
366 guint32 fourcc = 0, size = 0;
367 gboolean failure = FALSE;
368 while (fourcc != FOURCC_mdat && !failure) {
369 if (!read_atom_header (file, &fourcc, &size)) {
370 goto parse_error;
371 }
372 switch (fourcc) {
373 /* skip these atoms */
374 case FOURCC_ftyp:
375 case FOURCC_free:
376 case FOURCC_udta:
377 if (size < 8)
378 return FALSE;
379 if (fseek (file, size - 8, SEEK_CUR) != 0) {
380 goto file_seek_error;
381 }
382 break;
383 case FOURCC_mdat:
384 break;
385 default:
386 GST_ERROR ("Unexpected atom in headers %" GST_FOURCC_FORMAT,
387 GST_FOURCC_ARGS (fourcc));
388 failure = TRUE;
389 break;
390 }
391 }
392
393 if (!failure) {
394 /* Reverse to mdat start */
395 if (fseek (file, -8, SEEK_CUR) != 0)
396 goto file_seek_error;
397 }
398
399 return !failure;
400
401 parse_error:
402 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
403 "Failed to parse atom");
404 return FALSE;
405
406 file_seek_error:
407 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
408 "Failed to seek to start of the file");
409 return FALSE;
410
411 }
412
413 MdatRecovFile *
414 mdat_recov_file_create (FILE * file, gboolean datafile, GError ** err)
415 {
416 MdatRecovFile *mrf = g_new0 (MdatRecovFile, 1);
417
418 g_return_val_if_fail (file != NULL, NULL);
419
420 mrf->file = file;
421 mrf->rawfile = datafile;
422
423 /* get the file/data length */
424 if (fseek (file, 0, SEEK_END) != 0)
425 goto file_length_error;
426 /* still needs to deduce the mdat header and ftyp size */
427 mrf->data_size = ftell (file);
428 if (mrf->data_size == -1L)
429 goto file_length_error;
430
431 if (fseek (file, 0, SEEK_SET) != 0)
432 goto file_seek_error;
433
434 if (datafile) {
435 /* this file contains no atoms, only raw data to be placed on the mdat
436 * this happens when faststart mode is used */
437 mrf->mdat_start = 0;
438 mrf->mdat_header_size = 16;
439 mrf->mdat_size = 16;
440 return mrf;
441 }
442
443 if (!mdat_recov_file_find_mdat (file, err)) {
444 goto fail;
445 }
446
447 /* we don't parse this if we have a tmpdatafile */
448 if (!mdat_recov_file_parse_mdat_start (mrf)) {
449 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
450 "Error while parsing mdat atom");
451 goto fail;
452 }
453
454 return mrf;
455
456 file_seek_error:
457 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
458 "Failed to seek to start of the file");
459 goto fail;
460
461 file_length_error:
462 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
463 "Failed to determine file size");
464 goto fail;
465
466 fail:
467 mdat_recov_file_free (mrf);
468 return NULL;
469 }
470
471 void
472 mdat_recov_file_free (MdatRecovFile * mrf)
473 {
474 fclose (mrf->file);
475 g_free (mrf);
476 }
477
478 static gboolean
479 moov_recov_parse_num_traks (MoovRecovFile * moovrf)
480 {
481 guint8 traks[4];
482 if (fread (traks, 1, 4, moovrf->file) != 4)
483 return FALSE;
484 moovrf->num_traks = GST_READ_UINT32_BE (traks);
485 return TRUE;
486 }
487
488 static gboolean
489 moov_recov_parse_moov_timescale (MoovRecovFile * moovrf)
490 {
491 guint8 ts[4];
492 if (fread (ts, 1, 4, moovrf->file) != 4)
493 return FALSE;
494 moovrf->timescale = GST_READ_UINT32_BE (ts);
495 return TRUE;
496 }
497
498 static gboolean
499 skip_atom (MoovRecovFile * moovrf, guint32 expected_fourcc)
500 {
501 guint32 size;
502 guint32 fourcc;
503
504 if (!read_atom_header (moovrf->file, &fourcc, &size))
505 return FALSE;
506 if (fourcc != expected_fourcc)
507 return FALSE;
508 if (size < 8)
509 return FALSE;
510
511 return (fseek (moovrf->file, size - 8, SEEK_CUR) == 0);
512 }
513
514 static gboolean
515 moov_recov_parse_tkhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
516 {
517 guint32 size;
518 guint32 fourcc;
519 guint8 data[4];
520
521 /* make sure we are on a tkhd atom */
522 if (!read_atom_header (moovrf->file, &fourcc, &size))
523 return FALSE;
524 if (fourcc != FOURCC_tkhd)
525 return FALSE;
526 if (size < 8)
527 return FALSE;
528
529 trakrd->tkhd_file_offset = ftell (moovrf->file) - 8;
530
531 guint8 version;
532 if (fread (&version, 1, 1, moovrf->file) != 1)
533 return FALSE;
534 if (version != 0) {
535 GST_WARNING ("Version %d tkhd not supported", version);
536 return FALSE;
537 }
538
539 if (size != 92)
540 return FALSE;
541
542 /* move 12-1 bytes forward to the trak_id pos */
543 if (fseek (moovrf->file, 12 - 1, SEEK_CUR) != 0)
544 return FALSE;
545 if (fread (data, 1, 4, moovrf->file) != 4)
546 return FALSE;
547
548 /* advance the rest of tkhd */
549 if (fseek (moovrf->file, 68, SEEK_CUR) != 0)
550 return FALSE;
551
552 trakrd->trak_id = GST_READ_UINT32_BE (data);
553 return TRUE;
554 }
555
556 static gboolean
557 moov_recov_parse_stbl (MoovRecovFile * moovrf, TrakRecovData * trakrd)
558 {
559 guint32 size;
560 guint32 fourcc;
561 guint32 auxsize;
562
563 if (!read_atom_header (moovrf->file, &fourcc, &size))
564 return FALSE;
565 if (fourcc != FOURCC_stbl)
566 return FALSE;
567 if (size < 8)
568 return FALSE;
569
570 trakrd->stbl_file_offset = ftell (moovrf->file) - 8;
571 trakrd->stbl_size = size;
572
573 /* skip the stsd */
574 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
575 return FALSE;
576 if (fourcc != FOURCC_stsd)
577 return FALSE;
578 if (auxsize < 8)
579 return FALSE;
580 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR) != 0)
581 return FALSE;
582
583 trakrd->stsd_size = auxsize;
584 trakrd->post_stsd_offset = ftell (moovrf->file);
585
586 if (trakrd->stbl_size < trakrd->post_stsd_offset - trakrd->stbl_file_offset)
587 return FALSE;
588
589 /* as this is the last atom we parse, we don't skip forward */
590
591 return TRUE;
592 }
593
594 static gboolean
595 moov_recov_parse_minf (MoovRecovFile * moovrf, TrakRecovData * trakrd)
596 {
597 guint32 size;
598 guint32 fourcc;
599 guint32 auxsize;
600 guint64 offset;
601
602 if (!read_atom_header (moovrf->file, &fourcc, &size))
603 return FALSE;
604 if (fourcc != FOURCC_minf)
605 return FALSE;
606 if (size < 8)
607 return FALSE;
608
609 trakrd->minf_file_offset = ftell (moovrf->file) - 8;
610 trakrd->minf_size = size;
611
612 /* skip either of vmhd, smhd, hmhd that might follow */
613 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
614 return FALSE;
615 if (fourcc != FOURCC_vmhd && fourcc != FOURCC_smhd && fourcc != FOURCC_hmhd &&
616 fourcc != FOURCC_gmhd)
617 return FALSE;
618 if (auxsize < 8)
619 return FALSE;
620 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
621 return FALSE;
622
623 /* skip a possible hdlr and the following dinf */
624 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
625 return FALSE;
626 if (auxsize < 8)
627 return FALSE;
628 if (fourcc == FOURCC_hdlr) {
629 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
630 return FALSE;
631 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
632 return FALSE;
633 if (auxsize < 8)
634 return FALSE;
635 }
636 if (fourcc != FOURCC_dinf)
637 return FALSE;
638 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
639 return FALSE;
640
641 /* now we are ready to read the stbl */
642 if (!moov_recov_parse_stbl (moovrf, trakrd))
643 return FALSE;
644
645 offset = ftell (moovrf->file);
646 if (trakrd->minf_size < offset - trakrd->minf_file_offset)
647 return FALSE;
648
649 return TRUE;
650 }
651
652 static gboolean
653 moov_recov_parse_mdhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
654 {
655 guint32 size;
656 guint32 fourcc;
657 guint8 data[4];
658
659 /* make sure we are on a tkhd atom */
660 if (!read_atom_header (moovrf->file, &fourcc, &size))
661 return FALSE;
662 if (fourcc != FOURCC_mdhd)
663 return FALSE;
664 if (size < 8)
665 return FALSE;
666
667 trakrd->mdhd_file_offset = ftell (moovrf->file) - 8;
668
669 guint8 version;
670 if (fread (&version, 1, 1, moovrf->file) != 1)
671 return FALSE;
672 if (version != 0) {
673 GST_WARNING ("Version %d mdhd not supported", version);
674 return FALSE;
675 }
676
677 if (size != 32)
678 return FALSE;
679
680 /* get the timescale */
681 if (fseek (moovrf->file, 12 - 1, SEEK_CUR) != 0)
682 return FALSE;
683 if (fread (data, 1, 4, moovrf->file) != 4)
684 return FALSE;
685 trakrd->timescale = GST_READ_UINT32_BE (data);
686 if (fseek (moovrf->file, 8, SEEK_CUR) != 0)
687 return FALSE;
688 return TRUE;
689 }
690
691 static gboolean
692 moov_recov_parse_mdia (MoovRecovFile * moovrf, TrakRecovData * trakrd)
693 {
694 guint32 size;
695 guint32 fourcc;
696 guint64 offset;
697
698 /* make sure we are on a tkhd atom */
699 if (!read_atom_header (moovrf->file, &fourcc, &size))
700 return FALSE;
701 if (fourcc != FOURCC_mdia)
702 return FALSE;
703 if (size < 8)
704 return FALSE;
705
706 trakrd->mdia_file_offset = ftell (moovrf->file) - 8;
707 trakrd->mdia_size = size;
708
709 if (!moov_recov_parse_mdhd (moovrf, trakrd))
710 return FALSE;
711
712 if (!skip_atom (moovrf, FOURCC_hdlr))
713 return FALSE;
714 if (!moov_recov_parse_minf (moovrf, trakrd))
715 return FALSE;
716
717 offset = ftell (moovrf->file);
718 if (trakrd->mdia_size < offset - trakrd->mdia_file_offset)
719 return FALSE;
720
721 return TRUE;
722 }
723
724 static gboolean
725 moov_recov_parse_trak (MoovRecovFile * moovrf, TrakRecovData * trakrd)
726 {
727 guint64 offset;
728 guint32 size;
729 guint32 fourcc;
730
731 offset = ftell (moovrf->file);
732 if (offset == -1) {
733 return FALSE;
734 }
735
736 /* make sure we are on a trak atom */
737 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
738 return FALSE;
739 }
740 if (fourcc != FOURCC_trak) {
741 return FALSE;
742 }
743 if (size < 8)
744 return FALSE;
745 trakrd->trak_size = size;
746
747 /* now we should have a trak header 'tkhd' */
748 if (!moov_recov_parse_tkhd (moovrf, trakrd))
749 return FALSE;
750
751 /* FIXME add edts handling here and in qtmux, as this is only detected
752 * after buffers start flowing */
753
754 if (!moov_recov_parse_mdia (moovrf, trakrd))
755 return FALSE;
756
757 if (fseek (moovrf->file,
758 (long int) trakrd->mdia_file_offset + trakrd->mdia_size,
759 SEEK_SET) != 0)
760 return FALSE;
761
762 trakrd->extra_atoms_offset = ftell (moovrf->file);
763 if (trakrd->trak_size < trakrd->extra_atoms_offset - offset)
764 return FALSE;
765
766 trakrd->extra_atoms_size = size - (trakrd->extra_atoms_offset - offset);
767
768 trakrd->file_offset = offset;
769 /* position after the trak */
770 return fseek (moovrf->file, (long int) offset + size, SEEK_SET) == 0;
771 }
772
773 MoovRecovFile *
774 moov_recov_file_create (FILE * file, GError ** err)
775 {
776 gint i;
777 MoovRecovFile *moovrf = g_new0 (MoovRecovFile, 1);
778
779 g_return_val_if_fail (file != NULL, NULL);
780
781 moovrf->file = file;
782
783 /* look for ftyp and prefix at the start */
784 if (!moov_recov_file_parse_prefix (moovrf)) {
785 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
786 "Error while parsing prefix atoms");
787 goto fail;
788 }
789
790 /* parse the mvhd */
791 if (!moov_recov_file_parse_mvhd (moovrf)) {
792 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
793 "Error while parsing mvhd atom");
794 goto fail;
795 }
796
797 if (!moov_recov_parse_moov_timescale (moovrf)) {
798 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
799 "Error while parsing timescale");
800 goto fail;
801 }
802 if (!moov_recov_parse_num_traks (moovrf)) {
803 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
804 "Error while parsing parsing number of traks");
805 goto fail;
806 }
807
808 /* sanity check */
809 if (moovrf->num_traks > 1024) {
810 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
811 "Unsupported number of traks");
812 goto fail;
813 }
814
815 /* init the traks */
816 moovrf->traks_rd = g_new0 (TrakRecovData, moovrf->num_traks);
817 for (i = 0; i < moovrf->num_traks; i++) {
818 atom_stbl_init (&(moovrf->traks_rd[i].stbl));
819 }
820 for (i = 0; i < moovrf->num_traks; i++) {
821 if (!moov_recov_parse_trak (moovrf, &(moovrf->traks_rd[i]))) {
822 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
823 "Error while parsing trak atom");
824 goto fail;
825 }
826 }
827
828 return moovrf;
829
830 fail:
831 moov_recov_file_free (moovrf);
832 return NULL;
833 }
834
835 void
836 moov_recov_file_free (MoovRecovFile * moovrf)
837 {
838 gint i;
839 fclose (moovrf->file);
840 if (moovrf->traks_rd) {
841 for (i = 0; i < moovrf->num_traks; i++) {
842 atom_stbl_clear (&(moovrf->traks_rd[i].stbl));
843 }
844 g_free (moovrf->traks_rd);
845 }
846 g_free (moovrf);
847 }
848
849 static gboolean
850 moov_recov_parse_buffer_entry (MoovRecovFile * moovrf, TrakBufferEntryInfo * b)
851 {
852 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
853 gint read;
854
855 read = fread (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, moovrf->file);
856 if (read != TRAK_BUFFER_ENTRY_INFO_SIZE)
857 return FALSE;
858
859 b->track_id = GST_READ_UINT32_BE (data);
860 b->nsamples = GST_READ_UINT32_BE (data + 4);
861 b->delta = GST_READ_UINT32_BE (data + 8);
862 b->size = GST_READ_UINT32_BE (data + 12);
863 b->chunk_offset = GST_READ_UINT64_BE (data + 16);
864 b->sync = data[24] != 0;
865 b->do_pts = data[25] != 0;
866 b->pts_offset = GST_READ_UINT64_BE (data + 26);
867 return TRUE;
868 }
869
870 static gboolean
871 mdat_recov_add_sample (MdatRecovFile * mdatrf, guint32 size)
872 {
873 /* test if this data exists */
874 if (mdatrf->mdat_size - mdatrf->mdat_header_size + size > mdatrf->data_size)
875 return FALSE;
876
877 mdatrf->mdat_size += size;
878 return TRUE;
879 }
880
881 static TrakRecovData *
882 moov_recov_get_trak (MoovRecovFile * moovrf, guint32 id)
883 {
884 gint i;
885 for (i = 0; i < moovrf->num_traks; i++) {
886 if (moovrf->traks_rd[i].trak_id == id)
887 return &(moovrf->traks_rd[i]);
888 }
889 return NULL;
890 }
891
892 static void
893 trak_recov_data_add_sample (TrakRecovData * trak, TrakBufferEntryInfo * b)
894 {
895 trak->duration += b->nsamples * b->delta;
896 atom_stbl_add_samples (&trak->stbl, b->nsamples, b->delta, b->size,
897 b->chunk_offset, b->sync, b->pts_offset);
898 }
899
900 /*
901 * Parses the buffer entries in the MoovRecovFile and matches the inputs
902 * with the data in the MdatRecovFile. Whenever a buffer entry of that
903 * represents 'x' bytes of data, the same amount of data is 'validated' in
904 * the MdatRecovFile and will be inluded in the generated moovie file.
905 */
906 gboolean
907 moov_recov_parse_buffers (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
908 GError ** err)
909 {
910 TrakBufferEntryInfo entry;
911 TrakRecovData *trak;
912
913 /* we assume both moovrf and mdatrf are at the starting points of their
914 * data reading */
915 while (moov_recov_parse_buffer_entry (moovrf, &entry)) {
916 /* be sure we still have this data in mdat */
917 trak = moov_recov_get_trak (moovrf, entry.track_id);
918 if (trak == NULL) {
919 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
920 "Invalid trak id found in buffer entry");
921 return FALSE;
922 }
923 if (!mdat_recov_add_sample (mdatrf, entry.size))
924 break;
925 trak_recov_data_add_sample (trak, &entry);
926 }
927 return TRUE;
928 }
929
930 static guint32
931 trak_recov_data_get_trak_atom_size (TrakRecovData * trak)
932 {
933 AtomSTBL *stbl = &trak->stbl;
934 guint64 offset;
935
936 /* write out our stbl child atoms */
937 offset = 0;
938
939 if (!atom_stts_copy_data (&stbl->stts, NULL, NULL, &offset)) {
940 goto fail;
941 }
942 if (atom_array_get_len (&stbl->stss.entries) > 0) {
943 if (!atom_stss_copy_data (&stbl->stss, NULL, NULL, &offset)) {
944 goto fail;
945 }
946 }
947 if (!atom_stsc_copy_data (&stbl->stsc, NULL, NULL, &offset)) {
948 goto fail;
949 }
950 if (!atom_stsz_copy_data (&stbl->stsz, NULL, NULL, &offset)) {
951 goto fail;
952 }
953 if (stbl->ctts) {
954 if (!atom_ctts_copy_data (stbl->ctts, NULL, NULL, &offset)) {
955 goto fail;
956 }
957 }
958 if (!atom_stco64_copy_data (&stbl->stco64, NULL, NULL, &offset)) {
959 goto fail;
960 }
961
962 return trak->trak_size + ((trak->stsd_size + offset + 8) - trak->stbl_size);
963
964 fail:
965 return 0;
966 }
967
968 static guint8 *
969 moov_recov_get_stbl_children_data (MoovRecovFile * moovrf, TrakRecovData * trak,
970 guint64 * p_size)
971 {
972 AtomSTBL *stbl = &trak->stbl;
973 guint8 *buffer;
974 guint64 size;
975 guint64 offset;
976
977 /* write out our stbl child atoms
978 *
979 * Use 1MB as a starting size, *_copy_data functions
980 * will grow the buffer if needed.
981 */
982 size = 1024 * 1024;
983 buffer = g_malloc0 (size);
984 offset = 0;
985
986 if (!atom_stts_copy_data (&stbl->stts, &buffer, &size, &offset)) {
987 goto fail;
988 }
989 if (atom_array_get_len (&stbl->stss.entries) > 0) {
990 if (!atom_stss_copy_data (&stbl->stss, &buffer, &size, &offset)) {
991 goto fail;
992 }
993 }
994 if (!atom_stsc_copy_data (&stbl->stsc, &buffer, &size, &offset)) {
995 goto fail;
996 }
997 if (!atom_stsz_copy_data (&stbl->stsz, &buffer, &size, &offset)) {
998 goto fail;
999 }
1000 if (stbl->ctts) {
1001 if (!atom_ctts_copy_data (stbl->ctts, &buffer, &size, &offset)) {
1002 goto fail;
1003 }
1004 }
1005 if (!atom_stco64_copy_data (&stbl->stco64, &buffer, &size, &offset)) {
1006 goto fail;
1007 }
1008 *p_size = offset;
1009 return buffer;
1010
1011 fail:
1012 g_free (buffer);
1013 return NULL;
1014 }
1015
1016 static gboolean
1017 copy_data_from_file_to_file (FILE * from, guint position, guint size, FILE * to,
1018 GError ** err)
1019 {
1020 guint8 *data = NULL;
1021
1022 if (fseek (from, position, SEEK_SET) != 0)
1023 goto fail;
1024 data = g_malloc (size);
1025 if (fread (data, 1, size, from) != size) {
1026 goto fail;
1027 }
1028 if (fwrite (data, 1, size, to) != size) {
1029 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1030 goto fail;
1031 }
1032
1033 g_free (data);
1034 return TRUE;
1035
1036 fail:
1037 g_free (data);
1038 return FALSE;
1039 }
1040
1041 gboolean
1042 moov_recov_write_file (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
1043 FILE * outf, GError ** err, GError ** warn)
1044 {
1045 guint8 auxdata[16];
1046 guint8 *data = NULL;
1047 guint8 *prefix_data = NULL;
1048 guint8 *mvhd_data = NULL;
1049 guint8 *trak_data = NULL;
1050 guint32 moov_size = 0;
1051 gint i;
1052 guint64 stbl_children_size = 0;
1053 guint8 *stbl_children = NULL;
1054 guint32 longest_duration = 0;
1055 guint16 version;
1056 guint remaining;
1057
1058 /* check the version */
1059 if (fseek (moovrf->file, 0, SEEK_SET) != 0) {
1060 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1061 "Failed to seek to the start of the moov recovery file");
1062 goto fail;
1063 }
1064 if (fread (auxdata, 1, 2, moovrf->file) != 2) {
1065 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1066 "Failed to read version from file");
1067 }
1068
1069 version = GST_READ_UINT16_BE (auxdata);
1070 if (version != ATOMS_RECOV_FILE_VERSION) {
1071 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_VERSION,
1072 "Input file version (%u) is not supported in this version (%u)",
1073 version, ATOMS_RECOV_FILE_VERSION);
1074 return FALSE;
1075 }
1076
1077 /* write the ftyp */
1078 prefix_data = g_malloc (moovrf->prefix_size);
1079 if (fread (prefix_data, 1, moovrf->prefix_size,
1080 moovrf->file) != moovrf->prefix_size) {
1081 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1082 "Failed to read the ftyp atom from file");
1083 goto fail;
1084 }
1085 if (fwrite (prefix_data, 1, moovrf->prefix_size, outf) != moovrf->prefix_size) {
1086 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1087 goto fail;
1088 }
1089 g_free (prefix_data);
1090 prefix_data = NULL;
1091
1092 /* need to calculate the moov size beforehand to add the offset to
1093 * chunk offset entries */
1094 moov_size += moovrf->mvhd_size + 8; /* mvhd + moov size + fourcc */
1095 for (i = 0; i < moovrf->num_traks; i++) {
1096 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1097 guint32 duration; /* in moov's timescale */
1098 guint32 trak_size;
1099
1100 /* convert trak duration to moov's duration */
1101 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
1102 trak->timescale);
1103
1104 if (duration > longest_duration)
1105 longest_duration = duration;
1106 trak_size = trak_recov_data_get_trak_atom_size (trak);
1107 if (trak_size == 0) {
1108 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_GENERIC,
1109 "Failed to estimate trak atom size");
1110 goto fail;
1111 }
1112 moov_size += trak_size;
1113 }
1114
1115 /* add chunks offsets */
1116 for (i = 0; i < moovrf->num_traks; i++) {
1117 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1118 /* 8 or 16 for the mdat header */
1119 gint64 offset = moov_size + ftell (outf) + mdatrf->mdat_header_size;
1120 atom_stco64_chunks_set_offset (&trak->stbl.stco64, offset);
1121 }
1122
1123 /* write the moov */
1124 GST_WRITE_UINT32_BE (auxdata, moov_size);
1125 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_moov);
1126 if (fwrite (auxdata, 1, 8, outf) != 8) {
1127 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1128 goto fail;
1129 }
1130
1131 /* write the mvhd */
1132 mvhd_data = g_malloc (moovrf->mvhd_size);
1133 if (fseek (moovrf->file, moovrf->mvhd_pos, SEEK_SET) != 0)
1134 goto fail;
1135 if (fread (mvhd_data, 1, moovrf->mvhd_size,
1136 moovrf->file) != moovrf->mvhd_size)
1137 goto fail;
1138 GST_WRITE_UINT32_BE (mvhd_data + 20, moovrf->timescale);
1139 GST_WRITE_UINT32_BE (mvhd_data + 24, longest_duration);
1140 if (fwrite (mvhd_data, 1, moovrf->mvhd_size, outf) != moovrf->mvhd_size) {
1141 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1142 goto fail;
1143 }
1144 g_free (mvhd_data);
1145 mvhd_data = NULL;
1146
1147 /* write the traks, this is the tough part because we need to update:
1148 * - stbl atom
1149 * - sizes of atoms from stbl to trak
1150 * - trak duration
1151 */
1152 for (i = 0; i < moovrf->num_traks; i++) {
1153 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1154 guint trak_data_size;
1155 guint32 stbl_new_size;
1156 guint32 minf_new_size;
1157 guint32 mdia_new_size;
1158 guint32 trak_new_size;
1159 guint32 size_diff;
1160 guint32 duration; /* in moov's timescale */
1161
1162 /* convert trak duration to moov's duration */
1163 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
1164 trak->timescale);
1165
1166 stbl_children = moov_recov_get_stbl_children_data (moovrf, trak,
1167 &stbl_children_size);
1168 if (stbl_children == NULL)
1169 goto fail;
1170
1171 /* calc the new size of the atoms from stbl to trak in the atoms tree */
1172 stbl_new_size = trak->stsd_size + stbl_children_size + 8;
1173 size_diff = stbl_new_size - trak->stbl_size;
1174 minf_new_size = trak->minf_size + size_diff;
1175 mdia_new_size = trak->mdia_size + size_diff;
1176 trak_new_size = trak->trak_size + size_diff;
1177
1178 if (fseek (moovrf->file, trak->file_offset, SEEK_SET) != 0)
1179 goto fail;
1180 trak_data_size = trak->post_stsd_offset - trak->file_offset;
1181 trak_data = g_malloc (trak_data_size);
1182 if (fread (trak_data, 1, trak_data_size, moovrf->file) != trak_data_size) {
1183 goto fail;
1184 }
1185 /* update the size values in those read atoms before writing */
1186 GST_WRITE_UINT32_BE (trak_data, trak_new_size);
1187 GST_WRITE_UINT32_BE (trak_data + (trak->mdia_file_offset -
1188 trak->file_offset), mdia_new_size);
1189 GST_WRITE_UINT32_BE (trak_data + (trak->minf_file_offset -
1190 trak->file_offset), minf_new_size);
1191 GST_WRITE_UINT32_BE (trak_data + (trak->stbl_file_offset -
1192 trak->file_offset), stbl_new_size);
1193
1194 /* update duration values in tkhd and mdhd */
1195 GST_WRITE_UINT32_BE (trak_data + (trak->tkhd_file_offset -
1196 trak->file_offset) + 28, duration);
1197 GST_WRITE_UINT32_BE (trak_data + (trak->mdhd_file_offset -
1198 trak->file_offset) + 24, trak->duration);
1199
1200 if (fwrite (trak_data, 1, trak_data_size, outf) != trak_data_size) {
1201 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1202 goto fail;
1203 }
1204 if (fwrite (stbl_children, 1, stbl_children_size, outf) !=
1205 stbl_children_size) {
1206 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1207 goto fail;
1208 }
1209
1210 g_free (trak_data);
1211 trak_data = NULL;
1212 g_free (stbl_children);
1213 stbl_children = NULL;
1214
1215 /* Copy the extra atoms after 'minf' */
1216 if (!copy_data_from_file_to_file (moovrf->file, trak->extra_atoms_offset,
1217 trak->extra_atoms_size, outf, err))
1218 goto fail;
1219 }
1220
1221 /* write the mdat */
1222 /* write the header first */
1223 if (mdatrf->mdat_header_size == 16) {
1224 GST_WRITE_UINT32_BE (auxdata, 1);
1225 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_mdat);
1226 GST_WRITE_UINT64_BE (auxdata + 8, mdatrf->mdat_size);
1227 } else if (mdatrf->mdat_header_size == 8) {
1228 GST_WRITE_UINT32_BE (auxdata, mdatrf->mdat_size);
1229 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_mdat);
1230 } else {
1231 GST_ERROR ("Unexpected atom size: %u", mdatrf->mdat_header_size);
1232 g_assert_not_reached ();
1233 goto fail;
1234 }
1235
1236 if (fwrite (auxdata, 1, mdatrf->mdat_header_size,
1237 outf) != mdatrf->mdat_header_size) {
1238 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1239 goto fail;
1240 }
1241
1242 /* now read the mdat data and output to the file */
1243 if (fseek (mdatrf->file, mdatrf->mdat_start +
1244 (mdatrf->rawfile ? 0 : mdatrf->mdat_header_size), SEEK_SET) != 0)
1245 goto fail;
1246
1247 remaining = mdatrf->mdat_size - mdatrf->mdat_header_size;
1248 data = g_malloc (MAX_CHUNK_SIZE);
1249 while (!feof (mdatrf->file) && remaining > 0) {
1250 gint read, write, readsize;
1251
1252 readsize = MIN (MAX_CHUNK_SIZE, remaining);
1253
1254 read = fread (data, 1, readsize, mdatrf->file);
1255 write = fwrite (data, 1, read, outf);
1256 remaining -= read;
1257
1258 if (write != read) {
1259 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1260 "Failed to copy data to output file: %s", g_strerror (errno));
1261 goto fail;
1262 }
1263 }
1264 g_free (data);
1265
1266 if (remaining) {
1267 g_set_error (warn, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1268 "Samples in recovery file were not present on headers."
1269 " Bytes lost: %u", remaining);
1270 } else if (!feof (mdatrf->file)) {
1271 g_set_error (warn, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1272 "Samples in headers were not found in data file.");
1273 GST_FIXME ("Rewrite mdat size if we reach this to make the file"
1274 " fully correct");
1275 }
1276
1277 return TRUE;
1278
1279 fail:
1280 g_free (stbl_children);
1281 g_free (mvhd_data);
1282 g_free (prefix_data);
1283 g_free (trak_data);
1284 g_free (data);
1285 return FALSE;
1286 }