apac.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * APAC demuxer
  3. * Copyright (c) 2022 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 apac_probe(const AVProbeData *p)
  27. {
  28. if (AV_RB32(p->buf) == MKBETAG('A','P','A','C') &&
  29. AV_RB32(p->buf + 8) == MKBETAG('P','R','O','F') &&
  30. AV_RB32(p->buf + 12) == MKBETAG('N','A','D',' '))
  31. return AVPROBE_SCORE_MAX;
  32. return 0;
  33. }
  34. static int apac_read_header(AVFormatContext *s)
  35. {
  36. AVIOContext *pb = s->pb;
  37. uint32_t chunk_size;
  38. AVStream *st;
  39. int64_t pos;
  40. avio_skip(pb, 16);
  41. chunk_size = avio_rl32(pb);
  42. avio_skip(pb, chunk_size);
  43. if (avio_rb32(pb) != MKBETAG('P','F','M','T'))
  44. return AVERROR_INVALIDDATA;
  45. chunk_size = avio_rl32(pb);
  46. pos = avio_tell(pb);
  47. avio_skip(pb, 2);
  48. st = avformat_new_stream(s, NULL);
  49. if (!st)
  50. return AVERROR(ENOMEM);
  51. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  52. st->codecpar->codec_id = AV_CODEC_ID_APAC;
  53. st->codecpar->ch_layout.nb_channels = avio_rl16(pb);
  54. st->codecpar->sample_rate = avio_rl32(pb);
  55. if (st->codecpar->ch_layout.nb_channels <= 0 ||
  56. st->codecpar->ch_layout.nb_channels > 2 ||
  57. st->codecpar->sample_rate <= 0)
  58. return AVERROR_INVALIDDATA;
  59. avio_skip(pb, 2);
  60. st->codecpar->bits_per_coded_sample = avio_rl16(pb);
  61. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  62. avio_skip(pb, (chunk_size + pos) - avio_tell(pb) + (chunk_size & 1));
  63. if (avio_rb32(pb) != MKBETAG('P','A','D',' '))
  64. return AVERROR_INVALIDDATA;
  65. avio_skip(pb, 4);
  66. return 0;
  67. }
  68. const AVInputFormat ff_apac_demuxer = {
  69. .name = "apac",
  70. .long_name = NULL_IF_CONFIG_SMALL("raw APAC"),
  71. .read_probe = apac_probe,
  72. .read_header = apac_read_header,
  73. .read_packet = ff_raw_read_partial_packet,
  74. .extensions = "apc",
  75. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS,
  76. .raw_codec_id = AV_CODEC_ID_APAC,
  77. .priv_data_size = sizeof(FFRawDemuxerContext),
  78. .priv_class = &ff_raw_demuxer_class,
  79. };