FFmpeg
dvaudiodec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Laurent Aimar
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 
22 #include "libavutil/intreadwrite.h"
23 #include "avcodec.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26 #include "dvaudio.h"
27 
28 typedef struct DVAudioContext {
30  int is_12bit;
31  int is_pal;
32  int16_t shuffle[2000];
34 
36 {
37  DVAudioContext *s = avctx->priv_data;
38  int i;
39 
40  if (avctx->codec_tag == 0x0215) {
41  s->block_size = 7200;
42  } else if (avctx->codec_tag == 0x0216) {
43  s->block_size = 8640;
44  } else if (avctx->block_align == 7200 ||
45  avctx->block_align == 8640) {
46  s->block_size = avctx->block_align;
47  } else {
48  return AVERROR(EINVAL);
49  }
50 
51  s->is_pal = s->block_size == 8640;
52  s->is_12bit = avctx->bits_per_coded_sample == 12;
56 
57  for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) {
58  const unsigned a = s->is_pal ? 18 : 15;
59  const unsigned b = 3 * a;
60 
61  s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
62  (2 + s->is_12bit) * (i / b) + 8;
63  }
64 
65  return 0;
66 }
67 
68 static inline uint16_t dv_audio_12to16(uint16_t sample)
69 {
70  uint16_t shift, result;
71 
72  sample = (sample < 0x800) ? sample : sample | 0xf000;
73  shift = (sample & 0xf00) >> 8;
74 
75  if (shift < 0x2 || shift > 0xd) {
76  result = sample;
77  } else if (shift < 0x8) {
78  shift--;
79  result = (sample - (256 * shift)) << shift;
80  } else {
81  shift = 0xe - shift;
82  result = ((sample + ((256 * shift) + 1)) << shift) - 1;
83  }
84 
85  return result;
86 }
87 
89  int *got_frame_ptr, AVPacket *pkt)
90 {
91  DVAudioContext *s = avctx->priv_data;
92  const uint8_t *src = pkt->data;
93  int16_t *dst;
94  int ret, i;
95 
96  if (pkt->size < s->block_size)
97  return AVERROR_INVALIDDATA;
98 
99  frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal);
100  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
101  return ret;
102  dst = (int16_t *)frame->data[0];
103 
104  for (i = 0; i < frame->nb_samples; i++) {
105  const uint8_t *v = &src[s->shuffle[i]];
106 
107  if (s->is_12bit) {
108  *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
109  *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
110  } else {
111  *dst++ = AV_RB16(&v[0]);
112  *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
113  }
114  }
115 
116  *got_frame_ptr = 1;
117 
118  return s->block_size;
119 }
120 
122  .p.name = "dvaudio",
123  CODEC_LONG_NAME("Ulead DV Audio"),
124  .p.type = AVMEDIA_TYPE_AUDIO,
125  .p.id = AV_CODEC_ID_DVAUDIO,
126  .init = decode_init,
128  .p.capabilities = AV_CODEC_CAP_DR1,
129  .priv_data_size = sizeof(DVAudioContext),
130 };
DVAudioContext::is_12bit
int is_12bit
Definition: dvaudiodec.c:30
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:383
dvaudio.h
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVPacket::data
uint8_t * data
Definition: packet.h:491
b
#define b
Definition: input.c:41
FFCodec
Definition: codec_internal.h:127
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
pkt
AVPacket * pkt
Definition: movenc.c:59
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
DVAudioContext
Definition: dvaudiodec.c:28
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
DVAudioContext::block_size
int block_size
Definition: dvaudiodec.c:29
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dvaudiodec.c:35
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
DVAudioContext::shuffle
int16_t shuffle[2000]
Definition: dvaudiodec.c:32
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:492
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
codec_internal.h
shift
static int shift(int a, int b)
Definition: bonk.c:262
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
sample
#define sample
Definition: flacdsp_template.c:44
dv_audio_12to16
static uint16_t dv_audio_12to16(uint16_t sample)
Definition: dvaudiodec.c:68
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: dvaudiodec.c:88
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1510
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
ff_dvaudio_decoder
const FFCodec ff_dvaudio_decoder
Definition: dvaudiodec.c:121
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1113
AVCodecContext
main external API structure.
Definition: avcodec.h:441
channel_layout.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:640
AV_CODEC_ID_DVAUDIO
@ AV_CODEC_ID_DVAUDIO
Definition: codec_id.h:448
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:466
dv_get_audio_sample_count
static int dv_get_audio_sample_count(const uint8_t *buffer, int dsf)
Definition: dvaudio.h:24
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
DVAudioContext::is_pal
int is_pal
Definition: dvaudiodec.c:31
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
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