Merge pull request #10 from VFR-maniac/fix

Avoid reading one extra frame at the end of the input file.
This commit is contained in:
Marko Viitanen 2014-02-04 07:35:47 -08:00
commit 2438386f4b
3 changed files with 29 additions and 18 deletions

View file

@ -184,12 +184,16 @@ int main(int argc, char *argv[])
encoder->in.cur_pic->pred_v = MALLOC(pixel, (cfg->width * cfg->height) >> 2); encoder->in.cur_pic->pred_v = MALLOC(pixel, (cfg->width * cfg->height) >> 2);
// Start coding cycle while data on input and not on the last frame // Start coding cycle while data on input and not on the last frame
while(!feof(input) && (!cfg->frames || encoder->frame < cfg->frames)) { while(!cfg->frames || encoder->frame < cfg->frames) {
int32_t diff; int32_t diff;
double temp_psnr[3]; double temp_psnr[3];
// Read one frame from the input // Read one frame from the input
read_one_frame(input, encoder); if (!read_one_frame(input, encoder)) {
if (!feof(input))
printf("Failed to read a frame %d\n", encoder->frame);
break;
}
// The actual coding happens here, after this function we have a coded frame // The actual coding happens here, after this function we have a coded frame
encode_one_frame(encoder); encode_one_frame(encoder);

View file

@ -447,7 +447,7 @@ void fill_after_frame(FILE *file, unsigned height, unsigned array_width,
} }
} }
void read_and_fill_frame_data(FILE *file, unsigned width, unsigned height, int read_and_fill_frame_data(FILE *file, unsigned width, unsigned height,
unsigned array_width, pixel *data) unsigned array_width, pixel *data)
{ {
pixel* p = data; pixel* p = data;
@ -457,7 +457,8 @@ void read_and_fill_frame_data(FILE *file, unsigned width, unsigned height,
while (p < end) { while (p < end) {
// Read the beginning of the line from input. // Read the beginning of the line from input.
fread(p, sizeof(unsigned char), width, file); if (width != fread(p, sizeof(unsigned char), width, file))
return 0;
// Fill the rest with the last pixel value. // Fill the rest with the last pixel value.
fill_char = p[width - 1]; fill_char = p[width - 1];
@ -468,9 +469,10 @@ void read_and_fill_frame_data(FILE *file, unsigned width, unsigned height,
p += array_width; p += array_width;
} }
return 1;
} }
void read_one_frame(FILE* file, encoder_control* encoder) int read_one_frame(FILE* file, encoder_control* encoder)
{ {
encoder_input* in = &encoder->in; encoder_input* in = &encoder->in;
unsigned width = in->real_width; unsigned width = in->real_width;
@ -480,20 +482,24 @@ void read_one_frame(FILE* file, encoder_control* encoder)
if (width != array_width) { if (width != array_width) {
// In the case of frames not being aligned on 8 bit borders, bits need to be copied to fill them in. // In the case of frames not being aligned on 8 bit borders, bits need to be copied to fill them in.
read_and_fill_frame_data(file, width, height, array_width, if (!read_and_fill_frame_data(file, width, height, array_width,
in->cur_pic->y_data); in->cur_pic->y_data) ||
read_and_fill_frame_data(file, width >> 1, height >> 1, array_width >> 1, !read_and_fill_frame_data(file, width >> 1, height >> 1, array_width >> 1,
in->cur_pic->u_data); in->cur_pic->u_data) ||
read_and_fill_frame_data(file, width >> 1, height >> 1, array_width >> 1, !read_and_fill_frame_data(file, width >> 1, height >> 1, array_width >> 1,
in->cur_pic->v_data); in->cur_pic->v_data))
return 0;
} else { } else {
// Otherwise the data can be read directly to the array. // Otherwise the data can be read directly to the array.
fread(in->cur_pic->y_data, sizeof(unsigned char), unsigned y_size = width * height;
width * height, file); unsigned uv_size = (width >> 1) * (height >> 1);
fread(in->cur_pic->u_data, sizeof(unsigned char), if (y_size != fread(in->cur_pic->y_data, sizeof(unsigned char),
(width >> 1) * (height >> 1), file); y_size, file) ||
fread(in->cur_pic->v_data, sizeof(unsigned char), uv_size != fread(in->cur_pic->u_data, sizeof(unsigned char),
(width >> 1) * (height >> 1), file); uv_size, file) ||
uv_size != fread(in->cur_pic->v_data, sizeof(unsigned char),
uv_size, file))
return 0;
} }
if (height != array_height) { if (height != array_height) {
@ -504,6 +510,7 @@ void read_one_frame(FILE* file, encoder_control* encoder)
fill_after_frame(file, height >> 1, array_width >> 1, array_height >> 1, fill_after_frame(file, height >> 1, array_width >> 1, array_height >> 1,
in->cur_pic->v_data); in->cur_pic->v_data);
} }
return 1;
} }
/** /**

View file

@ -86,7 +86,7 @@ encoder_control *init_encoder_control(config *cfg);
void init_encoder_input(encoder_input *input, FILE* inputfile, void init_encoder_input(encoder_input *input, FILE* inputfile,
int32_t width, int32_t height); int32_t width, int32_t height);
void encode_one_frame(encoder_control *encoder); void encode_one_frame(encoder_control *encoder);
void read_one_frame(FILE *file, encoder_control *encoder); int read_one_frame(FILE *file, encoder_control *encoder);
void encode_seq_parameter_set(encoder_control *encoder); void encode_seq_parameter_set(encoder_control *encoder);
void encode_pic_parameter_set(encoder_control *encoder); void encode_pic_parameter_set(encoder_control *encoder);