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 "rm.h"
00023
00024 typedef struct {
00025 int nb_packets;
00026 int packet_total_size;
00027 int packet_max_size;
00028
00029 int bit_rate;
00030 float frame_rate;
00031 int nb_frames;
00032 int total_frames;
00033 int num;
00034 AVCodecContext *enc;
00035 } StreamInfo;
00036
00037 typedef struct {
00038 StreamInfo streams[2];
00039 StreamInfo *audio_stream, *video_stream;
00040 int data_pos;
00041 } RMMuxContext;
00042
00043
00044 #define BUFFER_DURATION 0
00045
00046
00047 static void put_str(ByteIOContext *s, const char *tag)
00048 {
00049 put_be16(s,strlen(tag));
00050 while (*tag) {
00051 put_byte(s, *tag++);
00052 }
00053 }
00054
00055 static void put_str8(ByteIOContext *s, const char *tag)
00056 {
00057 put_byte(s, strlen(tag));
00058 while (*tag) {
00059 put_byte(s, *tag++);
00060 }
00061 }
00062
00063 static void rv10_write_header(AVFormatContext *ctx,
00064 int data_size, int index_pos)
00065 {
00066 RMMuxContext *rm = ctx->priv_data;
00067 ByteIOContext *s = ctx->pb;
00068 StreamInfo *stream;
00069 unsigned char *data_offset_ptr, *start_ptr;
00070 const char *desc, *mimetype;
00071 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
00072 int bit_rate, v, duration, flags, data_pos;
00073 AVMetadataTag *tag;
00074
00075 start_ptr = s->buf_ptr;
00076
00077 put_tag(s, ".RMF");
00078 put_be32(s,18);
00079 put_be16(s,0);
00080 put_be32(s,0);
00081 put_be32(s,4 + ctx->nb_streams);
00082
00083 put_tag(s,"PROP");
00084 put_be32(s, 50);
00085 put_be16(s, 0);
00086 packet_max_size = 0;
00087 packet_total_size = 0;
00088 nb_packets = 0;
00089 bit_rate = 0;
00090 duration = 0;
00091 for(i=0;i<ctx->nb_streams;i++) {
00092 StreamInfo *stream = &rm->streams[i];
00093 bit_rate += stream->bit_rate;
00094 if (stream->packet_max_size > packet_max_size)
00095 packet_max_size = stream->packet_max_size;
00096 nb_packets += stream->nb_packets;
00097 packet_total_size += stream->packet_total_size;
00098
00099 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
00100 if (v > duration)
00101 duration = v;
00102 }
00103 put_be32(s, bit_rate);
00104 put_be32(s, bit_rate);
00105 put_be32(s, packet_max_size);
00106 if (nb_packets > 0)
00107 packet_avg_size = packet_total_size / nb_packets;
00108 else
00109 packet_avg_size = 0;
00110 put_be32(s, packet_avg_size);
00111 put_be32(s, nb_packets);
00112 put_be32(s, duration);
00113 put_be32(s, BUFFER_DURATION);
00114 put_be32(s, index_pos);
00115
00116 data_offset_ptr = s->buf_ptr;
00117 put_be32(s, 0);
00118 put_be16(s, ctx->nb_streams);
00119 flags = 1 | 2;
00120 if (url_is_streamed(s))
00121 flags |= 4;
00122 put_be16(s, flags);
00123
00124
00125
00126 put_tag(s,"CONT");
00127 size = 4 * 2 + 10;
00128 for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
00129 tag = av_metadata_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
00130 if(tag) size += strlen(tag->value);
00131 }
00132 put_be32(s,size);
00133 put_be16(s,0);
00134 for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
00135 tag = av_metadata_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
00136 put_str(s, tag ? tag->value : "");
00137 }
00138
00139 for(i=0;i<ctx->nb_streams;i++) {
00140 int codec_data_size;
00141
00142 stream = &rm->streams[i];
00143
00144 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
00145 desc = "The Audio Stream";
00146 mimetype = "audio/x-pn-realaudio";
00147 codec_data_size = 73;
00148 } else {
00149 desc = "The Video Stream";
00150 mimetype = "video/x-pn-realvideo";
00151 codec_data_size = 34;
00152 }
00153
00154 put_tag(s,"MDPR");
00155 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
00156 put_be32(s, size);
00157 put_be16(s, 0);
00158
00159 put_be16(s, i);
00160 put_be32(s, stream->bit_rate);
00161 put_be32(s, stream->bit_rate);
00162 put_be32(s, stream->packet_max_size);
00163 if (stream->nb_packets > 0)
00164 packet_avg_size = stream->packet_total_size /
00165 stream->nb_packets;
00166 else
00167 packet_avg_size = 0;
00168 put_be32(s, packet_avg_size);
00169 put_be32(s, 0);
00170 put_be32(s, BUFFER_DURATION);
00171
00172 if (url_is_streamed(s) || !stream->total_frames)
00173 put_be32(s, (int)(3600 * 1000));
00174 else
00175 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
00176 put_str8(s, desc);
00177 put_str8(s, mimetype);
00178 put_be32(s, codec_data_size);
00179
00180 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
00181 int coded_frame_size, fscode, sample_rate;
00182 sample_rate = stream->enc->sample_rate;
00183 coded_frame_size = (stream->enc->bit_rate *
00184 stream->enc->frame_size) / (8 * sample_rate);
00185
00186 put_tag(s, ".ra");
00187 put_byte(s, 0xfd);
00188 put_be32(s, 0x00040000);
00189 put_tag(s, ".ra4");
00190 put_be32(s, 0x01b53530);
00191 put_be16(s, 4);
00192 put_be32(s, 0x39);
00193
00194 switch(sample_rate) {
00195 case 48000:
00196 case 24000:
00197 case 12000:
00198 fscode = 1;
00199 break;
00200 default:
00201 case 44100:
00202 case 22050:
00203 case 11025:
00204 fscode = 2;
00205 break;
00206 case 32000:
00207 case 16000:
00208 case 8000:
00209 fscode = 3;
00210 }
00211 put_be16(s, fscode);
00212
00213
00214 if (coded_frame_size == 557)
00215 coded_frame_size--;
00216 put_be32(s, coded_frame_size);
00217 put_be32(s, 0x51540);
00218 put_be32(s, 0x249f0);
00219 put_be32(s, 0x249f0);
00220 put_be16(s, 0x01);
00221
00222 put_be16(s, coded_frame_size);
00223 put_be32(s, 0);
00224 put_be16(s, stream->enc->sample_rate);
00225 put_be32(s, 0x10);
00226 put_be16(s, stream->enc->channels);
00227 put_str8(s, "Int0");
00228 put_str8(s, "dnet");
00229 put_be16(s, 0);
00230 put_be16(s, 0);
00231 put_be16(s, 0);
00232 put_byte(s, 0);
00233 } else {
00234
00235 put_be32(s,34);
00236 if(stream->enc->codec_id == CODEC_ID_RV10)
00237 put_tag(s,"VIDORV10");
00238 else
00239 put_tag(s,"VIDORV20");
00240 put_be16(s, stream->enc->width);
00241 put_be16(s, stream->enc->height);
00242 put_be16(s, (int) stream->frame_rate);
00243 put_be32(s,0);
00244 put_be16(s, (int) stream->frame_rate);
00245 put_be32(s,0);
00246 put_be16(s, 8);
00247
00248
00249
00250 if(stream->enc->codec_id == CODEC_ID_RV10)
00251 put_be32(s,0x10000000);
00252 else
00253 put_be32(s,0x20103001);
00254
00255 }
00256 }
00257
00258
00259 data_pos = s->buf_ptr - start_ptr;
00260 rm->data_pos = data_pos;
00261 data_offset_ptr[0] = data_pos >> 24;
00262 data_offset_ptr[1] = data_pos >> 16;
00263 data_offset_ptr[2] = data_pos >> 8;
00264 data_offset_ptr[3] = data_pos;
00265
00266
00267 put_tag(s,"DATA");
00268 put_be32(s,data_size + 10 + 8);
00269 put_be16(s,0);
00270
00271 put_be32(s, nb_packets);
00272 put_be32(s,0);
00273 }
00274
00275 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
00276 int length, int key_frame)
00277 {
00278 int timestamp;
00279 ByteIOContext *s = ctx->pb;
00280
00281 stream->nb_packets++;
00282 stream->packet_total_size += length;
00283 if (length > stream->packet_max_size)
00284 stream->packet_max_size = length;
00285
00286 put_be16(s,0);
00287 put_be16(s,length + 12);
00288 put_be16(s, stream->num);
00289 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
00290 put_be32(s, timestamp);
00291 put_byte(s, 0);
00292 put_byte(s, key_frame ? 2 : 0);
00293 }
00294
00295 static int rm_write_header(AVFormatContext *s)
00296 {
00297 RMMuxContext *rm = s->priv_data;
00298 StreamInfo *stream;
00299 int n;
00300 AVCodecContext *codec;
00301
00302 for(n=0;n<s->nb_streams;n++) {
00303 s->streams[n]->id = n;
00304 codec = s->streams[n]->codec;
00305 stream = &rm->streams[n];
00306 memset(stream, 0, sizeof(StreamInfo));
00307 stream->num = n;
00308 stream->bit_rate = codec->bit_rate;
00309 stream->enc = codec;
00310
00311 switch(codec->codec_type) {
00312 case CODEC_TYPE_AUDIO:
00313 rm->audio_stream = stream;
00314 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
00315
00316 stream->packet_max_size = 1024;
00317 stream->nb_packets = 0;
00318 stream->total_frames = stream->nb_packets;
00319 break;
00320 case CODEC_TYPE_VIDEO:
00321 rm->video_stream = stream;
00322 stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
00323
00324 stream->packet_max_size = 4096;
00325 stream->nb_packets = 0;
00326 stream->total_frames = stream->nb_packets;
00327 break;
00328 default:
00329 return -1;
00330 }
00331 }
00332
00333 rv10_write_header(s, 0, 0);
00334 put_flush_packet(s->pb);
00335 return 0;
00336 }
00337
00338 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
00339 {
00340 uint8_t *buf1;
00341 RMMuxContext *rm = s->priv_data;
00342 ByteIOContext *pb = s->pb;
00343 StreamInfo *stream = rm->audio_stream;
00344 int i;
00345
00346
00347 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
00348
00349 write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
00350
00351
00352 for(i=0;i<size;i+=2) {
00353 buf1[i] = buf[i+1];
00354 buf1[i+1] = buf[i];
00355 }
00356 put_buffer(pb, buf1, size);
00357 put_flush_packet(pb);
00358 stream->nb_frames++;
00359 av_free(buf1);
00360 return 0;
00361 }
00362
00363 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
00364 {
00365 RMMuxContext *rm = s->priv_data;
00366 ByteIOContext *pb = s->pb;
00367 StreamInfo *stream = rm->video_stream;
00368 int key_frame = !!(flags & PKT_FLAG_KEY);
00369
00370
00371
00372
00373
00374 #if 1
00375 write_packet_header(s, stream, size + 7 + (size >= 0x4000)*4, key_frame);
00376
00377 put_byte(pb, 0x81);
00378
00379
00380 if (key_frame) {
00381 put_byte(pb, 0x81);
00382 } else {
00383 put_byte(pb, 0x01);
00384 }
00385 if(size >= 0x4000){
00386 put_be32(pb, size);
00387 put_be32(pb, size);
00388 }else{
00389 put_be16(pb, 0x4000 | size);
00390 put_be16(pb, 0x4000 | size);
00391 }
00392 #else
00393
00394 write_packet_header(s, size + 6);
00395 put_byte(pb, 0xc0);
00396 put_be16(pb, 0x4000 + size);
00397 put_be16(pb, 0x4000 + packet_number * 126);
00398 #endif
00399 put_byte(pb, stream->nb_frames & 0xff);
00400
00401 put_buffer(pb, buf, size);
00402 put_flush_packet(pb);
00403
00404 stream->nb_frames++;
00405 return 0;
00406 }
00407
00408 static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
00409 {
00410 if (s->streams[pkt->stream_index]->codec->codec_type ==
00411 CODEC_TYPE_AUDIO)
00412 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
00413 else
00414 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
00415 }
00416
00417 static int rm_write_trailer(AVFormatContext *s)
00418 {
00419 RMMuxContext *rm = s->priv_data;
00420 int data_size, index_pos, i;
00421 ByteIOContext *pb = s->pb;
00422
00423 if (!url_is_streamed(s->pb)) {
00424
00425 index_pos = url_fseek(pb, 0, SEEK_CUR);
00426 data_size = index_pos - rm->data_pos;
00427
00428
00429 put_tag(pb, "INDX");
00430 put_be32(pb, 10 + 10 * s->nb_streams);
00431 put_be16(pb, 0);
00432
00433 for(i=0;i<s->nb_streams;i++) {
00434 put_be32(pb, 0);
00435 put_be16(pb, i);
00436 put_be32(pb, 0);
00437 }
00438
00439 put_be32(pb, 0);
00440 put_be32(pb, 0);
00441
00442 url_fseek(pb, 0, SEEK_SET);
00443 for(i=0;i<s->nb_streams;i++)
00444 rm->streams[i].total_frames = rm->streams[i].nb_frames;
00445 rv10_write_header(s, data_size, index_pos);
00446 } else {
00447
00448 put_be32(pb, 0);
00449 put_be32(pb, 0);
00450 }
00451 put_flush_packet(pb);
00452 return 0;
00453 }
00454
00455
00456 AVOutputFormat rm_muxer = {
00457 "rm",
00458 NULL_IF_CONFIG_SMALL("RealMedia format"),
00459 "application/vnd.rn-realmedia",
00460 "rm,ra",
00461 sizeof(RMMuxContext),
00462 CODEC_ID_AC3,
00463 CODEC_ID_RV10,
00464 rm_write_header,
00465 rm_write_packet,
00466 rm_write_trailer,
00467 };