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 #include "parser_internal.h"
35 
36 typedef struct MLPParseContext
37 {
39 
41 
42  int in_sync;
43 
46 
48 {
50  return 0;
51 }
52 
54  AVCodecContext *avctx,
55  const uint8_t **poutbuf, int *poutbuf_size,
56  const uint8_t *buf, int buf_size)
57 {
58  MLPParseContext *mp = s->priv_data;
59  int sync_present;
60  uint8_t parity_bits;
61  int next;
62  int ret;
63  int i, p = 0;
64 
65  s->key_frame = 0;
66 
67  *poutbuf_size = 0;
68  *poutbuf = NULL;
69  if (buf_size == 0)
70  return 0;
71 
72  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
73  next = buf_size;
74  } else {
75  if (!mp->in_sync) {
76  // Not in sync - find a major sync header
77 
78  for (i = 0; i < buf_size; i++) {
79  mp->pc.state = (mp->pc.state << 8) | buf[i];
80  if ((mp->pc.state & 0xfffffffe) == 0xf8726fba &&
81  // ignore if we do not have the data for the start of header
82  mp->pc.index + i >= 7) {
83  mp->in_sync = 1;
84  mp->bytes_left = 0;
85  break;
86  }
87  }
88 
89  if (!mp->in_sync) {
90  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
91  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
92  return buf_size;
93  }
94 
95  if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) {
96  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
97  return ret;
98  }
99 
100  return i - 7;
101  }
102 
103  if (mp->bytes_left == 0) {
104  // Find length of this packet
105 
106  /* Copy overread bytes from last frame into buffer. */
107  for(; mp->pc.overread>0; mp->pc.overread--) {
108  mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++];
109  }
110 
111  if (mp->pc.index + buf_size < 2) {
112  if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1)
113  av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n");
114  return buf_size;
115  }
116 
117  mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8)
118  | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]);
119  mp->bytes_left = (mp->bytes_left & 0xfff) * 2;
120  if (mp->bytes_left <= 0) { // prevent infinite loop
121  goto lost_sync;
122  }
123  mp->bytes_left -= mp->pc.index;
124  }
125 
126  next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left;
127 
128  if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) {
129  mp->bytes_left -= buf_size;
130  return buf_size;
131  }
132 
133  mp->bytes_left = 0;
134  }
135 
136  sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba;
137 
138  if (!sync_present) {
139  /* The first nibble of a frame is a parity check of the 4-byte
140  * access unit header and all the 2- or 4-byte substream headers. */
141  // Only check when this isn't a sync frame - syncs have a checksum.
142 
143  s->key_frame = 0;
144 
145  parity_bits = 0;
146  for (i = -1; i < mp->num_substreams; i++) {
147  parity_bits ^= buf[p++];
148  parity_bits ^= buf[p++];
149 
150  if (i < 0 || buf[p-2] & 0x80) {
151  parity_bits ^= buf[p++];
152  parity_bits ^= buf[p++];
153  }
154  }
155 
156  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
157  av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n");
158  goto lost_sync;
159  }
160  } else {
161  GetBitContext gb;
163 
164  init_get_bits(&gb, buf + 4, (buf_size - 4) << 3);
165  if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0)
166  goto lost_sync;
167 
168  s->key_frame = 1;
169 
170  avctx->bits_per_raw_sample = mh.group1_bits;
171  if (avctx->bits_per_raw_sample > 16)
172  avctx->sample_fmt = AV_SAMPLE_FMT_S32;
173  else
174  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
175  avctx->sample_rate = mh.group1_samplerate;
176  avctx->frame_size =
177  s->duration = mh.access_unit_size;
178 
180  if (mh.stream_type == 0xbb) {
181  /* MLP stream */
182  av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_mlp);
183  } else { /* mh.stream_type == 0xba */
184  /* TrueHD stream */
185  if (!mh.channels_thd_stream2) {
186  av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream1);
187  } else {
188  av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream2);
189  }
190  }
191 
192  if (!mh.is_vbr) /* Stream is CBR */
193  avctx->bit_rate = mh.peak_bitrate;
194 
195  mp->num_substreams = mh.num_substreams;
196  }
197 
198  *poutbuf = buf;
199  *poutbuf_size = buf_size;
200 
201  return next;
202 
203 lost_sync:
204  mp->in_sync = 0;
205  return 1;
206 }
207 
210  .priv_data_size = sizeof(MLPParseContext),
211  .init = mlp_init,
212  .parse = mlp_parse,
214 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1051
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1024
ff_parse_close
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:298
mp
static double mp(int i, double w0, double r)
Definition: af_atilt.c:60
parser_internal.h
mh
#define mh
Definition: vf_colormatrix.c:105
MLPParseContext::bytes_left
int bytes_left
Definition: mlp_parser.c:40
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:503
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:517
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
ParseContext
Definition: parser.h:28
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
GetBitContext
Definition: get_bits.h:109
av_cold
#define av_cold
Definition: attributes.h:106
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:252
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1553
get_bits.h
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:481
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: apv_parser.c:46
MLPParseContext::num_substreams
int num_substreams
Definition: mlp_parser.c:44
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1031
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:96
MLPParseContext::in_sync
int in_sync
Definition: mlp_parser.c:42
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:53
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:211
FFCodecParser
Definition: parser_internal.h:29
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
mlp_parse.h
ff_mlp_parser
const FFCodecParser ff_mlp_parser
Definition: mlp_parser.c:208
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2609
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
MLPParseContext
Definition: mlp_parser.c:36
internal.h
MLPHeaderInfo
Definition: mlp_parse.h:30
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
parser.h
ff_mlp_init_crc
av_cold void ff_mlp_init_crc(void)
Definition: mlp.c:83
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
mlp_init
static av_cold int mlp_init(AVCodecParserContext *s)
Definition: mlp_parser.c:47
AVCodecParserContext
Definition: avcodec.h:2575
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:431
MLPParseContext::pc
ParseContext pc
Definition: mlp_parser.c:38
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
mlp.h
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
AV_CODEC_ID_MLP
@ AV_CODEC_ID_MLP
Definition: codec_id.h:488