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.

146 lines
4.4KB

  1. /*
  2. * DNxHD/VC-3 parser
  3. * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * DNxHD/VC-3 parser
  24. */
  25. #include "parser.h"
  26. #include "dnxhddata.h"
  27. typedef struct {
  28. ParseContext pc;
  29. int cur_byte;
  30. int remaining;
  31. int w, h;
  32. } DNXHDParserContext;
  33. static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
  34. const uint8_t *buf, int buf_size)
  35. {
  36. ParseContext *pc = &dctx->pc;
  37. uint64_t state = pc->state64;
  38. int pic_found = pc->frame_start_found;
  39. int i = 0;
  40. if (!pic_found) {
  41. for (i = 0; i < buf_size; i++) {
  42. state = (state << 8) | buf[i];
  43. if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
  44. i++;
  45. pic_found = 1;
  46. dctx->cur_byte = 0;
  47. dctx->remaining = 0;
  48. break;
  49. }
  50. }
  51. }
  52. if (pic_found && !dctx->remaining) {
  53. if (!buf_size) /* EOF considered as end of frame */
  54. return 0;
  55. for (; i < buf_size; i++) {
  56. dctx->cur_byte++;
  57. state = (state << 8) | buf[i];
  58. if (dctx->cur_byte == 24) {
  59. dctx->h = (state >> 32) & 0xFFFF;
  60. } else if (dctx->cur_byte == 26) {
  61. dctx->w = (state >> 32) & 0xFFFF;
  62. } else if (dctx->cur_byte == 42) {
  63. int cid = (state >> 32) & 0xFFFFFFFF;
  64. int remaining;
  65. if (cid <= 0)
  66. continue;
  67. remaining = avpriv_dnxhd_get_frame_size(cid);
  68. if (remaining <= 0) {
  69. remaining = avpriv_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
  70. if (remaining <= 0)
  71. continue;
  72. }
  73. dctx->remaining = remaining;
  74. if (buf_size - i + 47 >= dctx->remaining) {
  75. pc->frame_start_found = 0;
  76. pc->state64 = -1;
  77. dctx->cur_byte = 0;
  78. dctx->remaining = 0;
  79. return remaining;
  80. } else {
  81. dctx->remaining -= buf_size;
  82. // Update variables for correctness, they are currently not used beyond here
  83. state = -1;
  84. dctx->cur_byte += buf_size - i;
  85. break;
  86. }
  87. }
  88. }
  89. } else if (pic_found) {
  90. if (dctx->remaining > buf_size) {
  91. dctx->remaining -= buf_size;
  92. } else {
  93. int remaining = dctx->remaining;
  94. pc->frame_start_found = 0;
  95. pc->state64 = -1;
  96. dctx->cur_byte = 0;
  97. dctx->remaining = 0;
  98. return remaining;
  99. }
  100. }
  101. pc->frame_start_found = pic_found;
  102. pc->state64 = state;
  103. return END_NOT_FOUND;
  104. }
  105. static int dnxhd_parse(AVCodecParserContext *s,
  106. AVCodecContext *avctx,
  107. const uint8_t **poutbuf, int *poutbuf_size,
  108. const uint8_t *buf, int buf_size)
  109. {
  110. DNXHDParserContext *dctx = s->priv_data;
  111. ParseContext *pc = &dctx->pc;
  112. int next;
  113. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  114. next = buf_size;
  115. } else {
  116. next = dnxhd_find_frame_end(dctx, buf, buf_size);
  117. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  118. *poutbuf = NULL;
  119. *poutbuf_size = 0;
  120. return buf_size;
  121. }
  122. }
  123. *poutbuf = buf;
  124. *poutbuf_size = buf_size;
  125. return next;
  126. }
  127. AVCodecParser ff_dnxhd_parser = {
  128. .codec_ids = { AV_CODEC_ID_DNXHD },
  129. .priv_data_size = sizeof(DNXHDParserContext),
  130. .parser_parse = dnxhd_parse,
  131. .parser_close = ff_parse_close,
  132. };