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