dca_parser.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; 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)
  37. /**
  38. * Find 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. pc1->size++;
  68. state = (state << 8) | buf[i];
  69. if (state == DCA_HD_MARKER && !pc1->hd_pos)
  70. pc1->hd_pos = pc1->size;
  71. if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
  72. if(pc1->framesize > pc1->size)
  73. continue;
  74. if(!pc1->framesize){
  75. pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
  76. }
  77. pc->frame_start_found = 0;
  78. pc->state = -1;
  79. pc1->size = 0;
  80. return i - 3;
  81. }
  82. }
  83. }
  84. pc->frame_start_found = start_found;
  85. pc->state = state;
  86. return END_NOT_FOUND;
  87. }
  88. static av_cold int dca_parse_init(AVCodecParserContext * s)
  89. {
  90. DCAParseContext *pc1 = s->priv_data;
  91. pc1->lastmarker = 0;
  92. return 0;
  93. }
  94. static int dca_parse(AVCodecParserContext * s,
  95. AVCodecContext * avctx,
  96. const uint8_t ** poutbuf, int *poutbuf_size,
  97. const uint8_t * buf, int buf_size)
  98. {
  99. DCAParseContext *pc1 = s->priv_data;
  100. ParseContext *pc = &pc1->pc;
  101. int next;
  102. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  103. next = buf_size;
  104. } else {
  105. next = dca_find_frame_end(pc1, buf, buf_size);
  106. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  107. *poutbuf = NULL;
  108. *poutbuf_size = 0;
  109. return buf_size;
  110. }
  111. }
  112. *poutbuf = buf;
  113. *poutbuf_size = buf_size;
  114. return next;
  115. }
  116. AVCodecParser ff_dca_parser = {
  117. .codec_ids = { CODEC_ID_DTS },
  118. .priv_data_size = sizeof(DCAParseContext),
  119. .parser_init = dca_parse_init,
  120. .parser_parse = dca_parse,
  121. .parser_close = ff_parse_close,
  122. };