FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libdcadec.c
Go to the documentation of this file.
1 /*
2  * libdcadec decoder wrapper
3  * Copyright (C) 2015 Hendrik Leppkes
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 #include <libdcadec/dca_context.h>
23 
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
27 
28 #include "avcodec.h"
29 #include "dca.h"
30 #include "dca_syncwords.h"
31 #include "internal.h"
32 
33 typedef struct DCADecContext {
34  struct dcadec_context *ctx;
38 
39 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
40  int *got_frame_ptr, AVPacket *avpkt)
41 {
42  DCADecContext *s = avctx->priv_data;
43  AVFrame *frame = data;
44  int ret, i, k;
45  int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
46  uint32_t mrk;
47  uint8_t *input = avpkt->data;
48  int input_size = avpkt->size;
49 
50  /* convert bytestream syntax to RAW BE format if required */
51  if (input_size < 8) {
52  av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
53  return AVERROR_INVALIDDATA;
54  }
55  mrk = AV_RB32(input);
56  if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
58  if (!s->buffer)
59  return AVERROR(ENOMEM);
60 
61  for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
62  ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
63 
64  if (ret < 0)
65  return ret;
66 
67  input = s->buffer;
68  input_size = ret;
69  }
70 
71  if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
72  av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
73  return AVERROR_EXTERNAL;
74  }
75  if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
76  &sample_rate, &bits_per_sample, &profile)) < 0) {
77  av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
78  return AVERROR_EXTERNAL;
79  }
80 
81  avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
82  avctx->channel_layout = channel_mask;
83  avctx->sample_rate = sample_rate;
84 
85  if (bits_per_sample == 16)
87  else if (bits_per_sample > 16 && bits_per_sample <= 24)
89  else {
90  av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
91  bits_per_sample);
92  return AVERROR(ENOSYS);
93  }
94 
95  avctx->bits_per_raw_sample = bits_per_sample;
96 
97  switch (profile) {
98  case DCADEC_PROFILE_DS:
99  avctx->profile = FF_PROFILE_DTS;
100  break;
101  case DCADEC_PROFILE_DS_96_24:
102  avctx->profile = FF_PROFILE_DTS_96_24;
103  break;
104  case DCADEC_PROFILE_DS_ES:
105  avctx->profile = FF_PROFILE_DTS_ES;
106  break;
107  case DCADEC_PROFILE_HD_HRA:
109  break;
110  case DCADEC_PROFILE_HD_MA:
111  avctx->profile = FF_PROFILE_DTS_HD_MA;
112  break;
113  case DCADEC_PROFILE_EXPRESS:
115  break;
116  case DCADEC_PROFILE_UNKNOWN:
117  default:
118  avctx->profile = FF_PROFILE_UNKNOWN;
119  break;
120  }
121 
122  /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
123  if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
124  struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
125  avctx->bit_rate = info->bit_rate;
126  dcadec_context_free_core_info(info);
127  } else
128  avctx->bit_rate = 0;
129 
130  frame->nb_samples = nsamples;
131  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
132  return ret;
133 
134  for (i = 0; i < avctx->channels; i++) {
135  if (frame->format == AV_SAMPLE_FMT_S16P) {
136  int16_t *plane = (int16_t *)frame->extended_data[i];
137  for (k = 0; k < nsamples; k++)
138  plane[k] = samples[i][k];
139  } else {
140  int32_t *plane = (int32_t *)frame->extended_data[i];
141  int shift = 32 - bits_per_sample;
142  for (k = 0; k < nsamples; k++)
143  plane[k] = samples[i][k] << shift;
144  }
145  }
146 
147  *got_frame_ptr = 1;
148 
149  return avpkt->size;
150 }
151 
153 {
154  DCADecContext *s = avctx->priv_data;
155  dcadec_context_clear(s->ctx);
156 }
157 
159 {
160  DCADecContext *s = avctx->priv_data;
161 
162  dcadec_context_destroy(s->ctx);
163  s->ctx = NULL;
164 
165  av_freep(&s->buffer);
166 
167  return 0;
168 }
169 
171 {
172  DCADecContext *s = avctx->priv_data;
173  int flags = 0;
174 
175  /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
176  if (avctx->flags & CODEC_FLAG_BITEXACT)
177  flags |= DCADEC_FLAG_CORE_BIT_EXACT;
178 
179  s->ctx = dcadec_context_create(flags);
180  if (!s->ctx)
181  return AVERROR(ENOMEM);
182 
184  avctx->bits_per_raw_sample = 24;
185 
186  return 0;
187 }
188 
189 static const AVProfile profiles[] = {
190  { FF_PROFILE_DTS, "DTS" },
191  { FF_PROFILE_DTS_ES, "DTS-ES" },
192  { FF_PROFILE_DTS_96_24, "DTS 96/24" },
193  { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
194  { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
195  { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
196  { FF_PROFILE_UNKNOWN },
197 };
198 
200  .name = "libdcadec",
201  .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
202  .type = AVMEDIA_TYPE_AUDIO,
203  .id = AV_CODEC_ID_DTS,
204  .priv_data_size = sizeof(DCADecContext),
205  .init = dcadec_init,
207  .close = dcadec_close,
208  .flush = dcadec_flush,
209  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
212  .profiles = NULL_IF_CONFIG_SMALL(profiles),
213 };
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
struct dcadec_context * ctx
Definition: libdcadec.c:34
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold void dcadec_flush(AVCodecContext *avctx)
Definition: libdcadec.c:152
int size
Definition: avcodec.h:1163
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2727
int profile
profile
Definition: avcodec.h:2835
AVCodec.
Definition: avcodec.h:3181
#define FF_PROFILE_DTS_EXPRESS
Definition: avcodec.h:2855
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define FF_PROFILE_DTS_ES
Definition: avcodec.h:2851
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2836
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:85
int buffer_size
Definition: libdcadec.c:36
#define FF_PROFILE_DTS_96_24
Definition: avcodec.h:2852
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:759
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:478
int profile
Definition: mxfenc.c:1804
#define AVERROR(e)
Definition: error.h:43
#define FF_PROFILE_DTS
Definition: avcodec.h:2850
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1335
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:630
#define FF_PROFILE_DTS_HD_HRA
Definition: avcodec.h:2853
int bit_rate
the average bitrate
Definition: avcodec.h:1305
audio channel layout utility functions
signed 32 bits, planar
Definition: samplefmt.h:69
ret
Definition: avfilter.c:974
int32_t
int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
Convert bitstream to one representation based on sync marker.
Definition: dca.c:39
static int dcadec_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libdcadec.c:39
static void flush(AVCodecContext *avctx)
Definition: aacdec.c:514
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
static av_cold int dcadec_init(AVCodecContext *avctx)
Definition: libdcadec.c:170
sample_rate
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int sample_rate
samples per second
Definition: avcodec.h:1985
main external API structure.
Definition: avcodec.h:1241
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
static const AVProfile profiles[]
Definition: libdcadec.c:189
static av_cold int dcadec_close(AVCodecContext *avctx)
Definition: libdcadec.c:158
static int flags
Definition: cpu.c:47
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
common internal api header.
common internal and external API header
#define CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:856
AVProfile.
Definition: avcodec.h:3169
uint8_t * buffer
Definition: libdcadec.c:35
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:2854
void * priv_data
Definition: avcodec.h:1283
int channels
number of audio channels
Definition: avcodec.h:1986
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:68
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
This structure stores compressed data.
Definition: avcodec.h:1139
AVCodec ff_libdcadec_decoder
Definition: libdcadec.c:199
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
for(j=16;j >0;--j)