blob: 77bea1ccddc4d6c346e8e908eb69109ccb25cc0e [file] [log] [blame]
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -08001/*
Adrian Grangea872b062016-03-24 11:38:32 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -08003 *
Adrian Grangea872b062016-03-24 11:38:32 -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"
Adrian Grangecebe6f02016-03-25 12:11:05 -070016#include "aom/aom_encoder.h"
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080017
18struct VpxVideoWriterStruct {
19 VpxVideoInfo info;
20 FILE *file;
21 int frame_count;
22};
23
24static void write_header(FILE *file, const VpxVideoInfo *info,
25 int frame_count) {
Adrian Grangecebe6f02016-03-25 12:11:05 -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
Adrian Grangecebe6f02016-03-25 12:11:05 -070035VpxVideoWriter *aom_video_writer_open(const char *filename,
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080036 VpxContainer container,
37 const VpxVideoInfo *info) {
38 if (container == kContainerIVF) {
39 VpxVideoWriter *writer = NULL;
40 FILE *const file = fopen(filename, "wb");
clang-format99e28b82016-01-27 12:42:45 -080041 if (!file) return NULL;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080042
43 writer = malloc(sizeof(*writer));
clang-format99e28b82016-01-27 12:42:45 -080044 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
Adrian Grangecebe6f02016-03-25 12:11:05 -070058void aom_video_writer_close(VpxVideoWriter *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
Adrian Grangecebe6f02016-03-25 12:11:05 -070069int aom_video_writer_write_frame(VpxVideoWriter *writer, const uint8_t *buffer,
clang-format99e28b82016-01-27 12:42:45 -080070 size_t size, int64_t pts) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080071 ivf_write_frame_header(writer->file, pts, size);
clang-format99e28b82016-01-27 12:42:45 -080072 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}