2 * "Real" compatible muxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 int packet_total_size;
28 /* codec related output */
31 int nb_frames; /* current frame number */
32 int total_frames; /* total number of frames */
38 StreamInfo streams[2];
39 StreamInfo *audio_stream, *video_stream;
40 int data_pos; /* position of the data after the header */
44 #define BUFFER_DURATION 0
47 static void put_str(ByteIOContext *s, const char *tag)
49 put_be16(s,strlen(tag));
55 static void put_str8(ByteIOContext *s, const char *tag)
57 put_byte(s, strlen(tag));
63 static void rv10_write_header(AVFormatContext *ctx,
64 int data_size, int index_pos)
66 RMMuxContext *rm = ctx->priv_data;
67 ByteIOContext *s = ctx->pb;
69 unsigned char *data_offset_ptr, *start_ptr;
70 const char *desc, *mimetype;
71 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
72 int bit_rate, v, duration, flags, data_pos;
75 start_ptr = s->buf_ptr;
78 put_be32(s,18); /* header size */
81 put_be32(s,4 + ctx->nb_streams); /* num headers */
87 packet_total_size = 0;
91 for(i=0;i<ctx->nb_streams;i++) {
92 StreamInfo *stream = &rm->streams[i];
93 bit_rate += stream->bit_rate;
94 if (stream->packet_max_size > packet_max_size)
95 packet_max_size = stream->packet_max_size;
96 nb_packets += stream->nb_packets;
97 packet_total_size += stream->packet_total_size;
98 /* select maximum duration */
99 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
103 put_be32(s, bit_rate); /* max bit rate */
104 put_be32(s, bit_rate); /* avg bit rate */
105 put_be32(s, packet_max_size); /* max packet size */
107 packet_avg_size = packet_total_size / nb_packets;
110 put_be32(s, packet_avg_size); /* avg packet size */
111 put_be32(s, nb_packets); /* num packets */
112 put_be32(s, duration); /* duration */
113 put_be32(s, BUFFER_DURATION); /* preroll */
114 put_be32(s, index_pos); /* index offset */
115 /* computation of data the data offset */
116 data_offset_ptr = s->buf_ptr;
117 put_be32(s, 0); /* data offset : will be patched after */
118 put_be16(s, ctx->nb_streams); /* num streams */
119 flags = 1 | 2; /* save allowed & perfect play */
120 if (url_is_streamed(s))
121 flags |= 4; /* live broadcast */
128 for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
129 tag = av_metadata_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
130 if(tag) size += strlen(tag->value);
134 for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
135 tag = av_metadata_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
136 put_str(s, tag ? tag->value : "");
139 for(i=0;i<ctx->nb_streams;i++) {
142 stream = &rm->streams[i];
144 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
145 desc = "The Audio Stream";
146 mimetype = "audio/x-pn-realaudio";
147 codec_data_size = 73;
149 desc = "The Video Stream";
150 mimetype = "video/x-pn-realvideo";
151 codec_data_size = 34;
155 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
159 put_be16(s, i); /* stream number */
160 put_be32(s, stream->bit_rate); /* max bit rate */
161 put_be32(s, stream->bit_rate); /* avg bit rate */
162 put_be32(s, stream->packet_max_size); /* max packet size */
163 if (stream->nb_packets > 0)
164 packet_avg_size = stream->packet_total_size /
168 put_be32(s, packet_avg_size); /* avg packet size */
169 put_be32(s, 0); /* start time */
170 put_be32(s, BUFFER_DURATION); /* preroll */
172 if (url_is_streamed(s) || !stream->total_frames)
173 put_be32(s, (int)(3600 * 1000));
175 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
177 put_str8(s, mimetype);
178 put_be32(s, codec_data_size);
180 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
181 int coded_frame_size, fscode, sample_rate;
182 sample_rate = stream->enc->sample_rate;
183 coded_frame_size = (stream->enc->bit_rate *
184 stream->enc->frame_size) / (8 * sample_rate);
185 /* audio codec info */
188 put_be32(s, 0x00040000); /* version */
190 put_be32(s, 0x01b53530); /* stream length */
191 put_be16(s, 4); /* unknown */
192 put_be32(s, 0x39); /* header size */
194 switch(sample_rate) {
211 put_be16(s, fscode); /* codec additional info, for AC-3, seems
212 to be a frequency code */
213 /* special hack to compensate rounding errors... */
214 if (coded_frame_size == 557)
216 put_be32(s, coded_frame_size); /* frame length */
217 put_be32(s, 0x51540); /* unknown */
218 put_be32(s, 0x249f0); /* unknown */
219 put_be32(s, 0x249f0); /* unknown */
221 /* frame length : seems to be very important */
222 put_be16(s, coded_frame_size);
223 put_be32(s, 0); /* unknown */
224 put_be16(s, stream->enc->sample_rate); /* sample rate */
225 put_be32(s, 0x10); /* unknown */
226 put_be16(s, stream->enc->channels);
227 put_str8(s, "Int0"); /* codec name */
228 put_str8(s, "dnet"); /* codec name */
229 put_be16(s, 0); /* title length */
230 put_be16(s, 0); /* author length */
231 put_be16(s, 0); /* copyright length */
232 put_byte(s, 0); /* end of header */
234 /* video codec info */
235 put_be32(s,34); /* size */
236 if(stream->enc->codec_id == CODEC_ID_RV10)
237 put_tag(s,"VIDORV10");
239 put_tag(s,"VIDORV20");
240 put_be16(s, stream->enc->width);
241 put_be16(s, stream->enc->height);
242 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
243 put_be32(s,0); /* unknown meaning */
244 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
245 put_be32(s,0); /* unknown meaning */
246 put_be16(s, 8); /* unknown meaning */
247 /* Seems to be the codec version: only use basic H263. The next
248 versions seems to add a diffential DC coding as in
249 MPEG... nothing new under the sun */
250 if(stream->enc->codec_id == CODEC_ID_RV10)
251 put_be32(s,0x10000000);
253 put_be32(s,0x20103001);
254 //put_be32(s,0x10003000);
258 /* patch data offset field */
259 data_pos = s->buf_ptr - start_ptr;
260 rm->data_pos = data_pos;
261 data_offset_ptr[0] = data_pos >> 24;
262 data_offset_ptr[1] = data_pos >> 16;
263 data_offset_ptr[2] = data_pos >> 8;
264 data_offset_ptr[3] = data_pos;
268 put_be32(s,data_size + 10 + 8);
271 put_be32(s, nb_packets); /* number of packets */
272 put_be32(s,0); /* next data header */
275 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
276 int length, int key_frame)
279 ByteIOContext *s = ctx->pb;
281 stream->nb_packets++;
282 stream->packet_total_size += length;
283 if (length > stream->packet_max_size)
284 stream->packet_max_size = length;
286 put_be16(s,0); /* version */
287 put_be16(s,length + 12);
288 put_be16(s, stream->num); /* stream number */
289 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
290 put_be32(s, timestamp); /* timestamp */
291 put_byte(s, 0); /* reserved */
292 put_byte(s, key_frame ? 2 : 0); /* flags */
295 static int rm_write_header(AVFormatContext *s)
297 RMMuxContext *rm = s->priv_data;
300 AVCodecContext *codec;
302 for(n=0;n<s->nb_streams;n++) {
303 s->streams[n]->id = n;
304 codec = s->streams[n]->codec;
305 stream = &rm->streams[n];
306 memset(stream, 0, sizeof(StreamInfo));
308 stream->bit_rate = codec->bit_rate;
311 switch(codec->codec_type) {
312 case CODEC_TYPE_AUDIO:
313 rm->audio_stream = stream;
314 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
315 /* XXX: dummy values */
316 stream->packet_max_size = 1024;
317 stream->nb_packets = 0;
318 stream->total_frames = stream->nb_packets;
320 case CODEC_TYPE_VIDEO:
321 rm->video_stream = stream;
322 stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
323 /* XXX: dummy values */
324 stream->packet_max_size = 4096;
325 stream->nb_packets = 0;
326 stream->total_frames = stream->nb_packets;
333 rv10_write_header(s, 0, 0);
334 put_flush_packet(s->pb);
338 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
341 RMMuxContext *rm = s->priv_data;
342 ByteIOContext *pb = s->pb;
343 StreamInfo *stream = rm->audio_stream;
346 /* XXX: suppress this malloc */
347 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
349 write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
351 /* for AC-3, the words seem to be reversed */
352 for(i=0;i<size;i+=2) {
356 put_buffer(pb, buf1, size);
357 put_flush_packet(pb);
363 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
365 RMMuxContext *rm = s->priv_data;
366 ByteIOContext *pb = s->pb;
367 StreamInfo *stream = rm->video_stream;
368 int key_frame = !!(flags & PKT_FLAG_KEY);
370 /* XXX: this is incorrect: should be a parameter */
372 /* Well, I spent some time finding the meaning of these bits. I am
373 not sure I understood everything, but it works !! */
375 write_packet_header(s, stream, size + 7 + (size >= 0x4000)*4, key_frame);
376 /* bit 7: '1' if final packet of a frame converted in several packets */
378 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
379 frame starting from 1 */
386 put_be32(pb, size); /* total frame size */
387 put_be32(pb, size); /* offset from the start or the end */
389 put_be16(pb, 0x4000 | size); /* total frame size */
390 put_be16(pb, 0x4000 | size); /* offset from the start or the end */
394 write_packet_header(s, size + 6);
396 put_be16(pb, 0x4000 + size); /* total frame size */
397 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
399 put_byte(pb, stream->nb_frames & 0xff);
401 put_buffer(pb, buf, size);
402 put_flush_packet(pb);
408 static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
410 if (s->streams[pkt->stream_index]->codec->codec_type ==
412 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
414 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
417 static int rm_write_trailer(AVFormatContext *s)
419 RMMuxContext *rm = s->priv_data;
420 int data_size, index_pos, i;
421 ByteIOContext *pb = s->pb;
423 if (!url_is_streamed(s->pb)) {
424 /* end of file: finish to write header */
425 index_pos = url_fseek(pb, 0, SEEK_CUR);
426 data_size = index_pos - rm->data_pos;
428 /* FIXME: write index */
430 /* undocumented end header */
434 url_fseek(pb, 0, SEEK_SET);
435 for(i=0;i<s->nb_streams;i++)
436 rm->streams[i].total_frames = rm->streams[i].nb_frames;
437 rv10_write_header(s, data_size, 0);
439 /* undocumented end header */
443 put_flush_packet(pb);
448 AVOutputFormat rm_muxer = {
450 NULL_IF_CONFIG_SMALL("RealMedia format"),
451 "application/vnd.rn-realmedia",
453 sizeof(RMMuxContext),