LCOV - code coverage report
Current view: top level - libavformat - ttaenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 62 76 81.6 %
Date: 2017-12-14 19:11:59 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * True Audio (TTA) muxer
       3             :  * Copyright (c) 2016 James Almer
       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 "libavutil/crc.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : 
      25             : #include "apetag.h"
      26             : #include "avformat.h"
      27             : #include "avio_internal.h"
      28             : #include "internal.h"
      29             : 
      30             : typedef struct TTAMuxContext {
      31             :     AVIOContext *seek_table;
      32             :     AVPacketList *queue, *queue_end;
      33             :     uint32_t nb_samples;
      34             :     int frame_size;
      35             :     int last_frame;
      36             : } TTAMuxContext;
      37             : 
      38           2 : static int tta_init(AVFormatContext *s)
      39             : {
      40           2 :     TTAMuxContext *tta = s->priv_data;
      41             :     AVCodecParameters *par;
      42             : 
      43           2 :     if (s->nb_streams != 1) {
      44           0 :         av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
      45           0 :         return AVERROR(EINVAL);
      46             :     }
      47           2 :     par = s->streams[0]->codecpar;
      48             : 
      49           2 :     if (par->codec_id != AV_CODEC_ID_TTA) {
      50           0 :         av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
      51           0 :         return AVERROR(EINVAL);
      52             :     }
      53           2 :     if (par->extradata && par->extradata_size < 22) {
      54           0 :         av_log(s, AV_LOG_ERROR, "Invalid TTA extradata\n");
      55           0 :         return AVERROR_INVALIDDATA;
      56             :     }
      57             : 
      58             :     /* Prevent overflow */
      59           2 :     if (par->sample_rate > 0x7FFFFFu) {
      60           0 :         av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
      61           0 :         return AVERROR(EINVAL);
      62             :     }
      63           2 :     tta->frame_size = par->sample_rate * 256 / 245;
      64           2 :     avpriv_set_pts_info(s->streams[0], 64, 1, par->sample_rate);
      65             : 
      66           2 :     return 0;
      67             : }
      68             : 
      69           2 : static int tta_write_header(AVFormatContext *s)
      70             : {
      71           2 :     TTAMuxContext *tta = s->priv_data;
      72           2 :     AVCodecParameters *par = s->streams[0]->codecpar;
      73             :     int ret;
      74             : 
      75           2 :     if ((ret = avio_open_dyn_buf(&tta->seek_table)) < 0)
      76           0 :         return ret;
      77             : 
      78             :     /* Ignore most extradata information if present. It can be innacurate
      79             :        if for example remuxing from Matroska */
      80           2 :     ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
      81           2 :     ffio_init_checksum(tta->seek_table, ff_crcEDB88320_update, UINT32_MAX);
      82           2 :     avio_write(s->pb, "TTA1", 4);
      83           2 :     avio_wl16(s->pb, par->extradata ? AV_RL16(par->extradata + 4) : 1);
      84           2 :     avio_wl16(s->pb, par->channels);
      85           2 :     avio_wl16(s->pb, par->bits_per_raw_sample);
      86           2 :     avio_wl32(s->pb, par->sample_rate);
      87             : 
      88           2 :     return 0;
      89             : }
      90             : 
      91           7 : static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
      92             : {
      93           7 :     TTAMuxContext *tta = s->priv_data;
      94           7 :     AVPacketList *pktl = av_mallocz(sizeof(*pktl));
      95             :     int ret;
      96             : 
      97           7 :     if (!pktl)
      98           0 :         return AVERROR(ENOMEM);
      99             : 
     100           7 :     ret = av_packet_ref(&pktl->pkt, pkt);
     101           7 :     if (ret < 0) {
     102           0 :         av_free(pktl);
     103           0 :         return ret;
     104             :     }
     105           7 :     if (tta->queue_end)
     106           5 :         tta->queue_end->next = pktl;
     107             :     else
     108           2 :         tta->queue = pktl;
     109           7 :     tta->queue_end = pktl;
     110             : 
     111           7 :     avio_wl32(tta->seek_table, pkt->size);
     112           7 :     tta->nb_samples += pkt->duration;
     113             : 
     114           7 :     if (tta->frame_size != pkt->duration) {
     115           2 :         if (tta->last_frame) {
     116             :             /* Two frames with a different duration than the default frame
     117             :                size means the TTA stream comes from a faulty container, and
     118             :                there's no way the last frame duration will be correct. */
     119           0 :             av_log(s, AV_LOG_ERROR, "Invalid frame durations\n");
     120             : 
     121           0 :             return AVERROR_INVALIDDATA;
     122             :         }
     123             :         /* First frame with a different duration than the default frame size.
     124             :            Assume it's the last frame in the stream and continue. */
     125           2 :         tta->last_frame++;
     126             :     }
     127             : 
     128           7 :     return 0;
     129             : }
     130             : 
     131           2 : static void tta_queue_flush(AVFormatContext *s)
     132             : {
     133           2 :     TTAMuxContext *tta = s->priv_data;
     134             :     AVPacketList *pktl;
     135             : 
     136          11 :     while (pktl = tta->queue) {
     137           7 :         AVPacket *pkt = &pktl->pkt;
     138           7 :         avio_write(s->pb, pkt->data, pkt->size);
     139           7 :         av_packet_unref(pkt);
     140           7 :         tta->queue = pktl->next;
     141           7 :         av_free(pktl);
     142             :     }
     143           2 :     tta->queue_end = NULL;
     144           2 : }
     145             : 
     146           2 : static int tta_write_trailer(AVFormatContext *s)
     147             : {
     148           2 :     TTAMuxContext *tta = s->priv_data;
     149             :     uint8_t *ptr;
     150             :     unsigned int crc;
     151             :     int size;
     152             : 
     153           2 :     avio_wl32(s->pb, tta->nb_samples);
     154           2 :     crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
     155           2 :     avio_wl32(s->pb, crc);
     156             : 
     157             :     /* Write Seek table */
     158           2 :     crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
     159           2 :     avio_wl32(tta->seek_table, crc);
     160           2 :     size = avio_close_dyn_buf(tta->seek_table, &ptr);
     161           2 :     avio_write(s->pb, ptr, size);
     162           2 :     av_free(ptr);
     163             : 
     164             :     /* Write audio data */
     165           2 :     tta_queue_flush(s);
     166             : 
     167           2 :     ff_ape_write_tag(s);
     168           2 :     avio_flush(s->pb);
     169             : 
     170           2 :     return 0;
     171             : }
     172             : 
     173             : AVOutputFormat ff_tta_muxer = {
     174             :     .name              = "tta",
     175             :     .long_name         = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
     176             :     .mime_type         = "audio/x-tta",
     177             :     .extensions        = "tta",
     178             :     .priv_data_size    = sizeof(TTAMuxContext),
     179             :     .audio_codec       = AV_CODEC_ID_TTA,
     180             :     .video_codec       = AV_CODEC_ID_NONE,
     181             :     .init              = tta_init,
     182             :     .write_header      = tta_write_header,
     183             :     .write_packet      = tta_write_packet,
     184             :     .write_trailer     = tta_write_trailer,
     185             : };

Generated by: LCOV version 1.13