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.

143 lines
5.0KB

  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, nal_length_size, AV_CODEC_ID_HEVC, 1);
  30. if (ret < 0) {
  31. goto done;
  32. }
  33. for (i = 0; i < pkt.nb_nals; i++) {
  34. H2645NAL *nal = &pkt.nals[i];
  35. /* ignore everything except parameter sets and VCL NALUs */
  36. switch (nal->type) {
  37. case HEVC_NAL_VPS:
  38. ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
  39. if (ret < 0)
  40. goto done;
  41. break;
  42. case HEVC_NAL_SPS:
  43. ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
  44. if (ret < 0)
  45. goto done;
  46. break;
  47. case HEVC_NAL_PPS:
  48. ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
  49. if (ret < 0)
  50. goto done;
  51. break;
  52. case HEVC_NAL_SEI_PREFIX:
  53. case HEVC_NAL_SEI_SUFFIX:
  54. ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
  55. if (ret < 0)
  56. goto done;
  57. break;
  58. default:
  59. av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
  60. break;
  61. }
  62. }
  63. done:
  64. ff_h2645_packet_uninit(&pkt);
  65. if (err_recognition & AV_EF_EXPLODE)
  66. return ret;
  67. return 0;
  68. }
  69. int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
  70. HEVCSEI *sei, int *is_nalff, int *nal_length_size,
  71. int err_recognition, int apply_defdispwin, void *logctx)
  72. {
  73. int ret = 0;
  74. GetByteContext gb;
  75. bytestream2_init(&gb, data, size);
  76. if (size > 3 && (data[0] || data[1] || data[2] > 1)) {
  77. /* It seems the extradata is encoded as hvcC format.
  78. * Temporarily, we support configurationVersion==0 until 14496-15 3rd
  79. * is finalized. When finalized, configurationVersion will be 1 and we
  80. * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
  81. int i, j, num_arrays, nal_len_size;
  82. *is_nalff = 1;
  83. bytestream2_skip(&gb, 21);
  84. nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
  85. num_arrays = bytestream2_get_byte(&gb);
  86. /* nal units in the hvcC always have length coded with 2 bytes,
  87. * so put a fake nal_length_size = 2 while parsing them */
  88. *nal_length_size = 2;
  89. /* Decode nal units from hvcC. */
  90. for (i = 0; i < num_arrays; i++) {
  91. int type = bytestream2_get_byte(&gb) & 0x3f;
  92. int cnt = bytestream2_get_be16(&gb);
  93. for (j = 0; j < cnt; j++) {
  94. // +2 for the nal size field
  95. int nalsize = bytestream2_peek_be16(&gb) + 2;
  96. if (bytestream2_get_bytes_left(&gb) < nalsize) {
  97. av_log(logctx, AV_LOG_ERROR,
  98. "Invalid NAL unit size in extradata.\n");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
  102. *nal_length_size, err_recognition, apply_defdispwin,
  103. logctx);
  104. if (ret < 0) {
  105. av_log(logctx, AV_LOG_ERROR,
  106. "Decoding nal unit %d %d from hvcC failed\n",
  107. type, i);
  108. return ret;
  109. }
  110. bytestream2_skip(&gb, nalsize);
  111. }
  112. }
  113. /* Now store right nal length size, that will be used to parse
  114. * all other nals */
  115. *nal_length_size = nal_len_size;
  116. } else {
  117. *is_nalff = 0;
  118. ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
  119. err_recognition, apply_defdispwin, logctx);
  120. if (ret < 0)
  121. return ret;
  122. }
  123. return ret;
  124. }