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