FFmpeg
mlpdec.c
Go to the documentation of this file.
1 /*
2  * MLP and TrueHD demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2005 Alex Beregszaszi
5  * Copyright (c) 2015 Carl Eugen Hoyos
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "internal.h"
27 #include "rawdec.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavcodec/mlp.h"
30 #include "libavcodec/mlp_parse.h"
31 
32 static int av_always_inline mlp_thd_probe(const AVProbeData *p, uint32_t sync)
33 {
34  const uint8_t *buf, *last_buf = p->buf, *end = p->buf + p->buf_size;
35  int frames = 0, valid = 0, size = 0;
36  int nsubframes = 0;
37 
38  for (buf = p->buf; buf + 8 <= end; buf++) {
39  if (AV_RB32(buf + 4) == sync) {
40  frames++;
41  if (last_buf + size == buf) {
42  valid += 1 + nsubframes / 8;
43  }
44  nsubframes = 0;
45  last_buf = buf;
46  size = (AV_RB16(buf) & 0xfff) * 2;
47  } else if (buf - last_buf == size) {
48  nsubframes++;
49  size += (AV_RB16(buf) & 0xfff) * 2;
50  }
51  }
52  if (valid >= 100)
53  return AVPROBE_SCORE_MAX;
54  return 0;
55 }
56 
58 {
60 
61  if (ret < 0)
62  return ret;
63 
64  ret = ffio_ensure_seekback(s->pb, 10);
65  if (ret == 0) {
66  uint8_t buffer[10];
67  int read, sample_rate = 0;
68 
69  read = avio_read(s->pb, buffer, 10);
70  if (read == 10) {
71  switch (buffer[7]) {
72  case SYNC_TRUEHD:
74  break;
75  case SYNC_MLP:
77  break;
78  }
79 
80  if (sample_rate)
81  avpriv_set_pts_info(s->streams[0], 64, 1, sample_rate);
82  }
83 
84  if (read > 0)
85  avio_seek(s->pb, -read, SEEK_CUR);
86  }
87 
88  return 0;
89 }
90 
91 #if CONFIG_MLP_DEMUXER
92 static int mlp_probe(const AVProbeData *p)
93 {
94  return mlp_thd_probe(p, 0xf8726fbb);
95 }
96 
98  .name = "mlp",
99  .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
100  .read_probe = mlp_probe,
101  .read_header = mlp_read_header,
102  .read_packet = ff_raw_read_partial_packet,
104  .extensions = "mlp",
105  .raw_codec_id = AV_CODEC_ID_MLP,
106  .priv_data_size = sizeof(FFRawDemuxerContext),
107  .priv_class = &ff_raw_demuxer_class,
108 };
109 #endif
110 
111 #if CONFIG_TRUEHD_DEMUXER
112 static int thd_probe(const AVProbeData *p)
113 {
114  return mlp_thd_probe(p, 0xf8726fba);
115 }
116 
118  .name = "truehd",
119  .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
120  .read_probe = thd_probe,
121  .read_header = mlp_read_header,
122  .read_packet = ff_raw_read_partial_packet,
124  .extensions = "thd",
125  .raw_codec_id = AV_CODEC_ID_TRUEHD,
126  .priv_data_size = sizeof(FFRawDemuxerContext),
127  .priv_class = &ff_raw_demuxer_class,
128 };
129 #endif
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:475
mlp_thd_probe
static int av_always_inline mlp_thd_probe(const AVProbeData *p, uint32_t sync)
Definition: mlpdec.c:32
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:450
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:467
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
ff_mlp_demuxer
const AVInputFormat ff_mlp_demuxer
SYNC_TRUEHD
#define SYNC_TRUEHD
Definition: mlp.h:28
FFRawDemuxerContext
Definition: rawdec.h:36
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:476
AVInputFormat
Definition: avformat.h:650
ff_raw_read_partial_packet
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:34
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
ff_truehd_demuxer
const AVInputFormat ff_truehd_demuxer
ff_raw_demuxer_class
const AVClass ff_raw_demuxer_class
Definition: rawdec.c:142
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
rawdec.h
ff_raw_audio_read_header
int ff_raw_audio_read_header(AVFormatContext *s)
Definition: rawdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
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
mlp_read_header
static int mlp_read_header(AVFormatContext *s)
Definition: mlpdec.c:57
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1055
mlp_parse.h
avio_internal.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
avformat.h
SYNC_MLP
#define SYNC_MLP
Definition: mlp.h:27
mlp_samplerate
static int mlp_samplerate(int in)
Definition: mlp_parse.h:84
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:641
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:1196
mlp.h
AV_CODEC_ID_MLP
@ AV_CODEC_ID_MLP
Definition: codec_id.h:452
AV_RB16
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_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98