FFmpeg
Loading...
Searching...
No Matches
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
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)
173 else
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
203lost_sync:
204 mp->in_sync = 0;
205 return 1;
206}
207
static double mp(int i, double w0, double r)
Definition af_atilt.c:60
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:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2652
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
bitstream reader API header.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:497
@ AV_CODEC_ID_MLP
Definition codec_id.h:482
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
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.
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define AV_RB32(p)
#define av_cold
Definition attributes.h:117
common internal API header
av_cold void ff_mlp_init_crc(void)
Definition mlp.c:81
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
const FFCodecParser ff_mlp_parser
Definition mlp_parser.c:208
static av_cold int mlp_init(AVCodecParserContext *s)
Definition mlp_parser.c:47
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
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
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:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1572
int sample_rate
samples per second
Definition avcodec.h:1040
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
ParseContext pc
Definition mlp_parser.c:38
#define av_log(a,...)
#define mh