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 "internal.h"
00023 #include "riff.h"
00024 #include "asf.h"
00025 #include "avio_internal.h"
00026 #include "libavutil/dict.h"
00027
00028 #undef NDEBUG
00029 #include <assert.h>
00030
00031
00032 #define ASF_INDEXED_INTERVAL 10000000
00033 #define ASF_INDEX_BLOCK (1<<9)
00034
00035 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
00036 #define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
00037 ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
00038 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
00039 )
00040
00041 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
00042 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
00043 #else
00044 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
00045 #endif
00046
00047 #define ASF_PPI_PROPERTY_FLAGS (\
00048 ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
00049 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
00050 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
00051 ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
00052 )
00053
00054 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
00055
00056 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
00057
00058 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
00059 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
00060 #endif
00061 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
00062 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
00063 #endif
00064 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
00065 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
00066 #endif
00067 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
00068 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
00069 #endif
00070
00071
00072 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
00073 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
00074 #endif
00075 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
00076 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
00077 #endif
00078 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
00079 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
00080 #endif
00081 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
00082 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
00083 #endif
00084
00085 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
00086 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
00087 #endif
00088 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
00089 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
00090 #endif
00091 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
00092 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
00093 #endif
00094 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
00095 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
00096 #endif
00097
00098 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
00099 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
00100 #endif
00101 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
00102 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
00103 #endif
00104 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
00105 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
00106 #endif
00107 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
00108 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
00109 #endif
00110
00111 #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))
00112 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
00113 #endif
00114 #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))
00115 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
00116 #endif
00117 #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))
00118 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
00119 #endif
00120 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
00121 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
00122 #endif
00123
00124 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
00125 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
00126 #endif
00127 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
00128 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
00129 #endif
00130 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
00131 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
00132 #endif
00133 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
00134 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
00135 #endif
00136
00137 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
00138 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
00139 #endif
00140 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
00141 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
00142 #endif
00143 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
00144 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
00145 #endif
00146
00147 #define PACKET_HEADER_MIN_SIZE (\
00148 ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
00149 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
00150 1 + \
00151 1 + \
00152 ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
00153 ASF_PPI_SEQUENCE_FIELD_SIZE + \
00154 ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
00155 4 + \
00156 2 \
00157 )
00158
00159
00160
00161 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
00162
00163 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD (\
00164 1 + \
00165 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
00166 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
00167 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
00168 ASF_PAYLOAD_REPLICATED_DATA_LENGTH \
00169 )
00170
00171 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
00172 1 + \
00173 ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
00174 ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
00175 ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
00176 ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
00177 ASF_PAYLOAD_LENGTH_FIELD_SIZE \
00178 )
00179
00180 #define SINGLE_PAYLOAD_DATA_LENGTH (\
00181 PACKET_SIZE - \
00182 PACKET_HEADER_MIN_SIZE - \
00183 PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
00184 )
00185
00186 #define MULTI_PAYLOAD_CONSTANT (\
00187 PACKET_SIZE - \
00188 PACKET_HEADER_MIN_SIZE - \
00189 1 - \
00190 2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
00191 )
00192
00193 typedef struct {
00194 uint32_t seqno;
00195 int is_streamed;
00196 ASFStream streams[128];
00197
00198 uint64_t nb_packets;
00199 int64_t duration;
00200
00201 unsigned char multi_payloads_present;
00202 int packet_size_left;
00203 int64_t packet_timestamp_start;
00204 int64_t packet_timestamp_end;
00205 unsigned int packet_nb_payloads;
00206 uint8_t packet_buf[PACKET_SIZE];
00207 AVIOContext pb;
00208
00209 uint64_t data_offset;
00210
00211 ASFIndex* index_ptr;
00212 uint32_t nb_index_memory_alloc;
00213 uint16_t maximum_packet;
00214 uint32_t next_packet_number;
00215 uint16_t next_packet_count;
00216 int next_start_sec;
00217 int end_sec;
00218 } ASFContext;
00219
00220 static const AVCodecTag codec_asf_bmp_tags[] = {
00221 { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
00222 { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
00223 { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
00224 { AV_CODEC_ID_NONE, 0 },
00225 };
00226
00227 #define PREROLL_TIME 3100
00228
00229 void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
00230 {
00231 assert(sizeof(*g) == 16);
00232 avio_write(s, *g, sizeof(*g));
00233 }
00234
00235 static void put_str16(AVIOContext *s, const char *tag)
00236 {
00237 int len;
00238 uint8_t *pb;
00239 AVIOContext *dyn_buf;
00240 if (avio_open_dyn_buf(&dyn_buf) < 0)
00241 return;
00242
00243 avio_put_str16le(dyn_buf, tag);
00244 len = avio_close_dyn_buf(dyn_buf, &pb);
00245 avio_wl16(s, len);
00246 avio_write(s, pb, len);
00247 av_freep(&pb);
00248 }
00249
00250 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
00251 {
00252 int64_t pos;
00253
00254 pos = avio_tell(pb);
00255 ff_put_guid(pb, g);
00256 avio_wl64(pb, 24);
00257 return pos;
00258 }
00259
00260
00261 static void end_header(AVIOContext *pb, int64_t pos)
00262 {
00263 int64_t pos1;
00264
00265 pos1 = avio_tell(pb);
00266 avio_seek(pb, pos + 16, SEEK_SET);
00267 avio_wl64(pb, pos1 - pos);
00268 avio_seek(pb, pos1, SEEK_SET);
00269 }
00270
00271
00272 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
00273 {
00274 ASFContext *asf = s->priv_data;
00275 AVIOContext *pb = s->pb;
00276 int length;
00277
00278 length = payload_length + 8;
00279 avio_wl16(pb, type);
00280 avio_wl16(pb, length);
00281 avio_wl32(pb, asf->seqno);
00282 avio_wl16(pb, flags);
00283 avio_wl16(pb, length);
00284 asf->seqno++;
00285 }
00286
00287
00288 static int64_t unix_to_file_time(int ti)
00289 {
00290 int64_t t;
00291
00292 t = ti * INT64_C(10000000);
00293 t += INT64_C(116444736000000000);
00294 return t;
00295 }
00296
00297
00298 static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
00299 {
00300 ASFContext *asf = s->priv_data;
00301 AVIOContext *pb = s->pb;
00302 AVDictionaryEntry *tags[5];
00303 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
00304 int has_title;
00305 int metadata_count;
00306 AVCodecContext *enc;
00307 int64_t header_offset, cur_pos, hpos;
00308 int bit_rate;
00309 int64_t duration;
00310
00311 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
00312
00313 tags[0] = av_dict_get(s->metadata, "title" , NULL, 0);
00314 tags[1] = av_dict_get(s->metadata, "author" , NULL, 0);
00315 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
00316 tags[3] = av_dict_get(s->metadata, "comment" , NULL, 0);
00317 tags[4] = av_dict_get(s->metadata, "rating" , NULL, 0);
00318
00319 duration = asf->duration + PREROLL_TIME * 10000;
00320 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
00321 metadata_count = av_dict_count(s->metadata);
00322
00323 bit_rate = 0;
00324 for(n=0;n<s->nb_streams;n++) {
00325 enc = s->streams[n]->codec;
00326
00327 avpriv_set_pts_info(s->streams[n], 32, 1, 1000);
00328
00329 bit_rate += enc->bit_rate;
00330 }
00331
00332 if (asf->is_streamed) {
00333 put_chunk(s, 0x4824, 0, 0xc00);
00334 }
00335
00336 ff_put_guid(pb, &ff_asf_header);
00337 avio_wl64(pb, -1);
00338 avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams);
00339 avio_w8(pb, 1);
00340 avio_w8(pb, 2);
00341
00342
00343 header_offset = avio_tell(pb);
00344 hpos = put_header(pb, &ff_asf_file_header);
00345 ff_put_guid(pb, &ff_asf_my_guid);
00346 avio_wl64(pb, file_size);
00347 file_time = 0;
00348 avio_wl64(pb, unix_to_file_time(file_time));
00349 avio_wl64(pb, asf->nb_packets);
00350 avio_wl64(pb, duration);
00351 avio_wl64(pb, asf->duration);
00352 avio_wl64(pb, PREROLL_TIME);
00353 avio_wl32(pb, (asf->is_streamed || !pb->seekable ) ? 3 : 2);
00354 avio_wl32(pb, s->packet_size);
00355 avio_wl32(pb, s->packet_size);
00356 avio_wl32(pb, bit_rate);
00357 end_header(pb, hpos);
00358
00359
00360 hpos = put_header(pb, &ff_asf_head1_guid);
00361 ff_put_guid(pb, &ff_asf_head2_guid);
00362 avio_wl32(pb, 6);
00363 avio_wl16(pb, 0);
00364 end_header(pb, hpos);
00365
00366
00367 if (has_title) {
00368 int len;
00369 uint8_t *buf;
00370 AVIOContext *dyn_buf;
00371
00372 if (avio_open_dyn_buf(&dyn_buf) < 0)
00373 return AVERROR(ENOMEM);
00374
00375 hpos = put_header(pb, &ff_asf_comment_header);
00376
00377 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
00378 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
00379 avio_wl16(pb, len);
00380 }
00381 len = avio_close_dyn_buf(dyn_buf, &buf);
00382 avio_write(pb, buf, len);
00383 av_freep(&buf);
00384 end_header(pb, hpos);
00385 }
00386 if (metadata_count) {
00387 AVDictionaryEntry *tag = NULL;
00388 hpos = put_header(pb, &ff_asf_extended_content_header);
00389 avio_wl16(pb, metadata_count);
00390 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00391 put_str16(pb, tag->key);
00392 avio_wl16(pb, 0);
00393 put_str16(pb, tag->value);
00394 }
00395 end_header(pb, hpos);
00396 }
00397
00398
00399 for(n=0;n<s->nb_streams;n++) {
00400 int64_t es_pos;
00401
00402
00403 enc = s->streams[n]->codec;
00404 asf->streams[n].num = n + 1;
00405 asf->streams[n].seq = 1;
00406
00407
00408 switch(enc->codec_type) {
00409 case AVMEDIA_TYPE_AUDIO:
00410 wav_extra_size = 0;
00411 extra_size = 18 + wav_extra_size;
00412 extra_size2 = 8;
00413 break;
00414 default:
00415 case AVMEDIA_TYPE_VIDEO:
00416 wav_extra_size = enc->extradata_size;
00417 extra_size = 0x33 + wav_extra_size;
00418 extra_size2 = 0;
00419 break;
00420 }
00421
00422 hpos = put_header(pb, &ff_asf_stream_header);
00423 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00424 ff_put_guid(pb, &ff_asf_audio_stream);
00425 ff_put_guid(pb, &ff_asf_audio_conceal_spread);
00426 } else {
00427 ff_put_guid(pb, &ff_asf_video_stream);
00428 ff_put_guid(pb, &ff_asf_video_conceal_none);
00429 }
00430 avio_wl64(pb, 0);
00431 es_pos = avio_tell(pb);
00432 avio_wl32(pb, extra_size);
00433 avio_wl32(pb, extra_size2);
00434 avio_wl16(pb, n + 1);
00435 avio_wl32(pb, 0);
00436
00437 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00438
00439 int wavsize = ff_put_wav_header(pb, enc);
00440
00441 if (wavsize < 0)
00442 return -1;
00443 if (wavsize != extra_size) {
00444 cur_pos = avio_tell(pb);
00445 avio_seek(pb, es_pos, SEEK_SET);
00446 avio_wl32(pb, wavsize);
00447 avio_seek(pb, cur_pos, SEEK_SET);
00448 }
00449
00450 avio_w8(pb, 0x01);
00451 if(enc->codec_id == AV_CODEC_ID_ADPCM_G726 || !enc->block_align){
00452 avio_wl16(pb, 0x0190);
00453 avio_wl16(pb, 0x0190);
00454 }else{
00455 avio_wl16(pb, enc->block_align);
00456 avio_wl16(pb, enc->block_align);
00457 }
00458 avio_wl16(pb, 0x01);
00459 avio_w8(pb, 0x00);
00460 } else {
00461 avio_wl32(pb, enc->width);
00462 avio_wl32(pb, enc->height);
00463 avio_w8(pb, 2);
00464 avio_wl16(pb, 40 + enc->extradata_size);
00465
00466
00467 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
00468 }
00469 end_header(pb, hpos);
00470 }
00471
00472
00473
00474 hpos = put_header(pb, &ff_asf_codec_comment_header);
00475 ff_put_guid(pb, &ff_asf_codec_comment1_header);
00476 avio_wl32(pb, s->nb_streams);
00477 for(n=0;n<s->nb_streams;n++) {
00478 AVCodec *p;
00479 const char *desc;
00480 int len;
00481 uint8_t *buf;
00482 AVIOContext *dyn_buf;
00483
00484 enc = s->streams[n]->codec;
00485 p = avcodec_find_encoder(enc->codec_id);
00486
00487 if(enc->codec_type == AVMEDIA_TYPE_AUDIO)
00488 avio_wl16(pb, 2);
00489 else if(enc->codec_type == AVMEDIA_TYPE_VIDEO)
00490 avio_wl16(pb, 1);
00491 else
00492 avio_wl16(pb, -1);
00493
00494 if(enc->codec_id == AV_CODEC_ID_WMAV2)
00495 desc = "Windows Media Audio V8";
00496 else
00497 desc = p ? p->name : enc->codec_name;
00498
00499 if ( avio_open_dyn_buf(&dyn_buf) < 0)
00500 return AVERROR(ENOMEM);
00501
00502 avio_put_str16le(dyn_buf, desc);
00503 len = avio_close_dyn_buf(dyn_buf, &buf);
00504 avio_wl16(pb, len / 2);
00505
00506 avio_write(pb, buf, len);
00507 av_freep(&buf);
00508
00509 avio_wl16(pb, 0);
00510
00511
00512
00513 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00514 avio_wl16(pb, 2);
00515 avio_wl16(pb, enc->codec_tag);
00516 } else {
00517 avio_wl16(pb, 4);
00518 avio_wl32(pb, enc->codec_tag);
00519 }
00520 if(!enc->codec_tag)
00521 return -1;
00522 }
00523 end_header(pb, hpos);
00524
00525
00526
00527 cur_pos = avio_tell(pb);
00528 header_size = cur_pos - header_offset;
00529 if (asf->is_streamed) {
00530 header_size += 8 + 30 + 50;
00531
00532 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
00533 avio_wl16(pb, header_size);
00534 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
00535 avio_wl16(pb, header_size);
00536
00537 header_size -= 8 + 30 + 50;
00538 }
00539 header_size += 24 + 6;
00540 avio_seek(pb, header_offset - 14, SEEK_SET);
00541 avio_wl64(pb, header_size);
00542 avio_seek(pb, cur_pos, SEEK_SET);
00543
00544
00545 asf->data_offset = cur_pos;
00546 ff_put_guid(pb, &ff_asf_data_header);
00547 avio_wl64(pb, data_chunk_size);
00548 ff_put_guid(pb, &ff_asf_my_guid);
00549 avio_wl64(pb, asf->nb_packets);
00550 avio_w8(pb, 1);
00551 avio_w8(pb, 1);
00552 return 0;
00553 }
00554
00555 static int asf_write_header(AVFormatContext *s)
00556 {
00557 ASFContext *asf = s->priv_data;
00558
00559 s->packet_size = PACKET_SIZE;
00560 asf->nb_packets = 0;
00561
00562 asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
00563 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
00564 asf->maximum_packet = 0;
00565
00566
00567
00568
00569 if (asf_write_header1(s, 0, 50) < 0) {
00570
00571 return -1;
00572 }
00573
00574 avio_flush(s->pb);
00575
00576 asf->packet_nb_payloads = 0;
00577 asf->packet_timestamp_start = -1;
00578 asf->packet_timestamp_end = -1;
00579 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
00580 NULL, NULL, NULL, NULL);
00581
00582 if (s->avoid_negative_ts < 0)
00583 s->avoid_negative_ts = 1;
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 int64_t 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, (uint32_t) 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 int64_t 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 void update_index(AVFormatContext *s, int start_sec,
00789 uint32_t packet_number, uint16_t packet_count)
00790 {
00791 ASFContext *asf = s->priv_data;
00792
00793 if (start_sec > asf->next_start_sec) {
00794 int i;
00795
00796 if (!asf->next_start_sec) {
00797 asf->next_packet_number = packet_number;
00798 asf->next_packet_count = packet_count;
00799 }
00800
00801 if (start_sec > asf->nb_index_memory_alloc) {
00802 asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
00803 asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
00804 }
00805 for (i = asf->next_start_sec; i < start_sec; i++) {
00806 asf->index_ptr[i].packet_number = asf->next_packet_number;
00807 asf->index_ptr[i].packet_count = asf->next_packet_count;
00808 }
00809 }
00810 asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
00811 asf->next_packet_number = packet_number;
00812 asf->next_packet_count = packet_count;
00813 asf->next_start_sec = start_sec;
00814 }
00815
00816 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
00817 {
00818 ASFContext *asf = s->priv_data;
00819 ASFStream *stream;
00820 AVCodecContext *codec;
00821 uint32_t packet_number;
00822 int64_t pts;
00823 int start_sec;
00824 int flags= pkt->flags;
00825
00826 codec = s->streams[pkt->stream_index]->codec;
00827 stream = &asf->streams[pkt->stream_index];
00828
00829 if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
00830 flags &= ~AV_PKT_FLAG_KEY;
00831
00832 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
00833 assert(pts != AV_NOPTS_VALUE);
00834 pts *= 10000;
00835 asf->duration= FFMAX(asf->duration, pts + pkt->duration * 10000);
00836
00837 packet_number = asf->nb_packets;
00838 put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
00839
00840 start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
00841 / ASF_INDEXED_INTERVAL);
00842
00843
00844 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
00845 uint16_t packet_count = asf->nb_packets - packet_number;
00846 update_index(s, start_sec, packet_number, packet_count);
00847 }
00848 asf->end_sec = start_sec;
00849
00850 return 0;
00851 }
00852
00853
00854 static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
00855 {
00856 AVIOContext *pb = s->pb;
00857 int i;
00858
00859 ff_put_guid(pb, &ff_asf_simple_index_header);
00860 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
00861 ff_put_guid(pb, &ff_asf_my_guid);
00862 avio_wl64(pb, ASF_INDEXED_INTERVAL);
00863 avio_wl32(pb, max);
00864 avio_wl32(pb, count);
00865 for(i=0; i<count; i++) {
00866 avio_wl32(pb, index[i].packet_number);
00867 avio_wl16(pb, index[i].packet_count);
00868 }
00869
00870 return 0;
00871 }
00872
00873 static int asf_write_trailer(AVFormatContext *s)
00874 {
00875 ASFContext *asf = s->priv_data;
00876 int64_t file_size,data_size;
00877
00878
00879 if (asf->pb.buf_ptr > asf->pb.buffer)
00880 flush_packet(s);
00881
00882
00883 data_size = avio_tell(s->pb);
00884 if (!asf->is_streamed && asf->next_start_sec) {
00885 update_index(s, asf->end_sec + 1, 0, 0);
00886 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->next_start_sec);
00887 }
00888 avio_flush(s->pb);
00889
00890 if (asf->is_streamed || !s->pb->seekable) {
00891 put_chunk(s, 0x4524, 0, 0);
00892 } else {
00893
00894 file_size = avio_tell(s->pb);
00895 avio_seek(s->pb, 0, SEEK_SET);
00896 asf_write_header1(s, file_size, data_size - asf->data_offset);
00897 }
00898
00899 av_free(asf->index_ptr);
00900 return 0;
00901 }
00902
00903 #if CONFIG_ASF_MUXER
00904 AVOutputFormat ff_asf_muxer = {
00905 .name = "asf",
00906 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
00907 .mime_type = "video/x-ms-asf",
00908 .extensions = "asf,wmv,wma",
00909 .priv_data_size = sizeof(ASFContext),
00910 .audio_codec = AV_CODEC_ID_WMAV2,
00911 .video_codec = AV_CODEC_ID_MSMPEG4V3,
00912 .write_header = asf_write_header,
00913 .write_packet = asf_write_packet,
00914 .write_trailer = asf_write_trailer,
00915 .flags = AVFMT_GLOBALHEADER,
00916 .codec_tag = (const AVCodecTag* const []){
00917 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
00918 },
00919 };
00920 #endif
00921
00922 #if CONFIG_ASF_STREAM_MUXER
00923 AVOutputFormat ff_asf_stream_muxer = {
00924 .name = "asf_stream",
00925 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
00926 .mime_type = "video/x-ms-asf",
00927 .extensions = "asf,wmv,wma",
00928 .priv_data_size = sizeof(ASFContext),
00929 .audio_codec = AV_CODEC_ID_WMAV2,
00930 .video_codec = AV_CODEC_ID_MSMPEG4V3,
00931 .write_header = asf_write_stream_header,
00932 .write_packet = asf_write_packet,
00933 .write_trailer = asf_write_trailer,
00934 .flags = AVFMT_GLOBALHEADER,
00935 .codec_tag = (const AVCodecTag* const []){
00936 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
00937 },
00938 };
00939 #endif //CONFIG_ASF_STREAM_MUXER