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.

134 lines
3.9KB

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