rmenc.c
Go to the documentation of this file.
1 /*
2  * "Real" compatible muxer.
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avformat.h"
22 #include "avio_internal.h"
23 #include "rm.h"
24 #include "libavutil/dict.h"
25 
26 typedef struct {
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;
37 } StreamInfo;
38 
39 typedef struct {
40  StreamInfo streams[2];
42  int data_pos; /* position of the data after the header */
43 } RMMuxContext;
44 
45 /* in ms */
46 #define BUFFER_DURATION 0
47 
48 
49 static void put_str(AVIOContext *s, const char *tag)
50 {
51  avio_wb16(s,strlen(tag));
52  while (*tag) {
53  avio_w8(s, *tag++);
54  }
55 }
56 
57 static void put_str8(AVIOContext *s, const char *tag)
58 {
59  avio_w8(s, strlen(tag));
60  while (*tag) {
61  avio_w8(s, *tag++);
62  }
63 }
64 
66  int data_size, int index_pos)
67 {
68  RMMuxContext *rm = ctx->priv_data;
69  AVIOContext *s = ctx->pb;
70  StreamInfo *stream;
71  unsigned char *data_offset_ptr, *start_ptr;
72  const char *desc, *mimetype;
73  int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
74  int bit_rate, v, duration, flags, data_pos;
76 
77  start_ptr = s->buf_ptr;
78 
79  ffio_wfourcc(s, ".RMF");
80  avio_wb32(s,18); /* header size */
81  avio_wb16(s,0);
82  avio_wb32(s,0);
83  avio_wb32(s,4 + ctx->nb_streams); /* num headers */
84 
85  ffio_wfourcc(s,"PROP");
86  avio_wb32(s, 50);
87  avio_wb16(s, 0);
88  packet_max_size = 0;
89  packet_total_size = 0;
90  nb_packets = 0;
91  bit_rate = 0;
92  duration = 0;
93  for(i=0;i<ctx->nb_streams;i++) {
94  StreamInfo *stream = &rm->streams[i];
95  bit_rate += stream->bit_rate;
96  if (stream->packet_max_size > packet_max_size)
97  packet_max_size = stream->packet_max_size;
98  nb_packets += stream->nb_packets;
99  packet_total_size += stream->packet_total_size;
100  /* select maximum duration */
101  v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
102  if (v > duration)
103  duration = v;
104  }
105  avio_wb32(s, bit_rate); /* max bit rate */
106  avio_wb32(s, bit_rate); /* avg bit rate */
107  avio_wb32(s, packet_max_size); /* max packet size */
108  if (nb_packets > 0)
109  packet_avg_size = packet_total_size / nb_packets;
110  else
111  packet_avg_size = 0;
112  avio_wb32(s, packet_avg_size); /* avg packet size */
113  avio_wb32(s, nb_packets); /* num packets */
114  avio_wb32(s, duration); /* duration */
115  avio_wb32(s, BUFFER_DURATION); /* preroll */
116  avio_wb32(s, index_pos); /* index offset */
117  /* computation of data the data offset */
118  data_offset_ptr = s->buf_ptr;
119  avio_wb32(s, 0); /* data offset : will be patched after */
120  avio_wb16(s, ctx->nb_streams); /* num streams */
121  flags = 1 | 2; /* save allowed & perfect play */
122  if (!s->seekable)
123  flags |= 4; /* live broadcast */
124  avio_wb16(s, flags);
125 
126  /* comments */
127 
128  ffio_wfourcc(s,"CONT");
129  size = 4 * 2 + 10;
130  for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
131  tag = av_dict_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
132  if(tag) size += strlen(tag->value);
133  }
134  avio_wb32(s,size);
135  avio_wb16(s,0);
136  for(i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
137  tag = av_dict_get(ctx->metadata, ff_rm_metadata[i], NULL, 0);
138  put_str(s, tag ? tag->value : "");
139  }
140 
141  for(i=0;i<ctx->nb_streams;i++) {
142  int codec_data_size;
143 
144  stream = &rm->streams[i];
145 
146  if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
147  desc = "The Audio Stream";
148  mimetype = "audio/x-pn-realaudio";
149  codec_data_size = 73;
150  } else {
151  desc = "The Video Stream";
152  mimetype = "video/x-pn-realvideo";
153  codec_data_size = 34;
154  }
155 
156  ffio_wfourcc(s,"MDPR");
157  size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
158  avio_wb32(s, size);
159  avio_wb16(s, 0);
160 
161  avio_wb16(s, i); /* stream number */
162  avio_wb32(s, stream->bit_rate); /* max bit rate */
163  avio_wb32(s, stream->bit_rate); /* avg bit rate */
164  avio_wb32(s, stream->packet_max_size); /* max packet size */
165  if (stream->nb_packets > 0)
166  packet_avg_size = stream->packet_total_size /
167  stream->nb_packets;
168  else
169  packet_avg_size = 0;
170  avio_wb32(s, packet_avg_size); /* avg packet size */
171  avio_wb32(s, 0); /* start time */
172  avio_wb32(s, BUFFER_DURATION); /* preroll */
173  /* duration */
174  if (!s->seekable || !stream->total_frames)
175  avio_wb32(s, (int)(3600 * 1000));
176  else
177  avio_wb32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
178  put_str8(s, desc);
179  put_str8(s, mimetype);
180  avio_wb32(s, codec_data_size);
181 
182  if (stream->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
183  int coded_frame_size, fscode, sample_rate;
184  sample_rate = stream->enc->sample_rate;
185  coded_frame_size = (stream->enc->bit_rate *
186  stream->enc->frame_size) / (8 * sample_rate);
187  /* audio codec info */
188  avio_write(s, ".ra", 3);
189  avio_w8(s, 0xfd);
190  avio_wb32(s, 0x00040000); /* version */
191  ffio_wfourcc(s, ".ra4");
192  avio_wb32(s, 0x01b53530); /* stream length */
193  avio_wb16(s, 4); /* unknown */
194  avio_wb32(s, 0x39); /* header size */
195 
196  switch(sample_rate) {
197  case 48000:
198  case 24000:
199  case 12000:
200  fscode = 1;
201  break;
202  default:
203  case 44100:
204  case 22050:
205  case 11025:
206  fscode = 2;
207  break;
208  case 32000:
209  case 16000:
210  case 8000:
211  fscode = 3;
212  }
213  avio_wb16(s, fscode); /* codec additional info, for AC-3, seems
214  to be a frequency code */
215  /* special hack to compensate rounding errors... */
216  if (coded_frame_size == 557)
217  coded_frame_size--;
218  avio_wb32(s, coded_frame_size); /* frame length */
219  avio_wb32(s, 0x51540); /* unknown */
220  avio_wb32(s, stream->enc->bit_rate / 8 * 60); /* bytes per minute */
221  avio_wb32(s, stream->enc->bit_rate / 8 * 60); /* bytes per minute */
222  avio_wb16(s, 0x01);
223  /* frame length : seems to be very important */
224  avio_wb16(s, coded_frame_size);
225  avio_wb32(s, 0); /* unknown */
226  avio_wb16(s, stream->enc->sample_rate); /* sample rate */
227  avio_wb32(s, 0x10); /* unknown */
228  avio_wb16(s, stream->enc->channels);
229  put_str8(s, "Int0"); /* codec name */
230  if (stream->enc->codec_tag) {
231  avio_w8(s, 4); /* tag length */
232  avio_wl32(s, stream->enc->codec_tag);
233  } else {
234  av_log(ctx, AV_LOG_ERROR, "Invalid codec tag\n");
235  return -1;
236  }
237  avio_wb16(s, 0); /* title length */
238  avio_wb16(s, 0); /* author length */
239  avio_wb16(s, 0); /* copyright length */
240  avio_w8(s, 0); /* end of header */
241  } else {
242  /* video codec info */
243  avio_wb32(s,34); /* size */
244  ffio_wfourcc(s, "VIDO");
245  if(stream->enc->codec_id == AV_CODEC_ID_RV10)
246  ffio_wfourcc(s,"RV10");
247  else
248  ffio_wfourcc(s,"RV20");
249  avio_wb16(s, stream->enc->width);
250  avio_wb16(s, stream->enc->height);
251  avio_wb16(s, (int) stream->frame_rate); /* frames per seconds ? */
252  avio_wb32(s,0); /* unknown meaning */
253  avio_wb16(s, (int) stream->frame_rate); /* unknown meaning */
254  avio_wb32(s,0); /* unknown meaning */
255  avio_wb16(s, 8); /* unknown meaning */
256  /* Seems to be the codec version: only use basic H263. The next
257  versions seems to add a diffential DC coding as in
258  MPEG... nothing new under the sun */
259  if(stream->enc->codec_id == AV_CODEC_ID_RV10)
260  avio_wb32(s,0x10000000);
261  else
262  avio_wb32(s,0x20103001);
263  //avio_wb32(s,0x10003000);
264  }
265  }
266 
267  /* patch data offset field */
268  data_pos = s->buf_ptr - start_ptr;
269  rm->data_pos = data_pos;
270  data_offset_ptr[0] = data_pos >> 24;
271  data_offset_ptr[1] = data_pos >> 16;
272  data_offset_ptr[2] = data_pos >> 8;
273  data_offset_ptr[3] = data_pos;
274 
275  /* data stream */
276  ffio_wfourcc(s, "DATA");
277  avio_wb32(s,data_size + 10 + 8);
278  avio_wb16(s,0);
279 
280  avio_wb32(s, nb_packets); /* number of packets */
281  avio_wb32(s,0); /* next data header */
282  return 0;
283 }
284 
286  int length, int key_frame)
287 {
288  int timestamp;
289  AVIOContext *s = ctx->pb;
290 
291  stream->nb_packets++;
292  stream->packet_total_size += length;
293  if (length > stream->packet_max_size)
294  stream->packet_max_size = length;
295 
296  avio_wb16(s,0); /* version */
297  avio_wb16(s,length + 12);
298  avio_wb16(s, stream->num); /* stream number */
299  timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
300  avio_wb32(s, timestamp); /* timestamp */
301  avio_w8(s, 0); /* reserved */
302  avio_w8(s, key_frame ? 2 : 0); /* flags */
303 }
304 
306 {
307  RMMuxContext *rm = s->priv_data;
308  StreamInfo *stream;
309  int n;
310  AVCodecContext *codec;
311 
312  if (s->nb_streams > 2) {
313  av_log(s, AV_LOG_ERROR, "At most 2 streams are currently supported for muxing in RM\n");
314  return AVERROR_PATCHWELCOME;
315  }
316 
317  for(n=0;n<s->nb_streams;n++) {
318  s->streams[n]->id = n;
319  codec = s->streams[n]->codec;
320  stream = &rm->streams[n];
321  memset(stream, 0, sizeof(StreamInfo));
322  stream->num = n;
323  stream->bit_rate = codec->bit_rate;
324  stream->enc = codec;
325 
326  switch(codec->codec_type) {
327  case AVMEDIA_TYPE_AUDIO:
328  rm->audio_stream = stream;
329  stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
330  /* XXX: dummy values */
331  stream->packet_max_size = 1024;
332  stream->nb_packets = 0;
333  stream->total_frames = stream->nb_packets;
334  break;
335  case AVMEDIA_TYPE_VIDEO:
336  rm->video_stream = stream;
337  stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
338  /* XXX: dummy values */
339  stream->packet_max_size = 4096;
340  stream->nb_packets = 0;
341  stream->total_frames = stream->nb_packets;
342  break;
343  default:
344  return -1;
345  }
346  }
347 
348  if (rv10_write_header(s, 0, 0))
349  return AVERROR_INVALIDDATA;
350  avio_flush(s->pb);
351  return 0;
352 }
353 
354 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
355 {
356  uint8_t *buf1;
357  RMMuxContext *rm = s->priv_data;
358  AVIOContext *pb = s->pb;
359  StreamInfo *stream = rm->audio_stream;
360  int i;
361 
362  /* XXX: suppress this malloc */
363  buf1 = av_malloc(size * sizeof(uint8_t));
364 
365  write_packet_header(s, stream, size, !!(flags & AV_PKT_FLAG_KEY));
366 
367  if (stream->enc->codec_id == AV_CODEC_ID_AC3) {
368  /* for AC-3, the words seem to be reversed */
369  for(i=0;i<size;i+=2) {
370  buf1[i] = buf[i+1];
371  buf1[i+1] = buf[i];
372  }
373  avio_write(pb, buf1, size);
374  } else {
375  avio_write(pb, buf, size);
376  }
377  stream->nb_frames++;
378  av_free(buf1);
379  return 0;
380 }
381 
382 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
383 {
384  RMMuxContext *rm = s->priv_data;
385  AVIOContext *pb = s->pb;
386  StreamInfo *stream = rm->video_stream;
387  int key_frame = !!(flags & AV_PKT_FLAG_KEY);
388 
389  /* XXX: this is incorrect: should be a parameter */
390 
391  /* Well, I spent some time finding the meaning of these bits. I am
392  not sure I understood everything, but it works !! */
393 #if 1
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 
427 {
428  if (s->streams[pkt->stream_index]->codec->codec_type ==
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 
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 
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,
477  .codec_tag = (const AVCodecTag* const []){ ff_rm_codec_tags, 0 },
478 };
static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
Definition: rmenc.c:354
float v
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int nb_packets
Definition: rmenc.c:27
AVCodecContext * enc
Definition: rmenc.c:36
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:84
static int write_packet(AVFormatContext *s, AVPacket *pkt)
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define FF_ARRAY_ELEMS(a)
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Format I/O context.
Definition: avformat.h:944
Public dictionary API.
int packet_total_size
Definition: rmenc.c:28
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
int id
Format-specific stream ID.
Definition: avformat.h:650
AVStream ** streams
Definition: avformat.h:992
static void put_str(AVIOContext *s, const char *tag)
Definition: rmenc.c:49
uint8_t * data
uint32_t tag
Definition: movenc.c:894
static void put_str8(AVIOContext *s, const char *tag)
Definition: rmenc.c:57
StreamInfo * audio_stream
Definition: rmenc.c:41
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
static int64_t duration
Definition: ffplay.c:294
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:50
static int write_trailer(AVFormatContext *s)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int total_frames
Definition: rmenc.c:34
StreamInfo streams[2]
Definition: rmenc.c:40
AVDictionary * metadata
Definition: avformat.h:1092
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int nb_frames
Definition: rmenc.c:33
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
int flags
A combination of AV_PKT_FLAG values.
int bit_rate
Definition: rmenc.c:31
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream, int length, int key_frame)
Definition: rmenc.c:285
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
int width
picture width / height.
const char * name
Definition: avformat.h:378
static int rm_write_header(AVFormatContext *s)
Definition: rmenc.c:305
static AVStream * audio_stream
Definition: demuxing.c:39
float frame_rate
Definition: rmenc.c:32
static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rmenc.c:426
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int frame_size
Number of samples per channel in an audio frame.
const AVCodecTag ff_rm_codec_tags[]
Definition: rm.c:31
NULL
Definition: eval.c:55
sample_rate
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
void * buf
Definition: avisynth_c.h:594
#define BUFFER_DURATION
Definition: rmenc.c:46
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
static int rm_write_trailer(AVFormatContext *s)
Definition: rmenc.c:435
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:373
const char *const ff_rm_metadata[4]
Definition: rm.c:24
static int flags
Definition: cpu.c:23
Main libavformat public API header.
int packet_max_size
Definition: rmenc.c:29
int data_pos
Definition: rmenc.c:42
AVOutputFormat ff_rm_muxer
Definition: rmenc.c:466
static int rv10_write_header(AVFormatContext *ctx, int data_size, int index_pos)
Definition: rmenc.c:65
int den
denominator
Definition: rational.h:45
char * value
Definition: dict.h:82
int channels
number of audio channels
int num
Definition: rmenc.c:35
void * priv_data
Format private data.
Definition: avformat.h:964
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:299
const char int length
Definition: avisynth_c.h:668
static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
Definition: rmenc.c:382
This structure stores compressed data.
StreamInfo * video_stream
Definition: rmenc.c:41