Revision cf16c17a

View differences:

libavformat/rmdec.c
48 48
    int audio_pkt_cnt; ///< Output packet counter
49 49
} RMDemuxContext;
50 50

  
51
static const unsigned char sipr_swaps[38][2] = {
52
    {  0, 63 }, {  1, 22 }, {  2, 44 }, {  3, 90 },
53
    {  5, 81 }, {  7, 31 }, {  8, 86 }, {  9, 58 },
54
    { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
55
    { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
56
    { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
57
    { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
58
    { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
59
    { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
60
    { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
61
    { 67, 83 }, { 77, 80 }
62
};
63

  
64
static const unsigned char sipr_subpk_size[4] = { 29, 19, 37, 20 };
65

  
51 66
static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
52 67
{
53 68
    int i;
......
171 186
                return -1;
172 187
            }
173 188

  
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 189
            if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
180 190
            else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
181 191
            else st->codec->codec_id = CODEC_ID_ATRAC3;
192

  
193
            ast->audio_framesize = st->codec->block_align;
194
            if (st->codec->codec_id == CODEC_ID_SIPR) {
195
                if (flavor > 3) {
196
                    av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
197
                           flavor);
198
                    return -1;
199
                }
200
                st->codec->block_align = sipr_subpk_size[flavor];
201
            } else {
202
                if(sub_packet_size <= 0){
203
                    av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
204
                    return -1;
205
                }
206
                st->codec->block_align = ast->sub_packet_size;
207
            }
182 208
            st->codec->extradata_size= codecdata_length;
183 209
            st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
184 210
            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 211

  
188 212
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
189 213
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
......
637 661
    }
638 662
}
639 663

  
664
/** perform 4-bit block reordering for SIPR data */
665
static void
666
rm_reorder_sipr_data (RMStream *ast)
667
{
668
    int n, bs = ast->sub_packet_h * ast->audio_framesize * 2 / 96; // nibbles per subpacket
669

  
670
    for (n = 0; n < 38; n++) {
671
        int j;
672
        int i = bs * sipr_swaps[n][0];
673
        int o = bs * sipr_swaps[n][1];
674
        uint8_t *buf = ast->pkt.data;
675

  
676
        /* swap 4bit-nibbles of block 'i' with 'o' */
677
        for (j = 0; j < bs; j++, i++, o++) {
678
            int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
679
                y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
680

  
681
            buf[o >> 1] = (x << (4 * (o & 1))) |
682
                (buf[o >> 1] & (0xF << (4 * !(o & 1))));
683
            buf[i >> 1] = (y << (4 * (i & 1))) |
684
                (buf[i >> 1] & (0xF << (4 * !(i & 1))));
685
        }
686
    }
687
}
688

  
640 689
int
641 690
ff_rm_parse_packet (AVFormatContext *s, ByteIOContext *pb,
642 691
                    AVStream *st, RMStream *ast, int len, AVPacket *pkt,
......
675 724
                    for (x = 0; x < w/sps; x++)
676 725
                        get_buffer(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
677 726
                    break;
727
                case CODEC_ID_SIPR:
728
                    get_buffer(pb, ast->pkt.data + y * w, w);
729
                    break;
678 730
            }
679 731

  
680 732
            if (++(ast->sub_packet_cnt) < h)
681 733
                return -1;
734
            if (st->codec->codec_id == CODEC_ID_SIPR)
735
                rm_reorder_sipr_data(ast);
736

  
682 737
             ast->sub_packet_cnt = 0;
683 738
             rm->audio_stream_num = st->index;
684 739
             rm->audio_pkt_cnt = h * w / st->codec->block_align;

Also available in: Unified diff