avienc.c
Go to the documentation of this file.
1 /*
2  * AVI muxer
3  * Copyright (c) 2000 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "avformat.h"
22 #include "internal.h"
23 #include "avi.h"
24 #include "avio_internal.h"
25 #include "riff.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/dict.h"
28 
29 /*
30  * TODO:
31  * - fill all fields if non streamed (nb_frames for example)
32  */
33 
34 typedef struct AVIIentry {
35  unsigned int flags, pos, len;
36 } AVIIentry;
37 
38 #define AVI_INDEX_CLUSTER_SIZE 16384
39 
40 typedef struct AVIIndex {
41  int64_t indx_start;
42  int entry;
45 } AVIIndex;
46 
47 typedef struct {
48  int64_t riff_start, movi_list, odml_list;
49  int64_t frames_hdr_all;
50  int riff_id;
51 } AVIContext;
52 
53 typedef struct {
54  int64_t frames_hdr_strm;
57  int entry;
58 
60 } AVIStream ;
61 
62 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
63 {
64  int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
65  int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
66  return &idx->cluster[cl][id];
67 }
68 
70  const char* riff_tag, const char* list_tag)
71 {
72  AVIContext *avi= s->priv_data;
73  int64_t loff;
74  int i;
75 
76  avi->riff_id++;
77  for (i=0; i<s->nb_streams; i++){
78  AVIStream *avist= s->streams[i]->priv_data;
79  avist->indexes.entry = 0;
80  }
81 
82  avi->riff_start = ff_start_tag(pb, "RIFF");
83  ffio_wfourcc(pb, riff_tag);
84  loff = ff_start_tag(pb, "LIST");
85  ffio_wfourcc(pb, list_tag);
86  return loff;
87 }
88 
89 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
90 {
91  tag[0] = '0' + index/10;
92  tag[1] = '0' + index%10;
93  if (type == AVMEDIA_TYPE_VIDEO) {
94  tag[2] = 'd';
95  tag[3] = 'c';
96  } else if (type == AVMEDIA_TYPE_SUBTITLE) {
97  // note: this is not an official code
98  tag[2] = 's';
99  tag[3] = 'b';
100  } else {
101  tag[2] = 'w';
102  tag[3] = 'b';
103  }
104  tag[4] = '\0';
105  return tag;
106 }
107 
108 static int avi_write_counters(AVFormatContext* s, int riff_id)
109 {
110  AVIOContext *pb = s->pb;
111  AVIContext *avi = s->priv_data;
112  int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
113  int64_t file_size;
114  AVCodecContext* stream;
115 
116  file_size = avio_tell(pb);
117  for(n = 0; n < s->nb_streams; n++) {
118  AVIStream *avist= s->streams[n]->priv_data;
119 
120  assert(avist->frames_hdr_strm);
121  stream = s->streams[n]->codec;
122  avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
123  ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
124  if(au_ssize == 0) {
125  avio_wl32(pb, avist->packet_count);
126  } else {
127  avio_wl32(pb, avist->audio_strm_length / au_ssize);
128  }
129  if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
130  nb_frames = FFMAX(nb_frames, avist->packet_count);
131  }
132  if(riff_id == 1) {
133  assert(avi->frames_hdr_all);
134  avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
135  avio_wl32(pb, nb_frames);
136  }
137  avio_seek(pb, file_size, SEEK_SET);
138 
139  return 0;
140 }
141 
143 {
144  AVIContext *avi = s->priv_data;
145  AVIOContext *pb = s->pb;
146  int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
147  AVCodecContext *stream, *video_enc;
148  int64_t list1, list2, strh, strf;
150 
151  if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
152  av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
154  return -1;
155  }
156 
157  for(n=0;n<s->nb_streams;n++) {
158  s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
159  if(!s->streams[n]->priv_data)
160  return AVERROR(ENOMEM);
161  }
162 
163  /* header list */
164  avi->riff_id = 0;
165  list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
166 
167  /* avi header */
168  ffio_wfourcc(pb, "avih");
169  avio_wl32(pb, 14 * 4);
170  bitrate = 0;
171 
172  video_enc = NULL;
173  for(n=0;n<s->nb_streams;n++) {
174  stream = s->streams[n]->codec;
175  bitrate += stream->bit_rate;
176  if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
177  video_enc = stream;
178  }
179 
180  nb_frames = 0;
181 
182  if(video_enc){
183  avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
184  } else {
185  avio_wl32(pb, 0);
186  }
187  avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
188  avio_wl32(pb, 0); /* padding */
189  if (!pb->seekable)
190  avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
191  else
193  avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
194  avio_wl32(pb, nb_frames); /* nb frames, filled later */
195  avio_wl32(pb, 0); /* initial frame */
196  avio_wl32(pb, s->nb_streams); /* nb streams */
197  avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
198  if(video_enc){
199  avio_wl32(pb, video_enc->width);
200  avio_wl32(pb, video_enc->height);
201  } else {
202  avio_wl32(pb, 0);
203  avio_wl32(pb, 0);
204  }
205  avio_wl32(pb, 0); /* reserved */
206  avio_wl32(pb, 0); /* reserved */
207  avio_wl32(pb, 0); /* reserved */
208  avio_wl32(pb, 0); /* reserved */
209 
210  /* stream list */
211  for(i=0;i<n;i++) {
212  AVIStream *avist= s->streams[i]->priv_data;
213  list2 = ff_start_tag(pb, "LIST");
214  ffio_wfourcc(pb, "strl");
215 
216  stream = s->streams[i]->codec;
217 
218  /* stream generic header */
219  strh = ff_start_tag(pb, "strh");
220  switch(stream->codec_type) {
222  // XSUB subtitles behave like video tracks, other subtitles
223  // are not (yet) supported.
224  if (stream->codec_id != AV_CODEC_ID_XSUB) {
225  av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
226  return AVERROR_PATCHWELCOME;
227  }
228  case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
229  case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
230 // case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
231  case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
232  }
233  if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
234  stream->codec_id == AV_CODEC_ID_XSUB)
235  avio_wl32(pb, stream->codec_tag);
236  else
237  avio_wl32(pb, 1);
238  avio_wl32(pb, 0); /* flags */
239  avio_wl16(pb, 0); /* priority */
240  avio_wl16(pb, 0); /* language */
241  avio_wl32(pb, 0); /* initial frame */
242 
243  ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
244 
245  avio_wl32(pb, au_scale); /* scale */
246  avio_wl32(pb, au_byterate); /* rate */
247  avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
248 
249  avio_wl32(pb, 0); /* start */
250  avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
251  if (!pb->seekable)
252  avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
253  else
254  avio_wl32(pb, 0); /* length, XXX: filled later */
255 
256  /* suggested buffer size */ //FIXME set at the end to largest chunk
257  if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
258  avio_wl32(pb, 1024 * 1024);
259  else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
260  avio_wl32(pb, 12 * 1024);
261  else
262  avio_wl32(pb, 0);
263  avio_wl32(pb, -1); /* quality */
264  avio_wl32(pb, au_ssize); /* sample size */
265  avio_wl32(pb, 0);
266  avio_wl16(pb, stream->width);
267  avio_wl16(pb, stream->height);
268  ff_end_tag(pb, strh);
269 
270  if(stream->codec_type != AVMEDIA_TYPE_DATA){
271  strf = ff_start_tag(pb, "strf");
272  switch(stream->codec_type) {
274  // XSUB subtitles behave like video tracks, other subtitles
275  // are not (yet) supported.
276  if (stream->codec_id != AV_CODEC_ID_XSUB) break;
277  case AVMEDIA_TYPE_VIDEO:
278  ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
279  break;
280  case AVMEDIA_TYPE_AUDIO:
281  if (ff_put_wav_header(pb, stream) < 0) {
282  return -1;
283  }
284  break;
285  default:
286  return -1;
287  }
288  ff_end_tag(pb, strf);
289  if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
290  ff_riff_write_info_tag(s->pb, "strn", t->value);
291  t = NULL;
292  }
293  }
294 
295  if (pb->seekable) {
296  unsigned char tag[5];
297  int j;
298 
299  /* Starting to lay out AVI OpenDML master index.
300  * We want to make it JUNK entry for now, since we'd
301  * like to get away without making AVI an OpenDML one
302  * for compatibility reasons.
303  */
304  avist->indexes.entry = avist->indexes.ents_allocated = 0;
305  avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
306  avio_wl16(pb, 4); /* wLongsPerEntry */
307  avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
308  avio_w8(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
309  avio_wl32(pb, 0); /* nEntriesInUse (will fill out later on) */
310  ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
311  /* dwChunkId */
312  avio_wl64(pb, 0); /* dwReserved[3]
313  avio_wl32(pb, 0); Must be 0. */
314  for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
315  avio_wl64(pb, 0);
316  ff_end_tag(pb, avist->indexes.indx_start);
317  }
318 
319  if( stream->codec_type == AVMEDIA_TYPE_VIDEO
320  && s->streams[i]->sample_aspect_ratio.num>0
321  && s->streams[i]->sample_aspect_ratio.den>0){
322  int vprp= ff_start_tag(pb, "vprp");
324  (AVRational){stream->width, stream->height});
325  int num, den;
326  av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
327 
328  avio_wl32(pb, 0); //video format = unknown
329  avio_wl32(pb, 0); //video standard= unknown
330  avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
331  avio_wl32(pb, stream->width );
332  avio_wl32(pb, stream->height);
333  avio_wl16(pb, den);
334  avio_wl16(pb, num);
335  avio_wl32(pb, stream->width );
336  avio_wl32(pb, stream->height);
337  avio_wl32(pb, 1); //progressive FIXME
338 
339  avio_wl32(pb, stream->height);
340  avio_wl32(pb, stream->width );
341  avio_wl32(pb, stream->height);
342  avio_wl32(pb, stream->width );
343  avio_wl32(pb, 0);
344  avio_wl32(pb, 0);
345 
346  avio_wl32(pb, 0);
347  avio_wl32(pb, 0);
348  ff_end_tag(pb, vprp);
349  }
350 
351  ff_end_tag(pb, list2);
352  }
353 
354  if (pb->seekable) {
355  /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
356  avi->odml_list = ff_start_tag(pb, "JUNK");
357  ffio_wfourcc(pb, "odml");
358  ffio_wfourcc(pb, "dmlh");
359  avio_wl32(pb, 248);
360  for (i = 0; i < 248; i+= 4)
361  avio_wl32(pb, 0);
362  ff_end_tag(pb, avi->odml_list);
363  }
364 
365  ff_end_tag(pb, list1);
366 
368 
369  /* some padding for easier tag editing */
370  list2 = ff_start_tag(pb, "JUNK");
371  for (i = 0; i < 1016; i += 4)
372  avio_wl32(pb, 0);
373  ff_end_tag(pb, list2);
374 
375  avi->movi_list = ff_start_tag(pb, "LIST");
376  ffio_wfourcc(pb, "movi");
377 
378  avio_flush(pb);
379 
380  return 0;
381 }
382 
384 {
385  AVIOContext *pb = s->pb;
386  AVIContext *avi = s->priv_data;
387  char tag[5];
388  char ix_tag[] = "ix00";
389  int i, j;
390 
391  assert(pb->seekable);
392 
393  if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
394  return -1;
395 
396  for (i=0;i<s->nb_streams;i++) {
397  AVIStream *avist= s->streams[i]->priv_data;
398  int64_t ix, pos;
399 
400  avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
401  ix_tag[3] = '0' + i;
402 
403  /* Writing AVI OpenDML leaf index chunk */
404  ix = avio_tell(pb);
405  ffio_wfourcc(pb, ix_tag); /* ix?? */
406  avio_wl32(pb, avist->indexes.entry * 8 + 24);
407  /* chunk size */
408  avio_wl16(pb, 2); /* wLongsPerEntry */
409  avio_w8(pb, 0); /* bIndexSubType (0 == frame index) */
410  avio_w8(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
411  avio_wl32(pb, avist->indexes.entry);
412  /* nEntriesInUse */
413  ffio_wfourcc(pb, tag); /* dwChunkId */
414  avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
415  avio_wl32(pb, 0); /* dwReserved_3 (must be 0) */
416 
417  for (j=0; j<avist->indexes.entry; j++) {
418  AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
419  avio_wl32(pb, ie->pos + 8);
420  avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
421  (ie->flags & 0x10 ? 0 : 0x80000000));
422  }
423  avio_flush(pb);
424  pos = avio_tell(pb);
425 
426  /* Updating one entry in the AVI OpenDML master index */
427  avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
428  ffio_wfourcc(pb, "indx"); /* enabling this entry */
429  avio_skip(pb, 8);
430  avio_wl32(pb, avi->riff_id); /* nEntriesInUse */
431  avio_skip(pb, 16*avi->riff_id);
432  avio_wl64(pb, ix); /* qwOffset */
433  avio_wl32(pb, pos - ix); /* dwSize */
434  avio_wl32(pb, avist->indexes.entry); /* dwDuration */
435 
436  avio_seek(pb, pos, SEEK_SET);
437  }
438  return 0;
439 }
440 
442 {
443  AVIOContext *pb = s->pb;
444  AVIContext *avi = s->priv_data;
445  int64_t idx_chunk;
446  int i;
447  char tag[5];
448 
449  if (pb->seekable) {
450  AVIStream *avist;
451  AVIIentry* ie = 0, *tie;
452  int empty, stream_id = -1;
453 
454  idx_chunk = ff_start_tag(pb, "idx1");
455  for(i=0; i<s->nb_streams; i++){
456  avist= s->streams[i]->priv_data;
457  avist->entry=0;
458  }
459 
460  do {
461  empty = 1;
462  for (i=0; i<s->nb_streams; i++) {
463  avist= s->streams[i]->priv_data;
464  if (avist->indexes.entry <= avist->entry)
465  continue;
466 
467  tie = avi_get_ientry(&avist->indexes, avist->entry);
468  if (empty || tie->pos < ie->pos) {
469  ie = tie;
470  stream_id = i;
471  }
472  empty = 0;
473  }
474  if (!empty) {
475  avist= s->streams[stream_id]->priv_data;
476  avi_stream2fourcc(tag, stream_id,
477  s->streams[stream_id]->codec->codec_type);
478  ffio_wfourcc(pb, tag);
479  avio_wl32(pb, ie->flags);
480  avio_wl32(pb, ie->pos);
481  avio_wl32(pb, ie->len);
482  avist->entry++;
483  }
484  } while (!empty);
485  ff_end_tag(pb, idx_chunk);
486 
487  avi_write_counters(s, avi->riff_id);
488  }
489  return 0;
490 }
491 
493 {
494  AVIContext *avi = s->priv_data;
495  AVIOContext *pb = s->pb;
496  unsigned char tag[5];
497  unsigned int flags=0;
498  const int stream_index= pkt->stream_index;
499  AVIStream *avist= s->streams[stream_index]->priv_data;
500  AVCodecContext *enc= s->streams[stream_index]->codec;
501  int size= pkt->size;
502 
503  while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
504  AVPacket empty_packet;
505 
506  av_init_packet(&empty_packet);
507  empty_packet.size= 0;
508  empty_packet.data= NULL;
509  empty_packet.stream_index= stream_index;
510  avi_write_packet(s, &empty_packet);
511  }
512  avist->packet_count++;
513 
514  // Make sure to put an OpenDML chunk when the file size exceeds the limits
515  if (pb->seekable &&
516  (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
517 
518  avi_write_ix(s);
519  ff_end_tag(pb, avi->movi_list);
520 
521  if (avi->riff_id == 1)
522  avi_write_idx1(s);
523 
524  ff_end_tag(pb, avi->riff_start);
525  avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
526  }
527 
528  avi_stream2fourcc(tag, stream_index, enc->codec_type);
529  if(pkt->flags&AV_PKT_FLAG_KEY)
530  flags = 0x10;
531  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
532  avist->audio_strm_length += size;
533  }
534 
535  if (s->pb->seekable) {
536  AVIIndex* idx = &avist->indexes;
537  int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
538  int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
539  if (idx->ents_allocated <= idx->entry) {
540  idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
541  if (!idx->cluster)
542  return -1;
544  if (!idx->cluster[cl])
545  return -1;
547  }
548 
549  idx->cluster[cl][id].flags = flags;
550  idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
551  idx->cluster[cl][id].len = size;
552  idx->entry++;
553  }
554 
555  avio_write(pb, tag, 4);
556  avio_wl32(pb, size);
557  avio_write(pb, pkt->data, size);
558  if (size & 1)
559  avio_w8(pb, 0);
560 
561  avio_flush(pb);
562  return 0;
563 }
564 
566 {
567  AVIContext *avi = s->priv_data;
568  AVIOContext *pb = s->pb;
569  int res = 0;
570  int i, j, n, nb_frames;
571  int64_t file_size;
572 
573  if (pb->seekable){
574  if (avi->riff_id == 1) {
575  ff_end_tag(pb, avi->movi_list);
576  res = avi_write_idx1(s);
577  ff_end_tag(pb, avi->riff_start);
578  } else {
579  avi_write_ix(s);
580  ff_end_tag(pb, avi->movi_list);
581  ff_end_tag(pb, avi->riff_start);
582 
583  file_size = avio_tell(pb);
584  avio_seek(pb, avi->odml_list - 8, SEEK_SET);
585  ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
586  avio_skip(pb, 16);
587 
588  for (n=nb_frames=0;n<s->nb_streams;n++) {
589  AVCodecContext *stream = s->streams[n]->codec;
590  AVIStream *avist= s->streams[n]->priv_data;
591 
592  if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
593  if (nb_frames < avist->packet_count)
594  nb_frames = avist->packet_count;
595  } else {
596  if (stream->codec_id == AV_CODEC_ID_MP2 || stream->codec_id == AV_CODEC_ID_MP3) {
597  nb_frames += avist->packet_count;
598  }
599  }
600  }
601  avio_wl32(pb, nb_frames);
602  avio_seek(pb, file_size, SEEK_SET);
603 
604  avi_write_counters(s, avi->riff_id);
605  }
606  }
607 
608  for (i=0; i<s->nb_streams; i++) {
609  AVIStream *avist= s->streams[i]->priv_data;
610  for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
611  av_free(avist->indexes.cluster[j]);
612  av_freep(&avist->indexes.cluster);
613  avist->indexes.ents_allocated = avist->indexes.entry = 0;
614  }
615 
616  return res;
617 }
618 
620  .name = "avi",
621  .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
622  .mime_type = "video/x-msvideo",
623  .extensions = "avi",
624  .priv_data_size = sizeof(AVIContext),
626  .video_codec = AV_CODEC_ID_MPEG4,
630  .codec_tag = (const AVCodecTag* const []){
632  },
633  .flags = AVFMT_VARIABLE_FPS,
634 };