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.

127 lines
3.6KB

  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. } DCAParseContext;
  33. #define IS_MARKER(state, i, buf, buf_size) \
  34. ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
  35. || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
  36. || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
  37. /**
  38. * finds the end of the current frame in the bitstream.
  39. * @return the position of the first byte of the next frame, or -1
  40. */
  41. static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
  42. int buf_size)
  43. {
  44. int start_found, i;
  45. uint32_t state;
  46. ParseContext *pc = &pc1->pc;
  47. start_found = pc->frame_start_found;
  48. state = pc->state;
  49. i = 0;
  50. if (!start_found) {
  51. for (i = 0; i < buf_size; i++) {
  52. state = (state << 8) | buf[i];
  53. if (IS_MARKER(state, i, buf, buf_size)) {
  54. if (pc1->lastmarker && state == pc1->lastmarker) {
  55. start_found = 1;
  56. break;
  57. } else if (!pc1->lastmarker) {
  58. start_found = 1;
  59. pc1->lastmarker = state;
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. if (start_found) {
  66. for (; i < buf_size; i++) {
  67. state = (state << 8) | buf[i];
  68. if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
  69. pc->frame_start_found = 0;
  70. pc->state = -1;
  71. return i - 3;
  72. }
  73. }
  74. }
  75. pc->frame_start_found = start_found;
  76. pc->state = state;
  77. return END_NOT_FOUND;
  78. }
  79. static int dca_parse_init(AVCodecParserContext * s)
  80. {
  81. DCAParseContext *pc1 = s->priv_data;
  82. pc1->lastmarker = 0;
  83. return 0;
  84. }
  85. static int dca_parse(AVCodecParserContext * s,
  86. AVCodecContext * avctx,
  87. const uint8_t ** poutbuf, int *poutbuf_size,
  88. const uint8_t * buf, int buf_size)
  89. {
  90. DCAParseContext *pc1 = s->priv_data;
  91. ParseContext *pc = &pc1->pc;
  92. int next;
  93. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  94. next = buf_size;
  95. } else {
  96. next = dca_find_frame_end(pc1, buf, buf_size);
  97. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  98. *poutbuf = NULL;
  99. *poutbuf_size = 0;
  100. return buf_size;
  101. }
  102. }
  103. *poutbuf = buf;
  104. *poutbuf_size = buf_size;
  105. return next;
  106. }
  107. AVCodecParser dca_parser = {
  108. {CODEC_ID_DTS},
  109. sizeof(DCAParseContext),
  110. dca_parse_init,
  111. dca_parse,
  112. ff_parse_close,
  113. };