atomsrecovery: read from mdat only what is on headers
[platform/upstream/gst-plugins-good.git] / gst / isomp4 / atomsrecovery.h
1 /* Quicktime muxer plugin for GStreamer
2  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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 #ifndef __ATOMS_RECOVERY_H__
44 #define __ATOMS_RECOVERY_H__
45
46 #include <glib.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <gst/gst.h>
50
51 #include "atoms.h"
52
53 /* Version to be incremented each time we decide
54  * to change the file layout */
55 #define ATOMS_RECOV_FILE_VERSION          1
56
57 #define ATOMS_RECOV_QUARK (g_quark_from_string ("qtmux-atoms-recovery"))
58
59 /* gerror error codes */
60 #define ATOMS_RECOV_ERR_GENERIC           1
61 #define ATOMS_RECOV_ERR_FILE              2
62 #define ATOMS_RECOV_ERR_PARSING           3
63 #define ATOMS_RECOV_ERR_VERSION           4
64
65 /* this struct represents each buffer in a moov file, containing the info
66  * that is placed in the stsd children atoms
67  * Fields should be writen in BE order, and booleans should be writen as
68  * 1byte with 0 for false, anything otherwise */
69 #define TRAK_BUFFER_ENTRY_INFO_SIZE 34
70 typedef struct
71 {
72   guint32   track_id;
73   guint32   nsamples;
74   guint32   delta;
75   guint32   size;
76   guint64   chunk_offset;
77   guint64   pts_offset;
78   gboolean  sync;
79   gboolean  do_pts;
80 } TrakBufferEntryInfo;
81
82 typedef struct
83 {
84   guint32 trak_id;
85   guint32 duration;  /* duration in trak timescale */
86   guint32 timescale; /* trak's timescale */
87
88   guint64 file_offset;
89
90   /* need for later updating duration */
91   guint64 tkhd_file_offset;
92   guint64 mdhd_file_offset;
93
94   /* need these offsets to update size */
95   guint32 trak_size;
96   guint64 mdia_file_offset;
97   guint32 mdia_size;
98   guint64 minf_file_offset;
99   guint32 minf_size;
100   guint64 stbl_file_offset;
101   guint32 stbl_size;
102
103   guint64 post_stsd_offset;
104   guint32 stsd_size;
105
106   guint32 extra_atoms_size;
107   guint32 extra_atoms_offset;
108
109   /* for storing the samples info */
110   AtomSTBL stbl;
111 } TrakRecovData;
112
113 typedef struct
114 {
115   FILE * file;
116   gboolean rawfile;
117
118   /* results from parsing the input file */
119   guint64   data_size;
120   guint32   mdat_header_size;
121   guint     mdat_start;
122
123   guint64   mdat_size;
124 } MdatRecovFile;
125
126 typedef struct
127 {
128   FILE * file;
129   guint32 timescale;
130
131   guint32 mvhd_pos;
132   guint32 mvhd_size;
133   guint32 prefix_size; /* prefix + ftyp total size */
134
135   gint num_traks;
136   TrakRecovData *traks_rd;
137 } MoovRecovFile;
138
139 gboolean atoms_recov_write_trak_info      (FILE * f, AtomTRAK * trak);
140 gboolean atoms_recov_write_headers        (FILE * f, AtomFTYP * ftyp,
141                                            GstBuffer * prefix, AtomMOOV * moov,
142                                            guint32 timescale,
143                                            guint32 traks_number);
144 gboolean atoms_recov_write_trak_samples   (FILE * f, AtomTRAK * trak,
145                                            guint32 nsamples, guint32 delta,
146                                            guint32 size, guint64 chunk_offset,
147                                            gboolean sync, gboolean do_pts,
148                                            gint64 pts_offset);
149
150 MdatRecovFile * mdat_recov_file_create   (FILE * file, gboolean datafile,
151                                           GError ** err);
152 void            mdat_recov_file_free     (MdatRecovFile * mrf);
153 MoovRecovFile * moov_recov_file_create   (FILE * file, GError ** err);
154 void            moov_recov_file_free     (MoovRecovFile * moovrf);
155 gboolean        moov_recov_parse_buffers (MoovRecovFile * moovrf,
156                                           MdatRecovFile * mdatrf,
157                                           GError ** err);
158 gboolean        moov_recov_write_file    (MoovRecovFile * moovrf,
159                                           MdatRecovFile * mdatrf, FILE * outf,
160                                           GError ** err, GError ** warn);
161
162 #endif /* __ATOMS_RECOVERY_H__ */