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.

146 lines
5.1KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "bytestream.h"
  19. #include "h2645_parse.h"
  20. #include "hevc.h"
  21. #include "hevc_parse.h"
  22. static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets *ps,
  23. HEVCSEI *sei, int is_nalff, int nal_length_size,
  24. int err_recognition, int apply_defdispwin, void *logctx)
  25. {
  26. int i;
  27. int ret = 0;
  28. H2645Packet pkt = { 0 };
  29. ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff,
  30. nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
  31. if (ret < 0) {
  32. goto done;
  33. }
  34. for (i = 0; i < pkt.nb_nals; i++) {
  35. H2645NAL *nal = &pkt.nals[i];
  36. if (nal->nuh_layer_id > 0)
  37. continue;
  38. /* ignore everything except parameter sets and VCL NALUs */
  39. switch (nal->type) {
  40. case HEVC_NAL_VPS:
  41. ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
  42. if (ret < 0)
  43. goto done;
  44. break;
  45. case HEVC_NAL_SPS:
  46. ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
  47. if (ret < 0)
  48. goto done;
  49. break;
  50. case HEVC_NAL_PPS:
  51. ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
  52. if (ret < 0)
  53. goto done;
  54. break;
  55. case HEVC_NAL_SEI_PREFIX:
  56. case HEVC_NAL_SEI_SUFFIX:
  57. ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
  58. if (ret < 0)
  59. goto done;
  60. break;
  61. default:
  62. av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
  63. break;
  64. }
  65. }
  66. done:
  67. ff_h2645_packet_uninit(&pkt);
  68. if (err_recognition & AV_EF_EXPLODE)
  69. return ret;
  70. return 0;
  71. }
  72. int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
  73. HEVCSEI *sei, int *is_nalff, int *nal_length_size,
  74. int err_recognition, int apply_defdispwin, void *logctx)
  75. {
  76. int ret = 0;
  77. GetByteContext gb;
  78. bytestream2_init(&gb, data, size);
  79. if (size > 3 && (data[0] || data[1] || data[2] > 1)) {
  80. /* It seems the extradata is encoded as hvcC format.
  81. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  82. * is finalized. When finalized, configurationVersion will be 1 and we
  83. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  84. int i, j, num_arrays, nal_len_size;
  85. *is_nalff = 1;
  86. bytestream2_skip(&gb, 21);
  87. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  88. num_arrays = bytestream2_get_byte(&gb);
  89. /* nal units in the hvcC always have length coded with 2 bytes,
  90. * so put a fake nal_length_size = 2 while parsing them */
  91. *nal_length_size = 2;
  92. /* Decode nal units from hvcC. */
  93. for (i = 0; i < num_arrays; i++) {
  94. int type = bytestream2_get_byte(&gb) & 0x3f;
  95. int cnt = bytestream2_get_be16(&gb);
  96. for (j = 0; j < cnt; j++) {
  97. // +2 for the nal size field
  98. int nalsize = bytestream2_peek_be16(&gb) + 2;
  99. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  100. av_log(logctx, AV_LOG_ERROR,
  101. "Invalid NAL unit size in extradata.\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
  105. *nal_length_size, err_recognition, apply_defdispwin,
  106. logctx);
  107. if (ret < 0) {
  108. av_log(logctx, AV_LOG_ERROR,
  109. "Decoding nal unit %d %d from hvcC failed\n",
  110. type, i);
  111. return ret;
  112. }
  113. bytestream2_skip(&gb, nalsize);
  114. }
  115. }
  116. /* Now store right nal length size, that will be used to parse
  117. * all other nals */
  118. *nal_length_size = nal_len_size;
  119. } else {
  120. *is_nalff = 0;
  121. ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
  122. err_recognition, apply_defdispwin, logctx);
  123. if (ret < 0)
  124. return ret;
  125. }
  126. return ret;
  127. }