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.

147 lines
4.5KB

  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. remaining += i - 47;
  74. dctx->remaining = remaining;
  75. if (buf_size >= dctx->remaining) {
  76. pc->frame_start_found = 0;
  77. pc->state64 = -1;
  78. dctx->cur_byte = 0;
  79. dctx->remaining = 0;
  80. return remaining;
  81. } else {
  82. dctx->remaining -= buf_size;
  83. // Update variables for correctness, they are currently not used beyond here
  84. state = -1;
  85. dctx->cur_byte += buf_size - i;
  86. break;
  87. }
  88. }
  89. }
  90. } else if (pic_found) {
  91. if (dctx->remaining > buf_size) {
  92. dctx->remaining -= buf_size;
  93. } else {
  94. int remaining = dctx->remaining;
  95. pc->frame_start_found = 0;
  96. pc->state64 = -1;
  97. dctx->cur_byte = 0;
  98. dctx->remaining = 0;
  99. return remaining;
  100. }
  101. }
  102. pc->frame_start_found = pic_found;
  103. pc->state64 = state;
  104. return END_NOT_FOUND;
  105. }
  106. static int dnxhd_parse(AVCodecParserContext *s,
  107. AVCodecContext *avctx,
  108. const uint8_t **poutbuf, int *poutbuf_size,
  109. const uint8_t *buf, int buf_size)
  110. {
  111. DNXHDParserContext *dctx = s->priv_data;
  112. ParseContext *pc = &dctx->pc;
  113. int next;
  114. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  115. next = buf_size;
  116. } else {
  117. next = dnxhd_find_frame_end(dctx, buf, buf_size);
  118. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  119. *poutbuf = NULL;
  120. *poutbuf_size = 0;
  121. return buf_size;
  122. }
  123. }
  124. *poutbuf = buf;
  125. *poutbuf_size = buf_size;
  126. return next;
  127. }
  128. AVCodecParser ff_dnxhd_parser = {
  129. .codec_ids = { AV_CODEC_ID_DNXHD },
  130. .priv_data_size = sizeof(DNXHDParserContext),
  131. .parser_parse = dnxhd_parse,
  132. .parser_close = ff_parse_close,
  133. };