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 MAX_CHUNK_SIZE (1024 * 1024) /* 1MB */
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))
98 atoms_recov_write_version (FILE * f)
101 GST_WRITE_UINT16_BE (data, ATOMS_RECOV_FILE_VERSION);
102 return fwrite (data, 2, 1, f) == 1;
106 atoms_recov_write_ftyp_info (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix)
115 if (!gst_buffer_map (prefix, &map, GST_MAP_READ)) {
118 if (fwrite (map.data, 1, map.size, f) != map.size) {
119 gst_buffer_unmap (prefix, &map);
122 gst_buffer_unmap (prefix, &map);
124 if (!atom_ftyp_copy_data (ftyp, &data, &size, &offset)) {
127 if (fwrite (data, 1, offset, f) != offset) {
136 * Writes important info on the 'moov' atom (non-trak related)
137 * to be able to recover the moov structure after a crash.
139 * Currently, it writes the MVHD atom.
142 atoms_recov_write_moov_info (FILE * f, AtomMOOV * moov)
147 guint64 atom_size = 0;
152 data = g_malloc (size);
153 atom_size = atom_mvhd_copy_data (&moov->mvhd, &data, &size, &offset);
155 writen = fwrite (data, 1, atom_size, f);
157 return atom_size > 0 && writen == atom_size;
161 * Writes the number of traks to the file.
162 * This simply writes a guint32 in BE.
165 atoms_recov_write_traks_number (FILE * f, guint32 traks)
168 GST_WRITE_UINT32_BE (data, traks);
169 return fwrite (data, 4, 1, f) == 1;
173 * Writes the moov's timescale to the file
174 * This simply writes a guint32 in BE.
177 atoms_recov_write_moov_timescale (FILE * f, guint32 timescale)
180 GST_WRITE_UINT32_BE (data, timescale);
181 return fwrite (data, 4, 1, f) == 1;
185 * Writes the trak atom to the file.
188 atoms_recov_write_trak_info (FILE * f, AtomTRAK * trak)
193 guint64 atom_size = 0;
196 /* buffer is realloced to a larger size if needed */
198 data = g_malloc (size);
199 atom_size = atom_trak_copy_data (trak, &data, &size, &offset);
201 writen = fwrite (data, atom_size, 1, f);
203 return atom_size > 0 && writen == atom_size;
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)
211 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
213 * We have to write a TrakBufferEntryInfo
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);
221 GST_WRITE_UINT8 (data + 24, 1);
223 GST_WRITE_UINT8 (data + 24, 0);
225 GST_WRITE_UINT8 (data + 25, 1);
226 GST_WRITE_UINT64_BE (data + 26, pts_offset);
228 GST_WRITE_UINT8 (data + 25, 0);
229 GST_WRITE_UINT64_BE (data + 26, 0);
232 return fwrite (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, f) ==
233 TRAK_BUFFER_ENTRY_INFO_SIZE;
237 atoms_recov_write_headers (FILE * f, AtomFTYP * ftyp, GstBuffer * prefix,
238 AtomMOOV * moov, guint32 timescale, guint32 traks_number)
240 if (!atoms_recov_write_version (f)) {
244 if (!atoms_recov_write_ftyp_info (f, ftyp, prefix)) {
248 if (!atoms_recov_write_moov_info (f, moov)) {
252 if (!atoms_recov_write_moov_timescale (f, timescale)) {
256 if (!atoms_recov_write_traks_number (f, traks_number)) {
264 read_atom_header (FILE * f, guint32 * fourcc, guint32 * size)
268 if (fread (aux, 1, 8, f) != 8)
270 *size = GST_READ_UINT32_BE (aux);
271 *fourcc = GST_READ_UINT32_LE (aux + 4);
276 moov_recov_file_parse_prefix (MoovRecovFile * moovrf)
280 guint32 total_size = 0;
281 if (fseek (moovrf->file, 2, SEEK_SET) != 0)
283 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
287 if (fourcc != FOURCC_ftyp) {
288 /* we might have a prefix here */
289 if (fseek (moovrf->file, size - 8, SEEK_CUR) != 0)
294 /* now read the ftyp */
295 if (!read_atom_header (moovrf->file, &fourcc, &size))
299 /* this has to be the ftyp */
300 if (fourcc != FOURCC_ftyp)
303 moovrf->prefix_size = total_size;
304 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
308 moov_recov_file_parse_mvhd (MoovRecovFile * moovrf)
312 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
315 /* check for sanity */
316 if (fourcc != FOURCC_mvhd)
319 moovrf->mvhd_size = size;
320 moovrf->mvhd_pos = ftell (moovrf->file) - 8;
322 /* skip the remaining of the mvhd in the file */
323 return fseek (moovrf->file, size - 8, SEEK_CUR) == 0;
327 mdat_recov_file_parse_mdat_start (MdatRecovFile * mdatrf)
329 guint32 fourcc, size;
331 if (!read_atom_header (mdatrf->file, &fourcc, &size)) {
335 mdatrf->mdat_header_size = 16;
336 mdatrf->mdat_size = 16;
338 mdatrf->mdat_header_size = 8;
339 mdatrf->mdat_size = 8;
341 mdatrf->mdat_start = ftell (mdatrf->file) - 8;
343 return fourcc == FOURCC_mdat;
347 mdat_recov_file_find_mdat (FILE * file, GError ** err)
349 guint32 fourcc = 0, size = 0;
350 gboolean failure = FALSE;
351 while (fourcc != FOURCC_mdat && !failure) {
352 if (!read_atom_header (file, &fourcc, &size)) {
356 /* skip these atoms */
360 if (fseek (file, size - 8, SEEK_CUR) != 0) {
361 goto file_seek_error;
367 GST_ERROR ("Unexpected atom in headers %" GST_FOURCC_FORMAT,
368 GST_FOURCC_ARGS (fourcc));
375 /* Reverse to mdat start */
376 if (fseek (file, -8, SEEK_CUR) != 0)
377 goto file_seek_error;
383 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
384 "Failed to parse atom");
388 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
389 "Failed to seek to start of the file");
395 mdat_recov_file_create (FILE * file, gboolean datafile, GError ** err)
397 MdatRecovFile *mrf = g_new0 (MdatRecovFile, 1);
399 g_return_val_if_fail (file != NULL, NULL);
402 mrf->rawfile = datafile;
404 /* get the file/data length */
405 if (fseek (file, 0, SEEK_END) != 0)
406 goto file_length_error;
407 /* still needs to deduce the mdat header and ftyp size */
408 mrf->data_size = ftell (file);
409 if (mrf->data_size == -1L)
410 goto file_length_error;
412 if (fseek (file, 0, SEEK_SET) != 0)
413 goto file_seek_error;
416 /* this file contains no atoms, only raw data to be placed on the mdat
417 * this happens when faststart mode is used */
419 mrf->mdat_header_size = 16;
424 if (!mdat_recov_file_find_mdat (file, err)) {
428 /* we don't parse this if we have a tmpdatafile */
429 if (!mdat_recov_file_parse_mdat_start (mrf)) {
430 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
431 "Error while parsing mdat atom");
438 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
439 "Failed to seek to start of the file");
443 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
444 "Failed to determine file size");
448 mdat_recov_file_free (mrf);
453 mdat_recov_file_free (MdatRecovFile * mrf)
460 moov_recov_parse_num_traks (MoovRecovFile * moovrf)
463 if (fread (traks, 1, 4, moovrf->file) != 4)
465 moovrf->num_traks = GST_READ_UINT32_BE (traks);
470 moov_recov_parse_moov_timescale (MoovRecovFile * moovrf)
473 if (fread (ts, 1, 4, moovrf->file) != 4)
475 moovrf->timescale = GST_READ_UINT32_BE (ts);
480 skip_atom (MoovRecovFile * moovrf, guint32 expected_fourcc)
485 if (!read_atom_header (moovrf->file, &fourcc, &size))
487 if (fourcc != expected_fourcc)
490 return (fseek (moovrf->file, size - 8, SEEK_CUR) == 0);
494 moov_recov_parse_tkhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
500 /* make sure we are on a tkhd atom */
501 if (!read_atom_header (moovrf->file, &fourcc, &size))
503 if (fourcc != FOURCC_tkhd)
506 trakrd->tkhd_file_offset = ftell (moovrf->file) - 8;
508 /* move 8 bytes forward to the trak_id pos */
509 if (fseek (moovrf->file, 12, SEEK_CUR) != 0)
511 if (fread (data, 1, 4, moovrf->file) != 4)
514 /* advance the rest of tkhd */
515 if (fseek (moovrf->file, 68, SEEK_CUR) != 0)
518 trakrd->trak_id = GST_READ_UINT32_BE (data);
523 moov_recov_parse_stbl (MoovRecovFile * moovrf, TrakRecovData * trakrd)
529 if (!read_atom_header (moovrf->file, &fourcc, &size))
531 if (fourcc != FOURCC_stbl)
534 trakrd->stbl_file_offset = ftell (moovrf->file) - 8;
535 trakrd->stbl_size = size;
538 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
540 if (fourcc != FOURCC_stsd)
542 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR) != 0)
545 trakrd->stsd_size = auxsize;
546 trakrd->post_stsd_offset = ftell (moovrf->file);
548 /* as this is the last atom we parse, we don't skip forward */
554 moov_recov_parse_minf (MoovRecovFile * moovrf, TrakRecovData * trakrd)
560 if (!read_atom_header (moovrf->file, &fourcc, &size))
562 if (fourcc != FOURCC_minf)
565 trakrd->minf_file_offset = ftell (moovrf->file) - 8;
566 trakrd->minf_size = size;
568 /* skip either of vmhd, smhd, hmhd that might follow */
569 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
571 if (fourcc != FOURCC_vmhd && fourcc != FOURCC_smhd && fourcc != FOURCC_hmhd &&
572 fourcc != FOURCC_gmhd)
574 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
577 /* skip a possible hdlr and the following dinf */
578 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
580 if (fourcc == FOURCC_hdlr) {
581 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
583 if (!read_atom_header (moovrf->file, &fourcc, &auxsize))
586 if (fourcc != FOURCC_dinf)
588 if (fseek (moovrf->file, auxsize - 8, SEEK_CUR))
591 /* now we are ready to read the stbl */
592 if (!moov_recov_parse_stbl (moovrf, trakrd))
599 moov_recov_parse_mdhd (MoovRecovFile * moovrf, TrakRecovData * trakrd)
605 /* make sure we are on a tkhd atom */
606 if (!read_atom_header (moovrf->file, &fourcc, &size))
608 if (fourcc != FOURCC_mdhd)
611 trakrd->mdhd_file_offset = ftell (moovrf->file) - 8;
613 /* get the timescale */
614 if (fseek (moovrf->file, 12, SEEK_CUR) != 0)
616 if (fread (data, 1, 4, moovrf->file) != 4)
618 trakrd->timescale = GST_READ_UINT32_BE (data);
619 if (fseek (moovrf->file, 8, SEEK_CUR) != 0)
625 moov_recov_parse_mdia (MoovRecovFile * moovrf, TrakRecovData * trakrd)
630 /* make sure we are on a tkhd atom */
631 if (!read_atom_header (moovrf->file, &fourcc, &size))
633 if (fourcc != FOURCC_mdia)
636 trakrd->mdia_file_offset = ftell (moovrf->file) - 8;
637 trakrd->mdia_size = size;
639 if (!moov_recov_parse_mdhd (moovrf, trakrd))
642 if (!skip_atom (moovrf, FOURCC_hdlr))
644 if (!moov_recov_parse_minf (moovrf, trakrd))
650 moov_recov_parse_trak (MoovRecovFile * moovrf, TrakRecovData * trakrd)
656 offset = ftell (moovrf->file);
661 /* make sure we are on a trak atom */
662 if (!read_atom_header (moovrf->file, &fourcc, &size)) {
665 if (fourcc != FOURCC_trak) {
668 trakrd->trak_size = size;
670 /* now we should have a trak header 'tkhd' */
671 if (!moov_recov_parse_tkhd (moovrf, trakrd))
674 /* FIXME add edts handling here and in qtmux, as this is only detected
675 * after buffers start flowing */
677 if (!moov_recov_parse_mdia (moovrf, trakrd))
680 if (fseek (moovrf->file,
681 (long int) trakrd->mdia_file_offset + trakrd->mdia_size,
685 trakrd->extra_atoms_offset = ftell (moovrf->file);
686 trakrd->extra_atoms_size = size - (trakrd->extra_atoms_offset - offset);
688 trakrd->file_offset = offset;
689 /* position after the trak */
690 return fseek (moovrf->file, (long int) offset + size, SEEK_SET) == 0;
694 moov_recov_file_create (FILE * file, GError ** err)
697 MoovRecovFile *moovrf = g_new0 (MoovRecovFile, 1);
699 g_return_val_if_fail (file != NULL, NULL);
703 /* look for ftyp and prefix at the start */
704 if (!moov_recov_file_parse_prefix (moovrf)) {
705 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
706 "Error while parsing prefix atoms");
711 if (!moov_recov_file_parse_mvhd (moovrf)) {
712 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
713 "Error while parsing mvhd atom");
717 if (!moov_recov_parse_moov_timescale (moovrf)) {
718 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
719 "Error while parsing timescale");
722 if (!moov_recov_parse_num_traks (moovrf)) {
723 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
724 "Error while parsing parsing number of traks");
729 if (moovrf->num_traks > 1024) {
730 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
731 "Unsupported number of traks");
736 moovrf->traks_rd = g_new0 (TrakRecovData, moovrf->num_traks);
737 for (i = 0; i < moovrf->num_traks; i++) {
738 atom_stbl_init (&(moovrf->traks_rd[i].stbl));
740 for (i = 0; i < moovrf->num_traks; i++) {
741 if (!moov_recov_parse_trak (moovrf, &(moovrf->traks_rd[i]))) {
742 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
743 "Error while parsing trak atom");
751 moov_recov_file_free (moovrf);
756 moov_recov_file_free (MoovRecovFile * moovrf)
759 fclose (moovrf->file);
760 if (moovrf->traks_rd) {
761 for (i = 0; i < moovrf->num_traks; i++) {
762 atom_stbl_clear (&(moovrf->traks_rd[i].stbl));
764 g_free (moovrf->traks_rd);
770 moov_recov_parse_buffer_entry (MoovRecovFile * moovrf, TrakBufferEntryInfo * b)
772 guint8 data[TRAK_BUFFER_ENTRY_INFO_SIZE];
775 read = fread (data, 1, TRAK_BUFFER_ENTRY_INFO_SIZE, moovrf->file);
776 if (read != TRAK_BUFFER_ENTRY_INFO_SIZE)
779 b->track_id = GST_READ_UINT32_BE (data);
780 b->nsamples = GST_READ_UINT32_BE (data + 4);
781 b->delta = GST_READ_UINT32_BE (data + 8);
782 b->size = GST_READ_UINT32_BE (data + 12);
783 b->chunk_offset = GST_READ_UINT64_BE (data + 16);
784 b->sync = data[24] != 0;
785 b->do_pts = data[25] != 0;
786 b->pts_offset = GST_READ_UINT64_BE (data + 26);
791 mdat_recov_add_sample (MdatRecovFile * mdatrf, guint32 size)
793 /* test if this data exists */
794 if (mdatrf->mdat_size - mdatrf->mdat_header_size + size > mdatrf->data_size)
797 mdatrf->mdat_size += size;
801 static TrakRecovData *
802 moov_recov_get_trak (MoovRecovFile * moovrf, guint32 id)
805 for (i = 0; i < moovrf->num_traks; i++) {
806 if (moovrf->traks_rd[i].trak_id == id)
807 return &(moovrf->traks_rd[i]);
813 trak_recov_data_add_sample (TrakRecovData * trak, TrakBufferEntryInfo * b)
815 trak->duration += b->nsamples * b->delta;
816 atom_stbl_add_samples (&trak->stbl, b->nsamples, b->delta, b->size,
817 b->chunk_offset, b->sync, b->pts_offset);
821 * Parses the buffer entries in the MoovRecovFile and matches the inputs
822 * with the data in the MdatRecovFile. Whenever a buffer entry of that
823 * represents 'x' bytes of data, the same amount of data is 'validated' in
824 * the MdatRecovFile and will be inluded in the generated moovie file.
827 moov_recov_parse_buffers (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
830 TrakBufferEntryInfo entry;
833 /* we assume both moovrf and mdatrf are at the starting points of their
835 while (moov_recov_parse_buffer_entry (moovrf, &entry)) {
836 /* be sure we still have this data in mdat */
837 trak = moov_recov_get_trak (moovrf, entry.track_id);
839 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_PARSING,
840 "Invalid trak id found in buffer entry");
843 if (!mdat_recov_add_sample (mdatrf, entry.size))
845 trak_recov_data_add_sample (trak, &entry);
851 trak_recov_data_get_trak_atom_size (TrakRecovData * trak)
853 AtomSTBL *stbl = &trak->stbl;
856 /* write out our stbl child atoms */
859 if (!atom_stts_copy_data (&stbl->stts, NULL, NULL, &offset)) {
862 if (atom_array_get_len (&stbl->stss.entries) > 0) {
863 if (!atom_stss_copy_data (&stbl->stss, NULL, NULL, &offset)) {
867 if (!atom_stsc_copy_data (&stbl->stsc, NULL, NULL, &offset)) {
870 if (!atom_stsz_copy_data (&stbl->stsz, NULL, NULL, &offset)) {
874 if (!atom_ctts_copy_data (stbl->ctts, NULL, NULL, &offset)) {
878 if (!atom_stco64_copy_data (&stbl->stco64, NULL, NULL, &offset)) {
882 return trak->trak_size + ((trak->stsd_size + offset + 8) - trak->stbl_size);
889 moov_recov_get_stbl_children_data (MoovRecovFile * moovrf, TrakRecovData * trak,
892 AtomSTBL *stbl = &trak->stbl;
897 /* write out our stbl child atoms
899 * Use 1MB as a starting size, *_copy_data functions
900 * will grow the buffer if needed.
903 buffer = g_malloc0 (size);
906 if (!atom_stts_copy_data (&stbl->stts, &buffer, &size, &offset)) {
909 if (atom_array_get_len (&stbl->stss.entries) > 0) {
910 if (!atom_stss_copy_data (&stbl->stss, &buffer, &size, &offset)) {
914 if (!atom_stsc_copy_data (&stbl->stsc, &buffer, &size, &offset)) {
917 if (!atom_stsz_copy_data (&stbl->stsz, &buffer, &size, &offset)) {
921 if (!atom_ctts_copy_data (stbl->ctts, &buffer, &size, &offset)) {
925 if (!atom_stco64_copy_data (&stbl->stco64, &buffer, &size, &offset)) {
937 copy_data_from_file_to_file (FILE * from, guint position, guint size, FILE * to,
942 if (fseek (from, position, SEEK_SET) != 0)
944 data = g_malloc (size);
945 if (fread (data, 1, size, from) != size) {
948 if (fwrite (data, 1, size, to) != size) {
949 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
962 moov_recov_write_file (MoovRecovFile * moovrf, MdatRecovFile * mdatrf,
963 FILE * outf, GError ** err, GError ** warn)
967 guint8 *prefix_data = NULL;
968 guint8 *mvhd_data = NULL;
969 guint8 *trak_data = NULL;
970 guint32 moov_size = 0;
972 guint64 stbl_children_size = 0;
973 guint8 *stbl_children = NULL;
974 guint32 longest_duration = 0;
978 /* check the version */
979 if (fseek (moovrf->file, 0, SEEK_SET) != 0) {
980 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
981 "Failed to seek to the start of the moov recovery file");
984 if (fread (auxdata, 1, 2, moovrf->file) != 2) {
985 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
986 "Failed to read version from file");
989 version = GST_READ_UINT16_BE (auxdata);
990 if (version != ATOMS_RECOV_FILE_VERSION) {
991 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_VERSION,
992 "Input file version (%u) is not supported in this version (%u)",
993 version, ATOMS_RECOV_FILE_VERSION);
998 prefix_data = g_malloc (moovrf->prefix_size);
999 if (fread (prefix_data, 1, moovrf->prefix_size,
1000 moovrf->file) != moovrf->prefix_size) {
1001 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1002 "Failed to read the ftyp atom from file");
1005 if (fwrite (prefix_data, 1, moovrf->prefix_size, outf) != moovrf->prefix_size) {
1006 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1009 g_free (prefix_data);
1012 /* need to calculate the moov size beforehand to add the offset to
1013 * chunk offset entries */
1014 moov_size += moovrf->mvhd_size + 8; /* mvhd + moov size + fourcc */
1015 for (i = 0; i < moovrf->num_traks; i++) {
1016 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1017 guint32 duration; /* in moov's timescale */
1020 /* convert trak duration to moov's duration */
1021 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
1024 if (duration > longest_duration)
1025 longest_duration = duration;
1026 trak_size = trak_recov_data_get_trak_atom_size (trak);
1027 if (trak_size == 0) {
1028 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_GENERIC,
1029 "Failed to estimate trak atom size");
1032 moov_size += trak_size;
1035 /* add chunks offsets */
1036 for (i = 0; i < moovrf->num_traks; i++) {
1037 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1038 /* 8 or 16 for the mdat header */
1039 gint64 offset = moov_size + ftell (outf) + mdatrf->mdat_header_size;
1040 atom_stco64_chunks_set_offset (&trak->stbl.stco64, offset);
1043 /* write the moov */
1044 GST_WRITE_UINT32_BE (auxdata, moov_size);
1045 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_moov);
1046 if (fwrite (auxdata, 1, 8, outf) != 8) {
1047 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1051 /* write the mvhd */
1052 mvhd_data = g_malloc (moovrf->mvhd_size);
1053 if (fseek (moovrf->file, moovrf->mvhd_pos, SEEK_SET) != 0)
1055 if (fread (mvhd_data, 1, moovrf->mvhd_size,
1056 moovrf->file) != moovrf->mvhd_size)
1058 GST_WRITE_UINT32_BE (mvhd_data + 20, moovrf->timescale);
1059 GST_WRITE_UINT32_BE (mvhd_data + 24, longest_duration);
1060 if (fwrite (mvhd_data, 1, moovrf->mvhd_size, outf) != moovrf->mvhd_size) {
1061 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1067 /* write the traks, this is the tough part because we need to update:
1069 * - sizes of atoms from stbl to trak
1072 for (i = 0; i < moovrf->num_traks; i++) {
1073 TrakRecovData *trak = &(moovrf->traks_rd[i]);
1074 guint trak_data_size;
1075 guint32 stbl_new_size;
1076 guint32 minf_new_size;
1077 guint32 mdia_new_size;
1078 guint32 trak_new_size;
1080 guint32 duration; /* in moov's timescale */
1082 /* convert trak duration to moov's duration */
1083 duration = gst_util_uint64_scale_round (trak->duration, moovrf->timescale,
1086 stbl_children = moov_recov_get_stbl_children_data (moovrf, trak,
1087 &stbl_children_size);
1088 if (stbl_children == NULL)
1091 /* calc the new size of the atoms from stbl to trak in the atoms tree */
1092 stbl_new_size = trak->stsd_size + stbl_children_size + 8;
1093 size_diff = stbl_new_size - trak->stbl_size;
1094 minf_new_size = trak->minf_size + size_diff;
1095 mdia_new_size = trak->mdia_size + size_diff;
1096 trak_new_size = trak->trak_size + size_diff;
1098 if (fseek (moovrf->file, trak->file_offset, SEEK_SET) != 0)
1100 trak_data_size = trak->post_stsd_offset - trak->file_offset;
1101 trak_data = g_malloc (trak_data_size);
1102 if (fread (trak_data, 1, trak_data_size, moovrf->file) != trak_data_size) {
1105 /* update the size values in those read atoms before writing */
1106 GST_WRITE_UINT32_BE (trak_data, trak_new_size);
1107 GST_WRITE_UINT32_BE (trak_data + (trak->mdia_file_offset -
1108 trak->file_offset), mdia_new_size);
1109 GST_WRITE_UINT32_BE (trak_data + (trak->minf_file_offset -
1110 trak->file_offset), minf_new_size);
1111 GST_WRITE_UINT32_BE (trak_data + (trak->stbl_file_offset -
1112 trak->file_offset), stbl_new_size);
1114 /* update duration values in tkhd and mdhd */
1115 GST_WRITE_UINT32_BE (trak_data + (trak->tkhd_file_offset -
1116 trak->file_offset) + 28, duration);
1117 GST_WRITE_UINT32_BE (trak_data + (trak->mdhd_file_offset -
1118 trak->file_offset) + 24, trak->duration);
1120 if (fwrite (trak_data, 1, trak_data_size, outf) != trak_data_size) {
1121 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1124 if (fwrite (stbl_children, 1, stbl_children_size, outf) !=
1125 stbl_children_size) {
1126 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1132 g_free (stbl_children);
1133 stbl_children = NULL;
1135 /* Copy the extra atoms after 'minf' */
1136 if (!copy_data_from_file_to_file (moovrf->file, trak->extra_atoms_offset,
1137 trak->extra_atoms_size, outf, err))
1141 /* write the mdat */
1142 /* write the header first */
1143 if (mdatrf->mdat_header_size == 16) {
1144 GST_WRITE_UINT32_BE (auxdata, 1);
1145 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_mdat);
1146 GST_WRITE_UINT64_BE (auxdata + 8, mdatrf->mdat_size);
1147 } else if (mdatrf->mdat_header_size == 8) {
1148 GST_WRITE_UINT32_BE (auxdata, mdatrf->mdat_size);
1149 GST_WRITE_UINT32_LE (auxdata + 4, FOURCC_mdat);
1151 GST_ERROR ("Unexpected atom size: %u", mdatrf->mdat_header_size);
1152 g_assert_not_reached ();
1156 if (fwrite (auxdata, 1, mdatrf->mdat_header_size,
1157 outf) != mdatrf->mdat_header_size) {
1158 ATOMS_RECOV_OUTPUT_WRITE_ERROR (err);
1162 /* now read the mdat data and output to the file */
1163 if (fseek (mdatrf->file, mdatrf->mdat_start +
1164 (mdatrf->rawfile ? 0 : mdatrf->mdat_header_size), SEEK_SET) != 0)
1167 remaining = mdatrf->mdat_size - mdatrf->mdat_header_size;
1168 data = g_malloc (MAX_CHUNK_SIZE);
1169 while (!feof (mdatrf->file) && remaining > 0) {
1170 gint read, write, readsize;
1172 readsize = MIN (MAX_CHUNK_SIZE, remaining);
1174 read = fread (data, 1, readsize, mdatrf->file);
1175 write = fwrite (data, 1, read, outf);
1178 if (write != read) {
1179 g_set_error (err, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1180 "Failed to copy data to output file: %s", g_strerror (errno));
1187 g_set_error (warn, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1188 "Samples in recovery file were not present on headers."
1189 " Bytes lost: %u", remaining);
1190 } else if (!feof (mdatrf->file)) {
1191 g_set_error (warn, ATOMS_RECOV_QUARK, ATOMS_RECOV_ERR_FILE,
1192 "Samples in headers were not found in data file.");
1193 GST_FIXME ("Rewrite mdat size if we reach this to make the file"
1200 g_free (stbl_children);
1202 g_free (prefix_data);