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.

297 lines
9.4KB

  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(const uint8_t *src, int length,
  28. HEVCNAL *nal)
  29. {
  30. int i, si, di;
  31. uint8_t *dst;
  32. nal->skipped_bytes = 0;
  33. #define STARTCODE_TEST \
  34. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
  35. if (src[i + 2] != 3) { \
  36. /* startcode, so we must be past the end */ \
  37. length = i; \
  38. } \
  39. break; \
  40. }
  41. #if HAVE_FAST_UNALIGNED
  42. #define FIND_FIRST_ZERO \
  43. if (i > 0 && !src[i]) \
  44. i--; \
  45. while (src[i]) \
  46. i++
  47. #if HAVE_FAST_64BIT
  48. for (i = 0; i + 1 < length; i += 9) {
  49. if (!((~AV_RN64A(src + i) &
  50. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  51. 0x8000800080008080ULL))
  52. continue;
  53. FIND_FIRST_ZERO;
  54. STARTCODE_TEST;
  55. i -= 7;
  56. }
  57. #else
  58. for (i = 0; i + 1 < length; i += 5) {
  59. if (!((~AV_RN32A(src + i) &
  60. (AV_RN32A(src + i) - 0x01000101U)) &
  61. 0x80008080U))
  62. continue;
  63. FIND_FIRST_ZERO;
  64. STARTCODE_TEST;
  65. i -= 3;
  66. }
  67. #endif /* HAVE_FAST_64BIT */
  68. #else
  69. for (i = 0; i + 1 < length; i += 2) {
  70. if (src[i])
  71. continue;
  72. if (i > 0 && src[i - 1] == 0)
  73. i--;
  74. STARTCODE_TEST;
  75. }
  76. #endif /* HAVE_FAST_UNALIGNED */
  77. if (i >= length - 1) { // no escaped 0
  78. nal->data =
  79. nal->raw_data = src;
  80. nal->size =
  81. nal->raw_size = length;
  82. return length;
  83. }
  84. av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
  85. length + AV_INPUT_BUFFER_PADDING_SIZE);
  86. if (!nal->rbsp_buffer)
  87. return AVERROR(ENOMEM);
  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) {
  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. return si;
  132. }
  133. static const char *nal_unit_name(int nal_type)
  134. {
  135. switch(nal_type) {
  136. case NAL_TRAIL_N : return "TRAIL_N";
  137. case NAL_TRAIL_R : return "TRAIL_R";
  138. case NAL_TSA_N : return "TSA_N";
  139. case NAL_TSA_R : return "TSA_R";
  140. case NAL_STSA_N : return "STSA_N";
  141. case NAL_STSA_R : return "STSA_R";
  142. case NAL_RADL_N : return "RADL_N";
  143. case NAL_RADL_R : return "RADL_R";
  144. case NAL_RASL_N : return "RASL_N";
  145. case NAL_RASL_R : return "RASL_R";
  146. case NAL_BLA_W_LP : return "BLA_W_LP";
  147. case NAL_BLA_W_RADL : return "BLA_W_RADL";
  148. case NAL_BLA_N_LP : return "BLA_N_LP";
  149. case NAL_IDR_W_RADL : return "IDR_W_RADL";
  150. case NAL_IDR_N_LP : return "IDR_N_LP";
  151. case NAL_CRA_NUT : return "CRA_NUT";
  152. case NAL_VPS : return "VPS";
  153. case NAL_SPS : return "SPS";
  154. case NAL_PPS : return "PPS";
  155. case NAL_AUD : return "AUD";
  156. case NAL_EOS_NUT : return "EOS_NUT";
  157. case NAL_EOB_NUT : return "EOB_NUT";
  158. case NAL_FD_NUT : return "FD_NUT";
  159. case NAL_SEI_PREFIX : return "SEI_PREFIX";
  160. case NAL_SEI_SUFFIX : return "SEI_SUFFIX";
  161. default : return "?";
  162. }
  163. }
  164. /**
  165. * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
  166. * 0 if the unit should be skipped, 1 otherwise
  167. */
  168. static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
  169. {
  170. GetBitContext *gb = &nal->gb;
  171. int nuh_layer_id;
  172. if (get_bits1(gb) != 0)
  173. return AVERROR_INVALIDDATA;
  174. nal->type = get_bits(gb, 6);
  175. nuh_layer_id = get_bits(gb, 6);
  176. nal->temporal_id = get_bits(gb, 3) - 1;
  177. if (nal->temporal_id < 0)
  178. return AVERROR_INVALIDDATA;
  179. av_log(avctx, AV_LOG_DEBUG,
  180. "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
  181. nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
  182. return nuh_layer_id == 0;
  183. }
  184. int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
  185. AVCodecContext *avctx, int is_nalff, int nal_length_size)
  186. {
  187. int consumed, ret = 0;
  188. pkt->nb_nals = 0;
  189. while (length >= 4) {
  190. HEVCNAL *nal;
  191. int extract_length = 0;
  192. if (is_nalff) {
  193. int i;
  194. for (i = 0; i < nal_length_size; i++)
  195. extract_length = (extract_length << 8) | buf[i];
  196. buf += nal_length_size;
  197. length -= nal_length_size;
  198. if (extract_length > length) {
  199. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
  200. return AVERROR_INVALIDDATA;
  201. }
  202. } else {
  203. /* search start code */
  204. while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
  205. ++buf;
  206. --length;
  207. if (length < 4) {
  208. if (pkt->nb_nals > 0) {
  209. // No more start codes: we discarded some irrelevant
  210. // bytes at the end of the packet.
  211. return 0;
  212. } else {
  213. av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");
  214. return AVERROR_INVALIDDATA;
  215. }
  216. }
  217. }
  218. buf += 3;
  219. length -= 3;
  220. extract_length = length;
  221. }
  222. if (pkt->nals_allocated < pkt->nb_nals + 1) {
  223. int new_size = pkt->nals_allocated + 1;
  224. void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
  225. if (!tmp)
  226. return AVERROR(ENOMEM);
  227. pkt->nals = tmp;
  228. memset(pkt->nals + pkt->nals_allocated, 0,
  229. (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
  230. nal = &pkt->nals[pkt->nb_nals];
  231. nal->skipped_bytes_pos_size = 1024; // initial buffer size
  232. nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
  233. if (!nal->skipped_bytes_pos)
  234. return AVERROR(ENOMEM);
  235. pkt->nals_allocated = new_size;
  236. }
  237. nal = &pkt->nals[pkt->nb_nals];
  238. consumed = ff_hevc_extract_rbsp(buf, extract_length, nal);
  239. if (consumed < 0)
  240. return consumed;
  241. pkt->nb_nals++;
  242. ret = init_get_bits8(&nal->gb, nal->data, nal->size);
  243. if (ret < 0)
  244. return ret;
  245. ret = hls_nal_unit(nal, avctx);
  246. if (ret <= 0) {
  247. if (ret < 0) {
  248. av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
  249. nal->type);
  250. }
  251. pkt->nb_nals--;
  252. }
  253. buf += consumed;
  254. length -= consumed;
  255. }
  256. return 0;
  257. }