~noskcaj/ubuntu/saucy/libav/merge0.8.7-1

« back to all changes in this revision

Viewing changes to libavformat/rmdec.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:30:00 UTC
  • mfrom: (1.2.8) (1.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20120112223000-cmfo7w78q13i2fd9
Tags: 4:0.8~beta2-1ubuntu1
* Merge from debian, remaining changes:
  - don't build against libdirac, lame, libopenjpeg, librtmp, 
    x264, and xvid  (all in universe)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "libavutil/intreadwrite.h"
24
24
#include "libavutil/dict.h"
25
25
#include "avformat.h"
 
26
#include "internal.h"
26
27
#include "riff.h"
27
28
#include "rm.h"
28
29
 
 
30
#define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/Atrac
 
31
#define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
 
32
#define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
 
33
#define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
 
34
#define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
 
35
#define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
 
36
 
29
37
struct RMStream {
30
38
    AVPacket pkt;      ///< place to store merged video frame / reordered audio data
31
39
    int videobufsize;  ///< current assembled frame size
39
47
    int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
40
48
    int audio_framesize; /// Audio frame size from container
41
49
    int sub_packet_lengths[16]; /// Length of each subpacket
 
50
    int32_t deint_id;  ///< deinterleaver used in audio stream
42
51
};
43
52
 
44
53
typedef struct {
147
156
        st->codec->channels = 1;
148
157
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
149
158
        st->codec->codec_id = CODEC_ID_RA_144;
 
159
        ast->deint_id = DEINT_ID_INT0;
150
160
    } else {
151
161
        int flavor, sub_packet_h, coded_framesize, sub_packet_size;
152
162
        int codecdata_length;
172
182
        avio_rb32(pb);
173
183
        st->codec->channels = avio_rb16(pb);
174
184
        if (version == 5) {
175
 
            avio_rb32(pb);
 
185
            ast->deint_id = avio_rl32(pb);
176
186
            avio_read(pb, buf, 4);
177
187
            buf[4] = 0;
178
188
        } else {
179
189
            get_str8(pb, buf, sizeof(buf)); /* desc */
 
190
            ast->deint_id = AV_RL32(buf);
180
191
            get_str8(pb, buf, sizeof(buf)); /* desc */
181
192
        }
182
193
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
183
194
        st->codec->codec_tag  = AV_RL32(buf);
184
195
        st->codec->codec_id   = ff_codec_get_id(ff_rm_codec_tags,
185
196
                                                st->codec->codec_tag);
 
197
 
186
198
        switch (st->codec->codec_id) {
187
199
        case CODEC_ID_AC3:
188
200
            st->need_parsing = AVSTREAM_PARSE_FULL;
191
203
            st->codec->extradata_size= 0;
192
204
            ast->audio_framesize = st->codec->block_align;
193
205
            st->codec->block_align = coded_framesize;
194
 
 
195
 
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
196
 
                av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
197
 
                return -1;
198
 
            }
199
 
 
200
 
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
201
206
            break;
202
207
        case CODEC_ID_COOK:
203
208
        case CODEC_ID_ATRAC3:
228
233
            }
229
234
            if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
230
235
                return ret;
231
 
 
232
 
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
233
 
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
234
 
                return -1;
235
 
            }
236
 
 
237
 
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
238
236
            break;
239
237
        case CODEC_ID_AAC:
240
238
            avio_rb16(pb); avio_r8(pb);
254
252
        default:
255
253
            av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
256
254
        }
 
255
        if (ast->deint_id == DEINT_ID_INT4 ||
 
256
            ast->deint_id == DEINT_ID_GENR ||
 
257
            ast->deint_id == DEINT_ID_SIPR) {
 
258
            if (st->codec->block_align <= 0 ||
 
259
                ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
 
260
                ast->audio_framesize * sub_packet_h < st->codec->block_align)
 
261
                return AVERROR_INVALIDDATA;
 
262
            if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
 
263
                return AVERROR(ENOMEM);
 
264
        }
 
