• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/avienc.c

Go to the documentation of this file.
00001 /*
00002  * AVI muxer
00003  * Copyright (c) 2000 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avformat.h"
00022 #include "avi.h"
00023 #include "avio_internal.h"
00024 #include "riff.h"
00025 #include "libavutil/intreadwrite.h"
00026 #include "libavutil/dict.h"
00027 
00028 /*
00029  * TODO:
00030  *  - fill all fields if non streamed (nb_frames for example)
00031  */
00032 
00033 typedef struct AVIIentry {
00034     unsigned int flags, pos, len;
00035 } AVIIentry;
00036 
00037 #define AVI_INDEX_CLUSTER_SIZE 16384
00038 
00039 typedef struct AVIIndex {
00040     int64_t     indx_start;
00041     int         entry;
00042     int         ents_allocated;
00043     AVIIentry** cluster;
00044 } AVIIndex;
00045 
00046 typedef struct {
00047     int64_t riff_start, movi_list, odml_list;
00048     int64_t frames_hdr_all;
00049     int riff_id;
00050 } AVIContext;
00051 
00052 typedef struct  {
00053     int64_t frames_hdr_strm;
00054     int audio_strm_length;
00055     int packet_count;
00056     int entry;
00057 
00058     AVIIndex indexes;
00059 } AVIStream ;
00060 
00061 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
00062 {
00063     int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
00064     int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
00065     return &idx->cluster[cl][id];
00066 }
00067 
00068 static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
00069                                   const char* riff_tag, const char* list_tag)
00070 {
00071     AVIContext *avi= s->priv_data;
00072     int64_t loff;
00073     int i;
00074 
00075     avi->riff_id++;
00076     for (i=0; i<s->nb_streams; i++){
00077         AVIStream *avist= s->streams[i]->priv_data;
00078         avist->indexes.entry = 0;
00079     }
00080 
00081     avi->riff_start = ff_start_tag(pb, "RIFF");
00082     ffio_wfourcc(pb, riff_tag);
00083     loff = ff_start_tag(pb, "LIST");
00084     ffio_wfourcc(pb, list_tag);
00085     return loff;
00086 }
00087 
00088 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
00089 {
00090     tag[0] = '0' + index/10;
00091     tag[1] = '0' + index%10;
00092     if (type == AVMEDIA_TYPE_VIDEO) {
00093         tag[2] = 'd';
00094         tag[3] = 'c';
00095     } else if (type == AVMEDIA_TYPE_SUBTITLE) {
00096         // note: this is not an official code
00097         tag[2] = 's';
00098         tag[3] = 'b';
00099     } else {
00100         tag[2] = 'w';
00101         tag[3] = 'b';
00102     }
00103     tag[4] = '\0';
00104     return tag;
00105 }
00106 
00107 static void avi_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
00108 {
00109     int len = strlen(str);
00110     if (len > 0) {
00111         len++;
00112         ffio_wfourcc(pb, tag);
00113         avio_wl32(pb, len);
00114         avio_put_str(pb, str);
00115         if (len & 1)
00116             avio_w8(pb, 0);
00117     }
00118 }
00119 
00120 static int avi_write_counters(AVFormatContext* s, int riff_id)
00121 {
00122     AVIOContext *pb = s->pb;
00123     AVIContext *avi = s->priv_data;
00124     int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
00125     int64_t file_size;
00126     AVCodecContext* stream;
00127 
00128     file_size = avio_tell(pb);
00129     for(n = 0; n < s->nb_streams; n++) {
00130         AVIStream *avist= s->streams[n]->priv_data;
00131 
00132         assert(avist->frames_hdr_strm);
00133         stream = s->streams[n]->codec;
00134         avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
00135         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
00136         if(au_ssize == 0) {
00137             avio_wl32(pb, avist->packet_count);
00138         } else {
00139             avio_wl32(pb, avist->audio_strm_length / au_ssize);
00140         }
00141         if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
00142             nb_frames = FFMAX(nb_frames, avist->packet_count);
00143     }
00144     if(riff_id == 1) {
00145         assert(avi->frames_hdr_all);
00146         avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
00147         avio_wl32(pb, nb_frames);
00148     }
00149     avio_seek(pb, file_size, SEEK_SET);
00150 
00151     return 0;
00152 }
00153 
00154 static int avi_write_header(AVFormatContext *s)
00155 {
00156     AVIContext *avi = s->priv_data;
00157     AVIOContext *pb = s->pb;
00158     int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
00159     AVCodecContext *stream, *video_enc;
00160     int64_t list1, list2, strh, strf;
00161     AVDictionaryEntry *t = NULL;
00162 
00163     if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
00164         av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
00165                AVI_MAX_STREAM_COUNT);
00166         return -1;
00167     }
00168 
00169     for(n=0;n<s->nb_streams;n++) {
00170         s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
00171         if(!s->streams[n]->priv_data)
00172             return AVERROR(ENOMEM);
00173     }
00174 
00175     /* header list */
00176     avi->riff_id = 0;
00177     list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
00178 
00179     /* avi header */
00180     ffio_wfourcc(pb, "avih");
00181     avio_wl32(pb, 14 * 4);
00182     bitrate = 0;
00183 
00184     video_enc = NULL;
00185     for(n=0;n<s->nb_streams;n++) {
00186         stream = s->streams[n]->codec;
00187         bitrate += stream->bit_rate;
00188         if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
00189             video_enc = stream;
00190     }
00191 
00192     nb_frames = 0;
00193 
00194     if(video_enc){
00195         avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
00196     } else {
00197         avio_wl32(pb, 0);
00198     }
00199     avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
00200     avio_wl32(pb, 0); /* padding */
00201     if (!pb->seekable)
00202         avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
00203     else
00204         avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
00205     avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
00206     avio_wl32(pb, nb_frames); /* nb frames, filled later */
00207     avio_wl32(pb, 0); /* initial frame */
00208     avio_wl32(pb, s->nb_streams); /* nb streams */
00209     avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
00210     if(video_enc){
00211         avio_wl32(pb, video_enc->width);
00212         avio_wl32(pb, video_enc->height);
00213     } else {
00214         avio_wl32(pb, 0);
00215         avio_wl32(pb, 0);
00216     }
00217     avio_wl32(pb, 0); /* reserved */
00218     avio_wl32(pb, 0); /* reserved */
00219     avio_wl32(pb, 0); /* reserved */
00220     avio_wl32(pb, 0); /* reserved */
00221 
00222     /* stream list */
00223     for(i=0;i<n;i++) {
00224         AVIStream *avist= s->streams[i]->priv_data;
00225         list2 = ff_start_tag(pb, "LIST");
00226         ffio_wfourcc(pb, "strl");
00227 
00228         stream = s->streams[i]->codec;
00229 
00230         /* stream generic header */
00231         strh = ff_start_tag(pb, "strh");
00232         switch(stream->codec_type) {
00233         case AVMEDIA_TYPE_SUBTITLE:
00234             // XSUB subtitles behave like video tracks, other subtitles
00235             // are not (yet) supported.
00236             if (stream->codec_id != CODEC_ID_XSUB) {
00237                 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
00238                 return AVERROR_PATCHWELCOME;
00239             }
00240         case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
00241         case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
00242 //      case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
00243         case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
00244         }
00245         if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
00246            stream->codec_id == CODEC_ID_XSUB)
00247             avio_wl32(pb, stream->codec_tag);
00248         else
00249             avio_wl32(pb, 1);
00250         avio_wl32(pb, 0); /* flags */
00251         avio_wl16(pb, 0); /* priority */
00252         avio_wl16(pb, 0); /* language */
00253         avio_wl32(pb, 0); /* initial frame */
00254 
00255         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
00256 
00257         avio_wl32(pb, au_scale); /* scale */
00258         avio_wl32(pb, au_byterate); /* rate */
00259         av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
00260 
00261         avio_wl32(pb, 0); /* start */
00262         avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
00263         if (!pb->seekable)
00264             avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
00265         else
00266             avio_wl32(pb, 0); /* length, XXX: filled later */
00267 
00268         /* suggested buffer size */ //FIXME set at the end to largest chunk
00269         if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
00270             avio_wl32(pb, 1024 * 1024);
00271         else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
00272             avio_wl32(pb, 12 * 1024);
00273         else
00274             avio_wl32(pb, 0);
00275         avio_wl32(pb, -1); /* quality */
00276         avio_wl32(pb, au_ssize); /* sample size */
00277         avio_wl32(pb, 0);
00278         avio_wl16(pb, stream->width);
00279         avio_wl16(pb, stream->height);
00280         ff_end_tag(pb, strh);
00281 
00282       if(stream->codec_type != AVMEDIA_TYPE_DATA){
00283         strf = ff_start_tag(pb, "strf");
00284         switch(stream->codec_type) {
00285         case AVMEDIA_TYPE_SUBTITLE:
00286             // XSUB subtitles behave like video tracks, other subtitles
00287             // are not (yet) supported.
00288             if (stream->codec_id != CODEC_ID_XSUB) break;
00289         case AVMEDIA_TYPE_VIDEO:
00290             ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
00291             break;
00292         case AVMEDIA_TYPE_AUDIO:
00293             if (ff_put_wav_header(pb, stream) < 0) {
00294                 return -1;
00295             }
00296             break;
00297         default:
00298             return -1;
00299         }
00300         ff_end_tag(pb, strf);
00301         if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
00302             avi_write_info_tag(s->pb, "strn", t->value);
00303             t = NULL;
00304         }
00305       }
00306 
00307         if (pb->seekable) {
00308             unsigned char tag[5];
00309             int j;
00310 
00311             /* Starting to lay out AVI OpenDML master index.
00312              * We want to make it JUNK entry for now, since we'd
00313              * like to get away without making AVI an OpenDML one
00314              * for compatibility reasons.
00315              */
00316             avist->indexes.entry = avist->indexes.ents_allocated = 0;
00317             avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
00318             avio_wl16(pb, 4);        /* wLongsPerEntry */
00319             avio_w8(pb, 0);          /* bIndexSubType (0 == frame index) */
00320             avio_w8(pb, 0);          /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
00321             avio_wl32(pb, 0);        /* nEntriesInUse (will fill out later on) */
00322             ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
00323                                     /* dwChunkId */
00324             avio_wl64(pb, 0);        /* dwReserved[3]
00325             avio_wl32(pb, 0);           Must be 0.    */
00326             for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
00327                  avio_wl64(pb, 0);
00328             ff_end_tag(pb, avist->indexes.indx_start);
00329         }
00330 
00331         if(   stream->codec_type == AVMEDIA_TYPE_VIDEO
00332            && s->streams[i]->sample_aspect_ratio.num>0
00333            && s->streams[i]->sample_aspect_ratio.den>0){
00334             int vprp= ff_start_tag(pb, "vprp");
00335             AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
00336                                       (AVRational){stream->width, stream->height});
00337             int num, den;
00338             av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
00339 
00340             avio_wl32(pb, 0); //video format  = unknown
00341             avio_wl32(pb, 0); //video standard= unknown
00342             avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
00343             avio_wl32(pb, stream->width );
00344             avio_wl32(pb, stream->height);
00345             avio_wl16(pb, den);
00346             avio_wl16(pb, num);
00347             avio_wl32(pb, stream->width );
00348             avio_wl32(pb, stream->height);
00349             avio_wl32(pb, 1); //progressive FIXME
00350 
00351             avio_wl32(pb, stream->height);
00352             avio_wl32(pb, stream->width );
00353             avio_wl32(pb, stream->height);
00354             avio_wl32(pb, stream->width );
00355             avio_wl32(pb, 0);
00356             avio_wl32(pb, 0);
00357 
00358             avio_wl32(pb, 0);
00359             avio_wl32(pb, 0);
00360             ff_end_tag(pb, vprp);
00361         }
00362 
00363         ff_end_tag(pb, list2);
00364     }
00365 
00366     if (pb->seekable) {
00367         /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
00368         avi->odml_list = ff_start_tag(pb, "JUNK");
00369         ffio_wfourcc(pb, "odml");
00370         ffio_wfourcc(pb, "dmlh");
00371         avio_wl32(pb, 248);
00372         for (i = 0; i < 248; i+= 4)
00373              avio_wl32(pb, 0);
00374         ff_end_tag(pb, avi->odml_list);
00375     }
00376 
00377     ff_end_tag(pb, list1);
00378 
00379     list2 = ff_start_tag(pb, "LIST");
00380     ffio_wfourcc(pb, "INFO");
00381     ff_metadata_conv(&s->metadata, ff_avi_metadata_conv, NULL);
00382     for (i = 0; *ff_avi_tags[i]; i++) {
00383         if ((t = av_dict_get(s->metadata, ff_avi_tags[i], NULL, AV_DICT_MATCH_CASE)))
00384             avi_write_info_tag(s->pb, t->key, t->value);
00385     }
00386     ff_end_tag(pb, list2);
00387 
00388     /* some padding for easier tag editing */
00389     list2 = ff_start_tag(pb, "JUNK");
00390     for (i = 0; i < 1016; i += 4)
00391         avio_wl32(pb, 0);
00392     ff_end_tag(pb, list2);
00393 
00394     avi->movi_list = ff_start_tag(pb, "LIST");
00395     ffio_wfourcc(pb, "movi");
00396 
00397     avio_flush(pb);
00398 
00399     return 0;
00400 }
00401 
00402 static int avi_write_ix(AVFormatContext *s)
00403 {
00404     AVIOContext *pb = s->pb;
00405     AVIContext *avi = s->priv_data;
00406     char tag[5];
00407     char ix_tag[] = "ix00";
00408     int i, j;
00409 
00410     assert(pb->seekable);
00411 
00412     if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
00413         return -1;
00414 
00415     for (i=0;i<s->nb_streams;i++) {
00416         AVIStream *avist= s->streams[i]->priv_data;
00417          int64_t ix, pos;
00418 
00419          avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
00420          ix_tag[3] = '0' + i;
00421 
00422          /* Writing AVI OpenDML leaf index chunk */
00423          ix = avio_tell(pb);
00424          ffio_wfourcc(pb, ix_tag);     /* ix?? */
00425          avio_wl32(pb, avist->indexes.entry * 8 + 24);
00426                                       /* chunk size */
00427          avio_wl16(pb, 2);             /* wLongsPerEntry */
00428          avio_w8(pb, 0);             /* bIndexSubType (0 == frame index) */
00429          avio_w8(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
00430          avio_wl32(pb, avist->indexes.entry);
00431                                       /* nEntriesInUse */
00432          ffio_wfourcc(pb, tag);        /* dwChunkId */
00433          avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
00434          avio_wl32(pb, 0);             /* dwReserved_3 (must be 0) */
00435 
00436          for (j=0; j<avist->indexes.entry; j++) {
00437              AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
00438              avio_wl32(pb, ie->pos + 8);
00439              avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
00440                           (ie->flags & 0x10 ? 0 : 0x80000000));
00441          }
00442          avio_flush(pb);
00443          pos = avio_tell(pb);
00444 
00445          /* Updating one entry in the AVI OpenDML master index */
00446          avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
00447          ffio_wfourcc(pb, "indx");            /* enabling this entry */
00448          avio_skip(pb, 8);
00449          avio_wl32(pb, avi->riff_id);         /* nEntriesInUse */
00450          avio_skip(pb, 16*avi->riff_id);
00451          avio_wl64(pb, ix);                   /* qwOffset */
00452          avio_wl32(pb, pos - ix);             /* dwSize */
00453          avio_wl32(pb, avist->indexes.entry); /* dwDuration */
00454 
00455          avio_seek(pb, pos, SEEK_SET);
00456     }
00457     return 0;
00458 }
00459 
00460 static int avi_write_idx1(AVFormatContext *s)
00461 {
00462     AVIOContext *pb = s->pb;
00463     AVIContext *avi = s->priv_data;
00464     int64_t idx_chunk;
00465     int i;
00466     char tag[5];
00467 
00468     if (pb->seekable) {
00469         AVIStream *avist;
00470         AVIIentry* ie = 0, *tie;
00471         int empty, stream_id = -1;
00472 
00473         idx_chunk = ff_start_tag(pb, "idx1");
00474         for(i=0; i<s->nb_streams; i++){
00475             avist= s->streams[i]->priv_data;
00476             avist->entry=0;
00477         }
00478 
00479         do {
00480             empty = 1;
00481             for (i=0; i<s->nb_streams; i++) {
00482                 avist= s->streams[i]->priv_data;
00483                  if (avist->indexes.entry <= avist->entry)
00484                      continue;
00485 
00486                  tie = avi_get_ientry(&avist->indexes, avist->entry);
00487                  if (empty || tie->pos < ie->pos) {
00488                      ie = tie;
00489                      stream_id = i;
00490                  }
00491                  empty = 0;
00492             }
00493             if (!empty) {
00494                 avist= s->streams[stream_id]->priv_data;
00495                 avi_stream2fourcc(tag, stream_id,
00496                                   s->streams[stream_id]->codec->codec_type);
00497                 ffio_wfourcc(pb, tag);
00498                 avio_wl32(pb, ie->flags);
00499                 avio_wl32(pb, ie->pos);
00500                 avio_wl32(pb, ie->len);
00501                 avist->entry++;
00502             }
00503         } while (!empty);
00504         ff_end_tag(pb, idx_chunk);
00505 
00506         avi_write_counters(s, avi->riff_id);
00507     }
00508     return 0;
00509 }
00510 
00511 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
00512 {
00513     AVIContext *avi = s->priv_data;
00514     AVIOContext *pb = s->pb;
00515     unsigned char tag[5];
00516     unsigned int flags=0;
00517     const int stream_index= pkt->stream_index;
00518     AVIStream *avist= s->streams[stream_index]->priv_data;
00519     AVCodecContext *enc= s->streams[stream_index]->codec;
00520     int size= pkt->size;
00521 
00522 //    av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avist->packet_count, stream_index);
00523     while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
00524         AVPacket empty_packet;
00525 
00526         if(pkt->dts - avist->packet_count > 60000){
00527             av_log(s, AV_LOG_ERROR, "Too large number of skiped frames %Ld\n", pkt->dts - avist->packet_count);
00528             return AVERROR(EINVAL);
00529         }
00530 
00531         av_init_packet(&empty_packet);
00532         empty_packet.size= 0;
00533         empty_packet.data= NULL;
00534         empty_packet.stream_index= stream_index;
00535         avi_write_packet(s, &empty_packet);
00536 //        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avist->packet_count);
00537     }
00538     avist->packet_count++;
00539 
00540     // Make sure to put an OpenDML chunk when the file size exceeds the limits
00541     if (pb->seekable &&
00542         (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
00543 
00544         avi_write_ix(s);
00545         ff_end_tag(pb, avi->movi_list);
00546 
00547         if (avi->riff_id == 1)
00548             avi_write_idx1(s);
00549 
00550         ff_end_tag(pb, avi->riff_start);
00551         avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
00552     }
00553 
00554     avi_stream2fourcc(tag, stream_index, enc->codec_type);
00555     if(pkt->flags&AV_PKT_FLAG_KEY)
00556         flags = 0x10;
00557     if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00558        avist->audio_strm_length += size;
00559     }
00560 
00561     if (s->pb->seekable) {
00562         AVIIndex* idx = &avist->indexes;
00563         int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
00564         int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
00565         if (idx->ents_allocated <= idx->entry) {
00566             idx->cluster = av_realloc_f(idx->cluster, sizeof(void*), cl+1);
00567             if (!idx->cluster)
00568                 return -1;
00569             idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
00570             if (!idx->cluster[cl])
00571                 return -1;
00572             idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
00573         }
00574 
00575         idx->cluster[cl][id].flags = flags;
00576         idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
00577         idx->cluster[cl][id].len = size;
00578         idx->entry++;
00579     }
00580 
00581     avio_write(pb, tag, 4);
00582     avio_wl32(pb, size);
00583     avio_write(pb, pkt->data, size);
00584     if (size & 1)
00585         avio_w8(pb, 0);
00586 
00587     avio_flush(pb);
00588     return 0;
00589 }
00590 
00591 static int avi_write_trailer(AVFormatContext *s)
00592 {
00593     AVIContext *avi = s->priv_data;
00594     AVIOContext *pb = s->pb;
00595     int res = 0;
00596     int i, j, n, nb_frames;
00597     int64_t file_size;
00598 
00599     if (pb->seekable){
00600         if (avi->riff_id == 1) {
00601             ff_end_tag(pb, avi->movi_list);
00602             res = avi_write_idx1(s);
00603             ff_end_tag(pb, avi->riff_start);
00604         } else {
00605             avi_write_ix(s);
00606             ff_end_tag(pb, avi->movi_list);
00607             ff_end_tag(pb, avi->riff_start);
00608 
00609             file_size = avio_tell(pb);
00610             avio_seek(pb, avi->odml_list - 8, SEEK_SET);
00611             ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
00612             avio_skip(pb, 16);
00613 
00614             for (n=nb_frames=0;n<s->nb_streams;n++) {
00615                 AVCodecContext *stream = s->streams[n]->codec;
00616                 AVIStream *avist= s->streams[n]->priv_data;
00617 
00618                 if (stream->codec_type == AVMEDIA_TYPE_VIDEO) {
00619                     if (nb_frames < avist->packet_count)
00620                         nb_frames = avist->packet_count;
00621                 } else {
00622                     if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
00623                         nb_frames += avist->packet_count;
00624                     }
00625                 }
00626             }
00627             avio_wl32(pb, nb_frames);
00628             avio_seek(pb, file_size, SEEK_SET);
00629 
00630             avi_write_counters(s, avi->riff_id);
00631         }
00632     }
00633     avio_flush(pb);
00634 
00635     for (i=0; i<s->nb_streams; i++) {
00636          AVIStream *avist= s->streams[i]->priv_data;
00637          for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
00638               av_free(avist->indexes.cluster[j]);
00639          av_freep(&avist->indexes.cluster);
00640          avist->indexes.ents_allocated = avist->indexes.entry = 0;
00641     }
00642 
00643     return res;
00644 }
00645 
00646 AVOutputFormat ff_avi_muxer = {
00647     "avi",
00648     NULL_IF_CONFIG_SMALL("AVI format"),
00649     "video/x-msvideo",
00650     "avi",
00651     sizeof(AVIContext),
00652     CODEC_ID_MP2,
00653     CODEC_ID_MPEG4,
00654     avi_write_header,
00655     avi_write_packet,
00656     avi_write_trailer,
00657     .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
00658     .flags= AVFMT_VARIABLE_FPS,
00659 };

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1