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.

134 lines
3.9KB

  1. /*
  2. * iLBC storage file format
  3. * Copyright (c) 2012 Martin Storsjo
  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 "rawenc.h"
  24. static const char mode20_header[] = "#!iLBC20\n";
  25. static const char mode30_header[] = "#!iLBC30\n";
  26. static int ilbc_write_header(AVFormatContext *s)
  27. {
  28. AVIOContext *pb = s->pb;
  29. AVCodecParameters *par;
  30. if (s->nb_streams != 1) {
  31. av_log(s, AV_LOG_ERROR, "Unsupported number of streams\n");
  32. return AVERROR(EINVAL);
  33. }
  34. par = s->streams[0]->codecpar;
  35. if (par->codec_id != AV_CODEC_ID_ILBC) {
  36. av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
  37. return AVERROR(EINVAL);
  38. }
  39. if (par->block_align == 50) {
  40. avio_write(pb, mode30_header, sizeof(mode30_header) - 1);
  41. } else if (par->block_align == 38) {
  42. avio_write(pb, mode20_header, sizeof(mode20_header) - 1);
  43. } else {
  44. av_log(s, AV_LOG_ERROR, "Unsupported mode\n");
  45. return AVERROR(EINVAL);
  46. }
  47. return 0;
  48. }
  49. static int ilbc_probe(const AVProbeData *p)
  50. {
  51. // Only check for "#!iLBC" which matches both formats
  52. if (!memcmp(p->buf, mode20_header, 6))
  53. return AVPROBE_SCORE_MAX;
  54. else
  55. return 0;
  56. }
  57. static int ilbc_read_header(AVFormatContext *s)
  58. {
  59. AVIOContext *pb = s->pb;
  60. AVStream *st;
  61. uint8_t header[9];
  62. avio_read(pb, header, 9);
  63. st = avformat_new_stream(s, NULL);
  64. if (!st)
  65. return AVERROR(ENOMEM);
  66. st->codecpar->codec_id = AV_CODEC_ID_ILBC;
  67. st->codecpar->sample_rate = 8000;
  68. st->codecpar->channels = 1;
  69. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  70. st->start_time = 0;
  71. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  72. if (!memcmp(header, mode20_header, sizeof(mode20_header) - 1)) {
  73. st->codecpar->block_align = 38;
  74. st->codecpar->bit_rate = 15200;
  75. } else if (!memcmp(header, mode30_header, sizeof(mode30_header) - 1)) {
  76. st->codecpar->block_align = 50;
  77. st->codecpar->bit_rate = 13333;
  78. } else {
  79. av_log(s, AV_LOG_ERROR, "Unrecognized iLBC file header\n");
  80. return AVERROR_INVALIDDATA;
  81. }
  82. return 0;
  83. }
  84. static int ilbc_read_packet(AVFormatContext *s,
  85. AVPacket *pkt)
  86. {
  87. AVCodecParameters *par = s->streams[0]->codecpar;
  88. int ret;
  89. if ((ret = av_new_packet(pkt, par->block_align)) < 0)
  90. return ret;
  91. pkt->stream_index = 0;
  92. pkt->pos = avio_tell(s->pb);
  93. pkt->duration = par->block_align == 38 ? 160 : 240;
  94. if ((ret = avio_read(s->pb, pkt->data, par->block_align)) != par->block_align) {
  95. return ret < 0 ? ret : AVERROR(EIO);
  96. }
  97. return 0;
  98. }
  99. AVInputFormat ff_ilbc_demuxer = {
  100. .name = "ilbc",
  101. .long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
  102. .read_probe = ilbc_probe,
  103. .read_header = ilbc_read_header,
  104. .read_packet = ilbc_read_packet,
  105. .flags = AVFMT_GENERIC_INDEX,
  106. };
  107. AVOutputFormat ff_ilbc_muxer = {
  108. .name = "ilbc",
  109. .long_name = NULL_IF_CONFIG_SMALL("iLBC storage"),
  110. .mime_type = "audio/iLBC",
  111. .extensions = "lbc",
  112. .audio_codec = AV_CODEC_ID_ILBC,
  113. .write_header = ilbc_write_header,
  114. .write_packet = ff_raw_write_packet,
  115. .flags = AVFMT_NOTIMESTAMPS,
  116. };