ilbc.c 4.1 KB

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