xref: /aosp_15_r20/external/libaom/common/video_writer.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_writer.h"
12*77c1e3ccSAndroid Build Coastguard Worker 
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "common/ivfenc.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker struct AvxVideoWriterStruct {
19*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoInfo info;
20*77c1e3ccSAndroid Build Coastguard Worker   FILE *file;
21*77c1e3ccSAndroid Build Coastguard Worker   int frame_count;
22*77c1e3ccSAndroid Build Coastguard Worker };
23*77c1e3ccSAndroid Build Coastguard Worker 
write_header(FILE * file,const AvxVideoInfo * info,int frame_count)24*77c1e3ccSAndroid Build Coastguard Worker static void write_header(FILE *file, const AvxVideoInfo *info,
25*77c1e3ccSAndroid Build Coastguard Worker                          int frame_count) {
26*77c1e3ccSAndroid Build Coastguard Worker   struct aom_codec_enc_cfg cfg;
27*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_w = info->frame_width;
28*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_h = info->frame_height;
29*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_timebase.num = info->time_base.numerator;
30*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_timebase.den = info->time_base.denominator;
31*77c1e3ccSAndroid Build Coastguard Worker 
32*77c1e3ccSAndroid Build Coastguard Worker   ivf_write_file_header(file, &cfg, info->codec_fourcc, frame_count);
33*77c1e3ccSAndroid Build Coastguard Worker }
34*77c1e3ccSAndroid Build Coastguard Worker 
aom_video_writer_open(const char * filename,AvxContainer container,const AvxVideoInfo * info)35*77c1e3ccSAndroid Build Coastguard Worker AvxVideoWriter *aom_video_writer_open(const char *filename,
36*77c1e3ccSAndroid Build Coastguard Worker                                       AvxContainer container,
37*77c1e3ccSAndroid Build Coastguard Worker                                       const AvxVideoInfo *info) {
38*77c1e3ccSAndroid Build Coastguard Worker   if (container == kContainerIVF) {
39*77c1e3ccSAndroid Build Coastguard Worker     AvxVideoWriter *writer = NULL;
40*77c1e3ccSAndroid Build Coastguard Worker     FILE *const file = fopen(filename, "wb");
41*77c1e3ccSAndroid Build Coastguard Worker     if (!file) return NULL;
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker     writer = malloc(sizeof(*writer));
44*77c1e3ccSAndroid Build Coastguard Worker     if (!writer) {
45*77c1e3ccSAndroid Build Coastguard Worker       fclose(file);
46*77c1e3ccSAndroid Build Coastguard Worker       return NULL;
47*77c1e3ccSAndroid Build Coastguard Worker     }
48*77c1e3ccSAndroid Build Coastguard Worker     writer->frame_count = 0;
49*77c1e3ccSAndroid Build Coastguard Worker     writer->info = *info;
50*77c1e3ccSAndroid Build Coastguard Worker     writer->file = file;
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker     write_header(writer->file, info, 0);
53*77c1e3ccSAndroid Build Coastguard Worker 
54*77c1e3ccSAndroid Build Coastguard Worker     return writer;
55*77c1e3ccSAndroid Build Coastguard Worker   }
56*77c1e3ccSAndroid Build Coastguard Worker 
57*77c1e3ccSAndroid Build Coastguard Worker   return NULL;
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker 
aom_video_writer_close(AvxVideoWriter * writer)60*77c1e3ccSAndroid Build Coastguard Worker void aom_video_writer_close(AvxVideoWriter *writer) {
61*77c1e3ccSAndroid Build Coastguard Worker   if (writer) {
62*77c1e3ccSAndroid Build Coastguard Worker     // Rewriting frame header with real frame count
63*77c1e3ccSAndroid Build Coastguard Worker     rewind(writer->file);
64*77c1e3ccSAndroid Build Coastguard Worker     write_header(writer->file, &writer->info, writer->frame_count);
65*77c1e3ccSAndroid Build Coastguard Worker 
66*77c1e3ccSAndroid Build Coastguard Worker     fclose(writer->file);
67*77c1e3ccSAndroid Build Coastguard Worker     free(writer);
68*77c1e3ccSAndroid Build Coastguard Worker   }
69*77c1e3ccSAndroid Build Coastguard Worker }
70*77c1e3ccSAndroid Build Coastguard Worker 
aom_video_writer_write_frame(AvxVideoWriter * writer,const uint8_t * buffer,size_t size,int64_t pts)71*77c1e3ccSAndroid Build Coastguard Worker int aom_video_writer_write_frame(AvxVideoWriter *writer, const uint8_t *buffer,
72*77c1e3ccSAndroid Build Coastguard Worker                                  size_t size, int64_t pts) {
73*77c1e3ccSAndroid Build Coastguard Worker   ivf_write_frame_header(writer->file, pts, size);
74*77c1e3ccSAndroid Build Coastguard Worker   if (fwrite(buffer, 1, size, writer->file) != size) return 0;
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker   ++writer->frame_count;
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker   return 1;
79*77c1e3ccSAndroid Build Coastguard Worker }
80*77c1e3ccSAndroid Build Coastguard Worker 
aom_video_writer_set_fourcc(AvxVideoWriter * writer,uint32_t fourcc)81*77c1e3ccSAndroid Build Coastguard Worker void aom_video_writer_set_fourcc(AvxVideoWriter *writer, uint32_t fourcc) {
82*77c1e3ccSAndroid Build Coastguard Worker   writer->info.codec_fourcc = fourcc;
83*77c1e3ccSAndroid Build Coastguard Worker }
84