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.

144 lines
4.3KB

  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 = ff_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. int remaining = 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. }
  84. }
  85. }
  86. } else if (pic_found) {
  87. if (dctx->remaining > buf_size) {
  88. dctx->remaining -= buf_size;
  89. } else {
  90. int remaining = dctx->remaining;
  91. pc->frame_start_found = 0;
  92. pc->state64 = -1;
  93. dctx->cur_byte = 0;
  94. dctx->remaining = 0;
  95. return remaining;
  96. }
  97. }
  98. pc->frame_start_found = pic_found;
  99. pc->state64 = state;
  100. return END_NOT_FOUND;
  101. }
  102. static int dnxhd_parse(AVCodecParserContext *s,
  103. AVCodecContext *avctx,
  104. const uint8_t **poutbuf, int *poutbuf_size,
  105. const uint8_t *buf, int buf_size)
  106. {
  107. DNXHDParserContext *dctx = s->priv_data;
  108. ParseContext *pc = &dctx->pc;
  109. int next;
  110. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  111. next = buf_size;
  112. } else {
  113. next = dnxhd_find_frame_end(dctx, buf, buf_size);
  114. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  115. *poutbuf = NULL;
  116. *poutbuf_size = 0;
  117. return buf_size;
  118. }
  119. }
  120. *poutbuf = buf;
  121. *poutbuf_size = buf_size;
  122. return next;
  123. }
  124. AVCodecParser ff_dnxhd_parser = {
  125. .codec_ids = { AV_CODEC_ID_DNXHD },
  126. .priv_data_size = sizeof(DNXHDParserContext),
  127. .parser_parse = dnxhd_parse,
  128. .parser_close = ff_parse_close,
  129. };