libavcodec/pcm-mpeg.c
5dd3707b
 /*
  * LPCM codecs for PCM formats found in MPEG streams
  * Copyright (c) 2009 Christian Schmidt
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
5dd3707b
  * PCM codecs for encodings found in MPEG streams (DVD/Blu-ray)
  */
 
a903f8f0
 #include "libavutil/channel_layout.h"
5dd3707b
 #include "avcodec.h"
 #include "bytestream.h"
594d4d5d
 #include "internal.h"
5dd3707b
 
 /*
  * Channel Mapping according to
  * Blu-ray Disc Read-Only Format Version 1
  * Part 3: Audio Visual Basic Specifications
  * mono     M1    X
  * stereo   L     R
  * 3/0      L     R    C    X
  * 2/1      L     R    S    X
  * 3/1      L     R    C    S
  * 2/2      L     R    LS   RS
  * 3/2      L     R    C    LS    RS    X
  * 3/2+lfe  L     R    C    LS    RS    lfe
  * 3/4      L     R    C    LS    Rls   Rrs  RS   X
  * 3/4+lfe  L     R    C    LS    Rls   Rrs  RS   lfe
  */
 
 /**
  * Parse the header of a LPCM frame read from a MPEG-TS stream
  * @param avctx the codec context
  * @param header pointer to the first four bytes of the data packet
  */
 static int pcm_bluray_parse_header(AVCodecContext *avctx,
                                    const uint8_t *header)
 {
     static const uint8_t bits_per_samples[4] = { 0, 16, 20, 24 };
     static const uint32_t channel_layouts[16] = {
63e8d976
         0, AV_CH_LAYOUT_MONO, 0, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_SURROUND,
         AV_CH_LAYOUT_2_1, AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_2_2, AV_CH_LAYOUT_5POINT0,
         AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_7POINT0, AV_CH_LAYOUT_7POINT1, 0, 0, 0, 0
5dd3707b
     };
     static const uint8_t channels[16] = {
         0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8, 0, 0, 0, 0
     };
     uint8_t channel_layout = header[2] >> 4;
 
     if (avctx->debug & FF_DEBUG_PICT_INFO)
9ef5a9de
         av_dlog(avctx, "pcm_bluray_parse_header: header = %02x%02x%02x%02x\n",
5dd3707b
                 header[0], header[1], header[2], header[3]);
 
     /* get the sample depth and derive the sample format from it */
     avctx->bits_per_coded_sample = bits_per_samples[header[3] >> 6];
52807022
     if (!(avctx->bits_per_coded_sample == 16 || avctx->bits_per_coded_sample == 24)) {
cd0cfdc0
         av_log(avctx, AV_LOG_ERROR, "unsupported sample depth (%d)\n", avctx->bits_per_coded_sample);
5dd3707b
         return -1;
     }
5d6e4c16
     avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16 :
                                                              AV_SAMPLE_FMT_S32;
84e0553c
     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
         avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
5dd3707b
 
d041dec3
     /* get the sample rate. Not all values are used. */
5dd3707b
     switch (header[2] & 0x0f) {
     case 1:
         avctx->sample_rate = 48000;
         break;
     case 4:
         avctx->sample_rate = 96000;
         break;
     case 5:
         avctx->sample_rate = 192000;
         break;
     default:
         avctx->sample_rate = 0;
d041dec3
         av_log(avctx, AV_LOG_ERROR, "reserved sample rate (%d)\n",
5dd3707b
                header[2] & 0x0f);
         return -1;
     }
 
     /*
d041dec3
      * get the channel number (and mapping). Not all values are used.
5dd3707b
      * It must be noted that the number of channels in the MPEG stream can
      * differ from the actual meaningful number, e.g. mono audio still has two
      * channels, one being empty.
      */
     avctx->channel_layout  = channel_layouts[channel_layout];
     avctx->channels        =        channels[channel_layout];
     if (!avctx->channels) {
d041dec3
         av_log(avctx, AV_LOG_ERROR, "reserved channel configuration (%d)\n",
5dd3707b
                channel_layout);
         return -1;
     }
 
4a742920
     avctx->bit_rate = FFALIGN(avctx->channels, 2) * avctx->sample_rate *
5dd3707b
                       avctx->bits_per_coded_sample;
 
     if (avctx->debug & FF_DEBUG_PICT_INFO)
9ef5a9de
         av_dlog(avctx,
4a742920
                 "pcm_bluray_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
5dd3707b
                 avctx->channels, avctx->bits_per_coded_sample,
                 avctx->sample_rate, avctx->bit_rate);
     return 0;
 }
 
