dca_parser.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // We have to check that we really read a full frame here, and that it isn't a pure HD frame, because their size is not constant.
  72. if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
  73. pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
  74. }
  75. pc->frame_start_found = 0;
  76. pc->state = -1;
  77. pc1->size = 0;
  78. return i - 3;
  79. }
  80. }
  81. }
  82. pc->frame_start_found = start_found;
  83. pc->state = state;
  84. return END_NOT_FOUND;
  85. }
  86. static av_cold int dca_parse_init(AVCodecParserContext * s)
  87. {
  88. DCAParseContext *pc1 = s->priv_data;
  89. pc1->lastmarker = 0;
  90. return 0;
  91. }
  92. static int dca_parse(AVCodecParserContext * s,
  93. AVCodecContext * avctx,
  94. const uint8_t ** poutbuf, int *poutbuf_size,
  95. const uint8_t * buf, int buf_size)
  96. {
  97. DCAParseContext *pc1 = s->priv_data;
  98. ParseContext *pc = &pc1->pc;
  99. int next;
  100. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  101. next = buf_size;
  102. } else {
  103. next = dca_find_frame_end(pc1, buf, buf_size);
  104. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  105. *poutbuf = NULL;
  106. *poutbuf_size = 0;
  107. return buf_size;
  108. }
  109. }
  110. *poutbuf = buf;
  111. *poutbuf_size = buf_size;
  112. return next;
  113. }
  114. AVCodecParser ff_dca_parser = {
  115. .codec_ids = { CODEC_ID_DTS },
  116. .priv_data_size = sizeof(DCAParseContext),
  117. .parser_init = dca_parse_init,
  118. .parser_parse = dca_parse,
  119. .parser_close = ff_parse_close,
  120. };