vp9_superframe_bsf.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Vp9 invisible (alt-ref) frame to superframe merge bitstream filter
  3. * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avassert.h"
  22. #include "avcodec.h"
  23. #include "bsf.h"
  24. #include "get_bits.h"
  25. #define MAX_CACHE 8
  26. typedef struct VP9BSFContext {
  27. int n_cache;
  28. AVPacket *cache[MAX_CACHE];
  29. } VP9BSFContext;
  30. static void stats(AVPacket * const *in, int n_in,
  31. unsigned *_max, unsigned *_sum)
  32. {
  33. int n;
  34. unsigned max = 0, sum = 0;
  35. for (n = 0; n < n_in; n++) {
  36. unsigned sz = in[n]->size;
  37. if (sz > max)
  38. max = sz;
  39. sum += sz;
  40. }
  41. *_max = max;
  42. *_sum = sum;
  43. }
  44. static int merge_superframe(AVPacket * const *in, int n_in, AVPacket *out)
  45. {
  46. unsigned max, sum, mag, marker, n, sz;
  47. uint8_t *ptr;
  48. int res;
  49. stats(in, n_in, &max, &sum);
  50. mag = av_log2(max) >> 3;
  51. marker = 0xC0 + (mag << 3) + (n_in - 1);
  52. sz = sum + 2 + (mag + 1) * n_in;
  53. res = av_new_packet(out, sz);
  54. if (res < 0)
  55. return res;
  56. ptr = out->data;
  57. for (n = 0; n < n_in; n++) {
  58. memcpy(ptr, in[n]->data, in[n]->size);
  59. ptr += in[n]->size;
  60. }
  61. #define wloop(mag, wr) \
  62. do { \
  63. for (n = 0; n < n_in; n++) { \
  64. wr; \
  65. ptr += mag + 1; \
  66. } \
  67. } while (0)
  68. // write superframe with marker 110[mag:2][nframes:3]
  69. *ptr++ = marker;
  70. switch (mag) {
  71. case 0:
  72. wloop(mag, *ptr = in[n]->size);
  73. break;
  74. case 1:
  75. wloop(mag, AV_WL16(ptr, in[n]->size));
  76. break;
  77. case 2:
  78. wloop(mag, AV_WL24(ptr, in[n]->size));
  79. break;
  80. case 3:
  81. wloop(mag, AV_WL32(ptr, in[n]->size));
  82. break;
  83. }
  84. *ptr++ = marker;
  85. av_assert0(ptr == &out->data[out->size]);
  86. return 0;
  87. }
  88. static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out)
  89. {
  90. GetBitContext gb;
  91. VP9BSFContext *s = ctx->priv_data;
  92. AVPacket *in;
  93. int res, invisible, profile, marker, uses_superframe_syntax = 0, n;
  94. res = ff_bsf_get_packet(ctx, &in);
  95. if (res < 0)
  96. return res;
  97. marker = in->data[in->size - 1];
  98. if ((marker & 0xe0) == 0xc0) {
  99. int nbytes = 1 + ((marker >> 3) & 0x3);
  100. int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
  101. uses_superframe_syntax = in->size >= idx_sz && in->data[in->size - idx_sz] == marker;
  102. }
  103. if ((res = init_get_bits8(&gb, in->data, in->size)) < 0)
  104. goto done;
  105. get_bits(&gb, 2); // frame marker
  106. profile = get_bits1(&gb);
  107. profile |= get_bits1(&gb) << 1;
  108. if (profile == 3) profile += get_bits1(&gb);
  109. if (get_bits1(&gb)) {
  110. invisible = 0;
  111. } else {
  112. get_bits1(&gb); // keyframe
  113. invisible = !get_bits1(&gb);
  114. }
  115. if (uses_superframe_syntax && s->n_cache > 0) {
  116. av_log(ctx, AV_LOG_ERROR,
  117. "Mixing of superframe syntax and naked VP9 frames not supported");
  118. res = AVERROR(ENOSYS);
  119. goto done;
  120. } else if ((!invisible || uses_superframe_syntax) && !s->n_cache) {
  121. // passthrough
  122. av_packet_move_ref(out, in);
  123. goto done;
  124. } else if (s->n_cache + 1 >= MAX_CACHE) {
  125. av_log(ctx, AV_LOG_ERROR,
  126. "Too many invisible frames");
  127. res = AVERROR_INVALIDDATA;
  128. goto done;
  129. }
  130. res = av_packet_ref(s->cache[s->n_cache++], in);
  131. if (res < 0)
  132. goto done;
  133. if (invisible) {
  134. res = AVERROR(EAGAIN);
  135. goto done;
  136. }
  137. av_assert0(s->n_cache > 0);
  138. // build superframe
  139. if ((res = merge_superframe(s->cache, s->n_cache, out)) < 0)
  140. goto done;
  141. res = av_packet_copy_props(out, s->cache[s->n_cache - 1]);
  142. if (res < 0)
  143. goto done;
  144. for (n = 0; n < s->n_cache; n++)
  145. av_packet_unref(s->cache[n]);
  146. s->n_cache = 0;
  147. done:
  148. if (res < 0)
  149. av_packet_unref(out);
  150. av_packet_free(&in);
  151. return res;
  152. }
  153. static int vp9_superframe_init(AVBSFContext *ctx)
  154. {
  155. VP9BSFContext *s = ctx->priv_data;
  156. int n;
  157. // alloc cached data
  158. for (n = 0; n < MAX_CACHE; n++) {
  159. s->cache[n] = av_packet_alloc();
  160. if (!s->cache[n])
  161. return AVERROR(ENOMEM);
  162. }
  163. return 0;
  164. }
  165. static void vp9_superframe_close(AVBSFContext *ctx)
  166. {
  167. VP9BSFContext *s = ctx->priv_data;
  168. int n;
  169. // free cached data
  170. for (n = 0; n < MAX_CACHE; n++)
  171. av_packet_free(&s->cache[n]);
  172. }
  173. static const enum AVCodecID codec_ids[] = {
  174. AV_CODEC_ID_VP9, AV_CODEC_ID_NONE,
  175. };
  176. const AVBitStreamFilter ff_vp9_superframe_bsf = {
  177. .name = "vp9_superframe",
  178. .priv_data_size = sizeof(VP9BSFContext),
  179. .filter = vp9_superframe_filter,
  180. .init = vp9_superframe_init,
  181. .close = vp9_superframe_close,
  182. .codec_ids = codec_ids,
  183. };