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.

146 lines
4.6KB

  1. /*
  2. * HEVC common code
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include "config.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/mem.h"
  24. #include "hevc.h"
  25. /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
  26. * between these functions would be nice. */
  27. int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
  28. HEVCNAL *nal)
  29. {
  30. int i, si, di;
  31. uint8_t *dst;
  32. if (s)
  33. s->skipped_bytes = 0;
  34. #define STARTCODE_TEST \
  35. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  36. if (src[i + 2] != 3) { \
  37. /* startcode, so we must be past the end */ \
  38. length = i; \
  39. } \
  40. break; \
  41. }
  42. #if HAVE_FAST_UNALIGNED
  43. #define FIND_FIRST_ZERO \
  44. if (i > 0 && !src[i]) \
  45. i--; \
  46. while (src[i]) \
  47. i++
  48. #if HAVE_FAST_64BIT
  49. for (i = 0; i + 1 < length; i += 9) {
  50. if (!((~AV_RN64A(src + i) &
  51. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  52. 0x8000800080008080ULL))
  53. continue;
  54. FIND_FIRST_ZERO;
  55. STARTCODE_TEST;
  56. i -= 7;
  57. }
  58. #else
  59. for (i = 0; i + 1 < length; i += 5) {
  60. if (!((~AV_RN32A(src + i) &
  61. (AV_RN32A(src + i) - 0x01000101U)) &
  62. 0x80008080U))
  63. continue;
  64. FIND_FIRST_ZERO;
  65. STARTCODE_TEST;
  66. i -= 3;
  67. }
  68. #endif /* HAVE_FAST_64BIT */
  69. #else
  70. for (i = 0; i + 1 < length; i += 2) {
  71. if (src[i])
  72. continue;
  73. if (i > 0 && src[i - 1] == 0)
  74. i--;
  75. STARTCODE_TEST;
  76. }
  77. #endif /* HAVE_FAST_UNALIGNED */
  78. if (i >= length - 1) { // no escaped 0
  79. nal->data =
  80. nal->raw_data = src;
  81. nal->size =
  82. nal->raw_size = length;
  83. return length;
  84. }
  85. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  86. length + FF_INPUT_BUFFER_PADDING_SIZE);
  87. if (!nal->rbsp_buffer)
  88. return AVERROR(ENOMEM);
  89. dst = nal->rbsp_buffer;
  90. memcpy(dst, src, i);
  91. si = di = i;
  92. while (si + 2 < length) {
  93. // remove escapes (very rare 1:2^22)
  94. if (src[si + 2] > 3) {
  95. dst[di++] = src[si++];
  96. dst[di++] = src[si++];
  97. } else if (src[si] == 0 && src[si + 1] == 0) {
  98. if (src[si + 2] == 3) { // escape
  99. dst[di++] = 0;
  100. dst[di++] = 0;
  101. si += 3;
  102. if (s) {
  103. s->skipped_bytes++;
  104. if (s->skipped_bytes_pos_size < s->skipped_bytes) {
  105. s->skipped_bytes_pos_size *= 2;
  106. av_reallocp_array(&s->skipped_bytes_pos,
  107. s->skipped_bytes_pos_size,
  108. sizeof(*s->skipped_bytes_pos));
  109. if (!s->skipped_bytes_pos)
  110. return AVERROR(ENOMEM);
  111. }
  112. if (s->skipped_bytes_pos)
  113. s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
  114. }
  115. continue;
  116. } else // next start code
  117. goto nsc;
  118. }
  119. dst[di++] = src[si++];
  120. }
  121. while (si < length)
  122. dst[di++] = src[si++];
  123. nsc:
  124. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  125. nal->data = dst;
  126. nal->size = di;
  127. nal->raw_data = src;
  128. nal->raw_size = si;
  129. return si;
  130. }