LCOV - code coverage report
Current view: top level - libavcodec - aac_adtstoasc_bsf.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 68 51.5 %
Date: 2017-12-11 20:48:03 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
       3             :  * Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
       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             : #include "adts_header.h"
      23             : #include "adts_parser.h"
      24             : #include "avcodec.h"
      25             : #include "bsf.h"
      26             : #include "put_bits.h"
      27             : #include "get_bits.h"
      28             : #include "mpeg4audio.h"
      29             : #include "internal.h"
      30             : 
      31             : typedef struct AACBSFContext {
      32             :     int first_frame_done;
      33             : } AACBSFContext;
      34             : 
      35             : /**
      36             :  * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
      37             :  * ADTS header and removes the ADTS header.
      38             :  */
      39         206 : static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *out)
      40             : {
      41         206 :     AACBSFContext *ctx = bsfc->priv_data;
      42             : 
      43             :     GetBitContext gb;
      44             :     PutBitContext pb;
      45             :     AACADTSHeaderInfo hdr;
      46             :     AVPacket *in;
      47             :     int ret;
      48             : 
      49         206 :     ret = ff_bsf_get_packet(bsfc, &in);
      50         206 :     if (ret < 0)
      51          86 :         return ret;
      52             : 
      53         120 :     if (bsfc->par_in->extradata && in->size >= 2 && (AV_RB16(in->data) >> 4) != 0xfff)
      54           0 :         goto finish;
      55             : 
      56         120 :     if (in->size < AV_AAC_ADTS_HEADER_SIZE)
      57           0 :         goto packet_too_small;
      58             : 
      59         120 :     init_get_bits(&gb, in->data, AV_AAC_ADTS_HEADER_SIZE * 8);
      60             : 
      61         120 :     if (ff_adts_header_parse(&gb, &hdr) < 0) {
      62           0 :         av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
      63           0 :         ret = AVERROR_INVALIDDATA;
      64           0 :         goto fail;
      65             :     }
      66             : 
      67         120 :     if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
      68           0 :         avpriv_report_missing_feature(bsfc,
      69             :                                       "Multiple RDBs per frame with CRC");
      70           0 :         ret = AVERROR_PATCHWELCOME;
      71           0 :         goto fail;
      72             :     }
      73             : 
      74         120 :     in->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
      75         120 :     if (in->size <= 0)
      76           0 :         goto packet_too_small;
      77         120 :     in->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
      78             : 
      79         120 :     if (!ctx->first_frame_done) {
      80           2 :         int            pce_size = 0;
      81             :         uint8_t        pce_data[MAX_PCE_SIZE];
      82             :         uint8_t       *extradata;
      83             : 
      84           2 :         if (!hdr.chan_config) {
      85           0 :             init_get_bits(&gb, in->data, in->size * 8);
      86           0 :             if (get_bits(&gb, 3) != 5) {
      87           0 :                 avpriv_report_missing_feature(bsfc,
      88             :                                               "PCE-based channel configuration "
      89             :                                               "without PCE as first syntax "
      90             :                                               "element");
      91           0 :                 ret = AVERROR_PATCHWELCOME;
      92           0 :                 goto fail;
      93             :             }
      94           0 :             init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
      95           0 :             pce_size = ff_copy_pce_data(&pb, &gb) / 8;
      96           0 :             flush_put_bits(&pb);
      97           0 :             in->size -= get_bits_count(&gb)/8;
      98           0 :             in->data += get_bits_count(&gb)/8;
      99             :         }
     100             : 
     101           2 :         extradata = av_packet_new_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
     102             :                                             2 + pce_size);
     103           2 :         if (!extradata) {
     104           0 :             ret = AVERROR(ENOMEM);
     105           0 :             goto fail;
     106             :         }
     107             : 
     108           2 :         init_put_bits(&pb, extradata, 2 + pce_size);
     109           2 :         put_bits(&pb, 5, hdr.object_type);
     110           2 :         put_bits(&pb, 4, hdr.sampling_index);
     111           2 :         put_bits(&pb, 4, hdr.chan_config);
     112           2 :         put_bits(&pb, 1, 0); //frame length - 1024 samples
     113           2 :         put_bits(&pb, 1, 0); //does not depend on core coder
     114           2 :         put_bits(&pb, 1, 0); //is not extension
     115           2 :         flush_put_bits(&pb);
     116           2 :         if (pce_size) {
     117           0 :             memcpy(extradata + 2, pce_data, pce_size);
     118             :         }
     119             : 
     120           2 :         ctx->first_frame_done = 1;
     121             :     }
     122             : 
     123         118 : finish:
     124         120 :     av_packet_move_ref(out, in);
     125         120 :     av_packet_free(&in);
     126             : 
     127         120 :     return 0;
     128             : 
     129           0 : packet_too_small:
     130           0 :     av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
     131           0 :     ret = AVERROR_INVALIDDATA;
     132           0 : fail:
     133           0 :     av_packet_free(&in);
     134           0 :     return ret;
     135             : }
     136             : 
     137           2 : static int aac_adtstoasc_init(AVBSFContext *ctx)
     138             : {
     139             :     /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
     140           2 :     if (ctx->par_in->extradata) {
     141             :         MPEG4AudioConfig mp4ac;
     142           0 :         int ret = avpriv_mpeg4audio_get_config(&mp4ac, ctx->par_in->extradata,
     143           0 :                                                ctx->par_in->extradata_size * 8, 1);
     144           0 :         if (ret < 0) {
     145           0 :             av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
     146           0 :             return ret;
     147             :         }
     148             :     }
     149             : 
     150           2 :     return 0;
     151             : }
     152             : 
     153             : static const enum AVCodecID codec_ids[] = {
     154             :     AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
     155             : };
     156             : 
     157             : const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
     158             :     .name           = "aac_adtstoasc",
     159             :     .priv_data_size = sizeof(AACBSFContext),
     160             :     .init           = aac_adtstoasc_init,
     161             :     .filter         = aac_adtstoasc_filter,
     162             :     .codec_ids      = codec_ids,
     163             : };

Generated by: LCOV version 1.13