lossless_encoder.c 4.34 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11 12 13 14 15
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Yaowu Xu's avatar
Yaowu Xu committed
16 17
#include "aom/aom_encoder.h"
#include "aom/aomcx.h"
18

Tom Finegan's avatar
Tom Finegan committed
19 20
#include "../tools_common.h"
#include "../video_writer.h"
21 22 23

static const char *exec_name;

24
void usage_exit(void) {
clang-format's avatar
clang-format committed
25 26 27
  fprintf(stderr,
          "lossless_encoder: Example demonstrating lossless "
          "encoding feature. Supports raw input only.\n");
28 29 30 31
  fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
  exit(EXIT_FAILURE);
}

Yaowu Xu's avatar
Yaowu Xu committed
32 33
static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
                        int frame_index, int flags, AvxVideoWriter *writer) {
34
  int got_pkts = 0;
Yaowu Xu's avatar
Yaowu Xu committed
35 36 37 38 39
  aom_codec_iter_t iter = NULL;
  const aom_codec_cx_pkt_t *pkt = NULL;
  const aom_codec_err_t res =
      aom_codec_encode(codec, img, frame_index, 1, flags, AOM_DL_GOOD_QUALITY);
  if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
40

Yaowu Xu's avatar
Yaowu Xu committed
41
  while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
42 43
    got_pkts = 1;

Yaowu Xu's avatar
Yaowu Xu committed
44 45 46
    if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
      const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
      if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
47 48 49 50 51 52 53 54 55 56 57 58 59 60
                                        pkt->data.frame.sz,
                                        pkt->data.frame.pts)) {
        die_codec(codec, "Failed to write compressed frame");
      }
      printf(keyframe ? "K" : ".");
      fflush(stdout);
    }
  }

  return got_pkts;
}

int main(int argc, char **argv) {
  FILE *infile = NULL;
Yaowu Xu's avatar
Yaowu Xu committed
61 62
  aom_codec_ctx_t codec;
  aom_codec_enc_cfg_t cfg;
63
  int frame_count = 0;
Yaowu Xu's avatar
Yaowu Xu committed
64 65
  aom_image_t raw;
  aom_codec_err_t res;
66
  AvxVideoInfo info;
Yaowu Xu's avatar
Yaowu Xu committed
67 68
  AvxVideoWriter *writer = NULL;
  const AvxInterface *encoder = NULL;
69 70 71 72
  const int fps = 30;

  exec_name = argv[0];

73 74 75 76
  // Clear explicitly, as simply assigning "{ 0 }" generates
  // "missing-field-initializers" warning in some compilers.
  memset(&info, 0, sizeof(info));

clang-format's avatar
clang-format committed
77
  if (argc < 5) die("Invalid number of arguments");
78

Yaowu Xu's avatar
Yaowu Xu committed
79
  encoder = get_aom_encoder_by_name("av1");
clang-format's avatar
clang-format committed
80
  if (!encoder) die("Unsupported codec.");
81 82 83 84 85 86 87

  info.codec_fourcc = encoder->fourcc;
  info.frame_width = strtol(argv[1], NULL, 0);
  info.frame_height = strtol(argv[2], NULL, 0);
  info.time_base.numerator = 1;
  info.time_base.denominator = fps;

clang-format's avatar
clang-format committed
88 89
  if (info.frame_width <= 0 || info.frame_height <= 0 ||
      (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
90 91 92
    die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  }

Yaowu Xu's avatar
Yaowu Xu committed
93
  if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
clang-format's avatar
clang-format committed
94
                     info.frame_height, 1)) {
95 96 97
    die("Failed to allocate image.");
  }

Yaowu Xu's avatar
Yaowu Xu committed
98
  printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
99

Yaowu Xu's avatar
Yaowu Xu committed
100
  res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format's avatar
clang-format committed
101
  if (res) die_codec(&codec, "Failed to get default codec config.");
102 103 104 105 106 107

  cfg.g_w = info.frame_width;
  cfg.g_h = info.frame_height;
  cfg.g_timebase.num = info.time_base.numerator;
  cfg.g_timebase.den = info.time_base.denominator;

Yaowu Xu's avatar
Yaowu Xu committed
108
  writer = aom_video_writer_open(argv[4], kContainerIVF, &info);
clang-format's avatar
clang-format committed
109
  if (!writer) die("Failed to open %s for writing.", argv[4]);
110 111 112 113

  if (!(infile = fopen(argv[3], "rb")))
    die("Failed to open %s for reading.", argv[3]);

Yaowu Xu's avatar
Yaowu Xu committed
114
  if (aom_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
115 116
    die_codec(&codec, "Failed to initialize encoder");

Yaowu Xu's avatar
Yaowu Xu committed
117
  if (aom_codec_control_(&codec, AV1E_SET_LOSSLESS, 1))
118 119 120
    die_codec(&codec, "Failed to use lossless mode");

  // Encode frames.
Yaowu Xu's avatar
Yaowu Xu committed
121
  while (aom_img_read(&raw, infile)) {
122 123 124 125
    encode_frame(&codec, &raw, frame_count++, 0, writer);
  }

  // Flush encoder.
clang-format's avatar
clang-format committed
126 127
  while (encode_frame(&codec, NULL, -1, 0, writer)) {
  }
128 129 130 131 132

  printf("\n");
  fclose(infile);
  printf("Processed %d frames.\n", frame_count);

Yaowu Xu's avatar
Yaowu Xu committed
133 134
  aom_img_free(&raw);
  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
135

Yaowu Xu's avatar
Yaowu Xu committed
136
  aom_video_writer_close(writer);
137 138 139

  return EXIT_SUCCESS;
}