FFmpeg
truehd_core_bsf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "avcodec.h"
22 #include "bsf.h"
23 #include "get_bits.h"
24 #include "mlp_parse.h"
25 #include "mlp.h"
26 
27 typedef struct AccessUnit {
29  uint16_t offset;
30  uint16_t optional;
31 } AccessUnit;
32 
33 typedef struct TrueHDCoreContext {
34  const AVClass *class;
35 
38 
40 {
42  GetBitContext gbc;
44  AVPacket *in;
45  int ret, i, size, last_offset = 0;
46  int in_size, out_size;
47  int have_header = 0;
48  int substream_bits = 0;
49  int start, end;
50  uint16_t dts;
51 
52  ret = ff_bsf_get_packet(ctx, &in);
53  if (ret < 0)
54  return ret;
55 
56  if (in->size < 4)
57  goto fail;
58 
59  ret = init_get_bits(&gbc, in->data, 32);
60  if (ret < 0)
61  goto fail;
62 
63  skip_bits(&gbc, 4);
64  in_size = get_bits(&gbc, 12) * 2;
65  if (in_size < 4 || in_size > in->size)
66  goto fail;
67 
68  out_size = in_size;
69  dts = get_bits(&gbc, 16);
70 
71  ret = init_get_bits8(&gbc, in->data + 4, in->size - 4);
72  if (ret < 0)
73  goto fail;
74 
75  if (show_bits_long(&gbc, 32) == 0xf8726fba) {
76  if ((ret = ff_mlp_read_major_sync(ctx, &s->hdr, &gbc)) != 0)
77  goto fail;
78  have_header = 1;
79  }
80 
82  goto fail;
83 
84  start = get_bits_count(&gbc);
85  for (i = 0; i < s->hdr.num_substreams; i++) {
86  for (int j = 0; j < 4; j++)
87  units[i].bits[j] = get_bits1(&gbc);
88 
89  units[i].offset = get_bits(&gbc, 12) * 2;
90  if (i < FFMIN(s->hdr.num_substreams, 3)) {
91  last_offset = units[i].offset;
92  substream_bits += 16;
93  }
94 
95  if (units[i].bits[0]) {
96  units[i].optional = get_bits(&gbc, 16);
97  if (i < FFMIN(s->hdr.num_substreams, 3))
98  substream_bits += 16;
99  }
100  }
101  end = get_bits_count(&gbc);
102 
103  size = ((end + 7) >> 3) + 4 + last_offset;
104  if (size >= 0 && size <= in->size)
105  out_size = size;
106  if (out_size < in_size) {
107  int bpos = 0, reduce = (end - start - substream_bits) >> 4;
108  uint16_t parity_nibble = 0;
109  uint16_t auheader;
110 
111  ret = av_new_packet(out, out_size);
112  if (ret < 0)
113  goto fail;
114 
115  AV_WB16(out->data + 2, dts);
116  parity_nibble = dts;
117  out->size -= reduce * 2;
118  parity_nibble ^= out->size / 2;
119 
120  if (out_size > 8)
121  AV_WN64(out->data + out_size - 8, 0);
122  if (have_header) {
123  memcpy(out->data + 4, in->data + 4, 28);
124  out->data[16 + 4] = (out->data[16 + 4] & 0x0f) | (FFMIN(s->hdr.num_substreams, 3) << 4);
125  out->data[25 + 4] = out->data[25 + 4] & 0xfe;
126  out->data[26 + 4] = 0xff;
127  out->data[27 + 4] = 0xff;
128  AV_WL16(out->data + 4 + 26, ff_mlp_checksum16(out->data + 4, 26));
129  }
130 
131  for (i = 0; i < FFMIN(s->hdr.num_substreams, 3); i++) {
132  uint16_t substr_hdr = 0;
133 
134  substr_hdr |= (units[i].bits[0] << 15);
135  substr_hdr |= (units[i].bits[1] << 14);
136  substr_hdr |= (units[i].bits[2] << 13);
137  substr_hdr |= (units[i].bits[3] << 12);
138  substr_hdr |= (units[i].offset / 2) & 0x0FFF;
139 
140  AV_WB16(out->data + have_header * 28 + 4 + bpos, substr_hdr);
141 
142  parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
143  parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
144 
145  if (units[i].bits[0]) {
146  AV_WB16(out->data + have_header * 28 + 4 + bpos, units[i].optional);
147 
148  parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
149  parity_nibble ^= out->data[have_header * 28 + 4 + bpos++];
150  }
151  }
152 
153  parity_nibble ^= parity_nibble >> 8;
154  parity_nibble ^= parity_nibble >> 4;
155  parity_nibble &= 0xF;
156 
157  memcpy(out->data + have_header * 28 + 4 + bpos,
158  in->data + 4 + (end >> 3),
159  out_size - (4 + (end >> 3)));
160  auheader = (parity_nibble ^ 0xF) << 12;
161  auheader |= (out->size / 2) & 0x0fff;
162  AV_WB16(out->data, auheader);
163 
164  ret = av_packet_copy_props(out, in);
165  } else {
166  av_packet_move_ref(out, in);
167  }
168 
169 fail:
170  if (ret < 0)
171  av_packet_unref(out);
172  av_packet_free(&in);
173 
174  return ret;
175 }
176 
178 {
179  TrueHDCoreContext *s = ctx->priv_data;
180  memset(&s->hdr, 0, sizeof(s->hdr));
181 }
182 
183 static const enum AVCodecID codec_ids[] = {
185 };
186 
188  .name = "truehd_core",
189  .priv_data_size = sizeof(TrueHDCoreContext),
192  .codec_ids = codec_ids,
193 };
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:602
static void flush(AVCodecContext *avctx)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
const AVBitStreamFilter ff_truehd_core_bsf
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
Definition: mlp.c:85
int num_substreams
Number of substreams within stream.
Definition: mlp_parse.h:56
The bitstream filter state.
Definition: avcodec.h:5759
int size
Definition: avcodec.h:1478
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:136
int out_size
Definition: movenc.c:55
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5780
static void truehd_core_flush(AVBSFContext *ctx)
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const char * name
Definition: avcodec.h:5809
uint8_t * data
Definition: avcodec.h:1477
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:655
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:100
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
static enum AVCodecID codec_ids[]
#define fail()
Definition: checkasm.h:120
uint16_t optional
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:565
#define FFMIN(a, b)
Definition: common.h:96
MLPHeaderInfo hdr
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t bits[4]
#define s(width, name)
Definition: cbs_vp9.c:257
Libavcodec external API header.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
uint16_t offset
static int truehd_core_filter(AVBSFContext *ctx, AVPacket *out)
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:216
#define MAX_SUBSTREAMS
Maximum number of substreams that can be decoded.
Definition: mlp.h:48
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
FILE * out
Definition: movenc.c:54
void INT64 start
Definition: avisynth_c.h:766
This structure stores compressed data.
Definition: avcodec.h:1454
#define AV_WN64(p, v)
Definition: intreadwrite.h:380