LCOV - code coverage report
Current view: top level - src/libavformat - bit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 65 9.2 %
Date: 2017-01-24 04:42:20 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        5563 : static int probe(AVProbeData *p)
      33             : {
      34             :     int i, j;
      35             : 
      36        5563 :     if(p->buf_size < 0x40)
      37           2 :         return 0;
      38             : 
      39       11122 :     for(i=0; i+3<p->buf_size && i< 10*0x50; ){
      40        5561 :         if(AV_RL16(&p->buf[0]) != SYNC_WORD)
      41        5561 :             return 0;
      42           0 :         j=AV_RL16(&p->buf[2]);
      43           0 :         if(j!=0x40 && j!=0x50)
      44           0 :             return 0;
      45           0 :         i+=j;
      46             :     }
      47           0 :     return AVPROBE_SCORE_EXTENSION;
      48             : }
      49             : 
      50           0 : static int read_header(AVFormatContext *s)
      51             : {
      52             :     AVStream* st;
      53             : 
      54           0 :     st=avformat_new_stream(s, NULL);
      55           0 :     if (!st)
      56           0 :         return AVERROR(ENOMEM);
      57             : 
      58           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
      59           0 :     st->codecpar->codec_id=AV_CODEC_ID_G729;
      60           0 :     st->codecpar->sample_rate=8000;
      61           0 :     st->codecpar->block_align = 16;
      62           0 :     st->codecpar->channels=1;
      63             : 
      64           0 :     avpriv_set_pts_info(st, 64, 1, 100);
      65           0 :     return 0;
      66             : }
      67             : 
      68           0 : static int read_packet(AVFormatContext *s,
      69             :                           AVPacket *pkt)
      70             : {
      71           0 :     AVIOContext *pb = s->pb;
      72             :     PutBitContext pbo;
      73             :     uint16_t buf[8 * MAX_FRAME_SIZE + 2];
      74             :     int packet_size;
      75           0 :     uint16_t* src=buf;
      76             :     int i, j, ret;
      77           0 :     int64_t pos= avio_tell(pb);
      78             : 
      79           0 :     if(avio_feof(pb))
      80           0 :         return AVERROR_EOF;
      81             : 
      82           0 :     avio_rl16(pb); // sync word
      83           0 :     packet_size = avio_rl16(pb) / 8;
      84           0 :     if(packet_size > MAX_FRAME_SIZE)
      85           0 :         return AVERROR_INVALIDDATA;
      86             : 
      87           0 :     ret = avio_read(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
      88           0 :     if(ret<0)
      89           0 :         return ret;
      90           0 :     if(ret != 8 * packet_size * sizeof(uint16_t))
      91           0 :         return AVERROR(EIO);
      92             : 
      93           0 :     if (av_new_packet(pkt, packet_size) < 0)
      94           0 :         return AVERROR(ENOMEM);
      95             : 
      96           0 :     init_put_bits(&pbo, pkt->data, packet_size);
      97           0 :     for(j=0; j < packet_size; j++)
      98           0 :         for(i=0; i<8;i++)
      99           0 :             put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
     100             : 
     101           0 :     flush_put_bits(&pbo);
     102             : 
     103           0 :     pkt->duration=1;
     104           0 :     pkt->pos = pos;
     105           0 :     return 0;
     106             : }
     107             : 
     108             : AVInputFormat ff_bit_demuxer = {
     109             :     .name        = "bit",
     110             :     .long_name   = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
     111             :     .read_probe  = probe,
     112             :     .read_header = read_header,
     113             :     .read_packet = read_packet,
     114             :     .extensions  = "bit",
     115             : };
     116             : 
     117             : #if CONFIG_MUXERS
     118           0 : static int write_header(AVFormatContext *s)
     119             : {
     120           0 :     AVCodecParameters *par = s->streams[0]->codecpar;
     121             : 
     122           0 :     if ((par->codec_id != AV_CODEC_ID_G729) || par->channels != 1) {
     123           0 :         av_log(s, AV_LOG_ERROR,
     124             :                "only codec g729 with 1 channel is supported by this format\n");
     125           0 :         return AVERROR(EINVAL);
     126             :     }
     127             : 
     128           0 :     par->bits_per_coded_sample = 16;
     129           0 :     par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
     130             : 
     131           0 :     return 0;
     132             : }
     133             : 
     134           0 : static int write_packet(AVFormatContext *s, AVPacket *pkt)
     135             : {
     136           0 :     AVIOContext *pb = s->pb;
     137             :     GetBitContext gb;
     138             :     int i;
     139             : 
     140           0 :     if (pkt->size != 10)
     141           0 :         return AVERROR(EINVAL);
     142             : 
     143           0 :     avio_wl16(pb, SYNC_WORD);
     144           0 :     avio_wl16(pb, 8 * 10);
     145             : 
     146           0 :     init_get_bits(&gb, pkt->data, 8*10);
     147           0 :     for(i=0; i< 8 * 10; i++)
     148           0 :         avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
     149             : 
     150           0 :     return 0;
     151             : }
     152             : 
     153             : AVOutputFormat ff_bit_muxer = {
     154             :     .name         = "bit",
     155             :     .long_name    = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
     156             :     .mime_type    = "audio/bit",
     157             :     .extensions   = "bit",
     158             :     .audio_codec  = AV_CODEC_ID_G729,
     159             :     .video_codec  = AV_CODEC_ID_NONE,
     160             :     .write_header = write_header,
     161             :     .write_packet = write_packet,
     162             : };
     163             : #endif

Generated by: LCOV version 1.12