Merge commit 'origin/master' into ordered_chapters
[FFMpeg-mirror/ordered_chapters.git] / libavformat / avienc.c
blob5ae95e2b6a2072dbb4e1f941e6a24c55f8dcd77f
1 /*
2 * AVI muxer
3 * Copyright (c) 2000 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
21 #include "avformat.h"
22 #include "avi.h"
23 #include "riff.h"
26 * TODO:
27 * - fill all fields if non streamed (nb_frames for example)
30 typedef struct AVIIentry {
31 unsigned int flags, pos, len;
32 } AVIIentry;
34 #define AVI_INDEX_CLUSTER_SIZE 16384
36 typedef struct AVIIndex {
37 int64_t indx_start;
38 int entry;
39 int ents_allocated;
40 AVIIentry** cluster;
41 } AVIIndex;
43 typedef struct {
44 int64_t riff_start, movi_list, odml_list;
45 int64_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
46 int audio_strm_length[MAX_STREAMS];
47 int riff_id;
48 int packet_count[MAX_STREAMS];
50 AVIIndex indexes[MAX_STREAMS];
51 } AVIContext;
53 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
55 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
56 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
57 return &idx->cluster[cl][id];
60 static int64_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
61 const char* riff_tag, const char* list_tag)
63 int64_t loff;
64 int i;
66 avi->riff_id++;
67 for (i=0; i<MAX_STREAMS; i++)
68 avi->indexes[i].entry = 0;
70 avi->riff_start = ff_start_tag(pb, "RIFF");
71 put_tag(pb, riff_tag);
72 loff = ff_start_tag(pb, "LIST");
73 put_tag(pb, list_tag);
74 return loff;
77 static char* avi_stream2fourcc(char* tag, int index, enum CodecType type)
79 tag[0] = '0';
80 tag[1] = '0' + index;
81 if (type == CODEC_TYPE_VIDEO) {
82 tag[2] = 'd';
83 tag[3] = 'c';
84 } else if (type == CODEC_TYPE_SUBTITLE) {
85 // note: this is not an official code
86 tag[2] = 's';
87 tag[3] = 'b';
88 } else {
89 tag[2] = 'w';
90 tag[3] = 'b';
92 tag[4] = '\0';
93 return tag;
96 static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
98 int len = strlen(str);
99 if (len > 0) {
100 len++;
101 put_tag(pb, tag);
102 put_le32(pb, len);
103 put_strz(pb, str);
104 if (len & 1)
105 put_byte(pb, 0);
109 static void avi_write_info_tag2(AVFormatContext *s, const char *fourcc, const char *key1, const char *key2)
111 AVMetadataTag *tag= av_metadata_get(s->metadata, key1, NULL, 0);
112 if(!tag && key2)
113 tag= av_metadata_get(s->metadata, key2, NULL, 0);
114 if(tag)
115 avi_write_info_tag(s->pb, fourcc, tag->value);
118 static int avi_write_counters(AVFormatContext* s, int riff_id)
120 ByteIOContext *pb = s->pb;
121 AVIContext *avi = s->priv_data;
122 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
123 int64_t file_size;
124 AVCodecContext* stream;
126 file_size = url_ftell(pb);
127 for(n = 0; n < s->nb_streams; n++) {
128 assert(avi->frames_hdr_strm[n]);
129 stream = s->streams[n]->codec;
130 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
131 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
132 if(au_ssize == 0) {
133 put_le32(pb, avi->packet_count[n]);
134 } else {
135 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
137 if(stream->codec_type == CODEC_TYPE_VIDEO)
138 nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
140 if(riff_id == 1) {
141 assert(avi->frames_hdr_all);
142 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
143 put_le32(pb, nb_frames);
145 url_fseek(pb, file_size, SEEK_SET);
147 return 0;
150 static int avi_write_header(AVFormatContext *s)
152 AVIContext *avi = s->priv_data;
153 ByteIOContext *pb = s->pb;
154 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
155 AVCodecContext *stream, *video_enc;
156 int64_t list1, list2, strh, strf;
158 /* header list */
159 avi->riff_id = 0;
160 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
162 /* avi header */
163 put_tag(pb, "avih");
164 put_le32(pb, 14 * 4);
165 bitrate = 0;
167 video_enc = NULL;
168 for(n=0;n<s->nb_streams;n++) {
169 stream = s->streams[n]->codec;
170 bitrate += stream->bit_rate;
171 if (stream->codec_type == CODEC_TYPE_VIDEO)
172 video_enc = stream;
175 nb_frames = 0;
177 if(video_enc){
178 put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
179 } else {
180 put_le32(pb, 0);
182 put_le32(pb, bitrate / 8); /* XXX: not quite exact */
183 put_le32(pb, 0); /* padding */
184 if (url_is_streamed(pb))
185 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
186 else
187 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
188 avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
189 put_le32(pb, nb_frames); /* nb frames, filled later */
190 put_le32(pb, 0); /* initial frame */
191 put_le32(pb, s->nb_streams); /* nb streams */
192 put_le32(pb, 1024 * 1024); /* suggested buffer size */
193 if(video_enc){
194 put_le32(pb, video_enc->width);
195 put_le32(pb, video_enc->height);
196 } else {
197 put_le32(pb, 0);
198 put_le32(pb, 0);
200 put_le32(pb, 0); /* reserved */
201 put_le32(pb, 0); /* reserved */
202 put_le32(pb, 0); /* reserved */
203 put_le32(pb, 0); /* reserved */
205 /* stream list */
206 for(i=0;i<n;i++) {
207 list2 = ff_start_tag(pb, "LIST");
208 put_tag(pb, "strl");
210 stream = s->streams[i]->codec;
212 /* stream generic header */
213 strh = ff_start_tag(pb, "strh");
214 switch(stream->codec_type) {
215 case CODEC_TYPE_SUBTITLE:
216 // XSUB subtitles behave like video tracks, other subtitles
217 // are not (yet) supported.
218 if (stream->codec_id != CODEC_ID_XSUB) break;
219 case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
220 case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
221 // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
222 case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
224 if(stream->codec_type == CODEC_TYPE_VIDEO ||
225 stream->codec_id == CODEC_ID_XSUB)
226 put_le32(pb, stream->codec_tag);
227 else
228 put_le32(pb, 1);
229 put_le32(pb, 0); /* flags */
230 put_le16(pb, 0); /* priority */
231 put_le16(pb, 0); /* language */
232 put_le32(pb, 0); /* initial frame */
234 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
236 put_le32(pb, au_scale); /* scale */
237 put_le32(pb, au_byterate); /* rate */
238 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
240 put_le32(pb, 0); /* start */
241 avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
242 if (url_is_streamed(pb))
243 put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
244 else
245 put_le32(pb, 0); /* length, XXX: filled later */
247 /* suggested buffer size */ //FIXME set at the end to largest chunk
248 if(stream->codec_type == CODEC_TYPE_VIDEO)
249 put_le32(pb, 1024 * 1024);
250 else if(stream->codec_type == CODEC_TYPE_AUDIO)
251 put_le32(pb, 12 * 1024);
252 else
253 put_le32(pb, 0);
254 put_le32(pb, -1); /* quality */
255 put_le32(pb, au_ssize); /* sample size */
256 put_le32(pb, 0);
257 put_le16(pb, stream->width);
258 put_le16(pb, stream->height);
259 ff_end_tag(pb, strh);
261 if(stream->codec_type != CODEC_TYPE_DATA){
262 strf = ff_start_tag(pb, "strf");
263 switch(stream->codec_type) {
264 case CODEC_TYPE_SUBTITLE:
265 // XSUB subtitles behave like video tracks, other subtitles
266 // are not (yet) supported.
267 if (stream->codec_id != CODEC_ID_XSUB) break;
268 case CODEC_TYPE_VIDEO:
269 ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
270 break;
271 case CODEC_TYPE_AUDIO:
272 if (ff_put_wav_header(pb, stream) < 0) {
273 return -1;
275 break;
276 default:
277 return -1;
279 ff_end_tag(pb, strf);
282 if (!url_is_streamed(pb)) {
283 unsigned char tag[5];
284 int j;
286 /* Starting to lay out AVI OpenDML master index.
287 * We want to make it JUNK entry for now, since we'd
288 * like to get away without making AVI an OpenDML one
289 * for compatibility reasons.
291 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
292 avi->indexes[i].indx_start = ff_start_tag(pb, "JUNK");
293 put_le16(pb, 4); /* wLongsPerEntry */
294 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
295 put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
296 put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
297 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
298 /* dwChunkId */
299 put_le64(pb, 0); /* dwReserved[3]
300 put_le32(pb, 0); Must be 0. */
301 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
302 put_le64(pb, 0);
303 ff_end_tag(pb, avi->indexes[i].indx_start);
306 if( stream->codec_type == CODEC_TYPE_VIDEO
307 && s->streams[i]->sample_aspect_ratio.num>0
308 && s->streams[i]->sample_aspect_ratio.den>0){
309 int vprp= ff_start_tag(pb, "vprp");
310 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
311 (AVRational){stream->width, stream->height});
312 int num, den;
313 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
315 put_le32(pb, 0); //video format = unknown
316 put_le32(pb, 0); //video standard= unknown
317 put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
318 put_le32(pb, stream->width );
319 put_le32(pb, stream->height);
320 put_le16(pb, den);
321 put_le16(pb, num);
322 put_le32(pb, stream->width );
323 put_le32(pb, stream->height);
324 put_le32(pb, 1); //progressive FIXME
326 put_le32(pb, stream->height);
327 put_le32(pb, stream->width );
328 put_le32(pb, stream->height);
329 put_le32(pb, stream->width );
330 put_le32(pb, 0);
331 put_le32(pb, 0);
333 put_le32(pb, 0);
334 put_le32(pb, 0);
335 ff_end_tag(pb, vprp);
338 ff_end_tag(pb, list2);
341 if (!url_is_streamed(pb)) {
342 /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
343 avi->odml_list = ff_start_tag(pb, "JUNK");
344 put_tag(pb, "odml");
345 put_tag(pb, "dmlh");
346 put_le32(pb, 248);
347 for (i = 0; i < 248; i+= 4)
348 put_le32(pb, 0);
349 ff_end_tag(pb, avi->odml_list);
352 ff_end_tag(pb, list1);
354 list2 = ff_start_tag(pb, "LIST");
355 put_tag(pb, "INFO");
356 avi_write_info_tag2(s, "INAM", "Title", NULL);
357 avi_write_info_tag2(s, "IART", "Artist", "Author");
358 avi_write_info_tag2(s, "ICOP", "Copyright", NULL);
359 avi_write_info_tag2(s, "ICMT", "Comment", NULL);
360 avi_write_info_tag2(s, "IPRD", "Album", NULL);
361 avi_write_info_tag2(s, "IGNR", "Genre", NULL);
362 avi_write_info_tag2(s, "IPRT", "Track", NULL);
363 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
364 avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
365 ff_end_tag(pb, list2);
367 /* some padding for easier tag editing */
368 list2 = ff_start_tag(pb, "JUNK");
369 for (i = 0; i < 1016; i += 4)
370 put_le32(pb, 0);
371 ff_end_tag(pb, list2);
373 avi->movi_list = ff_start_tag(pb, "LIST");
374 put_tag(pb, "movi");
376 put_flush_packet(pb);
378 return 0;
381 static int avi_write_ix(AVFormatContext *s)
383 ByteIOContext *pb = s->pb;
384 AVIContext *avi = s->priv_data;
385 char tag[5];
386 char ix_tag[] = "ix00";
387 int i, j;
389 assert(!url_is_streamed(pb));
391 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
392 return -1;
394 for (i=0;i<s->nb_streams;i++) {
395 int64_t ix, pos;
397 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
398 ix_tag[3] = '0' + i;
400 /* Writing AVI OpenDML leaf index chunk */
401 ix = url_ftell(pb);
402 put_tag(pb, &ix_tag[0]); /* ix?? */
403 put_le32(pb, avi->indexes[i].entry * 8 + 24);
404 /* chunk size */
405 put_le16(pb, 2); /* wLongsPerEntry */
406 put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
407 put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
408 put_le32(pb, avi->indexes[i].entry);
409 /* nEntriesInUse */
410 put_tag(pb, &tag[0]); /* dwChunkId */
411 put_le64(pb, avi->movi_list);/* qwBaseOffset */
412 put_le32(pb, 0); /* dwReserved_3 (must be 0) */
414 for (j=0; j<avi->indexes[i].entry; j++) {
415 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
416 put_le32(pb, ie->pos + 8);
417 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
418 (ie->flags & 0x10 ? 0 : 0x80000000));
420 put_flush_packet(pb);
421 pos = url_ftell(pb);
423 /* Updating one entry in the AVI OpenDML master index */
424 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
425 put_tag(pb, "indx"); /* enabling this entry */
426 url_fskip(pb, 8);
427 put_le32(pb, avi->riff_id); /* nEntriesInUse */
428 url_fskip(pb, 16*avi->riff_id);
429 put_le64(pb, ix); /* qwOffset */
430 put_le32(pb, pos - ix); /* dwSize */
431 put_le32(pb, avi->indexes[i].entry); /* dwDuration */
433 url_fseek(pb, pos, SEEK_SET);
435 return 0;
438 static int avi_write_idx1(AVFormatContext *s)
440 ByteIOContext *pb = s->pb;
441 AVIContext *avi = s->priv_data;
442 int64_t idx_chunk;
443 int i;
444 char tag[5];
446 if (!url_is_streamed(pb)) {
447 AVIIentry* ie = 0, *tie;
448 int entry[MAX_STREAMS];
449 int empty, stream_id = -1;
451 idx_chunk = ff_start_tag(pb, "idx1");
452 memset(&entry[0], 0, sizeof(entry));
453 do {
454 empty = 1;
455 for (i=0; i<s->nb_streams; i++) {
456 if (avi->indexes[i].entry <= entry[i])
457 continue;
459 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
460 if (empty || tie->pos < ie->pos) {
461 ie = tie;
462 stream_id = i;
464 empty = 0;
466 if (!empty) {
467 avi_stream2fourcc(&tag[0], stream_id,
468 s->streams[stream_id]->codec->codec_type);
469 put_tag(pb, &tag[0]);
470 put_le32(pb, ie->flags);
471 put_le32(pb, ie->pos);
472 put_le32(pb, ie->len);
473 entry[stream_id]++;
475 } while (!empty);
476 ff_end_tag(pb, idx_chunk);
478 avi_write_counters(s, avi->riff_id);
480 return 0;
483 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
485 AVIContext *avi = s->priv_data;
486 ByteIOContext *pb = s->pb;
487 unsigned char tag[5];
488 unsigned int flags=0;
489 const int stream_index= pkt->stream_index;
490 AVCodecContext *enc= s->streams[stream_index]->codec;
491 int size= pkt->size;
493 // av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
494 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
495 AVPacket empty_packet;
497 av_init_packet(&empty_packet);
498 empty_packet.size= 0;
499 empty_packet.data= NULL;
500 empty_packet.stream_index= stream_index;
501 avi_write_packet(s, &empty_packet);
502 // av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
504 avi->packet_count[stream_index]++;
506 // Make sure to put an OpenDML chunk when the file size exceeds the limits
507 if (!url_is_streamed(pb) &&
508 (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
510 avi_write_ix(s);
511 ff_end_tag(pb, avi->movi_list);
513 if (avi->riff_id == 1)
514 avi_write_idx1(s);
516 ff_end_tag(pb, avi->riff_start);
517 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
520 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
521 if(pkt->flags&PKT_FLAG_KEY)
522 flags = 0x10;
523 if (enc->codec_type == CODEC_TYPE_AUDIO) {
524 avi->audio_strm_length[stream_index] += size;
527 if (!url_is_streamed(s->pb)) {
528 AVIIndex* idx = &avi->indexes[stream_index];
529 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
530 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
531 if (idx->ents_allocated <= idx->entry) {
532 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
533 if (!idx->cluster)
534 return -1;
535 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
536 if (!idx->cluster[cl])
537 return -1;
538 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
541 idx->cluster[cl][id].flags = flags;
542 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
543 idx->cluster[cl][id].len = size;
544 idx->entry++;
547 put_buffer(pb, tag, 4);
548 put_le32(pb, size);
549 put_buffer(pb, pkt->data, size);
550 if (size & 1)
551 put_byte(pb, 0);
553 put_flush_packet(pb);
554 return 0;
557 static int avi_write_trailer(AVFormatContext *s)
559 AVIContext *avi = s->priv_data;
560 ByteIOContext *pb = s->pb;
561 int res = 0;
562 int i, j, n, nb_frames;
563 int64_t file_size;
565 if (!url_is_streamed(pb)){
566 if (avi->riff_id == 1) {
567 ff_end_tag(pb, avi->movi_list);
568 res = avi_write_idx1(s);
569 ff_end_tag(pb, avi->riff_start);
570 } else {
571 avi_write_ix(s);
572 ff_end_tag(pb, avi->movi_list);
573 ff_end_tag(pb, avi->riff_start);
575 file_size = url_ftell(pb);
576 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
577 put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
578 url_fskip(pb, 16);
580 for (n=nb_frames=0;n<s->nb_streams;n++) {
581 AVCodecContext *stream = s->streams[n]->codec;
582 if (stream->codec_type == CODEC_TYPE_VIDEO) {
583 if (nb_frames < avi->packet_count[n])
584 nb_frames = avi->packet_count[n];
585 } else {
586 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
587 nb_frames += avi->packet_count[n];
591 put_le32(pb, nb_frames);
592 url_fseek(pb, file_size, SEEK_SET);
594 avi_write_counters(s, avi->riff_id);
597 put_flush_packet(pb);
599 for (i=0; i<MAX_STREAMS; i++) {
600 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
601 av_free(avi->indexes[i].cluster[j]);
602 av_freep(&avi->indexes[i].cluster);
603 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
606 return res;
609 AVOutputFormat avi_muxer = {
610 "avi",
611 NULL_IF_CONFIG_SMALL("AVI format"),
612 "video/x-msvideo",
613 "avi",
614 sizeof(AVIContext),
615 CODEC_ID_MP2,
616 CODEC_ID_MPEG4,
617 avi_write_header,
618 avi_write_packet,
619 avi_write_trailer,
620 .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
621 .flags= AVFMT_VARIABLE_FPS,