OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 * | 9 * |
10 * Based on code from the OggTheora software codec source code, | 10 * Based on code from the OggTheora software codec source code, |
11 * Copyright (C) 2002-2010 The Xiph.Org Foundation and contributors. | 11 * Copyright (C) 2002-2010 The Xiph.Org Foundation and contributors. |
12 */ | 12 */ |
| 13 #include <errno.h> |
13 #include <stdlib.h> | 14 #include <stdlib.h> |
14 #include <string.h> | 15 #include <string.h> |
| 16 |
| 17 #include "vpx/vpx_integer.h" |
15 #include "y4minput.h" | 18 #include "y4minput.h" |
16 | 19 |
| 20 // Reads 'size' bytes from 'file' into 'buf' with some fault tolerance. |
| 21 // Returns true on success. |
| 22 static int file_read(void *buf, size_t size, FILE *file) { |
| 23 const int kMaxRetries = 5; |
| 24 int retry_count = 0; |
| 25 int file_error; |
| 26 size_t len = 0; |
| 27 do { |
| 28 const size_t n = fread((uint8_t*)buf + len, 1, size - len, file); |
| 29 len += n; |
| 30 file_error = ferror(file); |
| 31 if (file_error) { |
| 32 if (errno == EINTR || errno == EAGAIN) { |
| 33 clearerr(file); |
| 34 continue; |
| 35 } else { |
| 36 fprintf(stderr, "Error reading file: %u of %u bytes read, %d: %s\n", |
| 37 (uint32_t)len, (uint32_t)size, errno, strerror(errno)); |
| 38 return 0; |
| 39 } |
| 40 } |
| 41 } while (!feof(file) && len < size && ++retry_count < kMaxRetries); |
| 42 |
| 43 if (!feof(file) && len != size) { |
| 44 fprintf(stderr, "Error reading file: %u of %u bytes read," |
| 45 " error: %d, retries: %d, %d: %s\n", |
| 46 (uint32_t)len, (uint32_t)size, file_error, retry_count, |
| 47 errno, strerror(errno)); |
| 48 } |
| 49 return len == size; |
| 50 } |
| 51 |
17 static int y4m_parse_tags(y4m_input *_y4m, char *_tags) { | 52 static int y4m_parse_tags(y4m_input *_y4m, char *_tags) { |
18 int got_w; | 53 int got_w; |
19 int got_h; | 54 int got_h; |
20 int got_fps; | 55 int got_fps; |
21 int got_interlace; | 56 int got_interlace; |
22 int got_par; | 57 int got_par; |
23 int got_chroma; | 58 int got_chroma; |
24 char *p; | 59 char *p; |
25 char *q; | 60 char *q; |
26 got_w = got_h = got_fps = got_interlace = got_par = got_chroma = 0; | 61 got_w = got_h = got_fps = got_interlace = got_par = got_chroma = 0; |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 int only_420) { | 698 int only_420) { |
664 char buffer[80]; | 699 char buffer[80]; |
665 int ret; | 700 int ret; |
666 int i; | 701 int i; |
667 /*Read until newline, or 80 cols, whichever happens first.*/ | 702 /*Read until newline, or 80 cols, whichever happens first.*/ |
668 for (i = 0; i < 79; i++) { | 703 for (i = 0; i < 79; i++) { |
669 if (_nskip > 0) { | 704 if (_nskip > 0) { |
670 buffer[i] = *_skip++; | 705 buffer[i] = *_skip++; |
671 _nskip--; | 706 _nskip--; |
672 } else { | 707 } else { |
673 ret = (int)fread(buffer + i, 1, 1, _fin); | 708 if (!file_read(buffer + i, 1, _fin)) return -1; |
674 if (ret < 1)return -1; | |
675 } | 709 } |
676 if (buffer[i] == '\n')break; | 710 if (buffer[i] == '\n')break; |
677 } | 711 } |
678 /*We skipped too much header data.*/ | 712 /*We skipped too much header data.*/ |
679 if (_nskip > 0)return -1; | 713 if (_nskip > 0)return -1; |
680 if (i == 79) { | 714 if (i == 79) { |
681 fprintf(stderr, "Error parsing header; not a YUV2MPEG2 file?\n"); | 715 fprintf(stderr, "Error parsing header; not a YUV2MPEG2 file?\n"); |
682 return -1; | 716 return -1; |
683 } | 717 } |
684 buffer[i] = '\0'; | 718 buffer[i] = '\0'; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 free(_y4m->dst_buf); | 880 free(_y4m->dst_buf); |
847 free(_y4m->aux_buf); | 881 free(_y4m->aux_buf); |
848 } | 882 } |
849 | 883 |
850 int y4m_input_fetch_frame(y4m_input *_y4m, FILE *_fin, vpx_image_t *_img) { | 884 int y4m_input_fetch_frame(y4m_input *_y4m, FILE *_fin, vpx_image_t *_img) { |
851 char frame[6]; | 885 char frame[6]; |
852 int pic_sz; | 886 int pic_sz; |
853 int c_w; | 887 int c_w; |
854 int c_h; | 888 int c_h; |
855 int c_sz; | 889 int c_sz; |
856 int ret; | |
857 /*Read and skip the frame header.*/ | 890 /*Read and skip the frame header.*/ |
858 ret = (int)fread(frame, 1, 6, _fin); | 891 if (!file_read(frame, 6, _fin)) return 0; |
859 if (ret < 6)return 0; | |
860 if (memcmp(frame, "FRAME", 5)) { | 892 if (memcmp(frame, "FRAME", 5)) { |
861 fprintf(stderr, "Loss of framing in Y4M input data\n"); | 893 fprintf(stderr, "Loss of framing in Y4M input data\n"); |
862 return -1; | 894 return -1; |
863 } | 895 } |
864 if (frame[5] != '\n') { | 896 if (frame[5] != '\n') { |
865 char c; | 897 char c; |
866 int j; | 898 int j; |
867 for (j = 0; j < 79 && fread(&c, 1, 1, _fin) && c != '\n'; j++); | 899 for (j = 0; j < 79 && file_read(&c, 1, _fin) && c != '\n'; j++) {} |
868 if (j == 79) { | 900 if (j == 79) { |
869 fprintf(stderr, "Error parsing Y4M frame header\n"); | 901 fprintf(stderr, "Error parsing Y4M frame header\n"); |
870 return -1; | 902 return -1; |
871 } | 903 } |
872 } | 904 } |
873 /*Read the frame data that needs no conversion.*/ | 905 /*Read the frame data that needs no conversion.*/ |
874 if (fread(_y4m->dst_buf, 1, _y4m->dst_buf_read_sz, _fin) != _y4m->dst_buf_read
_sz) { | 906 if (!file_read(_y4m->dst_buf, _y4m->dst_buf_read_sz, _fin)) { |
875 fprintf(stderr, "Error reading Y4M frame data.\n"); | 907 fprintf(stderr, "Error reading Y4M frame data.\n"); |
876 return -1; | 908 return -1; |
877 } | 909 } |
878 /*Read the frame data that does need conversion.*/ | 910 /*Read the frame data that does need conversion.*/ |
879 if (fread(_y4m->aux_buf, 1, _y4m->aux_buf_read_sz, _fin) != _y4m->aux_buf_read
_sz) { | 911 if (!file_read(_y4m->aux_buf, _y4m->aux_buf_read_sz, _fin)) { |
880 fprintf(stderr, "Error reading Y4M frame data.\n"); | 912 fprintf(stderr, "Error reading Y4M frame data.\n"); |
881 return -1; | 913 return -1; |
882 } | 914 } |
883 /*Now convert the just read frame.*/ | 915 /*Now convert the just read frame.*/ |
884 (*_y4m->convert)(_y4m, _y4m->dst_buf, _y4m->aux_buf); | 916 (*_y4m->convert)(_y4m, _y4m->dst_buf, _y4m->aux_buf); |
885 /*Fill in the frame buffer pointers. | 917 /*Fill in the frame buffer pointers. |
886 We don't use vpx_img_wrap() because it forces padding for odd picture | 918 We don't use vpx_img_wrap() because it forces padding for odd picture |
887 sizes, which would require a separate fread call for every row.*/ | 919 sizes, which would require a separate fread call for every row.*/ |
888 memset(_img, 0, sizeof(*_img)); | 920 memset(_img, 0, sizeof(*_img)); |
889 /*Y4M has the planes in Y'CbCr order, which libvpx calls Y, U, and V.*/ | 921 /*Y4M has the planes in Y'CbCr order, which libvpx calls Y, U, and V.*/ |
(...skipping 10 matching lines...) Expand all Loading... |
900 c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; | 932 c_h = (_y4m->pic_h + _y4m->dst_c_dec_v - 1) / _y4m->dst_c_dec_v; |
901 c_sz = c_w * c_h; | 933 c_sz = c_w * c_h; |
902 _img->stride[PLANE_Y] = _img->stride[PLANE_ALPHA] = _y4m->pic_w; | 934 _img->stride[PLANE_Y] = _img->stride[PLANE_ALPHA] = _y4m->pic_w; |
903 _img->stride[PLANE_U] = _img->stride[PLANE_V] = c_w; | 935 _img->stride[PLANE_U] = _img->stride[PLANE_V] = c_w; |
904 _img->planes[PLANE_Y] = _y4m->dst_buf; | 936 _img->planes[PLANE_Y] = _y4m->dst_buf; |
905 _img->planes[PLANE_U] = _y4m->dst_buf + pic_sz; | 937 _img->planes[PLANE_U] = _y4m->dst_buf + pic_sz; |
906 _img->planes[PLANE_V] = _y4m->dst_buf + pic_sz + c_sz; | 938 _img->planes[PLANE_V] = _y4m->dst_buf + pic_sz + c_sz; |
907 _img->planes[PLANE_ALPHA] = _y4m->dst_buf + pic_sz + 2 * c_sz; | 939 _img->planes[PLANE_ALPHA] = _y4m->dst_buf + pic_sz + 2 * c_sz; |
908 return 1; | 940 return 1; |
909 } | 941 } |
OLD | NEW |