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.

155 lines
4.6KB

  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_get_hr_frame_size(int cid, int w, int h)
  34. {
  35. int result, i = ff_dnxhd_get_cid_table(cid);
  36. if (i < 0)
  37. return i;
  38. result = ((h + 15) / 16) * ((w + 15) / 16) * ff_dnxhd_cid_table[i].packet_scale.num / ff_dnxhd_cid_table[i].packet_scale.den;
  39. result = (result + 2048) / 4096 * 4096;
  40. return FFMAX(result, 8192);
  41. }
  42. static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
  43. const uint8_t *buf, int buf_size)
  44. {
  45. ParseContext *pc = &dctx->pc;
  46. uint64_t state = pc->state64;
  47. int pic_found = pc->frame_start_found;
  48. int i = 0;
  49. if (!pic_found) {
  50. for (i = 0; i < buf_size; i++) {
  51. state = (state << 8) | buf[i];
  52. if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
  53. i++;
  54. pic_found = 1;
  55. dctx->cur_byte = 0;
  56. dctx->remaining = 0;
  57. break;
  58. }
  59. }
  60. }
  61. if (pic_found && !dctx->remaining) {
  62. if (!buf_size) /* EOF considered as end of frame */
  63. return 0;
  64. for (; i < buf_size; i++) {
  65. dctx->cur_byte++;
  66. state = (state << 8) | buf[i];
  67. if (dctx->cur_byte == 24) {
  68. dctx->h = (state >> 32) & 0xFFFF;
  69. } else if (dctx->cur_byte == 26) {
  70. dctx->w = (state >> 32) & 0xFFFF;
  71. } else if (dctx->cur_byte == 42) {
  72. int cid = (state >> 32) & 0xFFFFFFFF;
  73. if (cid <= 0)
  74. continue;
  75. dctx->remaining = avpriv_dnxhd_get_frame_size(cid);
  76. if (dctx->remaining <= 0) {
  77. dctx->remaining = dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
  78. if (dctx->remaining <= 0)
  79. return dctx->remaining;
  80. }
  81. if (buf_size - i + 47 >= dctx->remaining) {
  82. int remaining = dctx->remaining;
  83. pc->frame_start_found = 0;
  84. pc->state64 = -1;
  85. dctx->cur_byte = 0;
  86. dctx->remaining = 0;
  87. return remaining;
  88. } else {
  89. dctx->remaining -= buf_size;
  90. }
  91. }
  92. }
  93. } else if (pic_found) {
  94. if (dctx->remaining > buf_size) {
  95. dctx->remaining -= buf_size;
  96. } else {
  97. int remaining = dctx->remaining;
  98. pc->frame_start_found = 0;
  99. pc->state64 = -1;
  100. dctx->cur_byte = 0;
  101. dctx->remaining = 0;
  102. return remaining;
  103. }
  104. }
  105. pc->frame_start_found = pic_found;
  106. pc->state64 = state;
  107. return END_NOT_FOUND;
  108. }
  109. static int dnxhd_parse(AVCodecParserContext *s,
  110. AVCodecContext *avctx,
  111. const uint8_t **poutbuf, int *poutbuf_size,
  112. const uint8_t *buf, int buf_size)
  113. {
  114. DNXHDParserContext *dctx = s->priv_data;
  115. ParseContext *pc = &dctx->pc;
  116. int next;
  117. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  118. next = buf_size;
  119. } else {
  120. next = dnxhd_find_frame_end(dctx, buf, buf_size);
  121. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  122. *poutbuf = NULL;
  123. *poutbuf_size = 0;
  124. return buf_size;
  125. }
  126. }
  127. *poutbuf = buf;
  128. *poutbuf_size = buf_size;
  129. return next;
  130. }
  131. AVCodecParser ff_dnxhd_parser = {
  132. .codec_ids = { AV_CODEC_ID_DNXHD },
  133. .priv_data_size = sizeof(DNXHDParserContext),
  134. .parser_parse = dnxhd_parse,
  135. .parser_close = ff_parse_close,
  136. };