265
        switch (ast->deint_id) {
 
266
        case DEINT_ID_INT4:
 
267
            if (ast->coded_framesize > ast->audio_framesize ||
 
268
                ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
 
269
                return AVERROR_INVALIDDATA;
 
270
            break;
 
271
        case DEINT_ID_GENR:
 
272
            if (ast->sub_packet_size <= 0 ||
 
273
                ast->sub_packet_size > ast->audio_framesize)
 
274
                return AVERROR_INVALIDDATA;
 
275
            break;
 
276
        case DEINT_ID_SIPR:
 
277
        case DEINT_ID_INT0:
 
278
        case DEINT_ID_VBRS:
 
279
        case DEINT_ID_VBRF:
 
280
            break;
 
281
        default:
 
282
            av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
 
283
            return AVERROR_INVALIDDATA;
 
284
        }
 
285
 
257
286
        if (read_all) {
258
287
            avio_r8(pb);
259
288
            avio_r8(pb);
273
302
    int64_t codec_pos;
274
303
    int ret;
275
304
 
276
 
    av_set_pts_info(st, 64, 1, 1000);
 
305
    avpriv_set_pts_info(st, 64, 1, 1000);
277
306
    codec_pos = avio_tell(pb);
278
307
    v = avio_rb32(pb);
279
308
    if (v == MKTAG(0xfd, 'a', 'r', '.')) {
293
322
//        av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
294
323
        if (st->codec->codec_id == CODEC_ID_NONE)
295
324
            goto fail1;
296
 
        st->codec->width = avio_rb16(pb);
 
325
        st->codec->width  = avio_rb16(pb);
297
326
        st->codec->height = avio_rb16(pb);
298
 
        st->codec->time_base.num= 1;
299
 
        fps= avio_rb16(pb);
 
327
        avio_skip(pb, 2); // looks like bits per sample
 
328
        avio_skip(pb, 4); // always zero?
300
329
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
301
 
        avio_rb32(pb);
302
 
        avio_skip(pb, 2);
303
 
        avio_rb16(pb);
 
330
        st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
 
331
        fps = avio_rb32(pb);
304
332
 
305
333
        if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
306
334
            return ret;
307
335
 
308
 
//        av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
309
 
        st->codec->time_base.den = fps * st->codec->time_base.num;
310
 
        //XXX: do we really need that?
311
 
        switch(st->codec->extradata[4]>>4){
312
 
        case 1: st->codec->codec_id = CODEC_ID_RV10; break;
313
 
        case 2: st->codec->codec_id = CODEC_ID_RV20; break;
314
 
        case 3: st->codec->codec_id = CODEC_ID_RV30; break;
315
 
        case 4: st->codec->codec_id = CODEC_ID_RV40; break;
316
 
        default:
317
 
            av_log(st->codec, AV_LOG_ERROR, "extra:%02X %02X %02X %02X %02X\n", st->codec->extradata[0], st->codec->extradata[1], st->codec->extradata[2], st->codec->extradata[3], st->codec->extradata[4]);
318
 
            goto fail1;
319
 
        }
 
336
        av_reduce(&st->r_frame_rate.den, &st->r_frame_rate.num,
 
337
                  0x10000, fps, (1 << 30) - 1);
 
338
        st->avg_frame_rate = st->r_frame_rate;
320
339
    }
321
340
 
322
341
skip:
371
390
    return 0;
372
391
}
373
392
 
374
 
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
 
393
static int rm_read_header_old(AVFormatContext *s)
375
394
{
376
395
    RMDemuxContext *rm = s->priv_data;
377
396
    AVStream *st;
378
397
 
379
398
    rm->old_format = 1;
380
 
    st = av_new_stream(s, 0);
 
399
    st = avformat_new_stream(s, NULL);
381
400
    if (!st)
382
401
        return -1;
383
402
    st->priv_data = ff_rm_alloc_rmstream();
399
418
    tag = avio_rl32(pb);
400
419
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
401
420
        /* very old .ra format */
402
 
        return rm_read_header_old(s, ap);
 
421
        return rm_read_header_old(s);
403
422
    } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
404
423
        return AVERROR(EIO);
405
424
    }
443
462
            rm_read_metadata(s, 1);
444
463
            break;
445
464
        case MKTAG('M', 'D', 'P', 'R'):
446
 
            st = av_new_stream(s, 0);
 
465
            st = avformat_new_stream(s, NULL);
447
466
            if (!st)
448
467
                return AVERROR(ENOMEM);
449
468
            st->id = avio_rb16(pb);
578
597
 
579
598
static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
580
599
                                   RMDemuxContext *rm, RMStream *vst,
581
 
                                   AVPacket *pkt, int len, int *pseq)
 
600
                                   AVPacket *pkt, int len, int *pseq,
 
601
                                   int64_t *timestamp)
