blob: 4e072c7dcc41099483fc72b77f6457bf1c354c8b [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 */
11
12#include <stdlib.h>
13
14#include "./ivfenc.h"
15#include "./video_writer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom/aom_encoder.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));
clang-format6c4d83e2016-08-08 19:03:30 -070044 if (!writer) return NULL;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080045
46 writer->frame_count = 0;
47 writer->info = *info;
48 writer->file = file;
49
50 write_header(writer->file, info, 0);
51
52 return writer;
53 }
54
55 return NULL;
56}
57
Yaowu Xuf883b422016-08-30 14:01:10 -070058void aom_video_writer_close(AvxVideoWriter *writer) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080059 if (writer) {
60 // Rewriting frame header with real frame count
61 rewind(writer->file);
62 write_header(writer->file, &writer->info, writer->frame_count);
63
64 fclose(writer->file);
65 free(writer);
66 }
67}
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069int aom_video_writer_write_frame(AvxVideoWriter *writer, const uint8_t *buffer,
clang-format6c4d83e2016-08-08 19:03:30 -070070 size_t size, int64_t pts) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080071 ivf_write_frame_header(writer->file, pts, size);
clang-format6c4d83e2016-08-08 19:03:30 -070072 if (fwrite(buffer, 1, size, writer->file) != size) return 0;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080073
74 ++writer->frame_count;
75
76 return 1;
77}