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.

331 lines
9.7KB

  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 "bytestream.h"
  26. #include "h2645_parse.h"
  27. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
  28. H2645NAL *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. static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
  117. {
  118. int size = nal->size;
  119. int v;
  120. while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
  121. size--;
  122. if (!size)
  123. return 0;
  124. v = nal->data[size - 1];
  125. if (size > INT_MAX / 8)
  126. return AVERROR(ERANGE);
  127. size *= 8;
  128. /* remove the stop bit and following trailing zeros,
  129. * or nothing for damaged bitstreams */
  130. if (v)
  131. size -= av_ctz(v) + 1;
  132. return size;
  133. }
  134. /**
  135. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  136. * 0 if the unit should be skipped, 1 otherwise
  137. */
  138. static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
  139. {
  140. GetBitContext *gb = &nal->gb;
  141. int nuh_layer_id;
  142. if (get_bits1(gb) != 0)
  143. return AVERROR_INVALIDDATA;
  144. nal->type = get_bits(gb, 6);
  145. nuh_layer_id = get_bits(gb, 6);
  146. nal->temporal_id = get_bits(gb, 3) - 1;
  147. if (nal->temporal_id < 0)
  148. return AVERROR_INVALIDDATA;
  149. av_log(logctx, AV_LOG_DEBUG,
  150. "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
  151. nal->type, nuh_layer_id, nal->temporal_id);
  152. return nuh_layer_id == 0;
  153. }
  154. static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
  155. {
  156. GetBitContext *gb = &nal->gb;
  157. if (get_bits1(gb) != 0)
  158. return AVERROR_INVALIDDATA;
  159. nal->ref_idc = get_bits(gb, 2);
  160. nal->type = get_bits(gb, 5);
  161. av_log(logctx, AV_LOG_DEBUG,
  162. "nal_unit_type: %d, nal_ref_idc: %d\n",
  163. nal->type, nal->ref_idc);
  164. return 1;
  165. }
  166. static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
  167. {
  168. int i = 0;
  169. if (buf + 3 >= next_avc)
  170. return next_avc - buf;
  171. while (buf + i + 3 < next_avc) {
  172. if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
  173. break;
  174. i++;
  175. }
  176. return i + 3;
  177. }
  178. int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
  179. void *logctx, int is_nalff, int nal_length_size,
  180. enum AVCodecID codec_id)
  181. {
  182. GetByteContext bc;
  183. int consumed, ret = 0;
  184. size_t next_avc = is_nalff ? 0 : length;
  185. bytestream2_init(&bc, buf, length);
  186. pkt->nb_nals = 0;
  187. while (bytestream2_get_bytes_left(&bc) >= 4) {
  188. H2645NAL *nal;
  189. int extract_length = 0;
  190. int skip_trailing_zeros = 1;
  191. /*
  192. * Only parse an AVC1 length field if one is expected at the current
  193. * buffer position. There are unfortunately streams with multiple
  194. * NAL units covered by the length field. Those NAL units are delimited
  195. * by Annex B start code prefixes. ff_h2645_extract_rbsp() detects it
  196. * correctly and consumes only the first NAL unit. The additional NAL
  197. * units are handled here in the Annex B parsing code.
  198. */
  199. if (bytestream2_tell(&bc) == next_avc) {
  200. int i;
  201. for (i = 0; i < nal_length_size; i++)
  202. extract_length = (extract_length << 8) | bytestream2_get_byte(&bc);
  203. if (extract_length > bytestream2_get_bytes_left(&bc)) {
  204. av_log(logctx, AV_LOG_ERROR,
  205. "Invalid NAL unit size (%d > %d).\n",
  206. extract_length, bytestream2_get_bytes_left(&bc));
  207. return AVERROR_INVALIDDATA;
  208. }
  209. // keep track of the next AVC1 length field
  210. next_avc = bytestream2_tell(&bc) + extract_length;
  211. } else {
  212. /*
  213. * expected to return immediately except for streams with mixed
  214. * NAL unit coding
  215. */
  216. int buf_index = find_next_start_code(bc.buffer, buf + next_avc);
  217. bytestream2_skip(&bc, buf_index);
  218. /*
  219. * break if an AVC1 length field is expected at the current buffer
  220. * position
  221. */
  222. if (bytestream2_tell(&bc) == next_avc)
  223. continue;
  224. if (bytestream2_get_bytes_left(&bc) > 0) {
  225. extract_length = bytestream2_get_bytes_left(&bc);
  226. } else if (pkt->nb_nals == 0) {
  227. av_log(logctx, AV_LOG_ERROR, "No NAL unit found\n");
  228. return AVERROR_INVALIDDATA;
  229. } else {
  230. break;
  231. }
  232. }
  233. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  234. int new_size = pkt->nals_allocated + 1;
  235. H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
  236. if (!tmp)
  237. return AVERROR(ENOMEM);
  238. pkt->nals = tmp;
  239. memset(pkt->nals + pkt->nals_allocated, 0,
  240. (new_size - pkt->nals_allocated) * sizeof(*tmp));
  241. pkt->nals_allocated = new_size;
  242. }
  243. nal = &pkt->nals[pkt->nb_nals++];
  244. consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, nal);
  245. if (consumed < 0)
  246. return consumed;
  247. bytestream2_skip(&bc, consumed);
  248. /* see commit 3566042a0 */
  249. if (bytestream2_get_bytes_left(&bc) >= 4 &&
  250. bytestream2_peek_be32(&bc) == 0x000001E0)
  251. skip_trailing_zeros = 0;
  252. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  253. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  254. if (ret < 0)
  255. return ret;
  256. if (codec_id == AV_CODEC_ID_HEVC)
  257. ret = hevc_parse_nal_header(nal, logctx);
  258. else
  259. ret = h264_parse_nal_header(nal, logctx);
  260. if (ret <= 0) {
  261. if (ret < 0) {
  262. av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  263. nal->type);
  264. }
  265. pkt->nb_nals--;
  266. }
  267. }
  268. return 0;
  269. }
  270. void ff_h2645_packet_uninit(H2645Packet *pkt)
  271. {
  272. int i;
  273. for (i = 0; i < pkt->nals_allocated; i++)
  274. av_freep(&pkt->nals[i].rbsp_buffer);
  275. av_freep(&pkt->nals);
  276. pkt->nals_allocated = 0;
  277. }