582
602
{
583
603
    int hdr, seq, pic_num, len2, pos;
584
604
    int type;
598
618
        return -1;
599
619
    rm->remaining_len = len;
600
620
    if(type&1){     // frame, not slice
601
 
        if(type == 3)  // frame as a part of packet
 
621
        if(type == 3){  // frame as a part of packet
602
622
            len= len2;
 
623
            *timestamp = pos;
 
624
        }
603
625
        if(rm->remaining_len < len)
604
626
            return -1;
605
627
        rm->remaining_len -= len;
639
661
    vst->videobufpos += len;
640
662
    rm->remaining_len-= len;
641
663
 
642
 
    if(type == 2 || (vst->videobufpos) == vst->videobufsize){
 
664
    if (type == 2 || vst->videobufpos == vst->videobufsize) {
643
665
        vst->pkt.data[0] = vst->cur_slice-1;
644
666
        *pkt= vst->pkt;
645
667
        vst->pkt.data= NULL;
707
729
 
708
730
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
709
731
        rm->current_stream= st->id;
710
 
        if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
 
732
        if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp))
711
733
            return -1; //got partial frame
712
734
    } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
713
 
        if ((st->codec->codec_id == CODEC_ID_RA_288) ||
714
 
            (st->codec->codec_id == CODEC_ID_COOK) ||
715
 
            (st->codec->codec_id == CODEC_ID_ATRAC3) ||
716
 
            (st->codec->codec_id == CODEC_ID_SIPR)) {
 
735
        if ((ast->deint_id == DEINT_ID_GENR) ||
 
736
            (ast->deint_id == DEINT_ID_INT4) ||
 
737
            (ast->deint_id == DEINT_ID_SIPR)) {
717
738
            int x;
718
739
            int sps = ast->sub_packet_size;
719
740
            int cfs = ast->coded_framesize;
726
747
            if (!y)
727
748
                ast->audiotimestamp = timestamp;
728
749
 
729
 
            switch(st->codec->codec_id) {
730
 
                case CODEC_ID_RA_288:
 
750
            switch (ast->deint_id) {
 
751
                case DEINT_ID_INT4:
731
752
                    for (x = 0; x < h/2; x++)
732
753
                        avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
733
754
                    break;
734
 
                case CODEC_ID_ATRAC3:
735
 
                case CODEC_ID_COOK:
 
755
                case DEINT_ID_GENR:
736
756
                    for (x = 0; x < w/sps; x++)
737
757
                        avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
738
758
                    break;
739
 
                case CODEC_ID_SIPR:
 
759
                case DEINT_ID_SIPR:
740
760
                    avio_read(pb, ast->pkt.data + y * w, w);
741
761
                    break;
742
762
            }
743
763
 
744
764
            if (++(ast->sub_packet_cnt) < h)
745
765
                return -1;
746
 
            if (st->codec->codec_id == CODEC_ID_SIPR)
 
766
            if (ast->deint_id == DEINT_ID_SIPR)
747
767
                ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
748
768
 
749
769
             ast->sub_packet_cnt = 0;
750
770
             rm->audio_stream_num = st->index;
751
771
             rm->audio_pkt_cnt = h * w / st->codec->block_align;
752
 
        } else if (st->codec->codec_id == CODEC_ID_AAC) {
 
772
        } else if ((ast->deint_id == DEINT_ID_VBRF) ||
 
773
                   (ast->deint_id == DEINT_ID_VBRS)) {
753
774
            int x;
754
775
            rm->audio_stream_num = st->index;
755
776
            ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
782
803
    }
783
804
#endif
784
805
 
785
 
    pkt->pts= timestamp;
 
806
    pkt->pts = timestamp;
786
807
    if (flags & 2)
787
808
        pkt->flags |= AV_PKT_FLAG_KEY;
788
809
 
797
818
 
798
819
    assert (rm->audio_pkt_cnt > 0);
799
820
 
800
 
    if (st->codec->codec_id == CODEC_ID_AAC)
 
821
    if (ast->deint_id == DEINT_ID_VBRF ||
 
822
        ast->deint_id == DEINT_ID_VBRS)
801
823
        av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
802
824
    else {
803
825
        av_new_packet(pkt, st->codec->block_align);
935
957
}
936
958
 
937
959
AVInputFormat ff_rm_demuxer = {
938
 
    "rm",
939
 
    NULL_IF_CONFIG_SMALL("RealMedia format"),
940
 
    sizeof(RMDemuxContext),
941
 
    rm_probe,
942
 
    rm_read_header,
943
 
    rm_read_packet,
944
 
    rm_read_close,
945
 
    NULL,
946
 
    rm_read_dts,
 
960
    .name           = "rm",
 
961
    .long_name      = NULL_IF_CONFIG_SMALL("RealMedia format"),
 
962
    .priv_data_size = sizeof(RMDemuxContext),
 
963
    .read_probe     = rm_probe,
 
964
    .read_header    = rm_read_header,
 
965
    .read_packet    = rm_read_packet,
 
966
    .read_close     = rm_read_close,
 
967
    .read_timestamp = rm_read_dts,
947
968
};
948
969
 
949
970
AVInputFormat ff_rdt_demuxer = {
950
 
    "rdt",
951
 
    NULL_IF_CONFIG_SMALL("RDT demuxer"),
952
 
    sizeof(RMDemuxContext),
953
 
    NULL,
954
 
    NULL,
955
 
    NULL,
956
 
    rm_read_close,
 
971
    .name           = "rdt",
 
972
    .long_name      = NULL_IF_CONFIG_SMALL("RDT demuxer"),
 
973
    .priv_data_size = sizeof(RMDemuxContext),
 
974
    .read_close     = rm_read_close,
 
975
    .flags          = AVFMT_NOFILE,
957
976
};