Implement RMStream stream-specific private data object in the RM demuxer.
[libav.git] / libavformat / rmdec.c
1 /*
2 * "Real" compatible demuxer.
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
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include "rm.h"
25
26 struct RMStream {
27 uint8_t *videobuf; ///< place to store merged video frame
28 int videobufsize; ///< current assembled frame size
29 int videobufpos; ///< position for the next slice in the video buffer
30 int curpic_num; ///< picture number of current frame
31 int cur_slice, slices;
32 int64_t pktpos; ///< first slice position in file
33 /// Audio descrambling matrix parameters
34 uint8_t *audiobuf; ///< place to store reordered audio data
35 int64_t audiotimestamp; ///< Audio packet timestamp
36 int sub_packet_cnt; // Subpacket counter, used while reading
37 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
38 int audio_framesize; /// Audio frame size from container
39 int sub_packet_lengths[16]; /// Length of each subpacket
40 };
41
42 typedef struct {
43 int nb_packets;
44 int old_format;
45 int current_stream;
46 int remaining_len;
47 int audio_stream_num; ///< Stream number for audio packets
48 int audio_pkt_cnt; ///< Output packet counter
49 } RMDemuxContext;
50
51 static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
52 {
53 int i;
54 char *q, r;
55
56 q = buf;
57 for(i=0;i<len;i++) {
58 r = get_byte(pb);
59 if (i < buf_size - 1)
60 *q++ = r;
61 }
62 if (buf_size > 0) *q = '\0';
63 }
64
65 static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
66 {
67 get_strl(pb, buf, buf_size, get_be16(pb));
68 }
69
70 static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
71 {
72 get_strl(pb, buf, buf_size, get_byte(pb));
73 }
74
75 RMStream *ff_rm_alloc_rmstream (void)
76 {
77 RMStream *rms = av_mallocz(sizeof(RMStream));
78 rms->curpic_num = -1;
79 return rms;
80 }
81
82 void ff_rm_free_rmstream (RMStream *rms)
83 {
84 av_free(rms->videobuf);
85 av_free(rms->audiobuf);
86 av_free(rms);
87 }
88
89 static int rm_read_audio_stream_info(AVFormatContext *s, ByteIOContext *pb,
90 AVStream *st, int read_all)
91 {
92 RMStream *ast = st->priv_data;
93 char buf[256];
94 uint32_t version;
95
96 /* ra type header */
97 version = get_be32(pb); /* version */
98 if (((version >> 16) & 0xff) == 3) {
99 int64_t startpos = url_ftell(pb);
100 url_fskip(pb, 14);
101 get_str8(pb, s->title, sizeof(s->title));
102 get_str8(pb, s->author, sizeof(s->author));
103 get_str8(pb, s->copyright, sizeof(s->copyright));
104 get_str8(pb, s->comment, sizeof(s->comment));
105 if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
106 // fourcc (should always be "lpcJ")
107 get_byte(pb);
108 get_str8(pb, buf, sizeof(buf));
109 }
110 // Skip extra header crap (this should never happen)
111 if ((startpos + (version & 0xffff)) > url_ftell(pb))
112 url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
113 st->codec->sample_rate = 8000;
114 st->codec->channels = 1;
115 st->codec->codec_type = CODEC_TYPE_AUDIO;
116 st->codec->codec_id = CODEC_ID_RA_144;
117 } else {
118 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
119 /* old version (4) */
120 get_be32(pb); /* .ra4 */
121 get_be32(pb); /* data size */
122 get_be16(pb); /* version2 */
123 get_be32(pb); /* header size */
124 flavor= get_be16(pb); /* add codec info / flavor */
125 ast->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
126 get_be32(pb); /* ??? */
127 get_be32(pb); /* ??? */
128 get_be32(pb); /* ??? */
129 ast->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
130 st->codec->block_align= get_be16(pb); /* frame size */
131 ast->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
132 get_be16(pb); /* ??? */
133 if (((version >> 16) & 0xff) == 5) {
134 get_be16(pb); get_be16(pb); get_be16(pb);
135 }
136 st->codec->sample_rate = get_be16(pb);
137 get_be32(pb);
138 st->codec->channels = get_be16(pb);
139 if (((version >> 16) & 0xff) == 5) {
140 get_be32(pb);
141 get_buffer(pb, buf, 4);
142 buf[4] = 0;
143 } else {
144 get_str8(pb, buf, sizeof(buf)); /* desc */
145 get_str8(pb, buf, sizeof(buf)); /* desc */
146 }
147 st->codec->codec_type = CODEC_TYPE_AUDIO;
148 if (!strcmp(buf, "dnet")) {
149 st->codec->codec_id = CODEC_ID_AC3;
150 st->need_parsing = AVSTREAM_PARSE_FULL;
151 } else if (!strcmp(buf, "28_8")) {
152 st->codec->codec_id = CODEC_ID_RA_288;
153 st->codec->extradata_size= 0;
154 ast->audio_framesize = st->codec->block_align;
155 st->codec->block_align = coded_framesize;
156
157 if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
158 av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
159 return -1;
160 }
161
162 ast->audiobuf = av_malloc(ast->audio_framesize * sub_packet_h);
163 } else if ((!strcmp(buf, "cook")) || (!strcmp(buf, "atrc")) || (!strcmp(buf, "sipr"))) {
164 int codecdata_length;
165 get_be16(pb); get_byte(pb);
166 if (((version >> 16) & 0xff) == 5)
167 get_byte(pb);
168 codecdata_length = get_be32(pb);
169 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
170 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
171 return -1;
172 }
173
174 if(sub_packet_size <= 0){
175 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
176 return -1;
177 }
178
179 if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
180 else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
181 else st->codec->codec_id = CODEC_ID_ATRAC3;
182 st->codec->extradata_size= codecdata_length;
183 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
184 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
185 ast->audio_framesize = st->codec->block_align;
186 st->codec->block_align = ast->sub_packet_size;
187
188 if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
189 av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
190 return -1;
191 }
192
193 ast->audiobuf = av_malloc(ast->audio_framesize * sub_packet_h);
194 } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
195 int codecdata_length;
196 get_be16(pb); get_byte(pb);
197 if (((version >> 16) & 0xff) == 5)
198 get_byte(pb);
199 st->codec->codec_id = CODEC_ID_AAC;
200 codecdata_length = get_be32(pb);
201 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
202 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
203 return -1;
204 }
205 if (codecdata_length >= 1) {
206 st->codec->extradata_size = codecdata_length - 1;
207 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
208 get_byte(pb);
209 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
210 }
211 } else {
212 st->codec->codec_id = CODEC_ID_NONE;
213 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
214 }
215 if (read_all) {
216 get_byte(pb);
217 get_byte(pb);
218 get_byte(pb);
219
220 get_str8(pb, s->title, sizeof(s->title));
221 get_str8(pb, s->author, sizeof(s->author));
222 get_str8(pb, s->copyright, sizeof(s->copyright));
223 get_str8(pb, s->comment, sizeof(s->comment));
224 }
225 }
226 return 0;
227 }
228
229 int
230 ff_rm_read_mdpr_codecdata (AVFormatContext *s, ByteIOContext *pb,
231 AVStream *st, int codec_data_size)
232 {
233 unsigned int v;
234 int size;
235 int64_t codec_pos;
236 RMStream *rst;
237
238 st->priv_data = rst = ff_rm_alloc_rmstream();
239 av_set_pts_info(st, 64, 1, 1000);
240 codec_pos = url_ftell(pb);
241 v = get_be32(pb);
242 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
243 /* ra type header */
244 if (rm_read_audio_stream_info(s, pb, st, 0))
245 return -1;
246 } else {
247 int fps, fps2;
248 if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
249 fail1:
250 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
251 goto skip;
252 }
253 st->codec->codec_tag = get_le32(pb);
254 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
255 if ( st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
256 && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
257 && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
258 && st->codec->codec_tag != MKTAG('R', 'V', '4', '0')
259 && st->codec->codec_tag != MKTAG('R', 'V', 'T', 'R'))
260 goto fail1;
261 st->codec->width = get_be16(pb);
262 st->codec->height = get_be16(pb);
263 st->codec->time_base.num= 1;
264 fps= get_be16(pb);
265 st->codec->codec_type = CODEC_TYPE_VIDEO;
266 get_be32(pb);
267 fps2= get_be16(pb);
268 get_be16(pb);
269
270 st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
271
272 if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
273 //check is redundant as get_buffer() will catch this
274 av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
275 return -1;
276 }
277 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
278 if (!st->codec->extradata)
279 return AVERROR(ENOMEM);
280 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
281
282 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
283 st->codec->time_base.den = fps * st->codec->time_base.num;
284 switch(((uint8_t*)st->codec->extradata)[4]>>4){
285 case 1: st->codec->codec_id = CODEC_ID_RV10; break;
286 case 2: st->codec->codec_id = CODEC_ID_RV20; break;
287 case 3: st->codec->codec_id = CODEC_ID_RV30; break;
288 case 4: st->codec->codec_id = CODEC_ID_RV40; break;
289 default: goto fail1;
290 }
291 }
292
293 skip:
294 /* skip codec info */
295 size = url_ftell(pb) - codec_pos;
296 url_fskip(pb, codec_data_size - size);
297
298 return 0;
299 }
300
301
302 static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
303 {
304 RMDemuxContext *rm = s->priv_data;
305 AVStream *st;
306
307 rm->old_format = 1;
308 st = av_new_stream(s, 0);
309 if (!st)
310 return -1;
311 return rm_read_audio_stream_info(s, s->pb, st, 1);
312 }
313
314 static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
315 {
316 RMDemuxContext *rm = s->priv_data;
317 AVStream *st;
318 ByteIOContext *pb = s->pb;
319 unsigned int tag;
320 int tag_size;
321 unsigned int start_time, duration;
322 char buf[128];
323 int flags = 0;
324
325 tag = get_le32(pb);
326 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
327 /* very old .ra format */
328 return rm_read_header_old(s, ap);
329 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
330 return AVERROR(EIO);
331 }
332
333 get_be32(pb); /* header size */
334 get_be16(pb);
335 get_be32(pb);
336 get_be32(pb); /* number of headers */
337
338 for(;;) {
339 if (url_feof(pb))
340 return -1;
341 tag = get_le32(pb);
342 tag_size = get_be32(pb);
343 get_be16(pb);
344 #if 0
345 printf("tag=%c%c%c%c (%08x) size=%d\n",
346 (tag) & 0xff,
347 (tag >> 8) & 0xff,
348 (tag >> 16) & 0xff,
349 (tag >> 24) & 0xff,
350 tag,
351 tag_size);
352 #endif
353 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
354 return -1;
355 switch(tag) {
356 case MKTAG('P', 'R', 'O', 'P'):
357 /* file header */
358 get_be32(pb); /* max bit rate */
359 get_be32(pb); /* avg bit rate */
360 get_be32(pb); /* max packet size */
361 get_be32(pb); /* avg packet size */
362 get_be32(pb); /* nb packets */
363 get_be32(pb); /* duration */
364 get_be32(pb); /* preroll */
365 get_be32(pb); /* index offset */
366 get_be32(pb); /* data offset */
367 get_be16(pb); /* nb streams */
368 flags = get_be16(pb); /* flags */
369 break;
370 case MKTAG('C', 'O', 'N', 'T'):
371 get_str16(pb, s->title, sizeof(s->title));
372 get_str16(pb, s->author, sizeof(s->author));
373 get_str16(pb, s->copyright, sizeof(s->copyright));
374 get_str16(pb, s->comment, sizeof(s->comment));
375 break;
376 case MKTAG('M', 'D', 'P', 'R'):
377 st = av_new_stream(s, 0);
378 if (!st)
379 return AVERROR(ENOMEM);
380 st->id = get_be16(pb);
381 get_be32(pb); /* max bit rate */
382 st->codec->bit_rate = get_be32(pb); /* bit rate */
383 get_be32(pb); /* max packet size */
384 get_be32(pb); /* avg packet size */
385 start_time = get_be32(pb); /* start time */
386 get_be32(pb); /* preroll */
387 duration = get_be32(pb); /* duration */
388 st->start_time = start_time;
389 st->duration = duration;
390 get_str8(pb, buf, sizeof(buf)); /* desc */
391 get_str8(pb, buf, sizeof(buf)); /* mimetype */
392 st->codec->codec_type = CODEC_TYPE_DATA;
393 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, get_be32(pb)) < 0)
394 return -1;
395 break;
396 case MKTAG('D', 'A', 'T', 'A'):
397 goto header_end;
398 default:
399 /* unknown tag: skip it */
400 url_fskip(pb, tag_size - 10);
401 break;
402 }
403 }
404 header_end:
405 rm->nb_packets = get_be32(pb); /* number of packets */
406 if (!rm->nb_packets && (flags & 4))
407 rm->nb_packets = 3600 * 25;
408 get_be32(pb); /* next data header */
409 return 0;
410 }
411
412 static int get_num(ByteIOContext *pb, int *len)
413 {
414 int n, n1;
415
416 n = get_be16(pb);
417 (*len)-=2;
418 n &= 0x7FFF;
419 if (n >= 0x4000) {
420 return n - 0x4000;
421 } else {
422 n1 = get_be16(pb);
423 (*len)-=2;
424 return (n << 16) | n1;
425 }
426 }
427
428 /* multiple of 20 bytes for ra144 (ugly) */
429 #define RAW_PACKET_SIZE 1000
430
431 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
432 RMDemuxContext *rm = s->priv_data;
433 ByteIOContext *pb = s->pb;
434 int len, num, res, i;
435 AVStream *st;
436 uint32_t state=0xFFFFFFFF;
437
438 while(!url_feof(pb)){
439 *pos= url_ftell(pb) - 3;
440 if(rm->remaining_len > 0){
441 num= rm->current_stream;
442 len= rm->remaining_len;
443 *timestamp = AV_NOPTS_VALUE;
444 *flags= 0;
445 }else{
446 state= (state<<8) + get_byte(pb);
447
448 if(state == MKBETAG('I', 'N', 'D', 'X')){
449 len = get_be16(pb) - 6;
450 if(len<0)
451 continue;
452 goto skip;
453 }
454
455 if(state > (unsigned)0xFFFF || state < 12)
456 continue;
457 len=state;
458 state= 0xFFFFFFFF;
459
460 num = get_be16(pb);
461 *timestamp = get_be32(pb);
462 res= get_byte(pb); /* reserved */
463 *flags = get_byte(pb); /* flags */
464
465
466 len -= 12;
467 }
468 for(i=0;i<s->nb_streams;i++) {
469 st = s->streams[i];
470 if (num == st->id)
471 break;
472 }
473 if (i == s->nb_streams) {
474 skip:
475 /* skip packet if unknown number */
476 url_fskip(pb, len);
477 rm->remaining_len -= len;
478 continue;
479 }
480 *stream_index= i;
481
482 return len;
483 }
484 return -1;
485 }
486
487 static int rm_assemble_video_frame(AVFormatContext *s, ByteIOContext *pb,
488 RMDemuxContext *rm, RMStream *vst,
489 AVPacket *pkt, int len)
490 {
491 int hdr, seq, pic_num, len2, pos;
492 int type;
493
494 hdr = get_byte(pb); len--;
495 type = hdr >> 6;
496 switch(type){
497 case 0: // slice
498 case 2: // last slice
499 seq = get_byte(pb); len--;
500 len2 = get_num(pb, &len);
501 pos = get_num(pb, &len);
502 pic_num = get_byte(pb); len--;
503 rm->remaining_len = len;
504 break;
505 case 1: //whole frame
506 seq = get_byte(pb); len--;
507 if(av_new_packet(pkt, len + 9) < 0)
508 return AVERROR(EIO);
509 pkt->data[0] = 0;
510 AV_WL32(pkt->data + 1, 1);
511 AV_WL32(pkt->data + 5, 0);
512 get_buffer(pb, pkt->data + 9, len);
513 rm->remaining_len = 0;
514 return 0;
515 case 3: //frame as a part of packet
516 len2 = get_num(pb, &len);
517 pos = get_num(pb, &len);
518 pic_num = get_byte(pb); len--;
519 rm->remaining_len = len - len2;
520 if(av_new_packet(pkt, len2 + 9) < 0)
521 return AVERROR(EIO);
522 pkt->data[0] = 0;
523 AV_WL32(pkt->data + 1, 1);
524 AV_WL32(pkt->data + 5, 0);
525 get_buffer(pb, pkt->data + 9, len2);
526 return 0;
527 }
528 //now we have to deal with single slice
529
530 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
531 vst->slices = ((hdr & 0x3F) << 1) + 1;
532 vst->videobufsize = len2 + 8*vst->slices + 1;
533 av_free(vst->videobuf);
534 if(!(vst->videobuf = av_malloc(vst->videobufsize)))
535 return AVERROR(ENOMEM);
536 vst->videobufpos = 8*vst->slices + 1;
537 vst->cur_slice = 0;
538 vst->curpic_num = pic_num;
539 vst->pktpos = url_ftell(pb);
540 }
541 if(type == 2)
542 len = FFMIN(len, pos);
543
544 if(++vst->cur_slice > vst->slices)
545 return 1;
546 AV_WL32(vst->videobuf - 7 + 8*vst->cur_slice, 1);
547 AV_WL32(vst->videobuf - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
548 if(vst->videobufpos + len > vst->videobufsize)
549 return 1;
550 if (get_buffer(pb, vst->videobuf + vst->videobufpos, len) != len)
551 return AVERROR(EIO);
552 vst->videobufpos += len;
553 rm->remaining_len-= len;
554
555 if(type == 2 || (vst->videobufpos) == vst->videobufsize){
556 vst->videobuf[0] = vst->cur_slice-1;
557 if(av_new_packet(pkt, vst->videobufpos - 8*(vst->slices - vst->cur_slice)) < 0)
558 return AVERROR(ENOMEM);
559 memcpy(pkt->data, vst->videobuf, 1 + 8*vst->cur_slice);
560 memcpy(pkt->data + 1 + 8*vst->cur_slice, vst->videobuf + 1 + 8*vst->slices,
561 vst->videobufpos - 1 - 8*vst->slices);
562 pkt->pts = AV_NOPTS_VALUE;
563 pkt->pos = vst->pktpos;
564 return 0;
565 }
566
567 return 1;
568 }
569
570 static inline void
571 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
572 {
573 uint8_t *ptr;
574 int j;
575
576 if (st->codec->codec_id == CODEC_ID_AC3) {
577 ptr = pkt->data;
578 for (j=0;j<pkt->size;j+=2) {
579 FFSWAP(int, ptr[0], ptr[1]);
580 ptr += 2;
581 }
582 }
583 }
584
585 int
586 ff_rm_parse_packet (AVFormatContext *s, ByteIOContext *pb,
587 AVStream *st, int len, AVPacket *pkt,
588 int *seq, int *flags, int64_t *timestamp)
589 {
590 RMDemuxContext *rm = s->priv_data;
591
592 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
593 rm->current_stream= st->id;
594 if(rm_assemble_video_frame(s, pb, rm, st->priv_data, pkt, len) == 1)
595 return -1; //got partial frame
596 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
597 RMStream *ast = st->priv_data;
598
599 if ((st->codec->codec_id == CODEC_ID_RA_288) ||
600 (st->codec->codec_id == CODEC_ID_COOK) ||
601 (st->codec->codec_id == CODEC_ID_ATRAC3) ||
602 (st->codec->codec_id == CODEC_ID_SIPR)) {
603 int x;
604 int sps = ast->sub_packet_size;
605 int cfs = ast->coded_framesize;
606 int h = ast->sub_packet_h;
607 int y = ast->sub_packet_cnt;
608 int w = ast->audio_framesize;
609
610 if (*flags & 2)
611 y = ast->sub_packet_cnt = 0;
612 if (!y)
613 ast->audiotimestamp = *timestamp;
614
615 switch(st->codec->codec_id) {
616 case CODEC_ID_RA_288:
617 for (x = 0; x < h/2; x++)
618 get_buffer(pb, ast->audiobuf+x*2*w+y*cfs, cfs);
619 break;
620 case CODEC_ID_ATRAC3:
621 case CODEC_ID_COOK:
622 for (x = 0; x < w/sps; x++)
623 get_buffer(pb, ast->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
624 break;
625 }
626
627 if (++(ast->sub_packet_cnt) < h)
628 return -1;
629 else {
630 ast->sub_packet_cnt = 0;
631 rm->audio_stream_num = st->index;
632 rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
633 // Release first audio packet
634 av_new_packet(pkt, st->codec->block_align);
635 memcpy(pkt->data, ast->audiobuf, st->codec->block_align);
636 *timestamp = ast->audiotimestamp;
637 *flags = 2; // Mark first packet as keyframe
638 }
639 } else if (st->codec->codec_id == CODEC_ID_AAC) {
640 int x;
641 rm->audio_stream_num = st->index;
642 ast->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
643 if (ast->sub_packet_cnt) {
644 for (x = 0; x < ast->sub_packet_cnt; x++)
645 ast->sub_packet_lengths[x] = get_be16(pb);
646 // Release first audio packet
647 rm->audio_pkt_cnt = ast->sub_packet_cnt - 1;
648 av_get_packet(pb, pkt, ast->sub_packet_lengths[0]);
649 *flags = 2; // Mark first packet as keyframe
650 }
651 } else {
652 av_get_packet(pb, pkt, len);
653 rm_ac3_swap_bytes(st, pkt);
654 }
655 } else
656 av_get_packet(pb, pkt, len);
657
658 if( (st->discard >= AVDISCARD_NONKEY && !(*flags&2))
659 || st->discard >= AVDISCARD_ALL){
660 av_free_packet(pkt);
661 return -1;
662 }
663
664 pkt->stream_index = st->index;
665
666 #if 0
667 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
668 if(st->codec->codec_id == CODEC_ID_RV20){
669 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
670 av_log(NULL, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
671
672 seq |= (*timestamp&~0x3FFF);
673 if(seq - *timestamp > 0x2000) seq -= 0x4000;
674 if(seq - *timestamp < -0x2000) seq += 0x4000;
675 }
676 }
677 #endif
678
679 pkt->pts= *timestamp;
680 if (*flags & 2)
681 pkt->flags |= PKT_FLAG_KEY;
682
683 return st->codec->codec_type == CODEC_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
684 }
685
686 void
687 ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
688 AVStream *st, AVPacket *pkt)
689 {
690 RMDemuxContext *rm = s->priv_data;
691 RMStream *ast = st->priv_data;
692
693 assert (rm->audio_pkt_cnt > 0);
694
695 if (st->codec->codec_id == CODEC_ID_AAC)
696 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
697 else {
698 av_new_packet(pkt, st->codec->block_align);
699 memcpy(pkt->data, ast->audiobuf + st->codec->block_align *
700 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
701 st->codec->block_align);
702 }
703 rm->audio_pkt_cnt--;
704 pkt->flags = 0;
705 pkt->stream_index = st->index;
706 }
707
708 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
709 {
710 RMDemuxContext *rm = s->priv_data;
711 ByteIOContext *pb = s->pb;
712 AVStream *st;
713 int i, len;
714 int64_t timestamp, pos;
715 int flags;
716
717 if (rm->audio_pkt_cnt) {
718 // If there are queued audio packet return them first
719 st = s->streams[rm->audio_stream_num];
720 ff_rm_retrieve_cache(s, s->pb, st, pkt);
721 } else if (rm->old_format) {
722 RMStream *ast;
723
724 st = s->streams[0];
725 ast = st->priv_data;
726 if (st->codec->codec_id == CODEC_ID_RA_288) {
727 int x, y;
728
729 for (y = 0; y < ast->sub_packet_h; y++)
730 for (x = 0; x < ast->sub_packet_h/2; x++)
731 if (get_buffer(pb, ast->audiobuf+x*2*ast->audio_framesize+y*ast->coded_framesize, ast->coded_framesize) <= 0)
732 return AVERROR(EIO);
733 rm->audio_stream_num = 0;
734 rm->audio_pkt_cnt = ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - 1;
735 // Release first audio packet
736 av_new_packet(pkt, st->codec->block_align);
737 memcpy(pkt->data, ast->audiobuf, st->codec->block_align);
738 pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
739 pkt->stream_index = 0;
740 } else {
741 /* just read raw bytes */
742 len = RAW_PACKET_SIZE;
743 len= av_get_packet(pb, pkt, len);
744 pkt->stream_index = 0;
745 if (len <= 0) {
746 return AVERROR(EIO);
747 }
748 pkt->size = len;
749 }
750 rm_ac3_swap_bytes(st, pkt);
751 } else {
752 int seq=1;
753 resync:
754 len=sync(s, &timestamp, &flags, &i, &pos);
755 if(len<0)
756 return AVERROR(EIO);
757 st = s->streams[i];
758
759 if (ff_rm_parse_packet (s, s->pb, st, len, pkt, &seq, &flags, &timestamp) < 0)
760 goto resync;
761
762 if((flags&2) && (seq&0x7F) == 1)
763 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
764 }
765
766 return 0;
767 }
768
769 static int rm_read_close(AVFormatContext *s)
770 {
771 int i;
772
773 for (i=0;i<s->nb_streams;i++)
774 ff_rm_free_rmstream(s->streams[i]->priv_data);
775
776 return 0;
777 }
778
779 static int rm_probe(AVProbeData *p)
780 {
781 /* check file header */
782 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
783 p->buf[2] == 'M' && p->buf[3] == 'F' &&
784 p->buf[4] == 0 && p->buf[5] == 0) ||
785 (p->buf[0] == '.' && p->buf[1] == 'r' &&
786 p->buf[2] == 'a' && p->buf[3] == 0xfd))
787 return AVPROBE_SCORE_MAX;
788 else
789 return 0;
790 }
791
792 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
793 int64_t *ppos, int64_t pos_limit)
794 {
795 RMDemuxContext *rm = s->priv_data;
796 int64_t pos, dts;
797 int stream_index2, flags, len, h;
798
799 pos = *ppos;
800
801 if(rm->old_format)
802 return AV_NOPTS_VALUE;
803
804 url_fseek(s->pb, pos, SEEK_SET);
805 rm->remaining_len=0;
806 for(;;){
807 int seq=1;
808 AVStream *st;
809
810 len=sync(s, &dts, &flags, &stream_index2, &pos);
811 if(len<0)
812 return AV_NOPTS_VALUE;
813
814 st = s->streams[stream_index2];
815 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
816 h= get_byte(s->pb); len--;
817 if(!(h & 0x40)){
818 seq = get_byte(s->pb); len--;
819 }
820 }
821
822 if((flags&2) && (seq&0x7F) == 1){
823 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
824 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
825 if(stream_index2 == stream_index)
826 break;
827 }
828
829 url_fskip(s->pb, len);
830 }
831 *ppos = pos;
832 return dts;
833 }
834
835 AVInputFormat rm_demuxer = {
836 "rm",
837 NULL_IF_CONFIG_SMALL("RM format"),
838 sizeof(RMDemuxContext),
839 rm_probe,
840 rm_read_header,
841 rm_read_packet,
842 rm_read_close,
843 NULL,
844 rm_read_dts,
845 };
846
847 AVInputFormat rdt_demuxer = {
848 "rdt",
849 NULL_IF_CONFIG_SMALL("RDT demuxer"),
850 sizeof(RMDemuxContext),
851 NULL, NULL, NULL, rm_read_close, NULL, NULL
852 };