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 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 // --------------- | 45 // --------------- |
46 // This example maintains the pattern passed on the command line in the | 46 // This example maintains the pattern passed on the command line in the |
47 // `n`, `m`, and `is_range` variables: | 47 // `n`, `m`, and `is_range` variables: |
48 // | 48 // |
49 // | 49 // |
50 // Making The Drop Decision | 50 // Making The Drop Decision |
51 // ------------------------ | 51 // ------------------------ |
52 // The example decides whether to drop the frame based on the current | 52 // The example decides whether to drop the frame based on the current |
53 // frame number, immediately before decoding the frame. | 53 // frame number, immediately before decoding the frame. |
54 | 54 |
55 #include <stdarg.h> | |
56 #include <stdio.h> | 55 #include <stdio.h> |
57 #include <stdlib.h> | 56 #include <stdlib.h> |
58 #include <string.h> | 57 #include <string.h> |
| 58 |
59 #define VPX_CODEC_DISABLE_COMPAT 1 | 59 #define VPX_CODEC_DISABLE_COMPAT 1 |
60 #include "./vpx_config.h" | 60 |
61 #include "vpx/vp8dx.h" | 61 #include "vpx/vp8dx.h" |
62 #include "vpx/vpx_decoder.h" | 62 #include "vpx/vpx_decoder.h" |
63 #define interface (vpx_codec_vp8_dx()) | |
64 | 63 |
| 64 #include "./tools_common.h" |
| 65 #include "./video_reader.h" |
| 66 #include "./vpx_config.h" |
65 | 67 |
66 #define IVF_FILE_HDR_SZ (32) | 68 static const char *exec_name; |
67 #define IVF_FRAME_HDR_SZ (12) | |
68 | 69 |
69 static unsigned int mem_get_le32(const unsigned char *mem) { | 70 void usage_exit() { |
70 return (mem[3] << 24)|(mem[2] << 16)|(mem[1] << 8)|(mem[0]); | 71 fprintf(stderr, "Usage: %s <infile> <outfile> <N-M|N/M>\n", exec_name); |
| 72 exit(EXIT_FAILURE); |
71 } | 73 } |
72 | 74 |
73 static void die(const char *fmt, ...) { | 75 int main(int argc, char **argv) { |
74 va_list ap; | 76 int frame_cnt = 0; |
| 77 FILE *outfile = NULL; |
| 78 vpx_codec_ctx_t codec; |
| 79 const VpxInterface *decoder = NULL; |
| 80 VpxVideoReader *reader = NULL; |
| 81 const VpxVideoInfo *info = NULL; |
| 82 int n = 0; |
| 83 int m = 0; |
| 84 int is_range = 0; |
| 85 char *nptr = NULL; |
75 | 86 |
76 va_start(ap, fmt); | 87 exec_name = argv[0]; |
77 vprintf(fmt, ap); | 88 |
78 if(fmt[strlen(fmt)-1] != '\n') | 89 if (argc != 4) |
79 printf("\n"); | 90 die("Invalid number of arguments."); |
80 exit(EXIT_FAILURE); | 91 |
| 92 reader = vpx_video_reader_open(argv[1]); |
| 93 if (!reader) |
| 94 die("Failed to open %s for reading.", argv[1]); |
| 95 |
| 96 if (!(outfile = fopen(argv[2], "wb"))) |
| 97 die("Failed to open %s for writing.", argv[2]); |
| 98 |
| 99 n = strtol(argv[3], &nptr, 0); |
| 100 m = strtol(nptr + 1, NULL, 0); |
| 101 is_range = (*nptr == '-'); |
| 102 if (!n || !m || (*nptr != '-' && *nptr != '/')) |
| 103 die("Couldn't parse pattern %s.\n", argv[3]); |
| 104 |
| 105 info = vpx_video_reader_get_info(reader); |
| 106 |
| 107 decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc); |
| 108 if (!decoder) |
| 109 die("Unknown input codec."); |
| 110 |
| 111 printf("Using %s\n", vpx_codec_iface_name(decoder->interface())); |
| 112 |
| 113 if (vpx_codec_dec_init(&codec, decoder->interface(), NULL, 0)) |
| 114 die_codec(&codec, "Failed to initialize decoder."); |
| 115 |
| 116 while (vpx_video_reader_read_frame(reader)) { |
| 117 vpx_codec_iter_t iter = NULL; |
| 118 vpx_image_t *img = NULL; |
| 119 size_t frame_size = 0; |
| 120 int skip; |
| 121 const unsigned char *frame = vpx_video_reader_get_frame(reader, |
| 122 &frame_size); |
| 123 if (vpx_codec_decode(&codec, frame, frame_size, NULL, 0)) |
| 124 die_codec(&codec, "Failed to decode frame."); |
| 125 |
| 126 ++frame_cnt; |
| 127 |
| 128 skip = (is_range && frame_cnt >= n && frame_cnt <= m) || |
| 129 (!is_range && m - (frame_cnt - 1) % m <= n); |
| 130 |
| 131 if (!skip) { |
| 132 putc('.', stdout); |
| 133 |
| 134 while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) |
| 135 vpx_img_write(img, outfile); |
| 136 } else { |
| 137 putc('X', stdout); |
| 138 } |
| 139 |
| 140 fflush(stdout); |
| 141 } |
| 142 |
| 143 printf("Processed %d frames.\n", frame_cnt); |
| 144 if (vpx_codec_destroy(&codec)) |
| 145 die_codec(&codec, "Failed to destroy codec."); |
| 146 |
| 147 printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n", |
| 148 info->frame_width, info->frame_height, argv[2]); |
| 149 |
| 150 vpx_video_reader_close(reader); |
| 151 fclose(outfile); |
| 152 |
| 153 return EXIT_SUCCESS; |
81 } | 154 } |
82 | |
83 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) { | |
84 const char *detail = vpx_codec_error_detail(ctx); | |
85 | |
86 printf("%s: %s\n", s, vpx_codec_error(ctx)); | |
87 if(detail) | |
88 printf(" %s\n",detail); | |
89 exit(EXIT_FAILURE); | |
90 } | |
91 | |
92 | |
93 int main(int argc, char **argv) { | |
94 FILE *infile, *outfile; | |
95 vpx_codec_ctx_t codec; | |
96 int flags = 0, frame_cnt = 0; | |
97 unsigned char file_hdr[IVF_FILE_HDR_SZ]; | |
98 unsigned char frame_hdr[IVF_FRAME_HDR_SZ]; | |
99 unsigned char frame[256*1024]; | |
100 vpx_codec_err_t res; | |
101 int n, m, is_range; | |
102 | |
103 (void)res; | |
104 /* Open files */ | |
105 if(argc!=4) | |
106 die("Usage: %s <infile> <outfile> <N-M|N/M>\n", argv[0]); | |
107 { | |
108 char *nptr; | |
109 n = strtol(argv[3], &nptr, 0); | |
110 m = strtol(nptr+1, NULL, 0); | |
111 is_range = *nptr == '-'; | |
112 if(!n || !m || (*nptr != '-' && *nptr != '/')) | |
113 die("Couldn't parse pattern %s\n", argv[3]); | |
114 } | |
115 if(!(infile = fopen(argv[1], "rb"))) | |
116 die("Failed to open %s for reading", argv[1]); | |
117 if(!(outfile = fopen(argv[2], "wb"))) | |
118 die("Failed to open %s for writing", argv[2]); | |
119 | |
120 /* Read file header */ | |
121 if(!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ | |
122 && file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I' | |
123 && file_hdr[3]=='F')) | |
124 die("%s is not an IVF file.", argv[1]); | |
125 | |
126 printf("Using %s\n",vpx_codec_iface_name(interface)); | |
127 /* Initialize codec */ | |
128 if(vpx_codec_dec_init(&codec, interface, NULL, flags)) | |
129 die_codec(&codec, "Failed to initialize decoder"); | |
130 | |
131 /* Read each frame */ | |
132 while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) { | |
133 int frame_sz = mem_get_le32(frame_hdr); | |
134 vpx_codec_iter_t iter = NULL; | |
135 vpx_image_t *img; | |
136 | |
137 | |
138 frame_cnt++; | |
139 if(frame_sz > sizeof(frame)) | |
140 die("Frame %d data too big for example code buffer", frame_sz); | |
141 if(fread(frame, 1, frame_sz, infile) != frame_sz) | |
142 die("Frame %d failed to read complete frame", frame_cnt); | |
143 | |
144 if((is_range && frame_cnt >= n && frame_cnt <= m) | |
145 ||(!is_range && m - (frame_cnt-1)%m <= n)) { | |
146 putc('X', stdout); | |
147 continue; | |
148 } | |
149 putc('.', stdout); | |
150 fflush(stdout); | |
151 /* Decode the frame */ | |
152 if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 0)) | |
153 die_codec(&codec, "Failed to decode frame"); | |
154 | |
155 /* Write decoded data to disk */ | |
156 while((img = vpx_codec_get_frame(&codec, &iter))) { | |
157 unsigned int plane, y; | |
158 | |
159 for(plane=0; plane < 3; plane++) { | |
160 unsigned char *buf =img->planes[plane]; | |
161 | |
162 for(y=0; y < (plane ? (img->d_h + 1) >> 1 : img->d_h); y++) { | |
163 (void) fwrite(buf, 1, (plane ? (img->d_w + 1) >> 1 : img->d_
w), | |
164 outfile); | |
165 buf += img->stride[plane]; | |
166 } | |
167 } | |
168 } | |
169 } | |
170 printf("Processed %d frames.\n",frame_cnt); | |
171 if(vpx_codec_destroy(&codec)) | |
172 die_codec(&codec, "Failed to destroy codec"); | |
173 | |
174 fclose(outfile); | |
175 fclose(infile); | |
176 return EXIT_SUCCESS; | |
177 } | |
OLD | NEW |