1 /* Quicktime muxer plugin for GStreamer
2 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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.
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.
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.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
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:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
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
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.
49 * 1) pipeline: ...yourelements ! qtmux moov-recovery-file=path.mrf ! \
50 * filesink location=moovie.mov
54 * 3) gst-launch-1.0 qtmoovrecover recovery-input=path.mrf broken-input=moovie.mov \
55 fixed-output=recovered.mov
57 * 4) (Hopefully) enjoy recovered.mov.
59 * --- Recovery file layout ---
60 * 1) Version (a guint16)
61 * 2) Prefix atom (if present)
63 * 4) MVHD atom (without timescale/duration set)
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.
76 * - guint64 chunk_offset;
79 * - guint64 pts_offset; (always present, ignored if do_pts is false)
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.
84 * Notes about recovery file layout: We still don't store tags nor EDTS data.
86 * IMPORTANT: this is still at a experimental state.
89 #include "atomsrecovery.h"
91 #define ATOMS_RECOV_OUTPUT_WRITE_ERROR(err) \
92 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE, \
93 "Failed to write to output file: %s", g_strerror (errno))
96 atoms_recov_write_version (FILE * f)
99 GST_WRITE_UINT16_BE (data, ATOMS_RECOV_FILE_VERSION);
100 return fwrite (data, 2, 1, f) == 1;
104 atoms_recov_write_ftyp_info (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix)
113 gst_buffer_map (prefix, &map, GST_MAP_READ);
114 if (fwrite (map.data, 1, map.size, f) != map.size) {
115 gst_buffer_unmap (prefix, &map);
118 gst_buffer_unmap (prefix, &map);
120 if (!atom_ftyp_copy_data (ftyp, &data, &size, &offset)) {
123 if (fwrite (data, 1, offset, f) != offset) {
132 * Writes important info on the 'moov' atom (non-trak related)
133 * to be able to recover the moov structure after a crash.
135 * Currently, it writes the MVHD atom.
138 atoms_recov_write_moov_info (FILE * f, AtomMOOV * moov)
143 guint64 atom_size = 0;
148 data = g_malloc (size);
149 atom_size = atom_mvhd_copy_data (&moov->mvhd, &data, &size, &offset);
151 writen = fwrite (data, 1, atom_size, f);
153 return atom_size > 0 && writen == atom_size;
157 * Writes the number of traks to the file.
158 * This simply writes a guint32 in BE.
161 atoms_recov_write_traks_number (FILE * f, guint32 traks)
164 GST_WRITE_UINT32_BE (data, traks);
165 return fwrite (data, 4, 1, f) == 1;
169 * Writes the moov's timescale to the file
170 * This simply writes a guint32 in BE.
173 atoms_recov_write_moov_timescale (FILE * f, guint32 timescale)
176 GST_WRITE_UINT32_BE (data, timescale);
177 return fwrite (data, 4, 1, f) == 1;
181 * Writes the trak atom to the file.
184 atoms_recov_write_trak_info (FILE * f, AtomTRAK * trak)
189 guint64 atom_size = 0;
192 /* buffer is realloced to a larger size if needed */
194 data = g_malloc (size);
195 atom_size = atom_trak_copy_data (trak, &data, &size, &offset);
197 writen = fwrite (data, atom_size, 1, f);
199 return atom_size > 0 && writen == atom_size;
203 atoms_recov_write_trak_samples (FILE * f, AtomTRAK * trak, guint32 nsamples,
204 guint32 delta, guint32 size, guint64 chunk_offset, gboolean sync,
205 gboolean do_pts, gint64 pts_offset)
207 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
209 * We have to write a TrakBufferEntryInfo
211 GST_WRITE_UINT32_BE (data + 0, trak->tkhd.track_ID);
212 GST_WRITE_UINT32_BE (data + 4, nsamples);
213 GST_WRITE_UINT32_BE (data + 8, delta);
214 GST_WRITE_UINT32_BE (data + 12, size);
215 GST_WRITE_UINT64_BE (data + 16, chunk_offset);
217 GST_WRITE_UINT8 (data + 24, 1);
219 GST_WRITE_UINT8 (data + 24, 0);
221 GST_WRITE_UINT8 (data + 25, 1);
222 GST_WRITE_UINT64_BE (data + 26, pts_offset);
224 GST_WRITE_UINT8 (data + 25, 0);
225 GST_WRITE_UINT64_BE (data + 26, 0);
228 return fwrite (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, f) ==
229 TRAK_BUFFER_ENTRY_INFO_SIZE;
233 atoms_recov_write_headers (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix,
234 AtomMOOV * moov, guint32 timescale, guint32 traks_number)
236 if (!atoms_recov_write_version (f)) {
240 if (!atoms_recov_write_ftyp_info (f, ftyp, prefix)) {
244 if (!atoms_recov_write_moov_info (f, moov)) {
248 if (!atoms_recov_write_moov_timescale (f, timescale)) {
252 if (!atoms_recov_write_traks_number (f, traks_number)) {
260 read_atom_header (FILE * f, guint32 * fourcc, guint32 * size)
264 if (fread (aux, 1, 8, f) != 8)
266 *size = GST_READ_UINT32_BE (aux);
267 *fourcc = GST_READ_UINT32_LE (aux + 4);
272 moov_recov_file_parse_prefix (MoovRecovFile * moovrf)
276 guint32 total_size = 0;
277 if (fseek (moovrf->file, 2, SEEK_SET) != 0)
279 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
283 if (fourcc != FOURCC_ftyp) {
284 /* we might have a prefix here */
285 if (fseek (moovrf->file, size - 8, SEEK_CUR) != 0)
290 /* now read the ftyp */
291 if (!read_atom_header (moovrf->file, &fourcc, &size))
295 /* this has to be the ftyp */
296 if (fourcc != FOURCC_ftyp)
299 moovrf->prefix_size = total_size;
300 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
304 moov_recov_file_parse_mvhd (MoovRecovFile * moovrf)
308 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
311 /* check for sanity */
312 if (fourcc != FOURCC_mvhd)
315 moovrf->mvhd_size = size;
316 moovrf->mvhd_pos = ftell (moovrf->file) - 8;
318 /* skip the remaining of the mvhd in the file */
319 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
323 mdat_recov_file_parse_mdat_start (MdatRecovFile * mdatrf)
325 guint32 fourcc, size;
327 if (!read_atom_header (mdatrf->file, &fourcc, &size)) {
331 mdatrf->mdat_header_size = 16;
332 mdatrf->mdat_size = 16;
334 mdatrf->mdat_header_size = 8;
335 mdatrf->mdat_size = 8;
337 mdatrf->mdat_start = ftell (mdatrf->file) - 8;
339 return fourcc == FOURCC_mdat;
343 mdat_recov_file_find_mdat (FILE * file, GError ** err)
345 guint32 fourcc = 0, size = 0;
346 gboolean failure = FALSE;
347 while (fourcc != FOURCC_mdat && !failure) {
348 if (!read_atom_header (file, &fourcc, &size)) {
352 /* skip these atoms */
356 if (fseek (file, size - 8, SEEK_CUR) != 0) {
357 goto file_seek_error;
363 GST_ERROR ("Unexpected atom in headers %" GST_FOURCC_FORMAT,
364 GST_FOURCC_ARGS (fourcc));
371 /* Reverse to mdat start */
372 if (fseek (file, -8, SEEK_CUR) != 0)
373 goto file_seek_error;
379 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
380 "Failed to parse atom");
384 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
385 "Failed to seek to start of the file");
391 mdat_recov_file_create (FILE * file, gboolean datafile, GError ** err)
393 MdatRecovFile *mrf = g_new0 (MdatRecovFile, 1);
395 g_return_val_if_fail (file != NULL, NULL);
398 mrf->rawfile = datafile;
400 /* get the file/data length */
401 if (fseek (file, 0, SEEK_END) != 0)
402 goto file_length_error;
403 /* still needs to deduce the mdat header and ftyp size */
404 mrf->data_size = ftell (file);
405 if (mrf->data_size == -1L)
406 goto file_length_error;
408 if (fseek (file, 0, SEEK_SET) != 0)
409 goto file_seek_error;
412 /* this file contains no atoms, only raw data to be placed on the mdat
413 * this happens when faststart mode is used */
415 mrf->mdat_header_size = 16;
420 if (!mdat_recov_file_find_mdat (file, err)) {
424 /* we don't parse this if we have a tmpdatafile */
425 if (!mdat_recov_file_parse_mdat_start (mrf)) {
426 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
427 "Error while parsing mdat atom");
434 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
435 "Failed to seek to start of the file");
439 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
440 "Failed to determine file size");
444 mdat_recov_file_free (mrf);
449 mdat_recov_file_free (MdatRecovFile * mrf)
456 moov_recov_parse_num_traks (MoovRecovFile * moovrf)
459 if (fread (traks, 1, 4, moovrf->file) != 4)
461 moovrf->num_traks = GST_READ_UINT32_BE (traks);
466 moov_recov_parse_moov_timescale (MoovRecovFile * moovrf)
469 if (fread (ts, 1, 4, moovrf->file) != 4)
471 moovrf->timescale = GST_READ_UINT32_BE (ts);
476 skip_atom (MoovRecovFile * moovrf, guint32 expected_fourcc)
481 if (!read_atom_header (moovrf->file, &fourcc, &size))
483 if (fourcc != expected_fourcc)
486 return (fseek (moovrf->file, size - 8, SEEK_CUR) == 0);
490 moov_recov_parse_tkhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
496 /* make sure we are on a tkhd atom */
497 if (!read_atom_header (moovrf->file, &fourcc, &size))
499 if (fourcc != FOURCC_tkhd)
502 trakrd->tkhd_file_offset = ftell (moovrf->file) - 8;
504 /* move 8 bytes forward to the trak_id pos */
505 if (fseek (moovrf->file, 12, SEEK_CUR) != 0)
507 if (fread (data, 1, 4, moovrf->file) != 4)
510 /* advance the rest of tkhd */
511 if (fseek (moovrf->file, 68, SEEK_CUR) != 0)
514 trakrd->trak_id = GST_READ_UINT32_BE (data);
519 moov_recov_parse_stbl (MoovRecovFile * moovrf, TrakRecovData * trakrd)
525 if (!read_atom_header (moovrf->file, &fourcc, &size))
527 if (fourcc != FOURCC_stbl)
530 trakrd->stbl_file_offset = ftell (moovrf->file) - 8;
531 trakrd->stbl_size = size;
534 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
536 if (fourcc != FOURCC_stsd)
538 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR) != 0)
541 trakrd->stsd_size = auxsize;
542 trakrd->post_stsd_offset = ftell (moovrf->file);
544 /* as this is the last atom we parse, we don't skip forward */
550 moov_recov_parse_minf (MoovRecovFile * moovrf, TrakRecovData * trakrd)
556 if (!read_atom_header (moovrf->file, &fourcc, &size))
558 if (fourcc != FOURCC_minf)
561 trakrd->minf_file_offset = ftell (moovrf->file) - 8;
562 trakrd->minf_size = size;
564 /* skip either of vmhd, smhd, hmhd that might follow */
565 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
567 if (fourcc != FOURCC_vmhd && fourcc != FOURCC_smhd && fourcc != FOURCC_hmhd &&
568 fourcc != FOURCC_gmhd)
570 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
573 /* skip a possible hdlr and the following dinf */
574 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
576 if (fourcc == FOURCC_hdlr) {
577 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
579 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
582 if (fourcc != FOURCC_dinf)
584 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
587 /* now we are ready to read the stbl */
588 if (!moov_recov_parse_stbl (moovrf, trakrd))
595 moov_recov_parse_mdhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
601 /* make sure we are on a tkhd atom */
602 if (!read_atom_header (moovrf->file, &fourcc, &size))
604 if (fourcc != FOURCC_mdhd)
607 trakrd->mdhd_file_offset = ftell (moovrf->file) - 8;
609 /* get the timescale */
610 if (fseek (moovrf->file, 12, SEEK_CUR) != 0)
612 if (fread (data, 1, 4, moovrf->file) != 4)
614 trakrd->timescale = GST_READ_UINT32_BE (data);
615 if (fseek (moovrf->file, 8, SEEK_CUR) != 0)
621 moov_recov_parse_mdia (MoovRecovFile * moovrf, TrakRecovData * trakrd)
626 /* make sure we are on a tkhd atom */
627 if (!read_atom_header (moovrf->file, &fourcc, &size))
629 if (fourcc != FOURCC_mdia)
632 trakrd->mdia_file_offset = ftell (moovrf->file) - 8;
633 trakrd->mdia_size = size;
635 if (!moov_recov_parse_mdhd (moovrf, trakrd))
638 if (!skip_atom (moovrf, FOURCC_hdlr))
640 if (!moov_recov_parse_minf (moovrf, trakrd))
646 moov_recov_parse_trak (MoovRecovFile * moovrf, TrakRecovData * trakrd)
652 offset = ftell (moovrf->file);
657 /* make sure we are on a trak atom */
658 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
661 if (fourcc != FOURCC_trak) {
664 trakrd->trak_size = size;
666 /* now we should have a trak header 'tkhd' */
667 if (!moov_recov_parse_tkhd (moovrf, trakrd))
670 /* FIXME add edts handling here and in qtmux, as this is only detected
671 * after buffers start flowing */
673 if (!moov_recov_parse_mdia (moovrf, trakrd))
676 trakrd->file_offset = offset;
677 /* position after the trak */
678 return fseek (moovrf->file, (long int) offset + size, SEEK_SET) == 0;
682 moov_recov_file_create (FILE * file, GError ** err)
685 MoovRecovFile *moovrf = g_new0 (MoovRecovFile, 1);
687 g_return_val_if_fail (file != NULL, NULL);
691 /* look for ftyp and prefix at the start */
692 if (!moov_recov_file_parse_prefix (moovrf)) {
693 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
694 "Error while parsing prefix atoms");
699 if (!moov_recov_file_parse_mvhd (moovrf)) {
700 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
701 "Error while parsing mvhd atom");
705 if (!moov_recov_parse_moov_timescale (moovrf)) {
706 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
707 "Error while parsing timescale");
710 if (!moov_recov_parse_num_traks (moovrf)) {
711 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
712 "Error while parsing parsing number of traks");
717 if (moovrf->num_traks > 1024) {
718 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
719 "Unsupported number of traks");
724 moovrf->traks_rd = g_new0 (TrakRecovData, moovrf->num_traks);
725 for (i = 0; i < moovrf->num_traks; i++) {
726 atom_stbl_init (&(moovrf->traks_rd[i].stbl));
728 for (i = 0; i < moovrf->num_traks; i++) {
729 if (!moov_recov_parse_trak (moovrf, &(moovrf->traks_rd[i]))) {
730 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
731 "Error while parsing trak atom");
739 moov_recov_file_free (moovrf);
744 moov_recov_file_free (MoovRecovFile * moovrf)
747 fclose (moovrf->file);
748 if (moovrf->traks_rd) {
749 for (i = 0; i < moovrf->num_traks; i++) {
750 atom_stbl_clear (&(moovrf->traks_rd[i].stbl));
752 g_free (moovrf->traks_rd);
758 moov_recov_parse_buffer_entry (MoovRecovFile * moovrf, TrakBufferEntryInfo * b)
760 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
763 read = fread (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, moovrf->file);
764 if (read != TRAK_BUFFER_ENTRY_INFO_SIZE)
767 b->track_id = GST_READ_UINT32_BE (data);
768 b->nsamples = GST_READ_UINT32_BE (data + 4);
769 b->delta = GST_READ_UINT32_BE (data + 8);
770 b->size = GST_READ_UINT32_BE (data + 12);
771 b->chunk_offset = GST_READ_UINT64_BE (data + 16);
772 b->sync = data[24] != 0;
773 b->do_pts = data[25] != 0;
774 b->pts_offset = GST_READ_UINT64_BE (data + 26);
779 mdat_recov_add_sample (MdatRecovFile * mdatrf, guint32 size)
781 /* test if this data exists */
782 if (mdatrf->mdat_size - mdatrf->mdat_header_size + size > mdatrf->data_size)
785 mdatrf->mdat_size += size;
789 static TrakRecovData *
790 moov_recov_get_trak (MoovRecovFile * moovrf, guint32 id)
793 for (i = 0; i < moovrf->num_traks; i++) {
794 if (moovrf->traks_rd[i].trak_id == id)
795 return &(moovrf->traks_rd[i]);
801 trak_recov_data_add_sample (TrakRecovData * trak, TrakBufferEntryInfo * b)
803 trak->duration += b->nsamples * b->delta;
804 atom_stbl_add_samples (&trak->stbl, b->nsamples, b->delta, b->size,
805 b->chunk_offset, b->sync, b->pts_offset);
809 * Parses the buffer entries in the MoovRecovFile and matches the inputs
810 * with the data in the MdatRecovFile. Whenever a buffer entry of that
811 * represents 'x' bytes of data, the same amount of data is 'validated' in
812 * the MdatRecovFile and will be inluded in the generated moovie file.
815 moov_recov_parse_buffers (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
818 TrakBufferEntryInfo entry;
821 /* we assume both moovrf and mdatrf are at the starting points of their
823 while (moov_recov_parse_buffer_entry (moovrf, &entry)) {
824 /* be sure we still have this data in mdat */
825 trak = moov_recov_get_trak (moovrf, entry.track_id);
827 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
828 "Invalid trak id found in buffer entry");
831 if (!mdat_recov_add_sample (mdatrf, entry.size))
833 trak_recov_data_add_sample (trak, &entry);
839 trak_recov_data_get_trak_atom_size (TrakRecovData * trak)
841 AtomSTBL *stbl = &trak->stbl;
844 /* write out our stbl child atoms */
847 if (!atom_stts_copy_data (&stbl->stts, NULL, NULL, &offset)) {
850 if (atom_array_get_len (&stbl->stss.entries) > 0) {
851 if (!atom_stss_copy_data (&stbl->stss, NULL, NULL, &offset)) {
855 if (!atom_stsc_copy_data (&stbl->stsc, NULL, NULL, &offset)) {
858 if (!atom_stsz_copy_data (&stbl->stsz, NULL, NULL, &offset)) {
862 if (!atom_ctts_copy_data (stbl->ctts, NULL, NULL, &offset)) {
866 if (!atom_stco64_copy_data (&stbl->stco64, NULL, NULL, &offset)) {
870 return trak->trak_size + ((trak->stsd_size + offset + 8) - trak->stbl_size);
877 moov_recov_get_stbl_children_data (MoovRecovFile * moovrf, TrakRecovData * trak,
880 AtomSTBL *stbl = &trak->stbl;
885 /* write out our stbl child atoms
887 * Use 1MB as a starting size, *_copy_data functions
888 * will grow the buffer if needed.
891 buffer = g_malloc0 (size);
894 if (!atom_stts_copy_data (&stbl->stts, &buffer, &size, &offset)) {
897 if (atom_array_get_len (&stbl->stss.entries) > 0) {
898 if (!atom_stss_copy_data (&stbl->stss, &buffer, &size, &offset)) {
902 if (!atom_stsc_copy_data (&stbl->stsc, &buffer, &size, &offset)) {
905 if (!atom_stsz_copy_data (&stbl->stsz, &buffer, &size, &offset)) {
909 if (!atom_ctts_copy_data (stbl->ctts, &buffer, &size, &offset)) {
913 if (!atom_stco64_copy_data (&stbl->stco64, &buffer, &size, &offset)) {
925 moov_recov_write_file (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
926 FILE * outf, GError ** err)
930 guint8 *prefix_data = NULL;
931 guint8 *mvhd_data = NULL;
932 guint8 *trak_data = NULL;
933 guint32 moov_size = 0;
935 guint64 stbl_children_size = 0;
936 guint8 *stbl_children = NULL;
937 guint32 longest_duration = 0;
940 /* check the version */
941 if (fseek (moovrf->file, 0, SEEK_SET) != 0) {
942 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
943 "Failed to seek to the start of the moov recovery file");
946 if (fread (auxdata, 1, 2, moovrf->file) != 2) {
947 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
948 "Failed to read version from file");
951 version = GST_READ_UINT16_BE (auxdata);
952 if (version != ATOMS_RECOV_FILE_VERSION) {
953 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_VERSION,
954 "Input file version (%u) is not supported in this version (%u)",
955 version, ATOMS_RECOV_FILE_VERSION);
960 prefix_data = g_malloc (moovrf->prefix_size);
961 if (fread (prefix_data, 1, moovrf->prefix_size,
962 moovrf->file) != moovrf->prefix_size) {
963 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
964 "Failed to read the ftyp atom from file");
967 if (fwrite (prefix_data, 1, moovrf->prefix_size, outf) != moovrf->prefix_size) {
968 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
971 g_free (prefix_data);
974 /* need to calculate the moov size beforehand to add the offset to
975 * chunk offset entries */
976 moov_size += moovrf->mvhd_size + 8; /* mvhd + moov size + fourcc */
977 for (i = 0; i < moovrf->num_traks; i++) {
978 TrakRecovData *trak = &(moovrf->traks_rd[i]);
979 guint32 duration; /* in moov's timescale */
982 /* convert trak duration to moov's duration */
983 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
986 if (duration > longest_duration)
987 longest_duration = duration;
988 trak_size = trak_recov_data_get_trak_atom_size (trak);
989 if (trak_size == 0) {
990 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_GENERIC,
991 "Failed to estimate trak atom size");
994 moov_size += trak_size;
997 /* add chunks offsets */
998 for (i = 0; i < moovrf->num_traks; i++) {
999 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1000 /* 16 for the mdat header */
1001 gint64 offset = moov_size + ftell (outf) + 16;
1002 atom_stco64_chunks_set_offset (&trak->stbl.stco64, offset);
1005 /* write the moov */
1006 GST_WRITE_UINT32_BE (auxdata, moov_size);
1007 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_moov);
1008 if (fwrite (auxdata, 1, 8, outf) != 8) {
1009 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1013 /* write the mvhd */
1014 mvhd_data = g_malloc (moovrf->mvhd_size);
1015 if (fseek (moovrf->file, moovrf->mvhd_pos, SEEK_SET) != 0)
1017 if (fread (mvhd_data, 1, moovrf->mvhd_size,
1018 moovrf->file) != moovrf->mvhd_size)
1020 GST_WRITE_UINT32_BE (mvhd_data + 20, moovrf->timescale);
1021 GST_WRITE_UINT32_BE (mvhd_data + 24, longest_duration);
1022 if (fwrite (mvhd_data, 1, moovrf->mvhd_size, outf) != moovrf->mvhd_size) {
1023 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1029 /* write the traks, this is the tough part because we need to update:
1031 * - sizes of atoms from stbl to trak
1034 for (i = 0; i < moovrf->num_traks; i++) {
1035 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1036 guint trak_data_size;
1037 guint32 stbl_new_size;
1038 guint32 minf_new_size;
1039 guint32 mdia_new_size;
1040 guint32 trak_new_size;
1042 guint32 duration; /* in moov's timescale */
1044 /* convert trak duration to moov's duration */
1045 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
1048 stbl_children = moov_recov_get_stbl_children_data (moovrf, trak,
1049 &stbl_children_size);
1050 if (stbl_children == NULL)
1053 /* calc the new size of the atoms from stbl to trak in the atoms tree */
1054 stbl_new_size = trak->stsd_size + stbl_children_size + 8;
1055 size_diff = stbl_new_size - trak->stbl_size;
1056 minf_new_size = trak->minf_size + size_diff;
1057 mdia_new_size = trak->mdia_size + size_diff;
1058 trak_new_size = trak->trak_size + size_diff;
1060 if (fseek (moovrf->file, trak->file_offset, SEEK_SET) != 0)
1062 trak_data_size = trak->post_stsd_offset - trak->file_offset;
1063 trak_data = g_malloc (trak_data_size);
1064 if (fread (trak_data, 1, trak_data_size, moovrf->file) != trak_data_size) {
1067 /* update the size values in those read atoms before writing */
1068 GST_WRITE_UINT32_BE (trak_data, trak_new_size);
1069 GST_WRITE_UINT32_BE (trak_data + (trak->mdia_file_offset -
1070 trak->file_offset), mdia_new_size);
1071 GST_WRITE_UINT32_BE (trak_data + (trak->minf_file_offset -
1072 trak->file_offset), minf_new_size);
1073 GST_WRITE_UINT32_BE (trak_data + (trak->stbl_file_offset -
1074 trak->file_offset), stbl_new_size);
1076 /* update duration values in tkhd and mdhd */
1077 GST_WRITE_UINT32_BE (trak_data + (trak->tkhd_file_offset -
1078 trak->file_offset) + 28, duration);
1079 GST_WRITE_UINT32_BE (trak_data + (trak->mdhd_file_offset -
1080 trak->file_offset) + 24, trak->duration);
1082 if (fwrite (trak_data, 1, trak_data_size, outf) != trak_data_size) {
1083 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1086 if (fwrite (stbl_children, 1, stbl_children_size, outf) !=
1087 stbl_children_size) {
1088 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1093 g_free (stbl_children);
1094 stbl_children = NULL;
1097 /* write the mdat */
1098 /* write the header first */
1099 GST_WRITE_UINT32_BE (auxdata, 1);
1100 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_mdat);
1101 GST_WRITE_UINT64_BE (auxdata + 8, mdatrf->mdat_size);
1102 if (fwrite (auxdata, 1, 16, outf) != 16) {
1103 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1107 /* now read the mdat data and output to the file */
1108 if (fseek (mdatrf->file, mdatrf->mdat_start +
1109 (mdatrf->rawfile ? 0 : mdatrf->mdat_header_size), SEEK_SET) != 0)
1112 data = g_malloc (4096);
1113 while (!feof (mdatrf->file)) {
1116 read = fread (data, 1, 4096, mdatrf->file);
1117 write = fwrite (data, 1, read, outf);
1119 if (write != read) {
1120 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1121 "Failed to copy data to output file: %s", g_strerror (errno));
1130 g_free (stbl_children);
1132 g_free (prefix_data);