LCOV - code coverage report
Current view: top level - libavcodec - pcm-dvd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 78 151 51.7 %
Date: 2017-12-15 18:13:28 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * LPCM codecs for PCM formats found in Video DVD streams
       3             :  * Copyright (c) 2013 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             :  * LPCM codecs for PCM formats found in Video DVD streams
      25             :  */
      26             : 
      27             : #include "avcodec.h"
      28             : #include "bytestream.h"
      29             : #include "internal.h"
      30             : 
      31             : typedef struct PCMDVDContext {
      32             :     uint32_t last_header;    // Cached header to see if parsing is needed
      33             :     int block_size;          // Size of a block of samples in bytes
      34             :     int last_block_size;     // Size of the last block of samples in bytes
      35             :     int samples_per_block;   // Number of samples per channel per block
      36             :     int groups_per_block;    // Number of 20/24-bit sample groups per block
      37             :     uint8_t *extra_samples;  // Pointer to leftover samples from a frame
      38             :     int extra_sample_count;  // Number of leftover samples in the buffer
      39             : } PCMDVDContext;
      40             : 
      41           2 : static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
      42             : {
      43           2 :     PCMDVDContext *s = avctx->priv_data;
      44             : 
      45             :     /* Invalid header to force parsing of the first header */
      46           2 :     s->last_header = -1;
      47             :     /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
      48           2 :     if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
      49           0 :         return AVERROR(ENOMEM);
      50             : 
      51           2 :     return 0;
      52             : }
      53             : 
      54           2 : static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
      55             : {
      56           2 :     PCMDVDContext *s = avctx->priv_data;
      57             : 
      58           2 :     av_freep(&s->extra_samples);
      59             : 
      60           2 :     return 0;
      61             : }
      62             : 
      63         124 : static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
      64             : {
      65             :     /* no traces of 44100 and 32000Hz in any commercial software or player */
      66             :     static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
      67         124 :     PCMDVDContext *s = avctx->priv_data;
      68         124 :     int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);
      69             : 
      70             :     /* early exit if the header didn't change apart from the frame number */
      71         124 :     if (s->last_header == header_int)
      72         122 :         return 0;
      73           2 :     s->last_header = -1;
      74             : 
      75           2 :     if (avctx->debug & FF_DEBUG_PICT_INFO)
      76           0 :         av_log(avctx, AV_LOG_DEBUG, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
      77           0 :                 header[0], header[1], header[2]);
      78             :     /*
      79             :      * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
      80             :      * header[1] quant (2), freq(2), reserved(1), channels(3)
      81             :      * header[2] dynamic range control (0x80 = off)
      82             :      */
      83             : 
      84             :     /* Discard potentially existing leftover samples from old channel layout */
      85           2 :     s->extra_sample_count = 0;
      86             : 
      87             :     /* get the sample depth and derive the sample format from it */
      88           2 :     avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
      89           2 :     if (avctx->bits_per_coded_sample == 28) {
      90           0 :         av_log(avctx, AV_LOG_ERROR,
      91             :                "PCM DVD unsupported sample depth %i\n",
      92             :                avctx->bits_per_coded_sample);
      93           0 :         return AVERROR_INVALIDDATA;
      94             :     }
      95           4 :     avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
      96           2 :                                                            : AV_SAMPLE_FMT_S32;
      97           2 :     avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
      98             : 
      99             :     /* get the sample rate */
     100           2 :     avctx->sample_rate = frequencies[header[1] >> 4 & 3];
     101             : 
     102             :     /* get the number of channels */
     103           2 :     avctx->channels = 1 + (header[1] & 7);
     104             :     /* calculate the bitrate */
     105           6 :     avctx->bit_rate = avctx->channels *
     106           4 :                       avctx->sample_rate *
     107           2 :                       avctx->bits_per_coded_sample;
     108             : 
     109             :     /* 4 samples form a group in 20/24-bit PCM on DVD Video.
     110             :      * A block is formed by the number of groups that are
     111             :      * needed to complete a set of samples for each channel. */
     112           2 :     if (avctx->bits_per_coded_sample == 16) {
     113           0 :         s->samples_per_block = 1;
     114           0 :         s->block_size        = avctx->channels * 2;
     115             :     } else {
     116           2 :         switch (avctx->channels) {
     117           2 :         case 1:
     118             :         case 2:
     119             :         case 4:
     120             :             /* one group has all the samples needed */
     121           2 :             s->block_size        = 4 * avctx->bits_per_coded_sample / 8;
     122           2 :             s->samples_per_block = 4 / avctx->channels;
     123           2 :             s->groups_per_block  = 1;
     124           2 :             break;
     125           0 :         case 8:
     126             :             /* two groups have all the samples needed */
     127           0 :             s->block_size        = 8 * avctx->bits_per_coded_sample / 8;
     128           0 :             s->samples_per_block = 1;
     129           0 :             s->groups_per_block  = 2;
     130           0 :             break;
     131           0 :         default:
     132             :             /* need avctx->channels groups */
     133           0 :             s->block_size        = 4 * avctx->channels *
     134           0 :                                    avctx->bits_per_coded_sample / 8;
     135           0 :             s->samples_per_block = 4;
     136           0 :             s->groups_per_block  = avctx->channels;
     137           0 :             break;
     138             :         }
     139             :     }
     140             : 
     141           2 :     if (avctx->debug & FF_DEBUG_PICT_INFO)
     142             :         ff_dlog(avctx,
     143             :                 "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %"PRId64" bit/s\n",
     144             :                 avctx->channels, avctx->bits_per_coded_sample,
     145             :                 avctx->sample_rate, avctx->bit_rate);
     146             : 
     147           2 :     s->last_header = header_int;
     148             : 
     149           2 :     return 0;
     150             : }
     151             : 
     152         124 : static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
     153             :                                     void *dst, int blocks)
     154             : {
     155         124 :     PCMDVDContext *s = avctx->priv_data;
     156         124 :     int16_t *dst16   = dst;
     157         124 :     int32_t *dst32   = dst;
     158             :     GetByteContext gb;
     159             :     int i;
     160             :     uint8_t t;
     161             : 
     162         124 :     bytestream2_init(&gb, src, blocks * s->block_size);
     163         124 :     switch (avctx->bits_per_coded_sample) {
     164           0 :     case 16: {
     165             : #if HAVE_BIGENDIAN
     166             :         bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
     167             :         dst16 += blocks * s->block_size / 2;
     168             : #else
     169           0 :         int samples = blocks * avctx->channels;
     170             :         do {
     171           0 :             *dst16++ = bytestream2_get_be16u(&gb);
     172           0 :         } while (--samples);
     173             : #endif
     174           0 :         return dst16;
     175             :     }
     176           0 :     case 20:
     177           0 :         if (avctx->channels == 1) {
     178             :             do {
     179           0 :                 for (i = 2; i; i--) {
     180           0 :                     dst32[0] = bytestream2_get_be16u(&gb) << 16;
     181           0 :                     dst32[1] = bytestream2_get_be16u(&gb) << 16;
     182           0 :                     t = bytestream2_get_byteu(&gb);
     183           0 :                     *dst32++ += (t & 0xf0) << 8;
     184           0 :                     *dst32++ += (t & 0x0f) << 12;
     185             :                 }
     186           0 :             } while (--blocks);
     187             :         } else {
     188             :         do {
     189           0 :             for (i = s->groups_per_block; i; i--) {
     190           0 :                 dst32[0] = bytestream2_get_be16u(&gb) << 16;
     191           0 :                 dst32[1] = bytestream2_get_be16u(&gb) << 16;
     192           0 :                 dst32[2] = bytestream2_get_be16u(&gb) << 16;
     193           0 :                 dst32[3] = bytestream2_get_be16u(&gb) << 16;
     194           0 :                 t = bytestream2_get_byteu(&gb);
     195           0 :                 *dst32++ += (t & 0xf0) << 8;
     196           0 :                 *dst32++ += (t & 0x0f) << 12;
     197           0 :                 t = bytestream2_get_byteu(&gb);
     198           0 :                 *dst32++ += (t & 0xf0) << 8;
     199           0 :                 *dst32++ += (t & 0x0f) << 12;
     200             :             }
     201           0 :         } while (--blocks);
     202             :         }
     203           0 :         return dst32;
     204         124 :     case 24:
     205         124 :         if (avctx->channels == 1) {
     206             :             do {
     207           0 :                 for (i = 2; i; i--) {
     208           0 :                     dst32[0] = bytestream2_get_be16u(&gb) << 16;
     209           0 :                     dst32[1] = bytestream2_get_be16u(&gb) << 16;
     210           0 :                     *dst32++ += bytestream2_get_byteu(&gb) << 8;
     211           0 :                     *dst32++ += bytestream2_get_byteu(&gb) << 8;
     212             :                 }
     213           0 :             } while (--blocks);
     214             :         } else {
     215             :         do {
     216       41416 :             for (i = s->groups_per_block; i; i--) {
     217       20708 :                 dst32[0] = bytestream2_get_be16u(&gb) << 16;
     218       20708 :                 dst32[1] = bytestream2_get_be16u(&gb) << 16;
     219       20708 :                 dst32[2] = bytestream2_get_be16u(&gb) << 16;
     220       20708 :                 dst32[3] = bytestream2_get_be16u(&gb) << 16;
     221       20708 :                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
     222       20708 :                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
     223       20708 :                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
     224       20708 :                 *dst32++ += bytestream2_get_byteu(&gb) << 8;
     225             :             }
     226       20708 :         } while (--blocks);
     227             :         }
     228         124 :         return dst32;
     229           0 :     default:
     230           0 :         return NULL;
     231             :     }
     232             : }
     233             : 
     234         124 : static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
     235             :                                 int *got_frame_ptr, AVPacket *avpkt)
     236             : {
     237         124 :     AVFrame *frame     = data;
     238         124 :     const uint8_t *src = avpkt->data;
     239         124 :     int buf_size       = avpkt->size;
     240         124 :     PCMDVDContext *s   = avctx->priv_data;
     241             :     int retval;
     242             :     int blocks;
     243             :     void *dst;
     244             : 
     245         124 :     if (buf_size < 3) {
     246           0 :         av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
     247           0 :         return AVERROR_INVALIDDATA;
     248             :     }
     249             : 
     250         124 :     if ((retval = pcm_dvd_parse_header(avctx, src)))
     251           0 :         return retval;
     252         124 :     if (s->last_block_size && s->last_block_size != s->block_size) {
     253           0 :         av_log(avctx, AV_LOG_WARNING, "block_size has changed %d != %d\n", s->last_block_size, s->block_size);
     254           0 :         s->extra_sample_count = 0;
     255             :     }
     256         124 :     s->last_block_size = s->block_size;
     257         124 :     src      += 3;
     258         124 :     buf_size -= 3;
     259             : 
     260         124 :     blocks = (buf_size + s->extra_sample_count) / s->block_size;
     261             : 
     262             :     /* get output buffer */
     263         124 :     frame->nb_samples = blocks * s->samples_per_block;
     264         124 :     if ((retval = ff_get_buffer(avctx, frame, 0)) < 0)
     265           0 :         return retval;
     266         124 :     dst = frame->data[0];
     267             : 
     268             :     /* consume leftover samples from last packet */
     269         124 :     if (s->extra_sample_count) {
     270           0 :         int missing_samples = s->block_size - s->extra_sample_count;
     271           0 :         if (buf_size >= missing_samples) {
     272           0 :             memcpy(s->extra_samples + s->extra_sample_count, src,
     273             :                    missing_samples);
     274           0 :             dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
     275           0 :             src += missing_samples;
     276           0 :             buf_size -= missing_samples;
     277           0 :             s->extra_sample_count = 0;
     278           0 :             blocks--;
     279             :         } else {
     280             :             /* new packet still doesn't have enough samples */
     281           0 :             memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
     282           0 :             s->extra_sample_count += buf_size;
     283           0 :             return avpkt->size;
     284             :         }
     285             :     }
     286             : 
     287             :     /* decode remaining complete samples */
     288         124 :     if (blocks) {
     289         124 :         pcm_dvd_decode_samples(avctx, src, dst, blocks);
     290         124 :         buf_size -= blocks * s->block_size;
     291             :     }
     292             : 
     293             :     /* store leftover samples */
     294         124 :     if (buf_size) {
     295           0 :         src += blocks * s->block_size;
     296           0 :         memcpy(s->extra_samples, src, buf_size);
     297           0 :         s->extra_sample_count = buf_size;
     298             :     }
     299             : 
     300         124 :     *got_frame_ptr = 1;
     301             : 
     302         124 :     return avpkt->size;
     303             : }
     304             : 
     305             : AVCodec ff_pcm_dvd_decoder = {
     306             :     .name           = "pcm_dvd",
     307             :     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
     308             :     .type           = AVMEDIA_TYPE_AUDIO,
     309             :     .id             = AV_CODEC_ID_PCM_DVD,
     310             :     .priv_data_size = sizeof(PCMDVDContext),
     311             :     .init           = pcm_dvd_decode_init,
     312             :     .decode         = pcm_dvd_decode_frame,
     313             :     .close          = pcm_dvd_decode_uninit,
     314             :     .capabilities   = AV_CODEC_CAP_DR1,
     315             :     .sample_fmts    = (const enum AVSampleFormat[]) {
     316             :         AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
     317             :     }
     318             : };

Generated by: LCOV version 1.13