]> git.sesse.net Git - ffmpeg/blob - libavcodec/mlp_parser.c
avcodec/mlp_parser: export AVCodecContext frame_size
[ffmpeg] / libavcodec / mlp_parser.c
1 /*
2  * MLP parser
3  * Copyright (c) 2007 Ian Caulfield
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * MLP parser
25  */
26
27 #include <stdint.h>
28
29 #include "libavutil/internal.h"
30 #include "get_bits.h"
31 #include "parser.h"
32 #include "mlp_parse.h"
33 #include "mlp.h"
34
35 typedef struct MLPParseContext
36 {
37     ParseContext pc;
38
39     int bytes_left;
40
41     int in_sync;
42
43     int num_substreams;
44 } MLPParseContext;
45
46 static av_cold int mlp_init(AVCodecParserContext *s)
47 {
48     ff_mlp_init_crc();
49     return 0;
50 }
51
52 static int mlp_parse(AVCodecParserContext *s,
53                      AVCodecContext *avctx,
54                      const uint8_t **poutbuf, int *poutbuf_size,
55                      const uint8_t *buf, int buf_size)
56 {
57     MLPParseContext *mp = s->priv_data;
58     int sync_present;
59     uint8_t parity_bits;
60     int next;
61     int ret;
62     int i, p = 0;
63
64     *poutbuf_size = 0;
65     if (buf_size == 0)
66         return 0;
67
68     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
69         next = buf_size;
70     } else {
71         if (!mp->in_sync) {
72             // Not in sync - find a major sync header
73
74             for (i = 0; i < buf_size; i++) {
75                 mp->pc.state = (mp->pc.state << 8) | buf[i];
76                 if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
77                     // ignore if we do not have the data for the start of header
78                     mp->pc.index + i >= 7) {
79                     mp->in_sync = 1;
80                     mp->bytes_left = 0;
81                     break;
82                 }
83             }
84
85             if (!mp->in_sync) {
86                 if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
87                     av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
88                 return buf_size;
89             }
90
91             if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
92                 av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
93                 return ret;
94             }
95
96             return i - 7;
97         }
98
99         if (mp->bytes_left == 0) {
100             // Find length of this packet
101
102             /* Copy overread bytes from last frame into buffer. */
103             for(; mp->pc.overread>0; mp->pc.overread--) {
104                 mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
105             }
106
107             if (mp->pc.index + buf_size < 2) {
108                 if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
109                     av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
110                 return buf_size;
111             }
112
113             mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
114                            |  (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
115             mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
116             if (mp->bytes_left <= 0) { // prevent infinite loop
117                 goto lost_sync;
118             }
119             mp->bytes_left -= mp->pc.index;
120         }
121
122         next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
123
124         if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
125             mp->bytes_left -= buf_size;
126             return buf_size;
127         }
128
129         mp->bytes_left = 0;
130     }
131
132     sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
133
134     if (!sync_present) {
135         /* The first nibble of a frame is a parity check of the 4-byte
136          * access unit header and all the 2- or 4-byte substream headers. */
137         // Only check when this isn't a sync frame - syncs have a checksum.
138
139         parity_bits = 0;
140         for (i = -1; i < mp->num_substreams; i++) {
141             parity_bits ^= buf[p++];
142             parity_bits ^= buf[p++];
143
144             if (i < 0 || buf[p-2] & 0x80) {
145                 parity_bits ^= buf[p++];
146                 parity_bits ^= buf[p++];
147             }
148         }
149
150         if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
151             av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
152             goto lost_sync;
153         }
154     } else {
155         GetBitContext gb;
156         MLPHeaderInfo mh;
157
158         init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
159         if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
160             goto lost_sync;
161
162         avctx->bits_per_raw_sample = mh.group1_bits;
163         if (avctx->bits_per_raw_sample > 16)
164             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
165         else
166             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
167         avctx->sample_rate = mh.group1_samplerate;
168         avctx->frame_size =
169         s->duration = mh.access_unit_size;
170
171         if(!avctx->channels || !avctx->channel_layout) {
172         if (mh.stream_type == 0xbb) {
173             /* MLP stream */
174             avctx->channels       = mh.channels_mlp;
175             avctx->channel_layout = mh.channel_layout_mlp;
176         } else { /* mh.stream_type == 0xba */
177             /* TrueHD stream */
178             if (!mh.channels_thd_stream2) {
179                 avctx->channels       = mh.channels_thd_stream1;
180                 avctx->channel_layout = mh.channel_layout_thd_stream1;
181             } else {
182                 avctx->channels       = mh.channels_thd_stream2;
183                 avctx->channel_layout = mh.channel_layout_thd_stream2;
184             }
185         }
186         }
187
188         if (!mh.is_vbr) /* Stream is CBR */
189             avctx->bit_rate = mh.peak_bitrate;
190
191         mp->num_substreams = mh.num_substreams;
192     }
193
194     *poutbuf = buf;
195     *poutbuf_size = buf_size;
196
197     return next;
198
199 lost_sync:
200     mp->in_sync = 0;
201     return 1;
202 }
203
204 AVCodecParser ff_mlp_parser = {
205     .codec_ids      = { AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD },
206     .priv_data_size = sizeof(MLPParseContext),
207     .parser_init    = mlp_init,
208     .parser_parse   = mlp_parse,
209     .parser_close   = ff_parse_close,
210 };