Libav
|
00001 /* 00002 * Common AAC and AC-3 parser 00003 * Copyright (c) 2003 Fabrice Bellard 00004 * Copyright (c) 2003 Michael Niedermayer 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "parser.h" 00024 #include "aac_ac3_parser.h" 00025 00026 int ff_aac_ac3_parse(AVCodecParserContext *s1, 00027 AVCodecContext *avctx, 00028 const uint8_t **poutbuf, int *poutbuf_size, 00029 const uint8_t *buf, int buf_size) 00030 { 00031 AACAC3ParseContext *s = s1->priv_data; 00032 ParseContext *pc = &s->pc; 00033 int len, i; 00034 int new_frame_start; 00035 00036 get_next: 00037 i=END_NOT_FOUND; 00038 if(s->remaining_size <= buf_size){ 00039 if(s->remaining_size && !s->need_next_header){ 00040 i= s->remaining_size; 00041 s->remaining_size = 0; 00042 }else{ //we need a header first 00043 len=0; 00044 for(i=s->remaining_size; i<buf_size; i++){ 00045 s->state = (s->state<<8) + buf[i]; 00046 if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start))) 00047 break; 00048 } 00049 if(len<=0){ 00050 i=END_NOT_FOUND; 00051 }else{ 00052 s->state=0; 00053 i-= s->header_size -1; 00054 s->remaining_size = len; 00055 if(!new_frame_start || pc->index+i<=0){ 00056 s->remaining_size += i; 00057 goto get_next; 00058 } 00059 } 00060 } 00061 } 00062 00063 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ 00064 s->remaining_size -= FFMIN(s->remaining_size, buf_size); 00065 *poutbuf = NULL; 00066 *poutbuf_size = 0; 00067 return buf_size; 00068 } 00069 00070 *poutbuf = buf; 00071 *poutbuf_size = buf_size; 00072 00073 /* update codec info */ 00074 if(s->codec_id) 00075 avctx->codec_id = s->codec_id; 00076 00077 /* Due to backwards compatible HE-AAC the sample rate, channel count, 00078 and total number of samples found in an AAC ADTS header are not 00079 reliable. Bit rate is still accurate because the total frame duration in 00080 seconds is still correct (as is the number of bits in the frame). */ 00081 if (avctx->codec_id != CODEC_ID_AAC) { 00082 avctx->sample_rate = s->sample_rate; 00083 00084 /* allow downmixing to stereo (or mono for AC-3) */ 00085 if(avctx->request_channels > 0 && 00086 avctx->request_channels < s->channels && 00087 (avctx->request_channels <= 2 || 00088 (avctx->request_channels == 1 && 00089 (avctx->codec_id == CODEC_ID_AC3 || 00090 avctx->codec_id == CODEC_ID_EAC3)))) { 00091 avctx->channels = avctx->request_channels; 00092 } else { 00093 avctx->channels = s->channels; 00094 avctx->channel_layout = s->channel_layout; 00095 } 00096 avctx->frame_size = s->samples; 00097 } 00098 00099 avctx->bit_rate = s->bit_rate; 00100 00101 return i; 00102 }