dca_parser.c 4.0 KB

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