1/* 2 * Common AAC and AC-3 parser 3 * Copyright (c) 2003 Fabrice Bellard 4 * Copyright (c) 2003 Michael Niedermayer 5 * 6 * This file is part of FFmpeg. 7 * 8 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23#include "config_components.h" 24 25#include "libavutil/channel_layout.h" 26#include "libavutil/common.h" 27#include "parser.h" 28#include "aac_ac3_parser.h" 29 30int ff_aac_ac3_parse(AVCodecParserContext *s1, 31 AVCodecContext *avctx, 32 const uint8_t **poutbuf, int *poutbuf_size, 33 const uint8_t *buf, int buf_size) 34{ 35 AACAC3ParseContext *s = s1->priv_data; 36 ParseContext *pc = &s->pc; 37 int len, i; 38 int new_frame_start; 39 int got_frame = 0; 40 41get_next: 42 i=END_NOT_FOUND; 43 if(s->remaining_size <= buf_size){ 44 if(s->remaining_size && !s->need_next_header){ 45 i= s->remaining_size; 46 s->remaining_size = 0; 47 }else{ //we need a header first 48 len=0; 49 for(i=s->remaining_size; i<buf_size; i++){ 50 s->state = (s->state<<8) + buf[i]; 51 if((len=s->sync(s->state, s, &s->need_next_header, &new_frame_start))) 52 break; 53 } 54 if(len<=0){ 55 i=END_NOT_FOUND; 56 }else{ 57 got_frame = 1; 58 s->state=0; 59 i-= s->header_size -1; 60 s->remaining_size = len; 61 if(!new_frame_start || pc->index+i<=0){ 62 s->remaining_size += i; 63 goto get_next; 64 } 65 else if (i < 0) { 66 s->remaining_size += i; 67 } 68 } 69 } 70 } 71 72 if(ff_combine_frame(pc, i, &buf, &buf_size)<0){ 73 s->remaining_size -= FFMIN(s->remaining_size, buf_size); 74 *poutbuf = NULL; 75 *poutbuf_size = 0; 76 return buf_size; 77 } 78 79 *poutbuf = buf; 80 *poutbuf_size = buf_size; 81 82 /* update codec info */ 83 if(s->codec_id) 84 avctx->codec_id = s->codec_id; 85 86 if (got_frame) { 87 /* Due to backwards compatible HE-AAC the sample rate, channel count, 88 and total number of samples found in an AAC ADTS header are not 89 reliable. Bit rate is still accurate because the total frame 90 duration in seconds is still correct (as is the number of bits in 91 the frame). */ 92 if (avctx->codec_id != AV_CODEC_ID_AAC) { 93 avctx->sample_rate = s->sample_rate; 94 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { 95 av_channel_layout_uninit(&avctx->ch_layout); 96 if (s->channel_layout) { 97 av_channel_layout_from_mask(&avctx->ch_layout, s->channel_layout); 98 } else { 99 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; 100 avctx->ch_layout.nb_channels = s->channels; 101 } 102#if FF_API_OLD_CHANNEL_LAYOUT 103FF_DISABLE_DEPRECATION_WARNINGS 104 avctx->channels = avctx->ch_layout.nb_channels; 105 avctx->channel_layout = s->channel_layout; 106FF_ENABLE_DEPRECATION_WARNINGS 107#endif 108 } 109 s1->duration = s->samples; 110 avctx->audio_service_type = s->service_type; 111 } 112 113 /* Calculate the average bit rate */ 114 s->frame_number++; 115 if (!CONFIG_EAC3_DECODER || avctx->codec_id != AV_CODEC_ID_EAC3) { 116 avctx->bit_rate += 117 (s->bit_rate - avctx->bit_rate) / s->frame_number; 118 } 119 } 120 121 return i; 122} 123