Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 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/24bit 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 |
||
39 | } PCMDVDContext; |
||
40 | |||
41 | static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx) |
||
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 | |||
54 | static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx) |
||
55 | { |
||
56 | PCMDVDContext *s = avctx->priv_data; |
||
57 | |||
58 | av_freep(&s->extra_samples); |
||
59 | |||
60 | return 0; |
||
61 | } |
||
62 | |||
63 | static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header) |
||
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 | |||
74 | if (avctx->debug & FF_DEBUG_PICT_INFO) |
||
75 | av_dlog(avctx, "pcm_dvd_parse_header: header = %02x%02x%02x\n", |
||
76 | header[0], header[1], header[2]); |
||
77 | /* |
||
78 | * header[0] emphasis (1), muse(1), reserved(1), frame number(5) |
||
79 | * header[1] quant (2), freq(2), reserved(1), channels(3) |
||
80 | * header[2] dynamic range control (0x80 = off) |
||
81 | */ |
||
82 | |||
83 | /* Discard potentially existing leftover samples from old channel layout */ |
||
84 | s->extra_sample_count = 0; |
||
85 | |||
86 | /* get the sample depth and derive the sample format from it */ |
||
87 | avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4; |
||
88 | if (avctx->bits_per_coded_sample == 28) { |
||
89 | av_log(avctx, AV_LOG_ERROR, |
||
90 | "PCM DVD unsupported sample depth %i\n", |
||
91 | avctx->bits_per_coded_sample); |
||
92 | return AVERROR_INVALIDDATA; |
||
93 | } |
||
94 | avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16 |
||
95 | : AV_SAMPLE_FMT_S32; |
||
96 | avctx->bits_per_raw_sample = avctx->bits_per_coded_sample; |
||
97 | |||
98 | /* get the sample rate */ |
||
99 | avctx->sample_rate = frequencies[header[1] >> 4 & 3]; |
||
100 | |||
101 | /* get the number of channels */ |
||
102 | avctx->channels = 1 + (header[1] & 7); |
||
103 | /* calculate the bitrate */ |
||
104 | avctx->bit_rate = avctx->channels * |
||
105 | avctx->sample_rate * |
||
106 | avctx->bits_per_coded_sample; |
||
107 | |||
108 | /* 4 samples form a group in 20/24bit PCM on DVD Video. |
||
109 | * A block is formed by the number of groups that are |
||
110 | * needed to complete a set of samples for each channel. */ |
||
111 | if (avctx->bits_per_coded_sample == 16) { |
||
112 | s->samples_per_block = 1; |
||
113 | s->block_size = avctx->channels * 2; |
||
114 | } else { |
||
115 | switch (avctx->channels) { |
||
116 | case 1: |
||
117 | case 2: |
||
118 | case 4: |
||
119 | /* one group has all the samples needed */ |
||
120 | s->block_size = 4 * avctx->bits_per_coded_sample / 8; |
||
121 | s->samples_per_block = 4 / avctx->channels; |
||
122 | s->groups_per_block = 1; |
||
123 | break; |
||
124 | case 8: |
||
125 | /* two groups have all the samples needed */ |
||
126 | s->block_size = 8 * avctx->bits_per_coded_sample / 8; |
||
127 | s->samples_per_block = 1; |
||
128 | s->groups_per_block = 2; |
||
129 | break; |
||
130 | default: |
||
131 | /* need avctx->channels groups */ |
||
132 | s->block_size = 4 * avctx->channels * |
||
133 | avctx->bits_per_coded_sample / 8; |
||
134 | s->samples_per_block = 4; |
||
135 | s->groups_per_block = avctx->channels; |
||
136 | break; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (avctx->debug & FF_DEBUG_PICT_INFO) |
||
141 | av_dlog(avctx, |
||
142 | "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n", |
||
143 | avctx->channels, avctx->bits_per_coded_sample, |
||
144 | avctx->sample_rate, avctx->bit_rate); |
||
145 | |||
146 | s->last_header = header_int; |
||
147 | |||
148 | return 0; |
||
149 | } |
||
150 | |||
151 | static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src, |
||
152 | void *dst, int blocks) |
||
153 | { |
||
154 | PCMDVDContext *s = avctx->priv_data; |
||
155 | int16_t *dst16 = dst; |
||
156 | int32_t *dst32 = dst; |
||
157 | GetByteContext gb; |
||
158 | int i; |
||
159 | uint8_t t; |
||
160 | int samples; |
||
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 | samples = blocks * avctx->channels; |
||
170 | do { |
||
171 | *dst16++ = bytestream2_get_be16u(&gb); |
||
172 | } while (--samples); |
||
173 | #endif |
||
174 | return dst16; |
||
175 | case 20: |
||
176 | if (avctx->channels == 1) { |
||
177 | do { |
||
178 | for (i = 2; i; i--) { |
||
179 | dst32[0] = bytestream2_get_be16u(&gb) << 16; |
||
180 | dst32[1] = bytestream2_get_be16u(&gb) << 16; |
||
181 | t = bytestream2_get_byteu(&gb); |
||
182 | *dst32++ += (t & 0xf0) << 8; |
||
183 | *dst32++ += (t & 0x0f) << 12; |
||
184 | } |
||
185 | } while (--blocks); |
||
186 | } else { |
||
187 | do { |
||
188 | for (i = s->groups_per_block; i; i--) { |
||
189 | dst32[0] = bytestream2_get_be16u(&gb) << 16; |
||
190 | dst32[1] = bytestream2_get_be16u(&gb) << 16; |
||
191 | dst32[2] = bytestream2_get_be16u(&gb) << 16; |
||
192 | dst32[3] = bytestream2_get_be16u(&gb) << 16; |
||
193 | t = bytestream2_get_byteu(&gb); |
||
194 | *dst32++ += (t & 0xf0) << 8; |
||
195 | *dst32++ += (t & 0x0f) << 12; |
||
196 | t = bytestream2_get_byteu(&gb); |
||
197 | *dst32++ += (t & 0xf0) << 8; |
||
198 | *dst32++ += (t & 0x0f) << 12; |
||
199 | } |
||
200 | } while (--blocks); |
||
201 | } |
||
202 | return dst32; |
||
203 | case 24: |
||
204 | if (avctx->channels == 1) { |
||
205 | do { |
||
206 | for (i = 2; i; i--) { |
||
207 | dst32[0] = bytestream2_get_be16u(&gb) << 16; |
||
208 | dst32[1] = bytestream2_get_be16u(&gb) << 16; |
||
209 | *dst32++ += bytestream2_get_byteu(&gb) << 8; |
||
210 | *dst32++ += bytestream2_get_byteu(&gb) << 8; |
||
211 | } |
||
212 | } while (--blocks); |
||
213 | } else { |
||
214 | do { |
||
215 | for (i = s->groups_per_block; i; i--) { |
||
216 | dst32[0] = bytestream2_get_be16u(&gb) << 16; |
||
217 | dst32[1] = bytestream2_get_be16u(&gb) << 16; |
||
218 | dst32[2] = bytestream2_get_be16u(&gb) << 16; |
||
219 | dst32[3] = bytestream2_get_be16u(&gb) << 16; |
||
220 | *dst32++ += bytestream2_get_byteu(&gb) << 8; |
||
221 | *dst32++ += bytestream2_get_byteu(&gb) << 8; |
||
222 | *dst32++ += bytestream2_get_byteu(&gb) << 8; |
||
223 | *dst32++ += bytestream2_get_byteu(&gb) << 8; |
||
224 | } |
||
225 | } while (--blocks); |
||
226 | } |
||
227 | return dst32; |
||
228 | default: |
||
229 | return NULL; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data, |
||
234 | int *got_frame_ptr, AVPacket *avpkt) |
||
235 | { |
||
236 | AVFrame *frame = data; |
||
237 | const uint8_t *src = avpkt->data; |
||
238 | int buf_size = avpkt->size; |
||
239 | PCMDVDContext *s = avctx->priv_data; |
||
240 | int retval; |
||
241 | int blocks; |
||
242 | void *dst; |
||
243 | |||
244 | if (buf_size < 3) { |
||
245 | av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n"); |
||
246 | return AVERROR_INVALIDDATA; |
||
247 | } |
||
248 | |||
249 | if ((retval = pcm_dvd_parse_header(avctx, src))) |
||
250 | return retval; |
||
251 | if (s->last_block_size != s->block_size) { |
||
252 | av_log(avctx, AV_LOG_WARNING, "block_size has changed\n"); |
||
253 | s->extra_sample_count = 0; |
||
254 | } |
||
255 | s->last_block_size = s->block_size; |
||
256 | src += 3; |
||
257 | buf_size -= 3; |
||
258 | |||
259 | blocks = (buf_size + s->extra_sample_count) / s->block_size; |
||
260 | |||
261 | /* get output buffer */ |
||
262 | frame->nb_samples = blocks * s->samples_per_block; |
||
263 | if ((retval = ff_get_buffer(avctx, frame, 0)) < 0) |
||
264 | return retval; |
||
265 | dst = frame->data[0]; |
||
266 | |||
267 | /* consume leftover samples from last packet */ |
||
268 | if (s->extra_sample_count) { |
||
269 | int missing_samples = s->block_size - s->extra_sample_count; |
||
270 | if (buf_size >= missing_samples) { |
||
271 | memcpy(s->extra_samples + s->extra_sample_count, src, |
||
272 | missing_samples); |
||
273 | dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1); |
||
274 | src += missing_samples; |
||
275 | buf_size -= missing_samples; |
||
276 | s->extra_sample_count = 0; |
||
277 | blocks--; |
||
278 | } else { |
||
279 | /* new packet still doesn't have enough samples */ |
||
280 | memcpy(s->extra_samples + s->extra_sample_count, src, buf_size); |
||
281 | s->extra_sample_count += buf_size; |
||
282 | return avpkt->size; |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /* decode remaining complete samples */ |
||
287 | if (blocks) { |
||
288 | pcm_dvd_decode_samples(avctx, src, dst, blocks); |
||
289 | buf_size -= blocks * s->block_size; |
||
290 | } |
||
291 | |||
292 | /* store leftover samples */ |
||
293 | if (buf_size) { |
||
294 | src += blocks * s->block_size; |
||
295 | memcpy(s->extra_samples, src, buf_size); |
||
296 | s->extra_sample_count = buf_size; |
||
297 | } |
||
298 | |||
299 | *got_frame_ptr = 1; |
||
300 | |||
301 | return avpkt->size; |
||
302 | } |
||
303 | |||
304 | AVCodec ff_pcm_dvd_decoder = { |
||
305 | .name = "pcm_dvd", |
||
306 | .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"), |
||
307 | .type = AVMEDIA_TYPE_AUDIO, |
||
308 | .id = AV_CODEC_ID_PCM_DVD, |
||
309 | .priv_data_size = sizeof(PCMDVDContext), |
||
310 | .init = pcm_dvd_decode_init, |
||
311 | .decode = pcm_dvd_decode_frame, |
||
312 | .close = pcm_dvd_decode_uninit, |
||
313 | .capabilities = CODEC_CAP_DR1, |
||
314 | .sample_fmts = (const enum AVSampleFormat[]) { |
||
315 | AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE |
||
316 | } |
||
317 | };>>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><> |