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 "riff.h"
00023 #include "asf.h"
00024 #include "avio_internal.h"
00025 #include "libavutil/dict.h"
00026
00027 #undef NDEBUG
00028 #include <assert.h>
00029
00030
00031 #define ASF_INDEXED_INTERVAL 10000000
00032 #define ASF_INDEX_BLOCK 600
00033
00034 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
00035 #define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
00036 ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
00037 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
00038 )
00039
00040 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
00041 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
00042 #else
00043 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
00044 #endif
00045
00046 #define ASF_PPI_PROPERTY_FLAGS (\
00047 ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
00048 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
00049 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
00050 ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
00051 )
00052
00053 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
00054
00055 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
00056
00057 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
00058 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
00059 #endif
00060 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
00061 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
00062 #endif
00063 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
00064 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
00065 #endif
00066 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
00067 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
00068 #endif
00069
00070
00071 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
00072 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
00073 #endif
00074 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
00075 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
00076 #endif
00077 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
00078 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
00079 #endif
00080 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
00081 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
00082 #endif
00083
00084 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
00085 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
00086 #endif
00087 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
00088 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
00089 #endif
00090 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
00091 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
00092 #endif
00093 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
00094 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
00095 #endif
00096
00097 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
00098 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
00099 #endif
00100 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
00101 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
00102 #endif
00103 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
00104 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
00105 #endif
00106 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
00107 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
00108 #endif
00109
00110 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
00111 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
00112 #endif
00113 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
00114 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
00115 #endif
00116 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
00117 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
00118 #endif
00119 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
00120 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
00121 #endif
00122
00123 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
00124 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
00125 #endif
00126 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
00127 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
00128 #endif
00129 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
00130 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
00131 #endif
00132 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
00133 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
00134 #endif
00135
00136 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
00137 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
00138 #endif
00139 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
00140 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
00141 #endif
00142 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
00143 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
00144 #endif
00145
00146 #define PACKET_HEADER_MIN_SIZE (\
00147 ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
00148 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
00149 1 + \
00150 1 + \
00151 ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
00152 ASF_PPI_SEQUENCE_FIELD_SIZE + \
00153 ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
00154 4 + \
00155 2 \
00156 )
00157
00158
00159
00160 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
00161
00162 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD (\
00163 1 + \
00164 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
00165 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
00166 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
00167 ASF_PAYLOAD_REPLICATED_DATA_LENGTH \
00168 )
00169
00170 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
00171 1 + \
00172 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
00173 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
00174 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
00175 ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
00176 ASF_PAYLOAD_LENGTH_FIELD_SIZE \
00177 )
00178
00179 #define SINGLE_PAYLOAD_DATA_LENGTH (\
00180 PACKET_SIZE - \
00181 PACKET_HEADER_MIN_SIZE - \
00182 PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
00183 )
00184
00185 #define MULTI_PAYLOAD_CONSTANT (\
00186 PACKET_SIZE - \
00187 PACKET_HEADER_MIN_SIZE - \
00188 1 - \
00189 2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
00190 )
00191
00192 typedef struct {
00193 uint32_t seqno;
00194 int is_streamed;
00195 ASFStream streams[128];
00196
00197 uint64_t nb_packets;
00198 int64_t duration;
00199
00200 unsigned char multi_payloads_present;
00201 int packet_size_left;
00202 int packet_timestamp_start;
00203 int packet_timestamp_end;
00204 unsigned int packet_nb_payloads;
00205 uint8_t packet_buf[PACKET_SIZE];
00206 AVIOContext pb;
00207
00208 uint64_t data_offset;
00209
00210 int64_t last_indexed_pts;
00211 ASFIndex* index_ptr;
00212 uint32_t nb_index_count;
00213 uint32_t nb_index_memory_alloc;
00214 uint16_t maximum_packet;
00215 } ASFContext;
00216
00217 static const AVCodecTag codec_asf_bmp_tags[] = {
00218 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
00219 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
00220 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
00221 { CODEC_ID_NONE, 0 },
00222 };
00223
00224 #define PREROLL_TIME 3100
00225
00226 static void put_guid(AVIOContext *s, const ff_asf_guid *g)
00227 {
00228 assert(sizeof(*g) == 16);
00229 avio_write(s, *g, sizeof(*g));
00230 }
00231
00232 static void put_str16(AVIOContext *s, const char *tag)
00233 {
00234 int len;
00235 uint8_t *pb;
00236 AVIOContext *dyn_buf;
00237 if (avio_open_dyn_buf(&dyn_buf) < 0)
00238 return;
00239
00240 avio_put_str16le(dyn_buf, tag);
00241 len = avio_close_dyn_buf(dyn_buf, &pb);
00242 avio_wl16(s, len);
00243 avio_write(s, pb, len);
00244 av_freep(&pb);
00245 }
00246
00247 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
00248 {
00249 int64_t pos;
00250
00251 pos = avio_tell(pb);
00252 put_guid(pb, g);
00253 avio_wl64(pb, 24);
00254 return pos;
00255 }
00256
00257
00258 static void end_header(AVIOContext *pb, int64_t pos)
00259 {
00260 int64_t pos1;
00261
00262 pos1 = avio_tell(pb);
00263 avio_seek(pb, pos + 16, SEEK_SET);
00264 avio_wl64(pb, pos1 - pos);
00265 avio_seek(pb, pos1, SEEK_SET);
00266 }
00267
00268
00269 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
00270 {
00271 ASFContext *asf = s->priv_data;
00272 AVIOContext *pb = s->pb;
00273 int length;
00274
00275 length = payload_length + 8;
00276 avio_wl16(pb, type);
00277 avio_wl16(pb, length);
00278 avio_wl32(pb, asf->seqno);
00279 avio_wl16(pb, flags);
00280 avio_wl16(pb, length);
00281 asf->seqno++;
00282 }
00283
00284
00285 static int64_t unix_to_file_time(int ti)
00286 {
00287 int64_t t;
00288
00289 t = ti * INT64_C(10000000);
00290 t += INT64_C(116444736000000000);
00291 return t;
00292 }
00293
00294
00295 static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
00296 {
00297 ASFContext *asf = s->priv_data;
00298 AVIOContext *pb = s->pb;
00299 AVDictionaryEntry *tags[5];
00300 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
00301 int has_title;
00302 int metadata_count;
00303 AVCodecContext *enc;
00304 int64_t header_offset, cur_pos, hpos;
00305 int bit_rate;
00306 int64_t duration;
00307
00308 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
00309
00310 tags[0] = av_dict_get(s->metadata, "title" , NULL, 0);
00311 tags[1] = av_dict_get(s->metadata, "author" , NULL, 0);
00312 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
00313 tags[3] = av_dict_get(s->metadata, "comment" , NULL, 0);
00314 tags[4] = av_dict_get(s->metadata, "rating" , NULL, 0);
00315
00316 duration = asf->duration + PREROLL_TIME * 10000;
00317 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
00318 metadata_count = s->metadata ? s->metadata->count : 0;
00319
00320 bit_rate = 0;
00321 for(n=0;n<s->nb_streams;n++) {
00322 enc = s->streams[n]->codec;
00323
00324 av_set_pts_info(s->streams[n], 32, 1, 1000);
00325
00326 bit_rate += enc->bit_rate;
00327 }
00328
00329 if (asf->is_streamed) {
00330 put_chunk(s, 0x4824, 0, 0xc00);
00331 }
00332
00333 put_guid(pb, &ff_asf_header);
00334 avio_wl64(pb, -1);
00335 avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams);
00336 avio_w8(pb, 1);
00337 avio_w8(pb, 2);
00338
00339
00340 header_offset = avio_tell(pb);
00341 hpos = put_header(pb, &ff_asf_file_header);
00342 put_guid(pb, &ff_asf_my_guid);
00343 avio_wl64(pb, file_size);
00344 file_time = 0;
00345 avio_wl64(pb, unix_to_file_time(file_time));
00346 avio_wl64(pb, asf->nb_packets);
00347 avio_wl64(pb, duration);
00348 avio_wl64(pb, asf->duration);
00349 avio_wl64(pb, PREROLL_TIME);
00350 avio_wl32(pb, (asf->is_streamed || !pb->seekable ) ? 3 : 2);
00351 avio_wl32(pb, s->packet_size);
00352 avio_wl32(pb, s->packet_size);
00353 avio_wl32(pb, bit_rate);
00354 end_header(pb, hpos);
00355
00356
00357 hpos = put_header(pb, &ff_asf_head1_guid);
00358 put_guid(pb, &ff_asf_head2_guid);
00359 avio_wl32(pb, 6);
00360 avio_wl16(pb, 0);
00361 end_header(pb, hpos);
00362
00363
00364 if (has_title) {
00365 int len;
00366 uint8_t *buf;
00367 AVIOContext *dyn_buf;
00368
00369 if (avio_open_dyn_buf(&dyn_buf) < 0)
00370 return AVERROR(ENOMEM);
00371
00372 hpos = put_header(pb, &ff_asf_comment_header);
00373
00374 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
00375 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
00376 avio_wl16(pb, len);
00377 }
00378 len = avio_close_dyn_buf(dyn_buf, &buf);
00379 avio_write(pb, buf, len);
00380 av_freep(&buf);
00381 end_header(pb, hpos);
00382 }
00383 if (metadata_count) {
00384 AVDictionaryEntry *tag = NULL;
00385 hpos = put_header(pb, &ff_asf_extended_content_header);
00386 avio_wl16(pb, metadata_count);
00387 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00388 put_str16(pb, tag->key);
00389 avio_wl16(pb, 0);
00390 put_str16(pb, tag->value);
00391 }
00392 end_header(pb, hpos);
00393 }
00394
00395
00396 for(n=0;n<s->nb_streams;n++) {
00397 int64_t es_pos;
00398
00399
00400 enc = s->streams[n]->codec;
00401 asf->streams[n].num = n + 1;
00402 asf->streams[n].seq = 0;
00403
00404
00405 switch(enc->codec_type) {
00406 case AVMEDIA_TYPE_AUDIO:
00407 wav_extra_size = 0;
00408 extra_size = 18 + wav_extra_size;
00409 extra_size2 = 8;
00410 break;
00411 default:
00412 case AVMEDIA_TYPE_VIDEO:
00413 wav_extra_size = enc->extradata_size;
00414 extra_size = 0x33 + wav_extra_size;
00415 extra_size2 = 0;
00416 break;
00417 }
00418
00419 hpos = put_header(pb, &ff_asf_stream_header);
00420 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00421 put_guid(pb, &ff_asf_audio_stream);
00422 put_guid(pb, &ff_asf_audio_conceal_spread);
00423 } else {
00424 put_guid(pb, &ff_asf_video_stream);
00425 put_guid(pb, &ff_asf_video_conceal_none);
00426 }
00427 avio_wl64(pb, 0);
00428 es_pos = avio_tell(pb);
00429 avio_wl32(pb, extra_size);
00430 avio_wl32(pb, extra_size2);
00431 avio_wl16(pb, n + 1);
00432 avio_wl32(pb, 0);
00433
00434 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00435
00436 int wavsize = ff_put_wav_header(pb, enc);
00437 if ((enc->codec_id != CODEC_ID_MP3) && (enc->codec_id != CODEC_ID_MP2) && (enc->codec_id != CODEC_ID_ADPCM_IMA_WAV) && (enc->extradata_size==0)) {
00438 wavsize += 2;
00439 avio_wl16(pb, 0);
00440 }
00441
00442 if (wavsize < 0)
00443 return -1;
00444 if (wavsize != extra_size) {
00445 cur_pos = avio_tell(pb);
00446 avio_seek(pb, es_pos, SEEK_SET);
00447 avio_wl32(pb, wavsize);
00448 avio_seek(pb, cur_pos, SEEK_SET);
00449 }
00450
00451 avio_w8(pb, 0x01);
00452 if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
00453 avio_wl16(pb, 0x0190);
00454 avio_wl16(pb, 0x0190);
00455 }else{
00456 avio_wl16(pb, enc->block_align);
00457 avio_wl16(pb, enc->block_align);
00458 }
00459 avio_wl16(pb, 0x01);
00460 avio_w8(pb, 0x00);
00461 } else {
00462 avio_wl32(pb, enc->width);
00463 avio_wl32(pb, enc->height);
00464 avio_w8(pb, 2);
00465 avio_wl16(pb, 40 + enc->extradata_size);
00466
00467
00468 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
00469 }
00470 end_header(pb, hpos);
00471 }
00472
00473
00474
00475 hpos = put_header(pb, &ff_asf_codec_comment_header);
00476 put_guid(pb, &ff_asf_codec_comment1_header);
00477 avio_wl32(pb, s->nb_streams);
00478 for(n=0;n<s->nb_streams;n++) {
00479 AVCodec *p;
00480 const char *desc;
00481 int len;
00482 uint8_t *buf;
00483 AVIOContext *dyn_buf;
00484
00485 enc = s->streams[n]->codec;
00486 p = avcodec_find_encoder(enc->codec_id);
00487
00488 if(enc->codec_type == AVMEDIA_TYPE_AUDIO)
00489 avio_wl16(pb, 2);
00490 else if(enc->codec_type == AVMEDIA_TYPE_VIDEO)
00491 avio_wl16(pb, 1);
00492 else
00493 avio_wl16(pb, -1);
00494
00495 if(enc->codec_id == CODEC_ID_WMAV2)
00496 desc = "Windows Media Audio V8";
00497 else
00498 desc = p ? p->name : enc->codec_name;
00499
00500 if ( avio_open_dyn_buf(&dyn_buf) < 0)
00501 return AVERROR(ENOMEM);
00502
00503 avio_put_str16le(dyn_buf, desc);
00504 len = avio_close_dyn_buf(dyn_buf, &buf);
00505 avio_wl16(pb, len / 2);
00506
00507 avio_write(pb, buf, len);
00508 av_freep(&buf);
00509
00510 avio_wl16(pb, 0);
00511
00512
00513
00514 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00515 avio_wl16(pb, 2);
00516 avio_wl16(pb, enc->codec_tag);
00517 } else {
00518 avio_wl16(pb, 4);
00519 avio_wl32(pb, enc->codec_tag);
00520 }
00521 if(!enc->codec_tag)
00522 return -1;
00523 }
00524 end_header(pb, hpos);
00525
00526
00527
00528 cur_pos = avio_tell(pb);
00529 header_size = cur_pos - header_offset;
00530 if (asf->is_streamed) {
00531 header_size += 8 + 30 + 50;
00532
00533 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
00534 avio_wl16(pb, header_size);
00535 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
00536 avio_wl16(pb, header_size);
00537
00538 header_size -= 8 + 30 + 50;
00539 }
00540 header_size += 24 + 6;
00541 avio_seek(pb, header_offset - 14, SEEK_SET);
00542 avio_wl64(pb, header_size);
00543 avio_seek(pb, cur_pos, SEEK_SET);
00544
00545
00546 asf->data_offset = cur_pos;
00547 put_guid(pb, &ff_asf_data_header);
00548 avio_wl64(pb, data_chunk_size);
00549 put_guid(pb, &ff_asf_my_guid);
00550 avio_wl64(pb, asf->nb_packets);
00551 avio_w8(pb, 1);
00552 avio_w8(pb, 1);
00553 return 0;
00554 }
00555
00556 static int asf_write_header(AVFormatContext *s)
00557 {
00558 ASFContext *asf = s->priv_data;
00559
00560 s->packet_size = PACKET_SIZE;
00561 asf->nb_packets = 0;
00562
00563 asf->last_indexed_pts = 0;
00564 asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
00565 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
00566 asf->nb_index_count = 0;
00567 asf->maximum_packet = 0;
00568
00569
00570
00571
00572 if (asf_write_header1(s, 0, 50) < 0) {
00573
00574 return -1;
00575 }
00576
00577 avio_flush(s->pb);
00578
00579 asf->packet_nb_payloads = 0;
00580 asf->packet_timestamp_start = -1;
00581 asf->packet_timestamp_end = -1;
00582 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
00583 NULL, NULL, NULL, NULL);
00584
00585 return 0;
00586 }
00587
00588 static int asf_write_stream_header(AVFormatContext *s)
00589 {
00590 ASFContext *asf = s->priv_data;
00591
00592 asf->is_streamed = 1;
00593
00594 return asf_write_header(s);
00595 }
00596
00597 static int put_payload_parsing_info(
00598 AVFormatContext *s,
00599 unsigned int sendtime,
00600 unsigned int duration,
00601 int nb_payloads,
00602 int padsize
00603 )
00604 {
00605 ASFContext *asf = s->priv_data;
00606 AVIOContext *pb = s->pb;
00607 int ppi_size, i;
00608 int64_t start= avio_tell(pb);
00609
00610 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
00611
00612 padsize -= PACKET_HEADER_MIN_SIZE;
00613 if(asf->multi_payloads_present)
00614 padsize--;
00615 assert(padsize>=0);
00616
00617 avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
00618 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
00619 avio_w8(pb, 0x0);
00620 }
00621
00622 if (asf->multi_payloads_present)
00623 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
00624
00625 if (padsize > 0) {
00626 if (padsize < 256)
00627 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
00628 else
00629 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
00630 }
00631 avio_w8(pb, iLengthTypeFlags);
00632
00633 avio_w8(pb, ASF_PPI_PROPERTY_FLAGS);
00634
00635 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
00636 avio_wl16(pb, padsize - 2);
00637 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
00638 avio_w8(pb, padsize - 1);
00639
00640 avio_wl32(pb, sendtime);
00641 avio_wl16(pb, duration);
00642 if (asf->multi_payloads_present)
00643 avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
00644
00645 ppi_size = avio_tell(pb) - start;
00646
00647 return ppi_size;
00648 }
00649
00650 static void flush_packet(AVFormatContext *s)
00651 {
00652 ASFContext *asf = s->priv_data;
00653 int packet_hdr_size, packet_filled_size;
00654
00655 assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
00656
00657 if (asf->is_streamed) {
00658 put_chunk(s, 0x4424, s->packet_size, 0);
00659 }
00660
00661 packet_hdr_size = put_payload_parsing_info(
00662 s,
00663 asf->packet_timestamp_start,
00664 asf->packet_timestamp_end - asf->packet_timestamp_start,
00665 asf->packet_nb_payloads,
00666 asf->packet_size_left
00667 );
00668
00669 packet_filled_size = PACKET_SIZE - asf->packet_size_left;
00670 assert(packet_hdr_size <= asf->packet_size_left);
00671 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
00672
00673 avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
00674
00675 avio_flush(s->pb);
00676 asf->nb_packets++;
00677 asf->packet_nb_payloads = 0;
00678 asf->packet_timestamp_start = -1;
00679 asf->packet_timestamp_end = -1;
00680 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
00681 NULL, NULL, NULL, NULL);
00682 }
00683
00684 static void put_payload_header(
00685 AVFormatContext *s,
00686 ASFStream *stream,
00687 int presentation_time,
00688 int m_obj_size,
00689 int m_obj_offset,
00690 int payload_len,
00691 int flags
00692 )
00693 {
00694 ASFContext *asf = s->priv_data;
00695 AVIOContext *pb = &asf->pb;
00696 int val;
00697
00698 val = stream->num;
00699 if (flags & AV_PKT_FLAG_KEY)
00700 val |= ASF_PL_FLAG_KEY_FRAME;
00701 avio_w8(pb, val);
00702
00703 avio_w8(pb, stream->seq);
00704 avio_wl32(pb, m_obj_offset);
00705
00706
00707
00708
00709
00710
00711 avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
00712
00713 avio_wl32(pb, m_obj_size);
00714 avio_wl32(pb, presentation_time);
00715
00716 if (asf->multi_payloads_present){
00717 avio_wl16(pb, payload_len);
00718 }
00719 }
00720
00721 static void put_frame(
00722 AVFormatContext *s,
00723 ASFStream *stream,
00724 AVStream *avst,
00725 int timestamp,
00726 const uint8_t *buf,
00727 int m_obj_size,
00728 int flags
00729 )
00730 {
00731 ASFContext *asf = s->priv_data;
00732 int m_obj_offset, payload_len, frag_len1;
00733
00734 m_obj_offset = 0;
00735 while (m_obj_offset < m_obj_size) {
00736 payload_len = m_obj_size - m_obj_offset;
00737 if (asf->packet_timestamp_start == -1) {
00738 asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
00739
00740 asf->packet_size_left = PACKET_SIZE;
00741 if (asf->multi_payloads_present){
00742 frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
00743 }
00744 else {
00745 frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
00746 }
00747 asf->packet_timestamp_start = timestamp;
00748 }
00749 else {
00750
00751 frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
00752
00753 if(frag_len1 < payload_len && avst->codec->codec_type == AVMEDIA_TYPE_AUDIO){
00754 flush_packet(s);
00755 continue;
00756 }
00757 }
00758 if (frag_len1 > 0) {
00759 if (payload_len > frag_len1)
00760 payload_len = frag_len1;
00761 else if (payload_len == (frag_len1 - 1))
00762 payload_len = frag_len1 - 2;
00763
00764 put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
00765 avio_write(&asf->pb, buf, payload_len);
00766
00767 if (asf->multi_payloads_present)
00768 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
00769 else
00770 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
00771 asf->packet_timestamp_end = timestamp;
00772
00773 asf->packet_nb_payloads++;
00774 } else {
00775 payload_len = 0;
00776 }
00777 m_obj_offset += payload_len;
00778 buf += payload_len;
00779
00780 if (!asf->multi_payloads_present)
00781 flush_packet(s);
00782 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
00783 flush_packet(s);
00784 }
00785 stream->seq++;
00786 }
00787
00788 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
00789 {
00790 ASFContext *asf = s->priv_data;
00791 ASFStream *stream;
00792 int64_t duration;
00793 AVCodecContext *codec;
00794 int64_t packet_st,pts;
00795 int start_sec,i;
00796 int flags= pkt->flags;
00797
00798 codec = s->streams[pkt->stream_index]->codec;
00799 stream = &asf->streams[pkt->stream_index];
00800
00801 if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
00802 flags &= ~AV_PKT_FLAG_KEY;
00803
00804 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
00805 assert(pts != AV_NOPTS_VALUE);
00806 duration = pts * 10000;
00807 asf->duration= FFMAX(asf->duration, duration + pkt->duration * 10000);
00808
00809 packet_st = asf->nb_packets;
00810 put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
00811
00812
00813 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
00814 start_sec = (int)(duration / INT64_C(10000000));
00815 if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
00816 for(i=asf->nb_index_count;i<start_sec;i++) {
00817 if (i>=asf->nb_index_memory_alloc) {
00818 asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
00819 asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
00820 }
00821
00822 asf->index_ptr[i].packet_number = (uint32_t)packet_st;
00823 asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets-packet_st);
00824 asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
00825 }
00826 asf->nb_index_count = start_sec;
00827 asf->last_indexed_pts = duration;
00828 }
00829 }
00830 return 0;
00831 }
00832
00833
00834 static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
00835 {
00836 AVIOContext *pb = s->pb;
00837 int i;
00838
00839 put_guid(pb, &ff_asf_simple_index_header);
00840 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
00841 put_guid(pb, &ff_asf_my_guid);
00842 avio_wl64(pb, ASF_INDEXED_INTERVAL);
00843 avio_wl32(pb, max);
00844 avio_wl32(pb, count);
00845 for(i=0; i<count; i++) {
00846 avio_wl32(pb, index[i].packet_number);
00847 avio_wl16(pb, index[i].packet_count);
00848 }
00849
00850 return 0;
00851 }
00852
00853 static int asf_write_trailer(AVFormatContext *s)
00854 {
00855 ASFContext *asf = s->priv_data;
00856 int64_t file_size,data_size;
00857
00858
00859 if (asf->pb.buf_ptr > asf->pb.buffer)
00860 flush_packet(s);
00861
00862
00863 data_size = avio_tell(s->pb);
00864 if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
00865 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
00866 }
00867 avio_flush(s->pb);
00868
00869 if (asf->is_streamed || !s->pb->seekable) {
00870 put_chunk(s, 0x4524, 0, 0);
00871 } else {
00872
00873 file_size = avio_tell(s->pb);
00874 avio_seek(s->pb, 0, SEEK_SET);
00875 asf_write_header1(s, file_size, data_size - asf->data_offset);
00876 }
00877
00878 avio_flush(s->pb);
00879 av_free(asf->index_ptr);
00880 return 0;
00881 }
00882
00883 #if CONFIG_ASF_MUXER
00884 AVOutputFormat ff_asf_muxer = {
00885 "asf",
00886 NULL_IF_CONFIG_SMALL("ASF format"),
00887 "video/x-ms-asf",
00888 "asf,wmv,wma",
00889 sizeof(ASFContext),
00890 #if CONFIG_LIBMP3LAME
00891 CODEC_ID_MP3,
00892 #else
00893 CODEC_ID_MP2,
00894 #endif
00895 CODEC_ID_MSMPEG4V3,
00896 asf_write_header,
00897 asf_write_packet,
00898 asf_write_trailer,
00899 .flags = AVFMT_GLOBALHEADER,
00900 .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
00901 };
00902 #endif
00903
00904 #if CONFIG_ASF_STREAM_MUXER
00905 AVOutputFormat ff_asf_stream_muxer = {
00906 "asf_stream",
00907 NULL_IF_CONFIG_SMALL("ASF format"),
00908 "video/x-ms-asf",
00909 "asf,wmv,wma",
00910 sizeof(ASFContext),
00911 #if CONFIG_LIBMP3LAME
00912 CODEC_ID_MP3,
00913 #else
00914 CODEC_ID_MP2,
00915 #endif
00916 CODEC_ID_MSMPEG4V3,
00917 asf_write_stream_header,
00918 asf_write_packet,
00919 asf_write_trailer,
00920 .flags = AVFMT_GLOBALHEADER,
00921 .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
00922 };
00923 #endif //CONFIG_ASF_STREAM_MUXER