00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "avi.h"
00023 #include "riff.h"
00024
00025
00026
00027
00028
00029
00030 typedef struct AVIIentry {
00031 unsigned int flags, pos, len;
00032 } AVIIentry;
00033
00034 #define AVI_INDEX_CLUSTER_SIZE 16384
00035
00036 typedef struct AVIIndex {
00037 int64_t indx_start;
00038 int entry;
00039 int ents_allocated;
00040 AVIIentry** cluster;
00041 } AVIIndex;
00042
00043 typedef struct {
00044 int64_t riff_start, movi_list, odml_list;
00045 int64_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
00046 int audio_strm_length[MAX_STREAMS];
00047 int riff_id;
00048 int packet_count[MAX_STREAMS];
00049
00050 AVIIndex indexes[MAX_STREAMS];
00051 } AVIContext;
00052
00053 static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
00054 {
00055 int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
00056 int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
00057 return &idx->cluster[cl][id];
00058 }
00059
00060 static int64_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
00061 const char* riff_tag, const char* list_tag)
00062 {
00063 int64_t loff;
00064 int i;
00065
00066 avi->riff_id++;
00067 for (i=0; i<MAX_STREAMS; i++)
00068 avi->indexes[i].entry = 0;
00069
00070 avi->riff_start = start_tag(pb, "RIFF");
00071 put_tag(pb, riff_tag);
00072 loff = start_tag(pb, "LIST");
00073 put_tag(pb, list_tag);
00074 return loff;
00075 }
00076
00077 static char* avi_stream2fourcc(char* tag, int index, enum CodecType type)
00078 {
00079 tag[0] = '0';
00080 tag[1] = '0' + index;
00081 if (type == CODEC_TYPE_VIDEO) {
00082 tag[2] = 'd';
00083 tag[3] = 'c';
00084 } else {
00085 tag[2] = 'w';
00086 tag[3] = 'b';
00087 }
00088 tag[4] = '\0';
00089 return tag;
00090 }
00091
00092 static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
00093 {
00094 int len = strlen(str);
00095 if (len > 0) {
00096 len++;
00097 put_tag(pb, tag);
00098 put_le32(pb, len);
00099 put_strz(pb, str);
00100 if (len & 1)
00101 put_byte(pb, 0);
00102 }
00103 }
00104
00105 static void avi_write_info_tag2(AVFormatContext *s, const char *fourcc, const char *key1, const char *key2)
00106 {
00107 AVMetadataTag *tag= av_metadata_get(s->metadata, key1, NULL, 0);
00108 if(!tag && key2)
00109 tag= av_metadata_get(s->metadata, key2, NULL, 0);
00110 if(tag)
00111 avi_write_info_tag(s->pb, fourcc, tag->value);
00112 }
00113
00114 static int avi_write_counters(AVFormatContext* s, int riff_id)
00115 {
00116 ByteIOContext *pb = s->pb;
00117 AVIContext *avi = s->priv_data;
00118 int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
00119 int64_t file_size;
00120 AVCodecContext* stream;
00121
00122 file_size = url_ftell(pb);
00123 for(n = 0; n < s->nb_streams; n++) {
00124 assert(avi->frames_hdr_strm[n]);
00125 stream = s->streams[n]->codec;
00126 url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
00127 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
00128 if(au_ssize == 0) {
00129 put_le32(pb, avi->packet_count[n]);
00130 } else {
00131 put_le32(pb, avi->audio_strm_length[n] / au_ssize);
00132 }
00133 if(stream->codec_type == CODEC_TYPE_VIDEO)
00134 nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
00135 }
00136 if(riff_id == 1) {
00137 assert(avi->frames_hdr_all);
00138 url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
00139 put_le32(pb, nb_frames);
00140 }
00141 url_fseek(pb, file_size, SEEK_SET);
00142
00143 return 0;
00144 }
00145
00146 static int avi_write_header(AVFormatContext *s)
00147 {
00148 AVIContext *avi = s->priv_data;
00149 ByteIOContext *pb = s->pb;
00150 int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
00151 AVCodecContext *stream, *video_enc;
00152 int64_t list1, list2, strh, strf;
00153
00154
00155 avi->riff_id = 0;
00156 list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
00157
00158
00159 put_tag(pb, "avih");
00160 put_le32(pb, 14 * 4);
00161 bitrate = 0;
00162
00163 video_enc = NULL;
00164 for(n=0;n<s->nb_streams;n++) {
00165 stream = s->streams[n]->codec;
00166 bitrate += stream->bit_rate;
00167 if (stream->codec_type == CODEC_TYPE_VIDEO)
00168 video_enc = stream;
00169 }
00170
00171 nb_frames = 0;
00172
00173 if(video_enc){
00174 put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
00175 } else {
00176 put_le32(pb, 0);
00177 }
00178 put_le32(pb, bitrate / 8);
00179 put_le32(pb, 0);
00180 if (url_is_streamed(pb))
00181 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED);
00182 else
00183 put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED);
00184 avi->frames_hdr_all = url_ftell(pb);
00185 put_le32(pb, nb_frames);
00186 put_le32(pb, 0);
00187 put_le32(pb, s->nb_streams);
00188 put_le32(pb, 1024 * 1024);
00189 if(video_enc){
00190 put_le32(pb, video_enc->width);
00191 put_le32(pb, video_enc->height);
00192 } else {
00193 put_le32(pb, 0);
00194 put_le32(pb, 0);
00195 }
00196 put_le32(pb, 0);
00197 put_le32(pb, 0);
00198 put_le32(pb, 0);
00199 put_le32(pb, 0);
00200
00201
00202 for(i=0;i<n;i++) {
00203 list2 = start_tag(pb, "LIST");
00204 put_tag(pb, "strl");
00205
00206 stream = s->streams[i]->codec;
00207
00208
00209 strh = start_tag(pb, "strh");
00210 switch(stream->codec_type) {
00211 case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
00212 case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
00213
00214 case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
00215 }
00216 if(stream->codec_type == CODEC_TYPE_VIDEO)
00217 put_le32(pb, stream->codec_tag);
00218 else
00219 put_le32(pb, 1);
00220 put_le32(pb, 0);
00221 put_le16(pb, 0);
00222 put_le16(pb, 0);
00223 put_le32(pb, 0);
00224
00225 ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
00226
00227 put_le32(pb, au_scale);
00228 put_le32(pb, au_byterate);
00229 av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
00230
00231 put_le32(pb, 0);
00232 avi->frames_hdr_strm[i] = url_ftell(pb);
00233 if (url_is_streamed(pb))
00234 put_le32(pb, AVI_MAX_RIFF_SIZE);
00235 else
00236 put_le32(pb, 0);
00237
00238
00239 if(stream->codec_type == CODEC_TYPE_VIDEO)
00240 put_le32(pb, 1024 * 1024);
00241 else if(stream->codec_type == CODEC_TYPE_AUDIO)
00242 put_le32(pb, 12 * 1024);
00243 else
00244 put_le32(pb, 0);
00245 put_le32(pb, -1);
00246 put_le32(pb, au_ssize);
00247 put_le32(pb, 0);
00248 put_le16(pb, stream->width);
00249 put_le16(pb, stream->height);
00250 end_tag(pb, strh);
00251
00252 if(stream->codec_type != CODEC_TYPE_DATA){
00253 strf = start_tag(pb, "strf");
00254 switch(stream->codec_type) {
00255 case CODEC_TYPE_VIDEO:
00256 put_bmp_header(pb, stream, codec_bmp_tags, 0);
00257 break;
00258 case CODEC_TYPE_AUDIO:
00259 if (put_wav_header(pb, stream) < 0) {
00260 return -1;
00261 }
00262 break;
00263 default:
00264 return -1;
00265 }
00266 end_tag(pb, strf);
00267 }
00268
00269 if (!url_is_streamed(pb)) {
00270 unsigned char tag[5];
00271 int j;
00272
00273
00274
00275
00276
00277
00278 avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
00279 avi->indexes[i].indx_start = start_tag(pb, "JUNK");
00280 put_le16(pb, 4);
00281 put_byte(pb, 0);
00282 put_byte(pb, 0);
00283 put_le32(pb, 0);
00284 put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
00285
00286 put_le64(pb, 0);
00287
00288 for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
00289 put_le64(pb, 0);
00290 end_tag(pb, avi->indexes[i].indx_start);
00291 }
00292
00293 if( stream->codec_type == CODEC_TYPE_VIDEO
00294 && s->streams[i]->sample_aspect_ratio.num>0
00295 && s->streams[i]->sample_aspect_ratio.den>0){
00296 int vprp= start_tag(pb, "vprp");
00297 AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
00298 (AVRational){stream->width, stream->height});
00299 int num, den;
00300 av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
00301
00302 put_le32(pb, 0);
00303 put_le32(pb, 0);
00304 put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
00305 put_le32(pb, stream->width );
00306 put_le32(pb, stream->height);
00307 put_le16(pb, den);
00308 put_le16(pb, num);
00309 put_le32(pb, stream->width );
00310 put_le32(pb, stream->height);
00311 put_le32(pb, 1);
00312
00313 put_le32(pb, stream->height);
00314 put_le32(pb, stream->width );
00315 put_le32(pb, stream->height);
00316 put_le32(pb, stream->width );
00317 put_le32(pb, 0);
00318 put_le32(pb, 0);
00319
00320 put_le32(pb, 0);
00321 put_le32(pb, 0);
00322 end_tag(pb, vprp);
00323 }
00324
00325 end_tag(pb, list2);
00326 }
00327
00328 if (!url_is_streamed(pb)) {
00329
00330 avi->odml_list = start_tag(pb, "JUNK");
00331 put_tag(pb, "odml");
00332 put_tag(pb, "dmlh");
00333 put_le32(pb, 248);
00334 for (i = 0; i < 248; i+= 4)
00335 put_le32(pb, 0);
00336 end_tag(pb, avi->odml_list);
00337 }
00338
00339 end_tag(pb, list1);
00340
00341 list2 = start_tag(pb, "LIST");
00342 put_tag(pb, "INFO");
00343 avi_write_info_tag2(s, "INAM", "Title", NULL);
00344 avi_write_info_tag2(s, "IART", "Artist", "Author");
00345 avi_write_info_tag2(s, "ICOP", "Copyright", NULL);
00346 avi_write_info_tag2(s, "ICMT", "Comment", NULL);
00347 avi_write_info_tag2(s, "IPRD", "Album", NULL);
00348 avi_write_info_tag2(s, "IGNR", "Genre", NULL);
00349 avi_write_info_tag2(s, "IPRT", "Track", NULL);
00350 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
00351 avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
00352 end_tag(pb, list2);
00353
00354
00355 list2 = start_tag(pb, "JUNK");
00356 for (i = 0; i < 1016; i += 4)
00357 put_le32(pb, 0);
00358 end_tag(pb, list2);
00359
00360 avi->movi_list = start_tag(pb, "LIST");
00361 put_tag(pb, "movi");
00362
00363 put_flush_packet(pb);
00364
00365 return 0;
00366 }
00367
00368 static int avi_write_ix(AVFormatContext *s)
00369 {
00370 ByteIOContext *pb = s->pb;
00371 AVIContext *avi = s->priv_data;
00372 char tag[5];
00373 char ix_tag[] = "ix00";
00374 int i, j;
00375
00376 assert(!url_is_streamed(pb));
00377
00378 if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
00379 return -1;
00380
00381 for (i=0;i<s->nb_streams;i++) {
00382 int64_t ix, pos;
00383
00384 avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
00385 ix_tag[3] = '0' + i;
00386
00387
00388 ix = url_ftell(pb);
00389 put_tag(pb, &ix_tag[0]);
00390 put_le32(pb, avi->indexes[i].entry * 8 + 24);
00391
00392 put_le16(pb, 2);
00393 put_byte(pb, 0);
00394 put_byte(pb, 1);
00395 put_le32(pb, avi->indexes[i].entry);
00396
00397 put_tag(pb, &tag[0]);
00398 put_le64(pb, avi->movi_list);
00399 put_le32(pb, 0);
00400
00401 for (j=0; j<avi->indexes[i].entry; j++) {
00402 AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
00403 put_le32(pb, ie->pos + 8);
00404 put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
00405 (ie->flags & 0x10 ? 0 : 0x80000000));
00406 }
00407 put_flush_packet(pb);
00408 pos = url_ftell(pb);
00409
00410
00411 url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
00412 put_tag(pb, "indx");
00413 url_fskip(pb, 8);
00414 put_le32(pb, avi->riff_id);
00415 url_fskip(pb, 16*avi->riff_id);
00416 put_le64(pb, ix);
00417 put_le32(pb, pos - ix);
00418 put_le32(pb, avi->indexes[i].entry);
00419
00420 url_fseek(pb, pos, SEEK_SET);
00421 }
00422 return 0;
00423 }
00424
00425 static int avi_write_idx1(AVFormatContext *s)
00426 {
00427 ByteIOContext *pb = s->pb;
00428 AVIContext *avi = s->priv_data;
00429 int64_t idx_chunk;
00430 int i;
00431 char tag[5];
00432
00433 if (!url_is_streamed(pb)) {
00434 AVIIentry* ie = 0, *tie;
00435 int entry[MAX_STREAMS];
00436 int empty, stream_id = -1;
00437
00438 idx_chunk = start_tag(pb, "idx1");
00439 memset(&entry[0], 0, sizeof(entry));
00440 do {
00441 empty = 1;
00442 for (i=0; i<s->nb_streams; i++) {
00443 if (avi->indexes[i].entry <= entry[i])
00444 continue;
00445
00446 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
00447 if (empty || tie->pos < ie->pos) {
00448 ie = tie;
00449 stream_id = i;
00450 }
00451 empty = 0;
00452 }
00453 if (!empty) {
00454 avi_stream2fourcc(&tag[0], stream_id,
00455 s->streams[stream_id]->codec->codec_type);
00456 put_tag(pb, &tag[0]);
00457 put_le32(pb, ie->flags);
00458 put_le32(pb, ie->pos);
00459 put_le32(pb, ie->len);
00460 entry[stream_id]++;
00461 }
00462 } while (!empty);
00463 end_tag(pb, idx_chunk);
00464
00465 avi_write_counters(s, avi->riff_id);
00466 }
00467 return 0;
00468 }
00469
00470 static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
00471 {
00472 AVIContext *avi = s->priv_data;
00473 ByteIOContext *pb = s->pb;
00474 unsigned char tag[5];
00475 unsigned int flags=0;
00476 const int stream_index= pkt->stream_index;
00477 AVCodecContext *enc= s->streams[stream_index]->codec;
00478 int size= pkt->size;
00479
00480
00481 while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
00482 AVPacket empty_packet;
00483
00484 av_init_packet(&empty_packet);
00485 empty_packet.size= 0;
00486 empty_packet.data= NULL;
00487 empty_packet.stream_index= stream_index;
00488 avi_write_packet(s, &empty_packet);
00489
00490 }
00491 avi->packet_count[stream_index]++;
00492
00493
00494 if (!url_is_streamed(pb) &&
00495 (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
00496
00497 avi_write_ix(s);
00498 end_tag(pb, avi->movi_list);
00499
00500 if (avi->riff_id == 1)
00501 avi_write_idx1(s);
00502
00503 end_tag(pb, avi->riff_start);
00504 avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
00505 }
00506
00507 avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
00508 if(pkt->flags&PKT_FLAG_KEY)
00509 flags = 0x10;
00510 if (enc->codec_type == CODEC_TYPE_AUDIO) {
00511 avi->audio_strm_length[stream_index] += size;
00512 }
00513
00514 if (!url_is_streamed(s->pb)) {
00515 AVIIndex* idx = &avi->indexes[stream_index];
00516 int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
00517 int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
00518 if (idx->ents_allocated <= idx->entry) {
00519 idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
00520 if (!idx->cluster)
00521 return -1;
00522 idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
00523 if (!idx->cluster[cl])
00524 return -1;
00525 idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
00526 }
00527
00528 idx->cluster[cl][id].flags = flags;
00529 idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
00530 idx->cluster[cl][id].len = size;
00531 idx->entry++;
00532 }
00533
00534 put_buffer(pb, tag, 4);
00535 put_le32(pb, size);
00536 put_buffer(pb, pkt->data, size);
00537 if (size & 1)
00538 put_byte(pb, 0);
00539
00540 put_flush_packet(pb);
00541 return 0;
00542 }
00543
00544 static int avi_write_trailer(AVFormatContext *s)
00545 {
00546 AVIContext *avi = s->priv_data;
00547 ByteIOContext *pb = s->pb;
00548 int res = 0;
00549 int i, j, n, nb_frames;
00550 int64_t file_size;
00551
00552 if (!url_is_streamed(pb)){
00553 if (avi->riff_id == 1) {
00554 end_tag(pb, avi->movi_list);
00555 res = avi_write_idx1(s);
00556 end_tag(pb, avi->riff_start);
00557 } else {
00558 avi_write_ix(s);
00559 end_tag(pb, avi->movi_list);
00560 end_tag(pb, avi->riff_start);
00561
00562 file_size = url_ftell(pb);
00563 url_fseek(pb, avi->odml_list - 8, SEEK_SET);
00564 put_tag(pb, "LIST");
00565 url_fskip(pb, 16);
00566
00567 for (n=nb_frames=0;n<s->nb_streams;n++) {
00568 AVCodecContext *stream = s->streams[n]->codec;
00569 if (stream->codec_type == CODEC_TYPE_VIDEO) {
00570 if (nb_frames < avi->packet_count[n])
00571 nb_frames = avi->packet_count[n];
00572 } else {
00573 if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
00574 nb_frames += avi->packet_count[n];
00575 }
00576 }
00577 }
00578 put_le32(pb, nb_frames);
00579 url_fseek(pb, file_size, SEEK_SET);
00580
00581 avi_write_counters(s, avi->riff_id);
00582 }
00583 }
00584 put_flush_packet(pb);
00585
00586 for (i=0; i<MAX_STREAMS; i++) {
00587 for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
00588 av_free(avi->indexes[i].cluster[j]);
00589 av_free(avi->indexes[i].cluster);
00590 avi->indexes[i].cluster = NULL;
00591 avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
00592 }
00593
00594 return res;
00595 }
00596
00597 AVOutputFormat avi_muxer = {
00598 "avi",
00599 NULL_IF_CONFIG_SMALL("AVI format"),
00600 "video/x-msvideo",
00601 "avi",
00602 sizeof(AVIContext),
00603 CODEC_ID_MP2,
00604 CODEC_ID_MPEG4,
00605 avi_write_header,
00606 avi_write_packet,
00607 avi_write_trailer,
00608 .codec_tag= (const AVCodecTag* const []){codec_bmp_tags, codec_wav_tags, 0},
00609 .flags= AVFMT_VARIABLE_FPS,
00610 };