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.

380 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. }
  85. av_fast_padded_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 = 0;
  228. extract_length = get_nalsize(nal_length_size,
  229. buf, length, &i, logctx);
  230. if (extract_length < 0)
  231. return extract_length;
  232. buf += nal_length_size;
  233. length -= nal_length_size;
  234. next_avc = buf + extract_length;
  235. } else {
  236. if (buf > next_avc)
  237. av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
  238. /* search start code */
  239. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  240. ++buf;
  241. --length;
  242. if (length < 4) {
  243. if (pkt->nb_nals > 0) {
  244. // No more start codes: we discarded some irrelevant
  245. // bytes at the end of the packet.
  246. return 0;
  247. } else {
  248. av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
  249. return AVERROR_INVALIDDATA;
  250. }
  251. } else if (buf >= (next_avc - 3))
  252. break;
  253. }
  254. buf += 3;
  255. length -= 3;
  256. extract_length = FFMIN(length, next_avc - buf);
  257. if (buf >= next_avc) {
  258. /* skip to the start of the next NAL */
  259. int offset = next_avc - buf;
  260. buf += offset;
  261. length -= offset;
  262. continue;
  263. }
  264. }
  265. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  266. int new_size = pkt->nals_allocated + 1;
  267. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  268. if (!tmp)
  269. return AVERROR(ENOMEM);
  270. pkt->nals = tmp;
  271. memset(pkt->nals + pkt->nals_allocated, 0,
  272. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  273. nal = &pkt->nals[pkt->nb_nals];
  274. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  275. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  276. if (!nal->skipped_bytes_pos)
  277. return AVERROR(ENOMEM);
  278. pkt->nals_allocated = new_size;
  279. }
  280. nal = &pkt->nals[pkt->nb_nals];
  281. consumed = ff_h2645_extract_rbsp(buf, extract_length, nal, small_padding);
  282. if (consumed < 0)
  283. return consumed;
  284. if (is_nalff && (extract_length != consumed) && extract_length)
  285. av_log(logctx, AV_LOG_DEBUG,
  286. "NALFF: Consumed only %d bytes instead of %d\n",
  287. consumed, extract_length);
  288. pkt->nb_nals++;
  289. /* see commit 3566042a0 */
  290. if (consumed < length - 3 &&
  291. buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&
  292. buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)
  293. skip_trailing_zeros = 0;
  294. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  295. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  296. if (ret < 0)
  297. return ret;
  298. if (codec_id == AV_CODEC_ID_HEVC)
  299. ret = hevc_parse_nal_header(nal, logctx);
  300. else
  301. ret = h264_parse_nal_header(nal, logctx);
  302. if (ret <= 0 || nal->size <= 0) {
  303. if (ret < 0) {
  304. av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  305. nal->type);
  306. }
  307. pkt->nb_nals--;
  308. }
  309. buf += consumed;
  310. length -= consumed;
  311. }
  312. return 0;
  313. }
  314. void ff_h2645_packet_uninit(H2645Packet *pkt)
  315. {
  316. int i;
  317. for (i = 0; i < pkt->nals_allocated; i++) {
  318. av_freep(&pkt->nals[i].rbsp_buffer);
  319. av_freep(&pkt->nals[i].skipped_bytes_pos);
  320. }
  321. av_freep(&pkt->nals);
  322. pkt->nals_allocated = 0;
  323. }