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.

534 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. "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22
  158. "RSV_IRAP_VCL23", // HEVC_NAL_RSV_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 otherwise
  265. */
  266. static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
  267. {
  268. GetBitContext *gb = &nal->gb;
  269. if (get_bits1(gb) != 0)
  270. return AVERROR_INVALIDDATA;
  271. nal->type = get_bits(gb, 6);
  272. nal->nuh_layer_id = get_bits(gb, 6);
  273. nal->temporal_id = get_bits(gb, 3) - 1;
  274. if (nal->temporal_id < 0)
  275. return AVERROR_INVALIDDATA;
  276. av_log(logctx, AV_LOG_DEBUG,
  277. "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
  278. nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id);
  279. return 0;
  280. }
  281. static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
  282. {
  283. GetBitContext *gb = &nal->gb;
  284. if (get_bits1(gb) != 0)
  285. return AVERROR_INVALIDDATA;
  286. nal->ref_idc = get_bits(gb, 2);
  287. nal->type = get_bits(gb, 5);
  288. av_log(logctx, AV_LOG_DEBUG,
  289. "nal_unit_type: %d(%s), nal_ref_idc: %d\n",
  290. nal->type, h264_nal_unit_name(nal->type), nal->ref_idc);
  291. return 0;
  292. }
  293. static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
  294. {
  295. int i = 0;
  296. if (buf + 3 >= next_avc)
  297. return next_avc - buf;
  298. while (buf + i + 3 < next_avc) {
  299. if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
  300. break;
  301. i++;
  302. }
  303. return i + 3;
  304. }
  305. static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref)
  306. {
  307. int min_size = size;
  308. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  309. goto fail;
  310. size += AV_INPUT_BUFFER_PADDING_SIZE;
  311. if (rbsp->rbsp_buffer_alloc_size >= size &&
  312. (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) {
  313. av_assert0(rbsp->rbsp_buffer);
  314. memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  315. return;
  316. }
  317. size = FFMIN(size + size / 16 + 32, INT_MAX);
  318. if (rbsp->rbsp_buffer_ref)
  319. av_buffer_unref(&rbsp->rbsp_buffer_ref);
  320. else
  321. av_free(rbsp->rbsp_buffer);
  322. rbsp->rbsp_buffer = av_mallocz(size);
  323. if (!rbsp->rbsp_buffer)
  324. goto fail;
  325. rbsp->rbsp_buffer_alloc_size = size;
  326. if (use_ref) {
  327. rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size,
  328. NULL, NULL, 0);
  329. if (!rbsp->rbsp_buffer_ref)
  330. goto fail;
  331. }
  332. return;
  333. fail:
  334. rbsp->rbsp_buffer_alloc_size = 0;
  335. if (rbsp->rbsp_buffer_ref) {
  336. av_buffer_unref(&rbsp->rbsp_buffer_ref);
  337. rbsp->rbsp_buffer = NULL;
  338. } else
  339. av_freep(&rbsp->rbsp_buffer);
  340. return;
  341. }
  342. int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
  343. void *logctx, int is_nalff, int nal_length_size,
  344. enum AVCodecID codec_id, int small_padding, int use_ref)
  345. {
  346. GetByteContext bc;
  347. int consumed, ret = 0;
  348. int next_avc = is_nalff ? 0 : length;
  349. int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
  350. bytestream2_init(&bc, buf, length);
  351. alloc_rbsp_buffer(&pkt->rbsp, length + padding, use_ref);
  352. if (!pkt->rbsp.rbsp_buffer)
  353. return AVERROR(ENOMEM);
  354. pkt->rbsp.rbsp_buffer_size = 0;
  355. pkt->nb_nals = 0;
  356. while (bytestream2_get_bytes_left(&bc) >= 4) {
  357. H2645NAL *nal;
  358. int extract_length = 0;
  359. int skip_trailing_zeros = 1;
  360. if (bytestream2_tell(&bc) == next_avc) {
  361. int i = 0;
  362. extract_length = get_nalsize(nal_length_size,
  363. bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
  364. if (extract_length < 0)
  365. return extract_length;
  366. bytestream2_skip(&bc, nal_length_size);
  367. next_avc = bytestream2_tell(&bc) + extract_length;
  368. } else {
  369. int buf_index;
  370. if (bytestream2_tell(&bc) > next_avc)
  371. av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
  372. /* search start code */
  373. buf_index = find_next_start_code(bc.buffer, buf + next_avc);
  374. bytestream2_skip(&bc, buf_index);
  375. if (!bytestream2_get_bytes_left(&bc)) {
  376. if (pkt->nb_nals > 0) {
  377. // No more start codes: we discarded some irrelevant
  378. // bytes at the end of the packet.
  379. return 0;
  380. } else {
  381. av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
  382. return AVERROR_INVALIDDATA;
  383. }
  384. }
  385. extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
  386. if (bytestream2_tell(&bc) >= next_avc) {
  387. /* skip to the start of the next NAL */
  388. bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
  389. continue;
  390. }
  391. }
  392. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  393. int new_size = pkt->nals_allocated + 1;
  394. void *tmp;
  395. if (new_size >= INT_MAX / sizeof(*pkt->nals))
  396. return AVERROR(ENOMEM);
  397. tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals));
  398. if (!tmp)
  399. return AVERROR(ENOMEM);
  400. pkt->nals = tmp;
  401. memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals));
  402. nal = &pkt->nals[pkt->nb_nals];
  403. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  404. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  405. if (!nal->skipped_bytes_pos)
  406. return AVERROR(ENOMEM);
  407. pkt->nals_allocated = new_size;
  408. }
  409. nal = &pkt->nals[pkt->nb_nals];
  410. consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
  411. if (consumed < 0)
  412. return consumed;
  413. if (is_nalff && (extract_length != consumed) && extract_length)
  414. av_log(logctx, AV_LOG_DEBUG,
  415. "NALFF: Consumed only %d bytes instead of %d\n",
  416. consumed, extract_length);
  417. pkt->nb_nals++;
  418. bytestream2_skip(&bc, consumed);
  419. /* see commit 3566042a0 */
  420. if (bytestream2_get_bytes_left(&bc) >= 4 &&
  421. bytestream2_peek_be32(&bc) == 0x000001E0)
  422. skip_trailing_zeros = 0;
  423. nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
  424. ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
  425. if (ret < 0)
  426. return ret;
  427. if (codec_id == AV_CODEC_ID_HEVC)
  428. ret = hevc_parse_nal_header(nal, logctx);
  429. else
  430. ret = h264_parse_nal_header(nal, logctx);
  431. if (ret < 0 || nal->size <= 0 || nal->size_bits <= 0) {
  432. if (ret < 0) {
  433. av_log(logctx, AV_LOG_WARNING, "Invalid NAL unit %d, skipping.\n",
  434. nal->type);
  435. }
  436. pkt->nb_nals--;
  437. }
  438. }
  439. return 0;
  440. }
  441. void ff_h2645_packet_uninit(H2645Packet *pkt)
  442. {
  443. int i;
  444. for (i = 0; i < pkt->nals_allocated; i++) {
  445. av_freep(&pkt->nals[i].skipped_bytes_pos);
  446. }
  447. av_freep(&pkt->nals);
  448. pkt->nals_allocated = pkt->nal_buffer_size = 0;
  449. if (pkt->rbsp.rbsp_buffer_ref) {
  450. av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref);
  451. pkt->rbsp.rbsp_buffer = NULL;
  452. } else
  453. av_freep(&pkt->rbsp.rbsp_buffer);
  454. pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;
  455. }