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