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 "internal.h"
25 #include "dvaudio.h"
26 
27 typedef struct DVAudioContext {
29  int is_12bit;
30  int is_pal;
31  int16_t shuffle[2000];
33 
35 {
36  DVAudioContext *s = avctx->priv_data;
37  int i;
38 
39  if (avctx->channels != 2) {
40  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
41  return AVERROR(EINVAL);
42  }
43 
44  if (avctx->codec_tag == 0x0215) {
45  s->block_size = 7200;
46  } else if (avctx->codec_tag == 0x0216) {
47  s->block_size = 8640;
48  } else if (avctx->block_align == 7200 ||
49  avctx->block_align == 8640) {
50  s->block_size = avctx->block_align;
51  } else {
52  return AVERROR(EINVAL);
53  }
54 
55  s->is_pal = s->block_size == 8640;
56  s->is_12bit = avctx->bits_per_coded_sample == 12;
59 
60  for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) {
61  const unsigned a = s->is_pal ? 18 : 15;
62  const unsigned b = 3 * a;
63 
64  s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
65  (2 + s->is_12bit) * (i / b) + 8;
66  }
67 
68  return 0;
69 }
70 
71 static inline uint16_t dv_audio_12to16(uint16_t sample)
72 {
73  uint16_t shift, result;
74 
75  sample = (sample < 0x800) ? sample : sample | 0xf000;
76  shift = (sample & 0xf00) >> 8;
77 
78  if (shift < 0x2 || shift > 0xd) {
79  result = sample;
80  } else if (shift < 0x8) {
81  shift--;
82  result = (sample - (256 * shift)) << shift;
83  } else {
84  shift = 0xe - shift;
85  result = ((sample + ((256 * shift) + 1)) << shift) - 1;
86  }
87 
88  return result;
89 }
90 
91 static int decode_frame(AVCodecContext *avctx, void *data,
92  int *got_frame_ptr, AVPacket *pkt)
93 {
94  DVAudioContext *s = avctx->priv_data;
95  AVFrame *frame = data;
96  const uint8_t *src = pkt->data;
97  int16_t *dst;
98  int ret, i;
99 
100  if (pkt->size < s->block_size)
101  return AVERROR_INVALIDDATA;
102 
103  frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal);
104  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
105  return ret;
106  dst = (int16_t *)frame->data[0];
107 
108  for (i = 0; i < frame->nb_samples; i++) {
109  const uint8_t *v = &src[s->shuffle[i]];
110 
111  if (s->is_12bit) {
112  *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
113  *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
114  } else {
115  *dst++ = AV_RB16(&v[0]);
116  *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
117  }
118  }
119 
120  *got_frame_ptr = 1;
121 
122  return s->block_size;
123 }
124 
126  .name = "dvaudio",
127  .long_name = NULL_IF_CONFIG_SMALL("Ulead DV Audio"),
128  .type = AVMEDIA_TYPE_AUDIO,
129  .id = AV_CODEC_ID_DVAUDIO,
130  .init = decode_init,
131  .decode = decode_frame,
132  .capabilities = AV_CODEC_CAP_DR1,
133  .priv_data_size = sizeof(DVAudioContext),
134  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
135 };
AVCodec
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
DVAudioContext::is_12bit
int is_12bit
Definition: dvaudiodec.c:29
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
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1043
dvaudio.h
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
b
#define b
Definition: input.c:40
data
const char data[16]
Definition: mxf.c:143
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:27
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
DVAudioContext::block_size
int block_size
Definition: dvaudiodec.c:28
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dvaudiodec.c:34
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
src
#define src
Definition: vp8dsp.c:255
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
DVAudioContext::shuffle
int16_t shuffle[2000]
Definition: dvaudiodec.c:31
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
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:374
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
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1000
sample
#define sample
Definition: flacdsp_template.c:44
dv_audio_12to16
static uint16_t dv_audio_12to16(uint16_t sample)
Definition: dvaudiodec.c:71
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: dvaudiodec.c:91
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
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
ff_dvaudio_decoder
const AVCodec ff_dvaudio_decoder
Definition: dvaudiodec.c:125
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1418
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
avcodec.h
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:1029
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:383
channel_layout.h
AV_CODEC_ID_DVAUDIO
@ AV_CODEC_ID_DVAUDIO
Definition: codec_id.h:429
shift
static int shift(int a, int b)
Definition: sonic.c:83
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:408
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:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
DVAudioContext::is_pal
int is_pal
Definition: dvaudiodec.c:30
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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