comparison flacdec.c @ 9054:7eedb5796dd8 libavcodec

Use a shared function to validate FLAC extradata.
author jbr
date Thu, 26 Feb 2009 02:29:24 +0000
parents 49817775b44f
children 15515243f476
comparison
equal deleted inserted replaced
9053:c6bd71120376 9054:7eedb5796dd8
94 GET_UTF8(val, get_bits(gb, 8), return -1;) 94 GET_UTF8(val, get_bits(gb, 8), return -1;)
95 return val; 95 return val;
96 } 96 }
97 97
98 static void allocate_buffers(FLACContext *s); 98 static void allocate_buffers(FLACContext *s);
99 static int metadata_parse(FLACContext *s); 99
100 int ff_flac_is_extradata_valid(AVCodecContext *avctx,
101 enum FLACExtradataFormat *format,
102 uint8_t **streaminfo_start)
103 {
104 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
105 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
106 return 0;
107 }
108 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
109 /* extradata contains STREAMINFO only */
110 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
111 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
112 FLAC_STREAMINFO_SIZE-avctx->extradata_size);
113 }
114 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
115 *streaminfo_start = avctx->extradata;
116 } else {
117 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
118 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
119 return 0;
120 }
121 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
122 *streaminfo_start = &avctx->extradata[8];
123 }
124 return 1;
125 }
100 126
101 static av_cold int flac_decode_init(AVCodecContext *avctx) 127 static av_cold int flac_decode_init(AVCodecContext *avctx)
102 { 128 {
129 enum FLACExtradataFormat format;
130 uint8_t *streaminfo;
103 FLACContext *s = avctx->priv_data; 131 FLACContext *s = avctx->priv_data;
104 s->avctx = avctx; 132 s->avctx = avctx;
105 133
106 avctx->sample_fmt = SAMPLE_FMT_S16; 134 avctx->sample_fmt = SAMPLE_FMT_S16;
107 135
108 if (avctx->extradata_size > 4) { 136 /* for now, the raw FLAC header is allowed to be passed to the decoder as
137 frame data instead of extradata. */
138 if (!avctx->extradata)
139 return 0;
140
141 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
142 return -1;
143
109 /* initialize based on the demuxer-supplied streamdata header */ 144 /* initialize based on the demuxer-supplied streamdata header */
110 if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
111 ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, 145 ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s,
112 avctx->extradata); 146 streaminfo);
113 allocate_buffers(s); 147 allocate_buffers(s);
114 } else {
115 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
116 metadata_parse(s);
117 }
118 }
119 148
120 return 0; 149 return 0;
121 } 150 }
122 151
123 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) 152 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)