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.

532 lines
17KB

  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 "h264.h"
  28. #include "h2645_parse.h"
  29. int ff_h2645_extract_rbsp(const uint8_t *src, int length,
  30. H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
  31. {
  32. int i, si, di;
  33. uint8_t *dst;
  34. nal->skipped_bytes = 0;
  35. #define STARTCODE_TEST \
  36. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  37. if (src[i + 2] != 3 && src[i + 2] != 0) { \
  38. /* startcode, so we must be past the end */ \
  39. length = i; \
  40. } \
  41. break; \
  42. }
  43. #if HAVE_FAST_UNALIGNED
  44. #define FIND_FIRST_ZERO \
  45. if (i > 0 && !src[i]) \
  46. i--; \
  47. while (src[i]) \
  48. i++
  49. #if HAVE_FAST_64BIT
  50. for (i = 0; i + 1 < length; i += 9) {
  51. if (!((~AV_RN64(src + i) &
  52. (AV_RN64(src + i) - 0x0100010001000101ULL)) &
  53. 0x8000800080008080ULL))
  54. continue;
  55. FIND_FIRST_ZERO;
  56. STARTCODE_TEST;
  57. i -= 7;
  58. }
  59. #else
  60. for (i = 0; i + 1 < length; i += 5) {
  61. if (!((~AV_RN32(src + i) &
  62. (AV_RN32(src + i) - 0x01000101U)) &
  63. 0x80008080U))
  64. continue;
  65. FIND_FIRST_ZERO;
  66. STARTCODE_TEST;
  67. i -= 3;
  68. }
  69. #endif /* HAVE_FAST_64BIT */
  70. #else
  71. for (i = 0; i + 1 < length; i += 2) {
  72. if (src[i])
  73. continue;
  74. if (i > 0 && src[i - 1] == 0)
  75. i--;
  76. STARTCODE_TEST;
  77. }
  78. #endif /* HAVE_FAST_UNALIGNED */
  79. if (i >= length - 1 && small_padding) { // no escaped 0
  80. nal->data =
  81. nal->raw_data = src;
  82. nal->size =
  83. nal->raw_size = length;
  84. return length;
  85. } else if (i > length)
  86. i = length;
  87. nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
  88. dst = nal->rbsp_buffer;
  89. memcpy(dst, src, i);
  90. si = di = i;
  91. while (si + 2 < length) {
  92. // remove escapes (very rare 1:2^22)
  93. if (src[si + 2] > 3) {
  94. dst[di++] = src[si++];
  95. dst[di++] = src[si++];
  96. } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
  97. if (src[si + 2] == 3) { // escape
  98. dst[di++] = 0;
  99. dst[di++] = 0;
  100. si += 3;
  101. if (nal->skipped_bytes_pos) {
  102. nal->skipped_bytes++;
  103. if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
  104. nal->skipped_bytes_pos_size *= 2;
  105. av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
  106. av_reallocp_array(&nal->skipped_bytes_pos,
  107. nal->skipped_bytes_pos_size,
  108. sizeof(*nal->skipped_bytes_pos));
  109. if (!nal->skipped_bytes_pos) {
  110. nal->skipped_bytes_pos_size = 0;
  111. return AVERROR(ENOMEM);
  112. }
  113. }
  114. if (nal->skipped_bytes_pos)
  115. nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
  116. }
  117. continue;
  118. } else // next start code
  119. goto nsc;
  120. }
  121. dst[di++] = src[si++];
  122. }
  123. while (si < length)
  124. dst[di++] = src[si++];
  125. nsc:
  126. memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  127. nal->data = dst;
  128. nal->size = di;
  129. nal->raw_data = src;
  130. nal->raw_size = si;
  131. rbsp->rbsp_buffer_size += si;
  132. return si;
  133. }
  134. static const char *hevc_nal_type_name[64] = {
  135. "TRAIL_N", // HEVC_NAL_TRAIL_N
  136. "TRAIL_R", // HEVC_NAL_TRAIL_R
  137. "TSA_N", // HEVC_NAL_TSA_N
  138. "TSA_R", // HEVC_NAL_TSA_R
  139. "STSA_N", // HEVC_NAL_STSA_N
  140. "STSA_R", // HEVC_NAL_STSA_R
  141. "RADL_N", // HEVC_NAL_RADL_N
  142. "RADL_R", // HEVC_NAL_RADL_R
  143. "RASL_N", // HEVC_NAL_RASL_N
  144. "RASL_R", // HEVC_NAL_RASL_R
  145. "RSV_VCL_N10", // HEVC_NAL_VCL_N10
  146. "RSV_VCL_R11", // HEVC_NAL_VCL_R11
  147. "RSV_VCL_N12", // HEVC_NAL_VCL_N12
  148. "RSV_VLC_R13", // HEVC_NAL_VCL_R13
  149. "RSV_VCL_N14", // HEVC_NAL_VCL_N14
  150. "RSV_VCL_R15", // HEVC_NAL_VCL_R15
  151. "BLA_W_LP", // HEVC_NAL_BLA_W_LP
  152. "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL
  153. "BLA_N_LP", // HEVC_NAL_BLA_N_LP
  154. "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL
  155. "IDR_N_LP", // HEVC_NAL_IDR_N_LP
  156. "CRA_NUT", // HEVC_NAL_CRA_NUT
  157. "IRAP_IRAP_VCL22", // HEVC_NAL_IRAP_VCL22
  158. "IRAP_IRAP_VCL23", // HEVC_NAL_IRAP_VCL23
  159. "RSV_VCL24", // HEVC_NAL_RSV_VCL24
  160. "RSV_VCL25", // HEVC_NAL_RSV_VCL25
  161. "RSV_VCL26", // HEVC_NAL_RSV_VCL26
  162. "RSV_VCL27", // HEVC_NAL_RSV_VCL27
  163. "RSV_VCL28", // HEVC_NAL_RSV_VCL28
  164. "RSV_VCL29", // HEVC_NAL_RSV_VCL29
  165. "RSV_VCL30", // HEVC_NAL_RSV_VCL30
  166. "RSV_VCL31", // HEVC_NAL_RSV_VCL31
  167. "VPS", // HEVC_NAL_VPS
  168. "SPS", // HEVC_NAL_SPS
  169. "PPS", // HEVC_NAL_PPS
  170. "AUD", // HEVC_NAL_AUD
  171. "EOS_NUT", // HEVC_NAL_EOS_NUT
  172. "EOB_NUT", // HEVC_NAL_EOB_NUT
  173. "FD_NUT", // HEVC_NAL_FD_NUT
  174. "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX
  175. "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX
  176. "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41
  177. "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42
  178. "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43
  179. "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44
  180. "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45
  181. "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46
  182. "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47
  183. "UNSPEC48", // HEVC_NAL_UNSPEC48
  184. "UNSPEC49", // HEVC_NAL_UNSPEC49
  185. "UNSPEC50", // HEVC_NAL_UNSPEC50
  186. "UNSPEC51", // HEVC_NAL_UNSPEC51
  187. "UNSPEC52", // HEVC_NAL_UNSPEC52
  188. "UNSPEC53", // HEVC_NAL_UNSPEC53
  189. "UNSPEC54", // HEVC_NAL_UNSPEC54
  190. "UNSPEC55", // HEVC_NAL_UNSPEC55
  191. "UNSPEC56", // HEVC_NAL_UNSPEC56
  192. "UNSPEC57", // HEVC_NAL_UNSPEC57
  193. "UNSPEC58", // HEVC_NAL_UNSPEC58
  194. "UNSPEC59", // HEVC_NAL_UNSPEC59
  195. "UNSPEC60", // HEVC_NAL_UNSPEC60
  196. "UNSPEC61", // HEVC_NAL_UNSPEC61
  197. "UNSPEC62", // HEVC_NAL_UNSPEC62
  198. "UNSPEC63", // HEVC_NAL_UNSPEC63
  199. };
  200. static const char *hevc_nal_unit_name(int nal_type)
  201. {
  202. av_assert0(nal_type >= 0 && nal_type < 64);
  203. return hevc_nal_type_name[nal_type];
  204. }
  205. static const char *h264_nal_type_name[32] = {
  206. "Unspecified 0", //H264_NAL_UNSPECIFIED
  207. "Coded slice of a non-IDR picture", // H264_NAL_SLICE
  208. "Coded slice data partition A", // H264_NAL_DPA
  209. "Coded slice data partition B", // H264_NAL_DPB
  210. "Coded slice data partition C", // H264_NAL_DPC
  211. "IDR", // H264_NAL_IDR_SLICE
  212. "SEI", // H264_NAL_SEI
  213. "SPS", // H264_NAL_SPS
  214. "PPS", // H264_NAL_PPS
  215. "AUD", // H264_NAL_AUD
  216. "End of sequence", // H264_NAL_END_SEQUENCE
  217. "End of stream", // H264_NAL_END_STREAM
  218. "Filler data", // H264_NAL_FILLER_DATA
  219. "SPS extension", // H264_NAL_SPS_EXT
  220. "Prefix", // H264_NAL_PREFIX
  221. "Subset SPS", // H264_NAL_SUB_SPS
  222. "Depth parameter set", // H264_NAL_DPS
  223. "Reserved 17", // H264_NAL_RESERVED17
  224. "Reserved 18", // H264_NAL_RESERVED18
  225. "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE
  226. "Slice extension", // H264_NAL_EXTEN_SLICE
  227. "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE
  228. "Reserved 22", // H264_NAL_RESERVED22
  229. "Reserved 23", // H264_NAL_RESERVED23
  230. "Unspecified 24", // H264_NAL_UNSPECIFIED24
  231. "Unspecified 25", // H264_NAL_UNSPECIFIED25
  232. "Unspecified 26", // H264_NAL_UNSPECIFIED26
  233. "Unspecified 27", // H264_NAL_UNSPECIFIED27
  234. "Unspecified 28", // H264_NAL_UNSPECIFIED28
  235. "Unspecified 29", // H264_NAL_UNSPECIFIED29
  236. "Unspecified 30", // H264_NAL_UNSPECIFIED30
  237. "Unspecified 31", // H264_NAL_UNSPECIFIED31
  238. };
  239. static const char *h264_nal_unit_name(int nal_type)
  240. {
  241. av_assert0(nal_type >= 0 && nal_type < 32);
  242. return h264_nal_type_name[nal_type];
  243. }
  244. static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
  245. {
  246. int size = nal->size;
  247. int v;
  248. while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
  249. size--;
  250. if (!size)
  251. return 0;
  252. v = nal->data[size - 1];
  253. if (size > INT_MAX / 8)
  254. return AVERROR(ERANGE);
  255. size *= 8;
  256. /* remove the stop bit and following trailing zeros,
  257. * or nothing for damaged bitstreams */
  258. if (v)
  259. size -= ff_ctz(v) + 1;
  260. return size;
  261. }
  262. /**
  263. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  264. * 0 if the unit should be skipped, 1 otherwise
  265. */
  266. static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
  267. {
  268. GetBitContext *gb = &nal->gb;
  269. int nuh_layer_id;
  270. if (get_bits1(gb) != 0)
  271. return AVERROR_INVALIDDATA;
  272. nal->type = get_bits(gb, 6);
  273. nuh_layer_id = get_bits(gb, 6);
  274. nal->temporal_id = get_bits(gb, 3) - 1;
  275. if (nal->temporal_id < 0)
  276. return AVERROR_INVALIDDATA;
  277. av_log(logctx, AV_LOG_DEBUG,
  278. "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
  279. nal->type, hevc_nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
  280. return nuh_layer_id == 0;
  281. }
  282. static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
  283. {
  284. GetBitContext *gb = &nal->gb;
  285. if (get_bits1(gb) != 0)
  286. return AVERROR_INVALIDDATA;
  287. nal->ref_idc = get_bits(gb, 2);
  288. nal->type = get_bits(gb, 5);
  289. av_log(logctx, AV_LOG_DEBUG,
  290. "nal_unit_type: %d(%s), nal_ref_idc: %d\n",
  291. nal->type, h264_nal_unit_name(nal->type), nal->ref_idc);
  292. return 1;
  293. }
  294. static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
  295. {
  296. int i = 0;
  297. if (buf + 3 >= next_avc)
  298. return next_avc - buf;
  299. while (buf + i + 3 < next_avc) {
  300. if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
  301. break;
  302. i++;
  303. }
  304. return i + 3;
  305. }
  306. static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref)
  307. {
  308. int min_size = size;
  309. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  310. goto fail;
  311. size += AV_INPUT_BUFFER_PADDING_SIZE;
  312. if (rbsp->rbsp_buffer_alloc_size >= size &&
  313. (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) {
  314. av_assert0(rbsp->rbsp_buffer);
  315. memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  316. return;
  317. }
  318. size = FFMIN(size + size / 16 + 32, INT_MAX);
  319. if (rbsp->rbsp_buffer_ref)
  320. av_buffer_unref(&rbsp->rbsp_buffer_ref);
  321. else
  322. av_free(rbsp->rbsp_buffer);
  323. rbsp->rbsp_buffer = av_mallocz(size);
  324. if (!rbsp->rbsp_buffer)
  325. goto fail;
  326. rbsp->rbsp_buffer_alloc_size = size;
  327. if (use_ref) {
  328. rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size,
  329. NULL, NULL, 0);
  330. if (!rbsp->rbsp_buffer_ref)
  331. goto fail;
  332. }
  333. return;
  334. fail:
  335. rbsp->rbsp_buffer_alloc_size = 0;
  336. if (rbsp->rbsp_buffer_ref) {
  337. av_buffer_unref(&rbsp->rbsp_buffer_ref);
  338. rbsp->rbsp_buffer = NULL;
  339. } else
  340. av_freep(&rbsp->rbsp_buffer);
  341. return;
  342. }
  343. int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
  344. void *logctx, int is_nalff, int nal_length_size,
  345. enum AVCodecID codec_id, int small_padding, int use_ref)
  346. {
  347. GetByteContext bc;
  348. int consumed, ret = 0;
  349. int next_avc = is_nalff ? 0 : length;
  350. int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
  351. bytestream2_init(&bc, buf, length);
  352. alloc_rbsp_buffer(&pkt->rbsp, length + padding, use_ref);
  353. if (!pkt->rbsp.rbsp_buffer)
  354. return AVERROR(ENOMEM);
  355. pkt->rbsp.rbsp_buffer_size = 0;
  356. pkt->nb_nals = 0;
  357. while (bytestream2_get_bytes_left(&bc) >= 4) {
  358. H2645NAL *nal;
  359. int extract_length = 0;
  360. int skip_trailing_zeros = 1;
  361. if (bytestream2_tell(&bc) == next_avc) {
  362. int i = 0;
  363. extract_length = get_nalsize(nal_length_size,
  364. bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
  365. if (extract_length < 0)
  366. return extract_length;
  367. bytestream2_skip(&bc, nal_length_size);
  368. next_avc = bytestream2_tell(&bc) + extract_length;
  369. } else {
  370. int buf_index;
  371. if (bytestream2_tell(&bc) > next_avc)
  372. av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
  373. /* search start code */
  374. buf_index = find_next_start_code(bc.buffer, buf + next_avc);
  375. bytestream2_skip(&bc, buf_index);
  376. if (!bytestream2_get_bytes_left(&bc)) {
  377. if (pkt->nb_nals > 0) {
  378. // No more start codes: we discarded some irrelevant
  379. // bytes at the end of the packet.
  380. return 0;
  381. } else {
  382. av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
  383. return AVERROR_INVALIDDATA;
  384. }
  385. }
  386. extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
  387. if (bytestream2_tell(&bc) >= next_avc) {
  388. /* skip to the start of the next NAL */
  389. bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
  390. continue;
  391. }
  392. }
  393. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  394. int new_size = pkt->nals_allocated + 1;
  395. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  396. if (!tmp)
  397. return AVERROR(ENOMEM);
  398. pkt->nals = tmp;
  399. memset(pkt->nals + pkt->nals_allocated, 0,
  400. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  401. nal = &pkt->nals[pkt->nb_nals];
  402. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  403. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  404. if (!nal->skipped_bytes_pos)
  405. return AVERROR(ENOMEM);
  406. pkt->nals_allocated = new_size;
  407. }
  408. nal = &pkt->nals[pkt->nb_nals];
  409. consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
  410. if (consumed < 0)
  411. return consumed;
  412. if (is_nalff && (extract_length != consumed) && extract_length)
  413. av_log(logctx, AV_LOG_DEBUG,
  414. "NALFF: Consumed only %d bytes instead of %d\n",
  415. consumed, extract_length);
  416. pkt->nb_nals++;
  417. bytestream2_skip(&bc, consumed);
  418. /* see commit 3566042a0 */
  419. if (bytestream2_get_bytes_left(&bc) >= 4 &&
  420. bytestream2_peek_be32(&bc) == 0x000001E0)
  421. skip_trailing_zeros = 0;
  422. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  423. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  424. if (ret < 0)
  425. return ret;
  426. if (codec_id == AV_CODEC_ID_HEVC)
  427. ret = hevc_parse_nal_header(nal, logctx);
  428. else
  429. ret = h264_parse_nal_header(nal, logctx);
  430. if (ret <= 0 || nal->size <= 0 || nal->size_bits <= 0) {
  431. if (ret < 0) {
  432. av_log(logctx, AV_LOG_WARNING, "Invalid NAL unit %d, skipping.\n",
  433. nal->type);
  434. }
  435. pkt->nb_nals--;
  436. }
  437. }
  438. return 0;
  439. }
  440. void ff_h2645_packet_uninit(H2645Packet *pkt)
  441. {
  442. int i;
  443. for (i = 0; i < pkt->nals_allocated; i++) {
  444. av_freep(&pkt->nals[i].skipped_bytes_pos);
  445. }
  446. av_freep(&pkt->nals);
  447. pkt->nals_allocated = 0;
  448. if (pkt->rbsp.rbsp_buffer_ref) {
  449. av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref);
  450. pkt->rbsp.rbsp_buffer = NULL;
  451. } else
  452. av_freep(&pkt->rbsp.rbsp_buffer);
  453. pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
  454. }