• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/pcm-mpeg.c

Go to the documentation of this file.
00001 /*
00002  * LPCM codecs for PCM formats found in MPEG streams
00003  * Copyright (c) 2009 Christian Schmidt
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "libavutil/audioconvert.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 
00031 /*
00032  * Channel Mapping according to
00033  * Blu-ray Disc Read-Only Format Version 1
00034  * Part 3: Audio Visual Basic Specifications
00035  * mono     M1    X
00036  * stereo   L     R
00037  * 3/0      L     R    C    X
00038  * 2/1      L     R    S    X
00039  * 3/1      L     R    C    S
00040  * 2/2      L     R    LS   RS
00041  * 3/2      L     R    C    LS    RS    X
00042  * 3/2+lfe  L     R    C    LS    RS    lfe
00043  * 3/4      L     R    C    LS    Rls   Rrs  RS   X
00044  * 3/4+lfe  L     R    C    LS    Rls   Rrs  RS   lfe
00045  */
00046 
00052 static int pcm_bluray_parse_header(AVCodecContext *avctx,
00053                                    const uint8_t *header)
00054 {
00055     static const uint8_t bits_per_samples[4] = { 0, 16, 20, 24 };
00056     static const uint32_t channel_layouts[16] = {
00057         0, AV_CH_LAYOUT_MONO, 0, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_SURROUND,
00058         AV_CH_LAYOUT_2_1, AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_2_2, AV_CH_LAYOUT_5POINT0,
00059         AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_7POINT0, AV_CH_LAYOUT_7POINT1, 0, 0, 0, 0
00060     };
00061     static const uint8_t channels[16] = {
00062         0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8, 0, 0, 0, 0
00063     };
00064     uint8_t channel_layout = header[2] >> 4;
00065 
00066     if (avctx->debug & FF_DEBUG_PICT_INFO)
00067         av_dlog(avctx, "pcm_bluray_parse_header: header = %02x%02x%02x%02x\n",
00068                 header[0], header[1], header[2], header[3]);
00069 
00070     /* get the sample depth and derive the sample format from it */
00071     avctx->bits_per_coded_sample = bits_per_samples[header[3] >> 6];
00072     if (!avctx->bits_per_coded_sample) {
00073         av_log(avctx, AV_LOG_ERROR, "unsupported sample depth (0)\n");
00074         return -1;
00075     }
00076     avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16 :
00077                                                              AV_SAMPLE_FMT_S32;
00078     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
00079         avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
00080 
00081     /* get the sample rate. Not all values are known or exist. */
00082     switch (header[2] & 0x0f) {
00083     case 1:
00084         avctx->sample_rate = 48000;
00085         break;
00086     case 4:
00087         avctx->sample_rate = 96000;
00088         break;
00089     case 5:
00090         avctx->sample_rate = 192000;
00091         break;
00092     default:
00093         avctx->sample_rate = 0;
00094         av_log(avctx, AV_LOG_ERROR, "unsupported sample rate (%d)\n",
00095                header[2] & 0x0f);
00096         return -1;
00097     }
00098 
00099     /*
00100      * get the channel number (and mapping). Not all values are known or exist.
00101      * It must be noted that the number of channels in the MPEG stream can
00102      * differ from the actual meaningful number, e.g. mono audio still has two
00103      * channels, one being empty.
00104      */
00105     avctx->channel_layout  = channel_layouts[channel_layout];
00106     avctx->channels        =        channels[channel_layout];
00107     if (!avctx->channels) {
00108         av_log(avctx, AV_LOG_ERROR, "unsupported channel configuration (%d)\n",
00109                channel_layout);
00110         return -1;
00111     }
00112 
00113     avctx->bit_rate = avctx->channels * avctx->sample_rate *
00114                       avctx->bits_per_coded_sample;
00115 
00116     if (avctx->debug & FF_DEBUG_PICT_INFO)
00117         av_dlog(avctx,
00118                 "pcm_bluray_parse_header: %d channels, %d bits per sample, %d kHz, %d kbit\n",
00119                 avctx->channels, avctx->bits_per_coded_sample,
00120                 avctx->sample_rate, avctx->bit_rate);
00121     return 0;
00122 }
00123 
00124 typedef struct PCMBRDecode {
00125     AVFrame frame;
00126 } PCMBRDecode;
00127 
00128 static av_cold int pcm_bluray_decode_init(AVCodecContext * avctx)
00129 {
00130     PCMBRDecode *s = avctx->priv_data;
00131 
00132     avcodec_get_frame_defaults(&s->frame);
00133     avctx->coded_frame = &s->frame;
00134 
00135     return 0;
00136 }
00137 
00138 static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
00139                                    int *got_frame_ptr, AVPacket *avpkt)
00140 {
00141     const uint8_t *src = avpkt->data;
00142     int buf_size = avpkt->size;
00143     PCMBRDecode *s = avctx->priv_data;
00144     int num_source_channels, channel, retval;
00145     int sample_size, samples;
00146     int16_t *dst16;
00147     int32_t *dst32;
00148 
00149     if (buf_size < 4) {
00150         av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
00151         return -1;
00152     }
00153 
00154     if (pcm_bluray_parse_header(avctx, src))
00155         return -1;
00156     src += 4;
00157     buf_size -= 4;
00158 
00159     /* There's always an even number of channels in the source */
00160     num_source_channels = FFALIGN(avctx->channels, 2);
00161     sample_size = (num_source_channels * (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
00162     samples = buf_size / sample_size;
00163 
00164     /* get output buffer */
00165     s->frame.nb_samples = samples;
00166     if ((retval = avctx->get_buffer(avctx, &s->frame)) < 0) {
00167         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00168         return retval;
00169     }
00170     dst16 = (int16_t *)s->frame.data[0];
00171     dst32 = (int32_t *)s->frame.data[0];
00172 
00173     if (samples) {
00174         switch (avctx->channel_layout) {
00175             /* cases with same number of source and coded channels */
00176         case AV_CH_LAYOUT_STEREO:
00177         case AV_CH_LAYOUT_4POINT0:
00178         case AV_CH_LAYOUT_2_2:
00179             samples *= num_source_channels;
00180             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00181 #if HAVE_BIGENDIAN
00182                 memcpy(dst16, src, buf_size);
00183 #else
00184                 do {
00185                     *dst16++ = bytestream_get_be16(&src);
00186                 } while (--samples);
00187 #endif
00188             } else {
00189                 do {
00190                     *dst32++ = bytestream_get_be24(&src) << 8;
00191                 } while (--samples);
00192             }
00193             break;
00194         /* cases where number of source channels = coded channels + 1 */
00195         case AV_CH_LAYOUT_MONO:
00196         case AV_CH_LAYOUT_SURROUND:
00197         case AV_CH_LAYOUT_2_1:
00198         case AV_CH_LAYOUT_5POINT0:
00199             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00200                 do {
00201 #if HAVE_BIGENDIAN
00202                     memcpy(dst16, src, avctx->channels * 2);
00203                     dst16 += avctx->channels;
00204                     src += sample_size;
00205 #else
00206                     channel = avctx->channels;
00207                     do {
00208                         *dst16++ = bytestream_get_be16(&src);
00209                     } while (--channel);
00210                     src += 2;
00211 #endif
00212                 } while (--samples);
00213             } else {
00214                 do {
00215                     channel = avctx->channels;
00216                     do {
00217                         *dst32++ = bytestream_get_be24(&src) << 8;
00218                     } while (--channel);
00219                     src += 3;
00220                 } while (--samples);
00221             }
00222             break;
00223             /* remapping: L, R, C, LBack, RBack, LF */
00224         case AV_CH_LAYOUT_5POINT1:
00225             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00226                 do {
00227                     dst16[0] = bytestream_get_be16(&src);
00228                     dst16[1] = bytestream_get_be16(&src);
00229                     dst16[2] = bytestream_get_be16(&src);
00230                     dst16[4] = bytestream_get_be16(&src);
00231                     dst16[5] = bytestream_get_be16(&src);
00232                     dst16[3] = bytestream_get_be16(&src);
00233                     dst16 += 6;
00234                 } while (--samples);
00235             } else {
00236                 do {
00237                     dst32[0] = bytestream_get_be24(&src) << 8;
00238                     dst32[1] = bytestream_get_be24(&src) << 8;
00239                     dst32[2] = bytestream_get_be24(&src) << 8;
00240                     dst32[4] = bytestream_get_be24(&src) << 8;
00241                     dst32[5] = bytestream_get_be24(&src) << 8;
00242                     dst32[3] = bytestream_get_be24(&src) << 8;
00243                     dst32 += 6;
00244                 } while (--samples);
00245             }
00246             break;
00247             /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
00248         case AV_CH_LAYOUT_7POINT0:
00249             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00250                 do {
00251                     dst16[0] = bytestream_get_be16(&src);
00252                     dst16[1] = bytestream_get_be16(&src);
00253                     dst16[2] = bytestream_get_be16(&src);
00254                     dst16[5] = bytestream_get_be16(&src);
00255                     dst16[3] = bytestream_get_be16(&src);
00256                     dst16[4] = bytestream_get_be16(&src);
00257                     dst16[6] = bytestream_get_be16(&src);
00258                     dst16 += 7;
00259                     src += 2;
00260                 } while (--samples);
00261             } else {
00262                 do {
00263                     dst32[0] = bytestream_get_be24(&src) << 8;
00264                     dst32[1] = bytestream_get_be24(&src) << 8;
00265                     dst32[2] = bytestream_get_be24(&src) << 8;
00266                     dst32[5] = bytestream_get_be24(&src) << 8;
00267                     dst32[3] = bytestream_get_be24(&src) << 8;
00268                     dst32[4] = bytestream_get_be24(&src) << 8;
00269                     dst32[6] = bytestream_get_be24(&src) << 8;
00270                     dst32 += 7;
00271                     src += 3;
00272                 } while (--samples);
00273             }
00274             break;
00275             /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
00276         case AV_CH_LAYOUT_7POINT1:
00277             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00278                 do {
00279                     dst16[0] = bytestream_get_be16(&src);
00280                     dst16[1] = bytestream_get_be16(&src);
00281                     dst16[2] = bytestream_get_be16(&src);
00282                     dst16[6] = bytestream_get_be16(&src);
00283                     dst16[4] = bytestream_get_be16(&src);
00284                     dst16[5] = bytestream_get_be16(&src);
00285                     dst16[7] = bytestream_get_be16(&src);
00286                     dst16[3] = bytestream_get_be16(&src);
00287                     dst16 += 8;
00288                 } while (--samples);
00289             } else {
00290                 do {
00291                     dst32[0] = bytestream_get_be24(&src) << 8;
00292                     dst32[1] = bytestream_get_be24(&src) << 8;
00293                     dst32[2] = bytestream_get_be24(&src) << 8;
00294                     dst32[6] = bytestream_get_be24(&src) << 8;
00295                     dst32[4] = bytestream_get_be24(&src) << 8;
00296                     dst32[5] = bytestream_get_be24(&src) << 8;
00297                     dst32[7] = bytestream_get_be24(&src) << 8;
00298                     dst32[3] = bytestream_get_be24(&src) << 8;
00299                     dst32 += 8;
00300                 } while (--samples);
00301             }
00302             break;
00303         }
00304     }
00305 
00306     *got_frame_ptr   = 1;
00307     *(AVFrame *)data = s->frame;
00308 
00309     retval = src - avpkt->data;
00310     if (avctx->debug & FF_DEBUG_BITSTREAM)
00311         av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
00312                 retval, buf_size);
00313     return retval;
00314 }
00315 
00316 AVCodec ff_pcm_bluray_decoder = {
00317     .name           = "pcm_bluray",
00318     .type           = AVMEDIA_TYPE_AUDIO,
00319     .id             = CODEC_ID_PCM_BLURAY,
00320     .priv_data_size = sizeof(PCMBRDecode),
00321     .init           = pcm_bluray_decode_init,
00322     .decode         = pcm_bluray_decode_frame,
00323     .capabilities   = CODEC_CAP_DR1,
00324     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
00325                                          AV_SAMPLE_FMT_NONE},
00326     .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
00327 };
Generated on Fri Feb 1 2013 14:34:41 for FFmpeg by doxygen 1.7.1