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.

229 lines
6.6KB

  1. /*
  2. * HEVC common code
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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(const uint8_t *src, int length,
  28. HEVCNAL *nal)
  29. {
  30. int i, si, di;
  31. uint8_t *dst;
  32. #define STARTCODE_TEST \
  33. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  34. if (src[i + 2] != 3) { \
  35. /* startcode, so we must be past the end */ \
  36. length = i; \
  37. } \
  38. break; \
  39. }
  40. #if HAVE_FAST_UNALIGNED
  41. #define FIND_FIRST_ZERO \
  42. if (i > 0 && !src[i]) \
  43. i--; \
  44. while (src[i]) \
  45. i++
  46. #if HAVE_FAST_64BIT
  47. for (i = 0; i + 1 < length; i += 9) {
  48. if (!((~AV_RN64A(src + i) &
  49. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  50. 0x8000800080008080ULL))
  51. continue;
  52. FIND_FIRST_ZERO;
  53. STARTCODE_TEST;
  54. i -= 7;
  55. }
  56. #else
  57. for (i = 0; i + 1 < length; i += 5) {
  58. if (!((~AV_RN32A(src + i) &
  59. (AV_RN32A(src + i) - 0x01000101U)) &
  60. 0x80008080U))
  61. continue;
  62. FIND_FIRST_ZERO;
  63. STARTCODE_TEST;
  64. i -= 3;
  65. }
  66. #endif /* HAVE_FAST_64BIT */
  67. #else
  68. for (i = 0; i + 1 < length; i += 2) {
  69. if (src[i])
  70. continue;
  71. if (i > 0 && src[i - 1] == 0)
  72. i--;
  73. STARTCODE_TEST;
  74. }
  75. #endif /* HAVE_FAST_UNALIGNED */
  76. if (i >= length - 1) { // no escaped 0
  77. nal->data =
  78. nal->raw_data = src;
  79. nal->size =
  80. nal->raw_size = length;
  81. return length;
  82. }
  83. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  84. length + AV_INPUT_BUFFER_PADDING_SIZE);
  85. if (!nal->rbsp_buffer)
  86. return AVERROR(ENOMEM);
  87. dst = nal->rbsp_buffer;
  88. memcpy(dst, src, i);
  89. si = di = i;
  90. while (si + 2 < length) {
  91. // remove escapes (very rare 1:2^22)
  92. if (src[si + 2] > 3) {
  93. dst[di++] = src[si++];
  94. dst[di++] = src[si++];
  95. } else if (src[si] == 0 && src[si + 1] == 0) {
  96. if (src[si + 2] == 3) { // escape
  97. dst[di++] = 0;
  98. dst[di++] = 0;
  99. si += 3;
  100. continue;
  101. } else // next start code
  102. goto nsc;
  103. }
  104. dst[di++] = src[si++];
  105. }
  106. while (si < length)
  107. dst[di++] = src[si++];
  108. nsc:
  109. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  110. nal->data = dst;
  111. nal->size = di;
  112. nal->raw_data = src;
  113. nal->raw_size = si;
  114. return si;
  115. }
  116. /**
  117. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  118. * 0 if the unit should be skipped, 1 otherwise
  119. */
  120. static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
  121. {
  122. GetBitContext *gb = &nal->gb;
  123. int nuh_layer_id;
  124. if (get_bits1(gb) != 0)
  125. return AVERROR_INVALIDDATA;
  126. nal->type = get_bits(gb, 6);
  127. nuh_layer_id = get_bits(gb, 6);
  128. nal->temporal_id = get_bits(gb, 3) - 1;
  129. if (nal->temporal_id < 0)
  130. return AVERROR_INVALIDDATA;
  131. av_log(avctx, AV_LOG_DEBUG,
  132. "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
  133. nal->type, nuh_layer_id, nal->temporal_id);
  134. return nuh_layer_id == 0;
  135. }
  136. int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
  137. AVCodecContext *avctx, int is_nalff, int nal_length_size)
  138. {
  139. int consumed, ret = 0;
  140. pkt->nb_nals = 0;
  141. while (length >= 4) {
  142. HEVCNAL *nal;
  143. int extract_length = 0;
  144. if (is_nalff) {
  145. int i;
  146. for (i = 0; i < nal_length_size; i++)
  147. extract_length = (extract_length << 8) | buf[i];
  148. buf += nal_length_size;
  149. length -= nal_length_size;
  150. if (extract_length > length) {
  151. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  152. return AVERROR_INVALIDDATA;
  153. }
  154. } else {
  155. if (buf[2] == 0) {
  156. length--;
  157. buf++;
  158. continue;
  159. }
  160. if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
  161. return AVERROR_INVALIDDATA;
  162. buf += 3;
  163. length -= 3;
  164. extract_length = length;
  165. }
  166. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  167. int new_size = pkt->nals_allocated + 1;
  168. HEVCNAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
  169. if (!tmp)
  170. return AVERROR(ENOMEM);
  171. pkt->nals = tmp;
  172. memset(pkt->nals + pkt->nals_allocated, 0,
  173. (new_size - pkt->nals_allocated) * sizeof(*tmp));
  174. pkt->nals_allocated = new_size;
  175. }
  176. nal = &pkt->nals[pkt->nb_nals++];
  177. consumed = ff_hevc_extract_rbsp(buf, extract_length, nal);
  178. if (consumed < 0)
  179. return consumed;
  180. ret = init_get_bits8(&nal->gb, nal->data, nal->size);
  181. if (ret < 0)
  182. return ret;
  183. ret = hls_nal_unit(nal, avctx);
  184. if (ret <= 0) {
  185. if (ret < 0) {
  186. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  187. nal->type);
  188. }
  189. pkt->nb_nals--;
  190. }
  191. buf += consumed;
  192. length -= consumed;
  193. }
  194. return 0;
  195. }