• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * G.726 raw demuxer
3  * Copyright 2017 Carl Eugen Hoyos
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 "config_components.h"
23 
24 #include "avformat.h"
25 #include "internal.h"
26 #include "libavutil/opt.h"
27 
28 typedef struct G726Context {
29     AVClass *class;
30     int code_size;
31     int sample_rate;
32 } G726Context;
33 
g726_read_header(AVFormatContext * s)34 static int g726_read_header(AVFormatContext *s)
35 {
36     G726Context *c = s->priv_data;
37     AVStream *st = avformat_new_stream(s, NULL);
38     if (!st)
39         return AVERROR(ENOMEM);
40 
41     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
42     st->codecpar->codec_id   = s->iformat->raw_codec_id;
43 
44     st->codecpar->sample_rate           = c->sample_rate;
45     st->codecpar->bits_per_coded_sample = c->code_size;
46     st->codecpar->bit_rate              = ((int[]){ 16000, 24000, 32000, 40000 })[c->code_size - 2];
47     st->codecpar->ch_layout.nb_channels = 1;
48 
49     return 0;
50 }
51 
g726_read_packet(AVFormatContext * s,AVPacket * pkt)52 static int g726_read_packet(AVFormatContext *s, AVPacket *pkt)
53 {
54     int res;
55     res = av_get_packet(s->pb, pkt, 1020); // a size similar to RAW_PACKET_SIZE divisible by all code_size values
56     if (res < 0)
57         return res;
58     return 0;
59 }
60 
61 #define OFFSET(x) offsetof(G726Context, x)
62 static const AVOption options[] = {
63     { "code_size", "Bits per G.726 code",
64         OFFSET(code_size),   AV_OPT_TYPE_INT, {.i64 =    4}, 2,       5, AV_OPT_FLAG_DECODING_PARAM },
65     { "sample_rate", "",
66         OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 8000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
67     { NULL },
68 };
69 
70 static const AVClass g726_demuxer_class = {
71     .class_name     = "G.726 demuxer",
72     .item_name      = av_default_item_name,
73     .option         = options,
74     .version        = LIBAVUTIL_VERSION_INT,
75 };
76 
77 #if CONFIG_G726_DEMUXER
78 const AVInputFormat ff_g726_demuxer = {
79     .name           = "g726",
80     .long_name      = NULL_IF_CONFIG_SMALL("raw big-endian G.726 (\"left aligned\")"),
81     .read_header    = g726_read_header,
82     .read_packet    = g726_read_packet,
83     .priv_data_size = sizeof(G726Context),
84     .priv_class     = &g726_demuxer_class,
85     .raw_codec_id   = AV_CODEC_ID_ADPCM_G726,
86 };
87 #endif
88 
89 #if CONFIG_G726LE_DEMUXER
90 const AVInputFormat ff_g726le_demuxer = {
91     .name           = "g726le",
92     .long_name      = NULL_IF_CONFIG_SMALL("raw little-endian G.726 (\"right aligned\")"),
93     .read_header    = g726_read_header,
94     .read_packet    = g726_read_packet,
95     .priv_data_size = sizeof(G726Context),
96     .priv_class     = &g726_demuxer_class,
97     .raw_codec_id   = AV_CODEC_ID_ADPCM_G726LE,
98 };
99 #endif
100 
101