xref: /aosp_15_r20/external/libaom/common/video_writer.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3  *
4  * 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.
10  */
11 #include "common/video_writer.h"
12 
13 #include <stdlib.h>
14 
15 #include "aom/aom_encoder.h"
16 #include "common/ivfenc.h"
17 
18 struct AvxVideoWriterStruct {
19   AvxVideoInfo info;
20   FILE *file;
21   int frame_count;
22 };
23 
write_header(FILE * file,const AvxVideoInfo * info,int frame_count)24 static void write_header(FILE *file, const AvxVideoInfo *info,
25                          int frame_count) {
26   struct aom_codec_enc_cfg cfg;
27   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 
aom_video_writer_open(const char * filename,AvxContainer container,const AvxVideoInfo * info)35 AvxVideoWriter *aom_video_writer_open(const char *filename,
36                                       AvxContainer container,
37                                       const AvxVideoInfo *info) {
38   if (container == kContainerIVF) {
39     AvxVideoWriter *writer = NULL;
40     FILE *const file = fopen(filename, "wb");
41     if (!file) return NULL;
42 
43     writer = malloc(sizeof(*writer));
44     if (!writer) {
45       fclose(file);
46       return NULL;
47     }
48     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 
aom_video_writer_close(AvxVideoWriter * writer)60 void aom_video_writer_close(AvxVideoWriter *writer) {
61   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 
aom_video_writer_write_frame(AvxVideoWriter * writer,const uint8_t * buffer,size_t size,int64_t pts)71 int aom_video_writer_write_frame(AvxVideoWriter *writer, const uint8_t *buffer,
72                                  size_t size, int64_t pts) {
73   ivf_write_frame_header(writer->file, pts, size);
74   if (fwrite(buffer, 1, size, writer->file) != size) return 0;
75 
76   ++writer->frame_count;
77 
78   return 1;
79 }
80 
aom_video_writer_set_fourcc(AvxVideoWriter * writer,uint32_t fourcc)81 void aom_video_writer_set_fourcc(AvxVideoWriter *writer, uint32_t fourcc) {
82   writer->info.codec_fourcc = fourcc;
83 }
84