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.

292 lines
9.2KB

  1. /*
  2. * HEVC common 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/intreadwrite.h"
  23. #include "libavutil/mem.h"
  24. #include "hevc.h"
  25. /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
  26. * between these functions would be nice. */
  27. int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
  28. HEVCNAL *nal)
  29. {
  30. int i, si, di;
  31. uint8_t *dst;
  32. if (s)
  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) { \
  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) { // 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 + AV_INPUT_BUFFER_PADDING_SIZE);
  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) {
  98. if (src[si + 2] == 3) { // escape
  99. dst[di++] = 0;
  100. dst[di++] = 0;
  101. si += 3;
  102. if (s && 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. /**
  166. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  167. * 0 if the unit should be skipped, 1 otherwise
  168. */
  169. static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
  170. {
  171. GetBitContext *gb = &nal->gb;
  172. int nuh_layer_id;
  173. if (get_bits1(gb) != 0)
  174. return AVERROR_INVALIDDATA;
  175. nal->type = get_bits(gb, 6);
  176. nuh_layer_id = get_bits(gb, 6);
  177. nal->temporal_id = get_bits(gb, 3) - 1;
  178. if (nal->temporal_id < 0)
  179. return AVERROR_INVALIDDATA;
  180. av_log(avctx, AV_LOG_DEBUG,
  181. "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
  182. nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
  183. return nuh_layer_id == 0;
  184. }
  185. int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length,
  186. AVCodecContext *avctx, int is_nalff, int nal_length_size)
  187. {
  188. int consumed, ret = 0;
  189. pkt->nb_nals = 0;
  190. while (length >= 4) {
  191. HEVCNAL *nal;
  192. int extract_length = 0;
  193. if (is_nalff) {
  194. int i;
  195. for (i = 0; i < nal_length_size; i++)
  196. extract_length = (extract_length << 8) | buf[i];
  197. buf += nal_length_size;
  198. length -= nal_length_size;
  199. if (extract_length > length) {
  200. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  201. return AVERROR_INVALIDDATA;
  202. }
  203. } else {
  204. /* search start code */
  205. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  206. ++buf;
  207. --length;
  208. if (length < 4) {
  209. av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");
  210. return AVERROR_INVALIDDATA;
  211. }
  212. }
  213. buf += 3;
  214. length -= 3;
  215. extract_length = length;
  216. }
  217. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  218. int new_size = pkt->nals_allocated + 1;
  219. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  220. if (!tmp)
  221. return AVERROR(ENOMEM);
  222. pkt->nals = tmp;
  223. memset(pkt->nals + pkt->nals_allocated, 0,
  224. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  225. nal = &pkt->nals[pkt->nb_nals];
  226. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  227. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  228. if (!nal->skipped_bytes_pos)
  229. return AVERROR(ENOMEM);
  230. pkt->nals_allocated = new_size;
  231. }
  232. nal = &pkt->nals[pkt->nb_nals];
  233. consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
  234. if (consumed < 0)
  235. return consumed;
  236. pkt->nb_nals++;
  237. ret = init_get_bits8(&nal->gb, nal->data, nal->size);
  238. if (ret < 0)
  239. return ret;
  240. ret = hls_nal_unit(nal, avctx);
  241. if (ret <= 0) {
  242. if (ret < 0) {
  243. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  244. nal->type);
  245. }
  246. pkt->nb_nals--;
  247. }
  248. buf += consumed;
  249. length -= consumed;
  250. }
  251. return 0;
  252. }