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.

291 lines
8.1KB

  1. /*
  2. * H.264/HEVC common parsing 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/intmath.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/mem.h"
  25. #include "h2645_parse.h"
  26. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
  27. H2645NAL *nal)
  28. {
  29. int i, si, di;
  30. uint8_t *dst;
  31. #define STARTCODE_TEST \
  32. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  33. if (src[i + 2] != 3) { \
  34. /* startcode, so we must be past the end */ \
  35. length = i; \
  36. } \
  37. break; \
  38. }
  39. #if HAVE_FAST_UNALIGNED
  40. #define FIND_FIRST_ZERO \
  41. if (i > 0 && !src[i]) \
  42. i--; \
  43. while (src[i]) \
  44. i++
  45. #if HAVE_FAST_64BIT
  46. for (i = 0; i + 1 < length; i += 9) {
  47. if (!((~AV_RN64A(src + i) &
  48. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  49. 0x8000800080008080ULL))
  50. continue;
  51. FIND_FIRST_ZERO;
  52. STARTCODE_TEST;
  53. i -= 7;
  54. }
  55. #else
  56. for (i = 0; i + 1 < length; i += 5) {
  57. if (!((~AV_RN32A(src + i) &
  58. (AV_RN32A(src + i) - 0x01000101U)) &
  59. 0x80008080U))
  60. continue;
  61. FIND_FIRST_ZERO;
  62. STARTCODE_TEST;
  63. i -= 3;
  64. }
  65. #endif /* HAVE_FAST_64BIT */
  66. #else
  67. for (i = 0; i + 1 < length; i += 2) {
  68. if (src[i])
  69. continue;
  70. if (i > 0 && src[i - 1] == 0)
  71. i--;
  72. STARTCODE_TEST;
  73. }
  74. #endif /* HAVE_FAST_UNALIGNED */
  75. if (i >= length - 1) { // no escaped 0
  76. nal->data =
  77. nal->raw_data = src;
  78. nal->size =
  79. nal->raw_size = length;
  80. return length;
  81. }
  82. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  83. length + AV_INPUT_BUFFER_PADDING_SIZE);
  84. if (!nal->rbsp_buffer)
  85. return AVERROR(ENOMEM);
  86. dst = nal->rbsp_buffer;
  87. memcpy(dst, src, i);
  88. si = di = i;
  89. while (si + 2 < length) {
  90. // remove escapes (very rare 1:2^22)
  91. if (src[si + 2] > 3) {
  92. dst[di++] = src[si++];
  93. dst[di++] = src[si++];
  94. } else if (src[si] == 0 && src[si + 1] == 0) {
  95. if (src[si + 2] == 3) { // escape
  96. dst[di++] = 0;
  97. dst[di++] = 0;
  98. si += 3;
  99. continue;
  100. } else // next start code
  101. goto nsc;
  102. }
  103. dst[di++] = src[si++];
  104. }
  105. while (si < length)
  106. dst[di++] = src[si++];
  107. nsc:
  108. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  109. nal->data = dst;
  110. nal->size = di;
  111. nal->raw_data = src;
  112. nal->raw_size = si;
  113. return si;
  114. }
  115. static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
  116. {
  117. int size = nal->size;
  118. int v;
  119. while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
  120. size--;
  121. if (!size)
  122. return 0;
  123. v = nal->data[size - 1];
  124. if (size > INT_MAX / 8)
  125. return AVERROR(ERANGE);
  126. size *= 8;
  127. /* remove the stop bit and following trailing zeros,
  128. * or nothing for damaged bitstreams */
  129. if (v)
  130. size -= av_ctz(v) + 1;
  131. return size;
  132. }
  133. /**
  134. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  135. * 0 if the unit should be skipped, 1 otherwise
  136. */
  137. static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
  138. {
  139. GetBitContext *gb = &nal->gb;
  140. int nuh_layer_id;
  141. if (get_bits1(gb) != 0)
  142. return AVERROR_INVALIDDATA;
  143. nal->type = get_bits(gb, 6);
  144. nuh_layer_id = get_bits(gb, 6);
  145. nal->temporal_id = get_bits(gb, 3) - 1;
  146. if (nal->temporal_id < 0)
  147. return AVERROR_INVALIDDATA;
  148. av_log(logctx, AV_LOG_DEBUG,
  149. "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
  150. nal->type, nuh_layer_id, nal->temporal_id);
  151. return nuh_layer_id == 0;
  152. }
  153. static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
  154. {
  155. GetBitContext *gb = &nal->gb;
  156. if (get_bits1(gb) != 0)
  157. return AVERROR_INVALIDDATA;
  158. nal->ref_idc = get_bits(gb, 2);
  159. nal->type = get_bits(gb, 5);
  160. av_log(logctx, AV_LOG_DEBUG,
  161. "nal_unit_type: %d, nal_ref_idc: %d\n",
  162. nal->type, nal->ref_idc);
  163. return 1;
  164. }
  165. int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
  166. void *logctx, int is_nalff, int nal_length_size,
  167. enum AVCodecID codec_id)
  168. {
  169. int consumed, ret = 0;
  170. pkt->nb_nals = 0;
  171. while (length >= 4) {
  172. H2645NAL *nal;
  173. int extract_length = 0;
  174. int skip_trailing_zeros = 1;
  175. if (is_nalff) {
  176. int i;
  177. for (i = 0; i < nal_length_size; i++)
  178. extract_length = (extract_length << 8) | buf[i];
  179. buf += nal_length_size;
  180. length -= nal_length_size;
  181. if (extract_length > length) {
  182. av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  183. return AVERROR_INVALIDDATA;
  184. }
  185. } else {
  186. if (buf[2] == 0) {
  187. length--;
  188. buf++;
  189. continue;
  190. }
  191. if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
  192. return AVERROR_INVALIDDATA;
  193. buf += 3;
  194. length -= 3;
  195. extract_length = length;
  196. }
  197. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  198. int new_size = pkt->nals_allocated + 1;
  199. H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
  200. if (!tmp)
  201. return AVERROR(ENOMEM);
  202. pkt->nals = tmp;
  203. memset(pkt->nals + pkt->nals_allocated, 0,
  204. (new_size - pkt->nals_allocated) * sizeof(*tmp));
  205. pkt->nals_allocated = new_size;
  206. }
  207. nal = &pkt->nals[pkt->nb_nals++];
  208. consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);
  209. if (consumed < 0)
  210. return consumed;
  211. /* see commit 3566042a0 */
  212. if (consumed < length - 3 &&
  213. buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&
  214. buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)
  215. skip_trailing_zeros = 0;
  216. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  217. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  218. if (ret < 0)
  219. return ret;
  220. if (codec_id == AV_CODEC_ID_HEVC)
  221. ret = hevc_parse_nal_header(nal, logctx);
  222. else
  223. ret = h264_parse_nal_header(nal, logctx);
  224. if (ret <= 0) {
  225. if (ret < 0) {
  226. av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  227. nal->type);
  228. }
  229. pkt->nb_nals--;
  230. }
  231. buf += consumed;
  232. length -= consumed;
  233. }
  234. return 0;
  235. }
  236. void ff_h2645_packet_uninit(H2645Packet *pkt)
  237. {
  238. int i;
  239. for (i = 0; i < pkt->nals_allocated; i++)
  240. av_freep(&pkt->nals[i].rbsp_buffer);
  241. av_freep(&pkt->nals);
  242. pkt->nals_allocated = 0;
  243. }