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