g729dec.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * G.729 raw format demuxer
  3. * Copyright (c) 2011 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 "libavutil/log.h"
  23. #include "libavutil/opt.h"
  24. typedef struct G729DemuxerContext {
  25. AVClass *class;
  26. int bit_rate;
  27. } G729DemuxerContext;
  28. static int g729_read_header(AVFormatContext *s, AVFormatParameters *ap)
  29. {
  30. AVStream* st;
  31. G729DemuxerContext *s1 = s->priv_data;
  32. st = avformat_new_stream(s, NULL);
  33. if (!st)
  34. return AVERROR(ENOMEM);
  35. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  36. st->codec->codec_id = CODEC_ID_G729;
  37. st->codec->sample_rate = 8000;
  38. st->codec->channels = 1;
  39. if (s1 && s1->bit_rate) {
  40. s->bit_rate = s1->bit_rate;
  41. }
  42. if (s->bit_rate == 0) {
  43. av_log(s, AV_LOG_DEBUG, "No bitrate specified. Assuming 8000 b/s\n");
  44. s->bit_rate = 8000;
  45. }
  46. if (s->bit_rate == 6400) {
  47. st->codec->block_align = 8;
  48. } else if (s->bit_rate == 8000) {
  49. st->codec->block_align = 10;
  50. } else {
  51. av_log(s, AV_LOG_ERROR, "Only 8000 b/s and 6400 b/s bitrates are supported. Provided: %d b/s\n", s->bit_rate);
  52. return AVERROR_INVALIDDATA;
  53. }
  54. av_set_pts_info(st, st->codec->block_align << 3, 1, st->codec->sample_rate);
  55. return 0;
  56. }
  57. static int g729_read_packet(AVFormatContext *s, AVPacket *pkt)
  58. {
  59. int ret;
  60. ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
  61. pkt->stream_index = 0;
  62. if (ret < 0)
  63. return ret;
  64. pkt->dts = pkt->pts = pkt->pos / s->streams[0]->codec->block_align;
  65. return ret;
  66. }
  67. static const AVOption g729_options[] = {
  68. { "bit_rate", "", offsetof(G729DemuxerContext, bit_rate), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  69. { NULL },
  70. };
  71. static const AVClass g729_demuxer_class = {
  72. .class_name = "g729 demuxer",
  73. .item_name = av_default_item_name,
  74. .option = g729_options,
  75. .version = LIBAVUTIL_VERSION_INT,
  76. };
  77. AVInputFormat ff_g729_demuxer = {
  78. .name = "g729",
  79. .long_name = NULL_IF_CONFIG_SMALL("G.729 raw format demuxer"),
  80. .priv_data_size = sizeof(G729DemuxerContext),
  81. .read_header = g729_read_header,
  82. .read_packet = g729_read_packet,
  83. .flags = AVFMT_GENERIC_INDEX,
  84. .extensions = "g729",
  85. .priv_class = &g729_demuxer_class,
  86. };