vp9_lossless_encoder.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2014 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. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "vpx/vpx_encoder.h"
  14. #include "vpx/vp8cx.h"
  15. #include "vp9/common/vp9_common.h"
  16. #include "../tools_common.h"
  17. #include "../video_writer.h"
  18. static const char *exec_name;
  19. void usage_exit(void) {
  20. fprintf(stderr,
  21. "vp9_lossless_encoder: Example demonstrating VP9 lossless "
  22. "encoding feature. Supports raw input only.\n");
  23. fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
  24. exit(EXIT_FAILURE);
  25. }
  26. static int encode_frame(vpx_codec_ctx_t *codec, vpx_image_t *img,
  27. int frame_index, int flags, VpxVideoWriter *writer) {
  28. int got_pkts = 0;
  29. vpx_codec_iter_t iter = NULL;
  30. const vpx_codec_cx_pkt_t *pkt = NULL;
  31. const vpx_codec_err_t res =
  32. vpx_codec_encode(codec, img, frame_index, 1, flags, VPX_DL_GOOD_QUALITY);
  33. if (res != VPX_CODEC_OK) die_codec(codec, "Failed to encode frame");
  34. while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
  35. got_pkts = 1;
  36. if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
  37. const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
  38. if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
  39. pkt->data.frame.sz,
  40. pkt->data.frame.pts)) {
  41. die_codec(codec, "Failed to write compressed frame");
  42. }
  43. printf(keyframe ? "K" : ".");
  44. fflush(stdout);
  45. }
  46. }
  47. return got_pkts;
  48. }
  49. int main(int argc, char **argv) {
  50. FILE *infile = NULL;
  51. vpx_codec_ctx_t codec;
  52. vpx_codec_enc_cfg_t cfg;
  53. int frame_count = 0;
  54. vpx_image_t raw;
  55. vpx_codec_err_t res;
  56. VpxVideoInfo info;
  57. VpxVideoWriter *writer = NULL;
  58. const VpxInterface *encoder = NULL;
  59. const int fps = 30;
  60. vp9_zero(info);
  61. exec_name = argv[0];
  62. if (argc < 5) die("Invalid number of arguments");
  63. encoder = get_vpx_encoder_by_name("vp9");
  64. if (!encoder) die("Unsupported codec.");
  65. info.codec_fourcc = encoder->fourcc;
  66. info.frame_width = (int)strtol(argv[1], NULL, 0);
  67. info.frame_height = (int)strtol(argv[2], NULL, 0);
  68. info.time_base.numerator = 1;
  69. info.time_base.denominator = fps;
  70. if (info.frame_width <= 0 || info.frame_height <= 0 ||
  71. (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
  72. die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  73. }
  74. if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
  75. info.frame_height, 1)) {
  76. die("Failed to allocate image.");
  77. }
  78. printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
  79. res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
  80. if (res) die_codec(&codec, "Failed to get default codec config.");
  81. cfg.g_w = info.frame_width;
  82. cfg.g_h = info.frame_height;
  83. cfg.g_timebase.num = info.time_base.numerator;
  84. cfg.g_timebase.den = info.time_base.denominator;
  85. writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
  86. if (!writer) die("Failed to open %s for writing.", argv[4]);
  87. if (!(infile = fopen(argv[3], "rb")))
  88. die("Failed to open %s for reading.", argv[3]);
  89. if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
  90. die_codec(&codec, "Failed to initialize encoder");
  91. if (vpx_codec_control_(&codec, VP9E_SET_LOSSLESS, 1))
  92. die_codec(&codec, "Failed to use lossless mode");
  93. // Encode frames.
  94. while (vpx_img_read(&raw, infile)) {
  95. encode_frame(&codec, &raw, frame_count++, 0, writer);
  96. }
  97. // Flush encoder.
  98. while (encode_frame(&codec, NULL, -1, 0, writer)) {
  99. }
  100. printf("\n");
  101. fclose(infile);
  102. printf("Processed %d frames.\n", frame_count);
  103. vpx_img_free(&raw);
  104. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
  105. vpx_video_writer_close(writer);
  106. return EXIT_SUCCESS;
  107. }