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.

105 lines
3.3KB

  1. /*
  2. * Copyright (c) 2011 Justin Ruggles
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * ADX audio parser
  23. *
  24. * Reads header to extradata and splits packets into individual blocks.
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "parser.h"
  28. #include "adx.h"
  29. typedef struct ADXParseContext {
  30. ParseContext pc;
  31. int header_size;
  32. int block_size;
  33. int buf_pos;
  34. } ADXParseContext;
  35. #define MIN_HEADER_SIZE 24
  36. static int adx_parse(AVCodecParserContext *s1,
  37. AVCodecContext *avctx,
  38. const uint8_t **poutbuf, int *poutbuf_size,
  39. const uint8_t *buf, int buf_size)
  40. {
  41. ADXParseContext *s = s1->priv_data;
  42. ParseContext *pc = &s->pc;
  43. int next = END_NOT_FOUND;
  44. if (!avctx->extradata_size) {
  45. int ret;
  46. ff_combine_frame(pc, END_NOT_FOUND, &buf, &buf_size);
  47. if (!s->header_size && pc->index >= MIN_HEADER_SIZE) {
  48. if (ret = avpriv_adx_decode_header(avctx, pc->buffer, pc->index,
  49. &s->header_size, NULL))
  50. return AVERROR_INVALIDDATA;
  51. s->block_size = BLOCK_SIZE * avctx->channels;
  52. }
  53. if (s->header_size && s->header_size <= pc->index) {
  54. avctx->extradata = av_mallocz(s->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  55. if (!avctx->extradata)
  56. return AVERROR(ENOMEM);
  57. avctx->extradata_size = s->header_size;
  58. memcpy(avctx->extradata, pc->buffer, s->header_size);
  59. memmove(pc->buffer, pc->buffer + s->header_size, s->header_size);
  60. pc->index -= s->header_size;
  61. }
  62. *poutbuf = NULL;
  63. *poutbuf_size = 0;
  64. return buf_size;
  65. }
  66. if (pc->index - s->buf_pos >= s->block_size) {
  67. *poutbuf = &pc->buffer[s->buf_pos];
  68. *poutbuf_size = s->block_size;
  69. s->buf_pos += s->block_size;
  70. return 0;
  71. }
  72. if (pc->index && s->buf_pos) {
  73. memmove(pc->buffer, &pc->buffer[s->buf_pos], pc->index - s->buf_pos);
  74. pc->index -= s->buf_pos;
  75. s->buf_pos = 0;
  76. }
  77. if (buf_size + pc->index >= s->block_size)
  78. next = s->block_size - pc->index;
  79. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0 || !buf_size) {
  80. *poutbuf = NULL;
  81. *poutbuf_size = 0;
  82. return buf_size;
  83. }
  84. *poutbuf = buf;
  85. *poutbuf_size = buf_size;
  86. return next;
  87. }
  88. AVCodecParser ff_adx_parser = {
  89. .codec_ids = { CODEC_ID_ADPCM_ADX },
  90. .priv_data_size = sizeof(ADXParseContext),
  91. .parser_parse = adx_parse,
  92. .parser_close = ff_parse_close,
  93. };