00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00028 #include "get_bits.h"
00029 #include "parser.h"
00030 #include "xiph.h"
00031 #include "vorbis_parser.h"
00032 
00033 static int parse_id_header(AVCodecContext *avctx, VorbisParseContext *s,
00034                            const uint8_t *buf, int buf_size)
00035 {
00036     
00037     if (buf_size < 30) {
00038         av_log(avctx, AV_LOG_ERROR, "Id header is too short\n");
00039         return AVERROR_INVALIDDATA;
00040     }
00041 
00042     
00043     if (buf[0] != 1) {
00044         av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Id header\n");
00045         return AVERROR_INVALIDDATA;
00046     }
00047 
00048     
00049     if (memcmp(&buf[1], "vorbis", 6)) {
00050         av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Id header\n");
00051         return AVERROR_INVALIDDATA;
00052     }
00053 
00054     if (!(buf[29] & 0x1)) {
00055         av_log(avctx, AV_LOG_ERROR, "Invalid framing bit in Id header\n");
00056         return AVERROR_INVALIDDATA;
00057     }
00058 
00059     s->blocksize[0] = 1 << (buf[28] & 0xF);
00060     s->blocksize[1] = 1 << (buf[28] >>  4);
00061 
00062     return 0;
00063 }
00064 
00065 static int parse_setup_header(AVCodecContext *avctx, VorbisParseContext *s,
00066                               const uint8_t *buf, int buf_size)
00067 {
00068     GetBitContext gb, gb0;
00069     uint8_t *rev_buf;
00070     int i, ret = 0;
00071     int got_framing_bit, mode_count, got_mode_header, last_mode_count = 0;
00072 
00073     
00074     if (buf_size < 7) {
00075         av_log(avctx, AV_LOG_ERROR, "Setup header is too short\n");
00076         return AVERROR_INVALIDDATA;
00077     }
00078 
00079     
00080     if (buf[0] != 5) {
00081         av_log(avctx, AV_LOG_ERROR, "Wrong packet type in Setup header\n");
00082         return AVERROR_INVALIDDATA;
00083     }
00084 
00085     
00086     if (memcmp(&buf[1], "vorbis", 6)) {
00087         av_log(avctx, AV_LOG_ERROR, "Invalid packet signature in Setup header\n");
00088         return AVERROR_INVALIDDATA;
00089     }
00090 
00091     
00092     if (!(rev_buf = av_malloc(buf_size))) {
00093         av_log(avctx, AV_LOG_ERROR, "Out of memory\n");
00094         return AVERROR(ENOMEM);
00095     }
00096     for (i = 0; i < buf_size; i++)
00097         rev_buf[i] = buf[buf_size - 1 - i];
00098     init_get_bits(&gb, rev_buf, buf_size * 8);
00099 
00100     got_framing_bit = 0;
00101     while (get_bits_left(&gb) > 97) {
00102         if (get_bits1(&gb)) {
00103             got_framing_bit = get_bits_count(&gb);
00104             break;
00105         }
00106     }
00107     if (!got_framing_bit) {
00108         av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
00109         ret = AVERROR_INVALIDDATA;
00110         goto bad_header;
00111     }
00112 
00113     
00114 
00115 
00116 
00117 
00118 
00119     mode_count = 0;
00120     got_mode_header = 0;
00121     while (get_bits_left(&gb) >= 97) {
00122         if (get_bits(&gb, 8) > 63 || get_bits(&gb, 16) || get_bits(&gb, 16))
00123             break;
00124         skip_bits(&gb, 1);
00125         mode_count++;
00126         if (mode_count > 64)
00127             break;
00128         gb0 = gb;
00129         if (get_bits(&gb0, 6) + 1 == mode_count) {
00130             got_mode_header = 1;
00131             last_mode_count = mode_count;
00132         }
00133     }
00134     if (!got_mode_header) {
00135         av_log(avctx, AV_LOG_ERROR, "Invalid Setup header\n");
00136         ret = AVERROR_INVALIDDATA;
00137         goto bad_header;
00138     }
00139     
00140 
00141 
00142 
00143     if (last_mode_count > 2) {
00144         av_log_ask_for_sample(avctx, "%d modes found. This is either a false "
00145                               "positive or a sample from an unknown encoder.\n",
00146                               last_mode_count);
00147     }
00148     
00149 
00150     if (last_mode_count > 63) {
00151         av_log(avctx, AV_LOG_ERROR, "Unsupported mode count: %d\n",
00152                last_mode_count);
00153         ret = AVERROR_INVALIDDATA;
00154         goto bad_header;
00155     }
00156     s->mode_count = mode_count = last_mode_count;
00157     
00158 
00159     s->mode_mask = ((1 << (av_log2(mode_count - 1) + 1)) - 1) << 1;
00160     
00161     s->prev_mask = (s->mode_mask | 0x1) + 1;
00162 
00163     init_get_bits(&gb, rev_buf, buf_size * 8);
00164     skip_bits_long(&gb, got_framing_bit);
00165     for (i = mode_count - 1; i >= 0; i--) {
00166         skip_bits_long(&gb, 40);
00167         s->mode_blocksize[i] = get_bits1(&gb);
00168     }
00169 
00170 bad_header:
00171     av_free(rev_buf);
00172     return ret;
00173 }
00174 
00175 int avpriv_vorbis_parse_extradata(AVCodecContext *avctx, VorbisParseContext *s)
00176 {
00177     uint8_t *header_start[3];
00178     int header_len[3];
00179     int ret;
00180 
00181     s->avctx = avctx;
00182     s->extradata_parsed = 1;
00183 
00184     if ((ret = avpriv_split_xiph_headers(avctx->extradata,
00185                                          avctx->extradata_size, 30,
00186                                          header_start, header_len)) < 0) {
00187         av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
00188         return ret;
00189     }
00190 
00191     if ((ret = parse_id_header(avctx, s, header_start[0], header_len[0])) < 0)
00192         return ret;
00193 
00194     if ((ret = parse_setup_header(avctx, s, header_start[2], header_len[2])) < 0)
00195         return ret;
00196 
00197     s->valid_extradata = 1;
00198     s->previous_blocksize = s->blocksize[s->mode_blocksize[0]];
00199 
00200     return 0;
00201 }
00202 
00203 int avpriv_vorbis_parse_frame(VorbisParseContext *s, const uint8_t *buf,
00204                               int buf_size)
00205 {
00206     int duration = 0;
00207 
00208     if (s->valid_extradata && buf_size > 0) {
00209         int mode, current_blocksize;
00210         int previous_blocksize = s->previous_blocksize;
00211 
00212         if (buf[0] & 1) {
00213             av_log(s->avctx, AV_LOG_ERROR, "Invalid packet\n");
00214             return AVERROR_INVALIDDATA;
00215         }
00216         if (s->mode_count == 1)
00217             mode = 0;
00218         else
00219             mode = (buf[0] & s->mode_mask) >> 1;
00220         if (mode >= s->mode_count) {
00221             av_log(s->avctx, AV_LOG_ERROR, "Invalid mode in packet\n");
00222             return AVERROR_INVALIDDATA;
00223         }
00224         if(s->mode_blocksize[mode]){
00225             int flag = !!(buf[0] & s->prev_mask);
00226             previous_blocksize = s->blocksize[flag];
00227         }
00228         current_blocksize     = s->blocksize[s->mode_blocksize[mode]];
00229         duration              = (previous_blocksize + current_blocksize) >> 2;
00230         s->previous_blocksize = current_blocksize;
00231     }
00232 
00233     return duration;
00234 }
00235 
00236 void avpriv_vorbis_parse_reset(VorbisParseContext *s)
00237 {
00238     if (s->valid_extradata)
00239         s->previous_blocksize = s->blocksize[0];
00240 }
00241 
00242 #if CONFIG_VORBIS_PARSER
00243 static int vorbis_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
00244                         const uint8_t **poutbuf, int *poutbuf_size,
00245                         const uint8_t *buf, int buf_size)
00246 {
00247     VorbisParseContext *s = s1->priv_data;
00248     int duration;
00249 
00250     if (!s->extradata_parsed && avctx->extradata && avctx->extradata_size)
00251         if (avpriv_vorbis_parse_extradata(avctx, s))
00252             goto end;
00253 
00254     if ((duration = avpriv_vorbis_parse_frame(s, buf, buf_size)) >= 0)
00255         s1->duration = duration;
00256 
00257 end:
00258     
00259 
00260     *poutbuf      = buf;
00261     *poutbuf_size = buf_size;
00262     return buf_size;
00263 }
00264 
00265 AVCodecParser ff_vorbis_parser = {
00266     .codec_ids      = { AV_CODEC_ID_VORBIS },
00267     .priv_data_size = sizeof(VorbisParseContext),
00268     .parser_parse   = vorbis_parse,
00269 };
00270 #endif