FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avienc.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 440 586 75.1%
Functions: 14 16 87.5%
Branches: 191 311 61.4%

Line Branch Exec Source
1 /*
2 * AVI muxer
3 * Copyright (c) 2000 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <math.h>
23
24 #include "avformat.h"
25 #include "internal.h"
26 #include "avi.h"
27 #include "avio_internal.h"
28 #include "libavutil/attributes.h"
29 #include "riff.h"
30 #include "mpegts.h"
31 #include "mux.h"
32 #include "rawutils.h"
33 #include "libavformat/avlanguage.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/avutil.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/mem.h"
40 #include "libavutil/timestamp.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavcodec/raw.h"
44
45 /*
46 * TODO:
47 * - fill all fields if non streamed (nb_frames for example)
48 */
49
50 typedef struct AVIIentry {
51 char tag[4];
52 unsigned int flags;
53 unsigned int pos;
54 unsigned int len;
55 } AVIIentry;
56
57 #define AVI_INDEX_CLUSTER_SIZE 16384
58 #define AVI_MASTER_INDEX_PREFIX_SIZE (8+2+1+1+4+8+4+4)
59 #define AVI_MASTER_INDEX_ENTRY_SIZE 16 /* bytes per entry */
60 #define AVI_MASTER_INDEX_SIZE_DEFAULT 256 /* number of entries */
61
62 typedef struct AVIIndex {
63 int64_t indx_start;
64 int64_t audio_strm_offset;
65 int entry;
66 int ents_allocated;
67 int master_odml_riff_id_base;
68 AVIIentry** cluster;
69 } AVIIndex;
70
71 typedef struct AVIContext {
72 const AVClass *class;
73 AVPacket *empty_packet;
74 int64_t riff_start, movi_list, odml_list;
75 int64_t frames_hdr_all;
76 int riff_id;
77 int reserve_index_space;
78 int master_index_max_size;
79 int write_channel_mask;
80 int flipped_raw_rgb;
81 } AVIContext;
82
83 typedef struct AVIStream {
84 int64_t frames_hdr_strm;
85 int64_t audio_strm_length;
86 int packet_count;
87 int entry;
88 int max_size;
89 int sample_requested;
90
91 int64_t last_dts;
92
93 AVIIndex indexes;
94
95 int64_t strh_flags_offset;
96
97 uint32_t palette[AVPALETTE_COUNT];
98 uint32_t old_palette[AVPALETTE_COUNT];
99 int64_t pal_offset;
100 } AVIStream;
101
102 static int avi_write_packet_internal(AVFormatContext *s, AVPacket *pkt);
103
104 16797 static inline AVIIentry *avi_get_ientry(const AVIIndex *idx, int ent_id)
105 {
106 16797 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
107 16797 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
108 16797 return &idx->cluster[cl][id];
109 }
110
111 15898 static int avi_add_ientry(AVFormatContext *s, int stream_index, char *tag,
112 unsigned int flags, unsigned int size)
113 {
114 15898 AVIContext *avi = s->priv_data;
115 15898 AVIOContext *pb = s->pb;
116 15898 AVIStream *avist = s->streams[stream_index]->priv_data;
117 15898 AVIIndex *idx = &avist->indexes;
118 15898 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
119 15898 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
120
121
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 15590 times.
15898 if (idx->ents_allocated <= idx->entry) {
122 308 idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (!idx->cluster) {
124 idx->ents_allocated = 0;
125 idx->entry = 0;
126 return AVERROR(ENOMEM);
127 }
128 616 idx->cluster[cl] =
129 308 av_malloc(AVI_INDEX_CLUSTER_SIZE * sizeof(AVIIentry));
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (!idx->cluster[cl])
131 return AVERROR(ENOMEM);
132 308 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
133 }
134
135
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15896 times.
15898 if (tag)
136 2 memcpy(idx->cluster[cl][id].tag, tag, 4);
137 else
138 15896 memset(idx->cluster[cl][id].tag, 0, 4);
139 15898 idx->cluster[cl][id].flags = flags;
140 15898 idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
141 15898 idx->cluster[cl][id].len = size;
142 15898 avist->max_size = FFMAX(avist->max_size, size);
143 15898 idx->entry++;
144
145 15898 return 0;
146 }
147
148 305 static av_cold int avi_init(struct AVFormatContext *s)
149 {
150 305 AVIContext *avi = s->priv_data;
151
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
305 if (avi->reserve_index_space > 0) {
153 avi->master_index_max_size = (avi->reserve_index_space - AVI_MASTER_INDEX_PREFIX_SIZE) / AVI_MASTER_INDEX_ENTRY_SIZE;
154 avi->master_index_max_size = FFMAX(avi->master_index_max_size, 16);
155 } else
156 305 avi->master_index_max_size = AVI_MASTER_INDEX_SIZE_DEFAULT;
157 305 av_log(s, AV_LOG_DEBUG, "reserve_index_space:%d master_index_max_size:%d\n",
158 avi->reserve_index_space, avi->master_index_max_size);
159
160 305 return 1; /* stream initialization continues in avi_write_header */
161 }
162
163 305 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
164 const char *riff_tag, const char *list_tag)
165 {
166 305 AVIContext *avi = s->priv_data;
167 int64_t loff;
168 int i;
169
170 305 avi->riff_id++;
171
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (i = 0; i < s->nb_streams; i++) {
172 308 AVIStream *avist = s->streams[i]->priv_data;
173 308 avist->indexes.audio_strm_offset = avist->audio_strm_length;
174 308 avist->indexes.entry = 0;
175 }
176
177 305 avi->riff_start = ff_start_tag(pb, "RIFF");
178 305 ffio_wfourcc(pb, riff_tag);
179 305 loff = ff_start_tag(pb, "LIST");
180 305 ffio_wfourcc(pb, list_tag);
181 305 return loff;
182 }
183
184 32102 static char *avi_stream2fourcc(char *tag, int index, enum AVMediaType type)
185 {
186 32102 tag[0] = '0' + index / 10;
187 32102 tag[1] = '0' + index % 10;
188
2/2
✓ Branch 0 taken 31691 times.
✓ Branch 1 taken 411 times.
32102 if (type == AVMEDIA_TYPE_VIDEO) {
189 31691 tag[2] = 'd';
190 31691 tag[3] = 'c';
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 } else if (type == AVMEDIA_TYPE_SUBTITLE) {
192 // note: this is not an official code
193 tag[2] = 's';
194 tag[3] = 'b';
195 } else {
196 411 tag[2] = 'w';
197 411 tag[3] = 'b';
198 }
199 32102 tag[4] = '\0';
200 32102 return tag;
201 }
202
203 305 static int avi_write_counters(AVFormatContext *s, int riff_id)
204 {
205 305 AVIOContext *pb = s->pb;
206 305 AVIContext *avi = s->priv_data;
207 305 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
208 int64_t file_size;
209 AVCodecParameters *par;
210
211 305 file_size = avio_tell(pb);
212
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (n = 0; n < s->nb_streams; n++) {
213 308 AVIStream *avist = s->streams[n]->priv_data;
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 av_assert0(avist->frames_hdr_strm);
216 308 par = s->streams[n]->codecpar;
217 308 avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
218 308 ff_parse_specific_params(s->streams[n], &au_byterate, &au_ssize, &au_scale);
219
2/2
✓ Branch 0 taken 307 times.
✓ Branch 1 taken 1 times.
308 if (au_ssize == 0)
220 307 avio_wl32(pb, avist->packet_count);
221 else
222 1 avio_wl32(pb, avist->audio_strm_length / au_ssize);
223
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 3 times.
308 if (par->codec_type == AVMEDIA_TYPE_VIDEO)
224 305 nb_frames = FFMAX(nb_frames, avist->packet_count);
225 }
226
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (riff_id == 1) {
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
305 av_assert0(avi->frames_hdr_all);
228 305 avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
229 305 avio_wl32(pb, nb_frames);
230 }
231 305 avio_seek(pb, file_size, SEEK_SET);
232
233 305 return 0;
234 }
235
236 308 static void write_odml_master(AVFormatContext *s, int stream_index)
237 {
238 308 AVIOContext *pb = s->pb;
239 308 AVIContext *avi = s->priv_data;
240 308 AVStream *st = s->streams[stream_index];
241 308 AVCodecParameters *par = st->codecpar;
242 308 AVIStream *avist = st->priv_data;
243 unsigned char tag[5];
244
245 /* Starting to lay out AVI OpenDML master index.
246 * We want to make it JUNK entry for now, since we'd
247 * like to get away without making AVI an OpenDML one
248 * for compatibility reasons. */
249 308 avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
250 308 avio_wl16(pb, 4); /* wLongsPerEntry */
251 308 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
252 308 avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
253 308 avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
254 308 ffio_wfourcc(pb, avi_stream2fourcc(tag, stream_index, par->codec_type));
255 /* dwChunkId */
256 308 ffio_fill(pb, 0, 3 * 4 /* dwReserved[3] */
257 308 + 16LL * avi->master_index_max_size);
258 308 ff_end_tag(pb, avist->indexes.indx_start);
259 308 }
260
261 305 static int avi_write_header(AVFormatContext *s)
262 {
263 305 AVIContext *avi = s->priv_data;
264 305 AVIOContext *pb = s->pb;
265 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
266 305 int64_t max_stream_duration = 0;
267 AVCodecParameters *video_par;
268 305 AVStream *video_st = NULL;
269 int64_t list1, list2, strh, strf;
270 305 AVDictionaryEntry *t = NULL;
271 int padding;
272
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
305 if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
274 av_log(s, AV_LOG_ERROR, "AVI does not support "
275 ">"AV_STRINGIFY(AVI_MAX_STREAM_COUNT)" streams\n");
276 return AVERROR(EINVAL);
277 }
278
279 305 avi->empty_packet = ffformatcontext(s)->pkt;
280
281
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (n = 0; n < s->nb_streams; n++) {
282 308 s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (!s->streams[n]->priv_data)
284 return AVERROR(ENOMEM);
285 }
286
287 /* header list */
288 305 avi->riff_id = 0;
289 305 list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
290
291 /* avi header */
292 305 ffio_wfourcc(pb, "avih");
293 305 avio_wl32(pb, 14 * 4);
294 305 bitrate = 0;
295
296 305 video_par = NULL;
297
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (n = 0; n < s->nb_streams; n++) {
298 308 AVCodecParameters *par = s->streams[n]->codecpar;
299 308 AVStream *st = s->streams[n];
300 308 bitrate = FFMIN(bitrate + par->bit_rate, INT32_MAX);
301
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 2 times.
308 if (st->duration > 0) {
302 306 int64_t stream_duration = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
303 306 max_stream_duration = FFMAX(stream_duration, max_stream_duration);
304 }
305
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 3 times.
308 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
306 305 video_par = par;
307 305 video_st = st;
308 }
309 }
310
311 /* guess master index size based on bitrate and duration */
312
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (!avi->reserve_index_space) {
313 double duration_est, filesize_est;
314
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 304 times.
305 if (s->duration > 0)
315 1 duration_est = (double)s->duration / AV_TIME_BASE;
316
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 1 times.
304 else if (max_stream_duration > 0)
317 303 duration_est = (double)max_stream_duration / AV_TIME_BASE;
318 else
319 1 duration_est = 10 * 60 * 60; /* default to 10 hours */
320 305 filesize_est = duration_est * (bitrate / 8) * 1.10; /* add 10% safety margin for muxer+bitrate */
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
305 avi->master_index_max_size = FFMAX((int)ceil(filesize_est / AVI_MAX_RIFF_SIZE) + 1,
322 avi->master_index_max_size);
323 305 av_log(s, AV_LOG_DEBUG, "duration_est:%0.3f, filesize_est:%0.1fGiB, master_index_max_size:%d\n",
324 duration_est, filesize_est / (1024*1024*1024), avi->master_index_max_size);
325 }
326
327 305 nb_frames = 0;
328
329 // TODO: should be avg_frame_rate
330
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (video_st)
331 305 avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_st->time_base.num /
332 305 video_st->time_base.den));
333 else
334 avio_wl32(pb, 0);
335 305 avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
336 305 avio_wl32(pb, 0); /* padding */
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
305 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
338 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
339 else
340 305 avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
341 305 avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
342 305 avio_wl32(pb, nb_frames); /* nb frames, filled later */
343 305 avio_wl32(pb, 0); /* initial frame */
344 305 avio_wl32(pb, s->nb_streams); /* nb streams */
345 305 avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
346
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (video_par) {
347 305 avio_wl32(pb, video_par->width);
348 305 avio_wl32(pb, video_par->height);
349 } else {
350 avio_wl32(pb, 0);
351 avio_wl32(pb, 0);
352 }
353 305 ffio_fill(pb, 0, 4 * 4); /* reserved */
354
355 /* stream list */
356
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (i = 0; i < n; i++) {
357 308 AVStream *st = s->streams[i];
358 308 AVCodecParameters *par = st->codecpar;
359 308 AVIStream *avist = st->priv_data;
360 308 list2 = ff_start_tag(pb, "LIST");
361 308 ffio_wfourcc(pb, "strl");
362
363 /* stream generic header */
364 308 strh = ff_start_tag(pb, "strh");
365
2/5
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
308 switch (par->codec_type) {
366 case AVMEDIA_TYPE_SUBTITLE:
367 // XSUB subtitles behave like video tracks, other subtitles
368 // are not (yet) supported.
369 if (par->codec_id != AV_CODEC_ID_XSUB) {
370 avpriv_report_missing_feature(s, "Subtitle streams other than DivX XSUB");
371 return AVERROR_PATCHWELCOME;
372 }
373 av_fallthrough;
374 case AVMEDIA_TYPE_VIDEO:
375 305 ffio_wfourcc(pb, "vids");
376 305 break;
377 3 case AVMEDIA_TYPE_AUDIO:
378 3 ffio_wfourcc(pb, "auds");
379 3 break;
380 // case AVMEDIA_TYPE_TEXT:
381 // ffio_wfourcc(pb, "txts");
382 // break;
383 case AVMEDIA_TYPE_DATA:
384 ffio_wfourcc(pb, "dats");
385 break;
386 }
387
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 305 times.
308 if (par->codec_type == AVMEDIA_TYPE_VIDEO ||
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 par->codec_id == AV_CODEC_ID_XSUB)
389 305 avio_wl32(pb, par->codec_tag);
390 else
391 3 avio_wl32(pb, 1);
392 308 avist->strh_flags_offset = avio_tell(pb);
393 308 avio_wl32(pb, 0); /* flags */
394 308 avio_wl16(pb, 0); /* priority */
395 308 avio_wl16(pb, 0); /* language */
396 308 avio_wl32(pb, 0); /* initial frame */
397
398 308 ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);
399
400
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 3 times.
308 if ( par->codec_type == AVMEDIA_TYPE_VIDEO
401
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 && par->codec_id != AV_CODEC_ID_XSUB
402
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 304 times.
305 && au_byterate > 1000LL*au_scale) {
403 1 au_byterate = 600;
404 1 au_scale = 1;
405 }
406 308 avpriv_set_pts_info(st, 64, au_scale, au_byterate);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (par->codec_id == AV_CODEC_ID_XSUB)
408 au_scale = au_byterate = 0;
409
410 308 avio_wl32(pb, au_scale); /* scale */
411 308 avio_wl32(pb, au_byterate); /* rate */
412
413 308 avio_wl32(pb, 0); /* start */
414 /* remember this offset to fill later */
415 308 avist->frames_hdr_strm = avio_tell(pb);
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
417 /* FIXME: this may be broken, but who cares */
418 avio_wl32(pb, AVI_MAX_RIFF_SIZE);
419 else
420 308 avio_wl32(pb, 0); /* length, XXX: filled later */
421
422 /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
423
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 3 times.
308 if (par->codec_type == AVMEDIA_TYPE_VIDEO)
424 305 avio_wl32(pb, 1024 * 1024);
425
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 else if (par->codec_type == AVMEDIA_TYPE_AUDIO)
426 3 avio_wl32(pb, 12 * 1024);
427 else
428 avio_wl32(pb, 0);
429 308 avio_wl32(pb, -1); /* quality */
430 308 avio_wl32(pb, au_ssize); /* sample size */
431 308 avio_wl32(pb, 0);
432
2/4
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 308 times.
308 if (par->width > 65535 || par->height > 65535) {
433 av_log(s, AV_LOG_ERROR, "%dx%d dimensions are too big\n", par->width, par->height);
434 return AVERROR(EINVAL);
435 }
436 308 avio_wl16(pb, par->width);
437 308 avio_wl16(pb, par->height);
438 308 ff_end_tag(pb, strh);
439
440
1/2
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
308 if (par->codec_type != AVMEDIA_TYPE_DATA) {
441 int ret, flags;
442 enum AVPixelFormat pix_fmt;
443
444 308 strf = ff_start_tag(pb, "strf");
445
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
308 switch (par->codec_type) {
446 case AVMEDIA_TYPE_SUBTITLE:
447 /* XSUB subtitles behave like video tracks, other subtitles
448 * are not (yet) supported. */
449 if (par->codec_id != AV_CODEC_ID_XSUB)
450 break;
451 av_fallthrough;
452 case AVMEDIA_TYPE_VIDEO:
453 /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
454
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 293 times.
305 if ( !par->codec_tag
455
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 && par->codec_id == AV_CODEC_ID_RAWVIDEO
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 && par->format == AV_PIX_FMT_RGB555LE
457 && par->bits_per_coded_sample == 15)
458 par->bits_per_coded_sample = 16;
459 305 avist->pal_offset = avio_tell(pb) + 40;
460 305 ff_put_bmp_header(pb, par, 0, 0, avi->flipped_raw_rgb);
461 305 pix_fmt = avpriv_pix_fmt_find(PIX_FMT_LIST_AVI,
462 305 par->bits_per_coded_sample);
463
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 293 times.
305 if ( !par->codec_tag
464
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 && par->codec_id == AV_CODEC_ID_RAWVIDEO
465
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 && par->format != pix_fmt
466
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 && par->format != AV_PIX_FMT_NONE)
467 8 av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
468 8 av_get_pix_fmt_name(par->format));
469
470
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 300 times.
305 if (par->format == AV_PIX_FMT_PAL8) {
471
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (par->bits_per_coded_sample < 0 || par->bits_per_coded_sample > 8) {
472 av_log(s, AV_LOG_ERROR, "PAL8 with %d bps is not allowed\n", par->bits_per_coded_sample);
473 return AVERROR(EINVAL);
474 }
475 }
476
477 305 break;
478 3 case AVMEDIA_TYPE_AUDIO:
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 flags = (avi->write_channel_mask == 0) ? FF_PUT_WAV_HEADER_SKIP_CHANNELMASK : 0;
480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_put_wav_header(s, pb, par, flags)) < 0)
481 return ret;
482 3 break;
483 default:
484 av_log(s, AV_LOG_ERROR,
485 "Invalid or not supported codec type '%s' found in the input\n",
486 (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
487 return AVERROR(EINVAL);
488 }
489 308 ff_end_tag(pb, strf);
490
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 308 times.
308 if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
491 ff_riff_write_info_tag(s->pb, "strn", t->value);
492 t = NULL;
493 }
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (par->codec_id == AV_CODEC_ID_XSUB
495 && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
496 const char* langstr = ff_convert_lang_to(t->value, AV_LANG_ISO639_1);
497 t = NULL;
498 if (langstr) {
499 char* str = av_asprintf("Subtitle - %s-xx;02", langstr);
500 if (!str)
501 return AVERROR(ENOMEM);
502 ff_riff_write_info_tag(s->pb, "strn", str);
503 av_free(str);
504 }
505 }
506 }
507
508
1/2
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
308 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
509 308 write_odml_master(s, i);
510 }
511
512
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 3 times.
308 if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
513
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 298 times.
305 st->sample_aspect_ratio.num > 0 &&
514
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 st->sample_aspect_ratio.den > 0) {
515 7 int vprp = ff_start_tag(pb, "vprp");
516 7 AVRational dar = av_mul_q(st->sample_aspect_ratio,
517 7 (AVRational) { par->width,
518 7 par->height });
519 int num, den, fields, i;
520 7 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
521
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
7 if (par->field_order == AV_FIELD_TT || par->field_order == AV_FIELD_BB ||
522
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 par->field_order == AV_FIELD_TB || par->field_order == AV_FIELD_BT) {
523 1 fields = 2; // interlaced
524 } else {
525 6 fields = 1; // progressive
526 }
527
528 7 avio_wl32(pb, 0); // video format = unknown
529 7 avio_wl32(pb, 0); // video standard = unknown
530 // TODO: should be avg_frame_rate
531 7 avio_wl32(pb, (2LL*st->time_base.den + st->time_base.num - 1) / (2LL * st->time_base.num));
532 7 avio_wl32(pb, par->width);
533 7 avio_wl32(pb, par->height);
534 7 avio_wl16(pb, den);
535 7 avio_wl16(pb, num);
536 7 avio_wl32(pb, par->width);
537 7 avio_wl32(pb, par->height);
538 7 avio_wl32(pb, fields); // fields per frame
539
540
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 for (i = 0; i < fields; i++) {
541 int start_line;
542 // OpenDML v1.02 is not very specific on what value to use for
543 // start_line when frame data is not coming from a capturing device,
544 // so just use 0/1 depending on the field order for interlaced frames
545
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 if (par->field_order == AV_FIELD_TT || par->field_order == AV_FIELD_TB) {
546 2 start_line = (i == 0) ? 0 : 1;
547
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 } else if (par->field_order == AV_FIELD_BB || par->field_order == AV_FIELD_BT) {
548 start_line = (i == 0) ? 1 : 0;
549 } else {
550 6 start_line = 0;
551 }
552
553 8 avio_wl32(pb, par->height / fields); // compressed bitmap height
554 8 avio_wl32(pb, par->width); // compressed bitmap width
555 8 avio_wl32(pb, par->height / fields); // valid bitmap height
556 8 avio_wl32(pb, par->width); // valid bitmap width
557 8 avio_wl32(pb, 0); // valid bitmap X offset
558 8 avio_wl32(pb, 0); // valid bitmap Y offset
559 8 avio_wl32(pb, 0); // valid X offset in T
560 8 avio_wl32(pb, start_line); // valid Y start line
561 }
562 7 ff_end_tag(pb, vprp);
563 }
564
565 308 ff_end_tag(pb, list2);
566 }
567
568
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
569 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
570 305 avi->odml_list = ff_start_tag(pb, "JUNK");
571 305 ffio_wfourcc(pb, "odml");
572 305 ffio_wfourcc(pb, "dmlh");
573 305 avio_wl32(pb, 248);
574 305 ffio_fill(pb, 0, 248);
575 305 ff_end_tag(pb, avi->odml_list);
576 }
577
578 305 ff_end_tag(pb, list1);
579
580 305 ff_riff_write_info(s);
581
582
583 305 padding = s->metadata_header_padding;
584
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (padding < 0)
585 305 padding = 1016;
586
587 /* some padding for easier tag editing */
588
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (padding) {
589 305 list2 = ff_start_tag(pb, "JUNK");
590 305 ffio_fill(pb, 0, FFALIGN((uint32_t)padding, 4));
591 305 ff_end_tag(pb, list2);
592 }
593
594 305 avi->movi_list = ff_start_tag(pb, "LIST");
595 305 ffio_wfourcc(pb, "movi");
596
597 305 return 0;
598 }
599
600 static void update_odml_entry(AVFormatContext *s, int stream_index, int64_t ix, int size)
601 {
602 AVIOContext *pb = s->pb;
603 AVIContext *avi = s->priv_data;
604 AVIStream *avist = s->streams[stream_index]->priv_data;
605 int64_t pos;
606 int au_byterate, au_ssize, au_scale;
607
608 pos = avio_tell(pb);
609
610 /* Updating one entry in the AVI OpenDML master index */
611 avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
612 ffio_wfourcc(pb, "indx"); /* enabling this entry */
613 avio_skip(pb, 8);
614 avio_wl32(pb, avi->riff_id - avist->indexes.master_odml_riff_id_base); /* nEntriesInUse */
615 avio_skip(pb, 16 * (avi->riff_id - avist->indexes.master_odml_riff_id_base));
616 avio_wl64(pb, ix); /* qwOffset */
617 avio_wl32(pb, size); /* dwSize */
618 ff_parse_specific_params(s->streams[stream_index], &au_byterate, &au_ssize, &au_scale);
619 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && au_ssize > 0) {
620 uint32_t audio_segm_size = (avist->audio_strm_length - avist->indexes.audio_strm_offset);
621 if ((audio_segm_size % au_ssize > 0) && !avist->sample_requested) {
622 avpriv_request_sample(s, "OpenDML index duration for audio packets with partial frames");
623 avist->sample_requested = 1;
624 }
625 avio_wl32(pb, audio_segm_size / au_ssize); /* dwDuration (sample count) */
626 } else
627 avio_wl32(pb, avist->indexes.entry); /* dwDuration (packet count) */
628
629 avio_seek(pb, pos, SEEK_SET);
630 }
631
632 static int avi_write_ix(AVFormatContext *s)
633 {
634 AVIOContext *pb = s->pb;
635 AVIContext *avi = s->priv_data;
636 char tag[5];
637 char ix_tag[] = "ix00";
638 int i, j;
639
640 av_assert0(pb->seekable & AVIO_SEEKABLE_NORMAL);
641
642 for (i = 0; i < s->nb_streams; i++) {
643 AVIStream *avist = s->streams[i]->priv_data;
644 if (avi->riff_id - avist->indexes.master_odml_riff_id_base == avi->master_index_max_size) {
645 int64_t pos;
646 int size = AVI_MASTER_INDEX_PREFIX_SIZE + AVI_MASTER_INDEX_ENTRY_SIZE * avi->master_index_max_size;
647
648 pos = avio_tell(pb);
649 update_odml_entry(s, i, pos, size);
650 write_odml_master(s, i);
651 av_assert1(avio_tell(pb) - pos == size);
652 avist->indexes.master_odml_riff_id_base = avi->riff_id - 1;
653 }
654 av_assert0(avi->riff_id - avist->indexes.master_odml_riff_id_base < avi->master_index_max_size);
655 }
656
657 for (i = 0; i < s->nb_streams; i++) {
658 AVIStream *avist = s->streams[i]->priv_data;
659 int64_t ix;
660
661 avi_stream2fourcc(tag, i, s->streams[i]->codecpar->codec_type);
662 ix_tag[3] = '0' + i;
663
664 /* Writing AVI OpenDML leaf index chunk */
665 ix = avio_tell(pb);
666 ffio_wfourcc(pb, ix_tag); /* ix?? */
667 avio_wl32(pb, avist->indexes.entry * 8 + 24);
668 /* chunk size */
669 avio_wl16(pb, 2); /* wLongsPerEntry */
670 avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
671 avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
672 avio_wl32(pb, avist->indexes.entry);
673 /* nEntriesInUse */
674 ffio_wfourcc(pb, tag); /* dwChunkId */
675 avio_wl64(pb, avi->movi_list); /* qwBaseOffset */
676 avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
677
678 for (j = 0; j < avist->indexes.entry; j++) {
679 AVIIentry *ie = avi_get_ientry(&avist->indexes, j);
680 avio_wl32(pb, ie->pos + 8);
681 avio_wl32(pb, ((uint32_t) ie->len & ~0x80000000) |
682 (ie->flags & 0x10 ? 0 : 0x80000000));
683 }
684
685 update_odml_entry(s, i, ix, avio_tell(pb) - ix);
686 }
687 return 0;
688 }
689
690 305 static int avi_write_idx1(AVFormatContext *s)
691 {
692 305 AVIOContext *pb = s->pb;
693 305 AVIContext *avi = s->priv_data;
694 int64_t idx_chunk;
695 int i;
696 char tag[5];
697
698
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
699 AVIStream *avist;
700 305 AVIIentry *ie = 0, *tie;
701 305 int empty, stream_id = -1;
702
703 305 idx_chunk = ff_start_tag(pb, "idx1");
704
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (i = 0; i < s->nb_streams; i++) {
705 308 avist = s->streams[i]->priv_data;
706 308 avist->entry = 0;
707 }
708
709 do {
710 16203 empty = 1;
711
2/2
✓ Branch 0 taken 17134 times.
✓ Branch 1 taken 16203 times.
33337 for (i = 0; i < s->nb_streams; i++) {
712 17134 avist = s->streams[i]->priv_data;
713
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 16797 times.
17134 if (avist->indexes.entry <= avist->entry)
714 337 continue;
715
716 16797 tie = avi_get_ientry(&avist->indexes, avist->entry);
717
4/4
✓ Branch 0 taken 899 times.
✓ Branch 1 taken 15898 times.
✓ Branch 2 taken 194 times.
✓ Branch 3 taken 705 times.
16797 if (empty || tie->pos < ie->pos) {
718 16092 ie = tie;
719 16092 stream_id = i;
720 }
721 16797 empty = 0;
722 }
723
2/2
✓ Branch 0 taken 15898 times.
✓ Branch 1 taken 305 times.
16203 if (!empty) {
724 15898 avist = s->streams[stream_id]->priv_data;
725
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15896 times.
15898 if (*ie->tag)
726 2 ffio_wfourcc(pb, ie->tag);
727 else {
728 15896 avi_stream2fourcc(tag, stream_id,
729 15896 s->streams[stream_id]->codecpar->codec_type);
730 15896 ffio_wfourcc(pb, tag);
731 }
732 15898 avio_wl32(pb, ie->flags);
733 15898 avio_wl32(pb, ie->pos);
734 15898 avio_wl32(pb, ie->len);
735 15898 avist->entry++;
736 }
737
2/2
✓ Branch 0 taken 15898 times.
✓ Branch 1 taken 305 times.
16203 } while (!empty);
738 305 ff_end_tag(pb, idx_chunk);
739
740 305 avi_write_counters(s, avi->riff_id);
741 }
742 305 return 0;
743 }
744
745 15641 static int write_skip_frames(AVFormatContext *s, int stream_index, int64_t dts)
746 {
747 15641 AVIContext *avi = s->priv_data;
748 15641 AVIStream *avist = s->streams[stream_index]->priv_data;
749 15641 AVCodecParameters *par = s->streams[stream_index]->codecpar;
750
751 ff_dlog(s, "dts:%s packet_count:%d stream_index:%d\n", av_ts2str(dts), avist->packet_count, stream_index);
752
1/2
✓ Branch 0 taken 16080 times.
✗ Branch 1 not taken.
31721 while (par->block_align == 0 && dts != AV_NOPTS_VALUE &&
753
7/8
✓ Branch 0 taken 16080 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 564 times.
✓ Branch 3 taken 15516 times.
✓ Branch 4 taken 564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 563 times.
✓ Branch 7 taken 1 times.
32284 dts > avist->packet_count && par->codec_id != AV_CODEC_ID_XSUB && avist->packet_count) {
754
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 563 times.
563 if (dts - avist->packet_count > 60000) {
756 av_log(s, AV_LOG_ERROR, "Too large number of skipped frames %"PRId64" > 60000\n", dts - avist->packet_count);
757 return AVERROR(EINVAL);
758 }
759
760 563 avi->empty_packet->stream_index = stream_index;
761 563 avi_write_packet_internal(s, avi->empty_packet);
762 ff_dlog(s, "dup dts:%s packet_count:%d\n", av_ts2str(dts), avist->packet_count);
763 }
764
765 15641 return 0;
766 }
767
768 15333 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
769 {
770 15333 const int stream_index = pkt->stream_index;
771 15333 AVCodecParameters *par = s->streams[stream_index]->codecpar;
772 int ret;
773
774
4/6
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 15254 times.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 79 times.
✗ Branch 5 not taken.
15333 if (par->codec_id == AV_CODEC_ID_H264 && par->codec_tag == MKTAG('H','2','6','4') && pkt->size) {
775 79 ret = ff_check_h264_startcode(s, s->streams[stream_index], pkt);
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (ret < 0)
777 return ret;
778 }
779
780
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15333 times.
15333 if ((ret = write_skip_frames(s, stream_index, pkt->dts)) < 0)
781 return ret;
782
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15333 times.
15333 if (!pkt->size)
784 return avi_write_packet_internal(s, pkt); /* Passthrough */
785
786
2/2
✓ Branch 0 taken 15129 times.
✓ Branch 1 taken 204 times.
15333 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
787 15129 AVIStream *avist = s->streams[stream_index]->priv_data;
788 15129 AVIOContext *pb = s->pb;
789 15129 AVPacket *opkt = pkt;
790 int reshuffle_ret;
791
4/4
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 13929 times.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 600 times.
15729 if (par->codec_id == AV_CODEC_ID_RAWVIDEO && par->codec_tag == 0) {
792
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 200 times.
600 int64_t bpc = par->bits_per_coded_sample != 15 ? par->bits_per_coded_sample : 16;
793 600 int expected_stride = ((par->width * bpc + 31) >> 5)*4;
794 600 reshuffle_ret = ff_reshuffle_raw_rgb(s, &pkt, par, expected_stride);
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (reshuffle_ret < 0)
796 return reshuffle_ret;
797 } else
798 14529 reshuffle_ret = 0;
799
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 14813 times.
15129 if (par->format == AV_PIX_FMT_PAL8) {
800 316 ret = ff_get_packet_palette(s, opkt, reshuffle_ret, avist->palette);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 316 times.
316 if (ret < 0)
802 goto fail;
803
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 113 times.
316 if (ret) {
804 203 int pal_size = 1 << par->bits_per_coded_sample;
805 int pc_tag, i;
806
807
2/4
✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 203 times.
203 av_assert0(par->bits_per_coded_sample >= 0 && par->bits_per_coded_sample <= 8);
808
809
3/4
✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 198 times.
203 if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && avist->pal_offset) {
810 5 int64_t cur_offset = avio_tell(pb);
811 5 avio_seek(pb, avist->pal_offset, SEEK_SET);
812
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 5 times.
1285 for (i = 0; i < pal_size; i++) {
813 1280 uint32_t v = avist->palette[i];
814 1280 avio_wl32(pb, v & 0xffffff);
815 }
816 5 avio_seek(pb, cur_offset, SEEK_SET);
817 5 memcpy(avist->old_palette, avist->palette, pal_size * 4);
818 5 avist->pal_offset = 0;
819 }
820
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 201 times.
203 if (memcmp(avist->palette, avist->old_palette, pal_size * 4)) {
821 unsigned char tag[5];
822 2 avi_stream2fourcc(tag, stream_index, par->codec_type);
823 2 tag[2] = 'p'; tag[3] = 'c';
824
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
825
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (avist->strh_flags_offset) {
826 1 int64_t cur_offset = avio_tell(pb);
827 1 avio_seek(pb, avist->strh_flags_offset, SEEK_SET);
828 1 avio_wl32(pb, AVISF_VIDEO_PALCHANGES);
829 1 avio_seek(pb, cur_offset, SEEK_SET);
830 1 avist->strh_flags_offset = 0;
831 }
832 2 ret = avi_add_ientry(s, stream_index, tag, AVIIF_NO_TIME,
833 2 pal_size * 4 + 4);
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
835 goto fail;
836 }
837 2 pc_tag = ff_start_tag(pb, tag);
838 2 avio_w8(pb, 0);
839 2 avio_w8(pb, pal_size & 0xFF);
840 2 avio_wl16(pb, 0); // reserved
841
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 2 times.
514 for (i = 0; i < pal_size; i++) {
842 512 uint32_t v = avist->palette[i];
843 512 avio_wb32(pb, v<<8);
844 }
845 2 ff_end_tag(pb, pc_tag);
846 2 memcpy(avist->old_palette, avist->palette, pal_size * 4);
847 }
848 }
849 }
850
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 15029 times.
15129 if (reshuffle_ret) {
851 100 ret = avi_write_packet_internal(s, pkt);
852
853 100 fail:
854
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (reshuffle_ret)
855 100 av_packet_free(&pkt);
856 100 return ret;
857 }
858 }
859
860 15233 return avi_write_packet_internal(s, pkt);
861 }
862
863 15896 static int avi_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
864 {
865 unsigned char tag[5];
866 15896 unsigned int flags = 0;
867 15896 const int stream_index = pkt->stream_index;
868 15896 int size = pkt->size;
869 15896 AVIContext *avi = s->priv_data;
870 15896 AVIOContext *pb = s->pb;
871 15896 AVIStream *avist = s->streams[stream_index]->priv_data;
872 15896 AVCodecParameters *par = s->streams[stream_index]->codecpar;
873
874
2/2
✓ Branch 0 taken 15333 times.
✓ Branch 1 taken 563 times.
15896 if (pkt->dts != AV_NOPTS_VALUE)
875 15333 avist->last_dts = pkt->dts + pkt->duration;
876
877 15896 avist->packet_count++;
878
879 // Make sure to put an OpenDML chunk when the file size exceeds the limits
880
1/2
✓ Branch 0 taken 15896 times.
✗ Branch 1 not taken.
15896 if ((pb->seekable & AVIO_SEEKABLE_NORMAL) &&
881
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15896 times.
15896 (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
882 avi_write_ix(s);
883 ff_end_tag(pb, avi->movi_list);
884
885 if (avi->riff_id == 1)
886 avi_write_idx1(s);
887
888 ff_end_tag(pb, avi->riff_start);
889 avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
890 }
891
892 15896 avi_stream2fourcc(tag, stream_index, par->codec_type);
893
2/2
✓ Branch 0 taken 9090 times.
✓ Branch 1 taken 6806 times.
15896 if (pkt->flags & AV_PKT_FLAG_KEY)
894 9090 flags = 0x10;
895
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 15692 times.
15896 if (par->codec_type == AVMEDIA_TYPE_AUDIO)
896 204 avist->audio_strm_length += size;
897
898
1/2
✓ Branch 0 taken 15896 times.
✗ Branch 1 not taken.
15896 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
899 int ret;
900 15896 ret = avi_add_ientry(s, stream_index, NULL, flags, size);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15896 times.
15896 if (ret < 0)
902 return ret;
903 }
904
905 15896 avio_write(pb, tag, 4);
906 15896 avio_wl32(pb, size);
907 15896 avio_write(pb, pkt->data, size);
908
2/2
✓ Branch 0 taken 5098 times.
✓ Branch 1 taken 10798 times.
15896 if (size & 1)
909 5098 avio_w8(pb, 0);
910
911 15896 return 0;
912 }
913
914 305 static int avi_write_trailer(AVFormatContext *s)
915 {
916 305 AVIContext *avi = s->priv_data;
917 305 AVIOContext *pb = s->pb;
918 305 int res = 0;
919 int i, n, nb_frames;
920 int64_t file_size;
921
922
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (i = 0; i < s->nb_streams; i++) {
923 308 AVIStream *avist = s->streams[i]->priv_data;
924 308 write_skip_frames(s, i, avist->last_dts);
925 }
926
927
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
928
1/2
✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
305 if (avi->riff_id == 1) {
929 305 ff_end_tag(pb, avi->movi_list);
930 305 res = avi_write_idx1(s);
931 305 ff_end_tag(pb, avi->riff_start);
932 } else {
933 avi_write_ix(s);
934 ff_end_tag(pb, avi->movi_list);
935 ff_end_tag(pb, avi->riff_start);
936
937 file_size = avio_tell(pb);
938 avio_seek(pb, avi->odml_list - 8, SEEK_SET);
939 ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
940 avio_skip(pb, 16);
941
942 for (n = nb_frames = 0; n < s->nb_streams; n++) {
943 AVCodecParameters *par = s->streams[n]->codecpar;
944 AVIStream *avist = s->streams[n]->priv_data;
945
946 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
947 if (nb_frames < avist->packet_count)
948 nb_frames = avist->packet_count;
949 } else {
950 if (par->codec_id == AV_CODEC_ID_MP2 ||
951 par->codec_id == AV_CODEC_ID_MP3)
952 nb_frames += avist->packet_count;
953 }
954 }
955 avio_wl32(pb, nb_frames);
956 avio_seek(pb, file_size, SEEK_SET);
957
958 avi_write_counters(s, avi->riff_id);
959 }
960 }
961
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
305 if (avi->riff_id >= avi->master_index_max_size) {
963 int index_space = AVI_MASTER_INDEX_PREFIX_SIZE +
964 AVI_MASTER_INDEX_ENTRY_SIZE * avi->riff_id;
965 av_log(s, AV_LOG_WARNING, "Output file not strictly OpenDML compliant, "
966 "consider re-muxing with 'reserve_index_space' option value >= %d\n",
967 index_space);
968 }
969
970
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (i = 0; i < s->nb_streams; i++) {
971 308 AVIStream *avist = s->streams[i]->priv_data;
972
1/2
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
308 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
973 308 avio_seek(pb, avist->frames_hdr_strm + 4, SEEK_SET);
974 308 avio_wl32(pb, avist->max_size);
975 }
976 }
977
978 305 return res;
979 }
980
981 305 static void avi_deinit(AVFormatContext *s)
982 {
983
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 305 times.
613 for (int i = 0; i < s->nb_streams; i++) {
984 308 AVIStream *avist = s->streams[i]->priv_data;
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (!avist)
986 continue;
987
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 308 times.
616 for (int j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++)
988 308 av_freep(&avist->indexes.cluster[j]);
989 308 av_freep(&avist->indexes.cluster);
990 308 avist->indexes.ents_allocated = avist->indexes.entry = 0;
991 }
992 305 }
993
994 #define OFFSET(x) offsetof(AVIContext, x)
995 #define ENC AV_OPT_FLAG_ENCODING_PARAM
996 static const AVOption options[] = {
997 { "reserve_index_space", "reserve space (in bytes) at the beginning of the file for each stream index", OFFSET(reserve_index_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, ENC },
998 { "write_channel_mask", "write channel mask into wave format header", OFFSET(write_channel_mask), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC },
999 { "flipped_raw_rgb", "Raw RGB bitmaps are stored bottom-up", OFFSET(flipped_raw_rgb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
1000 { NULL },
1001 };
1002
1003 static const AVClass avi_muxer_class = {
1004 .class_name = "AVI muxer",
1005 .item_name = av_default_item_name,
1006 .option = options,
1007 .version = LIBAVUTIL_VERSION_INT,
1008 };
1009
1010 const FFOutputFormat ff_avi_muxer = {
1011 .p.name = "avi",
1012 .p.long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
1013 .p.mime_type = "video/x-msvideo",
1014 .p.extensions = "avi",
1015 .priv_data_size = sizeof(AVIContext),
1016 .p.audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3,
1017 .p.video_codec = AV_CODEC_ID_MPEG4,
1018 .init = avi_init,
1019 .deinit = avi_deinit,
1020 .write_header = avi_write_header,
1021 .write_packet = avi_write_packet,
1022 .write_trailer = avi_write_trailer,
1023 .p.codec_tag = ff_riff_codec_tags_list,
1024 .p.priv_class = &avi_muxer_class,
1025 };
1026