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 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
00222 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
00223 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
00224 { 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 = s->metadata ? s->metadata->count : 0;
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 == 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 == 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 return 0;
00583 }
00584
00585 static int asf_write_stream_header(AVFormatContext *s)
00586 {
00587 ASFContext *asf = s->priv_data;
00588
00589 asf->is_streamed = 1;
00590
00591 return asf_write_header(s);
00592 }
00593
00594 static int put_payload_parsing_info(
00595 AVFormatContext *s,
00596 unsigned int sendtime,
00597 unsigned int duration,
00598 int nb_payloads,
00599 int padsize
00600 )
00601 {
00602 ASFContext *asf = s->priv_data;
00603 AVIOContext *pb = s->pb;
00604 int ppi_size, i;
00605 int64_t start= avio_tell(pb);
00606
00607 int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
00608
00609 padsize -= PACKET_HEADER_MIN_SIZE;
00610 if(asf->multi_payloads_present)
00611 padsize--;
00612 assert(padsize>=0);
00613
00614 avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
00615 for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
00616 avio_w8(pb, 0x0);
00617 }
00618
00619 if (asf->multi_payloads_present)
00620 iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
00621
00622 if (padsize > 0) {
00623 if (padsize < 256)
00624 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
00625 else
00626 iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
00627 }
00628 avio_w8(pb, iLengthTypeFlags);
00629
00630 avio_w8(pb, ASF_PPI_PROPERTY_FLAGS);
00631
00632 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
00633 avio_wl16(pb, padsize - 2);
00634 if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
00635 avio_w8(pb, padsize - 1);
00636
00637 avio_wl32(pb, sendtime);
00638 avio_wl16(pb, duration);
00639 if (asf->multi_payloads_present)
00640 avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
00641
00642 ppi_size = avio_tell(pb) - start;
00643
00644 return ppi_size;
00645 }
00646
00647 static void flush_packet(AVFormatContext *s)
00648 {
00649 ASFContext *asf = s->priv_data;
00650 int packet_hdr_size, packet_filled_size;
00651
00652 assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
00653
00654 if (asf->is_streamed) {
00655 put_chunk(s, 0x4424, s->packet_size, 0);
00656 }
00657
00658 packet_hdr_size = put_payload_parsing_info(
00659 s,
00660 asf->packet_timestamp_start,
00661 asf->packet_timestamp_end - asf->packet_timestamp_start,
00662 asf->packet_nb_payloads,
00663 asf->packet_size_left
00664 );
00665
00666 packet_filled_size = PACKET_SIZE - asf->packet_size_left;
00667 assert(packet_hdr_size <= asf->packet_size_left);
00668 memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
00669
00670 avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
00671
00672 avio_flush(s->pb);
00673 asf->nb_packets++;
00674 asf->packet_nb_payloads = 0;
00675 asf->packet_timestamp_start = -1;
00676 asf->packet_timestamp_end = -1;
00677 ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
00678 NULL, NULL, NULL, NULL);
00679 }
00680
00681 static void put_payload_header(
00682 AVFormatContext *s,
00683 ASFStream *stream,
00684 int64_t presentation_time,
00685 int m_obj_size,
00686 int m_obj_offset,
00687 int payload_len,
00688 int flags
00689 )
00690 {
00691 ASFContext *asf = s->priv_data;
00692 AVIOContext *pb = &asf->pb;
00693 int val;
00694
00695 val = stream->num;
00696 if (flags & AV_PKT_FLAG_KEY)
00697 val |= ASF_PL_FLAG_KEY_FRAME;
00698 avio_w8(pb, val);
00699
00700 avio_w8(pb, stream->seq);
00701 avio_wl32(pb, m_obj_offset);
00702
00703
00704
00705
00706
00707
00708 avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
00709
00710 avio_wl32(pb, m_obj_size);
00711 avio_wl32(pb, (uint32_t) presentation_time);
00712
00713 if (asf->multi_payloads_present){
00714 avio_wl16(pb, payload_len);
00715 }
00716 }
00717
00718 static void put_frame(
00719 AVFormatContext *s,
00720 ASFStream *stream,
00721 AVStream *avst,
00722 int64_t timestamp,
00723 const uint8_t *buf,
00724 int m_obj_size,
00725 int flags
00726 )
00727 {
00728 ASFContext *asf = s->priv_data;
00729 int m_obj_offset, payload_len, frag_len1;
00730
00731 m_obj_offset = 0;
00732 while (m_obj_offset < m_obj_size) {
00733 payload_len = m_obj_size - m_obj_offset;
00734 if (asf->packet_timestamp_start == -1) {
00735 asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
00736
00737 asf->packet_size_left = PACKET_SIZE;
00738 if (asf->multi_payloads_present){
00739 frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
00740 }
00741 else {
00742 frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
00743 }
00744 asf->packet_timestamp_start = timestamp;
00745 }
00746 else {
00747
00748 frag_len1 = asf->packet_size_left - PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS - PACKET_HEADER_MIN_SIZE - 1;
00749
00750 if(frag_len1 < payload_len && avst->codec->codec_type == AVMEDIA_TYPE_AUDIO){
00751 flush_packet(s);
00752 continue;
00753 }
00754 }
00755 if (frag_len1 > 0) {
00756 if (payload_len > frag_len1)
00757 payload_len = frag_len1;
00758 else if (payload_len == (frag_len1 - 1))
00759 payload_len = frag_len1 - 2;
00760
00761 put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
00762 avio_write(&asf->pb, buf, payload_len);
00763
00764 if (asf->multi_payloads_present)
00765 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS);
00766 else
00767 asf->packet_size_left -= (payload_len + PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD);
00768 asf->packet_timestamp_end = timestamp;
00769
00770 asf->packet_nb_payloads++;
00771 } else {
00772 payload_len = 0;
00773 }
00774 m_obj_offset += payload_len;
00775 buf += payload_len;
00776
00777 if (!asf->multi_payloads_present)
00778 flush_packet(s);
00779 else if (asf->packet_size_left <= (PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS + PACKET_HEADER_MIN_SIZE + 1))
00780 flush_packet(s);
00781 }
00782 stream->seq++;
00783 }
00784
00785 static void update_index(AVFormatContext *s, int start_sec,
00786 uint32_t packet_number, uint16_t packet_count)
00787 {
00788 ASFContext *asf = s->priv_data;
00789
00790 if (start_sec > asf->next_start_sec) {
00791 int i;
00792
00793 if (!asf->next_start_sec) {
00794 asf->next_packet_number = packet_number;
00795 asf->next_packet_count = packet_count;
00796 }
00797
00798 if (start_sec > asf->nb_index_memory_alloc) {
00799 asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
00800 asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
00801 }
00802 for (i = asf->next_start_sec; i < start_sec; i++) {
00803 asf->index_ptr[i].packet_number = asf->next_packet_number;
00804 asf->index_ptr[i].packet_count = asf->next_packet_count;
00805 }
00806 }
00807 asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
00808 asf->next_packet_number = packet_number;
00809 asf->next_packet_count = packet_count;
00810 asf->next_start_sec = start_sec;
00811 }
00812
00813 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
00814 {
00815 ASFContext *asf = s->priv_data;
00816 ASFStream *stream;
00817 AVCodecContext *codec;
00818 uint32_t packet_number;
00819 int64_t pts;
00820 int start_sec;
00821 int flags= pkt->flags;
00822
00823 codec = s->streams[pkt->stream_index]->codec;
00824 stream = &asf->streams[pkt->stream_index];
00825
00826 if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
00827 flags &= ~AV_PKT_FLAG_KEY;
00828
00829 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
00830 assert(pts != AV_NOPTS_VALUE);
00831 pts *= 10000;
00832 asf->duration= FFMAX(asf->duration, pts + pkt->duration * 10000);
00833
00834 packet_number = asf->nb_packets;
00835 put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
00836
00837 start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
00838 / ASF_INDEXED_INTERVAL);
00839
00840
00841 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
00842 uint16_t packet_count = asf->nb_packets - packet_number;
00843 update_index(s, start_sec, packet_number, packet_count);
00844 }
00845 asf->end_sec = start_sec;
00846
00847 return 0;
00848 }
00849
00850
00851 static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
00852 {
00853 AVIOContext *pb = s->pb;
00854 int i;
00855
00856 ff_put_guid(pb, &ff_asf_simple_index_header);
00857 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
00858 ff_put_guid(pb, &ff_asf_my_guid);
00859 avio_wl64(pb, ASF_INDEXED_INTERVAL);
00860 avio_wl32(pb, max);
00861 avio_wl32(pb, count);
00862 for(i=0; i<count; i++) {
00863 avio_wl32(pb, index[i].packet_number);
00864 avio_wl16(pb, index[i].packet_count);
00865 }
00866
00867 return 0;
00868 }
00869
00870 static int asf_write_trailer(AVFormatContext *s)
00871 {
00872 ASFContext *asf = s->priv_data;
00873 int64_t file_size,data_size;
00874
00875
00876 if (asf->pb.buf_ptr > asf->pb.buffer)
00877 flush_packet(s);
00878
00879
00880 data_size = avio_tell(s->pb);
00881 if (!asf->is_streamed && asf->next_start_sec) {
00882 update_index(s, asf->end_sec + 1, 0, 0);
00883 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->next_start_sec);
00884 }
00885 avio_flush(s->pb);
00886
00887 if (asf->is_streamed || !s->pb->seekable) {
00888 put_chunk(s, 0x4524, 0, 0);
00889 } else {
00890
00891 file_size = avio_tell(s->pb);
00892 avio_seek(s->pb, 0, SEEK_SET);
00893 asf_write_header1(s, file_size, data_size - asf->data_offset);
00894 }
00895
00896 avio_flush(s->pb);
00897 av_free(asf->index_ptr);
00898 return 0;
00899 }
00900
00901 #if CONFIG_ASF_MUXER
00902 AVOutputFormat ff_asf_muxer = {
00903 .name = "asf",
00904 .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
00905 .mime_type = "video/x-ms-asf",
00906 .extensions = "asf,wmv,wma",
00907 .priv_data_size = sizeof(ASFContext),
00908 .audio_codec = CODEC_ID_WMAV2,
00909 .video_codec = CODEC_ID_MSMPEG4V3,
00910 .write_header = asf_write_header,
00911 .write_packet = asf_write_packet,
00912 .write_trailer = asf_write_trailer,
00913 .flags = AVFMT_GLOBALHEADER,
00914 .codec_tag = (const AVCodecTag* const []){
00915 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
00916 },
00917 };
00918 #endif
00919
00920 #if CONFIG_ASF_STREAM_MUXER
00921 AVOutputFormat ff_asf_stream_muxer = {
00922 .name = "asf_stream",
00923 .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
00924 .mime_type = "video/x-ms-asf",
00925 .extensions = "asf,wmv,wma",
00926 .priv_data_size = sizeof(ASFContext),
00927 .audio_codec = CODEC_ID_WMAV2,
00928 .video_codec = CODEC_ID_MSMPEG4V3,
00929 .write_header = asf_write_stream_header,
00930 .write_packet = asf_write_packet,
00931 .write_trailer = asf_write_trailer,
00932 .flags = AVFMT_GLOBALHEADER,
00933 .codec_tag = (const AVCodecTag* const []){
00934 codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0
00935 },
00936 };
00937 #endif //CONFIG_ASF_STREAM_MUXER