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.

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