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.

119 lines
3.5KB

  1. /*
  2. * Copyright (C) 2008 Michael Niedermayer
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intreadwrite.h"
  21. #include "parser.h"
  22. typedef struct VP9ParseContext {
  23. int n_frames; // 1-8
  24. int size[8];
  25. } VP9ParseContext;
  26. static void parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
  27. {
  28. if (buf[0] & 0x4) {
  29. ctx->pict_type = AV_PICTURE_TYPE_P;
  30. ctx->key_frame = 0;
  31. } else {
  32. ctx->pict_type = AV_PICTURE_TYPE_I;
  33. ctx->key_frame = 1;
  34. }
  35. }
  36. static int parse(AVCodecParserContext *ctx,
  37. AVCodecContext *avctx,
  38. const uint8_t **out_data, int *out_size,
  39. const uint8_t *data, int size)
  40. {
  41. VP9ParseContext *s = ctx->priv_data;
  42. int marker;
  43. if (size <= 0) {
  44. *out_size = 0;
  45. *out_data = data;
  46. return 0;
  47. }
  48. if (s->n_frames > 0) {
  49. *out_data = data;
  50. *out_size = s->size[--s->n_frames];
  51. parse_frame(ctx, *out_data, *out_size);
  52. return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
  53. }
  54. marker = data[size - 1];
  55. if ((marker & 0xe0) == 0xc0) {
  56. int nbytes = 1 + ((marker >> 3) & 0x3);
  57. int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
  58. if (size >= idx_sz && data[size - idx_sz] == marker) {
  59. const uint8_t *idx = data + size + 1 - idx_sz;
  60. int first = 1;
  61. switch (nbytes) {
  62. #define case_n(a, rd) \
  63. case a: \
  64. while (n_frames--) { \
  65. unsigned sz = rd; \
  66. idx += a; \
  67. if (sz > size) { \
  68. s->n_frames = 0; \
  69. av_log(avctx, AV_LOG_ERROR, \
  70. "Superframe packet size too big: %u > %d\n", \
  71. sz, size); \
  72. return size; \
  73. } \
  74. if (first) { \
  75. first = 0; \
  76. *out_data = data; \
  77. *out_size = sz; \
  78. s->n_frames = n_frames; \
  79. } else { \
  80. s->size[n_frames] = sz; \
  81. } \
  82. data += sz; \
  83. size -= sz; \
  84. } \
  85. parse_frame(ctx, *out_data, *out_size); \
  86. return *out_size
  87. case_n(1, *idx);
  88. case_n(2, AV_RL16(idx));
  89. case_n(3, AV_RL24(idx));
  90. case_n(4, AV_RL32(idx));
  91. }
  92. }
  93. }
  94. *out_data = data;
  95. *out_size = size;
  96. parse_frame(ctx, data, size);
  97. return size;
  98. }
  99. AVCodecParser ff_vp9_parser = {
  100. .codec_ids = { AV_CODEC_ID_VP9 },
  101. .priv_data_size = sizeof(VP9ParseContext),
  102. .parser_parse = parse,
  103. };