FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avienc.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 438 585 74.9%
Branches: 190 311 61.1%

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