FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dca.c
Go to the documentation of this file.
1 /*
2  * DCA compatible decoder data
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "libavutil/error.h"
29 
30 #include "dca.h"
31 #include "dca_core.h"
32 #include "dca_syncwords.h"
33 #include "get_bits.h"
34 #include "put_bits.h"
35 
36 const uint32_t avpriv_dca_sample_rates[16] = {
37  0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
38  12000, 24000, 48000, 96000, 192000
39 };
40 
41 const uint32_t ff_dca_sampling_freqs[16] = {
42  8000, 16000, 32000, 64000, 128000, 22050, 44100, 88200,
43  176400, 352800, 12000, 24000, 48000, 96000, 192000, 384000,
44 };
45 
47  0, 1, 2, 3, 4, 1, 2, 3, 4, 4, 0, 1, 2, 3, 4, 4
48 };
49 
51  16, 16, 20, 20, 0, 24, 24, 0
52 };
53 
54 int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
55  int max_size)
56 {
57  uint32_t mrk;
58  int i, tmp;
59  PutBitContext pb;
60 
61  if ((unsigned) src_size > (unsigned) max_size)
62  src_size = max_size;
63 
64  mrk = AV_RB32(src);
65  switch (mrk) {
68  memcpy(dst, src, src_size);
69  return src_size;
71  for (i = 0; i < (src_size + 1) >> 1; i++) {
72  AV_WB16(dst, AV_RL16(src));
73  src += 2;
74  dst += 2;
75  }
76  return src_size;
79  init_put_bits(&pb, dst, max_size);
80  for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
81  tmp = ((mrk == DCA_SYNCWORD_CORE_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
82  put_bits(&pb, 14, tmp);
83  }
84  flush_put_bits(&pb);
85  return (put_bits_count(&pb) + 7) >> 3;
86  default:
87  return AVERROR_INVALIDDATA;
88  }
89 }
90 
92 {
93  if (get_bits_long(gb, 32) != DCA_SYNCWORD_CORE_BE)
95 
96  h->normal_frame = get_bits1(gb);
97  h->deficit_samples = get_bits(gb, 5) + 1;
100 
101  h->crc_present = get_bits1(gb);
102  h->npcmblocks = get_bits(gb, 7) + 1;
103  if (h->npcmblocks & (DCA_SUBBAND_SAMPLES - 1))
105 
106  h->frame_size = get_bits(gb, 14) + 1;
107  if (h->frame_size < 96)
109 
110  h->audio_mode = get_bits(gb, 6);
111  if (h->audio_mode >= DCA_AMODE_COUNT)
112  return DCA_PARSE_ERROR_AMODE;
113 
114  h->sr_code = get_bits(gb, 4);
117 
118  h->br_code = get_bits(gb, 5);
119  if (get_bits1(gb))
121 
122  h->drc_present = get_bits1(gb);
123  h->ts_present = get_bits1(gb);
124  h->aux_present = get_bits1(gb);
125  h->hdcd_master = get_bits1(gb);
126  h->ext_audio_type = get_bits(gb, 3);
127  h->ext_audio_present = get_bits1(gb);
128  h->sync_ssf = get_bits1(gb);
129  h->lfe_present = get_bits(gb, 2);
132 
133  h->predictor_history = get_bits1(gb);
134  if (h->crc_present)
135  skip_bits(gb, 16);
136  h->filter_perfect = get_bits1(gb);
137  h->encoder_rev = get_bits(gb, 4);
138  h->copy_hist = get_bits(gb, 2);
139  h->pcmr_code = get_bits(gb, 3);
142 
143  h->sumdiff_front = get_bits1(gb);
144  h->sumdiff_surround = get_bits1(gb);
145  h->dn_code = get_bits(gb, 4);
146  return 0;
147 }
148 
150 {
151  GetBitContext gb;
152  int ret;
153 
154  ret = init_get_bits8(&gb, buf, size);
155  if (ret < 0)
156  return ret;
157 
158  if (ff_dca_parse_core_frame_header(h, &gb) < 0)
159  return AVERROR_INVALIDDATA;
160 
161  return 0;
162 }
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
uint8_t sumdiff_surround
Surround sum/difference flag.
Definition: dca.h:74
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:207
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
uint8_t npcmblocks
Number of PCM sample blocks.
Definition: dca.h:55
uint8_t hdcd_master
HDCD mastering flag.
Definition: dca.h:63
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:87
uint8_t copy_hist
Copy history.
Definition: dca.h:71
#define src
Definition: vp8dsp.c:254
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
const uint8_t ff_dca_freq_ranges[16]
Definition: dca.c:46
uint8_t encoder_rev
Encoder software revision.
Definition: dca.h:70
uint8_t aux_present
Auxiliary data flag.
Definition: dca.h:62
uint8_t
#define DCA_SYNCWORD_CORE_14B_BE
Definition: dca_syncwords.h:24
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:87
bitstream reader API header.
uint8_t pcmr_code
Source PCM resolution.
Definition: dca.h:72
ptrdiff_t size
Definition: opengl_enc.c:101
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
#define DCA_SYNCWORD_CORE_BE
Definition: dca_syncwords.h:22
uint8_t crc_present
CRC present flag.
Definition: dca.h:54
uint8_t ext_audio_type
Extension audio descriptor flag.
Definition: dca.h:64
const uint8_t ff_dca_bits_per_sample[8]
Definition: dca.c:50
uint8_t predictor_history
Predictor history flag switch.
Definition: dca.h:68
#define DCA_PCMBLOCK_SAMPLES
Definition: dca_core.h:46
error code definitions
int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size)
Parse and validate core frame header.
Definition: dca.c:149
const uint32_t ff_dca_sampling_freqs[16]
Definition: dca.c:41
#define DCA_SYNCWORD_CORE_14B_LE
Definition: dca_syncwords.h:25
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
uint8_t lfe_present
Low frequency effects flag.
Definition: dca.h:67
uint8_t normal_frame
Frame type.
Definition: dca.h:52
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:54
uint8_t audio_mode
Audio channel arrangement.
Definition: dca.h:57
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:36
uint8_t br_code
Transmission bit rate.
Definition: dca.h:59
uint8_t deficit_samples
Deficit sample count.
Definition: dca.h:53
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
#define DCA_SYNCWORD_CORE_LE
Definition: dca_syncwords.h:23
void * buf
Definition: avisynth_c.h:690
uint8_t dn_code
Dialog normalization / unspecified.
Definition: dca.h:75
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
uint8_t ts_present
Embedded time stamp flag.
Definition: dca.h:61
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:346
uint16_t frame_size
Primary frame byte size.
Definition: dca.h:56
uint8_t sync_ssf
Audio sync word insertion flag.
Definition: dca.h:66
uint8_t sr_code
Core audio sampling frequency.
Definition: dca.h:58
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
uint8_t drc_present
Embedded dynamic range flag.
Definition: dca.h:60
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
uint8_t filter_perfect
Multirate interpolator switch.
Definition: dca.h:69
int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb)
Parse and validate core frame header.
Definition: dca.c:91
#define DCA_SYNCWORD_SUBSTREAM
Definition: dca_syncwords.h:32
#define DCA_SUBBAND_SAMPLES
Definition: dca_core.h:45
uint8_t ext_audio_present
Extended coding flag.
Definition: dca.h:65
uint8_t sumdiff_front
Front sum/difference flag.
Definition: dca.h:73
static uint8_t tmp[11]
Definition: aes_ctr.c:26
bitstream writer API