blob: 1d4328ae1e46f9914b32aee0596f66dd04923bd0 [file] [log] [blame]
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -08003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080010 */
Tom Finegandd3e2a52018-05-23 14:33:09 -070011#include "common/video_writer.h"
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080012
13#include <stdlib.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "aom/aom_encoder.h"
Tom Finegandd3e2a52018-05-23 14:33:09 -070016#include "common/ivfenc.h"
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080017
Yaowu Xuf883b422016-08-30 14:01:10 -070018struct AvxVideoWriterStruct {
19 AvxVideoInfo info;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080020 FILE *file;
21 int frame_count;
22};
23
Yaowu Xuf883b422016-08-30 14:01:10 -070024static void write_header(FILE *file, const AvxVideoInfo *info,
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080025 int frame_count) {
Yaowu Xuf883b422016-08-30 14:01:10 -070026 struct aom_codec_enc_cfg cfg;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080027 cfg.g_w = info->frame_width;
28 cfg.g_h = info->frame_height;
29 cfg.g_timebase.num = info->time_base.numerator;
30 cfg.g_timebase.den = info->time_base.denominator;
31
32 ivf_write_file_header(file, &cfg, info->codec_fourcc, frame_count);
33}
34
Yaowu Xuf883b422016-08-30 14:01:10 -070035AvxVideoWriter *aom_video_writer_open(const char *filename,
36 AvxContainer container,
37 const AvxVideoInfo *info) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080038 if (container == kContainerIVF) {
Yaowu Xuf883b422016-08-30 14:01:10 -070039 AvxVideoWriter *writer = NULL;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080040 FILE *const file = fopen(filename, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -070041 if (!file) return NULL;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080042
43 writer = malloc(sizeof(*writer));
Yaowu Xu2df76f22020-04-10 09:37:34 -070044 if (!writer) {
45 fclose(file);
46 return NULL;
47 }
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080048 writer->frame_count = 0;
49 writer->info = *info;
50 writer->file = file;
51
52 write_header(writer->file, info, 0);
53
54 return writer;
55 }
56
57 return NULL;
58}
59
Yaowu Xuf883b422016-08-30 14:01:10 -070060void aom_video_writer_close(AvxVideoWriter *writer) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080061 if (writer) {
62 // Rewriting frame header with real frame count
63 rewind(writer->file);
64 write_header(writer->file, &writer->info, writer->frame_count);
65
66 fclose(writer->file);
67 free(writer);
68 }
69}
70
Yaowu Xuf883b422016-08-30 14:01:10 -070071int aom_video_writer_write_frame(AvxVideoWriter *writer, const uint8_t *buffer,
clang-format6c4d83e2016-08-08 19:03:30 -070072 size_t size, int64_t pts) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080073 ivf_write_frame_header(writer->file, pts, size);
clang-format6c4d83e2016-08-08 19:03:30 -070074 if (fwrite(buffer, 1, size, writer->file) != size) return 0;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080075
76 ++writer->frame_count;
77
78 return 1;
79}
Yunqing Wang3ae70c92019-01-25 17:34:07 -080080
81void aom_video_writer_set_fourcc(AvxVideoWriter *writer, uint32_t fourcc) {
82 writer->info.codec_fourcc = fourcc;
83}