LCOV - code coverage report
Current view: top level - libavformat - bit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 70 7.1 %
Date: 2017-12-16 01:21:47 Functions: 1 5 20.0 %

          Line data    Source code
       1             : /*
       2             :  * G.729 bit format muxer and demuxer
       3             :  * Copyright (c) 2007-2008 Vladimir Voroshilov
       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             : #include "avformat.h"
      22             : #include "internal.h"
      23             : #include "libavcodec/get_bits.h"
      24             : #include "libavcodec/put_bits.h"
      25             : 
      26             : #define MAX_FRAME_SIZE 10
      27             : 
      28             : #define SYNC_WORD  0x6b21
      29             : #define BIT_0      0x7f
      30             : #define BIT_1      0x81
      31             : 
      32             : #if CONFIG_BIT_DEMUXER
      33        6130 : static int probe(AVProbeData *p)
      34             : {
      35        6130 :     int i = 0, j, valid = 0;
      36             : 
      37       12260 :     while (2 * i + 3 < p->buf_size){
      38        6130 :         if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
      39        6130 :             return 0;
      40           0 :         j = AV_RL16(&p->buf[2 * i++]);
      41           0 :         if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
      42           0 :             return 0;
      43           0 :         if (j)
      44           0 :             valid++;
      45           0 :         i += j;
      46             :     }
      47           0 :     if (valid > 10)
      48           0 :         return AVPROBE_SCORE_MAX;
      49           0 :     if (valid > 2)
      50           0 :         return AVPROBE_SCORE_EXTENSION - 1;
      51           0 :     return 0;
      52             : }
      53             : 
      54           0 : static int read_header(AVFormatContext *s)
      55             : {
      56             :     AVStream* st;
      57             : 
      58           0 :     st=avformat_new_stream(s, NULL);
      59           0 :     if (!st)
      60           0 :         return AVERROR(ENOMEM);
      61             : 
      62           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
      63           0 :     st->codecpar->codec_id=AV_CODEC_ID_G729;
      64           0 :     st->codecpar->sample_rate=8000;
      65           0 :     st->codecpar->block_align = 16;
      66           0 :     st->codecpar->channels=1;
      67             : 
      68           0 :     avpriv_set_pts_info(st, 64, 1, 100);
      69           0 :     return 0;
      70             : }
      71             : 
      72           0 : static int read_packet(AVFormatContext *s,
      73             :                           AVPacket *pkt)
      74             : {
      75           0 :     AVIOContext *pb = s->pb;
      76             :     PutBitContext pbo;
      77             :     uint16_t buf[8 * MAX_FRAME_SIZE + 2];
      78             :     int packet_size;
      79           0 :     uint16_t* src=buf;
      80             :     int i, j, ret;
      81           0 :     int64_t pos= avio_tell(pb);
      82             : 
      83           0 :     if(avio_feof(pb))
      84           0 :         return AVERROR_EOF;
      85             : 
      86           0 :     avio_rl16(pb); // sync word
      87           0 :     packet_size = avio_rl16(pb) / 8;
      88           0 :     if(packet_size > MAX_FRAME_SIZE)
      89           0 :         return AVERROR_INVALIDDATA;
      90             : 
      91           0 :     ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
      92           0 :     if(ret<0)
      93           0 :         return ret;
      94           0 :     if(ret != 8 * packet_size * sizeof(uint16_t))
      95           0 :         return AVERROR(EIO);
      96             : 
      97           0 :     if (av_new_packet(pkt, packet_size) < 0)
      98           0 :         return AVERROR(ENOMEM);
      99             : 
     100           0 :     init_put_bits(&pbo, pkt->data, packet_size);
     101           0 :     for(j=0; j < packet_size; j++)
     102           0 :         for(i=0; i<8;i++)
     103           0 :             put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
     104             : 
     105           0 :     flush_put_bits(&pbo);
     106             : 
     107           0 :     pkt->duration=1;
     108           0 :     pkt->pos = pos;
     109           0 :     return 0;
     110             : }
     111             : 
     112             : AVInputFormat ff_bit_demuxer = {
     113             :     .name        = "bit",
     114             :     .long_name   = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
     115             :     .read_probe  = probe,
     116             :     .read_header = read_header,
     117             :     .read_packet = read_packet,
     118             :     .extensions  = "bit",
     119             : };
     120             : #endif
     121             : 
     122             : #if CONFIG_BIT_MUXER
     123           0 : static int write_header(AVFormatContext *s)
     124             : {
     125           0 :     AVCodecParameters *par = s->streams[0]->codecpar;
     126             : 
     127           0 :     if ((par->codec_id != AV_CODEC_ID_G729) || par->channels != 1) {
     128           0 :         av_log(s, AV_LOG_ERROR,
     129             :                "only codec g729 with 1 channel is supported by this format\n");
     130           0 :         return AVERROR(EINVAL);
     131             :     }
     132             : 
     133           0 :     par->bits_per_coded_sample = 16;
     134           0 :     par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
     135             : 
     136           0 :     return 0;
     137             : }
     138             : 
     139           0 : static int write_packet(AVFormatContext *s, AVPacket *pkt)
     140             : {
     141           0 :     AVIOContext *pb = s->pb;
     142             :     GetBitContext gb;
     143             :     int i;
     144             : 
     145           0 :     if (pkt->size != 10)
     146           0 :         return AVERROR(EINVAL);
     147             : 
     148           0 :     avio_wl16(pb, SYNC_WORD);
     149           0 :     avio_wl16(pb, 8 * pkt->size);
     150             : 
     151           0 :     init_get_bits(&gb, pkt->data, 8 * pkt->size);
     152           0 :     for (i = 0; i < 8 * pkt->size; i++)
     153           0 :         avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
     154             : 
     155           0 :     return 0;
     156             : }
     157             : 
     158             : AVOutputFormat ff_bit_muxer = {
     159             :     .name         = "bit",
     160             :     .long_name    = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
     161             :     .mime_type    = "audio/bit",
     162             :     .extensions   = "bit",
     163             :     .audio_codec  = AV_CODEC_ID_G729,
     164             :     .video_codec  = AV_CODEC_ID_NONE,
     165             :     .write_header = write_header,
     166             :     .write_packet = write_packet,
     167             : };
     168             : #endif

Generated by: LCOV version 1.13