You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.0KB

  1. /*
  2. * G.729 raw format demuxer
  3. * Copyright (c) 2011 Vladimir Voroshilov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/log.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. typedef struct G729DemuxerContext {
  26. AVClass *class;
  27. int bit_rate;
  28. } G729DemuxerContext;
  29. static int g729_read_header(AVFormatContext *s)
  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->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  37. st->codecpar->codec_id = AV_CODEC_ID_G729;
  38. st->codecpar->sample_rate = 8000;
  39. st->codecpar->channels = 1;
  40. if (s1 && s1->bit_rate)
  41. s->bit_rate = s1->bit_rate;
  42. switch(s->bit_rate) {
  43. case 6400:
  44. st->codecpar->block_align = 8;
  45. break;
  46. case 8000:
  47. st->codecpar->block_align = 10;
  48. break;
  49. default:
  50. av_log(s, AV_LOG_ERROR, "Invalid bit_rate value %d. "
  51. "Only 6400 and 8000 b/s are supported.", s->bit_rate);
  52. return AVERROR(EINVAL);
  53. }
  54. avpriv_set_pts_info(st, st->codecpar->block_align << 3, 1,
  55. st->codecpar->sample_rate);
  56. return 0;
  57. }
  58. static int g729_read_packet(AVFormatContext *s, AVPacket *pkt)
  59. {
  60. AVStream *st = s->streams[0];
  61. int ret = av_get_packet(s->pb, pkt, st->codecpar->block_align);
  62. if (ret < 0)
  63. return ret;
  64. pkt->stream_index = 0;
  65. pkt->dts = pkt->pts = pkt->pos / st->codecpar->block_align;
  66. return 0;
  67. }
  68. #define OFFSET(x) offsetof(G729DemuxerContext, x)
  69. static const AVOption g729_options[] = {
  70. { "bit_rate", "", OFFSET(bit_rate), AV_OPT_TYPE_INT, { .i64 = 8000 }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  71. { NULL },
  72. };
  73. static const AVClass g729_demuxer_class = {
  74. .class_name = "g729 demuxer",
  75. .item_name = av_default_item_name,
  76. .option = g729_options,
  77. .version = LIBAVUTIL_VERSION_INT,
  78. };
  79. AVInputFormat ff_g729_demuxer = {
  80. .name = "g729",
  81. .long_name = NULL_IF_CONFIG_SMALL("G.729 raw format demuxer"),
  82. .priv_data_size = sizeof(G729DemuxerContext),
  83. .read_header = g729_read_header,
  84. .read_packet = g729_read_packet,
  85. .flags = AVFMT_GENERIC_INDEX,
  86. .extensions = "g729",
  87. .priv_class = &g729_demuxer_class,
  88. };