Revision 625daac4

View differences:

libavcodec/flac.c
19 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
 */
21 21

  
22
#include "libavutil/crc.h"
22 23
#include "flac.h"
24
#include "flacdata.h"
25

  
26
static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
27

  
28
static int64_t get_utf8(GetBitContext *gb)
29
{
30
    int64_t val;
31
    GET_UTF8(val, get_bits(gb, 8), return -1;)
32
    return val;
33
}
34

  
35
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
36
                                FLACFrameInfo *fi)
37
{
38
    int bs_code, sr_code, bps_code;
39

  
40
    /* frame sync code */
41
    skip_bits(gb, 16);
42

  
43
    /* block size and sample rate codes */
44
    bs_code = get_bits(gb, 4);
45
    sr_code = get_bits(gb, 4);
46

  
47
    /* channels and decorrelation */
48
    fi->ch_mode = get_bits(gb, 4);
49
    if (fi->ch_mode < FLAC_MAX_CHANNELS) {
50
        fi->channels = fi->ch_mode + 1;
51
        fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
52
    } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
53
        fi->channels = 2;
54
    } else {
55
        av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode);
56
        return -1;
57
    }
58

  
59
    /* bits per sample */
60
    bps_code = get_bits(gb, 3);
61
    if (bps_code == 3 || bps_code == 7) {
62
        av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n",
63
               bps_code);
64
        return -1;
65
    }
66
    fi->bps = sample_size_table[bps_code];
67

  
68
    /* reserved bit */
69
    if (get_bits1(gb)) {
70
        av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
71
        return -1;
72
    }
73

  
74
    /* sample or frame count */
75
    if (get_utf8(gb) < 0) {
76
        av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n");
77
        return -1;
78
    }
79

  
80
    /* blocksize */
81
    if (bs_code == 0) {
82
        av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n");
83
        return -1;
84
    } else if (bs_code == 6) {
85
        fi->blocksize = get_bits(gb, 8) + 1;
86
    } else if (bs_code == 7) {
87
        fi->blocksize = get_bits(gb, 16) + 1;
88
    } else {
89
        fi->blocksize = ff_flac_blocksize_table[bs_code];
90
    }
91

  
92
    /* sample rate */
93
    if (sr_code < 12) {
94
        fi->samplerate = ff_flac_sample_rate_table[sr_code];
95
    } else if (sr_code == 12) {
96
        fi->samplerate = get_bits(gb, 8) * 1000;
97
    } else if (sr_code == 13) {
98
        fi->samplerate = get_bits(gb, 16);
99
    } else if (sr_code == 14) {
100
        fi->samplerate = get_bits(gb, 16) * 10;
101
    } else {
102
        av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n",
103
               sr_code);
104
        return -1;
105
    }
106

  
107
    /* header CRC-8 check */
108
    skip_bits(gb, 8);
109
    if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
110
               get_bits_count(gb)/8)) {
111
        av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n");
112
        return -1;
113
    }
114

  
115
    return 0;
116
}
23 117

  
24 118
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
25 119
{
libavcodec/flac.h
28 28
#define AVCODEC_FLAC_H
29 29

  
30 30
#include "avcodec.h"
31
#include "get_bits.h"
31 32

  
32 33
#define FLAC_STREAMINFO_SIZE   34
33 34
#define FLAC_MAX_CHANNELS       8
......
120 121
 */
121 122
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
122 123

  
124
/**
125
 * Validate and decode a frame header.
126
 * @param      avctx AVCodecContext to use as av_log() context
127
 * @param      gb    GetBitContext from which to read frame header
128
 * @param[out] fi    frame information
129
 * @return non-zero on error, 0 if ok
130
 */
131
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
132
                                FLACFrameInfo *fi);
123 133
#endif /* AVCODEC_FLAC_H */
libavcodec/flacdec.c
67 67
    unsigned int allocated_bitstream_size;
