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.

157 lines
4.5KB

  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. int64_t pts;
  30. } VP9ParseContext;
  31. static int parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
  32. {
  33. VP9ParseContext *s = ctx->priv_data;
  34. GetBitContext gb;
  35. int res, profile, keyframe, invisible;
  36. if ((res = init_get_bits8(&gb, buf, size)) < 0)
  37. return res;
  38. get_bits(&gb, 2); // frame marker
  39. profile = get_bits1(&gb);
  40. profile |= get_bits1(&gb) << 1;
  41. if (profile == 3) profile += get_bits1(&gb);
  42. if (get_bits1(&gb)) {
  43. keyframe = 0;
  44. invisible = 0;
  45. } else {
  46. keyframe = !get_bits1(&gb);
  47. invisible = !get_bits1(&gb);
  48. }
  49. if (!keyframe) {
  50. ctx->pict_type = AV_PICTURE_TYPE_P;
  51. ctx->key_frame = 0;
  52. } else {
  53. ctx->pict_type = AV_PICTURE_TYPE_I;
  54. ctx->key_frame = 1;
  55. }
  56. if (!invisible) {
  57. if (ctx->pts == AV_NOPTS_VALUE)
  58. ctx->pts = s->pts;
  59. s->pts = AV_NOPTS_VALUE;
  60. } else {
  61. s->pts = ctx->pts;
  62. ctx->pts = AV_NOPTS_VALUE;
  63. }
  64. return 0;
  65. }
  66. static int parse(AVCodecParserContext *ctx,
  67. AVCodecContext *avctx,
  68. const uint8_t **out_data, int *out_size,
  69. const uint8_t *data, int size)
  70. {
  71. VP9ParseContext *s = ctx->priv_data;
  72. int full_size = size;
  73. int marker;
  74. if (size <= 0) {
  75. *out_size = 0;
  76. *out_data = data;
  77. return 0;
  78. }
  79. if (s->n_frames > 0) {
  80. *out_data = data;
  81. *out_size = s->size[--s->n_frames];
  82. parse_frame(ctx, *out_data, *out_size);
  83. return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
  84. }
  85. marker = data[size - 1];
  86. if ((marker & 0xe0) == 0xc0) {
  87. int nbytes = 1 + ((marker >> 3) & 0x3);
  88. int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
  89. if (size >= idx_sz && data[size - idx_sz] == marker) {
  90. const uint8_t *idx = data + size + 1 - idx_sz;
  91. int first = 1;
  92. switch (nbytes) {
  93. #define case_n(a, rd) \
  94. case a: \
  95. while (n_frames--) { \
  96. unsigned sz = rd; \
  97. idx += a; \
  98. if (sz > size) { \
  99. s->n_frames = 0; \
  100. *out_size = size; \
  101. *out_data = data; \
  102. av_log(avctx, AV_LOG_ERROR, \
  103. "Superframe packet size too big: %u > %d\n", \
  104. sz, size); \
  105. return full_size; \
  106. } \
  107. if (first) { \
  108. first = 0; \
  109. *out_data = data; \
  110. *out_size = sz; \
  111. s->n_frames = n_frames; \
  112. } else { \
  113. s->size[n_frames] = sz; \
  114. } \
  115. data += sz; \
  116. size -= sz; \
  117. } \
  118. parse_frame(ctx, *out_data, *out_size); \
  119. return *out_size
  120. case_n(1, *idx);
  121. case_n(2, AV_RL16(idx));
  122. case_n(3, AV_RL24(idx));
  123. case_n(4, AV_RL32(idx));
  124. }
  125. }
  126. }
  127. *out_data = data;
  128. *out_size = size;
  129. parse_frame(ctx, data, size);
  130. return size;
  131. }
  132. AVCodecParser ff_vp9_parser = {
  133. .codec_ids = { AV_CODEC_ID_VP9 },
  134. .priv_data_size = sizeof(VP9ParseContext),
  135. .parser_parse = parse,
  136. };