FFmpeg
pcm-dvd.c
Go to the documentation of this file.
1 /*
2  * LPCM codecs for PCM formats found in Video DVD streams
3  * Copyright (c) 2013 Christian Schmidt
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 /**
23  * @file
24  * LPCM codecs for PCM formats found in Video DVD streams
25  */
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 
31 typedef struct PCMDVDContext {
32  uint32_t last_header; // Cached header to see if parsing is needed
33  int block_size; // Size of a block of samples in bytes
34  int last_block_size; // Size of the last block of samples in bytes
35  int samples_per_block; // Number of samples per channel per block
36  int groups_per_block; // Number of 20/24-bit sample groups per block
37  uint8_t *extra_samples; // Pointer to leftover samples from a frame
38  int extra_sample_count; // Number of leftover samples in the buffer
40 
42 {
43  PCMDVDContext *s = avctx->priv_data;
44 
45  /* Invalid header to force parsing of the first header */
46  s->last_header = -1;
47  /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
48  if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
49  return AVERROR(ENOMEM);
50 
51  return 0;
52 }
53 
55 {
56  PCMDVDContext *s = avctx->priv_data;
57 
58  av_freep(&s->extra_samples);
59 
60  return 0;
61 }
62 
64 {
65  /* no traces of 44100 and 32000Hz in any commercial software or player */
66  static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
67  PCMDVDContext *s = avctx->priv_data;
68  int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);
69 
70  /* early exit if the header didn't change apart from the frame number */
71  if (s->last_header == header_int)
72  return 0;
73  s->last_header = -1;
74 
75  if (avctx->debug & FF_DEBUG_PICT_INFO)
76  av_log(avctx, AV_LOG_DEBUG, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
77  header[0], header[1], header[2]);
78  /*
79  * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
80  * header[1] quant (2), freq(2), reserved(1), channels(3)
81  * header[2] dynamic range control (0x80 = off)
82  */
83 
84  /* Discard potentially existing leftover samples from old channel layout */
85  s->extra_sample_count = 0;
86 
87  /* get the sample depth and derive the sample format from it */
88  avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
89  if (avctx->bits_per_coded_sample == 28) {
90  av_log(avctx, AV_LOG_ERROR,
91  "PCM DVD unsupported sample depth %i\n",
92  avctx->bits_per_coded_sample);
93  return AVERROR_INVALIDDATA;
94  }
95  avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
98 
99  /* get the sample rate */
100  avctx->sample_rate = frequencies[header[1] >> 4 & 3];
101 
102  /* get the number of channels */
103  avctx->channels = 1 + (header[1] & 7);
104  /* calculate the bitrate */
105  avctx->bit_rate = avctx->channels *
106  avctx->sample_rate *
107  avctx->bits_per_coded_sample;
108 
109  /* 4 samples form a group in 20/24-bit PCM on DVD Video.
110  * A block is formed by the number of groups that are
111  * needed to complete a set of samples for each channel. */
112  if (avctx->bits_per_coded_sample == 16) {
113  s->samples_per_block = 1;
114  s->block_size = avctx->channels * 2;
115  } else {
116  switch (avctx->channels) {
117  case 1:
118  case 2:
119  case 4:
120  /* one group has all the samples needed */
121  s->block_size = 4 * avctx->bits_per_coded_sample / 8;
122  s->samples_per_block = 4 / avctx->channels;
123  s->groups_per_block = 1;
124  break;
125  case 8:
126  /* two groups have all the samples needed */
127  s->block_size = 8 * avctx->bits_per_coded_sample / 8;
128  s->samples_per_block = 1;
129  s->groups_per_block = 2;
130  break;
131  default:
132  /* need avctx->channels groups */
133  s->block_size = 4 * avctx->channels *
134  avctx->bits_per_coded_sample / 8;
135  s->samples_per_block = 4;
136  s->groups_per_block = avctx->channels;
137  break;
138  }
139  }
140 
141  if (avctx->debug & FF_DEBUG_PICT_INFO)
142  ff_dlog(avctx,
143  "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %"PRId64" bit/s\n",
144  avctx->channels, avctx->bits_per_coded_sample,
145  avctx->sample_rate, avctx->bit_rate);
146 
147  s->last_header = header_int;
148 
149  return 0;
150 }
151 
152 static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
153  void *dst, int blocks)
154 {
155  PCMDVDContext *s = avctx->priv_data;
156  int16_t *dst16 = dst;
157  int32_t *dst32 = dst;
158  GetByteContext gb;
159  int i;
160  uint8_t t;
161 
162  bytestream2_init(&gb, src, blocks * s->block_size);
163  switch (avctx->bits_per_coded_sample) {
164  case 16: {
165 #if HAVE_BIGENDIAN
166  bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
167  dst16 += blocks * s->block_size / 2;
168 #else
169  int samples = blocks * avctx->channels;
170  do {
171  *dst16++ = bytestream2_get_be16u(&gb);
172  } while (--samples);
173 #endif
174  return dst16;
175  }
176  case 20:
177  if (avctx->channels == 1) {
178  do {
179  for (i = 2; i; i--) {
180  dst32[0] = bytestream2_get_be16u(&gb) << 16;
181  dst32[1] = bytestream2_get_be16u(&gb) << 16;
182  t = bytestream2_get_byteu(&gb);
183  *dst32++ += (t & 0xf0) << 8;
184  *dst32++ += (t & 0x0f) << 12;
185  }
186  } while (--blocks);
187  } else {
188  do {
189  for (i = s->groups_per_block; i; i--) {
190  dst32[0] = bytestream2_get_be16u(&gb) << 16;
191  dst32[1] = bytestream2_get_be16u(&gb) << 16;
192  dst32[2] = bytestream2_get_be16u(&gb) << 16;
193  dst32[3] = bytestream2_get_be16u(&gb) << 16;
194  t = bytestream2_get_byteu(&gb);
195  *dst32++ += (t & 0xf0) << 8;
196  *dst32++ += (t & 0x0f) << 12;
197  t = bytestream2_get_byteu(&gb);
198  *dst32++ += (t & 0xf0) << 8;
199  *dst32++ += (t & 0x0f) << 12;
200  }
201  } while (--blocks);
202  }
203  return dst32;
204  case 24:
205  if (avctx->channels == 1) {
206  do {
207  for (i = 2; i; i--) {
208  dst32[0] = bytestream2_get_be16u(&gb) << 16;
209  dst32[1] = bytestream2_get_be16u(&gb) << 16;
210  *dst32++ += bytestream2_get_byteu(&gb) << 8;
211  *dst32++ += bytestream2_get_byteu(&gb) << 8;
212  }
213  } while (--blocks);
214  } else {
215  do {
216  for (i = s->groups_per_block; i; i--) {
217  dst32[0] = bytestream2_get_be16u(&gb) << 16;
218  dst32[1] = bytestream2_get_be16u(&gb) << 16;
219  dst32[2] = bytestream2_get_be16u(&gb) << 16;
220  dst32[3] = bytestream2_get_be16u(&gb) << 16;
221  *dst32++ += bytestream2_get_byteu(&gb) << 8;
222  *dst32++ += bytestream2_get_byteu(&gb) << 8;
223  *dst32++ += bytestream2_get_byteu(&gb) << 8;
224  *dst32++ += bytestream2_get_byteu(&gb) << 8;
225  }
226  } while (--blocks);
227  }
228  return dst32;
229  default:
230  return NULL;
231  }
232 }
233 
234 static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
235  int *got_frame_ptr, AVPacket *avpkt)
236 {
237  AVFrame *frame = data;
238  const uint8_t *src = avpkt->data;
239  int buf_size = avpkt->size;
240  PCMDVDContext *s = avctx->priv_data;
241  int retval;
242  int blocks;
243  void *dst;
244 
245  if (buf_size < 3) {
246  av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
247  return AVERROR_INVALIDDATA;
248  }
249 
250  if ((retval = pcm_dvd_parse_header(avctx, src)))
251  return retval;
252  if (s->last_block_size && s->last_block_size != s->block_size) {
253  av_log(avctx, AV_LOG_WARNING, "block_size has changed %d != %d\n", s->last_block_size, s->block_size);
254  s->extra_sample_count = 0;
255  }
256  s->last_block_size = s->block_size;
257  src += 3;
258  buf_size -= 3;
259 
260  blocks = (buf_size + s->extra_sample_count) / s->block_size;
261 
262  /* get output buffer */
263  frame->nb_samples = blocks * s->samples_per_block;
264  if ((retval = ff_get_buffer(avctx, frame, 0)) < 0)
265  return retval;
266  dst = frame->data[0];
267 
268  /* consume leftover samples from last packet */
269  if (s->extra_sample_count) {
270  int missing_samples = s->block_size - s->extra_sample_count;
271  if (buf_size >= missing_samples) {
272  memcpy(s->extra_samples + s->extra_sample_count, src,
273  missing_samples);
274  dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
275  src += missing_samples;
276  buf_size -= missing_samples;
277  s->extra_sample_count = 0;
278  blocks--;
279  } else {
280  /* new packet still doesn't have enough samples */
281  memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
282  s->extra_sample_count += buf_size;
283  return avpkt->size;
284  }
285  }
286 
287  /* decode remaining complete samples */
288  if (blocks) {
289  pcm_dvd_decode_samples(avctx, src, dst, blocks);
290  buf_size -= blocks * s->block_size;
291  }
292 
293  /* store leftover samples */
294  if (buf_size) {
295  src += blocks * s->block_size;
296  memcpy(s->extra_samples, src, buf_size);
297  s->extra_sample_count = buf_size;
298  }
299 
300  *got_frame_ptr = 1;
301 
302  return avpkt->size;
303 }
304 
306  .name = "pcm_dvd",
307  .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
308  .type = AVMEDIA_TYPE_AUDIO,
309  .id = AV_CODEC_ID_PCM_DVD,
310  .priv_data_size = sizeof(PCMDVDContext),
313  .close = pcm_dvd_decode_uninit,
314  .capabilities = AV_CODEC_CAP_DR1,
315  .sample_fmts = (const enum AVSampleFormat[]) {
317  }
318 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2225
GetByteContext
Definition: bytestream.h:33
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:686
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
ff_pcm_dvd_decoder
AVCodec ff_pcm_dvd_decoder
Definition: pcm-dvd.c:305
data
const char data[16]
Definition: mxf.c:91
pcm_dvd_decode_uninit
static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
Definition: pcm-dvd.c:54
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2651
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
PCMDVDContext::block_size
int block_size
Definition: pcm-dvd.c:33
pcm_dvd_decode_frame
static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: pcm-dvd.c:234
src
#define src
Definition: vp8dsp.c:254
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
PCMDVDContext::groups_per_block
int groups_per_block
Definition: pcm-dvd.c:36
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:257
PCMDVDContext
Definition: pcm-dvd.c:31
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
PCMDVDContext::last_header
uint32_t last_header
Definition: pcm-dvd.c:32
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2796
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AV_CODEC_ID_PCM_DVD
@ AV_CODEC_ID_PCM_DVD
Definition: avcodec.h:482
int32_t
int32_t
Definition: audio_convert.c:194
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
PCMDVDContext::extra_samples
uint8_t * extra_samples
Definition: pcm-dvd.c:37
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1615
pcm_dvd_decode_samples
static void * pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src, void *dst, int blocks)
Definition: pcm-dvd.c:152
PCMDVDContext::last_block_size
int last_block_size
Definition: pcm-dvd.c:34
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
PCMDVDContext::extra_sample_count
int extra_sample_count
Definition: pcm-dvd.c:38
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:29
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1965
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
header
static const uint8_t header[24]
Definition: sdr2.c:67
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2789
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint8_t
uint8_t
Definition: audio_convert.c:194
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: avcodec.h:3488
avcodec.h
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:1565
pcm_dvd_parse_header
static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
Definition: pcm-dvd.c:63
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:2650
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
bytestream.h
pcm_dvd_decode_init
static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
Definition: pcm-dvd.c:41
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
PCMDVDContext::samples_per_block
int samples_per_block
Definition: pcm-dvd.c:35
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:59
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62