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.

408 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avassert.h"
  19. #include "libavutil/intmath.h"
  20. #include "libavutil/log.h"
  21. #include "libavutil/mem.h"
  22. #include "libavutil/opt.h"
  23. #include "bitstream.h"
  24. #include "bsf.h"
  25. #include "put_bits.h"
  26. #define FRAME_SLOTS 8
  27. typedef struct VP9RawReorderFrame {
  28. AVPacket *packet;
  29. int needs_output;
  30. int needs_display;
  31. int64_t pts;
  32. int64_t sequence;
  33. unsigned int slots;
  34. unsigned int profile;
  35. unsigned int show_existing_frame;
  36. unsigned int frame_to_show;
  37. unsigned int frame_type;
  38. unsigned int show_frame;
  39. unsigned int refresh_frame_flags;
  40. } VP9RawReorderFrame;
  41. typedef struct VP9RawReorderContext {
  42. int64_t sequence;
  43. VP9RawReorderFrame *slot[FRAME_SLOTS];
  44. VP9RawReorderFrame *next_frame;
  45. } VP9RawReorderContext;
  46. static void vp9_raw_reorder_frame_free(VP9RawReorderFrame **frame)
  47. {
  48. if (*frame)
  49. av_packet_free(&(*frame)->packet);
  50. av_freep(frame);
  51. }
  52. static void vp9_raw_reorder_clear_slot(VP9RawReorderContext *ctx, int s)
  53. {
  54. if (ctx->slot[s]) {
  55. ctx->slot[s]->slots &= ~(1 << s);
  56. if (ctx->slot[s]->slots == 0)
  57. vp9_raw_reorder_frame_free(&ctx->slot[s]);
  58. else
  59. ctx->slot[s] = NULL;
  60. }
  61. }
  62. static int vp9_raw_reorder_frame_parse(AVBSFContext *bsf, VP9RawReorderFrame *frame)
  63. {
  64. BitstreamContext bc;
  65. int err;
  66. unsigned int frame_marker;
  67. unsigned int profile_low_bit, profile_high_bit, reserved_zero;
  68. unsigned int error_resilient_mode;
  69. unsigned int frame_sync_code;
  70. err = bitstream_init8(&bc, frame->packet->data, frame->packet->size);
  71. if (err)
  72. return err;
  73. frame_marker = bitstream_read(&bc, 2);
  74. if (frame_marker != 2) {
  75. av_log(bsf, AV_LOG_ERROR, "Invalid frame marker: %u.\n",
  76. frame_marker);
  77. return AVERROR_INVALIDDATA;
  78. }
  79. profile_low_bit = bitstream_read_bit(&bc);
  80. profile_high_bit = bitstream_read_bit(&bc);
  81. frame->profile = (profile_high_bit << 1) | profile_low_bit;
  82. if (frame->profile == 3) {
  83. reserved_zero = bitstream_read_bit(&bc);
  84. if (reserved_zero != 0) {
  85. av_log(bsf, AV_LOG_ERROR, "Profile reserved_zero bit set: "
  86. "unsupported profile or invalid bitstream.\n");
  87. return AVERROR_INVALIDDATA;
  88. }
  89. }
  90. frame->show_existing_frame = bitstream_read_bit(&bc);
  91. if (frame->show_existing_frame) {
  92. frame->frame_to_show = bitstream_read(&bc, 3);
  93. return 0;
  94. }
  95. frame->frame_type = bitstream_read_bit(&bc);
  96. frame->show_frame = bitstream_read_bit(&bc);
  97. error_resilient_mode = bitstream_read_bit(&bc);
  98. if (frame->frame_type == 0) {
  99. frame_sync_code = bitstream_read(&bc, 24);
  100. if (frame_sync_code != 0x498342) {
  101. av_log(bsf, AV_LOG_ERROR, "Invalid frame sync code: %06x.\n",
  102. frame_sync_code);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. frame->refresh_frame_flags = 0xff;
  106. } else {
  107. unsigned int intra_only;
  108. if (frame->show_frame == 0)
  109. intra_only = bitstream_read_bit(&bc);
  110. else
  111. intra_only = 0;
  112. if (error_resilient_mode == 0) {
  113. // reset_frame_context
  114. bitstream_skip(&bc, 2);
  115. }
  116. if (intra_only) {
  117. frame_sync_code = bitstream_read(&bc, 24);
  118. if (frame_sync_code != 0x498342) {
  119. av_log(bsf, AV_LOG_ERROR, "Invalid frame sync code: "
  120. "%06x.\n", frame_sync_code);
  121. return AVERROR_INVALIDDATA;
  122. }
  123. if (frame->profile > 0) {
  124. unsigned int color_space;
  125. if (frame->profile >= 2) {
  126. // ten_or_twelve_bit
  127. bitstream_skip(&bc, 1);
  128. }
  129. color_space = bitstream_read(&bc, 3);
  130. if (color_space != 7 /* CS_RGB */) {
  131. // color_range
  132. bitstream_skip(&bc, 1);
  133. if (frame->profile == 1 || frame->profile == 3) {
  134. // subsampling
  135. bitstream_skip(&bc, 3);
  136. }
  137. } else {
  138. if (frame->profile == 1 || frame->profile == 3)
  139. bitstream_skip(&bc, 1);
  140. }
  141. }
  142. frame->refresh_frame_flags = bitstream_read(&bc, 8);
  143. } else {
  144. frame->refresh_frame_flags = bitstream_read(&bc, 8);
  145. }
  146. }
  147. return 0;
  148. }
  149. static int vp9_raw_reorder_make_output(AVBSFContext *bsf,
  150. AVPacket *out,
  151. VP9RawReorderFrame *last_frame)
  152. {
  153. VP9RawReorderContext *ctx = bsf->priv_data;
  154. VP9RawReorderFrame *next_output = last_frame,
  155. *next_display = last_frame, *frame;
  156. int s, err;
  157. for (s = 0; s < FRAME_SLOTS; s++) {
  158. frame = ctx->slot[s];
  159. if (!frame)
  160. continue;
  161. if (frame->needs_output && (!next_output ||
  162. frame->sequence < next_output->sequence))
  163. next_output = frame;
  164. if (frame->needs_display && (!next_display ||
  165. frame->pts < next_display->pts))
  166. next_display = frame;
  167. }
  168. if (!next_output && !next_display)
  169. return AVERROR_EOF;
  170. if (!next_display || (next_output &&
  171. next_output->sequence < next_display->sequence))
  172. frame = next_output;
  173. else
  174. frame = next_display;
  175. if (frame->needs_output && frame->needs_display &&
  176. next_output == next_display) {
  177. av_log(bsf, AV_LOG_DEBUG, "Output and display frame "
  178. "%"PRId64" (%"PRId64") in order.\n",
  179. frame->sequence, frame->pts);
  180. av_packet_move_ref(out, frame->packet);
  181. frame->needs_output = frame->needs_display = 0;
  182. } else if (frame->needs_output) {
  183. if (frame->needs_display) {
  184. av_log(bsf, AV_LOG_DEBUG, "Output frame %"PRId64" "
  185. "(%"PRId64") for later display.\n",
  186. frame->sequence, frame->pts);
  187. } else {
  188. av_log(bsf, AV_LOG_DEBUG, "Output unshown frame "
  189. "%"PRId64" (%"PRId64") to keep order.\n",
  190. frame->sequence, frame->pts);
  191. }
  192. av_packet_move_ref(out, frame->packet);
  193. out->pts = out->dts;
  194. frame->needs_output = 0;
  195. } else {
  196. PutBitContext pb;
  197. av_assert0(!frame->needs_output && frame->needs_display);
  198. if (frame->slots == 0) {
  199. av_log(bsf, AV_LOG_ERROR, "Attempting to display frame "
  200. "which is no longer available?\n");
  201. frame->needs_display = 0;
  202. return AVERROR_INVALIDDATA;
  203. }
  204. s = ff_ctz(frame->slots);
  205. av_assert0(s < FRAME_SLOTS);
  206. av_log(bsf, AV_LOG_DEBUG, "Display frame %"PRId64" "
  207. "(%"PRId64") from slot %d.\n",
  208. frame->sequence, frame->pts, s);
  209. err = av_new_packet(out, 2);
  210. if (err < 0)
  211. return err;
  212. init_put_bits(&pb, out->data, 2);
  213. // frame_marker
  214. put_bits(&pb, 2, 2);
  215. // profile_low_bit
  216. put_bits(&pb, 1, frame->profile & 1);
  217. // profile_high_bit
  218. put_bits(&pb, 1, (frame->profile >> 1) & 1);
  219. if (frame->profile == 3) {
  220. // reserved_zero
  221. put_bits(&pb, 1, 0);
  222. }
  223. // show_existing_frame
  224. put_bits(&pb, 1, 1);
  225. // frame_to_show_map_idx
  226. put_bits(&pb, 3, s);
  227. while (put_bits_count(&pb) < 16)
  228. put_bits(&pb, 1, 0);
  229. flush_put_bits(&pb);
  230. out->pts = out->dts = frame->pts;
  231. frame->needs_display = 0;
  232. }
  233. return 0;
  234. }
  235. static int vp9_raw_reorder_filter(AVBSFContext *bsf, AVPacket *out)
  236. {
  237. VP9RawReorderContext *ctx = bsf->priv_data;
  238. VP9RawReorderFrame *frame;
  239. AVPacket *in;
  240. int err, s;
  241. if (ctx->next_frame) {
  242. frame = ctx->next_frame;
  243. } else {
  244. err = ff_bsf_get_packet(bsf, &in);
  245. if (err < 0) {
  246. if (err == AVERROR_EOF)
  247. return vp9_raw_reorder_make_output(bsf, out, NULL);
  248. return err;
  249. }
  250. if (in->data[in->size - 1] & 0xe0 == 0xc0) {
  251. av_log(bsf, AV_LOG_ERROR, "Input in superframes is not "
  252. "supported.\n");
  253. av_packet_free(&in);
  254. return AVERROR(ENOSYS);
  255. }
  256. frame = av_mallocz(sizeof(*frame));
  257. if (!frame) {
  258. av_packet_free(&in);
  259. return AVERROR(ENOMEM);
  260. }
  261. frame->packet = in;
  262. frame->pts = in->pts;
  263. frame->sequence = ++ctx->sequence;
  264. err = vp9_raw_reorder_frame_parse(bsf, frame);
  265. if (err) {
  266. av_log(bsf, AV_LOG_ERROR, "Failed to parse input "
  267. "frame: %d.\n", err);
  268. goto fail;
  269. }
  270. frame->needs_output = 1;
  271. frame->needs_display = frame->pts != AV_NOPTS_VALUE;
  272. if (frame->show_existing_frame)
  273. av_log(bsf, AV_LOG_DEBUG, "Show frame %"PRId64" "
  274. "(%"PRId64"): show %u.\n", frame->sequence,
  275. frame->pts, frame->frame_to_show);
  276. else
  277. av_log(bsf, AV_LOG_DEBUG, "New frame %"PRId64" "
  278. "(%"PRId64"): type %u show %u refresh %02x.\n",
  279. frame->sequence, frame->pts, frame->frame_type,
  280. frame->show_frame, frame->refresh_frame_flags);
  281. ctx->next_frame = frame;
  282. }
  283. for (s = 0; s < FRAME_SLOTS; s++) {
  284. if (!(frame->refresh_frame_flags & (1 << s)))
  285. continue;
  286. if (ctx->slot[s] && ctx->slot[s]->needs_display &&
  287. ctx->slot[s]->slots == (1 << s)) {
  288. // We are overwriting this slot, which is last reference
  289. // to the frame previously present in it. In order to be
  290. // a valid stream, that frame must already have been
  291. // displayed before the pts of the current frame.
  292. err = vp9_raw_reorder_make_output(bsf, out, ctx->slot[s]);
  293. if (err < 0) {
  294. av_log(bsf, AV_LOG_ERROR, "Failed to create "
  295. "output overwriting slot %d: %d.\n",
  296. s, err);
  297. // Clear the slot anyway, so we don't end up
  298. // in an infinite loop.
  299. vp9_raw_reorder_clear_slot(ctx, s);
  300. return AVERROR_INVALIDDATA;
  301. }
  302. return 0;
  303. }
  304. vp9_raw_reorder_clear_slot(ctx, s);
  305. }
  306. for (s = 0; s < FRAME_SLOTS; s++) {
  307. if (!(frame->refresh_frame_flags & (1 << s)))
  308. continue;
  309. ctx->slot[s] = frame;
  310. }
  311. frame->slots = frame->refresh_frame_flags;
  312. if (!frame->refresh_frame_flags) {
  313. err = vp9_raw_reorder_make_output(bsf, out, frame);
  314. if (err < 0) {
  315. av_log(bsf, AV_LOG_ERROR, "Failed to create output "
  316. "for transient frame.\n");
  317. ctx->next_frame = NULL;
  318. return AVERROR_INVALIDDATA;
  319. }
  320. if (!frame->needs_display) {
  321. vp9_raw_reorder_frame_free(&frame);
  322. ctx->next_frame = NULL;
  323. }
  324. return 0;
  325. }
  326. ctx->next_frame = NULL;
  327. return AVERROR(EAGAIN);
  328. fail:
  329. vp9_raw_reorder_frame_free(&frame);
  330. return err;
  331. }
  332. static void vp9_raw_reorder_close(AVBSFContext *bsf)
  333. {
  334. VP9RawReorderContext *ctx = bsf->priv_data;
  335. int s;
  336. for (s = 0; s < FRAME_SLOTS; s++)
  337. vp9_raw_reorder_clear_slot(ctx, s);
  338. }
  339. static const enum AVCodecID vp9_raw_reorder_codec_ids[] = {
  340. AV_CODEC_ID_VP9, AV_CODEC_ID_NONE,
  341. };
  342. const AVBitStreamFilter ff_vp9_raw_reorder_bsf = {
  343. .name = "vp9_raw_reorder",
  344. .priv_data_size = sizeof(VP9RawReorderContext),
  345. .close = &vp9_raw_reorder_close,
  346. .filter = &vp9_raw_reorder_filter,
  347. .codec_ids = vp9_raw_reorder_codec_ids,
  348. };