808
|
1 /*
|
|
2 * "Real" compatible mux and demux.
|
|
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
|
|
23 /* in ms */
|
|
24 #define BUFFER_DURATION 0
|
|
25
|
|
26 typedef struct {
|
|
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 {
|
|
40 StreamInfo streams[2];
|
|
41 StreamInfo *audio_stream, *video_stream;
|
|
42 int data_pos; /* position of the data after the header */
|
|
43 int nb_packets;
|
|
44 int old_format;
|
|
45 int current_stream;
|
|
46 int remaining_len;
|
|
47 /// Audio descrambling matrix parameters
|
|
48 uint8_t *audiobuf; ///< place to store reordered audio data
|
|
49 int64_t audiotimestamp; ///< Audio packet timestamp
|
|
50 int sub_packet_cnt; // Subpacket counter, used while reading
|
|
51 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
|
|
52 int audio_stream_num; ///< Stream number for audio packets
|
|
53 int audio_pkt_cnt; ///< Output packet counter
|
|
54 int audio_framesize; /// Audio frame size from container
|
|
55 int sub_packet_lengths[16]; /// Length of each aac subpacket
|
|
56 } RMContext;
|
|
57
|
|
58 #ifdef CONFIG_MUXERS
|
|
59 static void put_str(ByteIOContext *s, const char *tag)
|
|
60 {
|
|
61 put_be16(s,strlen(tag));
|
|
62 while (*tag) {
|
|
63 put_byte(s, *tag++);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 static void put_str8(ByteIOContext *s, const char *tag)
|
|
68 {
|
|
69 put_byte(s, strlen(tag));
|
|
70 while (*tag) {
|
|
71 put_byte(s, *tag++);
|
|
72 }
|
|
73 }
|
|
74
|
|
75 static void rv10_write_header(AVFormatContext *ctx,
|
|
76 int data_size, int index_pos)
|
|
77 {
|
|
78 RMContext *rm = ctx->priv_data;
|
|
79 ByteIOContext *s = &ctx->pb;
|
|
80 StreamInfo *stream;
|
|
81 unsigned char *data_offset_ptr, *start_ptr;
|
|
82 const char *desc, *mimetype;
|
|
83 int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
|
|
84 int bit_rate, v, duration, flags, data_pos;
|
|
85
|
|
86 start_ptr = s->buf_ptr;
|
|
87
|
|
88 put_tag(s, ".RMF");
|
|
89 put_be32(s,18); /* header size */
|
|
90 put_be16(s,0);
|
|
91 put_be32(s,0);
|
|
92 put_be32(s,4 + ctx->nb_streams); /* num headers */
|
|
93
|
|
94 put_tag(s,"PROP");
|
|
95 put_be32(s, 50);
|
|
96 put_be16(s, 0);
|
|
97 packet_max_size = 0;
|
|
98 packet_total_size = 0;
|
|
99 nb_packets = 0;
|
|
100 bit_rate = 0;
|
|
101 duration = 0;
|
|
102 for(i=0;i<ctx->nb_streams;i++) {
|
|
103 StreamInfo *stream = &rm->streams[i];
|
|
104 bit_rate += stream->bit_rate;
|
|
105 if (stream->packet_max_size > packet_max_size)
|
|
106 packet_max_size = stream->packet_max_size;
|
|
107 nb_packets += stream->nb_packets;
|
|
108 packet_total_size += stream->packet_total_size;
|
|
109 /* select maximum duration */
|
|
110 v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
|
|
111 if (v > duration)
|
|
112 duration = v;
|
|
113 }
|
|
114 put_be32(s, bit_rate); /* max bit rate */
|
|
115 put_be32(s, bit_rate); /* avg bit rate */
|
|
116 put_be32(s, packet_max_size); /* max packet size */
|
|
117 if (nb_packets > 0)
|
|
118 packet_avg_size = packet_total_size / nb_packets;
|
|
119 else
|
|
120 packet_avg_size = 0;
|
|
121 put_be32(s, packet_avg_size); /* avg packet size */
|
|
122 put_be32(s, nb_packets); /* num packets */
|
|
123 put_be32(s, duration); /* duration */
|
|
124 put_be32(s, BUFFER_DURATION); /* preroll */
|
|
125 put_be32(s, index_pos); /* index offset */
|
|
126 /* computation of data the data offset */
|
|
127 data_offset_ptr = s->buf_ptr;
|
|
128 put_be32(s, 0); /* data offset : will be patched after */
|
|
129 put_be16(s, ctx->nb_streams); /* num streams */
|
|
130 flags = 1 | 2; /* save allowed & perfect play */
|
|
131 if (url_is_streamed(s))
|
|
132 flags |= 4; /* live broadcast */
|
|
133 put_be16(s, flags);
|
|
134
|
|
135 /* comments */
|
|
136
|
|
137 put_tag(s,"CONT");
|
|
138 size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
|
|
139 strlen(ctx->comment) + 4 * 2 + 10;
|
|
140 put_be32(s,size);
|
|
141 put_be16(s,0);
|
|
142 put_str(s, ctx->title);
|
|
143 put_str(s, ctx->author);
|
|
144 put_str(s, ctx->copyright);
|
|
145 put_str(s, ctx->comment);
|
|
146
|
|
147 for(i=0;i<ctx->nb_streams;i++) {
|
|
148 int codec_data_size;
|
|
149
|
|
150 stream = &rm->streams[i];
|
|
151
|
|
152 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
|
|
153 desc = "The Audio Stream";
|
|
154 mimetype = "audio/x-pn-realaudio";
|
|
155 codec_data_size = 73;
|
|
156 } else {
|
|
157 desc = "The Video Stream";
|
|
158 mimetype = "video/x-pn-realvideo";
|
|
159 codec_data_size = 34;
|
|
160 }
|
|
161
|
|
162 put_tag(s,"MDPR");
|
|
163 size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
|
|
164 put_be32(s, size);
|
|
165 put_be16(s, 0);
|
|
166
|
|
167 put_be16(s, i); /* stream number */
|
|
168 put_be32(s, stream->bit_rate); /* max bit rate */
|
|
169 put_be32(s, stream->bit_rate); /* avg bit rate */
|
|
170 put_be32(s, stream->packet_max_size); /* max packet size */
|
|
171 if (stream->nb_packets > 0)
|
|
172 packet_avg_size = stream->packet_total_size /
|
|
173 stream->nb_packets;
|
|
174 else
|
|
175 packet_avg_size = 0;
|
|
176 put_be32(s, packet_avg_size); /* avg packet size */
|
|
177 put_be32(s, 0); /* start time */
|
|
178 put_be32(s, BUFFER_DURATION); /* preroll */
|
|
179 /* duration */
|
|
180 if (url_is_streamed(s) || !stream->total_frames)
|
|
181 put_be32(s, (int)(3600 * 1000));
|
|
182 else
|
|
183 put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
|
|
184 put_str8(s, desc);
|
|
185 put_str8(s, mimetype);
|
|
186 put_be32(s, codec_data_size);
|
|
187
|
|
188 if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
|
|
189 int coded_frame_size, fscode, sample_rate;
|
|
190 sample_rate = stream->enc->sample_rate;
|
|
191 coded_frame_size = (stream->enc->bit_rate *
|
|
192 stream->enc->frame_size) / (8 * sample_rate);
|
|
193 /* audio codec info */
|
|
194 put_tag(s, ".ra");
|
|
195 put_byte(s, 0xfd);
|
|
196 put_be32(s, 0x00040000); /* version */
|
|
197 put_tag(s, ".ra4");
|
|
198 put_be32(s, 0x01b53530); /* stream length */
|
|
199 put_be16(s, 4); /* unknown */
|
|
200 put_be32(s, 0x39); /* header size */
|
|
201
|
|
202 switch(sample_rate) {
|
|
203 case 48000:
|
|
204 case 24000:
|
|
205 case 12000:
|
|
206 fscode = 1;
|
|
207 break;
|
|
208 default:
|
|
209 case 44100:
|
|
210 case 22050:
|
|
211 case 11025:
|
|
212 fscode = 2;
|
|
213 break;
|
|
214 case 32000:
|
|
215 case 16000:
|
|
216 case 8000:
|
|
217 fscode = 3;
|
|
218 }
|
|
219 put_be16(s, fscode); /* codec additional info, for AC3, seems
|
|
220 to be a frequency code */
|
|
221 /* special hack to compensate rounding errors... */
|
|
222 if (coded_frame_size == 557)
|
|
223 coded_frame_size--;
|
|
224 put_be32(s, coded_frame_size); /* frame length */
|
|
225 put_be32(s, 0x51540); /* unknown */
|
|
226 put_be32(s, 0x249f0); /* unknown */
|
|
227 put_be32(s, 0x249f0); /* unknown */
|
|
228 put_be16(s, 0x01);
|
|
229 /* frame length : seems to be very important */
|
|
230 put_be16(s, coded_frame_size);
|
|
231 put_be32(s, 0); /* unknown */
|
|
232 put_be16(s, stream->enc->sample_rate); /* sample rate */
|
|
233 put_be32(s, 0x10); /* unknown */
|
|
234 put_be16(s, stream->enc->channels);
|
|
235 put_str8(s, "Int0"); /* codec name */
|
|
236 put_str8(s, "dnet"); /* codec name */
|
|
237 put_be16(s, 0); /* title length */
|
|
238 put_be16(s, 0); /* author length */
|
|
239 put_be16(s, 0); /* copyright length */
|
|
240 put_byte(s, 0); /* end of header */
|
|
241 } else {
|
|
242 /* video codec info */
|
|
243 put_be32(s,34); /* size */
|
|
244 if(stream->enc->codec_id == CODEC_ID_RV10)
|
|
245 put_tag(s,"VIDORV10");
|
|
246 else
|
|
247 put_tag(s,"VIDORV20");
|
|
248 put_be16(s, stream->enc->width);
|
|
249 put_be16(s, stream->enc->height);
|
|
250 put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
|
|
251 put_be32(s,0); /* unknown meaning */
|
|
252 put_be16(s, (int) stream->frame_rate); /* unknown meaning */
|
|
253 put_be32(s,0); /* unknown meaning */
|
|
254 put_be16(s, 8); /* unknown meaning */
|
|
255 /* Seems to be the codec version: only use basic H263. The next
|
|
256 versions seems to add a diffential DC coding as in
|
|
257 MPEG... nothing new under the sun */
|
|
258 if(stream->enc->codec_id == CODEC_ID_RV10)
|
|
259 put_be32(s,0x10000000);
|
|
260 else
|
|
261 put_be32(s,0x20103001);
|
|
262 //put_be32(s,0x10003000);
|
|
263 }
|
|
264 }
|
|
265
|
|
266 /* patch data offset field */
|
|
267 data_pos = s->buf_ptr - start_ptr;
|
|
268 rm->data_pos = data_pos;
|
|
269 data_offset_ptr[0] = data_pos >> 24;
|
|
270 data_offset_ptr[1] = data_pos >> 16;
|
|
271 data_offset_ptr[2] = data_pos >> 8;
|
|
272 data_offset_ptr[3] = data_pos;
|
|
273
|
|
274 /* data stream */
|
|
275 put_tag(s,"DATA");
|
|
276 put_be32(s,data_size + 10 + 8);
|
|
277 put_be16(s,0);
|
|
278
|
|
279 put_be32(s, nb_packets); /* number of packets */
|
|
280 put_be32(s,0); /* next data header */
|
|
281 }
|
|
282
|
|
283 static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
|
|
284 int length, int key_frame)
|
|
285 {
|
|
286 int timestamp;
|
|
287 ByteIOContext *s = &ctx->pb;
|
|
288
|
|
289 stream->nb_packets++;
|
|
290 stream->packet_total_size += length;
|
|
291 if (length > stream->packet_max_size)
|
|
292 stream->packet_max_size = length;
|
|
293
|
|
294 put_be16(s,0); /* version */
|
|
295 put_be16(s,length + 12);
|
|
296 put_be16(s, stream->num); /* stream number */
|
|
297 timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
|
|
298 put_be32(s, timestamp); /* timestamp */
|
|
299 put_byte(s, 0); /* reserved */
|
|
300 put_byte(s, key_frame ? 2 : 0); /* flags */
|
|
301 }
|
|
302
|
|
303 static int rm_write_header(AVFormatContext *s)
|
|
304 {
|
|
305 RMContext *rm = s->priv_data;
|
|
306 StreamInfo *stream;
|
|
307 int n;
|
|
308 AVCodecContext *codec;
|
|
309
|
|
310 for(n=0;n<s->nb_streams;n++) {
|
|
311 s->streams[n]->id = n;
|
|
312 codec = s->streams[n]->codec;
|
|
313 stream = &rm->streams[n];
|
|
314 memset(stream, 0, sizeof(StreamInfo));
|
|
315 stream->num = n;
|
|
316 stream->bit_rate = codec->bit_rate;
|
|
317 stream->enc = codec;
|
|
318
|
|
319 switch(codec->codec_type) {
|
|
320 case CODEC_TYPE_AUDIO:
|
|
321 rm->audio_stream = stream;
|
|
322 stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
|
|
323 /* XXX: dummy values */
|
|
324 stream->packet_max_size = 1024;
|
|
325 stream->nb_packets = 0;
|
|
326 stream->total_frames = stream->nb_packets;
|
|
327 break;
|
|
328 case CODEC_TYPE_VIDEO:
|
|
329 rm->video_stream = stream;
|
|
330 stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
|
|
331 /* XXX: dummy values */
|
|
332 stream->packet_max_size = 4096;
|
|
333 stream->nb_packets = 0;
|
|
334 stream->total_frames = stream->nb_packets;
|
|
335 break;
|
|
336 default:
|
|
337 return -1;
|
|
338 }
|
|
339 }
|
|
340
|
|
341 rv10_write_header(s, 0, 0);
|
|
342 put_flush_packet(&s->pb);
|
|
343 return 0;
|
|
344 }
|
|
345
|
|
346 static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
|
|
347 {
|
|
348 uint8_t *buf1;
|
|
349 RMContext *rm = s->priv_data;
|
|
350 ByteIOContext *pb = &s->pb;
|
|
351 StreamInfo *stream = rm->audio_stream;
|
|
352 int i;
|
|
353
|
|
354 /* XXX: suppress this malloc */
|
|
355 buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
|
|
356
|
|
357 write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
|
|
358
|
|
359 /* for AC3, the words seems to be reversed */
|
|
360 for(i=0;i<size;i+=2) {
|
|
361 buf1[i] = buf[i+1];
|
|
362 buf1[i+1] = buf[i];
|
|
363 }
|
|
364 put_buffer(pb, buf1, size);
|
|
365 put_flush_packet(pb);
|
|
366 stream->nb_frames++;
|
|
367 av_free(buf1);
|
|
368 return 0;
|
|
369 }
|
|
370
|
|
371 static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
|
|
372 {
|
|
373 RMContext *rm = s->priv_data;
|
|
374 ByteIOContext *pb = &s->pb;
|
|
375 StreamInfo *stream = rm->video_stream;
|
|
376 int key_frame = !!(flags & PKT_FLAG_KEY);
|
|
377
|
|
378 /* XXX: this is incorrect: should be a parameter */
|
|
379
|
|
380 /* Well, I spent some time finding the meaning of these bits. I am
|
|
381 not sure I understood everything, but it works !! */
|
|
382 #if 1
|
|
383 write_packet_header(s, stream, size + 7, key_frame);
|
|
384 /* bit 7: '1' if final packet of a frame converted in several packets */
|
|
385 put_byte(pb, 0x81);
|
|
386 /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
|
|
387 frame starting from 1 */
|
|
388 if (key_frame) {
|
|
389 put_byte(pb, 0x81);
|
|
390 } else {
|
|
391 put_byte(pb, 0x01);
|
|
392 }
|
|
393 put_be16(pb, 0x4000 + (size)); /* total frame size */
|
|
394 put_be16(pb, 0x4000 + (size)); /* offset from the start or the end */
|
|
395 #else
|
|
396 /* full frame */
|
|
397 write_packet_header(s, size + 6);
|
|
398 put_byte(pb, 0xc0);
|
|
399 put_be16(pb, 0x4000 + size); /* total frame size */
|
|
400 put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
|
|
401 #endif
|
|
402 put_byte(pb, stream->nb_frames & 0xff);
|
|
403
|
|
404 put_buffer(pb, buf, size);
|
|
405 put_flush_packet(pb);
|
|
406
|
|
407 stream->nb_frames++;
|
|
408 return 0;
|
|
409 }
|
|
410
|
|
411 static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
412 {
|
|
413 if (s->streams[pkt->stream_index]->codec->codec_type ==
|
|
414 CODEC_TYPE_AUDIO)
|
|
415 return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
|
|
416 else
|
|
417 return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
|
|
418 }
|
|
419
|
|
420 static int rm_write_trailer(AVFormatContext *s)
|
|
421 {
|
|
422 RMContext *rm = s->priv_data;
|
|
423 int data_size, index_pos, i;
|
|
424 ByteIOContext *pb = &s->pb;
|
|
425
|
|
426 if (!url_is_streamed(&s->pb)) {
|
|
427 /* end of file: finish to write header */
|
|
428 index_pos = url_fseek(pb, 0, SEEK_CUR);
|
|
429 data_size = index_pos - rm->data_pos;
|
|
430
|
|
431 /* index */
|
|
432 put_tag(pb, "INDX");
|
|
433 put_be32(pb, 10 + 10 * s->nb_streams);
|
|
434 put_be16(pb, 0);
|
|
435
|
|
436 for(i=0;i<s->nb_streams;i++) {
|
|
437 put_be32(pb, 0); /* zero indices */
|
|
438 put_be16(pb, i); /* stream number */
|
|
439 put_be32(pb, 0); /* next index */
|
|
440 }
|
|
441 /* undocumented end header */
|
|
442 put_be32(pb, 0);
|
|
443 put_be32(pb, 0);
|
|
444
|
|
445 url_fseek(pb, 0, SEEK_SET);
|
|
446 for(i=0;i<s->nb_streams;i++)
|
|
447 rm->streams[i].total_frames = rm->streams[i].nb_frames;
|
|
448 rv10_write_header(s, data_size, index_pos);
|
|
449 } else {
|
|
450 /* undocumented end header */
|
|
451 put_be32(pb, 0);
|
|
452 put_be32(pb, 0);
|
|
453 }
|
|
454 put_flush_packet(pb);
|
|
455 return 0;
|
|
456 }
|
|
457 #endif //CONFIG_MUXERS
|
|
458
|
|
459 /***************************************************/
|
|
460
|
|
461 static void get_str(ByteIOContext *pb, char *buf, int buf_size)
|
|
462 {
|
|
463 int len, i;
|
|
464 char *q;
|
|
465
|
|
466 len = get_be16(pb);
|
|
467 q = buf;
|
|
468 for(i=0;i<len;i++) {
|
|
469 if (i < buf_size - 1)
|
|
470 *q++ = get_byte(pb);
|
|
471 }
|
|
472 *q = '\0';
|
|
473 }
|
|
474
|
|
475 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
|
|
476 {
|
|
477 int len, i;
|
|
478 char *q;
|
|
479
|
|
480 len = get_byte(pb);
|
|
481 q = buf;
|
|
482 for(i=0;i<len;i++) {
|
|
483 if (i < buf_size - 1)
|
|
484 *q++ = get_byte(pb);
|
|
485 }
|
|
486 *q = '\0';
|
|
487 }
|
|
488
|
|
489 static int rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
|
|
490 int read_all)
|
|
491 {
|
|
492 RMContext *rm = s->priv_data;
|
|
493 ByteIOContext *pb = &s->pb;
|
|
494 char buf[256];
|
|
495 uint32_t version;
|
|
496 int i;
|
|
497
|
|
498 /* ra type header */
|
|
499 version = get_be32(pb); /* version */
|
|
500 if (((version >> 16) & 0xff) == 3) {
|
|
501 int64_t startpos = url_ftell(pb);
|
|
502 /* very old version */
|
|
503 for(i = 0; i < 14; i++)
|
|
504 get_byte(pb);
|
|
505 get_str8(pb, s->title, sizeof(s->title));
|
|
506 get_str8(pb, s->author, sizeof(s->author));
|
|
507 get_str8(pb, s->copyright, sizeof(s->copyright));
|
|
508 get_str8(pb, s->comment, sizeof(s->comment));
|
|
509 if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
|
|
510 // fourcc (should always be "lpcJ")
|
|
511 get_byte(pb);
|
|
512 get_str8(pb, buf, sizeof(buf));
|
|
513 }
|
|
514 // Skip extra header crap (this should never happen)
|
|
515 if ((startpos + (version & 0xffff)) > url_ftell(pb))
|
|
516 url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
|
|
517 st->codec->sample_rate = 8000;
|
|
518 st->codec->channels = 1;
|
|
519 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
520 st->codec->codec_id = CODEC_ID_RA_144;
|
|
521 } else {
|
|
522 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
|
|
523 /* old version (4) */
|
|
524 get_be32(pb); /* .ra4 */
|
|
525 get_be32(pb); /* data size */
|
|
526 get_be16(pb); /* version2 */
|
|
527 get_be32(pb); /* header size */
|
|
528 flavor= get_be16(pb); /* add codec info / flavor */
|
|
529 rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
|
|
530 get_be32(pb); /* ??? */
|
|
531 get_be32(pb); /* ??? */
|
|
532 get_be32(pb); /* ??? */
|
|
533 rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
|
|
534 st->codec->block_align= get_be16(pb); /* frame size */
|
|
535 rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
|
|
536 get_be16(pb); /* ??? */
|
|
537 if (((version >> 16) & 0xff) == 5) {
|
|
538 get_be16(pb); get_be16(pb); get_be16(pb); }
|
|
539 st->codec->sample_rate = get_be16(pb);
|
|
540 get_be32(pb);
|
|
541 st->codec->channels = get_be16(pb);
|
|
542 if (((version >> 16) & 0xff) == 5) {
|
|
543 get_be32(pb);
|
|
544 buf[0] = get_byte(pb);
|
|
545 buf[1] = get_byte(pb);
|
|
546 buf[2] = get_byte(pb);
|
|
547 buf[3] = get_byte(pb);
|
|
548 buf[4] = 0;
|
|
549 } else {
|
|
550 get_str8(pb, buf, sizeof(buf)); /* desc */
|
|
551 get_str8(pb, buf, sizeof(buf)); /* desc */
|
|
552 }
|
|
553 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
554 if (!strcmp(buf, "dnet")) {
|
|
555 st->codec->codec_id = CODEC_ID_AC3;
|
|
556 } else if (!strcmp(buf, "28_8")) {
|
|
557 st->codec->codec_id = CODEC_ID_RA_288;
|
|
558 st->codec->extradata_size= 0;
|
|
559 rm->audio_framesize = st->codec->block_align;
|
|
560 st->codec->block_align = coded_framesize;
|
|
561
|
|
562 if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
|
|
563 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
|
|
564 return -1;
|
|
565 }
|
|
566
|
|
567 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
|
|
568 } else if (!strcmp(buf, "cook")) {
|
|
569 int codecdata_length, i;
|
|
570 get_be16(pb); get_byte(pb);
|
|
571 if (((version >> 16) & 0xff) == 5)
|
|
572 get_byte(pb);
|
|
573 codecdata_length = get_be32(pb);
|
|
574 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
|
|
575 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
|
|
576 return -1;
|
|
577 }
|
|
578
|
|
579 st->codec->codec_id = CODEC_ID_COOK;
|
|
580 st->codec->extradata_size= codecdata_length;
|
|
581 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
582 for(i = 0; i < codecdata_length; i++)
|
|
583 ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
|
|
584 rm->audio_framesize = st->codec->block_align;
|
|
585 st->codec->block_align = rm->sub_packet_size;
|
|
586
|
|
587 if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
|
|
588 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
|
|
589 return -1;
|
|
590 }
|
|
591
|
|
592 rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
|
|
593 } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
|
|
594 int codecdata_length, i;
|
|
595 get_be16(pb); get_byte(pb);
|
|
596 if (((version >> 16) & 0xff) == 5)
|
|
597 get_byte(pb);
|
|
598 st->codec->codec_id = CODEC_ID_AAC;
|
|
599 codecdata_length = get_be32(pb);
|
|
600 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
|
|
601 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
|
|
602 return -1;
|
|
603 }
|
|
604 if (codecdata_length >= 1) {
|
|
605 st->codec->extradata_size = codecdata_length - 1;
|
|
606 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
607 get_byte(pb);
|
|
608 for(i = 0; i < st->codec->extradata_size; i++)
|
|
609 ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
|
|
610 }
|
|
611 } else {
|
|
612 st->codec->codec_id = CODEC_ID_NONE;
|
|
613 pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
|
|
614 buf);
|
|
615 }
|
|
616 if (read_all) {
|
|
617 get_byte(pb);
|
|
618 get_byte(pb);
|
|
619 get_byte(pb);
|
|
620
|
|
621 get_str8(pb, s->title, sizeof(s->title));
|
|
622 get_str8(pb, s->author, sizeof(s->author));
|
|
623 get_str8(pb, s->copyright, sizeof(s->copyright));
|
|
624 get_str8(pb, s->comment, sizeof(s->comment));
|
|
625 }
|
|
626 }
|
|
627 return 0;
|
|
628 }
|
|
629
|
|
630 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
|
|
631 {
|
|
632 RMContext *rm = s->priv_data;
|
|
633 AVStream *st;
|
|
634
|
|
635 rm->old_format = 1;
|
|
636 st = av_new_stream(s, 0);
|
|
637 if (!st)
|
|
638 return -1;
|
|
639 return rm_read_audio_stream_info(s, st, 1);
|
|
640 }
|
|
641
|
|
642 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|
643 {
|
|
644 RMContext *rm = s->priv_data;
|
|
645 AVStream *st;
|
|
646 ByteIOContext *pb = &s->pb;
|
|
647 unsigned int tag, v;
|
|
648 int tag_size, size, codec_data_size, i;
|
|
649 int64_t codec_pos;
|
|
650 unsigned int start_time, duration;
|
|
651 char buf[128];
|
|
652 int flags = 0;
|
|
653
|
|
654 tag = get_le32(pb);
|
|
655 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
|
|
656 /* very old .ra format */
|
|
657 return rm_read_header_old(s, ap);
|
|
658 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
|
|
659 return AVERROR_IO;
|
|
660 }
|
|
661
|
|
662 get_be32(pb); /* header size */
|
|
663 get_be16(pb);
|
|
664 get_be32(pb);
|
|
665 get_be32(pb); /* number of headers */
|
|
666
|
|
667 for(;;) {
|
|
668 if (url_feof(pb))
|
|
669 goto fail;
|
|
670 tag = get_le32(pb);
|
|
671 tag_size = get_be32(pb);
|
|
672 get_be16(pb);
|
|
673 #if 0
|
|
674 printf("tag=%c%c%c%c (%08x) size=%d\n",
|
|
675 (tag) & 0xff,
|
|
676 (tag >> 8) & 0xff,
|
|
677 (tag >> 16) & 0xff,
|
|
678 (tag >> 24) & 0xff,
|
|
679 tag,
|
|
680 tag_size);
|
|
681 #endif
|
|
682 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
|
|
683 goto fail;
|
|
684 switch(tag) {
|
|
685 case MKTAG('P', 'R', 'O', 'P'):
|
|
686 /* file header */
|
|
687 get_be32(pb); /* max bit rate */
|
|
688 get_be32(pb); /* avg bit rate */
|
|
689 get_be32(pb); /* max packet size */
|
|
690 get_be32(pb); /* avg packet size */
|
|
691 get_be32(pb); /* nb packets */
|
|
692 get_be32(pb); /* duration */
|
|
693 get_be32(pb); /* preroll */
|
|
694 get_be32(pb); /* index offset */
|
|
695 get_be32(pb); /* data offset */
|
|
696 get_be16(pb); /* nb streams */
|
|
697 flags = get_be16(pb); /* flags */
|
|
698 break;
|
|
699 case MKTAG('C', 'O', 'N', 'T'):
|
|
700 get_str(pb, s->title, sizeof(s->title));
|
|
701 get_str(pb, s->author, sizeof(s->author));
|
|
702 get_str(pb, s->copyright, sizeof(s->copyright));
|
|
703 get_str(pb, s->comment, sizeof(s->comment));
|
|
704 break;
|
|
705 case MKTAG('M', 'D', 'P', 'R'):
|
|
706 st = av_new_stream(s, 0);
|
|
707 if (!st)
|
|
708 goto fail;
|
|
709 st->id = get_be16(pb);
|
|
710 get_be32(pb); /* max bit rate */
|
|
711 st->codec->bit_rate = get_be32(pb); /* bit rate */
|
|
712 get_be32(pb); /* max packet size */
|
|
713 get_be32(pb); /* avg packet size */
|
|
714 start_time = get_be32(pb); /* start time */
|
|
715 get_be32(pb); /* preroll */
|
|
716 duration = get_be32(pb); /* duration */
|
|
717 st->start_time = start_time;
|
|
718 st->duration = duration;
|
|
719 get_str8(pb, buf, sizeof(buf)); /* desc */
|
|
720 get_str8(pb, buf, sizeof(buf)); /* mimetype */
|
|
721 codec_data_size = get_be32(pb);
|
|
722 codec_pos = url_ftell(pb);
|
|
723 st->codec->codec_type = CODEC_TYPE_DATA;
|
|
724 av_set_pts_info(st, 64, 1, 1000);
|
|
725
|
|
726 v = get_be32(pb);
|
|
727 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
|
|
728 /* ra type header */
|
|
729 if (rm_read_audio_stream_info(s, st, 0))
|
|
730 return -1;
|
|
731 } else {
|
|
732 int fps, fps2;
|
|
733 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
|
|
734 fail1:
|
|
735 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
|
|
736 goto skip;
|
|
737 }
|
|
738 st->codec->codec_tag = get_le32(pb);
|
|
739 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
|
|
740 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
|
|
741 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
|
|
742 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
|
|
743 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
|
|
744 goto fail1;
|
|
745 st->codec->width = get_be16(pb);
|
|
746 st->codec->height = get_be16(pb);
|
|
747 st->codec->time_base.num= 1;
|
|
748 fps= get_be16(pb);
|
|
749 st->codec->codec_type = CODEC_TYPE_VIDEO;
|
|
750 get_be32(pb);
|
|
751 fps2= get_be16(pb);
|
|
752 get_be16(pb);
|
|
753
|
|
754 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
|
|
755
|
|
756 if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
|
|
757 //check is redundant as get_buffer() will catch this
|
|
758 av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
|
|
759 return -1;
|
|
760 }
|
|
761 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
762 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
|
|
763
|
|
764 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
|
|
765 st->codec->time_base.den = fps * st->codec->time_base.num;
|
|
766 switch(((uint8_t*)st->codec->extradata)[4]>>4){
|
|
767 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
|
|
768 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
|
|
769 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
|
|
770 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
|
|
771 default: goto fail1;
|
|
772 }
|
|
773 }
|
|
774 skip:
|
|
775 /* skip codec info */
|
|
776 size = url_ftell(pb) - codec_pos;
|
|
777 url_fskip(pb, codec_data_size - size);
|
|
778 break;
|
|
779 case MKTAG('D', 'A', 'T', 'A'):
|
|
780 goto header_end;
|
|
781 default:
|
|
782 /* unknown tag: skip it */
|
|
783 url_fskip(pb, tag_size - 10);
|
|
784 break;
|
|
785 }
|
|
786 }
|
|
787 header_end:
|
|
788 rm->nb_packets = get_be32(pb); /* number of packets */
|
|
789 if (!rm->nb_packets && (flags & 4))
|
|
790 rm->nb_packets = 3600 * 25;
|
|
791 get_be32(pb); /* next data header */
|
|
792 return 0;
|
|
793
|
|
794 fail:
|
|
795 for(i=0;i<s->nb_streams;i++) {
|
|
796 av_free(s->streams[i]);
|
|
797 }
|
|
798 return AVERROR_IO;
|
|
799 }
|
|
800
|
|
801 static int get_num(ByteIOContext *pb, int *len)
|
|
802 {
|
|
803 int n, n1;
|
|
804
|
|
805 n = get_be16(pb);
|
|
806 (*len)-=2;
|
|
807 if (n >= 0x4000) {
|
|
808 return n - 0x4000;
|
|
809 } else {
|
|
810 n1 = get_be16(pb);
|
|
811 (*len)-=2;
|
|
812 return (n << 16) | n1;
|
|
813 }
|
|
814 }
|
|
815
|
|
816 /* multiple of 20 bytes for ra144 (ugly) */
|
|
817 #define RAW_PACKET_SIZE 1000
|
|
818
|
|
819 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
|
|
820 RMContext *rm = s->priv_data;
|
|
821 ByteIOContext *pb = &s->pb;
|
|
822 int len, num, res, i;
|
|
823 AVStream *st;
|
|
824 uint32_t state=0xFFFFFFFF;
|
|
825
|
|
826 while(!url_feof(pb)){
|
|
827 *pos= url_ftell(pb);
|
|
828 if(rm->remaining_len > 0){
|
|
829 num= rm->current_stream;
|
|
830 len= rm->remaining_len;
|
|
831 *timestamp = AV_NOPTS_VALUE;
|
|
832 *flags= 0;
|
|
833 }else{
|
|
834 state= (state<<8) + get_byte(pb);
|
|
835
|
|
836 if(state == MKBETAG('I', 'N', 'D', 'X')){
|
|
837 len = get_be16(pb) - 6;
|
|
838 if(len<0)
|
|
839 continue;
|
|
840 goto skip;
|
|
841 }
|
|
842
|
|
843 if(state > (unsigned)0xFFFF || state < 12)
|
|
844 continue;
|
|
845 len=state;
|
|
846 state= 0xFFFFFFFF;
|
|
847
|
|
848 num = get_be16(pb);
|
|
849 *timestamp = get_be32(pb);
|
|
850 res= get_byte(pb); /* reserved */
|
|
851 *flags = get_byte(pb); /* flags */
|
|
852
|
|
853
|
|
854 len -= 12;
|
|
855 }
|
|
856 for(i=0;i<s->nb_streams;i++) {
|
|
857 st = s->streams[i];
|
|
858 if (num == st->id)
|
|
859 break;
|
|
860 }
|
|
861 if (i == s->nb_streams) {
|
|
862 skip:
|
|
863 /* skip packet if unknown number */
|
|
864 url_fskip(pb, len);
|
|
865 rm->remaining_len -= len;
|
|
866 continue;
|
|
867 }
|
|
868 *stream_index= i;
|
|
869
|
|
870 return len;
|
|
871 }
|
|
872 return -1;
|
|
873 }
|
|
874
|
|
875 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
876 {
|
|
877 RMContext *rm = s->priv_data;
|
|
878 ByteIOContext *pb = &s->pb;
|
|
879 AVStream *st;
|
|
880 int i, len, tmp, j;
|
|
881 int64_t timestamp, pos;
|
|
882 uint8_t *ptr;
|
|
883 int flags;
|
|
884
|
|
885 if (rm->audio_pkt_cnt) {
|
|
886 // If there are queued audio packet return them first
|
|
887 st = s->streams[rm->audio_stream_num];
|
|
888 if (st->codec->codec_id == CODEC_ID_AAC)
|
|
889 av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
|
|
890 else {
|
|
891 av_new_packet(pkt, st->codec->block_align);
|
|
892 memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
|
|
893 (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
|
|
894 st->codec->block_align);
|
|
895 }
|
|
896 rm->audio_pkt_cnt--;
|
|
897 pkt->flags = 0;
|
|
898 pkt->stream_index = rm->audio_stream_num;
|
|
899 } else if (rm->old_format) {
|
|
900 st = s->streams[0];
|
|
901 if (st->codec->codec_id == CODEC_ID_RA_288) {
|
|
902 int x, y;
|
|
903
|
|
904 for (y = 0; y < rm->sub_packet_h; y++)
|
|
905 for (x = 0; x < rm->sub_packet_h/2; x++)
|
|
906 if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
|
|
907 return AVERROR_IO;
|
|
908 rm->audio_stream_num = 0;
|
|
909 rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
|
|
910 // Release first audio packet
|
|
911 av_new_packet(pkt, st->codec->block_align);
|
|
912 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
|
|
913 pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
|
|
914 pkt->stream_index = 0;
|
|
915 } else {
|
|
916 /* just read raw bytes */
|
|
917 len = RAW_PACKET_SIZE;
|
|
918 len= av_get_packet(pb, pkt, len);
|
|
919 pkt->stream_index = 0;
|
|
920 if (len <= 0) {
|
|
921 return AVERROR_IO;
|
|
922 }
|
|
923 pkt->size = len;
|
|
924 }
|
|
925 } else {
|
|
926 int seq=1;
|
|
927 resync:
|
|
928 len=sync(s, ×tamp, &flags, &i, &pos);
|
|
929 if(len<0)
|
|
930 return AVERROR_IO;
|
|
931 st = s->streams[i];
|
|
932
|
|
933 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
|
934 int h, pic_num, len2, pos;
|
|
935
|
|
936 h= get_byte(pb); len--;
|
|
937 if(!(h & 0x40)){
|
|
938 seq = get_byte(pb); len--;
|
|
939 }
|
|
940
|
|
941 if((h & 0xc0) == 0x40){
|
|
942 len2= pos= 0;
|
|
943 }else{
|
|
944 len2 = get_num(pb, &len);
|
|
945 pos = get_num(pb, &len);
|
|
946 }
|
|
947 /* picture number */
|
|
948 pic_num= get_byte(pb); len--;
|
|
949 rm->remaining_len= len;
|
|
950 rm->current_stream= st->id;
|
|
951
|
|
952 // av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
|
|
953 if(len2 && len2<len)
|
|
954 len=len2;
|
|
955 rm->remaining_len-= len;
|
|
956 av_get_packet(pb, pkt, len);
|
|
957 }
|
|
958
|
|
959 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
|
|
960 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
|
|
961 (st->codec->codec_id == CODEC_ID_COOK)) {
|
|
962 int x;
|
|
963 int sps = rm->sub_packet_size;
|
|
964 int cfs = rm->coded_framesize;
|
|
965 int h = rm->sub_packet_h;
|
|
966 int y = rm->sub_packet_cnt;
|
|
967 int w = rm->audio_framesize;
|
|
968
|
|
969 if (flags & 2)
|
|
970 y = rm->sub_packet_cnt = 0;
|
|
971 if (!y)
|
|
972 rm->audiotimestamp = timestamp;
|
|
973
|
|
974 switch(st->codec->codec_id) {
|
|
975 case CODEC_ID_RA_288:
|
|
976 for (x = 0; x < h/2; x++)
|
|
977 get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
|
|
978 break;
|
|
979 case CODEC_ID_COOK:
|
|
980 for (x = 0; x < w/sps; x++)
|
|
981 get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
|
|
982 break;
|
|
983 }
|
|
984
|
|
985 if (++(rm->sub_packet_cnt) < h)
|
|
986 goto resync;
|
|
987 else {
|
|
988 rm->sub_packet_cnt = 0;
|
|
989 rm->audio_stream_num = i;
|
|
990 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
|
|
991 // Release first audio packet
|
|
992 av_new_packet(pkt, st->codec->block_align);
|
|
993 memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
|
|
994 timestamp = rm->audiotimestamp;
|
|
995 flags = 2; // Mark first packet as keyframe
|
|
996 }
|
|
997 } else if (st->codec->codec_id == CODEC_ID_AAC) {
|
|
998 int x;
|
|
999 rm->audio_stream_num = i;
|
|
1000 rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
|
|
1001 if (rm->sub_packet_cnt) {
|
|
1002 for (x = 0; x < rm->sub_packet_cnt; x++)
|
|
1003 rm->sub_packet_lengths[x] = get_be16(pb);
|
|
1004 // Release first audio packet
|
|
1005 rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
|
|
1006 av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
|
|
1007 flags = 2; // Mark first packet as keyframe
|
|
1008 }
|
|
1009 } else
|
|
1010 av_get_packet(pb, pkt, len);
|
|
1011 }
|
|
1012
|
|
1013 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
|
|
1014 || st->discard >= AVDISCARD_ALL){
|
|
1015 av_free_packet(pkt);
|
|
1016 goto resync;
|
|
1017 }
|
|
1018
|
|
1019 pkt->stream_index = i;
|
|
1020
|
|
1021 #if 0
|
|
1022 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
|
1023 if(st->codec->codec_id == CODEC_ID_RV20){
|
|
1024 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
|
|
1025 av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
|
|
1026
|
|
1027 seq |= (timestamp&~0x3FFF);
|
|
1028 if(seq - timestamp > 0x2000) seq -= 0x4000;
|
|
1029 if(seq - timestamp < -0x2000) seq += 0x4000;
|
|
1030 }
|
|
1031 }
|
|
1032 #endif
|
|
1033 pkt->pts= timestamp;
|
|
1034 if(flags&2){
|
|
1035 pkt->flags |= PKT_FLAG_KEY;
|
|
1036 if((seq&0x7F) == 1)
|
|
1037 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
|
|
1038 }
|
|
1039 }
|
|
1040
|
|
1041 /* for AC3, needs to swap bytes */
|
|
1042 if (st->codec->codec_id == CODEC_ID_AC3) {
|
|
1043 ptr = pkt->data;
|
|
1044 for(j=0;j<len;j+=2) {
|
|
1045 tmp = ptr[0];
|
|
1046 ptr[0] = ptr[1];
|
|
1047 ptr[1] = tmp;
|
|
1048 ptr += 2;
|
|
1049 }
|
|
1050 }
|
|
1051 return 0;
|
|
1052 }
|
|
1053
|
|
1054 static int rm_read_close(AVFormatContext *s)
|
|
1055 {
|
|
1056 RMContext *rm = s->priv_data;
|
|
1057
|
|
1058 av_free(rm->audiobuf);
|
|
1059 return 0;
|
|
1060 }
|
|
1061
|
|
1062 static int rm_probe(AVProbeData *p)
|
|
1063 {
|
|
1064 /* check file header */
|
|
1065 if (p->buf_size <= 32)
|
|
1066 return 0;
|
|
1067 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
|
|
1068 p->buf[2] == 'M' && p->buf[3] == 'F' &&
|
|
1069 p->buf[4] == 0 && p->buf[5] == 0) ||
|
|
1070 (p->buf[0] == '.' && p->buf[1] == 'r' &&
|
|
1071 p->buf[2] == 'a' && p->buf[3] == 0xfd))
|
|
1072 return AVPROBE_SCORE_MAX;
|
|
1073 else
|
|
1074 return 0;
|
|
1075 }
|
|
1076
|
|
1077 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
|
|
1078 int64_t *ppos, int64_t pos_limit)
|
|
1079 {
|
|
1080 RMContext *rm = s->priv_data;
|
|
1081 int64_t pos, dts;
|
|
1082 int stream_index2, flags, len, h;
|
|
1083
|
|
1084 pos = *ppos;
|
|
1085
|
|
1086 if(rm->old_format)
|
|
1087 return AV_NOPTS_VALUE;
|
|
1088
|
|
1089 url_fseek(&s->pb, pos, SEEK_SET);
|
|
1090 rm->remaining_len=0;
|
|
1091 for(;;){
|
|
1092 int seq=1;
|
|
1093 AVStream *st;
|
|
1094
|
|
1095 len=sync(s, &dts, &flags, &stream_index2, &pos);
|
|
1096 if(len<0)
|
|
1097 return AV_NOPTS_VALUE;
|
|
1098
|
|
1099 st = s->streams[stream_index2];
|
|
1100 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
|
1101 h= get_byte(&s->pb); len--;
|
|
1102 if(!(h & 0x40)){
|
|
1103 seq = get_byte(&s->pb); len--;
|
|
1104 }
|
|
1105 }
|
|
1106
|
|
1107 if((flags&2) && (seq&0x7F) == 1){
|
|
1108 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
|
|
1109 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
|
|
1110 if(stream_index2 == stream_index)
|
|
1111 break;
|
|
1112 }
|
|
1113
|
|
1114 url_fskip(&s->pb, len);
|
|
1115 }
|
|
1116 *ppos = pos;
|
|
1117 return dts;
|
|
1118 }
|
|
1119
|
|
1120 #ifdef CONFIG_RM_DEMUXER
|
|
1121 AVInputFormat rm_demuxer = {
|
|
1122 "rm",
|
|
1123 "rm format",
|
|
1124 sizeof(RMContext),
|
|
1125 rm_probe,
|
|
1126 rm_read_header,
|
|
1127 rm_read_packet,
|
|
1128 rm_read_close,
|
|
1129 NULL,
|
|
1130 rm_read_dts,
|
|
1131 };
|
|
1132 #endif
|
|
1133 #ifdef CONFIG_RM_MUXER
|
|
1134 AVOutputFormat rm_muxer = {
|
|
1135 "rm",
|
|
1136 "rm format",
|
|
1137 "application/vnd.rn-realmedia",
|
|
1138 "rm,ra",
|
|
1139 sizeof(RMContext),
|
|
1140 CODEC_ID_AC3,
|
|
1141 CODEC_ID_RV10,
|
|
1142 rm_write_header,
|
|
1143 rm_write_packet,
|
|
1144 rm_write_trailer,
|
|
1145 };
|
|
1146 #endif
|