1 /*
2 * AMV muxer
3 *
4 * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
29
30 /*
31 * Things to note:
32 * - AMV is a hard-coded (and broken) subset of AVI. It's not worth sullying the
33 * existing AVI muxer with its filth.
34 * - No separate demuxer as the existing AVI demuxer can handle these.
35 * - The sizes of certain tags are deliberately set to 0 as some players break
36 * when they're set correctly. Ditto with some header fields.
37 * - There is no index.
38 * - Players are **very** sensitive to the frame order and sizes.
39 * - Frames must be strictly interleaved as V-A, any V-V or A-A will
40 * cause crashes.
41 * - Variable video frame sizes seem to be handled fine.
42 * - Variable audio frame sizes cause crashes.
43 * - If audio is shorter than video, it's padded with silence.
44 * - If video is shorter than audio, the most recent frame is repeated.
45 */
46
47 #define AMV_STREAM_COUNT 2
48 #define AMV_STREAM_VIDEO 0
49 #define AMV_STREAM_AUDIO 1
50 #define AMV_VIDEO_STRH_SIZE 56
51 #define AMV_VIDEO_STRF_SIZE 36
52 #define AMV_AUDIO_STRH_SIZE 48
53 #define AMV_AUDIO_STRF_SIZE 20 /* sizeof(WAVEFORMATEX) + 2 */
54
56 {
61
63
66 AVPacket *
apad;
/* Dummy audio packet for padding; not owned by us. */
68
69 /*
70 * Cumulative PTS values for each stream, used for the final
71 * duration calculcation.
72 */
75
76 /* ff_{start,end}_tag(), but sets the size to 0. */
78 {
82 }
83
85 {
88
92 }
93
95 {
99
101
105 }
106
109
114 }
115
120 }
121
122 /* These files are broken-enough as they are. They shouldn't be streamed. */
126 }
127
131
135
136 /*
137 * Bail if the framerate's too high. Prevents the audio frame size from
138 * getting too small. 63fps is the closest value to 60fps that divides
139 * cleanly, so cap it there.
140 */
144 }
145
146 /*
147 * frame_size will be set if coming from the encoder.
148 * Make sure the its been configured correctly. The audio frame duration
149 * needs to match that of the video.
150 */
154
159 }
160
165 }
166
170 }
171
174 "Please change video frame rate. Suggested rates: 10,14,15,18,21,25,30\n");
176 }
177 } else {
178 /* If remuxing from the same source, then this will match the video. */
183 }
184 }
185
186 /* Allocate and fill dummy packet so we can pad the audio. */
190 }
191
195
199 }
202 return 0;
203 }
204
206 {
208
210 }
211
213 {
215
217
223
227
229 }
230
232 {
236
238
244
245 /* Bodge an (incorrect) WAVEFORMATEX (+2 pad bytes) */
257
259 }
260
262 {
267 uint8_t amvh[56] = {0};
268 int64_t list1;
269
274
277
284 AV_WL32(amvh + 52, 0);
/* duration, filled in later. */
285
288
292
295 return 0;
296 }
297
299 {
301
306 else
308
310 /* Can happen when remuxing files produced by another encoder. */
313 }
314
317
320 return 0;
321 }
322
324 {
327
329 return 0;
330
331 stream_index = (stream_index + 1) %
s->nb_streams;
336 else
338
340 }
341
343 {
346
347 /* Add a dummy frame if we've received two of the same index. */
350
353
355 /* Save the last packet for padding. */
359 }
360
361 return 0;
362 }
363
365 {
371
372 /* Pad-out one last audio frame if needed. */
376 }
377
380
383
386
387 /* Go back and write the duration. */
391 );
392
395 hh = mm / 60;
397 mm %= 60;
398
402 return 0;
403 }
404
408 .p.mime_type = "video/amv",
409 .p.extensions = "amv",
418 };