• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 
00079     /* get the sample rate. Not all values are known or exist. */
00080     switch (header[2] & 0x0f) {
00081     case 1:
00082         avctx->sample_rate = 48000;
00083         break;
00084     case 4:
00085         avctx->sample_rate = 96000;
00086         break;
00087     case 5:
00088         avctx->sample_rate = 192000;
00089         break;
00090     default:
00091         avctx->sample_rate = 0;
00092         av_log(avctx, AV_LOG_ERROR, "unsupported sample rate (%d)\n",
00093                header[2] & 0x0f);
00094         return -1;
00095     }
00096 
00097     /*
00098      * get the channel number (and mapping). Not all values are known or exist.
00099      * It must be noted that the number of channels in the MPEG stream can
00100      * differ from the actual meaningful number, e.g. mono audio still has two
00101      * channels, one being empty.
00102      */
00103     avctx->channel_layout  = channel_layouts[channel_layout];
00104     avctx->channels        =        channels[channel_layout];
00105     if (!avctx->channels) {
00106         av_log(avctx, AV_LOG_ERROR, "unsupported channel configuration (%d)\n",
00107                channel_layout);
00108         return -1;
00109     }
00110 
00111     avctx->bit_rate = avctx->channels * avctx->sample_rate *
00112                       avctx->bits_per_coded_sample;
00113 
00114     if (avctx->debug & FF_DEBUG_PICT_INFO)
00115         av_dlog(avctx,
00116                 "pcm_bluray_parse_header: %d channels, %d bits per sample, %d kHz, %d kbit\n",
00117                 avctx->channels, avctx->bits_per_coded_sample,
00118                 avctx->sample_rate, avctx->bit_rate);
00119     return 0;
00120 }
00121 
00122 typedef struct PCMBRDecode {
00123     AVFrame frame;
00124 } PCMBRDecode;
00125 
00126 static av_cold int pcm_bluray_decode_init(AVCodecContext * avctx)
00127 {
00128     PCMBRDecode *s = avctx->priv_data;
00129 
00130     avcodec_get_frame_defaults(&s->frame);
00131     avctx->coded_frame = &s->frame;
00132 
00133     return 0;
00134 }
00135 
00136 static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
00137                                    int *got_frame_ptr, AVPacket *avpkt)
00138 {
00139     const uint8_t *src = avpkt->data;
00140     int buf_size = avpkt->size;
00141     PCMBRDecode *s = avctx->priv_data;
00142     int num_source_channels, channel, retval;
00143     int sample_size, samples;
00144     int16_t *dst16;
00145     int32_t *dst32;
00146 
00147     if (buf_size < 4) {
00148         av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
00149         return -1;
00150     }
00151 
00152     if (pcm_bluray_parse_header(avctx, src))
00153         return -1;
00154     src += 4;
00155     buf_size -= 4;
00156 
00157     /* There's always an even number of channels in the source */
00158     num_source_channels = FFALIGN(avctx->channels, 2);
00159     sample_size = (num_source_channels * avctx->bits_per_coded_sample) >> 3;
00160     samples = buf_size / sample_size;
00161 
00162     /* get output buffer */
00163     s->frame.nb_samples = samples;
00164     if ((retval = avctx->get_buffer(avctx, &s->frame)) < 0) {
00165         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00166         return retval;
00167     }
00168     dst16 = (int16_t *)s->frame.data[0];
00169     dst32 = (int32_t *)s->frame.data[0];
00170 
00171     if (samples) {
00172         switch (avctx->channel_layout) {
00173             /* cases with same number of source and coded channels */
00174         case AV_CH_LAYOUT_STEREO:
00175         case AV_CH_LAYOUT_4POINT0:
00176         case AV_CH_LAYOUT_2_2:
00177             samples *= num_source_channels;
00178             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00179 #if HAVE_BIGENDIAN
00180                 memcpy(dst16, src, buf_size);
00181 #else
00182                 do {
00183                     *dst16++ = bytestream_get_be16(&src);
00184                 } while (--samples);
00185 #endif
00186             } else {
00187                 do {
00188                     *dst32++ = bytestream_get_be24(&src) << 8;
00189                 } while (--samples);
00190             }
00191             break;
00192         /* cases where number of source channels = coded channels + 1 */
00193         case AV_CH_LAYOUT_MONO:
00194         case AV_CH_LAYOUT_SURROUND:
00195         case AV_CH_LAYOUT_2_1:
00196         case AV_CH_LAYOUT_5POINT0:
00197             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00198                 do {
00199 #if HAVE_BIGENDIAN
00200                     memcpy(dst16, src, avctx->channels * 2);
00201                     dst16 += avctx->channels;
00202                     src += sample_size;
00203 #else
00204                     channel = avctx->channels;
00205                     do {
00206                         *dst16++ = bytestream_get_be16(&src);
00207                     } while (--channel);
00208                     src += 2;
00209 #endif
00210                 } while (--samples);
00211             } else {
00212                 do {
00213                     channel = avctx->channels;
00214                     do {
00215                         *dst32++ = bytestream_get_be24(&src) << 8;
00216                     } while (--channel);
00217                     src += 3;
00218                 } while (--samples);
00219             }
00220             break;
00221             /* remapping: L, R, C, LBack, RBack, LF */
00222         case AV_CH_LAYOUT_5POINT1:
00223             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00224                 do {
00225                     dst16[0] = bytestream_get_be16(&src);
00226                     dst16[1] = bytestream_get_be16(&src);
00227                     dst16[2] = bytestream_get_be16(&src);
00228                     dst16[4] = bytestream_get_be16(&src);
00229                     dst16[5] = bytestream_get_be16(&src);
00230                     dst16[3] = bytestream_get_be16(&src);
00231                     dst16 += 6;
00232                 } while (--samples);
00233             } else {
00234                 do {
00235                     dst32[0] = bytestream_get_be24(&src) << 8;
00236                     dst32[1] = bytestream_get_be24(&src) << 8;
00237                     dst32[2] = bytestream_get_be24(&src) << 8;
00238                     dst32[4] = bytestream_get_be24(&src) << 8;
00239                     dst32[5] = bytestream_get_be24(&src) << 8;
00240                     dst32[3] = bytestream_get_be24(&src) << 8;
00241                     dst32 += 6;
00242                 } while (--samples);
00243             }
00244             break;
00245             /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
00246         case AV_CH_LAYOUT_7POINT0:
00247             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00248                 do {
00249                     dst16[0] = bytestream_get_be16(&src);
00250                     dst16[1] = bytestream_get_be16(&src);
00251                     dst16[2] = bytestream_get_be16(&src);
00252                     dst16[5] = bytestream_get_be16(&src);
00253                     dst16[3] = bytestream_get_be16(&src);
00254                     dst16[4] = bytestream_get_be16(&src);
00255                     dst16[6] = bytestream_get_be16(&src);
00256                     dst16 += 7;
00257                     src += 2;
00258                 } while (--samples);
00259             } else {
00260                 do {
00261                     dst32[0] = bytestream_get_be24(&src) << 8;
00262                     dst32[1] = bytestream_get_be24(&src) << 8;
00263                     dst32[2] = bytestream_get_be24(&src) << 8;
00264                     dst32[5] = bytestream_get_be24(&src) << 8;
00265                     dst32[3] = bytestream_get_be24(&src) << 8;
00266                     dst32[4] = bytestream_get_be24(&src) << 8;
00267                     dst32[6] = bytestream_get_be24(&src) << 8;
00268                     dst32 += 7;
00269                     src += 3;
00270                 } while (--samples);
00271             }
00272             break;
00273             /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
00274         case AV_CH_LAYOUT_7POINT1:
00275             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00276                 do {
00277                     dst16[0] = bytestream_get_be16(&src);
00278                     dst16[1] = bytestream_get_be16(&src);
00279                     dst16[2] = bytestream_get_be16(&src);
00280                     dst16[6] = bytestream_get_be16(&src);
00281                     dst16[4] = bytestream_get_be16(&src);
00282                     dst16[5] = bytestream_get_be16(&src);
00283                     dst16[7] = bytestream_get_be16(&src);
00284                     dst16[3] = bytestream_get_be16(&src);
00285                     dst16 += 8;
00286                 } while (--samples);
00287             } else {
00288                 do {
00289                     dst32[0] = bytestream_get_be24(&src) << 8;
00290                     dst32[1] = bytestream_get_be24(&src) << 8;
00291                     dst32[2] = bytestream_get_be24(&src) << 8;
00292                     dst32[6] = bytestream_get_be24(&src) << 8;
00293                     dst32[4] = bytestream_get_be24(&src) << 8;
00294                     dst32[5] = bytestream_get_be24(&src) << 8;
00295                     dst32[7] = bytestream_get_be24(&src) << 8;
00296                     dst32[3] = bytestream_get_be24(&src) << 8;
00297                     dst32 += 8;
00298                 } while (--samples);
00299             }
00300             break;
00301         }
00302     }
00303 
00304     *got_frame_ptr   = 1;
00305     *(AVFrame *)data = s->frame;
00306 
00307     retval = src - avpkt->data;
00308     if (avctx->debug & FF_DEBUG_BITSTREAM)
00309         av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
00310                 retval, buf_size);
00311     return retval;
00312 }
00313 
00314 AVCodec ff_pcm_bluray_decoder = {
00315     .name           = "pcm_bluray",
00316     .type           = AVMEDIA_TYPE_AUDIO,
00317     .id             = CODEC_ID_PCM_BLURAY,
00318     .priv_data_size = sizeof(PCMBRDecode),
00319     .init           = pcm_bluray_decode_init,
00320     .decode         = pcm_bluray_decode_frame,
00321     .capabilities   = CODEC_CAP_DR1,
00322     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
00323                                          AV_SAMPLE_FMT_NONE},
00324     .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
00325 };
Generated on Sun Apr 22 2012 21:54:03 for Libav by doxygen 1.7.1