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.

265 lines
8.3KB

  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. nal->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. nal->skipped_bytes++;
  104. if (s->skipped_bytes_pos_size < nal->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[nal->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. }
  131. /**
  132. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  133. * 0 if the unit should be skipped, 1 otherwise
  134. */
  135. static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
  136. {
  137. GetBitContext *gb = &nal->gb;
  138. int nuh_layer_id;
  139. if (get_bits1(gb) != 0)
  140. return AVERROR_INVALIDDATA;
  141. nal->type = get_bits(gb, 6);
  142. nuh_layer_id = get_bits(gb, 6);
  143. nal->temporal_id = get_bits(gb, 3) - 1;
  144. if (nal->temporal_id < 0)
  145. return AVERROR_INVALIDDATA;
  146. av_log(avctx, AV_LOG_DEBUG,
  147. "nal_unit_type: %d, nuh_layer_id: %d, temporal_id: %d\n",
  148. nal->type, nuh_layer_id, nal->temporal_id);
  149. return nuh_layer_id == 0;
  150. }
  151. int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length,
  152. AVCodecContext *avctx, int is_nalff, int nal_length_size)
  153. {
  154. int consumed, ret = 0;
  155. pkt->nb_nals = 0;
  156. while (length >= 4) {
  157. HEVCNAL *nal;
  158. int extract_length = 0;
  159. if (is_nalff) {
  160. int i;
  161. for (i = 0; i < nal_length_size; i++)
  162. extract_length = (extract_length << 8) | buf[i];
  163. buf += nal_length_size;
  164. length -= nal_length_size;
  165. if (extract_length > length) {
  166. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  167. return AVERROR_INVALIDDATA;
  168. }
  169. } else {
  170. /* search start code */
  171. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  172. ++buf;
  173. --length;
  174. if (length < 4) {
  175. av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. }
  179. buf += 3;
  180. length -= 3;
  181. extract_length = length;
  182. }
  183. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  184. int new_size = pkt->nals_allocated + 1;
  185. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  186. if (!tmp)
  187. return AVERROR(ENOMEM);
  188. pkt->nals = tmp;
  189. memset(pkt->nals + pkt->nals_allocated, 0,
  190. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  191. tmp = av_realloc_array(s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));
  192. if (!tmp)
  193. return AVERROR(ENOMEM);
  194. s->skipped_bytes_pos_nal = tmp;
  195. nal = &pkt->nals[pkt->nb_nals];
  196. nal->skipped_bytes_pos_size_nal = 1024; // initial buffer size
  197. s->skipped_bytes_pos_nal[pkt->nals_allocated] = av_malloc_array(nal->skipped_bytes_pos_size_nal, sizeof(*s->skipped_bytes_pos));
  198. if (!s->skipped_bytes_pos_nal[pkt->nals_allocated])
  199. return AVERROR(ENOMEM);
  200. pkt->nals_allocated = new_size;
  201. }
  202. nal = &pkt->nals[pkt->nb_nals];
  203. s->skipped_bytes_pos_size = nal->skipped_bytes_pos_size_nal;
  204. s->skipped_bytes_pos = s->skipped_bytes_pos_nal[pkt->nb_nals];
  205. consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
  206. if (consumed < 0)
  207. return consumed;
  208. nal->skipped_bytes_pos_size_nal = s->skipped_bytes_pos_size;
  209. s->skipped_bytes_pos_nal[pkt->nb_nals++] = s->skipped_bytes_pos;
  210. ret = init_get_bits8(&nal->gb, nal->data, nal->size);
  211. if (ret < 0)
  212. return ret;
  213. ret = hls_nal_unit(nal, avctx);
  214. if (ret <= 0) {
  215. if (ret < 0) {
  216. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  217. nal->type);
  218. }
  219. pkt->nb_nals--;
  220. }
  221. buf += consumed;
  222. length -= consumed;
  223. }
  224. return 0;
  225. }