68 68
} FLACContext;
69 69

  
70
static const int sample_size_table[] =
71
{ 0, 8, 12, 0, 16, 20, 24, 0 };
72

  
73
static int64_t get_utf8(GetBitContext *gb)
74
{
75
    int64_t val;
76
    GET_UTF8(val, get_bits(gb, 8), return -1;)
77
    return val;
78
}
79

  
80 70
static void allocate_buffers(FLACContext *s);
81 71

  
82 72
int ff_flac_is_extradata_valid(AVCodecContext *avctx,
......
480 470
    return 0;
481 471
}
482 472

  
483
/**
484
 * Validate and decode a frame header.
485
 * @param      avctx AVCodecContext to use as av_log() context
486
 * @param      gb    GetBitContext from which to read frame header
487
 * @param[out] fi    frame information
488
 * @return non-zero on error, 0 if ok
489
 */
490
static int decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
491
                               FLACFrameInfo *fi)
492
{
493
    int bs_code, sr_code, bps_code;
494

  
495
    /* frame sync code */
496
    skip_bits(gb, 16);
497

  
498
    /* block size and sample rate codes */
499
    bs_code = get_bits(gb, 4);
500
    sr_code = get_bits(gb, 4);
501

  
502
    /* channels and decorrelation */
503
    fi->ch_mode = get_bits(gb, 4);
504
    if (fi->ch_mode < FLAC_MAX_CHANNELS) {
505
        fi->channels = fi->ch_mode + 1;
506
        fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
507
    } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
508
        fi->channels = 2;
509
    } else {
510
        av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode);
511
        return -1;
512
    }
513

  
514
    /* bits per sample */
515
    bps_code = get_bits(gb, 3);
516
    if (bps_code == 3 || bps_code == 7) {
517
        av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n",
518
               bps_code);
519
        return -1;
520
    }
521
    fi->bps = sample_size_table[bps_code];
522

  
523
    /* reserved bit */
524
    if (get_bits1(gb)) {
525
        av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
526
        return -1;
527
    }
528

  
529
    /* sample or frame count */
530
    if (get_utf8(gb) < 0) {
531
        av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n");
532
        return -1;
533
    }
534

  
535
    /* blocksize */
536
    if (bs_code == 0) {
537
        av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n");
538
        return -1;
539
    } else if (bs_code == 6) {
540
        fi->blocksize = get_bits(gb, 8) + 1;
541
    } else if (bs_code == 7) {
542
        fi->blocksize = get_bits(gb, 16) + 1;
543
    } else {
544
        fi->blocksize = ff_flac_blocksize_table[bs_code];
545
    }
546

  
547
    /* sample rate */
548
    if (sr_code < 12) {
549
        fi->samplerate = ff_flac_sample_rate_table[sr_code];
550
    } else if (sr_code == 12) {
551
        fi->samplerate = get_bits(gb, 8) * 1000;
552
    } else if (sr_code == 13) {
553
        fi->samplerate = get_bits(gb, 16);
554
    } else if (sr_code == 14) {
555
        fi->samplerate = get_bits(gb, 16) * 10;
556
    } else {
557
        av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n",
558
               sr_code);
559
        return -1;
560
    }
561

  
562
    /* header CRC-8 check */
563
    skip_bits(gb, 8);
564
    if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
565
               get_bits_count(gb)/8)) {
566
        av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n");
567
        return -1;
568
    }
569

  
570
    return 0;
571
}
572

  
573 473
static int decode_frame(FLACContext *s)
574 474
{
575 475
    int i;
576 476
    GetBitContext *gb = &s->gb;
577 477
    FLACFrameInfo fi;
578 478

  
579
    if (decode_frame_header(s->avctx, gb, &fi)) {
479
    if (ff_flac_decode_frame_header(s->avctx, gb, &fi)) {
580 480
        av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
581 481
        return -1;
582 482
    }

Also available in: Unified diff