]> git.sesse.net Git - ffmpeg/blob - libavcodec/latm_parser.c
Second hunk from secrity fix from google.
[ffmpeg] / libavcodec / latm_parser.c
1 /*
2  * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * AAC LATM parser
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30 #include <sys/types.h>
31
32 #include "parser.h"
33
34 #define LATM_HEADER     0x56e000        // 0x2b7 (11 bits)
35 #define LATM_MASK       0xFFE000        // top 11 bits
36 #define LATM_SIZE_MASK  0x001FFF        // bottom 13 bits
37
38 typedef struct LATMParseContext{
39     ParseContext pc;
40     int count;
41 } LATMParseContext;
42
43 /**
44  * finds the end of the current frame in the bitstream.
45  * @return the position of the first byte of the next frame, or -1
46  */
47 static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
48                                int buf_size)
49 {
50     LATMParseContext *s = s1->priv_data;
51     ParseContext *pc    = &s->pc;
52     int pic_found, i;
53     uint32_t state;
54
55     pic_found = pc->frame_start_found;
56     state     = pc->state;
57
58     i = 0;
59     if (!pic_found) {
60         for (i = 0; i < buf_size; i++) {
61             state = (state<<8) | buf[i];
62             if ((state & LATM_MASK) == LATM_HEADER) {
63                 i++;
64                 s->count  = -i;
65                 pic_found = 1;
66                 break;
67             }
68         }
69     }
70
71     if (pic_found) {
72         /* EOF considered as end of frame */
73         if (buf_size == 0)
74             return 0;
75         if ((state & LATM_SIZE_MASK) - s->count <= buf_size) {
76             pc->frame_start_found = 0;
77             pc->state             = -1;
78             return (state & LATM_SIZE_MASK) - s->count;
79         }
80     }
81
82     s->count             += buf_size;
83     pc->frame_start_found = pic_found;
84     pc->state             = state;
85
86     return END_NOT_FOUND;
87 }
88
89 static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
90                       const uint8_t **poutbuf, int *poutbuf_size,
91                       const uint8_t *buf, int buf_size)
92 {
93     LATMParseContext *s = s1->priv_data;
94     ParseContext *pc    = &s->pc;
95     int next;
96
97     if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
98         next = buf_size;
99     } else {
100         next = latm_find_frame_end(s1, buf, buf_size);
101
102         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
103             *poutbuf      = NULL;
104             *poutbuf_size = 0;
105             return buf_size;
106         }
107     }
108     *poutbuf      = buf;
109     *poutbuf_size = buf_size;
110     return next;
111 }
112
113 AVCodecParser aac_latm_parser = {
114     { CODEC_ID_AAC_LATM },
115     sizeof(LATMParseContext),
116     NULL,
117     latm_parse,
118     ff_parse_close
119 };