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.

438 lines
14KB

  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_RN64(src + i) &
  51. (AV_RN64(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_RN32(src + i) &
  61. (AV_RN32(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 *hevc_nal_type_name[64] = {
  134. "TRAIL_N", // HEVC_NAL_TRAIL_N
  135. "TRAIL_R", // HEVC_NAL_TRAIL_R
  136. "TSA_N", // HEVC_NAL_TSA_N
  137. "TSA_R", // HEVC_NAL_TSA_R
  138. "STSA_N", // HEVC_NAL_STSA_N
  139. "STSA_R", // HEVC_NAL_STSA_R
  140. "RADL_N", // HEVC_NAL_RADL_N
  141. "RADL_R", // HEVC_NAL_RADL_R
  142. "RASL_N", // HEVC_NAL_RASL_N
  143. "RASL_R", // HEVC_NAL_RASL_R
  144. "RSV_VCL_N10", // HEVC_NAL_VCL_N10
  145. "RSV_VCL_R11", // HEVC_NAL_VCL_R11
  146. "RSV_VCL_N12", // HEVC_NAL_VCL_N12
  147. "RSV_VLC_R13", // HEVC_NAL_VCL_R13
  148. "RSV_VCL_N14", // HEVC_NAL_VCL_N14
  149. "RSV_VCL_R15", // HEVC_NAL_VCL_R15
  150. "BLA_W_LP", // HEVC_NAL_BLA_W_LP
  151. "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL
  152. "BLA_N_LP", // HEVC_NAL_BLA_N_LP
  153. "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL
  154. "IDR_N_LP", // HEVC_NAL_IDR_N_LP
  155. "CRA_NUT", // HEVC_NAL_CRA_NUT
  156. "IRAP_IRAP_VCL22", // HEVC_NAL_IRAP_VCL22
  157. "IRAP_IRAP_VCL23", // HEVC_NAL_IRAP_VCL23
  158. "RSV_VCL24", // HEVC_NAL_RSV_VCL24
  159. "RSV_VCL25", // HEVC_NAL_RSV_VCL25
  160. "RSV_VCL26", // HEVC_NAL_RSV_VCL26
  161. "RSV_VCL27", // HEVC_NAL_RSV_VCL27
  162. "RSV_VCL28", // HEVC_NAL_RSV_VCL28
  163. "RSV_VCL29", // HEVC_NAL_RSV_VCL29
  164. "RSV_VCL30", // HEVC_NAL_RSV_VCL30
  165. "RSV_VCL31", // HEVC_NAL_RSV_VCL31
  166. "VPS", // HEVC_NAL_VPS
  167. "SPS", // HEVC_NAL_SPS
  168. "PPS", // HEVC_NAL_PPS
  169. "AUD", // HEVC_NAL_AUD
  170. "EOS_NUT", // HEVC_NAL_EOS_NUT
  171. "EOB_NUT", // HEVC_NAL_EOB_NUT
  172. "FD_NUT", // HEVC_NAL_FD_NUT
  173. "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX
  174. "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX
  175. "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41
  176. "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42
  177. "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43
  178. "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44
  179. "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45
  180. "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46
  181. "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47
  182. "UNSPEC48", // HEVC_NAL_UNSPEC48
  183. "UNSPEC49", // HEVC_NAL_UNSPEC49
  184. "UNSPEC50", // HEVC_NAL_UNSPEC50
  185. "UNSPEC51", // HEVC_NAL_UNSPEC51
  186. "UNSPEC52", // HEVC_NAL_UNSPEC52
  187. "UNSPEC53", // HEVC_NAL_UNSPEC53
  188. "UNSPEC54", // HEVC_NAL_UNSPEC54
  189. "UNSPEC55", // HEVC_NAL_UNSPEC55
  190. "UNSPEC56", // HEVC_NAL_UNSPEC56
  191. "UNSPEC57", // HEVC_NAL_UNSPEC57
  192. "UNSPEC58", // HEVC_NAL_UNSPEC58
  193. "UNSPEC59", // HEVC_NAL_UNSPEC59
  194. "UNSPEC60", // HEVC_NAL_UNSPEC60
  195. "UNSPEC61", // HEVC_NAL_UNSPEC61
  196. "UNSPEC62", // HEVC_NAL_UNSPEC62
  197. "UNSPEC63", // HEVC_NAL_UNSPEC63
  198. };
  199. static const char *nal_unit_name(int nal_type)
  200. {
  201. av_assert0(nal_type >= 0 && nal_type < 64);
  202. return hevc_nal_type_name[nal_type];
  203. }
  204. static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
  205. {
  206. int size = nal->size;
  207. int v;
  208. while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
  209. size--;
  210. if (!size)
  211. return 0;
  212. v = nal->data[size - 1];
  213. if (size > INT_MAX / 8)
  214. return AVERROR(ERANGE);
  215. size *= 8;
  216. /* remove the stop bit and following trailing zeros,
  217. * or nothing for damaged bitstreams */
  218. if (v)
  219. size -= ff_ctz(v) + 1;
  220. return size;
  221. }
  222. /**
  223. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  224. * 0 if the unit should be skipped, 1 otherwise
  225. */
  226. static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
  227. {
  228. GetBitContext *gb = &nal->gb;
  229. int nuh_layer_id;
  230. if (get_bits1(gb) != 0)
  231. return AVERROR_INVALIDDATA;
  232. nal->type = get_bits(gb, 6);
  233. nuh_layer_id = get_bits(gb, 6);
  234. nal->temporal_id = get_bits(gb, 3) - 1;
  235. if (nal->temporal_id < 0)
  236. return AVERROR_INVALIDDATA;
  237. av_log(logctx, AV_LOG_DEBUG,
  238. "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
  239. nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
  240. return nuh_layer_id == 0;
  241. }
  242. static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
  243. {
  244. GetBitContext *gb = &nal->gb;
  245. if (get_bits1(gb) != 0)
  246. return AVERROR_INVALIDDATA;
  247. nal->ref_idc = get_bits(gb, 2);
  248. nal->type = get_bits(gb, 5);
  249. av_log(logctx, AV_LOG_DEBUG,
  250. "nal_unit_type: %d, nal_ref_idc: %d\n",
  251. nal->type, nal->ref_idc);
  252. return 1;
  253. }
  254. static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
  255. {
  256. int i = 0;
  257. if (buf + 3 >= next_avc)
  258. return next_avc - buf;
  259. while (buf + i + 3 < next_avc) {
  260. if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
  261. break;
  262. i++;
  263. }
  264. return i + 3;
  265. }
  266. int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
  267. void *logctx, int is_nalff, int nal_length_size,
  268. enum AVCodecID codec_id, int small_padding)
  269. {
  270. GetByteContext bc;
  271. int consumed, ret = 0;
  272. int next_avc = is_nalff ? 0 : length;
  273. int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
  274. bytestream2_init(&bc, buf, length);
  275. av_fast_padded_malloc(&pkt->rbsp.rbsp_buffer, &pkt->rbsp.rbsp_buffer_alloc_size, length + padding);
  276. if (!pkt->rbsp.rbsp_buffer)
  277. return AVERROR(ENOMEM);
  278. pkt->rbsp.rbsp_buffer_size = 0;
  279. pkt->nb_nals = 0;
  280. while (bytestream2_get_bytes_left(&bc) >= 4) {
  281. H2645NAL *nal;
  282. int extract_length = 0;
  283. int skip_trailing_zeros = 1;
  284. if (bytestream2_tell(&bc) == next_avc) {
  285. int i = 0;
  286. extract_length = get_nalsize(nal_length_size,
  287. bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
  288. if (extract_length < 0)
  289. return extract_length;
  290. bytestream2_skip(&bc, nal_length_size);
  291. next_avc = bytestream2_tell(&bc) + extract_length;
  292. } else {
  293. int buf_index;
  294. if (bytestream2_tell(&bc) > next_avc)
  295. av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
  296. /* search start code */
  297. buf_index = find_next_start_code(bc.buffer, buf + next_avc);
  298. bytestream2_skip(&bc, buf_index);
  299. if (!bytestream2_get_bytes_left(&bc)) {
  300. if (pkt->nb_nals > 0) {
  301. // No more start codes: we discarded some irrelevant
  302. // bytes at the end of the packet.
  303. return 0;
  304. } else {
  305. av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
  306. return AVERROR_INVALIDDATA;
  307. }
  308. }
  309. extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
  310. if (bytestream2_tell(&bc) >= next_avc) {
  311. /* skip to the start of the next NAL */
  312. bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
  313. continue;
  314. }
  315. }
  316. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  317. int new_size = pkt->nals_allocated + 1;
  318. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  319. if (!tmp)
  320. return AVERROR(ENOMEM);
  321. pkt->nals = tmp;
  322. memset(pkt->nals + pkt->nals_allocated, 0,
  323. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  324. nal = &pkt->nals[pkt->nb_nals];
  325. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  326. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  327. if (!nal->skipped_bytes_pos)
  328. return AVERROR(ENOMEM);
  329. pkt->nals_allocated = new_size;
  330. }
  331. nal = &pkt->nals[pkt->nb_nals];
  332. consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
  333. if (consumed < 0)
  334. return consumed;
  335. if (is_nalff && (extract_length != consumed) && extract_length)
  336. av_log(logctx, AV_LOG_DEBUG,
  337. "NALFF: Consumed only %d bytes instead of %d\n",
  338. consumed, extract_length);
  339. pkt->nb_nals++;
  340. bytestream2_skip(&bc, consumed);
  341. /* see commit 3566042a0 */
  342. if (bytestream2_get_bytes_left(&bc) >= 4 &&
  343. bytestream2_peek_be32(&bc) == 0x000001E0)
  344. skip_trailing_zeros = 0;
  345. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  346. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  347. if (ret < 0)
  348. return ret;
  349. if (codec_id == AV_CODEC_ID_HEVC)
  350. ret = hevc_parse_nal_header(nal, logctx);
  351. else
  352. ret = h264_parse_nal_header(nal, logctx);
  353. if (ret <= 0 || nal->size <= 0 || nal->size_bits <= 0) {
  354. if (ret < 0) {
  355. av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  356. nal->type);
  357. }
  358. pkt->nb_nals--;
  359. }
  360. }
  361. return 0;
  362. }
  363. void ff_h2645_packet_uninit(H2645Packet *pkt)
  364. {
  365. int i;
  366. for (i = 0; i < pkt->nals_allocated; i++) {
  367. av_freep(&pkt->nals[i].skipped_bytes_pos);
  368. }
  369. av_freep(&pkt->nals);
  370. pkt->nals_allocated = 0;
  371. av_freep(&pkt->rbsp.rbsp_buffer);
  372. pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
  373. }