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.

381 lines
12KB

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