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.

125 lines
3.7KB

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