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.

132 lines
3.8KB

  1. /*
  2. * DCA parser
  3. * Copyright (C) 2004 Gildas Bazin
  4. * Copyright (C) 2004 Benjamin Zores
  5. * Copyright (C) 2006 Benjamin Larsson
  6. * Copyright (C) 2007 Konstantin Shishkov
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file dca_parser.c
  26. */
  27. #include "parser.h"
  28. #include "dca.h"
  29. typedef struct DCAParseContext {
  30. ParseContext pc;
  31. uint32_t lastmarker;
  32. int size;
  33. int framesize;
  34. } DCAParseContext;
  35. #define IS_MARKER(state, i, buf, buf_size) \
  36. ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
  37. || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
  38. || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
  39. /**
  40. * finds the end of the current frame in the bitstream.
  41. * @return the position of the first byte of the next frame, or -1
  42. */
  43. static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
  44. int buf_size)
  45. {
  46. int start_found, i;
  47. uint32_t state;
  48. ParseContext *pc = &pc1->pc;
  49. start_found = pc->frame_start_found;
  50. state = pc->state;
  51. i = 0;
  52. if (!start_found) {
  53. for (i = 0; i < buf_size; i++) {
  54. state = (state << 8) | buf[i];
  55. if (IS_MARKER(state, i, buf, buf_size)) {
  56. if (pc1->lastmarker && state == pc1->lastmarker) {
  57. start_found = 1;
  58. break;
  59. } else if (!pc1->lastmarker) {
  60. start_found = 1;
  61. pc1->lastmarker = state;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. if (start_found) {
  68. for (; i < buf_size; i++) {
  69. pc1->size++;
  70. state = (state << 8) | buf[i];
  71. if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size) && (!pc1->framesize || pc1->framesize == pc1->size)) {
  72. pc->frame_start_found = 0;
  73. pc->state = -1;
  74. pc1->framesize = pc1->size;
  75. pc1->size = 0;
  76. return i - 3;
  77. }
  78. }
  79. }
  80. pc->frame_start_found = start_found;
  81. pc->state = state;
  82. return END_NOT_FOUND;
  83. }
  84. static av_cold int dca_parse_init(AVCodecParserContext * s)
  85. {
  86. DCAParseContext *pc1 = s->priv_data;
  87. pc1->lastmarker = 0;
  88. return 0;
  89. }
  90. static int dca_parse(AVCodecParserContext * s,
  91. AVCodecContext * avctx,
  92. const uint8_t ** poutbuf, int *poutbuf_size,
  93. const uint8_t * buf, int buf_size)
  94. {
  95. DCAParseContext *pc1 = s->priv_data;
  96. ParseContext *pc = &pc1->pc;
  97. int next;
  98. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  99. next = buf_size;
  100. } else {
  101. next = dca_find_frame_end(pc1, buf, buf_size);
  102. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  103. *poutbuf = NULL;
  104. *poutbuf_size = 0;
  105. return buf_size;
  106. }
  107. }
  108. *poutbuf = buf;
  109. *poutbuf_size = buf_size;
  110. return next;
  111. }
  112. AVCodecParser dca_parser = {
  113. {CODEC_ID_DTS},
  114. sizeof(DCAParseContext),
  115. dca_parse_init,
  116. dca_parse,
  117. ff_parse_close,
  118. };