• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "movenccenc.h"
29 #include "libavcodec/packet_internal.h"
30 
31 #define MOV_FRAG_INFO_ALLOC_INCREMENT 64
32 #define MOV_INDEX_CLUSTER_SIZE 1024
33 #define MOV_TIMESCALE 1000
34 
35 #define RTP_MAX_PACKET_SIZE 1450
36 
37 #define MODE_MP4  0x01
38 #define MODE_MOV  0x02
39 #define MODE_3GP  0x04
40 #define MODE_PSP  0x08 // example working PSP command line:
41 // ffmpeg -i testinput.avi  -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
42 #define MODE_3G2  0x10
43 #define MODE_IPOD 0x20
44 #define MODE_ISM  0x40
45 #define MODE_F4V  0x80
46 #define MODE_AVIF 0x100
47 
48 typedef struct MOVIentry {
49     uint64_t     pos;
50     int64_t      dts;
51     int64_t      pts;
52     unsigned int size;
53     unsigned int samples_in_chunk;
54     unsigned int chunkNum;              ///< Chunk number if the current entry is a chunk start otherwise 0
55     unsigned int entries;
56     int          cts;
57 #define MOV_SYNC_SAMPLE         0x0001
58 #define MOV_PARTIAL_SYNC_SAMPLE 0x0002
59 #define MOV_DISPOSABLE_SAMPLE   0x0004
60 #define MOV_DISPOSABLE_EXT_SAMPLE 0x0008
61     uint32_t     flags;
62     AVProducerReferenceTime prft;
63 } MOVIentry;
64 
65 typedef struct HintSample {
66     const uint8_t *data;
67     int size;
68     int sample_number;
69     int offset;
70     int own_data;
71 } HintSample;
72 
73 typedef struct HintSampleQueue {
74     int size;
75     int len;
76     HintSample *samples;
77 } HintSampleQueue;
78 
79 typedef struct MOVFragmentInfo {
80     int64_t offset;
81     int64_t time;
82     int64_t duration;
83     int64_t tfrf_offset;
84     int size;
85 } MOVFragmentInfo;
86 
87 typedef struct MOVTrack {
88     int         mode;
89     int         entry;
90     unsigned    timescale;
91     uint64_t    time;
92     int64_t     track_duration;
93     int         last_sample_is_subtitle_end;
94     long        sample_count;
95     long        sample_size;
96     long        chunkCount;
97     int         has_keyframes;
98     int         has_disposable;
99 #define MOV_TRACK_CTTS         0x0001
100 #define MOV_TRACK_STPS         0x0002
101 #define MOV_TRACK_ENABLED      0x0004
102     uint32_t    flags;
103 #define MOV_TIMECODE_FLAG_DROPFRAME     0x0001
104 #define MOV_TIMECODE_FLAG_24HOURSMAX    0x0002
105 #define MOV_TIMECODE_FLAG_ALLOWNEGATIVE 0x0004
106     uint32_t    timecode_flags;
107     int         language;
108     int         track_id;
109     int         tag; ///< stsd fourcc
110     AVStream        *st;
111     AVCodecParameters *par;
112     int mono_as_fc;
113     int multichannel_as_mono;
114 
115     int         vos_len;
116     uint8_t     *vos_data;
117     MOVIentry   *cluster;
118     unsigned    cluster_capacity;
119     int         audio_vbr;
120     int         height; ///< active picture (w/o VBI) height for D-10/IMX
121     uint32_t    tref_tag;
122 #ifdef OHOS_TIMED_META_TRACK
123     int         ref_track_count;
124     int         *tref_ids; ///< trackIDs of the referenced tracks
125 #else
126     int         tref_id; ///< trackID of the referenced track
127 #endif
128     int64_t     start_dts;
129     int64_t     start_cts;
130     int64_t     end_pts;
131     int         end_reliable;
132     int64_t     dts_shift;
133 
134     int         hint_track;   ///< the track that hints this track, -1 if no hint track is set
135     int         src_track;    ///< the track that this hint (or tmcd) track describes
136     AVFormatContext *rtp_ctx; ///< the format context for the hinting rtp muxer
137     uint32_t    prev_rtp_ts;
138     int64_t     cur_rtp_ts_unwrapped;
139     uint32_t    max_packet_size;
140 
141     int64_t     default_duration;
142     uint32_t    default_sample_flags;
143     uint32_t    default_size;
144 
145     HintSampleQueue sample_queue;
146     AVPacket *cover_image;
147 
148     AVIOContext *mdat_buf;
149     int64_t     data_offset;
150     int         frag_discont;
151     int         entries_flushed;
152 
153     int         nb_frag_info;
154     MOVFragmentInfo *frag_info;
155     unsigned    frag_info_capacity;
156 
157     struct {
158         int     first_packet_seq;
159         int     first_packet_entry;
160         int     first_packet_seen;
161         int     first_frag_written;
162         int     packet_seq;
163         int     packet_entry;
164         int     slices;
165     } vc1_info;
166 
167     void       *eac3_priv;
168 
169     MOVMuxCencContext cenc;
170 
171     uint32_t palette[AVPALETTE_COUNT];
172     int pal_done;
173 
174     int is_unaligned_qt_rgb;
175 
176     unsigned int squash_fragment_samples_to_one; //< flag to note formats where all samples for a fragment are to be squashed
177 
178     PacketList squashed_packet_queue;
179 } MOVTrack;
180 
181 typedef enum {
182     MOV_ENC_NONE = 0,
183     MOV_ENC_CENC_AES_CTR,
184 } MOVEncryptionScheme;
185 
186 typedef enum {
187     MOV_PRFT_NONE = 0,
188     MOV_PRFT_SRC_WALLCLOCK,
189     MOV_PRFT_SRC_PTS,
190     MOV_PRFT_NB
191 } MOVPrftBox;
192 
193 typedef struct MOVMuxContext {
194     const AVClass *av_class;
195     int     mode;
196     int64_t time;
197     int     nb_streams;
198     int     nb_meta_tmcd;  ///< number of new created tmcd track based on metadata (aka not data copy)
199     int     chapter_track; ///< qt chapter track number
200 #ifdef OHOS_TIMED_META_TRACK
201     int     nb_timed_metadata_track; ///< number of timed metadata track
202 #endif
203     int64_t mdat_pos;
204     uint64_t mdat_size;
205     MOVTrack *tracks;
206 
207     int flags;
208     int rtp_flags;
209 
210     int iods_skip;
211     int iods_video_profile;
212     int iods_audio_profile;
213 
214     int moov_written;
215     int fragments;
216     int max_fragment_duration;
217     int min_fragment_duration;
218     int max_fragment_size;
219     int ism_lookahead;
220     AVIOContext *mdat_buf;
221     int first_trun;
222 
223     int video_track_timescale;
224 
225     int reserved_moov_size; ///< 0 for disabled, -1 for automatic, size otherwise
226     int64_t reserved_header_pos;
227 
228     char *major_brand;
229 
230     int per_stream_grouping;
231     AVFormatContext *fc;
232 
233     AVPacket *pkt;
234 
235     int use_editlist;
236     float gamma;
237 
238     int frag_interleave;
239     int missing_duration_warned;
240 
241     char *encryption_scheme_str;
242     MOVEncryptionScheme encryption_scheme;
243     uint8_t *encryption_key;
244     int encryption_key_len;
245     uint8_t *encryption_kid;
246     int encryption_kid_len;
247 
248     int need_rewrite_extradata;
249 
250     int use_stream_ids_as_track_ids;
251     int track_ids_ok;
252     int write_btrt;
253     int write_tmcd;
254     MOVPrftBox write_prft;
255     int empty_hdlr_name;
256     int movie_timescale;
257 
258     int64_t avif_extent_pos[2];  // index 0 is YUV and 1 is Alpha.
259     int avif_extent_length[2];   // index 0 is YUV and 1 is Alpha.
260     int is_animated_avif;
261 } MOVMuxContext;
262 
263 #define FF_MOV_FLAG_RTP_HINT              (1 <<  0)
264 #define FF_MOV_FLAG_FRAGMENT              (1 <<  1)
265 #define FF_MOV_FLAG_EMPTY_MOOV            (1 <<  2)
266 #define FF_MOV_FLAG_FRAG_KEYFRAME         (1 <<  3)
267 #define FF_MOV_FLAG_SEPARATE_MOOF         (1 <<  4)
268 #define FF_MOV_FLAG_FRAG_CUSTOM           (1 <<  5)
269 #define FF_MOV_FLAG_ISML                  (1 <<  6)
270 #define FF_MOV_FLAG_FASTSTART             (1 <<  7)
271 #define FF_MOV_FLAG_OMIT_TFHD_OFFSET      (1 <<  8)
272 #define FF_MOV_FLAG_DISABLE_CHPL          (1 <<  9)
273 #define FF_MOV_FLAG_DEFAULT_BASE_MOOF     (1 << 10)
274 #define FF_MOV_FLAG_DASH                  (1 << 11)
275 #define FF_MOV_FLAG_FRAG_DISCONT          (1 << 12)
276 #define FF_MOV_FLAG_DELAY_MOOV            (1 << 13)
277 #define FF_MOV_FLAG_GLOBAL_SIDX           (1 << 14)
278 #define FF_MOV_FLAG_WRITE_COLR            (1 << 15)
279 #define FF_MOV_FLAG_WRITE_GAMA            (1 << 16)
280 #define FF_MOV_FLAG_USE_MDTA              (1 << 17)
281 #define FF_MOV_FLAG_SKIP_TRAILER          (1 << 18)
282 #define FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS  (1 << 19)
283 #define FF_MOV_FLAG_FRAG_EVERY_FRAME      (1 << 20)
284 #define FF_MOV_FLAG_SKIP_SIDX             (1 << 21)
285 #define FF_MOV_FLAG_CMAF                  (1 << 22)
286 #define FF_MOV_FLAG_PREFER_ICC            (1 << 23)
287 #ifdef OHOS_TIMED_META_TRACK
288 #define FF_MOV_FLAG_TIMED_METADATA        (1 << 24)
289 #endif
290 
291 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);
292 
293 int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index);
294 int ff_mov_add_hinted_packet(AVFormatContext *s, AVPacket *pkt,
295                              int track_index, int sample,
296                              uint8_t *sample_data, int sample_size);
297 void ff_mov_close_hinting(MOVTrack *track);
298 
299 #endif /* AVFORMAT_MOVENC_H */
300