2014-02-06 06:34:46 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2014-02-06 06:34:46 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2014-02-06 06:34:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "./ivfenc.h"
|
|
|
|
#include "./video_writer.h"
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "aom/aom_encoder.h"
|
2014-02-06 06:34:46 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
struct AvxVideoWriterStruct {
|
|
|
|
AvxVideoInfo info;
|
2014-02-06 06:34:46 +04:00
|
|
|
FILE *file;
|
|
|
|
int frame_count;
|
|
|
|
};
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static void write_header(FILE *file, const AvxVideoInfo *info,
|
2014-02-06 06:34:46 +04:00
|
|
|
int frame_count) {
|
2016-08-31 00:01:10 +03:00
|
|
|
struct aom_codec_enc_cfg cfg;
|
2014-02-06 06:34:46 +04:00
|
|
|
cfg.g_w = info->frame_width;
|
|
|
|
cfg.g_h = info->frame_height;
|
|
|
|
cfg.g_timebase.num = info->time_base.numerator;
|
|
|
|
cfg.g_timebase.den = info->time_base.denominator;
|
|
|
|
|
|
|
|
ivf_write_file_header(file, &cfg, info->codec_fourcc, frame_count);
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
AvxVideoWriter *aom_video_writer_open(const char *filename,
|
|
|
|
AvxContainer container,
|
|
|
|
const AvxVideoInfo *info) {
|
2014-02-06 06:34:46 +04:00
|
|
|
if (container == kContainerIVF) {
|
2016-08-31 00:01:10 +03:00
|
|
|
AvxVideoWriter *writer = NULL;
|
2014-02-06 06:34:46 +04:00
|
|
|
FILE *const file = fopen(filename, "wb");
|
2016-08-09 05:03:30 +03:00
|
|
|
if (!file) return NULL;
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
writer = malloc(sizeof(*writer));
|
2016-08-09 05:03:30 +03:00
|
|
|
if (!writer) return NULL;
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
writer->frame_count = 0;
|
|
|
|
writer->info = *info;
|
|
|
|
writer->file = file;
|
|
|
|
|
|
|
|
write_header(writer->file, info, 0);
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
void aom_video_writer_close(AvxVideoWriter *writer) {
|
2014-02-06 06:34:46 +04:00
|
|
|
if (writer) {
|
|
|
|
// Rewriting frame header with real frame count
|
|
|
|
rewind(writer->file);
|
|
|
|
write_header(writer->file, &writer->info, writer->frame_count);
|
|
|
|
|
|
|
|
fclose(writer->file);
|
|
|
|
free(writer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
int aom_video_writer_write_frame(AvxVideoWriter *writer, const uint8_t *buffer,
|
2016-08-09 05:03:30 +03:00
|
|
|
size_t size, int64_t pts) {
|
2014-02-06 06:34:46 +04:00
|
|
|
ivf_write_frame_header(writer->file, pts, size);
|
2016-08-09 05:03:30 +03:00
|
|
|
if (fwrite(buffer, 1, size, writer->file) != size) return 0;
|
2014-02-06 06:34:46 +04:00
|
|
|
|
|
|
|
++writer->frame_count;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|