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
4.0KB

  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. #include "parser.h"
  25. #include "dca.h"
  26. typedef struct DCAParseContext {
  27. ParseContext pc;
  28. uint32_t lastmarker;
  29. int size;
  30. int framesize;
  31. int hd_pos;
  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 || state == DCA_HD_MARKER)
  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 || pc1->lastmarker == DCA_HD_MARKER) {
  55. start_found = 1;
  56. pc1->lastmarker = state;
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. if (start_found) {
  63. for (; i < buf_size; i++) {
  64. pc1->size++;
  65. state = (state << 8) | buf[i];
  66. if (state == DCA_HD_MARKER && !pc1->hd_pos)
  67. pc1->hd_pos = pc1->size;
  68. if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
  69. if(pc1->framesize > pc1->size)
  70. continue;
  71. if(!pc1->framesize){
  72. pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
  73. }
  74. pc->frame_start_found = 0;
  75. pc->state = -1;
  76. pc1->size = 0;
  77. return i - 3;
  78. }
  79. }
  80. }
  81. pc->frame_start_found = start_found;
  82. pc->state = state;
  83. return END_NOT_FOUND;
  84. }
  85. static av_cold int dca_parse_init(AVCodecParserContext * s)
  86. {
  87. DCAParseContext *pc1 = s->priv_data;
  88. pc1->lastmarker = 0;
  89. return 0;
  90. }
  91. static int dca_parse(AVCodecParserContext * s,
  92. AVCodecContext * avctx,
  93. const uint8_t ** poutbuf, int *poutbuf_size,
  94. const uint8_t * buf, int buf_size)
  95. {
  96. DCAParseContext *pc1 = s->priv_data;
  97. ParseContext *pc = &pc1->pc;
  98. int next;
  99. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  100. next = buf_size;
  101. } else {
  102. next = dca_find_frame_end(pc1, buf, buf_size);
  103. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  104. *poutbuf = NULL;
  105. *poutbuf_size = 0;
  106. return buf_size;
  107. }
  108. }
  109. *poutbuf = buf;
  110. *poutbuf_size = buf_size;
  111. return next;
  112. }
  113. AVCodecParser ff_dca_parser = {
  114. {CODEC_ID_DTS},
  115. sizeof(DCAParseContext),
  116. dca_parse_init,
  117. dca_parse,
  118. ff_parse_close,
  119. };