pcm-mpeg.c
Go to the documentation of this file.
1 /*
2  * LPCM codecs for PCM formats found in MPEG streams
3  * Copyright (c) 2009 Christian Schmidt
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "libavutil/audioconvert.h"
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "bytestream.h"
31 
32 /*
33  * Channel Mapping according to
34  * Blu-ray Disc Read-Only Format Version 1
35  * Part 3: Audio Visual Basic Specifications
36  * mono M1 X
37  * stereo L R
38  * 3/0 L R C X
39  * 2/1 L R S X
40  * 3/1 L R C S
41  * 2/2 L R LS RS
42  * 3/2 L R C LS RS X
43  * 3/2+lfe L R C LS RS lfe
44  * 3/4 L R C LS Rls Rrs RS X
45  * 3/4+lfe L R C LS Rls Rrs RS lfe
46  */
47 
54  const uint8_t *header)
55 {
56  static const uint8_t bits_per_samples[4] = { 0, 16, 20, 24 };
57  static const uint32_t channel_layouts[16] = {
61  };
62  static const uint8_t channels[16] = {
63  0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8, 0, 0, 0, 0
64  };
65  uint8_t channel_layout = header[2] >> 4;
66 
67  if (avctx->debug & FF_DEBUG_PICT_INFO)
68  av_dlog(avctx, "pcm_bluray_parse_header: header = %02x%02x%02x%02x\n",
69  header[0], header[1], header[2], header[3]);
70 
71  /* get the sample depth and derive the sample format from it */
72  avctx->bits_per_coded_sample = bits_per_samples[header[3] >> 6];
73  if (!avctx->bits_per_coded_sample) {
74  av_log(avctx, AV_LOG_ERROR, "unsupported sample depth (0)\n");
75  return -1;
76  }
77  avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16 :
79 
80  /* get the sample rate. Not all values are known or exist. */
81  switch (header[2] & 0x0f) {
82  case 1:
83  avctx->sample_rate = 48000;
84  break;
85  case 4:
86  avctx->sample_rate = 96000;
87  break;
88  case 5:
89  avctx->sample_rate = 192000;
90  break;
91  default:
92  avctx->sample_rate = 0;
93  av_log(avctx, AV_LOG_ERROR, "unsupported sample rate (%d)\n",
94  header[2] & 0x0f);
95  return -1;
96  }
97 
98  /*
99  * get the channel number (and mapping). Not all values are known or exist.
100  * It must be noted that the number of channels in the MPEG stream can
101  * differ from the actual meaningful number, e.g. mono audio still has two
102  * channels, one being empty.
103  */
104  avctx->channel_layout = channel_layouts[channel_layout];
105  avctx->channels = channels[channel_layout];
106  if (!avctx->channels) {
107  av_log(avctx, AV_LOG_ERROR, "unsupported channel configuration (%d)\n",
108  channel_layout);
109  return -1;
110  }
111 
112  avctx->bit_rate = avctx->channels * avctx->sample_rate *
113  avctx->bits_per_coded_sample;
114 
115  if (avctx->debug & FF_DEBUG_PICT_INFO)
116  av_dlog(avctx,
117  "pcm_bluray_parse_header: %d channels, %d bits per sample, %d kHz, %d kbit\n",
118  avctx->channels, avctx->bits_per_coded_sample,
119  avctx->sample_rate, avctx->bit_rate);
120  return 0;
121 }
122 
123 typedef struct PCMBRDecode {
125 } PCMBRDecode;
126 
128 {
129  PCMBRDecode *s = avctx->priv_data;
130 
132  avctx->coded_frame = &s->frame;
133 
134  return 0;
135 }
136 
137 static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
138  int *got_frame_ptr, AVPacket *avpkt)
139 {
140  const uint8_t *src = avpkt->data;
141  int buf_size = avpkt->size;
142  PCMBRDecode *s = avctx->priv_data;
143  int num_source_channels, channel, retval;
144  int sample_size, samples;
145  int16_t *dst16;
146  int32_t *dst32;
147 
148  if (buf_size < 4) {
149  av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
150  return -1;
151  }
152 
153  if (pcm_bluray_parse_header(avctx, src))
154  return -1;
155  src += 4;
156  buf_size -= 4;
157 
158  /* There's always an even number of channels in the source */
159  num_source_channels = FFALIGN(avctx->channels, 2);
160  sample_size = (num_source_channels * (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
161  samples = buf_size / sample_size;
162 
163  /* get output buffer */
164  s->frame.nb_samples = samples;
165  if ((retval = ff_get_buffer(avctx, &s->frame)) < 0) {
166  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
167  return retval;
168  }
169  dst16 = (int16_t *)s->frame.data[0];
170  dst32 = (int32_t *)s->frame.data[0];
171 
172  if (samples) {
173  switch (avctx->channel_layout) {
174  /* cases with same number of source and coded channels */
175  case AV_CH_LAYOUT_STEREO:
177  case AV_CH_LAYOUT_2_2:
178  samples *= num_source_channels;
179  if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
180 #if HAVE_BIGENDIAN
181  memcpy(dst16, src, buf_size);
182 #else
183  do {
184  *dst16++ = bytestream_get_be16(&src);
185  } while (--samples);
186 #endif
187  } else {
188  do {
189  *dst32++ = bytestream_get_be24(&src) << 8;
190  } while (--samples);
191  }
192  break;
193  /* cases where number of source channels = coded channels + 1 */
194  case AV_CH_LAYOUT_MONO:
196  case AV_CH_LAYOUT_2_1:
198  if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
199  do {
200 #if HAVE_BIGENDIAN
201  memcpy(dst16, src, avctx->channels * 2);
202  dst16 += avctx->channels;
203  src += sample_size;
204 #else
205  channel = avctx->channels;
206  do {
207  *dst16++ = bytestream_get_be16(&src);
208  } while (--channel);
209  src += 2;
210 #endif
211  } while (--samples);
212  } else {
213  do {
214  channel = avctx->channels;
215  do {
216  *dst32++ = bytestream_get_be24(&src) << 8;
217  } while (--channel);
218  src += 3;
219  } while (--samples);
220  }
221  break;
222  /* remapping: L, R, C, LBack, RBack, LF */
224  if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
225  do {
226  dst16[0] = bytestream_get_be16(&src);
227  dst16[1] = bytestream_get_be16(&src);
228  dst16[2] = bytestream_get_be16(&src);
229  dst16[4] = bytestream_get_be16(&src);
230  dst16[5] = bytestream_get_be16(&src);
231  dst16[3] = bytestream_get_be16(&src);
232  dst16 += 6;
233  } while (--samples);
234  } else {
235  do {
236  dst32[0] = bytestream_get_be24(&src) << 8;
237  dst32[1] = bytestream_get_be24(&src) << 8;
238  dst32[2] = bytestream_get_be24(&src) << 8;
239  dst32[4] = bytestream_get_be24(&src) << 8;
240  dst32[5] = bytestream_get_be24(&src) << 8;
241  dst32[3] = bytestream_get_be24(&src) << 8;
242  dst32 += 6;
243  } while (--samples);
244  }
245  break;
246  /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
248  if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
249  do {
250  dst16[0] = bytestream_get_be16(&src);
251  dst16[1] = bytestream_get_be16(&src);
252  dst16[2] = bytestream_get_be16(&src);
253  dst16[5] = bytestream_get_be16(&src);
254  dst16[3] = bytestream_get_be16(&src);
255  dst16[4] = bytestream_get_be16(&src);
256  dst16[6] = bytestream_get_be16(&src);
257  dst16 += 7;
258  src += 2;
259  } while (--samples);
260  } else {
261  do {
262  dst32[0] = bytestream_get_be24(&src) << 8;
263  dst32[1] = bytestream_get_be24(&src) << 8;
264  dst32[2] = bytestream_get_be24(&src) << 8;
265  dst32[5] = bytestream_get_be24(&src) << 8;
266  dst32[3] = bytestream_get_be24(&src) << 8;
267  dst32[4] = bytestream_get_be24(&src) << 8;
268  dst32[6] = bytestream_get_be24(&src) << 8;
269  dst32 += 7;
270  src += 3;
271  } while (--samples);
272  }
273  break;
274  /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
276  if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
277  do {
278  dst16[0] = bytestream_get_be16(&src);
279  dst16[1] = bytestream_get_be16(&src);
280  dst16[2] = bytestream_get_be16(&src);
281  dst16[6] = bytestream_get_be16(&src);
282  dst16[4] = bytestream_get_be16(&src);
283  dst16[5] = bytestream_get_be16(&src);
284  dst16[7] = bytestream_get_be16(&src);
285  dst16[3] = bytestream_get_be16(&src);
286  dst16 += 8;
287  } while (--samples);
288  } else {
289  do {
290  dst32[0] = bytestream_get_be24(&src) << 8;
291  dst32[1] = bytestream_get_be24(&src) << 8;
292  dst32[2] = bytestream_get_be24(&src) << 8;
293  dst32[6] = bytestream_get_be24(&src) << 8;
294  dst32[4] = bytestream_get_be24(&src) << 8;
295  dst32[5] = bytestream_get_be24(&src) << 8;
296  dst32[7] = bytestream_get_be24(&src) << 8;
297  dst32[3] = bytestream_get_be24(&src) << 8;
298  dst32 += 8;
299  } while (--samples);
300  }
301  break;
302  }
303  }
304 
305  *got_frame_ptr = 1;
306  *(AVFrame *)data = s->frame;
307 
308  retval = src - avpkt->data;
309  if (avctx->debug & FF_DEBUG_BITSTREAM)
310  av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
311  retval, buf_size);
312  return retval;
313 }
314 
316  .name = "pcm_bluray",
317  .type = AVMEDIA_TYPE_AUDIO,
318  .id = CODEC_ID_PCM_BLURAY,
319  .priv_data_size = sizeof(PCMBRDecode),
322  .capabilities = CODEC_CAP_DR1,
323  .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
325  .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
326 };