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.

167 lines
5.1KB

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