ad2c17d529c4f443e0f3b945a739fc3aacf21dcc
[libav.git] / libavformat / rmenc.c
1 /*
2 * "Real" compatible muxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #include "avformat.h"
22 #include "avio_internal.h"
23 #include "rm.h"
24 #include "libavutil/dict.h"
25
26 typedef struct StreamInfo {
27 int nb_packets;
28 int packet_total_size;
29 int packet_max_size;
30 /* codec related output */
31 int bit_rate;
32 float frame_rate;
33 int nb_frames; /* current frame number */
34 int total_frames; /* total number of frames */
35 int num;
36 AVCodecContext *enc;
37 } StreamInfo;
38
39 typedef struct RMMuxContext {
40 StreamInfo streams[2];
41 StreamInfo *audio_stream, *video_stream;
42 int data_pos; /* position of the data after the header */
43 } RMMuxContext;
44
45 /* in ms */
46 #define BUFFER_DURATION 0
47 /* the header needs at most 7 + 4 + 12 B */
48 #define MAX_HEADER_SIZE (7 + 4 + 12)
49 /* UINT16_MAX is the maximal chunk size */
50 #define MAX_PACKET_SIZE (UINT16_MAX - MAX_HEADER_SIZE)
51
52
53 static void put_str(AVIOContext *s, const char *tag)
54 {
55 avio_wb16(s,strlen(tag));
56 while (*tag) {
57 avio_w8(s, *tag++);
58 }
59 }
60
61 static void put_str8(AVIOContext *s, const char *tag)
62 {
63 avio_w8(s, strlen(tag));
64 while (*tag) {
65 avio_w8(s, *tag++);
66 }
67 }
68
69 static int rv10_write_header(AVFormatContext *ctx,
70 int data_size, int index_pos)
71 {
72 RMMuxContext *rm = ctx->priv_data;
73 AVIOContext *s = ctx->pb;
74 StreamInfo *stream;
75 unsigned char *data_offset_ptr, *start_ptr;
76 const char *desc, *mimetype;
77 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
78 int bit_rate, v, duration, flags, data_pos;
79 AVDictionaryEntry *tag;
80
81 start_ptr = s->buf_ptr;
82
83 ffio_wfourcc(s, ".RMF");
84 avio_wb32(s,18); /* header size */
85 avio_wb16(s,0);
86 avio_wb32(s,0);
87 avio_wb32(s,4 + ctx->nb_streams); /* num headers */
88
89 ffio_wfourcc(s,"PROP");
90 avio_wb32(s, 50);
91 avio_wb16(s, 0);
92 packet_max_size = 0;
93 packet_total_size = 0;
94 nb_packets = 0;
95 bit_rate = 0;
96 duration = 0;
97 for(i=0;i<ctx->nb_streams;i++) {
98 StreamInfo *stream = &rm->streams[i];
99 bit_rate += stream->bit_rate;
100 if (stream->packet_max_size > packet_max_size)
101 packet_max_size = stream->packet_max_size;
102 nb_packets += stream->nb_packets;
103 packet_total_size += stream->packet_total_size;
104 /* select maximum duration */
105 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
106 if (v > duration)
107 duration = v;
108 }
109 avio_wb32(s, bit_rate); /* max bit rate */
110 avio_wb32(s, bit_rate); /* avg bit rate */
111 avio_wb32(s, packet_max_size); /* max packet size */
112 if (nb_packets > 0)
113 packet_avg_size = packet_total_size / nb_packets;
114 else
115 packet_avg_size = 0;
116 avio_wb32(s, packet_avg_size); /* avg packet size */
117 avio_wb32(s, nb_packets); /* num packets */
118 avio_wb32(s, duration); /* duration */
119 avio_wb32(s, BUFFER_DURATION); /* preroll */
120 avio_wb32(s, index_pos); /* index offset */
121 /* computation of data the data offset */
122 data_offset_ptr = s->buf_ptr;
123 avio_wb32(s, 0); /* data offset : will be patched after */
124 avio_wb16(s, ctx->nb_streams); /* num streams */
125 flags = 1 | 2; /* save allowed & perfect play */
126 if (!s->seekable)
127 flags |= 4; /* live broadcast */
128 avio_wb16(s, flags);
129
130 /* comments */
131
132 ffio_wfourcc(s,"CONT");
133 size = 4 * 2 + 10;
134 for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
135 tag = av_dict_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
136 if(tag) size += strlen(tag->value);
137 }
138 avio_wb32(s,size);
139 avio_wb16(s,0);
140 for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
141 tag = av_dict_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
142 put_str(s, tag ? tag->value : "");
143 }
144
145 for(i=0;i<ctx->nb_streams;i++) {
146 int codec_data_size;
147
148 stream = &rm->streams[i];
149
150 if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
151 desc = "The Audio Stream";
152 mimetype = "audio/x-pn-realaudio";
153 codec_data_size = 73;
154 } else {
155 desc = "The Video Stream";
156 mimetype = "video/x-pn-realvideo";
157 codec_data_size = 34;
158 }
159
160 ffio_wfourcc(s,"MDPR");
161 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
162 avio_wb32(s, size);
163 avio_wb16(s, 0);
164
165 avio_wb16(s, i); /* stream number */
166 avio_wb32(s, stream->bit_rate); /* max bit rate */
167 avio_wb32(s, stream->bit_rate); /* avg bit rate */
168 avio_wb32(s, stream->packet_max_size); /* max packet size */
169 if (stream->nb_packets > 0)
170 packet_avg_size = stream->packet_total_size /
171 stream->nb_packets;
172 else
173 packet_avg_size = 0;
174 avio_wb32(s, packet_avg_size); /* avg packet size */
175 avio_wb32(s, 0); /* start time */
176 avio_wb32(s, BUFFER_DURATION); /* preroll */
177 /* duration */
178 if (!s->seekable || !stream->total_frames)
179 avio_wb32(s, (int)(3600 * 1000));
180 else
181 avio_wb32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
182 put_str8(s, desc);
183 put_str8(s, mimetype);
184 avio_wb32(s, codec_data_size);
185
186 if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
187 int coded_frame_size, fscode, sample_rate;
188 sample_rate = stream->enc->sample_rate;
189 coded_frame_size = (stream->enc->bit_rate *
190 stream->enc->frame_size) / (8 * sample_rate);
191 /* audio codec info */
192 avio_write(s, ".ra", 3);
193 avio_w8(s, 0xfd);
194 avio_wb32(s, 0x00040000); /* version */
195 ffio_wfourcc(s, ".ra4");
196 avio_wb32(s, 0x01b53530); /* stream length */
197 avio_wb16(s, 4); /* unknown */
198 avio_wb32(s, 0x39); /* header size */
199
200 switch(sample_rate) {
201 case 48000:
202 case 24000:
203 case 12000:
204 fscode = 1;
205 break;
206 default:
207 case 44100:
208 case 22050:
209 case 11025:
210 fscode = 2;
211 break;
212 case 32000:
213 case 16000:
214 case 8000:
215 fscode = 3;
216 }
217 avio_wb16(s, fscode); /* codec additional info, for AC-3, seems
218 to be a frequency code */
219 /* special hack to compensate rounding errors... */
220 if (coded_frame_size == 557)
221 coded_frame_size--;
222 avio_wb32(s, coded_frame_size); /* frame length */
223 avio_wb32(s, 0x51540); /* unknown */
224 avio_wb32(s, 0x249f0); /* unknown */
225 avio_wb32(s, 0x249f0); /* unknown */
226 avio_wb16(s, 0x01);
227 /* frame length : seems to be very important */
228 avio_wb16(s, coded_frame_size);
229 avio_wb32(s, 0); /* unknown */
230 avio_wb16(s, stream->enc->sample_rate); /* sample rate */
231 avio_wb32(s, 0x10); /* unknown */
232 avio_wb16(s, stream->enc->channels);
233 put_str8(s, "Int0"); /* codec name */
234 if (stream->enc->codec_tag) {
235 avio_w8(s, 4); /* tag length */
236 avio_wl32(s, stream->enc->codec_tag);
237 } else {
238 av_log(ctx, AV_LOG_ERROR, "Invalid codec tag\n");
239 return -1;
240 }
241 avio_wb16(s, 0); /* title length */
242 avio_wb16(s, 0); /* author length */
243 avio_wb16(s, 0); /* copyright length */
244 avio_w8(s, 0); /* end of header */
245 } else {
246 /* video codec info */
247 avio_wb32(s,34); /* size */
248 ffio_wfourcc(s, "VIDO");
249 if(stream->enc->codec_id == AV_CODEC_ID_RV10)
250 ffio_wfourcc(s,"RV10");
251 else
252 ffio_wfourcc(s,"RV20");
253 avio_wb16(s, stream->enc->width);
254 avio_wb16(s, stream->enc->height);
255 avio_wb16(s, (int) stream->frame_rate); /* frames per seconds ? */
256 avio_wb32(s,0); /* unknown meaning */
257 avio_wb16(s, (int) stream->frame_rate); /* unknown meaning */
258 avio_wb32(s,0); /* unknown meaning */
259 avio_wb16(s, 8); /* unknown meaning */
260 /* Seems to be the codec version: only use basic H263. The next
261 versions seems to add a diffential DC coding as in
262 MPEG... nothing new under the sun */
263 if(stream->enc->codec_id == AV_CODEC_ID_RV10)
264 avio_wb32(s,0x10000000);
265 else
266 avio_wb32(s,0x20103001);
267 //avio_wb32(s,0x10003000);
268 }
269 }
270
271 /* patch data offset field */
272 data_pos = s->buf_ptr - start_ptr;
273 rm->data_pos = data_pos;
274 data_offset_ptr[0] = data_pos >> 24;
275 data_offset_ptr[1] = data_pos >> 16;
276 data_offset_ptr[2] = data_pos >> 8;
277 data_offset_ptr[3] = data_pos;
278
279 /* data stream */
280 ffio_wfourcc(s, "DATA");
281 avio_wb32(s,data_size + 10 + 8);
282 avio_wb16(s,0);
283
284 avio_wb32(s, nb_packets); /* number of packets */
285 avio_wb32(s,0); /* next data header */
286 return 0;
287 }
288
289 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
290 int length, int key_frame)
291 {
292 int timestamp;
293 AVIOContext *s = ctx->pb;
294
295 stream->nb_packets++;
296 stream->packet_total_size += length;
297 if (length > stream->packet_max_size)
298 stream->packet_max_size = length;
299
300 avio_wb16(s,0); /* version */
301 avio_wb16(s,length + 12);
302 avio_wb16(s, stream->num); /* stream number */
303 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
304 avio_wb32(s, timestamp); /* timestamp */
305 avio_w8(s, 0); /* reserved */
306 avio_w8(s, key_frame ? 2 : 0); /* flags */
307 }
308
309 static int rm_write_header(AVFormatContext *s)
310 {
311 RMMuxContext *rm = s->priv_data;
312 StreamInfo *stream;
313 int n;
314 AVCodecContext *codec;
315
316 for(n=0;n<s->nb_streams;n++) {
317 AVStream *st = s->streams[n];
318
319 s->streams[n]->id = n;
320 codec = s->streams[n]->codec;
321 stream = &rm->streams[n];
322 memset(stream, 0, sizeof(StreamInfo));
323 stream->num = n;
324 stream->bit_rate = codec->bit_rate;
325 stream->enc = codec;
326
327 switch(codec->codec_type) {
328 case AVMEDIA_TYPE_AUDIO:
329 rm->audio_stream = stream;
330 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
331 /* XXX: dummy values */
332 stream->packet_max_size = 1024;
333 stream->nb_packets = 0;
334 stream->total_frames = stream->nb_packets;
335 break;
336 case AVMEDIA_TYPE_VIDEO:
337 rm->video_stream = stream;
338 // TODO: should be avg_frame_rate
339 stream->frame_rate = (float)st->time_base.den / (float)st->time_base.num;
340 /* XXX: dummy values */
341 stream->packet_max_size = 4096;
342 stream->nb_packets = 0;
343 stream->total_frames = stream->nb_packets;
344 break;
345 default:
346 return -1;
347 }
348 }
349
350 if (rv10_write_header(s, 0, 0))
351 return AVERROR_INVALIDDATA;
352 avio_flush(s->pb);
353 return 0;
354 }
355
356 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
357 {
358 RMMuxContext *rm = s->priv_data;
359 AVIOContext *pb = s->pb;
360 StreamInfo *stream = rm->audio_stream;
361 int i;
362
363 write_packet_header(s, stream, size, !!(flags & AV_PKT_FLAG_KEY));
364
365 if (stream->enc->codec_id == AV_CODEC_ID_AC3) {
366 /* for AC-3, the words seem to be reversed */
367 for (i = 0; i < size; i += 2) {
368 avio_w8(pb, buf[i + 1]);
369 avio_w8(pb, buf[i]);
370 }
371 } else {
372 avio_write(pb, buf, size);
373 }
374 stream->nb_frames++;
375 return 0;
376 }
377
378 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
379 {
380 RMMuxContext *rm = s->priv_data;
381 AVIOContext *pb = s->pb;
382 StreamInfo *stream = rm->video_stream;
383 int key_frame = !!(flags & AV_PKT_FLAG_KEY);
384
385 /* XXX: this is incorrect: should be a parameter */
386
387 /* Well, I spent some time finding the meaning of these bits. I am
388 not sure I understood everything, but it works !! */
389 #if 1
390 if (size > MAX_PACKET_SIZE) {
391 avpriv_report_missing_feature(s, "Muxing packets larger than 64 kB");
392 return AVERROR(ENOSYS);
393 }
394 write_packet_header(s, stream, size + 7 + (size >= 0x4000)*4, key_frame);
395 /* bit 7: '1' if final packet of a frame converted in several packets */
396 avio_w8(pb, 0x81);
397 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
398 frame starting from 1 */
399 if (key_frame) {
400 avio_w8(pb, 0x81);
401 } else {
402 avio_w8(pb, 0x01);
403 }
404 if(size >= 0x4000){
405 avio_wb32(pb, size); /* total frame size */
406 avio_wb32(pb, size); /* offset from the start or the end */
407 }else{
408 avio_wb16(pb, 0x4000 | size); /* total frame size */
409 avio_wb16(pb, 0x4000 | size); /* offset from the start or the end */
410 }
411 #else
412 /* full frame */
413 write_packet_header(s, size + 6);
414 avio_w8(pb, 0xc0);
415 avio_wb16(pb, 0x4000 + size); /* total frame size */
416 avio_wb16(pb, 0x4000 + packet_number * 126); /* position in stream */
417 #endif
418 avio_w8(pb, stream->nb_frames & 0xff);
419
420 avio_write(pb, buf, size);
421
422 stream->nb_frames++;
423 return 0;
424 }
425
426 static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
427 {
428 if (s->streams[pkt->stream_index]->codec->codec_type ==
429 AVMEDIA_TYPE_AUDIO)
430 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
431 else
432 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
433 }
434
435 static int rm_write_trailer(AVFormatContext *s)
436 {
437 RMMuxContext *rm = s->priv_data;
438 int data_size, index_pos, i;
439 AVIOContext *pb = s->pb;
440
441 if (s->pb->seekable) {
442 /* end of file: finish to write header */
443 index_pos = avio_tell(pb);
444 data_size = index_pos - rm->data_pos;
445
446 /* FIXME: write index */
447
448 /* undocumented end header */
449 avio_wb32(pb, 0);
450 avio_wb32(pb, 0);
451
452 avio_seek(pb, 0, SEEK_SET);
453 for(i=0;i<s->nb_streams;i++)
454 rm->streams[i].total_frames = rm->streams[i].nb_frames;
455 rv10_write_header(s, data_size, 0);
456 } else {
457 /* undocumented end header */
458 avio_wb32(pb, 0);
459 avio_wb32(pb, 0);
460 }
461
462 return 0;
463 }
464
465
466 AVOutputFormat ff_rm_muxer = {
467 .name = "rm",
468 .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
469 .mime_type = "application/vnd.rn-realmedia",
470 .extensions = "rm,ra",
471 .priv_data_size = sizeof(RMMuxContext),
472 .audio_codec = AV_CODEC_ID_AC3,
473 .video_codec = AV_CODEC_ID_RV10,
474 .write_header = rm_write_header,
475 .write_packet = rm_write_packet,
476 .write_trailer = rm_write_trailer,
477 .codec_tag = (const AVCodecTag* const []){ ff_rm_codec_tags, 0 },
478 };