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