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.

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