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 600
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 int64_t last_indexed_pts;
00212 ASFIndex* index_ptr;
00213 uint32_t nb_index_count;
00214 uint32_t nb_index_memory_alloc;
00215 uint16_t maximum_packet;
00216 } ASFContext;
00217
00218 static const AVCodecTag codec_asf_bmp_tags[] = {
00219 { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
00220 { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
00221 { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
00222 { CODEC_ID_NONE, 0 },
00223 };
00224
00225 #define PREROLL_TIME 3100
00226
00227 void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
00228 {
00229 assert(sizeof(*g) == 16);
00230 avio_write(s, *g, sizeof(*g));
00231 }
00232
00233 static void put_str16(AVIOContext *s, const char *tag)
00234 {
00235 int len;
00236 uint8_t *pb;
00237 AVIOContext *dyn_buf;
00238 if (avio_open_dyn_buf(&dyn_buf) < 0)
00239 return;
00240
00241 avio_put_str16le(dyn_buf, tag);
00242 len = avio_close_dyn_buf(dyn_buf, &pb);
00243 avio_wl16(s, len);
00244 avio_write(s, pb, len);
00245 av_freep(&pb);
00246 }
00247
00248 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
00249 {
00250 int64_t pos;
00251
00252 pos = avio_tell(pb);
00253 ff_put_guid(pb, g);
00254 avio_wl64(pb, 24);
00255 return pos;
00256 }
00257
00258
00259 static void end_header(AVIOContext *pb, int64_t pos)
00260 {
00261 int64_t pos1;
00262
00263 pos1 = avio_tell(pb);
00264 avio_seek(pb, pos + 16, SEEK_SET);
00265 avio_wl64(pb, pos1 - pos);
00266 avio_seek(pb, pos1, SEEK_SET);
00267 }
00268
00269
00270 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
00271 {
00272 ASFContext *asf = s->priv_data;
00273 AVIOContext *pb = s->pb;
00274 int length;
00275
00276 length = payload_length + 8;
00277 avio_wl16(pb, type);
00278 avio_wl16(pb, length);
00279 avio_wl32(pb, asf->seqno);
00280 avio_wl16(pb, flags);
00281 avio_wl16(pb, length);
00282 asf->seqno++;
00283 }
00284
00285
00286 static int64_t unix_to_file_time(int ti)
00287 {
00288 int64_t t;
00289
00290 t = ti * INT64_C(10000000);
00291 t += INT64_C(116444736000000000);
00292 return t;
00293 }
00294
00295
00296 static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
00297 {
00298 ASFContext *asf = s->priv_data;
00299 AVIOContext *pb = s->pb;
00300 AVDictionaryEntry *tags[5];
00301 int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
00302 int has_title;
00303 int metadata_count;
00304 AVCodecContext *enc;
00305 int64_t header_offset, cur_pos, hpos;
00306 int bit_rate;
00307 int64_t duration;
00308
00309 ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL);
00310
00311 tags[0] = av_dict_get(s->metadata, "title" , NULL, 0);
00312 tags[1] = av_dict_get(s->metadata, "author" , NULL, 0);
00313 tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
00314 tags[3] = av_dict_get(s->metadata, "comment" , NULL, 0);
00315 tags[4] = av_dict_get(s->metadata, "rating" , NULL, 0);
00316
00317 duration = asf->duration + PREROLL_TIME * 10000;
00318 has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
00319 metadata_count = s->metadata ? s->metadata->count : 0;
00320
00321 bit_rate = 0;
00322 for(n=0;n<s->nb_streams;n++) {
00323 enc = s->streams[n]->codec;
00324
00325 avpriv_set_pts_info(s->streams[n], 32, 1, 1000);
00326
00327 bit_rate += enc->bit_rate;
00328 }
00329
00330 if (asf->is_streamed) {
00331 put_chunk(s, 0x4824, 0, 0xc00);
00332 }
00333
00334 ff_put_guid(pb, &ff_asf_header);
00335 avio_wl64(pb, -1);
00336 avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams);
00337 avio_w8(pb, 1);
00338 avio_w8(pb, 2);
00339
00340
00341 header_offset = avio_tell(pb);
00342 hpos = put_header(pb, &ff_asf_file_header);
00343 ff_put_guid(pb, &ff_asf_my_guid);
00344 avio_wl64(pb, file_size);
00345 file_time = 0;
00346 avio_wl64(pb, unix_to_file_time(file_time));
00347 avio_wl64(pb, asf->nb_packets);
00348 avio_wl64(pb, duration);
00349 avio_wl64(pb, asf->duration);
00350 avio_wl64(pb, PREROLL_TIME);
00351 avio_wl32(pb, (asf->is_streamed || !pb->seekable ) ? 3 : 2);
00352 avio_wl32(pb, s->packet_size);
00353 avio_wl32(pb, s->packet_size);
00354 avio_wl32(pb, bit_rate);
00355 end_header(pb, hpos);
00356
00357
00358 hpos = put_header(pb, &ff_asf_head1_guid);
00359 ff_put_guid(pb, &ff_asf_head2_guid);
00360 avio_wl32(pb, 6);
00361 avio_wl16(pb, 0);
00362 end_header(pb, hpos);
00363
00364
00365 if (has_title) {
00366 int len;
00367 uint8_t *buf;
00368 AVIOContext *dyn_buf;
00369
00370 if (avio_open_dyn_buf(&dyn_buf) < 0)
00371 return AVERROR(ENOMEM);
00372
00373 hpos = put_header(pb, &ff_asf_comment_header);
00374
00375 for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
00376 len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
00377 avio_wl16(pb, len);
00378 }
00379 len = avio_close_dyn_buf(dyn_buf, &buf);
00380 avio_write(pb, buf, len);
00381 av_freep(&buf);
00382 end_header(pb, hpos);
00383 }
00384 if (metadata_count) {
00385 AVDictionaryEntry *tag = NULL;
00386 hpos = put_header(pb, &ff_asf_extended_content_header);
00387 avio_wl16(pb, metadata_count);
00388 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00389 put_str16(pb, tag->key);
00390 avio_wl16(pb, 0);
00391 put_str16(pb, tag->value);
00392 }
00393 end_header(pb, hpos);
00394 }
00395
00396
00397 for(n=0;n<s->nb_streams;n++) {
00398 int64_t es_pos;
00399
00400
00401 enc = s->streams[n]->codec;
00402 asf->streams[n].num = n + 1;
00403 asf->streams[n].seq = 0;
00404
00405
00406 switch(enc->codec_type) {
00407 case AVMEDIA_TYPE_AUDIO:
00408 wav_extra_size = 0;
00409 extra_size = 18 + wav_extra_size;
00410 extra_size2 = 8;
00411 break;
00412 default:
00413 case AVMEDIA_TYPE_VIDEO:
00414 wav_extra_size = enc->extradata_size;
00415 extra_size = 0x33 + wav_extra_size;
00416 extra_size2 = 0;
00417 break;
00418 }
00419
00420 hpos = put_header(pb, &ff_asf_stream_header);
00421 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00422 ff_put_guid(pb, &ff_asf_audio_stream);
00423 ff_put_guid(pb, &ff_asf_audio_conceal_spread);
00424 } else {
00425 ff_put_guid(pb, &ff_asf_video_stream);
00426 ff_put_guid(pb, &ff_asf_video_conceal_none);
00427 }
00428 avio_wl64(pb, 0);
00429 es_pos = avio_tell(pb);
00430 avio_wl32(pb, extra_size);
00431 avio_wl32(pb, extra_size2);
00432 avio_wl16(pb, n + 1);
00433 avio_wl32(pb, 0);
00434
00435 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00436
00437 int wavsize = ff_put_wav_header(pb, enc);
00438
00439 if (wavsize < 0)
00440 return -1;
00441 if (wavsize != extra_size) {
00442 cur_pos = avio_tell(pb);
00443 avio_seek(pb, es_pos, SEEK_SET);
00444 avio_wl32(pb, wavsize);
00445 avio_seek(pb, cur_pos, SEEK_SET);
00446 }
00447
00448 avio_w8(pb, 0x01);
00449 if(enc->codec_id == CODEC_ID_ADPCM_G726 || !enc->block_align){
00450 avio_wl16(pb, 0x0190);
00451 avio_wl16(pb, 0x0190);
00452 }else{
00453 avio_wl16(pb, enc->block_align);
00454 avio_wl16(pb, enc->block_align);
00455 }
00456 avio_wl16(pb, 0x01);
00457 avio_w8(pb, 0x00);
00458 } else {
00459 avio_wl32(pb, enc->width);
00460 avio_wl32(pb, enc->height);
00461 avio_w8(pb, 2);
00462 avio_wl16(pb, 40 + enc->extradata_size);
00463
00464
00465 ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1);
00466 }
00467 end_header(pb, hpos);
00468 }
00469
00470
00471
00472 hpos = put_header(pb, &ff_asf_codec_comment_header);
00473 ff_put_guid(pb, &ff_asf_codec_comment1_header);
00474 avio_wl32(pb, s->nb_streams);
00475 for(n=0;n<s->nb_streams;n++) {
00476 AVCodec *p;
00477 const char *desc;
00478 int len;
00479 uint8_t *buf;
00480 AVIOContext *dyn_buf;
00481
00482 enc = s->streams[n]->codec;
00483 p = avcodec_find_encoder(enc->codec_id);
00484
00485 if(enc->codec_type == AVMEDIA_TYPE_AUDIO)
00486 avio_wl16(pb, 2);
00487 else if(enc->codec_type == AVMEDIA_TYPE_VIDEO)
00488 avio_wl16(pb, 1);
00489 else
00490 avio_wl16(pb, -1);
00491
00492 if(enc->codec_id == CODEC_ID_WMAV2)
00493 desc = "Windows Media Audio V8";
00494 else
00495 desc = p ? p->name : enc->codec_name;
00496
00497 if ( avio_open_dyn_buf(&dyn_buf) < 0)
00498 return AVERROR(ENOMEM);
00499
00500 avio_put_str16le(dyn_buf, desc);
00501 len = avio_close_dyn_buf(dyn_buf, &buf);
00502 avio_wl16(pb, len / 2);
00503
00504 avio_write(pb, buf, len);
00505 av_freep(&buf);
00506
00507 avio_wl16(pb, 0);
00508
00509
00510
00511 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00512 avio_wl16(pb, 2);
00513 avio_wl16(pb, enc->codec_tag);
00514 } else {
00515 avio_wl16(pb, 4);
00516 avio_wl32(pb, enc->codec_tag);
00517 }
00518 if(!enc->codec_tag)
00519 return -1;
00520 }
00521 end_header(pb, hpos);
00522
00523
00524
00525 cur_pos = avio_tell(pb);
00526 header_size = cur_pos - header_offset;
00527 if (asf->is_streamed) {
00528 header_size += 8 + 30 + 50;
00529
00530 avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
00531 avio_wl16(pb, header_size);
00532 avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
00533 avio_wl16(pb, header_size);
00534
00535 header_size -= 8 + 30 + 50;
00536 }
00537 header_size += 24 + 6;
00538 avio_seek(pb, header_offset - 14, SEEK_SET);
00539 avio_wl64(pb, header_size);
00540 avio_seek(pb, cur_pos, SEEK_SET);
00541
00542
00543 asf->data_offset = cur_pos;
00544 ff_put_guid(pb, &ff_asf_data_header);
00545 avio_wl64(pb, data_chunk_size);
00546 ff_put_guid(pb, &ff_asf_my_guid);
00547 avio_wl64(pb, asf->nb_packets);
00548 avio_w8(pb, 1);
00549 avio_w8(pb, 1);
00550 return 0;
00551 }
00552
00553 static int asf_write_header(AVFormatContext *s)
00554 {
00555 ASFContext *asf = s->priv_data;
00556
00557 s->packet_size = PACKET_SIZE;
00558 asf->nb_packets = 0;
00559
00560 asf->last_indexed_pts = 0;
00561 asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
00562 asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
00563 asf->nb_index_count = 0;
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 int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
00786 {
00787 ASFContext *asf = s->priv_data;
00788 ASFStream *stream;
00789 int64_t duration;
00790 AVCodecContext *codec;
00791 int64_t packet_st,pts;
00792 int start_sec,i;
00793 int flags= pkt->flags;
00794
00795 codec = s->streams[pkt->stream_index]->codec;
00796 stream = &asf->streams[pkt->stream_index];
00797
00798 if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
00799 flags &= ~AV_PKT_FLAG_KEY;
00800
00801 pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
00802 assert(pts != AV_NOPTS_VALUE);
00803 duration = pts * 10000;
00804 asf->duration= FFMAX(asf->duration, duration + pkt->duration * 10000);
00805
00806 packet_st = asf->nb_packets;
00807 put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
00808
00809
00810 if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
00811 start_sec = (int)(duration / INT64_C(10000000));
00812 if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
00813 for(i=asf->nb_index_count;i<start_sec;i++) {
00814 if (i>=asf->nb_index_memory_alloc) {
00815 asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
00816 asf->index_ptr = (ASFIndex*)av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
00817 }
00818
00819 asf->index_ptr[i].packet_number = (uint32_t)packet_st;
00820 asf->index_ptr[i].packet_count = (uint16_t)(asf->nb_packets-packet_st);
00821 asf->maximum_packet = FFMAX(asf->maximum_packet, (uint16_t)(asf->nb_packets-packet_st));
00822 }
00823 asf->nb_index_count = start_sec;
00824 asf->last_indexed_pts = duration;
00825 }
00826 }
00827 return 0;
00828 }
00829
00830
00831 static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
00832 {
00833 AVIOContext *pb = s->pb;
00834 int i;
00835
00836 ff_put_guid(pb, &ff_asf_simple_index_header);
00837 avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
00838 ff_put_guid(pb, &ff_asf_my_guid);
00839 avio_wl64(pb, ASF_INDEXED_INTERVAL);
00840 avio_wl32(pb, max);
00841 avio_wl32(pb, count);
00842 for(i=0; i<count; i++) {
00843 avio_wl32(pb, index[i].packet_number);
00844 avio_wl16(pb, index[i].packet_count);
00845 }
00846
00847 return 0;
00848 }
00849
00850 static int asf_write_trailer(AVFormatContext *s)
00851 {
00852 ASFContext *asf = s->priv_data;
00853 int64_t file_size,data_size;
00854
00855
00856 if (asf->pb.buf_ptr > asf->pb.buffer)
00857 flush_packet(s);
00858
00859
00860 data_size = avio_tell(s->pb);
00861 if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
00862 asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
00863 }
00864 avio_flush(s->pb);
00865
00866 if (asf->is_streamed || !s->pb->seekable) {
00867 put_chunk(s, 0x4524, 0, 0);
00868 } else {
00869
00870 file_size = avio_tell(s->pb);
00871 avio_seek(s->pb, 0, SEEK_SET);
00872 asf_write_header1(s, file_size, data_size - asf->data_offset);
00873 }
00874
00875 avio_flush(s->pb);
00876 av_free(asf->index_ptr);
00877 return 0;
00878 }
00879
00880 #if CONFIG_ASF_MUXER
00881 AVOutputFormat ff_asf_muxer = {
00882 .name = "asf",
00883 .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
00884 .mime_type = "video/x-ms-asf",
00885 .extensions = "asf,wmv,wma",
00886 .priv_data_size = sizeof(ASFContext),
00887 .audio_codec = CODEC_ID_WMAV2,
00888 .video_codec = CODEC_ID_MSMPEG4V3,
00889 .write_header = asf_write_header,
00890 .write_packet = asf_write_packet,
00891 .write_trailer = asf_write_trailer,
00892 .flags = AVFMT_GLOBALHEADER,
00893 .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
00894 };
00895 #endif
00896
00897 #if CONFIG_ASF_STREAM_MUXER
00898 AVOutputFormat ff_asf_stream_muxer = {
00899 .name = "asf_stream",
00900 .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
00901 .mime_type = "video/x-ms-asf",
00902 .extensions = "asf,wmv,wma",
00903 .priv_data_size = sizeof(ASFContext),
00904 .audio_codec = CODEC_ID_WMAV2,
00905 .video_codec = CODEC_ID_MSMPEG4V3,
00906 .write_header = asf_write_stream_header,
00907 .write_packet = asf_write_packet,
00908 .write_trailer = asf_write_trailer,
00909 .flags = AVFMT_GLOBALHEADER,
00910 .codec_tag= (const AVCodecTag* const []){codec_asf_bmp_tags, ff_codec_bmp_tags, ff_codec_wav_tags, 0},
00911 };
00912 #endif //CONFIG_ASF_STREAM_MUXER