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.

397 lines
13KB

  1. /*
  2. * H.264/HEVC common parsing 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/intmath.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/mem.h"
  25. #include "bytestream.h"
  26. #include "hevc.h"
  27. #include "h2645_parse.h"
  28. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
  29. H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
  30. {
  31. int i, si, di;
  32. uint8_t *dst;
  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 && src[i + 2] != 0) { \
  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 && small_padding) { // no escaped 0
  79. nal->data =
  80. nal->raw_data = src;
  81. nal->size =
  82. nal->raw_size = length;
  83. return length;
  84. } else if (i > length)
  85. i = length;
  86. nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
  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 && src[si + 2] != 0) {
  96. if (src[si + 2] == 3) { // escape
  97. dst[di++] = 0;
  98. dst[di++] = 0;
  99. si += 3;
  100. if (nal->skipped_bytes_pos) {
  101. nal->skipped_bytes++;
  102. if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
  103. nal->skipped_bytes_pos_size *= 2;
  104. av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
  105. av_reallocp_array(&nal->skipped_bytes_pos,
  106. nal->skipped_bytes_pos_size,
  107. sizeof(*nal->skipped_bytes_pos));
  108. if (!nal->skipped_bytes_pos) {
  109. nal->skipped_bytes_pos_size = 0;
  110. return AVERROR(ENOMEM);
  111. }
  112. }
  113. if (nal->skipped_bytes_pos)
  114. nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
  115. }
  116. continue;
  117. } else // next start code
  118. goto nsc;
  119. }
  120. dst[di++] = src[si++];
  121. }
  122. while (si < length)
  123. dst[di++] = src[si++];
  124. nsc:
  125. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  126. nal->data = dst;
  127. nal->size = di;
  128. nal->raw_data = src;
  129. nal->raw_size = si;
  130. rbsp->rbsp_buffer_size += si;
  131. return si;
  132. }
  133. static const char *nal_unit_name(int nal_type)
  134. {
  135. switch(nal_type) {
  136. case HEVC_NAL_TRAIL_N : return "TRAIL_N";
  137. case HEVC_NAL_TRAIL_R : return "TRAIL_R";
  138. case HEVC_NAL_TSA_N : return "TSA_N";
  139. case HEVC_NAL_TSA_R : return "TSA_R";
  140. case HEVC_NAL_STSA_N : return "STSA_N";
  141. case HEVC_NAL_STSA_R : return "STSA_R";
  142. case HEVC_NAL_RADL_N : return "RADL_N";
  143. case HEVC_NAL_RADL_R : return "RADL_R";
  144. case HEVC_NAL_RASL_N : return "RASL_N";
  145. case HEVC_NAL_RASL_R : return "RASL_R";
  146. case HEVC_NAL_BLA_W_LP : return "BLA_W_LP";
  147. case HEVC_NAL_BLA_W_RADL : return "BLA_W_RADL";
  148. case HEVC_NAL_BLA_N_LP : return "BLA_N_LP";
  149. case HEVC_NAL_IDR_W_RADL : return "IDR_W_RADL";
  150. case HEVC_NAL_IDR_N_LP : return "IDR_N_LP";
  151. case HEVC_NAL_CRA_NUT : return "CRA_NUT";
  152. case HEVC_NAL_VPS : return "VPS";
  153. case HEVC_NAL_SPS : return "SPS";
  154. case HEVC_NAL_PPS : return "PPS";
  155. case HEVC_NAL_AUD : return "AUD";
  156. case HEVC_NAL_EOS_NUT : return "EOS_NUT";
  157. case HEVC_NAL_EOB_NUT : return "EOB_NUT";
  158. case HEVC_NAL_FD_NUT : return "FD_NUT";
  159. case HEVC_NAL_SEI_PREFIX : return "SEI_PREFIX";
  160. case HEVC_NAL_SEI_SUFFIX : return "SEI_SUFFIX";
  161. default : return "?";
  162. }
  163. }
  164. static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
  165. {
  166. int size = nal->size;
  167. int v;
  168. while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
  169. size--;
  170. if (!size)
  171. return 0;
  172. v = nal->data[size - 1];
  173. if (size > INT_MAX / 8)
  174. return AVERROR(ERANGE);
  175. size *= 8;
  176. /* remove the stop bit and following trailing zeros,
  177. * or nothing for damaged bitstreams */
  178. if (v)
  179. size -= ff_ctz(v) + 1;
  180. return size;
  181. }
  182. /**
  183. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  184. * 0 if the unit should be skipped, 1 otherwise
  185. */
  186. static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
  187. {
  188. GetBitContext *gb = &nal->gb;
  189. int nuh_layer_id;
  190. if (get_bits1(gb) != 0)
  191. return AVERROR_INVALIDDATA;
  192. nal->type = get_bits(gb, 6);
  193. nuh_layer_id = get_bits(gb, 6);
  194. nal->temporal_id = get_bits(gb, 3) - 1;
  195. if (nal->temporal_id < 0)
  196. return AVERROR_INVALIDDATA;
  197. av_log(logctx, AV_LOG_DEBUG,
  198. "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
  199. nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
  200. return nuh_layer_id == 0;
  201. }
  202. static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
  203. {
  204. GetBitContext *gb = &nal->gb;
  205. if (get_bits1(gb) != 0)
  206. return AVERROR_INVALIDDATA;
  207. nal->ref_idc = get_bits(gb, 2);
  208. nal->type = get_bits(gb, 5);
  209. av_log(logctx, AV_LOG_DEBUG,
  210. "nal_unit_type: %d, nal_ref_idc: %d\n",
  211. nal->type, nal->ref_idc);
  212. return 1;
  213. }
  214. static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
  215. {
  216. int i = 0;
  217. if (buf + 3 >= next_avc)
  218. return next_avc - buf;
  219. while (buf + i + 3 < next_avc) {
  220. if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
  221. break;
  222. i++;
  223. }
  224. return i + 3;
  225. }
  226. int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
  227. void *logctx, int is_nalff, int nal_length_size,
  228. enum AVCodecID codec_id, int small_padding)
  229. {
  230. GetByteContext bc;
  231. int consumed, ret = 0;
  232. int next_avc = is_nalff ? 0 : length;
  233. int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
  234. bytestream2_init(&bc, buf, length);
  235. av_fast_padded_malloc(&pkt->rbsp.rbsp_buffer, &pkt->rbsp.rbsp_buffer_alloc_size, length + padding);
  236. if (!pkt->rbsp.rbsp_buffer)
  237. return AVERROR(ENOMEM);
  238. pkt->rbsp.rbsp_buffer_size = 0;
  239. pkt->nb_nals = 0;
  240. while (bytestream2_get_bytes_left(&bc) >= 4) {
  241. H2645NAL *nal;
  242. int extract_length = 0;
  243. int skip_trailing_zeros = 1;
  244. if (bytestream2_tell(&bc) == next_avc) {
  245. int i = 0;
  246. extract_length = get_nalsize(nal_length_size,
  247. bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
  248. if (extract_length < 0)
  249. return extract_length;
  250. bytestream2_skip(&bc, nal_length_size);
  251. next_avc = bytestream2_tell(&bc) + extract_length;
  252. } else {
  253. int buf_index;
  254. if (bytestream2_tell(&bc) > next_avc)
  255. av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
  256. /* search start code */
  257. buf_index = find_next_start_code(bc.buffer, buf + next_avc);
  258. bytestream2_skip(&bc, buf_index);
  259. if (!bytestream2_get_bytes_left(&bc)) {
  260. if (pkt->nb_nals > 0) {
  261. // No more start codes: we discarded some irrelevant
  262. // bytes at the end of the packet.
  263. return 0;
  264. } else {
  265. av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
  266. return AVERROR_INVALIDDATA;
  267. }
  268. }
  269. extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
  270. if (bytestream2_tell(&bc) >= next_avc) {
  271. /* skip to the start of the next NAL */
  272. bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
  273. continue;
  274. }
  275. }
  276. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  277. int new_size = pkt->nals_allocated + 1;
  278. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  279. if (!tmp)
  280. return AVERROR(ENOMEM);
  281. pkt->nals = tmp;
  282. memset(pkt->nals + pkt->nals_allocated, 0,
  283. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  284. nal = &pkt->nals[pkt->nb_nals];
  285. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  286. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  287. if (!nal->skipped_bytes_pos)
  288. return AVERROR(ENOMEM);
  289. pkt->nals_allocated = new_size;
  290. }
  291. nal = &pkt->nals[pkt->nb_nals];
  292. consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
  293. if (consumed < 0)
  294. return consumed;
  295. if (is_nalff && (extract_length != consumed) && extract_length)
  296. av_log(logctx, AV_LOG_DEBUG,
  297. "NALFF: Consumed only %d bytes instead of %d\n",
  298. consumed, extract_length);
  299. pkt->nb_nals++;
  300. bytestream2_skip(&bc, consumed);
  301. /* see commit 3566042a0 */
  302. if (bytestream2_get_bytes_left(&bc) >= 4 &&
  303. bytestream2_peek_be32(&bc) == 0x000001E0)
  304. skip_trailing_zeros = 0;
  305. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  306. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  307. if (ret < 0)
  308. return ret;
  309. if (codec_id == AV_CODEC_ID_HEVC)
  310. ret = hevc_parse_nal_header(nal, logctx);
  311. else
  312. ret = h264_parse_nal_header(nal, logctx);
  313. if (ret <= 0 || nal->size <= 0 || nal->size_bits <= 0) {
  314. if (ret < 0) {
  315. av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  316. nal->type);
  317. }
  318. pkt->nb_nals--;
  319. }
  320. }
  321. return 0;
  322. }
  323. void ff_h2645_packet_uninit(H2645Packet *pkt)
  324. {
  325. int i;
  326. for (i = 0; i < pkt->nals_allocated; i++) {
  327. av_freep(&pkt->nals[i].skipped_bytes_pos);
  328. }
  329. av_freep(&pkt->nals);
  330. pkt->nals_allocated = 0;
  331. av_freep(&pkt->rbsp.rbsp_buffer);
  332. pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
  333. }