From 012c0580dfebd3272bf77f2c60aec791c3a6aa38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arttu=20Yl=C3=A4-Outinen?= Date: Wed, 17 Jun 2015 15:25:32 +0300 Subject: [PATCH] Move writing reconstructed image to yuv_io module. Adds function yuv_io_write. --- src/encoderstate.c | 24 ++++-------------------- src/yuv_io.c | 30 ++++++++++++++++++++++++++++++ src/yuv_io.h | 4 ++++ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/encoderstate.c b/src/encoderstate.c index 0bf19cd7..d2b0cd49 100644 --- a/src/encoderstate.c +++ b/src/encoderstate.c @@ -942,26 +942,10 @@ void encoder_compute_stats(encoder_state_t *state, FILE * const recout, double f threadqueue_waitfor(encoder->threadqueue, state->tqj_bitstream_written); if (recout) { - const videoframe_t * const frame = state->tile->frame; - // Write reconstructed frame out. - // Use conformance-window dimensions instead of internal ones. - const int width = frame->width; - const int out_width = encoder->in.real_width; - const int out_height = encoder->in.real_height; - int y; - const pixel_t *y_rec = frame->rec->y; - const pixel_t *u_rec = frame->rec->u; - const pixel_t *v_rec = frame->rec->v; - - for (y = 0; y < out_height; ++y) { - fwrite(&y_rec[y * width], sizeof(*y_rec), out_width, recout); - } - for (y = 0; y < out_height / 2; ++y) { - fwrite(&u_rec[y * width / 2], sizeof(*u_rec), out_width / 2, recout); - } - for (y = 0; y < out_height / 2; ++y) { - fwrite(&v_rec[y * width / 2], sizeof(*v_rec), out_width / 2, recout); - } + yuv_io_write(recout, + state->tile->frame->rec, + encoder->in.real_width, + encoder->in.real_height); } videoframe_compute_psnr(state->tile->frame, frame_psnr); diff --git a/src/yuv_io.c b/src/yuv_io.c index a45d0c43..33da011b 100644 --- a/src/yuv_io.c +++ b/src/yuv_io.c @@ -148,3 +148,33 @@ int yuv_io_seek(FILE* file, unsigned frames, return !error || feof(file); } + + +/** + * \brief Write a single frame to a file. + * + * \param file output file + * \param img image to output + * \param output_width width of the output in pixels + * \param output_height height of the output in pixels + * + * \return 1 on success, 0 on failure + */ +int yuv_io_write(FILE* file, + image_t const* img, + unsigned output_width, unsigned output_height) +{ + const int width = img->width; + for (int y = 0; y < output_height; ++y) { + fwrite(&img->y[y * width], sizeof(*img->y), output_width, file); + // TODO: Check that fwrite succeeded. + } + for (int y = 0; y < output_height / 2; ++y) { + fwrite(&img->u[y * width / 2], sizeof(*img->u), output_width / 2, file); + } + for (int y = 0; y < output_height / 2; ++y) { + fwrite(&img->v[y * width / 2], sizeof(*img->v), output_width / 2, file); + } + + return 1; +} diff --git a/src/yuv_io.h b/src/yuv_io.h index dca7e574..6fcd2081 100644 --- a/src/yuv_io.h +++ b/src/yuv_io.h @@ -35,4 +35,8 @@ int yuv_io_read(FILE* file, int yuv_io_seek(FILE* file, unsigned frames, unsigned input_width, unsigned input_height); +int yuv_io_write(FILE* file, + image_t const* img, + unsigned output_width, unsigned output_height); + #endif // YUV_IO_H_