]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_parser.c
ffv1: Pass correct pointers to av_free()
[ffmpeg] / libavcodec / dca_parser.c
1 /*
2  * DCA parser
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "dca.h"
26 #include "dca_syncwords.h"
27 #include "get_bits.h"
28 #include "parser.h"
29
30 typedef struct DCAParseContext {
31     ParseContext pc;
32     uint32_t lastmarker;
33     int size;
34     int framesize;
35     int hd_pos;
36 } DCAParseContext;
37
38 #define IS_MARKER(state, i, buf, buf_size) \
39     ((state == DCA_SYNCWORD_CORE_14B_LE && (i < buf_size - 2) && (buf[i + 1] & 0xF0) == 0xF0 &&  buf[i + 2]         == 0x07) || \
40      (state == DCA_SYNCWORD_CORE_14B_BE && (i < buf_size - 2) &&  buf[i + 1]         == 0x07 && (buf[i + 2] & 0xF0) == 0xF0) || \
41       state == DCA_SYNCWORD_CORE_LE || state == DCA_SYNCWORD_CORE_BE)
42
43 /**
44  * Find 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 dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
48                               int buf_size)
49 {
50     int start_found, i;
51     uint32_t state;
52     ParseContext *pc = &pc1->pc;
53
54     start_found = pc->frame_start_found;
55     state       = pc->state;
56
57     i = 0;
58     if (!start_found) {
59         for (i = 0; i < buf_size; i++) {
60             state = (state << 8) | buf[i];
61             if (IS_MARKER(state, i, buf, buf_size)) {
62                 if (pc1->lastmarker && state == pc1->lastmarker) {
63                     start_found = 1;
64                     i++;
65                     break;
66                 } else if (!pc1->lastmarker) {
67                     start_found     = 1;
68                     pc1->lastmarker = state;
69                     i++;
70                     break;
71                 }
72             }
73         }
74     }
75     if (start_found) {
76         for (; i < buf_size; i++) {
77             pc1->size++;
78             state = (state << 8) | buf[i];
79             if (state == DCA_SYNCWORD_SUBSTREAM && !pc1->hd_pos)
80                 pc1->hd_pos = pc1->size;
81             if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
82                 if (pc1->framesize > pc1->size)
83                     continue;
84                 pc->frame_start_found = 0;
85                 pc->state             = -1;
86                 pc1->size             = 0;
87                 return i - 3;
88             }
89         }
90     }
91     pc->frame_start_found = start_found;
92     pc->state             = state;
93     return END_NOT_FOUND;
94 }
95
96 static av_cold int dca_parse_init(AVCodecParserContext *s)
97 {
98     DCAParseContext *pc1 = s->priv_data;
99
100     pc1->lastmarker = 0;
101     return 0;
102 }
103
104 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
105                             int *sample_rate, int *framesize)
106 {
107     GetBitContext gb;
108     uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
109     int ret, sample_blocks, sr_code;
110
111     if (buf_size < 12)
112         return AVERROR_INVALIDDATA;
113
114     if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
115         return ret;
116
117     init_get_bits(&gb, hdr, 96);
118
119     skip_bits_long(&gb, 39);
120     sample_blocks = get_bits(&gb, 7) + 1;
121     if (sample_blocks < 8)
122         return AVERROR_INVALIDDATA;
123     *duration = 256 * (sample_blocks / 8);
124
125     *framesize = get_bits(&gb, 14) + 1;
126     if (*framesize < 95)
127         return AVERROR_INVALIDDATA;
128
129     skip_bits(&gb, 6);
130     sr_code      = get_bits(&gb, 4);
131     *sample_rate = avpriv_dca_sample_rates[sr_code];
132     if (*sample_rate == 0)
133         return AVERROR_INVALIDDATA;
134
135     return 0;
136 }
137
138 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
139                      const uint8_t **poutbuf, int *poutbuf_size,
140                      const uint8_t *buf, int buf_size)
141 {
142     DCAParseContext *pc1 = s->priv_data;
143     ParseContext *pc = &pc1->pc;
144     int next, duration, sample_rate;
145
146     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
147         next = buf_size;
148     } else {
149         next = dca_find_frame_end(pc1, buf, buf_size);
150
151         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
152             *poutbuf      = NULL;
153             *poutbuf_size = 0;
154             return buf_size;
155         }
156     }
157
158     /* read the duration and sample rate from the frame header */
159     if (!dca_parse_params(buf, buf_size, &duration, &sample_rate, &pc1->framesize)) {
160         s->duration        = duration;
161         avctx->sample_rate = sample_rate;
162     } else
163         s->duration = 0;
164
165     *poutbuf      = buf;
166     *poutbuf_size = buf_size;
167     return next;
168 }
169
170 AVCodecParser ff_dca_parser = {
171     .codec_ids      = { AV_CODEC_ID_DTS },
172     .priv_data_size = sizeof(DCAParseContext),
173     .parser_init    = dca_parse_init,
174     .parser_parse   = dca_parse,
175     .parser_close   = ff_parse_close,
176 };