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.

126 lines
3.9KB

  1. /*
  2. * HEVC Annex B format parser
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/common.h"
  23. #include "parser.h"
  24. #include "hevc.h"
  25. #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
  26. /**
  27. * Find the end of the current frame in the bitstream.
  28. * @return the position of the first byte of the next frame, or END_NOT_FOUND
  29. */
  30. static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
  31. int buf_size)
  32. {
  33. int i;
  34. ParseContext *pc = s->priv_data;
  35. for (i = 0; i < buf_size; i++) {
  36. int nut;
  37. pc->state64 = (pc->state64 << 8) | buf[i];
  38. if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
  39. continue;
  40. nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
  41. // Beginning of access unit
  42. if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
  43. (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
  44. if (pc->frame_start_found) {
  45. pc->frame_start_found = 0;
  46. return i - 5;
  47. }
  48. } else if (nut <= NAL_RASL_R ||
  49. (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
  50. int first_slice_segment_in_pic_flag = buf[i] >> 7;
  51. if (first_slice_segment_in_pic_flag) {
  52. if (!pc->frame_start_found) {
  53. pc->frame_start_found = 1;
  54. s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
  55. } else { // First slice of next frame found
  56. pc->frame_start_found = 0;
  57. return i - 5;
  58. }
  59. }
  60. }
  61. }
  62. return END_NOT_FOUND;
  63. }
  64. static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
  65. const uint8_t **poutbuf, int *poutbuf_size,
  66. const uint8_t *buf, int buf_size)
  67. {
  68. int next;
  69. ParseContext *pc = s->priv_data;
  70. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  71. next = buf_size;
  72. } else {
  73. next = hevc_find_frame_end(s, buf, buf_size);
  74. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  75. *poutbuf = NULL;
  76. *poutbuf_size = 0;
  77. return buf_size;
  78. }
  79. }
  80. *poutbuf = buf;
  81. *poutbuf_size = buf_size;
  82. return next;
  83. }
  84. // Split after the parameter sets at the beginning of the stream if they exist.
  85. static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  86. {
  87. int i;
  88. uint32_t state = -1;
  89. int has_ps = 0;
  90. for (i = 0; i < buf_size; i++) {
  91. state = (state << 8) | buf[i];
  92. if (((state >> 8) & 0xFFFFFF) == START_CODE) {
  93. int nut = (state >> 1) & 0x3F;
  94. if (nut >= NAL_VPS && nut <= NAL_PPS)
  95. has_ps = 1;
  96. else if (has_ps)
  97. return i - 3;
  98. else // no parameter set at the beginning of the stream
  99. return 0;
  100. }
  101. }
  102. return 0;
  103. }
  104. AVCodecParser ff_hevc_parser = {
  105. .codec_ids = { AV_CODEC_ID_HEVC },
  106. .priv_data_size = sizeof(ParseContext),
  107. .parser_parse = hevc_parse,
  108. .parser_close = ff_parse_close,
  109. .split = hevc_split,
  110. };