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.

141 lines
4.1KB

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