postproc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // Postprocessing Decoder
  11. // ======================
  12. //
  13. // This example adds postprocessing to the simple decoder loop.
  14. //
  15. // Initializing Postprocessing
  16. // ---------------------------
  17. // You must inform the codec that you might request postprocessing at
  18. // initialization time. This is done by passing the VPX_CODEC_USE_POSTPROC
  19. // flag to `vpx_codec_dec_init`. If the codec does not support
  20. // postprocessing, this call will return VPX_CODEC_INCAPABLE. For
  21. // demonstration purposes, we also fall back to default initialization if
  22. // the codec does not provide support.
  23. //
  24. // Using Adaptive Postprocessing
  25. // -----------------------------
  26. // VP6 provides "adaptive postprocessing." It will automatically select the
  27. // best postprocessing filter on a frame by frame basis based on the amount
  28. // of time remaining before the user's specified deadline expires. The
  29. // special value 0 indicates that the codec should take as long as
  30. // necessary to provide the best quality frame. This example gives the
  31. // codec 15ms (15000us) to return a frame. Remember that this is a soft
  32. // deadline, and the codec may exceed it doing its regular processing. In
  33. // these cases, no additional postprocessing will be done.
  34. //
  35. // Codec Specific Postprocessing Controls
  36. // --------------------------------------
  37. // Some codecs provide fine grained controls over their built-in
  38. // postprocessors. VP8 is one example. The following sample code toggles
  39. // postprocessing on and off every 15 frames.
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "vpx/vp8dx.h"
  44. #include "vpx/vpx_decoder.h"
  45. #include "../tools_common.h"
  46. #include "../video_reader.h"
  47. #include "./vpx_config.h"
  48. static const char *exec_name;
  49. void usage_exit(void) {
  50. fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
  51. exit(EXIT_FAILURE);
  52. }
  53. int main(int argc, char **argv) {
  54. int frame_cnt = 0;
  55. FILE *outfile = NULL;
  56. vpx_codec_ctx_t codec;
  57. vpx_codec_err_t res;
  58. VpxVideoReader *reader = NULL;
  59. const VpxInterface *decoder = NULL;
  60. const VpxVideoInfo *info = NULL;
  61. exec_name = argv[0];
  62. if (argc != 3) die("Invalid number of arguments.");
  63. reader = vpx_video_reader_open(argv[1]);
  64. if (!reader) die("Failed to open %s for reading.", argv[1]);
  65. if (!(outfile = fopen(argv[2], "wb")))
  66. die("Failed to open %s for writing", argv[2]);
  67. info = vpx_video_reader_get_info(reader);
  68. decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
  69. if (!decoder) die("Unknown input codec.");
  70. printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
  71. res = vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL,
  72. VPX_CODEC_USE_POSTPROC);
  73. if (res == VPX_CODEC_INCAPABLE)
  74. die_codec(&codec, "Postproc not supported by this decoder.");
  75. if (res) die_codec(&codec, "Failed to initialize decoder.");
  76. while (vpx_video_reader_read_frame(reader)) {
  77. vpx_codec_iter_t iter = NULL;
  78. vpx_image_t *img = NULL;
  79. size_t frame_size = 0;
  80. const unsigned char *frame =
  81. vpx_video_reader_get_frame(reader, &frame_size);
  82. ++frame_cnt;
  83. if (frame_cnt % 30 == 1) {
  84. vp8_postproc_cfg_t pp = { 0, 0, 0 };
  85. if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
  86. die_codec(&codec, "Failed to turn off postproc.");
  87. } else if (frame_cnt % 30 == 16) {
  88. vp8_postproc_cfg_t pp = { VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE, 4,
  89. 0 };
  90. if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
  91. die_codec(&codec, "Failed to turn on postproc.");
  92. };
  93. // Decode the frame with 15ms deadline
  94. if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 15000))
  95. die_codec(&codec, "Failed to decode frame");
  96. while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
  97. vpx_img_write(img, outfile);
  98. }
  99. }
  100. printf("Processed %d frames.\n", frame_cnt);
  101. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
  102. printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
  103. info->frame_width, info->frame_height, argv[2]);
  104. vpx_video_reader_close(reader);
  105. fclose(outfile);
  106. return EXIT_SUCCESS;
  107. }