g729dec.c 3.0 KB

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