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 */
25
30 /* codec related output */
38
42 int data_pos;
/* position of the data after the header */
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
54 {
56 while (*tag) {
58 }
59 }
60
62 {
64 while (*tag) {
66 }
67 }
68
70 int data_size, int index_pos)
71 {
75 const char *
desc, *mimetype;
76 int nb_packets, packet_total_size, packet_max_size,
size, packet_avg_size, i;
78 int data_offset;
80
86
90 packet_max_size = 0;
91 packet_total_size = 0;
92 nb_packets = 0;
93 bit_rate = 0;
94 duration = 0;
102 /* select maximum duration */
104 if (v > duration)
105 duration = v;
106 }
109 avio_wb32(
s, packet_max_size);
/* max packet size */
110 if (nb_packets > 0)
111 packet_avg_size = packet_total_size / nb_packets;
112 else
113 packet_avg_size = 0;
114 avio_wb32(
s, packet_avg_size);
/* avg packet size */
119 /* computation of data the data offset */
121 avio_wb32(
s, 0);
/* data offset : will be patched after */
123 flags = 1 | 2;
/* save allowed & perfect play */
125 flags |= 4;
/* live broadcast */
127
128 /* comments */
129
135 }
141 }
142
144 int codec_data_size;
145
146 stream = &rm->streams[i];
147
149 desc =
"The Audio Stream";
150 mimetype = "audio/x-pn-realaudio";
151 codec_data_size = 73;
152 } else {
153 desc =
"The Video Stream";
154 mimetype = "video/x-pn-realvideo";
155 codec_data_size = 34;
156 }
157
159 size = 10 + 9 * 4 + strlen(
desc) + strlen(mimetype) + codec_data_size;
162
164 avio_wb32(
s, stream->bit_rate);
/* max bit rate */
165 avio_wb32(
s, stream->bit_rate);
/* avg bit rate */
166 avio_wb32(
s, stream->packet_max_size);
/* max packet size */
167 if (stream->nb_packets > 0)
168 packet_avg_size = stream->packet_total_size /
169 stream->nb_packets;
170 else
171 packet_avg_size = 0;
172 avio_wb32(
s, packet_avg_size);
/* avg packet size */
175 /* duration */
178 else
183
187 sample_rate = stream->par->sample_rate;
188 coded_frame_size = (stream->par->bit_rate *
190 /* audio codec info */
198
199 switch(sample_rate) {
200 case 48000:
201 case 24000:
202 case 12000:
203 fscode = 1;
204 break;
205 default:
206 case 44100:
207 case 22050:
208 case 11025:
209 fscode = 2;
210 break;
211 case 32000:
212 case 16000:
213 case 8000:
214 fscode = 3;
215 }
216 avio_wb16(
s, fscode);
/* codec additional info, for AC-3, seems
217 to be a frequency code */
218 /* special hack to compensate rounding errors... */
219 if (coded_frame_size == 557)
220 coded_frame_size--;
221 avio_wb32(
s, coded_frame_size);
/* frame length */
223 avio_wb32(
s, stream->par->bit_rate / 8 * 60);
/* bytes per minute */
224 avio_wb32(
s, stream->par->bit_rate / 8 * 60);
/* bytes per minute */
226 /* frame length : seems to be very important */
229 avio_wb16(
s, stream->par->sample_rate);
/* sample rate */
233 if (stream->par->codec_tag) {
236 } else {
238 return -1;
239 }
244 } else {
245 /* video codec info */
250 else
254
255 if (stream->frame_rate.num / stream->frame_rate.den > 65535) {
256 av_log(
s,
AV_LOG_ERROR,
"Frame rate %d is too high\n", stream->frame_rate.num / stream->frame_rate.den);
258 }
259
260 avio_wb16(
s, stream->frame_rate.num / stream->frame_rate.den);
/* frames per seconds ? */
262 avio_wb16(
s, stream->frame_rate.num / stream->frame_rate.den);
/* unknown meaning */
265 /* Seems to be the codec version: only use basic H.263. The next
266 versions seems to add a differential DC coding as in
267 MPEG... nothing new under the sun. */
270 else
272 //avio_wb32(s,0x10003000);
273 }
274 }
275
276 /* patch data offset field */
278 if (
avio_seek(
s, data_offset, SEEK_SET) >= 0) {
281 }
282
283 /* data stream */
287
288 avio_wb32(
s, nb_packets);
/* number of packets */
290 return 0;
291 }
292
294 int length,
int key_frame)
295 {
296 int timestamp;
298
303
310 avio_w8(
s, key_frame ? 2 : 0);
/* flags */
311 }
312
314 {
319
321 av_log(s,
AV_LOG_ERROR,
"At most 2 streams are currently supported for muxing in RM\n");
323 }
324
328
336
342 /* XXX: dummy values */
346 break;
349 // TODO: should be avg_frame_rate
351 /* XXX: dummy values */
355 break;
356 default:
357 return -1;
358 }
359 }
360
364 return 0;
365 }
366
368 {
372 int i;
373
375
377 /* for AC-3, the words seem to be reversed */
378 for (i = 0; i <
size; i += 2) {
381 }
382 } else {
384 }
386 return 0;
387 }
388
390 {
395
396 /* XXX: this is incorrect: should be a parameter */
397
398 /* Well, I spent some time finding the meaning of these bits. I am
399 not sure I understood everything, but it works !! */
401 av_log(s,
AV_LOG_ERROR,
"Muxing packets larger than 64 kB (%d) is not supported\n", size);
403 }
405 /* bit 7: '1' if final packet of a frame converted in several packets */
407 /* bit 7: '1' if I-frame. bits 6..0 : sequence number in current
408 frame starting from 1 */
409 if (key_frame) {
411 } else {
413 }
414 if(size >= 0x4000){
415 avio_wb32(pb, size);
/* total frame size */
416 avio_wb32(pb, size);
/* offset from the start or the end */
417 }else{
418 avio_wb16(pb, 0x4000 | size);
/* total frame size */
419 avio_wb16(pb, 0x4000 | size);
/* offset from the start or the end */
420 }
422
424
426 return 0;
427 }
428
430 {
434 else
436 }
437
439 {
441 int data_size, index_pos, i;
443
445 /* end of file: finish to write header */
447 data_size = index_pos - rm->
data_pos;
448
449 /* FIXME: write index */
450
451 /* undocumented end header */
454
459 } else {
460 /* undocumented end header */
463 }
464
465 return 0;
466 }
467
468
472 .mime_type = "application/vnd.rn-realmedia",
473 .extensions = "rm,ra",
481 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
This struct describes the properties of an encoded stream.
void avio_wl32(AVIOContext *s, unsigned int val)
int id
Format-specific stream ID.
AVStream ** streams
A list of all streams in the file.
static void put_str(AVIOContext *s, const char *tag)
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
static void put_str8(AVIOContext *s, const char *tag)
StreamInfo * audio_stream
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
enum AVMediaType codec_type
General type of the encoded data.
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
int flags
A combination of AV_PKT_FLAG values.
static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream, int length, int key_frame)
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
static int write_trailer(AVFormatContext *s1)
static int rm_write_header(AVFormatContext *s)
static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
#define FF_ARRAY_ELEMS(a)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
const AVCodecTag ff_rm_codec_tags[]
AVIOContext * pb
I/O context.
void avio_w8(AVIOContext *s, int b)
Rational number (pair of numerator and denominator).
static int rm_write_trailer(AVFormatContext *s)
void avio_wb16(AVIOContext *s, unsigned int val)
const char *const ff_rm_metadata[4]
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
int sample_rate
Audio only.
AVOutputFormat ff_rm_muxer
static int rv10_write_header(AVFormatContext *ctx, int data_size, int index_pos)
void * priv_data
Format private data.
static void write_header(FFV1Context *f)
void avio_wb32(AVIOContext *s, unsigned int val)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
This structure stores compressed data.
StreamInfo * video_stream