FFmpeg
mlp_parser.c
Go to the documentation of this file.
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 {
38 
40 
41  int in_sync;
42 
45 
47 {
49  return 0;
50 }
51 
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  s->key_frame = 0;
65 
66  *poutbuf_size = 0;
67  if (buf_size == 0)
68  return 0;
69 
71  next = buf_size;
72  } else {
73  if (!mp->in_sync) {
74  // Not in sync - find a major sync header
75 
76  for (i = 0; i < buf_size; i++) {
77  mp->pc.state = (mp->pc.state << 8) | buf[i];
78  if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
79  // ignore if we do not have the data for the start of header
80  mp->pc.index + i >= 7) {
81  mp->in_sync = 1;
82  mp->bytes_left = 0;
83  break;
84  }
85  }
86 
87  if (!mp->in_sync) {
88  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
89  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
90  return buf_size;
91  }
92 
93  if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
94  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
95  return ret;
96  }
97 
98  return i - 7;
99  }
100 
101  if (mp->bytes_left == 0) {
102  // Find length of this packet
103 
104  /* Copy overread bytes from last frame into buffer. */
105  for(; mp->pc.overread>0; mp->pc.overread--) {
106  mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
107  }
108 
109  if (mp->pc.index + buf_size < 2) {
110  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
111  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
112  return buf_size;
113  }
114 
115  mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
116  | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
117  mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
118  if (mp->bytes_left <= 0) { // prevent infinite loop
119  goto lost_sync;
120  }
121  mp->bytes_left -= mp->pc.index;
122  }
123 
124  next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
125 
126  if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
127  mp->bytes_left -= buf_size;
128  return buf_size;
129  }
130 
131  mp->bytes_left = 0;
132  }
133 
134  sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
135 
136  if (!sync_present) {
137  /* The first nibble of a frame is a parity check of the 4-byte
138  * access unit header and all the 2- or 4-byte substream headers. */
139  // Only check when this isn't a sync frame - syncs have a checksum.
140 
141  s->key_frame = 0;
142 
143  parity_bits = 0;
144  for (i = -1; i < mp->num_substreams; i++) {
145  parity_bits ^= buf[p++];
146  parity_bits ^= buf[p++];
147 
148  if (i < 0 || buf[p-2] & 0x80) {
149  parity_bits ^= buf[p++];
150  parity_bits ^= buf[p++];
151  }
152  }
153 
154  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
155  av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
156  goto lost_sync;
157  }
158  } else {
159  GetBitContext gb;
161 
162  init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
163  if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
164  goto lost_sync;
165 
166  s->key_frame = 1;
167 
168  avctx->bits_per_raw_sample = mh.group1_bits;
169  if (avctx->bits_per_raw_sample > 16)
170  avctx->sample_fmt = AV_SAMPLE_FMT_S32;
171  else
172  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
173  avctx->sample_rate = mh.group1_samplerate;
174  avctx->frame_size =
175  s->duration = mh.access_unit_size;
176 
177  if(!avctx->channels || !avctx->channel_layout) {
178  if (mh.stream_type == 0xbb) {
179  /* MLP stream */
180  avctx->channels = mh.channels_mlp;
182  } else { /* mh.stream_type == 0xba */
183  /* TrueHD stream */
184  if (!mh.channels_thd_stream2) {
185  avctx->channels = mh.channels_thd_stream1;
187  } else {
188  avctx->channels = mh.channels_thd_stream2;
190  }
191  }
192  }
193 
194  if (!mh.is_vbr) /* Stream is CBR */
195  avctx->bit_rate = mh.peak_bitrate;
196 
198  }
199 
200  *poutbuf = buf;
201  *poutbuf_size = buf_size;
202 
203  return next;
204 
205 lost_sync:
206  mp->in_sync = 0;
207  return 1;
208 }
209 
212  .priv_data_size = sizeof(MLPParseContext),
213  .parser_init = mlp_init,
214  .parser_parse = mlp_parse,
215  .parser_close = ff_parse_close,
216 };
int channels_thd_stream1
Channel count for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parse.h:44
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:707
int num_substreams
Number of substreams within stream.
Definition: mlp_parse.h:56
int codec_ids[5]
Definition: avcodec.h:3905
int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
Read a major sync info header - contains high level information about the stream - sample rate...
Definition: mlp_parse.c:86
uint64_t channel_layout_mlp
Channel layout for MLP streams.
Definition: mlp_parse.h:46
int duration
Duration of the current frame.
Definition: avcodec.h:3859
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1888
int access_unit_size
Number of samples per coded frame.
Definition: mlp_parse.h:50
int peak_bitrate
Peak bitrate for VBR, actual bitrate (==peak) for CBR.
Definition: mlp_parse.h:54
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1325
uint8_t
#define av_cold
Definition: attributes.h:82
ParseContext pc
Definition: mlp_parser.c:37
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
static int mlp_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: mlp_parser.c:52
bitstream reader API header.
signed 32 bits
Definition: samplefmt.h:62
#define av_log(a,...)
AVCodecParser ff_mlp_parser
Definition: mlp_parser.c:210
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:234
int channels_mlp
Channel count for MLP streams.
Definition: mlp_parse.h:43
int overread_index
the index into ParseContext.buffer of the overread bytes
Definition: parser.h:36
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1368
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:319
common internal API header
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
#define s(width, name)
Definition: cbs_vp9.c:257
int is_vbr
Stream is VBR instead of CBR.
Definition: mlp_parse.h:53
if(ret)
int stream_type
0xBB for MLP, 0xBA for TrueHD
Definition: mlp_parse.h:28
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1337
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
uint8_t * buffer
Definition: parser.h:29
int sample_rate
samples per second
Definition: avcodec.h:1317
main external API structure.
Definition: avcodec.h:657
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
#define END_NOT_FOUND
Definition: parser.h:40
int channels_thd_stream2
Channel count for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parse.h:45
signed 16 bits
Definition: samplefmt.h:61
uint64_t channel_layout_thd_stream1
Channel layout for substream 1 of TrueHD streams ("6-channel presentation")
Definition: mlp_parse.h:47
uint64_t channel_layout_thd_stream2
Channel layout for substream 2 of TrueHD streams ("8-channel presentation")
Definition: mlp_parse.h:48
int index
Definition: parser.h:30
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3771
int channels
number of audio channels
Definition: avcodec.h:1318
int group1_bits
The bit depth of the first substream.
Definition: mlp_parse.h:31
av_cold void ff_mlp_init_crc(void)
Definition: mlp.c:75
int group1_samplerate
Sample rate of first substream.
Definition: mlp_parse.h:34
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3786
#define mh
static av_cold int mlp_init(AVCodecParserContext *s)
Definition: mlp_parser.c:46