yading@10: /* yading@10: * Common AAC and AC-3 parser yading@10: * Copyright (c) 2003 Fabrice Bellard yading@10: * Copyright (c) 2003 Michael Niedermayer yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include "libavutil/common.h" yading@10: #include "parser.h" yading@10: #include "aac_ac3_parser.h" yading@10: yading@10: int ff_aac_ac3_parse(AVCodecParserContext *s1, yading@10: AVCodecContext *avctx, yading@10: const uint8_t **poutbuf, int *poutbuf_size, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: AACAC3ParseContext *s = s1->priv_data; yading@10: ParseContext *pc = &s->pc; yading@10: int len, i; yading@10: int new_frame_start; yading@10: yading@10: get_next: yading@10: i=END_NOT_FOUND; yading@10: if(s->remaining_size <= buf_size){ yading@10: if(s->remaining_size && !s->need_next_header){ yading@10: i= s->remaining_size; yading@10: s->remaining_size = 0; yading@10: }else{ //we need a header first yading@10: len=0; yading@10: for(i=s->remaining_size; istate = (s->state<<8) + buf[i]; yading@10: if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start))) yading@10: break; yading@10: } yading@10: if(len<=0){ yading@10: i=END_NOT_FOUND; yading@10: }else{ yading@10: s->state=0; yading@10: i-= s->header_size -1; yading@10: s->remaining_size = len; yading@10: if(!new_frame_start || pc->index+i<=0){ yading@10: s->remaining_size += i; yading@10: goto get_next; yading@10: } yading@10: } yading@10: } yading@10: } yading@10: yading@10: if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ yading@10: s->remaining_size -= FFMIN(s->remaining_size, buf_size); yading@10: *poutbuf = NULL; yading@10: *poutbuf_size = 0; yading@10: return buf_size; yading@10: } yading@10: yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: yading@10: /* update codec info */ yading@10: if(s->codec_id) yading@10: avctx->codec_id = s->codec_id; yading@10: yading@10: /* Due to backwards compatible HE-AAC the sample rate, channel count, yading@10: and total number of samples found in an AAC ADTS header are not yading@10: reliable. Bit rate is still accurate because the total frame duration in yading@10: seconds is still correct (as is the number of bits in the frame). */ yading@10: if (avctx->codec_id != AV_CODEC_ID_AAC) { yading@10: avctx->sample_rate = s->sample_rate; yading@10: yading@10: /* allow downmixing to stereo (or mono for AC-3) */ yading@10: if(avctx->request_channels > 0 && yading@10: avctx->request_channels < s->channels && yading@10: (avctx->request_channels <= 2 || yading@10: (avctx->request_channels == 1 && yading@10: (avctx->codec_id == AV_CODEC_ID_AC3 || yading@10: avctx->codec_id == AV_CODEC_ID_EAC3)))) { yading@10: avctx->channels = avctx->request_channels; yading@10: } else { yading@10: avctx->channels = s->channels; yading@10: avctx->channel_layout = s->channel_layout; yading@10: } yading@10: s1->duration = s->samples; yading@10: avctx->audio_service_type = s->service_type; yading@10: } yading@10: yading@10: avctx->bit_rate = s->bit_rate; yading@10: yading@10: return i; yading@10: }