]> git.sesse.net Git - ffmpeg/blob - libavformat/amvenc.c
avformat/rtsp: av_rescale -> av_rescale_q
[ffmpeg] / libavformat / amvenc.c
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  */
22 #include "avformat.h"
23 #include "riff.h"
24 #include "internal.h"
25 #include "avio_internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/avassert.h"
28
29 /*
30  * Things to note:
31  * - AMV is a hard-coded (and broken) subset of AVI. It's not worth sullying the
32  *   existing AVI muxer with its filth.
33  * - No separate demuxer as the existing AVI demuxer can handle these.
34  * - The sizes of certain tags are deliberately set to 0 as some players break
35  *   when they're set correctly. Ditto with some header fields.
36  * - There is no index.
37  * - Players are **very** sensitive to the frame order and sizes.
38  *   - Frames must be strictly interleaved as V-A, any V-V or A-A will
39  *     cause crashes.
40  *   - Variable video frame sizes seem to be handled fine.
41  *   - Variable audio frame sizes cause crashes.
42  *   - If audio is shorter than video, it's padded with silence.
43  *   - If video is shorter than audio, the most recent frame is repeated.
44  */
45
46 #define AMV_STREAM_COUNT     2
47 #define AMV_STREAM_VIDEO     0
48 #define AMV_STREAM_AUDIO     1
49 #define AMV_VIDEO_STRH_SIZE 56
50 #define AMV_VIDEO_STRF_SIZE 36
51 #define AMV_AUDIO_STRH_SIZE 48
52 #define AMV_AUDIO_STRF_SIZE 20 /* sizeof(WAVEFORMATEX) + 2 */
53
54 typedef struct AMVContext
55 {
56     int64_t riff_start;
57     int64_t movi_list;
58     int64_t offset_duration;
59     int     last_stream;
60
61     int32_t us_per_frame; /* Microseconds per frame.         */
62
63     int32_t aframe_size;  /* Expected audio frame size.      */
64     int32_t ablock_align; /* Expected audio block align.     */
65     AVPacket apad;        /* Dummy audio packet for padding. */
66     AVPacket vpad;        /* Most recent video frame, for padding. */
67
68     /*
69      * Cumulative PTS values for each stream, used for the final
70      * duration calculcation.
71      */
72     int64_t lastpts[AMV_STREAM_COUNT];
73 } AMVContext;
74
75 /* ff_{start,end}_tag(), but sets the size to 0. */
76 static int64_t amv_start_tag(AVIOContext *pb, const char *tag)
77 {
78     ffio_wfourcc(pb, tag);
79     avio_wl32(pb, 0);
80     return avio_tell(pb);
81 }
82
83 static void amv_end_tag(AVIOContext *pb, int64_t start)
84 {
85     int64_t pos;
86     av_assert0((start&1) == 0);
87
88     pos = avio_tell(pb);
89     if (pos & 1)
90         avio_w8(pb, 0);
91 }
92
93 static av_cold int amv_init(AVFormatContext *s)
94 {
95     AMVContext *amv = s->priv_data;
96     AVStream   *vst, *ast;
97     int ret;
98
99     amv->last_stream  = -1;
100
101     if (s->nb_streams != AMV_STREAM_COUNT) {
102         av_log(s, AV_LOG_ERROR, "AMV files only support 2 streams\n");
103         return AVERROR(EINVAL);
104     }
105
106     vst = s->streams[AMV_STREAM_VIDEO];
107     ast = s->streams[AMV_STREAM_AUDIO];
108
109     if (vst->codecpar->codec_id != AV_CODEC_ID_AMV) {
110         av_log(s, AV_LOG_ERROR, "First AMV stream must be %s\n",
111                 avcodec_get_name(AV_CODEC_ID_AMV));
112         return AVERROR(EINVAL);
113     }
114
115     if (ast->codecpar->codec_id != AV_CODEC_ID_ADPCM_IMA_AMV) {
116         av_log(s, AV_LOG_ERROR, "Second AMV stream must be %s\n",
117                 avcodec_get_name(AV_CODEC_ID_ADPCM_IMA_AMV));
118         return AVERROR(EINVAL);
119     }
120
121     /* These files are broken-enough as they are. They shouldn't be streamed. */
122     if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
123         av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
124         return AVERROR(EINVAL);
125     }
126
127     amv->us_per_frame = av_rescale(AV_TIME_BASE, vst->time_base.num, vst->time_base.den);
128     amv->aframe_size  = av_rescale(ast->codecpar->sample_rate, amv->us_per_frame, AV_TIME_BASE);
129     amv->ablock_align = 8 + (FFALIGN(amv->aframe_size, 2) / 2);
130
131     av_log(s, AV_LOG_TRACE, "us_per_frame = %d\n", amv->us_per_frame);
132     av_log(s, AV_LOG_TRACE, "aframe_size  = %d\n", amv->aframe_size);
133     av_log(s, AV_LOG_TRACE, "ablock_align = %d\n", amv->ablock_align);
134
135     /*
136      * Bail if the framerate's too high. Prevents the audio frame size from
137      * getting too small. 63fps is the closest value to 60fps that divides
138      * cleanly, so cap it there.
139      */
140     if (amv->us_per_frame < 15873) {
141         av_log(s, AV_LOG_ERROR, "Refusing to mux >63fps video\n");
142         return AVERROR(EINVAL);
143     }
144
145     /*
146      * frame_size will be set if coming from the encoder.
147      * Make sure the its been configured correctly. The audio frame duration
148      * needs to match that of the video.
149      */
150     if (ast->codecpar->frame_size) {
151         AVCodecParameters *par = ast->codecpar;
152         int bad = 0;
153
154         if (par->frame_size != amv->aframe_size) {
155             av_log(s, AV_LOG_ERROR, "Invalid audio frame size. Got %d, wanted %d\n",
156                    par->frame_size, amv->aframe_size);
157             bad = 1;
158         }
159
160         if (par->block_align != amv->ablock_align) {
161             av_log(s, AV_LOG_ERROR, "Invalid audio block align. Got %d, wanted %d\n",
162                    par->block_align, amv->ablock_align);
163             bad = 1;
164         }
165
166         if (bad) {
167             av_log(s, AV_LOG_ERROR, "Try -block_size %d\n", amv->aframe_size);
168             return AVERROR(EINVAL);
169         }
170
171         if (ast->codecpar->sample_rate % amv->aframe_size) {
172             av_log(s, AV_LOG_ERROR, "Audio sample rate not a multiple of the frame size.\n"
173                 "Please change video frame rate. Suggested rates: 10,14,15,18,21,25,30\n");
174             return AVERROR(EINVAL);
175         }
176     } else {
177         /* If remuxing from the same source, then this will match the video. */
178         int32_t aus = av_rescale(AV_TIME_BASE, ast->time_base.num, ast->time_base.den);
179         if (aus != amv->us_per_frame) {
180             av_log(s, AV_LOG_ERROR, "Cannot remux streams with a different time base\n");
181             return AVERROR(EINVAL);
182         }
183     }
184
185     /* Allocate and fill dummy packet so we can pad the audio. */
186     if ((ret = av_new_packet(&amv->apad, amv->ablock_align)) < 0)
187         return ret;
188
189     amv->apad.stream_index = AMV_STREAM_AUDIO;
190     memset(amv->apad.data, 0, amv->ablock_align);
191     AV_WL32(amv->apad.data + 4, amv->aframe_size);
192
193     av_init_packet(&amv->vpad);
194     amv->vpad.stream_index = AMV_STREAM_VIDEO;
195     amv->vpad.duration     = 1;
196     return 0;
197 }
198
199 static void amv_deinit(AVFormatContext *s)
200 {
201     AMVContext *amv = s->priv_data;
202
203     av_packet_unref(&amv->apad);
204     av_packet_unref(&amv->vpad);
205 }
206
207 static void amv_write_vlist(AVFormatContext *s, AVCodecParameters *par)
208 {
209     int64_t tag_list, tag_str;
210
211     av_assert0(par->codec_id == AV_CODEC_ID_AMV);
212
213     tag_list = amv_start_tag(s->pb, "LIST");
214     ffio_wfourcc(s->pb, "strl");
215     tag_str = ff_start_tag(s->pb, "strh");
216     ffio_fill(s->pb, 0, AMV_VIDEO_STRH_SIZE);
217     ff_end_tag(s->pb, tag_str);
218
219     tag_str = ff_start_tag(s->pb, "strf");
220     ffio_fill(s->pb, 0, AMV_VIDEO_STRF_SIZE);
221     ff_end_tag(s->pb, tag_str);
222
223     amv_end_tag(s->pb, tag_list);
224 }
225
226 static void amv_write_alist(AVFormatContext *s, AVCodecParameters *par)
227 {
228     uint8_t buf[AMV_AUDIO_STRF_SIZE];
229     AVIOContext *pb = s->pb;
230     int64_t tag_list, tag_str;
231
232     av_assert0(par->codec_id == AV_CODEC_ID_ADPCM_IMA_AMV);
233
234     tag_list = amv_start_tag(pb, "LIST");
235     ffio_wfourcc(pb, "strl");
236     tag_str = ff_start_tag(pb, "strh");
237     ffio_fill(s->pb, 0, AMV_AUDIO_STRH_SIZE);
238     ff_end_tag(pb, tag_str);
239
240     /* Bodge an (incorrect) WAVEFORMATEX (+2 pad bytes) */
241     tag_str = ff_start_tag(pb, "strf");
242     AV_WL16(buf +  0, 1);
243     AV_WL16(buf +  2, par->channels);
244     AV_WL32(buf +  4, par->sample_rate);
245     AV_WL32(buf +  8, par->sample_rate * par->channels * 2);
246     AV_WL16(buf + 12, 2);
247     AV_WL16(buf + 14, 16);
248     AV_WL16(buf + 16, 0);
249     AV_WL16(buf + 18, 0);
250     avio_write(pb, buf, AMV_AUDIO_STRF_SIZE);
251     ff_end_tag(pb, tag_str);
252
253     amv_end_tag(pb, tag_list);
254 }
255
256 static int amv_write_header(AVFormatContext *s)
257 {
258     AMVContext *amv = s->priv_data;
259     AVIOContext *pb = s->pb;
260     AVStream *vst   = s->streams[AMV_STREAM_VIDEO];
261     AVStream *ast   = s->streams[AMV_STREAM_AUDIO];
262     uint8_t amvh[56] = {0};
263     int64_t list1;
264
265     amv->riff_start = amv_start_tag(pb, "RIFF");
266     ffio_wfourcc(pb, "AMV ");
267     list1 = amv_start_tag(pb, "LIST");
268     ffio_wfourcc(pb, "hdrl");
269
270     ffio_wfourcc(pb, "amvh");
271     avio_wl32(pb, 56);
272
273     AV_WL32(amvh +  0, amv->us_per_frame);
274     AV_WL32(amvh + 32, vst->codecpar->width);
275     AV_WL32(amvh + 36, vst->codecpar->height);
276     AV_WL32(amvh + 40, vst->time_base.den);
277     AV_WL32(amvh + 44, vst->time_base.num);
278     AV_WL32(amvh + 48, 0);
279     AV_WL32(amvh + 52, 0); /* duration, filled in later. */
280
281     avio_write(pb, amvh, sizeof(amvh));
282     amv->offset_duration = avio_tell(pb) - 4;
283
284     amv_write_vlist(s, vst->codecpar);
285     amv_write_alist(s, ast->codecpar);
286     amv_end_tag(pb, list1);
287
288     amv->movi_list = amv_start_tag(pb, "LIST");
289     ffio_wfourcc(pb, "movi");
290     return 0;
291 }
292
293 static int amv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
294 {
295     AMVContext *amv = s->priv_data;
296
297     if (pkt->stream_index == AMV_STREAM_VIDEO)
298         ffio_wfourcc(s->pb, "00dc");
299     else if (pkt->stream_index == AMV_STREAM_AUDIO)
300         ffio_wfourcc(s->pb, "01wb");
301     else
302         av_assert0(0);
303
304     if (pkt->stream_index == AMV_STREAM_AUDIO && pkt->size != amv->ablock_align) {
305         /* Can happen when remuxing files produced by another encoder. */
306         av_log(s, AV_LOG_WARNING, "Invalid audio packet size (%d != %d)\n",
307                pkt->size, amv->ablock_align);
308     }
309
310     avio_wl32(s->pb, pkt->size);
311     avio_write(s->pb, pkt->data, pkt->size);
312
313     amv->lastpts[pkt->stream_index] += pkt->duration;
314     amv->last_stream = pkt->stream_index;
315     return 0;
316 }
317
318 static int amv_pad(AVFormatContext *s, AVPacket *pkt)
319 {
320     AMVContext *amv = s->priv_data;
321     int stream_index = pkt->stream_index;
322
323     if (stream_index != amv->last_stream)
324         return 0;
325
326     stream_index = (stream_index + 1) % s->nb_streams;
327     if (stream_index == AMV_STREAM_VIDEO)
328         return amv_write_packet_internal(s, &amv->vpad);
329     else if (stream_index == AMV_STREAM_AUDIO)
330         return amv_write_packet_internal(s, &amv->apad);
331     else
332         av_assert0(0);
333
334     return AVERROR(EINVAL);
335 }
336
337 static int amv_write_packet(AVFormatContext *s, AVPacket *pkt)
338 {
339     AMVContext *amv = s->priv_data;
340     int ret;
341
342     /* Add a dummy frame if we've received two of the same index. */
343     if ((ret = amv_pad(s, pkt)) < 0)
344         return ret;
345
346     if ((ret = amv_write_packet_internal(s, pkt)) < 0)
347         return ret;
348
349     if (pkt->stream_index == AMV_STREAM_VIDEO) {
350         /* Save the last packet for padding. */
351         av_packet_unref(&amv->vpad);
352         if ((ret = av_packet_ref(&amv->vpad, pkt)) < 0)
353             return ret;
354     }
355
356     return 0;
357 }
358
359 static int amv_write_trailer(AVFormatContext *s)
360 {
361     AMVContext *amv = s->priv_data;
362     AVStream   *vst = s->streams[AMV_STREAM_VIDEO];
363     AVStream   *ast = s->streams[AMV_STREAM_AUDIO];
364     int64_t maxpts, ret;
365     int hh, mm, ss;
366
367     /* Pad-out one last audio frame if needed. */
368     if (amv->last_stream == AMV_STREAM_VIDEO) {
369         if ((ret = amv_write_packet_internal(s, &amv->apad)) < 0)
370             return ret;
371     }
372
373     amv_end_tag(s->pb, amv->movi_list);
374     amv_end_tag(s->pb, amv->riff_start);
375
376     ffio_wfourcc(s->pb, "AMV_");
377     ffio_wfourcc(s->pb, "END_");
378
379     if ((ret = avio_seek(s->pb, amv->offset_duration, SEEK_SET)) < 0)
380         return ret;
381
382     /* Go back and write the duration. */
383     maxpts = FFMAX(
384         av_rescale_q(amv->lastpts[AMV_STREAM_VIDEO], vst->time_base, AV_TIME_BASE_Q),
385         av_rescale_q(amv->lastpts[AMV_STREAM_AUDIO], ast->time_base, AV_TIME_BASE_Q)
386     );
387
388     ss  = maxpts / AV_TIME_BASE;
389     mm  = ss / 60;
390     hh  = mm / 60;
391     ss %= 60;
392     mm %= 60;
393
394     avio_w8(s->pb, ss);
395     avio_w8(s->pb, mm);
396     avio_wl16(s->pb, hh);
397     return 0;
398 }
399
400 AVOutputFormat ff_amv_muxer = {
401     .name           = "amv",
402     .long_name      = NULL_IF_CONFIG_SMALL("AMV"),
403     .mime_type      = "video/amv",
404     .extensions     = "amv",
405     .priv_data_size = sizeof(AMVContext),
406     .audio_codec    = AV_CODEC_ID_ADPCM_IMA_AMV,
407     .video_codec    = AV_CODEC_ID_AMV,
408     .init           = amv_init,
409     .deinit         = amv_deinit,
410     .write_header   = amv_write_header,
411     .write_packet   = amv_write_packet,
412     .write_trailer  = amv_write_trailer,
413 };