factorize code and add safety check to prevent memcpying negative amounts
[ffmpeg.git] / libavcodec / aac_ac3_parser.c
1 /*
2  * Common AAC and AC3 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 "parser.h"
24 #include "aac_ac3_parser.h"
25
26 int ff_aac_ac3_parse(AVCodecParserContext *s1,
27                      AVCodecContext *avctx,
28                      const uint8_t **poutbuf, int *poutbuf_size,
29                      const uint8_t *buf, int buf_size)
30 {
31     AACAC3ParseContext *s = s1->priv_data;
32     const uint8_t *buf_ptr;
33     int len, sample_rate, bit_rate, channels, samples;
34
35     *poutbuf = NULL;
36     *poutbuf_size = 0;
37
38     buf_ptr = buf;
39     while (buf_size > 0) {
40         int size_needed= s->frame_size ? s->frame_size : s->header_size;
41         len = s->inbuf_ptr - s->inbuf;
42
43         if(len<size_needed){
44             len = FFMIN(size_needed - len, buf_size);
45             memcpy(s->inbuf_ptr, buf_ptr, len);
46             buf_ptr      += len;
47             s->inbuf_ptr += len;
48             buf_size     -= len;
49         }
50
51         if (s->frame_size == 0) {
52             if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
53                 len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
54                               &samples);
55                 if (len == 0) {
56                     /* no sync found : move by one byte (inefficient, but simple!) */
57                     memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
58                     s->inbuf_ptr--;
59                 } else {
60                     s->frame_size = len;
61                     /* update codec info */
62                     avctx->sample_rate = sample_rate;
63                     /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
64                     if(avctx->codec_id == CODEC_ID_AC3){
65                         if(avctx->channels!=1 && avctx->channels!=2){
66                             avctx->channels = channels;
67                         }
68                     } else {
69                         avctx->channels = channels;
70                     }
71                     avctx->bit_rate = bit_rate;
72                     avctx->frame_size = samples;
73                 }
74             }
75         } else {
76             if(s->inbuf_ptr - s->inbuf == s->frame_size){
77                 *poutbuf = s->inbuf;
78                 *poutbuf_size = s->frame_size;
79                 s->inbuf_ptr = s->inbuf;
80                 s->frame_size = 0;
81                 break;
82             }
83         }
84     }
85     return buf_ptr - buf;
86 }