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  *poutbuf = NULL;
68  if (buf_size == 0)
69  return 0;
70 
71  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
72  next = buf_size;
73  } else {
74  if (!mp->in_sync) {
75  // Not in sync - find a major sync header
76 
77  for (i = 0; i < buf_size; i++) {
78  mp->pc.state = (mp->pc.state << 8) | buf[i];
79  if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
80  // ignore if we do not have the data for the start of header
81  mp->pc.index + i >= 7) {
82  mp->in_sync = 1;
83  mp->bytes_left = 0;
84  break;
85  }
86  }
87 
88  if (!mp->in_sync) {
89  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
90  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
91  return buf_size;
92  }
93 
94  if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
95  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
96  return ret;
97  }
98 
99  return i - 7;
100  }
101 
102  if (mp->bytes_left == 0) {
103  // Find length of this packet
104 
105  /* Copy overread bytes from last frame into buffer. */
106  for(; mp->pc.overread>0; mp->pc.overread--) {
107  mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
108  }
109 
110  if (mp->pc.index + buf_size < 2) {
111  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
112  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
113  return buf_size;
114  }
115 
116  mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
117  | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
118  mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
119  if (mp->bytes_left <= 0) { // prevent infinite loop
120  goto lost_sync;
121  }
122  mp->bytes_left -= mp->pc.index;
123  }
124 
125  next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
126 
127  if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
128  mp->bytes_left -= buf_size;
129  return buf_size;
130  }
131 
132  mp->bytes_left = 0;
133  }
134 
135  sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
136 
137  if (!sync_present) {
138  /* The first nibble of a frame is a parity check of the 4-byte
139  * access unit header and all the 2- or 4-byte substream headers. */
140  // Only check when this isn't a sync frame - syncs have a checksum.
141 
142  s->key_frame = 0;
143 
144  parity_bits = 0;
145  for (i = -1; i < mp->num_substreams; i++) {
146  parity_bits ^= buf[p++];
147  parity_bits ^= buf[p++];
148 
149  if (i < 0 || buf[p-2] & 0x80) {
150  parity_bits ^= buf[p++];
151  parity_bits ^= buf[p++];
152  }
153  }
154 
155  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
156  av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
157  goto lost_sync;
158  }
159  } else {
160  GetBitContext gb;
162 
163  init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
164  if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
165  goto lost_sync;
166 
167  s->key_frame = 1;
168 
169  avctx->bits_per_raw_sample = mh.group1_bits;
170  if (avctx->bits_per_raw_sample > 16)
171  avctx->sample_fmt = AV_SAMPLE_FMT_S32;
172  else
173  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
174  avctx->sample_rate = mh.group1_samplerate;
175  avctx->frame_size =
176  s->duration = mh.access_unit_size;
177 
178  if(!avctx->channels || !avctx->channel_layout) {
179  if (mh.stream_type == 0xbb) {
180  /* MLP stream */
181  avctx->channels = mh.channels_mlp;
182  avctx->channel_layout = mh.channel_layout_mlp;
183  } else { /* mh.stream_type == 0xba */
184  /* TrueHD stream */
185  if (!mh.channels_thd_stream2) {
186  avctx->channels = mh.channels_thd_stream1;
187  avctx->channel_layout = mh.channel_layout_thd_stream1;
188  } else {
189  avctx->channels = mh.channels_thd_stream2;
190  avctx->channel_layout = mh.channel_layout_thd_stream2;
191  }
192  }
193  }
194 
195  if (!mh.is_vbr) /* Stream is CBR */
196  avctx->bit_rate = mh.peak_bitrate;
197 
198  mp->num_substreams = mh.num_substreams;
199  }
200 
201  *poutbuf = buf;
202  *poutbuf_size = buf_size;
203 
204  return next;
205 
206 lost_sync:
207  mp->in_sync = 0;
208  return 1;
209 }
210 
213  .priv_data_size = sizeof(MLPParseContext),
214  .parser_init = mlp_init,
215  .parser_parse = mlp_parse,
216  .parser_close = ff_parse_close,
217 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1186
ParseContext::overread_index
int overread_index
the index into ParseContext.buffer of the overread bytes
Definition: parser.h:36
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:319
mh
#define mh
Definition: vf_colormatrix.c:107
MLPParseContext::bytes_left
int bytes_left
Definition: mlp_parser.c:39
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:454
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
ParseContext
Definition: parser.h:28
GetBitContext
Definition: get_bits.h:61
ParseContext::buffer
uint8_t * buffer
Definition: parser.h:29
av_cold
#define av_cold
Definition: attributes.h:90
ParseContext::index
int index
Definition: parser.h:30
s
#define s(width, name)
Definition: cbs_vp9.c:257
ParseContext::overread
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1757
get_bits.h
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
AVCodecParser::codec_ids
int codec_ids[5]
Definition: avcodec.h:3521
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
ff_mlp_parser
AVCodecParser ff_mlp_parser
Definition: mlp_parser.c:211
MLPParseContext::num_substreams
int num_substreams
Definition: mlp_parser.c:43
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
AV_RB32
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:92
MLPParseContext::in_sync
int in_sync
Definition: mlp_parser.c:41
ff_mlp_read_major_sync
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
mlp_parse
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
ff_combine_frame
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
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
mlp_parse.h
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3387
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
MLPParseContext
Definition: mlp_parser.c:35
internal.h
MLPHeaderInfo
Definition: mlp_parse.h:26
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
parser.h
ff_mlp_init_crc
av_cold void ff_mlp_init_crc(void)
Definition: mlp.c:75
mlp_init
static av_cold int mlp_init(AVCodecParserContext *s)
Definition: mlp_parser.c:46
AVCodecParserContext
Definition: avcodec.h:3353
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:526
MLPParseContext::pc
ParseContext pc
Definition: mlp_parser.c:37
mlp.h
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:3520
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
AV_CODEC_ID_MLP
@ AV_CODEC_ID_MLP
Definition: codec_id.h:439