movenc: Allow interleaving samples when writing fragmented files
[libav.git] / libavformat / movenc.h
1 /*
2 * MOV, 3GP, MP4 muxer
3 * Copyright (c) 2003 Thomas Raivio
4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6 *
7 * This file is part of Libav.
8 *
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef AVFORMAT_MOVENC_H
25 #define AVFORMAT_MOVENC_H
26
27 #include "avformat.h"
28
29 #define MOV_FRAG_INFO_ALLOC_INCREMENT 64
30 #define MOV_INDEX_CLUSTER_SIZE 1024
31 #define MOV_TIMESCALE 1000
32
33 #define RTP_MAX_PACKET_SIZE 1450
34
35 #define MODE_MP4 0x01
36 #define MODE_MOV 0x02
37 #define MODE_3GP 0x04
38 #define MODE_PSP 0x08 // example working PSP command line:
39 // avconv -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
40 #define MODE_3G2 0x10
41 #define MODE_IPOD 0x20
42 #define MODE_ISM 0x40
43 #define MODE_F4V 0x80
44
45 typedef struct MOVIentry {
46 uint64_t pos;
47 int64_t dts;
48 unsigned int size;
49 unsigned int samples_in_chunk;
50 unsigned int entries;
51 int cts;
52 #define MOV_SYNC_SAMPLE 0x0001
53 #define MOV_PARTIAL_SYNC_SAMPLE 0x0002
54 uint32_t flags;
55 } MOVIentry;
56
57 typedef struct HintSample {
58 uint8_t *data;
59 int size;
60 int sample_number;
61 int offset;
62 int own_data;
63 } HintSample;
64
65 typedef struct HintSampleQueue {
66 int size;
67 int len;
68 HintSample *samples;
69 } HintSampleQueue;
70
71 typedef struct MOVFragmentInfo {
72 int64_t offset;
73 int64_t time;
74 int64_t duration;
75 int64_t tfrf_offset;
76 int size;
77 } MOVFragmentInfo;
78
79 typedef struct MOVTrack {
80 int mode;
81 int entry;
82 unsigned timescale;
83 uint64_t time;
84 int64_t track_duration;
85 long sample_count;
86 long sample_size;
87 int has_keyframes;
88 #define MOV_TRACK_CTTS 0x0001
89 #define MOV_TRACK_STPS 0x0002
90 #define MOV_TRACK_ENABLED 0x0004
91 uint32_t flags;
92 int language;
93 int track_id;
94 int tag; ///< stsd fourcc
95 AVStream *st;
96 AVCodecContext *enc;
97
98 int vos_len;
99 uint8_t *vos_data;
100 MOVIentry *cluster;
101 unsigned cluster_capacity;
102 int audio_vbr;
103 int height; ///< active picture (w/o VBI) height for D-10/IMX
104 uint32_t tref_tag;
105 int tref_id; ///< trackID of the referenced track
106 int64_t start_dts;
107 int64_t start_cts;
108
109 int hint_track; ///< the track that hints this track, -1 if no hint track is set
110 int src_track; ///< the track that this hint track describes
111 AVFormatContext *rtp_ctx; ///< the format context for the hinting rtp muxer
112 uint32_t prev_rtp_ts;
113 int64_t cur_rtp_ts_unwrapped;
114 uint32_t max_packet_size;
115
116 int64_t default_duration;
117 uint32_t default_sample_flags;
118 uint32_t default_size;
119
120 HintSampleQueue sample_queue;
121
122 AVIOContext *mdat_buf;
123 int64_t data_offset;
124 int64_t frag_start;
125 int frag_discont;
126 int entries_flushed;
127
128 int nb_frag_info;
129 MOVFragmentInfo *frag_info;
130 unsigned frag_info_capacity;
131
132 struct {
133 int first_packet_seq;
134 int first_packet_entry;
135 int first_packet_seen;
136 int first_frag_written;
137 int packet_seq;
138 int packet_entry;
139 int slices;
140 } vc1_info;
141 } MOVTrack;
142
143 typedef struct MOVMuxContext {
144 const AVClass *av_class;
145 int mode;
146 int64_t time;
147 int nb_streams;
148 int chapter_track; ///< qt chapter track number
149 int64_t mdat_pos;
150 uint64_t mdat_size;
151 MOVTrack *tracks;
152
153 int flags;
154 int rtp_flags;
155 int iods_skip;
156 int iods_video_profile;
157 int iods_audio_profile;
158
159 int moov_written;
160 int fragments;
161 int max_fragment_duration;
162 int min_fragment_duration;
163 int max_fragment_size;
164 int ism_lookahead;
165 AVIOContext *mdat_buf;
166 int first_trun;
167
168 int64_t reserved_moov_pos;
169
170 char *major_brand;
171
172 int per_stream_grouping;
173 AVFormatContext *fc;
174
175 int use_editlist;
176 int frag_interleave;
177 } MOVMuxContext;
178
179 #define FF_MOV_FLAG_RTP_HINT (1 << 0)
180 #define FF_MOV_FLAG_FRAGMENT (1 << 1)
181 #define FF_MOV_FLAG_EMPTY_MOOV (1 << 2)
182 #define FF_MOV_FLAG_FRAG_KEYFRAME (1 << 3)
183 #define FF_MOV_FLAG_SEPARATE_MOOF (1 << 4)
184 #define FF_MOV_FLAG_FRAG_CUSTOM (1 << 5)
185 #define FF_MOV_FLAG_ISML (1 << 6)
186 #define FF_MOV_FLAG_FASTSTART (1 << 7)
187 #define FF_MOV_FLAG_OMIT_TFHD_OFFSET (1 << 8)
188 #define FF_MOV_FLAG_DISABLE_CHPL (1 << 9)
189 #define FF_MOV_FLAG_DEFAULT_BASE_MOOF (1 << 10)
190 #define FF_MOV_FLAG_DASH (1 << 11)
191 #define FF_MOV_FLAG_FRAG_DISCONT (1 << 12)
192 #define FF_MOV_FLAG_DELAY_MOOV (1 << 13)
193
194 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);
195
196 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index);
197 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
198 int track_index, int sample,
199 uint8_t *sample_data, int sample_size);
200 void ff_mov_close_hinting(MOVTrack *track);
201
202 #endif /* AVFORMAT_MOVENC_H */