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.

87 lines
2.4KB

  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "bsf.h"
  21. #include "bsf_internal.h"
  22. #include "get_bits.h"
  23. #include "ac3_parser_internal.h"
  24. static int eac3_core_filter(AVBSFContext *ctx, AVPacket *pkt)
  25. {
  26. AC3HeaderInfo hdr;
  27. GetBitContext gbc;
  28. int ret;
  29. ret = ff_bsf_get_packet_ref(ctx, pkt);
  30. if (ret < 0)
  31. return ret;
  32. ret = init_get_bits8(&gbc, pkt->data, pkt->size);
  33. if (ret < 0)
  34. goto fail;
  35. ret = ff_ac3_parse_header(&gbc, &hdr);
  36. if (ret < 0) {
  37. ret = AVERROR_INVALIDDATA;
  38. goto fail;
  39. }
  40. if (hdr.frame_type == EAC3_FRAME_TYPE_INDEPENDENT ||
  41. hdr.frame_type == EAC3_FRAME_TYPE_AC3_CONVERT) {
  42. pkt->size = FFMIN(hdr.frame_size, pkt->size);
  43. } else if (hdr.frame_type == EAC3_FRAME_TYPE_DEPENDENT && pkt->size > hdr.frame_size) {
  44. AC3HeaderInfo hdr2;
  45. ret = init_get_bits8(&gbc, pkt->data + hdr.frame_size, pkt->size - hdr.frame_size);
  46. if (ret < 0)
  47. goto fail;
  48. ret = ff_ac3_parse_header(&gbc, &hdr2);
  49. if (ret < 0) {
  50. ret = AVERROR_INVALIDDATA;
  51. goto fail;
  52. }
  53. if (hdr2.frame_type == EAC3_FRAME_TYPE_INDEPENDENT ||
  54. hdr2.frame_type == EAC3_FRAME_TYPE_AC3_CONVERT) {
  55. pkt->size -= hdr.frame_size;
  56. pkt->data += hdr.frame_size;
  57. } else {
  58. pkt->size = 0;
  59. }
  60. } else {
  61. pkt->size = 0;
  62. }
  63. return 0;
  64. fail:
  65. av_packet_unref(pkt);
  66. return ret;
  67. }
  68. static const enum AVCodecID codec_ids[] = {
  69. AV_CODEC_ID_EAC3, AV_CODEC_ID_NONE,
  70. };
  71. const AVBitStreamFilter ff_eac3_core_bsf = {
  72. .name = "eac3_core",
  73. .filter = eac3_core_filter,
  74. .codec_ids = codec_ids,
  75. };