bc057f75
 typedef struct PCMBRDecode {
     AVFrame frame;
 } PCMBRDecode;
 
 static av_cold int pcm_bluray_decode_init(AVCodecContext * avctx)
 {
     PCMBRDecode *s = avctx->priv_data;
 
     avcodec_get_frame_defaults(&s->frame);
     avctx->coded_frame = &s->frame;
 
     return 0;
 }
 
 static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
                                    int *got_frame_ptr, AVPacket *avpkt)
5dd3707b
 {
     const uint8_t *src = avpkt->data;
     int buf_size = avpkt->size;
bc057f75
     PCMBRDecode *s = avctx->priv_data;
bd3e07c8
     GetByteContext gb;
5dd3707b
     int num_source_channels, channel, retval;
bc057f75
     int sample_size, samples;
     int16_t *dst16;
     int32_t *dst32;
5dd3707b
 
     if (buf_size < 4) {
         av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
         return -1;
     }
 
     if (pcm_bluray_parse_header(avctx, src))
         return -1;
     src += 4;
     buf_size -= 4;
 
bd3e07c8
     bytestream2_init(&gb, src, buf_size);
 
5dd3707b
     /* There's always an even number of channels in the source */
     num_source_channels = FFALIGN(avctx->channels, 2);
f1320dc3
     sample_size = (num_source_channels * (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
5dd3707b
     samples = buf_size / sample_size;
 
bc057f75
     /* get output buffer */
     s->frame.nb_samples = samples;
594d4d5d
     if ((retval = ff_get_buffer(avctx, &s->frame)) < 0) {
bc057f75
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return retval;
5dd3707b
     }
bc057f75
     dst16 = (int16_t *)s->frame.data[0];
     dst32 = (int32_t *)s->frame.data[0];
5dd3707b
 
     if (samples) {
         switch (avctx->channel_layout) {
             /* cases with same number of source and coded channels */
63e8d976
         case AV_CH_LAYOUT_STEREO:
         case AV_CH_LAYOUT_4POINT0:
         case AV_CH_LAYOUT_2_2:
5dd3707b
             samples *= num_source_channels;
5d6e4c16
             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
5dd3707b
 #if HAVE_BIGENDIAN
bd3e07c8
                 bytestream2_get_buffer(&gb, dst16, buf_size);
5dd3707b
 #else
                 do {
bd3e07c8
                     *dst16++ = bytestream2_get_be16u(&gb);
5dd3707b
                 } while (--samples);
 #endif
             } else {
                 do {
bd3e07c8
                     *dst32++ = bytestream2_get_be24u(&gb) << 8;
5dd3707b
                 } while (--samples);
             }
             break;
         /* cases where number of source channels = coded channels + 1 */
63e8d976
         case AV_CH_LAYOUT_MONO:
         case AV_CH_LAYOUT_SURROUND:
         case AV_CH_LAYOUT_2_1:
         case AV_CH_LAYOUT_5POINT0:
5d6e4c16
             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
5dd3707b
                 do {
 #if HAVE_BIGENDIAN
bd3e07c8
                     bytestream2_get_buffer(&gb, dst16, avctx->channels * 2);
5dd3707b
                     dst16 += avctx->channels;
 #else
                     channel = avctx->channels;
                     do {
bd3e07c8
                         *dst16++ = bytestream2_get_be16u(&gb);
5dd3707b
                     } while (--channel);
 #endif
bd3e07c8
                     bytestream2_skip(&gb, 2);
5dd3707b
                 } while (--samples);
             } else {
                 do {
                     channel = avctx->channels;
                     do {
bd3e07c8
                         *dst32++ = bytestream2_get_be24u(&gb) << 8;
5dd3707b
                     } while (--channel);
bd3e07c8
                     bytestream2_skip(&gb, 3);
5dd3707b
                 } while (--samples);
             }
             break;
             /* remapping: L, R, C, LBack, RBack, LF */
63e8d976
         case AV_CH_LAYOUT_5POINT1:
5d6e4c16
             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
5dd3707b
                 do {
bd3e07c8
                     dst16[0] = bytestream2_get_be16u(&gb);
                     dst16[1] = bytestream2_get_be16u(&gb);
                     dst16[2] = bytestream2_get_be16u(&gb);
                     dst16[4] = bytestream2_get_be16u(&gb);
                     dst16[5] = bytestream2_get_be16u(&gb);
                     dst16[3] = bytestream2_get_be16u(&gb);
5dd3707b
                     dst16 += 6;
                 } while (--samples);
             } else {
                 do {
bd3e07c8
                     dst32[0] = bytestream2_get_be24u(&gb) << 8;
                     dst32[1] = bytestream2_get_be24u(&gb) << 8;
                     dst32[2] = bytestream2_get_be24u(&gb) << 8;
                     dst32[4] = bytestream2_get_be24u(&gb) << 8;
                     dst32[5] = bytestream2_get_be24u(&gb) << 8;
                     dst32[3] = bytestream2_get_be24u(&gb) << 8;
5dd3707b
                     dst32 += 6;
                 } while (--samples);
             }
             break;
             /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
63e8d976
         case AV_CH_LAYOUT_7POINT0:
5d6e4c16
             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
5dd3707b
                 do {
bd3e07c8
                     dst16[0] = bytestream2_get_be16u(&gb);
                     dst16[1] = bytestream2_get_be16u(&gb);
                     dst16[2] = bytestream2_get_be16u(&gb);
                     dst16[5] = bytestream2_get_be16u(&gb);
                     dst16[3] = bytestream2_get_be16u(&gb);
                     dst16[4] = bytestream2_get_be16u(&gb);
                     dst16[6] = bytestream2_get_be16u(&gb);
5dd3707b
                     dst16 += 7;
bd3e07c8
                     bytestream2_skip(&gb, 2);
5dd3707b
                 } while (--samples);
             } else {
                 do {
bd3e07c8
                     dst32[0] = bytestream2_get_be24u(&gb) << 8;
                     dst32[1] = bytestream2_get_be24u(&gb) << 8;
                     dst32[2] = bytestream2_get_be24u(&gb) << 8;
                     dst32[5] = bytestream2_get_be24u(&gb) << 8;
                     dst32[3] = bytestream2_get_be24u(&gb) << 8;
                     dst32[4] = bytestream2_get_be24u(&gb) << 8;
                     dst32[6] = bytestream2_get_be24u(&gb) << 8;
5dd3707b
                     dst32 += 7;
bd3e07c8
                     bytestream2_skip(&gb, 3);
5dd3707b
                 } while (--samples);
             }
             break;
             /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
63e8d976
         case AV_CH_LAYOUT_7POINT1:
5d6e4c16
             if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
5dd3707b
                 do {
bd3e07c8
                     dst16[0] = bytestream2_get_be16u(&gb);
                     dst16[1] = bytestream2_get_be16u(&gb);
                     dst16[2] = bytestream2_get_be16u(&gb);
                     dst16[6] = bytestream2_get_be16u(&gb);
                     dst16[4] = bytestream2_get_be16u(&gb);
                     dst16[5] = bytestream2_get_be16u(&gb);
                     dst16[7] = bytestream2_get_be16u(&gb);
                     dst16[3] = bytestream2_get_be16u(&gb);
5dd3707b
                     dst16 += 8;
                 } while (--samples);
             } else {
                 do {
bd3e07c8
                     dst32[0] = bytestream2_get_be24u(&gb) << 8;
                     dst32[1] = bytestream2_get_be24u(&gb) << 8;
                     dst32[2] = bytestream2_get_be24u(&gb) << 8;
                     dst32[6] = bytestream2_get_be24u(&gb) << 8;
                     dst32[4] = bytestream2_get_be24u(&gb) << 8;
                     dst32[5] = bytestream2_get_be24u(&gb) << 8;
                     dst32[7] = bytestream2_get_be24u(&gb) << 8;
                     dst32[3] = bytestream2_get_be24u(&gb) << 8;
5dd3707b
                     dst32 += 8;
                 } while (--samples);
             }
             break;
         }
     }
 
bc057f75
     *got_frame_ptr   = 1;
     *(AVFrame *)data = s->frame;
 
bd3e07c8
     retval = bytestream2_tell(&gb);
5dd3707b
     if (avctx->debug & FF_DEBUG_BITSTREAM)
9ef5a9de
         av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
bc057f75
                 retval, buf_size);
c58bcead
     return retval + 4;
5dd3707b
 }
 
e7e2df27
 AVCodec ff_pcm_bluray_decoder = {
ec6402b7
     .name           = "pcm_bluray",
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_PCM_BLURAY,
bc057f75
     .priv_data_size = sizeof(PCMBRDecode),
     .init           = pcm_bluray_decode_init,
ec6402b7
     .decode         = pcm_bluray_decode_frame,
bc057f75
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .sample_fmts    = (const enum AVSampleFormat[]){
         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
     },
     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
5dd3707b
 };