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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 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)
111 if (fwrite (GST_BUFFER_DATA (prefix), 1, GST_BUFFER_SIZE (prefix), f) !=
112 GST_BUFFER_SIZE (prefix)) {
116 if (!atom_ftyp_copy_data (ftyp, &data, &size, &offset)) {
119 if (fwrite (data, 1, offset, f) != offset) {
128 * Writes important info on the 'moov' atom (non-trak related)
129 * to be able to recover the moov structure after a crash.
131 * Currently, it writes the MVHD atom.
134 atoms_recov_write_moov_info (FILE * f, AtomMOOV * moov)
139 guint64 atom_size = 0;
144 data = g_malloc (size);
145 atom_size = atom_mvhd_copy_data (&moov->mvhd, &data, &size, &offset);
147 writen = fwrite (data, 1, atom_size, f);
149 return atom_size > 0 && writen == atom_size;
153 * Writes the number of traks to the file.
154 * This simply writes a guint32 in BE.
157 atoms_recov_write_traks_number (FILE * f, guint32 traks)
160 GST_WRITE_UINT32_BE (data, traks);
161 return fwrite (data, 4, 1, f) == 1;
165 * Writes the moov's timescale to the file
166 * This simply writes a guint32 in BE.
169 atoms_recov_write_moov_timescale (FILE * f, guint32 timescale)
172 GST_WRITE_UINT32_BE (data, timescale);
173 return fwrite (data, 4, 1, f) == 1;
177 * Writes the trak atom to the file.
180 atoms_recov_write_trak_info (FILE * f, AtomTRAK * trak)
185 guint64 atom_size = 0;
188 /* buffer is realloced to a larger size if needed */
190 data = g_malloc (size);
191 atom_size = atom_trak_copy_data (trak, &data, &size, &offset);
193 writen = fwrite (data, atom_size, 1, f);
195 return atom_size > 0 && writen == atom_size;
199 atoms_recov_write_trak_samples (FILE * f, AtomTRAK * trak, guint32 nsamples,
200 guint32 delta, guint32 size, guint64 chunk_offset, gboolean sync,
201 gboolean do_pts, gint64 pts_offset)
203 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
205 * We have to write a TrakBufferEntryInfo
207 GST_WRITE_UINT32_BE (data + 0, trak->tkhd.track_ID);
208 GST_WRITE_UINT32_BE (data + 4, nsamples);
209 GST_WRITE_UINT32_BE (data + 8, delta);
210 GST_WRITE_UINT32_BE (data + 12, size);
211 GST_WRITE_UINT64_BE (data + 16, chunk_offset);
213 GST_WRITE_UINT8 (data + 24, 1);
215 GST_WRITE_UINT8 (data + 24, 0);
217 GST_WRITE_UINT8 (data + 25, 1);
218 GST_WRITE_UINT64_BE (data + 26, pts_offset);
220 GST_WRITE_UINT8 (data + 25, 0);
221 GST_WRITE_UINT64_BE (data + 26, 0);
224 return fwrite (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, f) ==
225 TRAK_BUFFER_ENTRY_INFO_SIZE;
229 atoms_recov_write_headers (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix,
230 AtomMOOV * moov, guint32 timescale, guint32 traks_number)
232 if (!atoms_recov_write_version (f)) {
236 if (!atoms_recov_write_ftyp_info (f, ftyp, prefix)) {
240 if (!atoms_recov_write_moov_info (f, moov)) {
244 if (!atoms_recov_write_moov_timescale (f, timescale)) {
248 if (!atoms_recov_write_traks_number (f, traks_number)) {
256 read_atom_header (FILE * f, guint32 * fourcc, guint32 * size)
260 if (fread (aux, 1, 8, f) != 8)
262 *size = GST_READ_UINT32_BE (aux);
263 *fourcc = GST_READ_UINT32_LE (aux + 4);
268 moov_recov_file_parse_prefix (MoovRecovFile * moovrf)
272 guint32 total_size = 0;
273 if (fseek (moovrf->file, 2, SEEK_SET) != 0)
275 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
279 if (fourcc != FOURCC_ftyp) {
280 /* we might have a prefix here */
281 if (fseek (moovrf->file, size - 8, SEEK_CUR) != 0)
286 /* now read the ftyp */
287 if (!read_atom_header (moovrf->file, &fourcc, &size))
291 /* this has to be the ftyp */
292 if (fourcc != FOURCC_ftyp)
295 moovrf->prefix_size = total_size;
296 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
300 moov_recov_file_parse_mvhd (MoovRecovFile * moovrf)
304 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
307 /* check for sanity */
308 if (fourcc != FOURCC_mvhd)
311 moovrf->mvhd_size = size;
312 moovrf->mvhd_pos = ftell (moovrf->file) - 8;
314 /* skip the remaining of the mvhd in the file */
315 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
319 mdat_recov_file_parse_mdat_start (MdatRecovFile * mdatrf)
321 guint32 fourcc, size;
323 if (!read_atom_header (mdatrf->file, &fourcc, &size)) {
327 mdatrf->mdat_header_size = 16;
328 mdatrf->mdat_size = 16;
330 mdatrf->mdat_header_size = 8;
331 mdatrf->mdat_size = 8;
333 mdatrf->mdat_start = ftell (mdatrf->file) - 8;
335 return fourcc == FOURCC_mdat;
339 mdat_recov_file_create (FILE * file, gboolean datafile, GError ** err)
341 MdatRecovFile *mrf = g_new0 (MdatRecovFile, 1);
342 guint32 fourcc, size;
344 g_return_val_if_fail (file != NULL, NULL);
347 mrf->rawfile = datafile;
349 /* get the file/data length */
350 if (fseek (file, 0, SEEK_END) != 0)
351 goto file_length_error;
352 /* still needs to deduce the mdat header and ftyp size */
353 mrf->data_size = ftell (file);
354 if (mrf->data_size == -1L)
355 goto file_length_error;
357 if (fseek (file, 0, SEEK_SET) != 0)
358 goto file_seek_error;
361 /* this file contains no atoms, only raw data to be placed on the mdat
362 * this happens when faststart mode is used */
364 mrf->mdat_header_size = 16;
369 if (!read_atom_header (file, &fourcc, &size)) {
372 if (fourcc != FOURCC_ftyp) {
373 /* this could be a prefix atom, let's skip it and try again */
374 if (fseek (file, size - 8, SEEK_CUR) != 0) {
375 goto file_seek_error;
377 if (!read_atom_header (file, &fourcc, &size)) {
382 if (fourcc != FOURCC_ftyp) {
385 if (fseek (file, size - 8, SEEK_CUR) != 0)
386 goto file_seek_error;
388 /* we don't parse this if we have a tmpdatafile */
389 if (!mdat_recov_file_parse_mdat_start (mrf)) {
390 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
391 "Error while parsing mdat atom");
398 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
399 "Failed to parse atom");
403 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
404 "Failed to seek to start of the file");
408 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
409 "Failed to determine file size");
413 mdat_recov_file_free (mrf);
418 mdat_recov_file_free (MdatRecovFile * mrf)
425 moov_recov_parse_num_traks (MoovRecovFile * moovrf)
428 if (fread (traks, 1, 4, moovrf->file) != 4)
430 moovrf->num_traks = GST_READ_UINT32_BE (traks);
435 moov_recov_parse_moov_timescale (MoovRecovFile * moovrf)
438 if (fread (ts, 1, 4, moovrf->file) != 4)
440 moovrf->timescale = GST_READ_UINT32_BE (ts);
445 skip_atom (MoovRecovFile * moovrf, guint32 expected_fourcc)
450 if (!read_atom_header (moovrf->file, &fourcc, &size))
452 if (fourcc != expected_fourcc)
455 return (fseek (moovrf->file, size - 8, SEEK_CUR) == 0);
459 moov_recov_parse_tkhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
465 /* make sure we are on a tkhd atom */
466 if (!read_atom_header (moovrf->file, &fourcc, &size))
468 if (fourcc != FOURCC_tkhd)
471 trakrd->tkhd_file_offset = ftell (moovrf->file) - 8;
473 /* move 8 bytes forward to the trak_id pos */
474 if (fseek (moovrf->file, 12, SEEK_CUR) != 0)
476 if (fread (data, 1, 4, moovrf->file) != 4)
479 /* advance the rest of tkhd */
480 fseek (moovrf->file, 68, SEEK_CUR);
482 trakrd->trak_id = GST_READ_UINT32_BE (data);
487 moov_recov_parse_stbl (MoovRecovFile * moovrf, TrakRecovData * trakrd)
493 if (!read_atom_header (moovrf->file, &fourcc, &size))
495 if (fourcc != FOURCC_stbl)
498 trakrd->stbl_file_offset = ftell (moovrf->file) - 8;
499 trakrd->stbl_size = size;
502 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
504 if (fourcc != FOURCC_stsd)
506 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR) != 0)
509 trakrd->stsd_size = auxsize;
510 trakrd->post_stsd_offset = ftell (moovrf->file);
512 /* as this is the last atom we parse, we don't skip forward */
518 moov_recov_parse_minf (MoovRecovFile * moovrf, TrakRecovData * trakrd)
524 if (!read_atom_header (moovrf->file, &fourcc, &size))
526 if (fourcc != FOURCC_minf)
529 trakrd->minf_file_offset = ftell (moovrf->file) - 8;
530 trakrd->minf_size = size;
532 /* skip either of vmhd, smhd, hmhd that might follow */
533 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
535 if (fourcc != FOURCC_vmhd && fourcc != FOURCC_smhd && fourcc != FOURCC_hmhd &&
536 fourcc != FOURCC_gmhd)
538 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
541 /* skip a possible hdlr and the following dinf */
542 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
544 if (fourcc == FOURCC_hdlr) {
545 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
547 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
550 if (fourcc != FOURCC_dinf)
552 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
555 /* now we are ready to read the stbl */
556 if (!moov_recov_parse_stbl (moovrf, trakrd))
563 moov_recov_parse_mdhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
569 /* make sure we are on a tkhd atom */
570 if (!read_atom_header (moovrf->file, &fourcc, &size))
572 if (fourcc != FOURCC_mdhd)
575 trakrd->mdhd_file_offset = ftell (moovrf->file) - 8;
577 /* get the timescale */
578 if (fseek (moovrf->file, 12, SEEK_CUR) != 0)
580 if (fread (data, 1, 4, moovrf->file) != 4)
582 trakrd->timescale = GST_READ_UINT32_BE (data);
583 if (fseek (moovrf->file, 8, SEEK_CUR) != 0)
589 moov_recov_parse_mdia (MoovRecovFile * moovrf, TrakRecovData * trakrd)
594 /* make sure we are on a tkhd atom */
595 if (!read_atom_header (moovrf->file, &fourcc, &size))
597 if (fourcc != FOURCC_mdia)
600 trakrd->mdia_file_offset = ftell (moovrf->file) - 8;
601 trakrd->mdia_size = size;
603 if (!moov_recov_parse_mdhd (moovrf, trakrd))
606 if (!skip_atom (moovrf, FOURCC_hdlr))
608 if (!moov_recov_parse_minf (moovrf, trakrd))
614 moov_recov_parse_trak (MoovRecovFile * moovrf, TrakRecovData * trakrd)
620 offset = ftell (moovrf->file);
625 /* make sure we are on a trak atom */
626 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
629 if (fourcc != FOURCC_trak) {
632 trakrd->trak_size = size;
634 /* now we should have a trak header 'tkhd' */
635 if (!moov_recov_parse_tkhd (moovrf, trakrd))
638 /* FIXME add edts handling here and in qtmux, as this is only detected
639 * after buffers start flowing */
641 if (!moov_recov_parse_mdia (moovrf, trakrd))
644 trakrd->file_offset = offset;
645 /* position after the trak */
646 return fseek (moovrf->file, (long int) offset + size, SEEK_SET) == 0;
650 moov_recov_file_create (FILE * file, GError ** err)
653 MoovRecovFile *moovrf = g_new0 (MoovRecovFile, 1);
655 g_return_val_if_fail (file != NULL, NULL);
659 /* look for ftyp and prefix at the start */
660 if (!moov_recov_file_parse_prefix (moovrf)) {
661 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
662 "Error while parsing prefix atoms");
667 if (!moov_recov_file_parse_mvhd (moovrf)) {
668 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
669 "Error while parsing mvhd atom");
673 if (!moov_recov_parse_moov_timescale (moovrf)) {
674 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
675 "Error while parsing timescale");
678 if (!moov_recov_parse_num_traks (moovrf)) {
679 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
680 "Error while parsing parsing number of traks");
685 moovrf->traks_rd = g_new0 (TrakRecovData, moovrf->num_traks);
686 for (i = 0; i < moovrf->num_traks; i++) {
687 atom_stbl_init (&(moovrf->traks_rd[i].stbl));
689 for (i = 0; i < moovrf->num_traks; i++) {
690 if (!moov_recov_parse_trak (moovrf, &(moovrf->traks_rd[i]))) {
691 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
692 "Error while parsing trak atom");
700 moov_recov_file_free (moovrf);
705 moov_recov_file_free (MoovRecovFile * moovrf)
708 fclose (moovrf->file);
709 if (moovrf->traks_rd) {
710 for (i = 0; i < moovrf->num_traks; i++) {
711 atom_stbl_clear (&(moovrf->traks_rd[i].stbl));
713 g_free (moovrf->traks_rd);
719 moov_recov_parse_buffer_entry (MoovRecovFile * moovrf, TrakBufferEntryInfo * b)
721 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
724 read = fread (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, moovrf->file);
725 if (read != TRAK_BUFFER_ENTRY_INFO_SIZE)
728 b->track_id = GST_READ_UINT32_BE (data);
729 b->nsamples = GST_READ_UINT32_BE (data + 4);
730 b->delta = GST_READ_UINT32_BE (data + 8);
731 b->size = GST_READ_UINT32_BE (data + 12);
732 b->chunk_offset = GST_READ_UINT64_BE (data + 16);
733 b->sync = data[24] != 0;
734 b->do_pts = data[25] != 0;
735 b->pts_offset = GST_READ_UINT64_BE (data + 26);
740 mdat_recov_add_sample (MdatRecovFile * mdatrf, guint32 size)
742 /* test if this data exists */
743 if (mdatrf->mdat_size - mdatrf->mdat_header_size + size > mdatrf->data_size)
746 mdatrf->mdat_size += size;
750 static TrakRecovData *
751 moov_recov_get_trak (MoovRecovFile * moovrf, guint32 id)
754 for (i = 0; i < moovrf->num_traks; i++) {
755 if (moovrf->traks_rd[i].trak_id == id)
756 return &(moovrf->traks_rd[i]);
762 trak_recov_data_add_sample (TrakRecovData * trak, TrakBufferEntryInfo * b)
764 trak->duration += b->nsamples * b->delta;
765 atom_stbl_add_samples (&trak->stbl, b->nsamples, b->delta, b->size,
766 b->chunk_offset, b->sync, b->pts_offset);
770 * Parses the buffer entries in the MoovRecovFile and matches the inputs
771 * with the data in the MdatRecovFile. Whenever a buffer entry of that
772 * represents 'x' bytes of data, the same amount of data is 'validated' in
773 * the MdatRecovFile and will be inluded in the generated moovie file.
776 moov_recov_parse_buffers (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
779 TrakBufferEntryInfo entry;
782 /* we assume both moovrf and mdatrf are at the starting points of their
784 while (moov_recov_parse_buffer_entry (moovrf, &entry)) {
785 /* be sure we still have this data in mdat */
786 trak = moov_recov_get_trak (moovrf, entry.track_id);
788 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
789 "Invalid trak id found in buffer entry");
792 if (!mdat_recov_add_sample (mdatrf, entry.size))
794 trak_recov_data_add_sample (trak, &entry);
800 trak_recov_data_get_trak_atom_size (TrakRecovData * trak)
802 AtomSTBL *stbl = &trak->stbl;
805 /* write out our stbl child atoms */
808 if (!atom_stts_copy_data (&stbl->stts, NULL, NULL, &offset)) {
811 if (atom_array_get_len (&stbl->stss.entries) > 0) {
812 if (!atom_stss_copy_data (&stbl->stss, NULL, NULL, &offset)) {
816 if (!atom_stsc_copy_data (&stbl->stsc, NULL, NULL, &offset)) {
819 if (!atom_stsz_copy_data (&stbl->stsz, NULL, NULL, &offset)) {
823 if (!atom_ctts_copy_data (stbl->ctts, NULL, NULL, &offset)) {
827 if (!atom_stco64_copy_data (&stbl->stco64, NULL, NULL, &offset)) {
831 return trak->trak_size + ((trak->stsd_size + offset + 8) - trak->stbl_size);
838 moov_recov_get_stbl_children_data (MoovRecovFile * moovrf, TrakRecovData * trak,
841 AtomSTBL *stbl = &trak->stbl;
846 /* write out our stbl child atoms
848 * Use 1MB as a starting size, *_copy_data functions
849 * will grow the buffer if needed.
852 buffer = g_malloc0 (size);
855 if (!atom_stts_copy_data (&stbl->stts, &buffer, &size, &offset)) {
858 if (atom_array_get_len (&stbl->stss.entries) > 0) {
859 if (!atom_stss_copy_data (&stbl->stss, &buffer, &size, &offset)) {
863 if (!atom_stsc_copy_data (&stbl->stsc, &buffer, &size, &offset)) {
866 if (!atom_stsz_copy_data (&stbl->stsz, &buffer, &size, &offset)) {
870 if (!atom_ctts_copy_data (stbl->ctts, &buffer, &size, &offset)) {
874 if (!atom_stco64_copy_data (&stbl->stco64, &buffer, &size, &offset)) {
886 moov_recov_write_file (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
887 FILE * outf, GError ** err)
891 guint8 *prefix_data = NULL;
892 guint8 *mvhd_data = NULL;
893 guint8 *trak_data = NULL;
894 guint32 moov_size = 0;
896 guint64 stbl_children_size = 0;
897 guint8 *stbl_children = NULL;
898 guint32 longest_duration = 0;
901 /* check the version */
902 if (fseek (moovrf->file, 0, SEEK_SET) != 0) {
903 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
904 "Failed to seek to the start of the moov recovery file");
907 if (fread (auxdata, 1, 2, moovrf->file) != 2) {
908 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
909 "Failed to read version from file");
912 version = GST_READ_UINT16_BE (auxdata);
913 if (version != ATOMS_RECOV_FILE_VERSION) {
914 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_VERSION,
915 "Input file version (%u) is not supported in this version (%u)",
916 version, ATOMS_RECOV_FILE_VERSION);
921 prefix_data = g_malloc (moovrf->prefix_size);
922 if (fread (prefix_data, 1, moovrf->prefix_size,
923 moovrf->file) != moovrf->prefix_size) {
924 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
925 "Failed to read the ftyp atom from file");
928 if (fwrite (prefix_data, 1, moovrf->prefix_size, outf) != moovrf->prefix_size) {
929 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
932 g_free (prefix_data);
935 /* need to calculate the moov size beforehand to add the offset to
936 * chunk offset entries */
937 moov_size += moovrf->mvhd_size + 8; /* mvhd + moov size + fourcc */
938 for (i = 0; i < moovrf->num_traks; i++) {
939 TrakRecovData *trak = &(moovrf->traks_rd[i]);
940 guint32 duration; /* in moov's timescale */
943 /* convert trak duration to moov's duration */
944 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
947 if (duration > longest_duration)
948 longest_duration = duration;
949 trak_size = trak_recov_data_get_trak_atom_size (trak);
950 if (trak_size == 0) {
951 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_GENERIC,
952 "Failed to estimate trak atom size");
955 moov_size += trak_size;
958 /* add chunks offsets */
959 for (i = 0; i < moovrf->num_traks; i++) {
960 TrakRecovData *trak = &(moovrf->traks_rd[i]);
961 /* 16 for the mdat header */
962 gint64 offset = moov_size + ftell (outf) + 16;
963 atom_stco64_chunks_add_offset (&trak->stbl.stco64, offset);
967 GST_WRITE_UINT32_BE (auxdata, moov_size);
968 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_moov);
969 if (fwrite (auxdata, 1, 8, outf) != 8) {
970 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
975 mvhd_data = g_malloc (moovrf->mvhd_size);
976 if (fseek (moovrf->file, moovrf->mvhd_pos, SEEK_SET) != 0)
978 if (fread (mvhd_data, 1, moovrf->mvhd_size,
979 moovrf->file) != moovrf->mvhd_size)
981 GST_WRITE_UINT32_BE (mvhd_data + 20, moovrf->timescale);
982 GST_WRITE_UINT32_BE (mvhd_data + 24, longest_duration);
983 if (fwrite (mvhd_data, 1, moovrf->mvhd_size, outf) != moovrf->mvhd_size) {
984 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
990 /* write the traks, this is the tough part because we need to update:
992 * - sizes of atoms from stbl to trak
995 for (i = 0; i < moovrf->num_traks; i++) {
996 TrakRecovData *trak = &(moovrf->traks_rd[i]);
997 guint trak_data_size;
998 guint32 stbl_new_size;
999 guint32 minf_new_size;
1000 guint32 mdia_new_size;
1001 guint32 trak_new_size;
1003 guint32 duration; /* in moov's timescale */
1005 /* convert trak duration to moov's duration */
1006 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
1009 stbl_children = moov_recov_get_stbl_children_data (moovrf, trak,
1010 &stbl_children_size);
1011 if (stbl_children == NULL)
1014 /* calc the new size of the atoms from stbl to trak in the atoms tree */
1015 stbl_new_size = trak->stsd_size + stbl_children_size + 8;
1016 size_diff = stbl_new_size - trak->stbl_size;
1017 minf_new_size = trak->minf_size + size_diff;
1018 mdia_new_size = trak->mdia_size + size_diff;
1019 trak_new_size = trak->trak_size + size_diff;
1021 if (fseek (moovrf->file, trak->file_offset, SEEK_SET) != 0)
1023 trak_data_size = trak->post_stsd_offset - trak->file_offset;
1024 trak_data = g_malloc (trak_data_size);
1025 if (fread (trak_data, 1, trak_data_size, moovrf->file) != trak_data_size) {
1028 /* update the size values in those read atoms before writing */
1029 GST_WRITE_UINT32_BE (trak_data, trak_new_size);
1030 GST_WRITE_UINT32_BE (trak_data + (trak->mdia_file_offset -
1031 trak->file_offset), mdia_new_size);
1032 GST_WRITE_UINT32_BE (trak_data + (trak->minf_file_offset -
1033 trak->file_offset), minf_new_size);
1034 GST_WRITE_UINT32_BE (trak_data + (trak->stbl_file_offset -
1035 trak->file_offset), stbl_new_size);
1037 /* update duration values in tkhd and mdhd */
1038 GST_WRITE_UINT32_BE (trak_data + (trak->tkhd_file_offset -
1039 trak->file_offset) + 28, duration);
1040 GST_WRITE_UINT32_BE (trak_data + (trak->mdhd_file_offset -
1041 trak->file_offset) + 24, trak->duration);
1043 if (fwrite (trak_data, 1, trak_data_size, outf) != trak_data_size) {
1044 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1047 if (fwrite (stbl_children, 1, stbl_children_size, outf) !=
1048 stbl_children_size) {
1049 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1054 g_free (stbl_children);
1055 stbl_children = NULL;
1058 /* write the mdat */
1059 /* write the header first */
1060 GST_WRITE_UINT32_BE (auxdata, 1);
1061 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_mdat);
1062 GST_WRITE_UINT64_BE (auxdata + 8, mdatrf->mdat_size);
1063 if (fwrite (auxdata, 1, 16, outf) != 16) {
1064 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1068 /* now read the mdat data and output to the file */
1069 if (fseek (mdatrf->file, mdatrf->mdat_start +
1070 (mdatrf->rawfile ? 0 : mdatrf->mdat_header_size), SEEK_SET) != 0)
1073 data = g_malloc (4096);
1074 while (!feof (mdatrf->file)) {
1077 read = fread (data, 1, 4096, mdatrf->file);
1078 write = fwrite (data, 1, read, outf);
1080 if (write != read) {
1081 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1082 "Failed to copy data to output file: %s", g_strerror (errno));
1091 g_free (stbl_children);
1093 g_free (prefix_data);