/src/ffmpeg/libavcodec/latm_parser.c
Line | Count | Source |
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 <stdint.h> |
27 | | #include "parser.h" |
28 | | #include "parser_internal.h" |
29 | | |
30 | 8.81M | #define LATM_HEADER 0x56e000 // 0x2b7 (11 bits) |
31 | 8.81M | #define LATM_MASK 0xFFE000 // top 11 bits |
32 | 497k | #define LATM_SIZE_MASK 0x001FFF // bottom 13 bits |
33 | | |
34 | | typedef struct LATMParseContext{ |
35 | | ParseContext pc; |
36 | | int count; |
37 | | } LATMParseContext; |
38 | | |
39 | | /** |
40 | | * Find the end of the current frame in the bitstream. |
41 | | * @return the position of the first byte of the next frame, or -1 |
42 | | */ |
43 | | static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf, |
44 | | int buf_size) |
45 | 320k | { |
46 | 320k | LATMParseContext *s = s1->priv_data; |
47 | 320k | ParseContext *pc = &s->pc; |
48 | 320k | int pic_found, i; |
49 | 320k | uint32_t state; |
50 | | |
51 | 320k | pic_found = pc->frame_start_found; |
52 | 320k | state = pc->state; |
53 | | |
54 | 320k | if (!pic_found) { |
55 | 8.87M | for (i = 0; i < buf_size; i++) { |
56 | 8.81M | state = (state<<8) | buf[i]; |
57 | 8.81M | if ((state & LATM_MASK) == LATM_HEADER) { |
58 | 232k | i++; |
59 | 232k | s->count = -i; |
60 | 232k | pic_found = 1; |
61 | 232k | break; |
62 | 232k | } |
63 | 8.81M | } |
64 | 287k | } |
65 | | |
66 | 320k | if (pic_found) { |
67 | | /* EOF considered as end of frame */ |
68 | 265k | if (buf_size == 0) |
69 | 82 | return 0; |
70 | 265k | if ((state & LATM_SIZE_MASK) - s->count <= buf_size) { |
71 | 232k | pc->frame_start_found = 0; |
72 | 232k | pc->state = -1; |
73 | 232k | return (state & LATM_SIZE_MASK) - s->count; |
74 | 232k | } |
75 | 265k | } |
76 | | |
77 | 88.0k | s->count += buf_size; |
78 | 88.0k | pc->frame_start_found = pic_found; |
79 | 88.0k | pc->state = state; |
80 | | |
81 | 88.0k | return END_NOT_FOUND; |
82 | 320k | } |
83 | | |
84 | | static int latm_parse(AVCodecParserContext *s1, AVCodecContext *avctx, |
85 | | const uint8_t **poutbuf, int *poutbuf_size, |
86 | | const uint8_t *buf, int buf_size) |
87 | 320k | { |
88 | 320k | LATMParseContext *s = s1->priv_data; |
89 | 320k | ParseContext *pc = &s->pc; |
90 | 320k | int next; |
91 | | |
92 | 320k | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
93 | 108 | next = buf_size; |
94 | 320k | } else { |
95 | 320k | next = latm_find_frame_end(s1, buf, buf_size); |
96 | | |
97 | 320k | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
98 | 87.8k | *poutbuf = NULL; |
99 | 87.8k | *poutbuf_size = 0; |
100 | 87.8k | return buf_size; |
101 | 87.8k | } |
102 | 320k | } |
103 | 232k | *poutbuf = buf; |
104 | 232k | *poutbuf_size = buf_size; |
105 | 232k | return next; |
106 | 320k | } |
107 | | |
108 | | const FFCodecParser ff_aac_latm_parser = { |
109 | | PARSER_CODEC_LIST(AV_CODEC_ID_AAC_LATM), |
110 | | .priv_data_size = sizeof(LATMParseContext), |
111 | | .parse = latm_parse, |
112 | | .close = ff_parse_close |
113 | | }; |