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.

174 lines
5.0KB

  1. /*
  2. * VP9 compatible video decoder
  3. *
  4. * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. * Copyright (C) 2013 Clément Bœsch <u pkh me>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavcodec/get_bits.h"
  25. #include "parser.h"
  26. typedef struct VP9ParseContext {
  27. int n_frames; // 1-8
  28. int size[8];
  29. int marker_size;
  30. int64_t pts;
  31. } VP9ParseContext;
  32. static int parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
  33. {
  34. VP9ParseContext *s = ctx->priv_data;
  35. GetBitContext gb;
  36. int res, profile, keyframe, invisible;
  37. if ((res = init_get_bits8(&gb, buf, size)) < 0)
  38. return res;
  39. get_bits(&gb, 2); // frame marker
  40. profile = get_bits1(&gb);
  41. profile |= get_bits1(&gb) << 1;
  42. if (profile == 3) profile += get_bits1(&gb);
  43. if (get_bits1(&gb)) {
  44. keyframe = 0;
  45. invisible = 0;
  46. } else {
  47. keyframe = !get_bits1(&gb);
  48. invisible = !get_bits1(&gb);
  49. }
  50. if (!keyframe) {
  51. ctx->pict_type = AV_PICTURE_TYPE_P;
  52. ctx->key_frame = 0;
  53. } else {
  54. ctx->pict_type = AV_PICTURE_TYPE_I;
  55. ctx->key_frame = 1;
  56. }
  57. if (!invisible) {
  58. if (ctx->pts == AV_NOPTS_VALUE)
  59. ctx->pts = s->pts;
  60. s->pts = AV_NOPTS_VALUE;
  61. } else if (ctx->pts != AV_NOPTS_VALUE) {
  62. s->pts = ctx->pts;
  63. ctx->pts = AV_NOPTS_VALUE;
  64. }
  65. return 0;
  66. }
  67. static int parse(AVCodecParserContext *ctx,
  68. AVCodecContext *avctx,
  69. const uint8_t **out_data, int *out_size,
  70. const uint8_t *data, int size)
  71. {
  72. VP9ParseContext *s = ctx->priv_data;
  73. int full_size = size;
  74. int marker;
  75. if (size <= 0) {
  76. *out_size = 0;
  77. *out_data = data;
  78. return 0;
  79. }
  80. if (s->n_frames > 0) {
  81. int i;
  82. int size_sum = 0;
  83. for (i = 0; i < s->n_frames ;i++)
  84. size_sum += s->size[i];
  85. size_sum += s->marker_size;
  86. if (size_sum != size) {
  87. av_log(avctx, AV_LOG_ERROR, "Inconsistent input frame sizes %d %d\n",
  88. size_sum, size);
  89. s->n_frames = 0;
  90. }
  91. }
  92. if (s->n_frames > 0) {
  93. *out_data = data;
  94. *out_size = s->size[--s->n_frames];
  95. parse_frame(ctx, *out_data, *out_size);
  96. return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
  97. }
  98. marker = data[size - 1];
  99. if ((marker & 0xe0) == 0xc0) {
  100. int nbytes = 1 + ((marker >> 3) & 0x3);
  101. int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
  102. if (size >= idx_sz && data[size - idx_sz] == marker) {
  103. const uint8_t *idx = data + size + 1 - idx_sz;
  104. int first = 1;
  105. switch (nbytes) {
  106. #define case_n(a, rd) \
  107. case a: \
  108. while (n_frames--) { \
  109. unsigned sz = rd; \
  110. idx += a; \
  111. if (sz == 0 || sz > size) { \
  112. s->n_frames = 0; \
  113. *out_size = size; \
  114. *out_data = data; \
  115. av_log(avctx, AV_LOG_ERROR, \
  116. "Invalid superframe packet size: %u frame size: %d\n", \
  117. sz, size); \
  118. return full_size; \
  119. } \
  120. if (first) { \
  121. first = 0; \
  122. *out_data = data; \
  123. *out_size = sz; \
  124. s->n_frames = n_frames; \
  125. } else { \
  126. s->size[n_frames] = sz; \
  127. } \
  128. data += sz; \
  129. size -= sz; \
  130. } \
  131. s->marker_size = size; \
  132. parse_frame(ctx, *out_data, *out_size); \
  133. return s->n_frames > 0 ? *out_size : full_size
  134. case_n(1, *idx);
  135. case_n(2, AV_RL16(idx));
  136. case_n(3, AV_RL24(idx));
  137. case_n(4, AV_RL32(idx));
  138. }
  139. }
  140. }
  141. *out_data = data;
  142. *out_size = size;
  143. parse_frame(ctx, data, size);
  144. return size;
  145. }
  146. AVCodecParser ff_vp9_parser = {
  147. .codec_ids = { AV_CODEC_ID_VP9 },
  148. .priv_data_size = sizeof(VP9ParseContext),
  149. .parser_parse = parse,
  150. };