bonk.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Bonk demuxer
  3. * Copyright (c) 2016 Paul B Mahol
  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 "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "demux.h"
  24. #include "internal.h"
  25. #include "rawdec.h"
  26. static int bonk_probe(const AVProbeData *p)
  27. {
  28. for (int i = 0; i < p->buf_size - 22; i++) {
  29. if (!p->buf[i] && AV_RL32(p->buf + i + 1) == MKTAG('B','O','N','K')) {
  30. if (p->buf[i + 5])
  31. return 0;
  32. if (AV_RL32(p->buf + i + 6) == 0)
  33. return 0;
  34. if (AV_RL32(p->buf + i + 10) == 0)
  35. return 0;
  36. if (p->buf[i + 14] == 0)
  37. return 0;
  38. if (AV_RL16(p->buf + i + 17) == 0 ||
  39. AV_RL16(p->buf + i + 17) > 2048)
  40. return 0;
  41. if (p->buf[i + 19] == 0)
  42. return 0;
  43. if (AV_RL16(p->buf + i + 20) == 0)
  44. return 0;
  45. return AVPROBE_SCORE_MAX;
  46. } else if (!p->buf[i]) {
  47. break;
  48. }
  49. }
  50. return 0;
  51. }
  52. static int bonk_read_header(AVFormatContext *s)
  53. {
  54. AVStream *st;
  55. int ret;
  56. for (int i = 0; !avio_feof(s->pb); i++) {
  57. int b = avio_r8(s->pb);
  58. if (!b && avio_rl32(s->pb) == MKTAG('B','O','N','K'))
  59. break;
  60. else if (!b)
  61. return AVERROR_INVALIDDATA;
  62. }
  63. st = avformat_new_stream(s, NULL);
  64. if (!st)
  65. return AVERROR(ENOMEM);
  66. if ((ret = ff_get_extradata(s, st->codecpar, s->pb, 17)) < 0)
  67. return ret;
  68. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  69. st->codecpar->codec_id = AV_CODEC_ID_BONK;
  70. st->codecpar->sample_rate = AV_RL32(st->codecpar->extradata + 5);
  71. st->codecpar->ch_layout.nb_channels = st->codecpar->extradata[9];
  72. if (st->codecpar->ch_layout.nb_channels == 0)
  73. return AVERROR_INVALIDDATA;
  74. st->duration = AV_RL32(st->codecpar->extradata + 1) / st->codecpar->ch_layout.nb_channels;
  75. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  76. ffformatcontext(s)->data_offset = avio_tell(s->pb);
  77. return 0;
  78. }
  79. const AVInputFormat ff_bonk_demuxer = {
  80. .name = "bonk",
  81. .long_name = NULL_IF_CONFIG_SMALL("raw Bonk"),
  82. .read_probe = bonk_probe,
  83. .read_header = bonk_read_header,
  84. .read_packet = ff_raw_read_partial_packet,
  85. .extensions = "bonk",
  86. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS,
  87. .raw_codec_id = AV_CODEC_ID_BONK,
  88. .priv_data_size = sizeof(FFRawDemuxerContext),
  89. .priv_class = &ff_raw_demuxer_class,
  90. };