1/* 2 * MPEG Audio parser 3 * Copyright (c) 2003 Fabrice Bellard 4 * Copyright (c) 2003 Michael Niedermayer 5 * 6 * This file is part of Libav. 7 * 8 * Libav is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * Libav is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with Libav; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23#include "parser.h" 24#include "mpegaudiodecheader.h" 25 26 27typedef struct MpegAudioParseContext { 28 ParseContext pc; 29 int frame_size; 30 uint32_t header; 31 int header_count; 32} MpegAudioParseContext; 33 34#define MPA_HEADER_SIZE 4 35 36/* header + layer + bitrate + freq + lsf/mpeg25 */ 37#define SAME_HEADER_MASK \ 38 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) 39 40static int mpegaudio_parse(AVCodecParserContext *s1, 41 AVCodecContext *avctx, 42 const uint8_t **poutbuf, int *poutbuf_size, 43 const uint8_t *buf, int buf_size) 44{ 45 MpegAudioParseContext *s = s1->priv_data; 46 ParseContext *pc = &s->pc; 47 uint32_t state= pc->state; 48 int i; 49 int next= END_NOT_FOUND; 50 51 for(i=0; i<buf_size; ){ 52 if(s->frame_size){ 53 int inc= FFMIN(buf_size - i, s->frame_size); 54 i += inc; 55 s->frame_size -= inc; 56 57 if(!s->frame_size){ 58 next= i; 59 break; 60 } 61 }else{ 62 while(i<buf_size){ 63 int ret, sr, channels, bit_rate, frame_size; 64 65 state= (state<<8) + buf[i++]; 66 67 ret = avpriv_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate); 68 if (ret < 4) { 69 s->header_count= -2; 70 } else { 71 if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) 72 s->header_count= -3; 73 s->header= state; 74 s->header_count++; 75 s->frame_size = ret-4; 76 77 if(s->header_count > 1){ 78 avctx->sample_rate= sr; 79 avctx->channels = channels; 80 avctx->frame_size = frame_size; 81 avctx->bit_rate = bit_rate; 82 } 83 break; 84 } 85 } 86 } 87 } 88 89 pc->state= state; 90 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { 91 *poutbuf = NULL; 92 *poutbuf_size = 0; 93 return buf_size; 94 } 95 96 *poutbuf = buf; 97 *poutbuf_size = buf_size; 98 return next; 99} 100 101 102AVCodecParser ff_mpegaudio_parser = { 103 .codec_ids = { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 }, 104 .priv_data_size = sizeof(MpegAudioParseContext), 105 .parser_parse = mpegaudio_parse, 106 .parser_close = ff_parse_close, 107}; 108