|
| 1 | +/* |
| 2 | + OpenDCP: Builds Digital Cinema Packages |
| 3 | + Copyright (c) 2010-2013 Terrence Meiczinger, All Rights Reserved |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <string.h> |
| 20 | +#include <getopt.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <time.h> |
| 24 | +#include <sys/stat.h> |
| 25 | +#include <opendcp.h> |
| 26 | +#include "opendcp_cli.h" |
| 27 | + |
| 28 | +void progress_bar(); |
| 29 | + |
| 30 | +void version() { |
| 31 | + FILE *fp; |
| 32 | + |
| 33 | + fp = stdout; |
| 34 | + fprintf(fp, "\n%s version %s %s\n\n", OPENDCP_NAME, OPENDCP_VERSION, OPENDCP_COPYRIGHT); |
| 35 | + |
| 36 | + exit(0); |
| 37 | +} |
| 38 | + |
| 39 | +void dcp_usage() { |
| 40 | + FILE *fp; |
| 41 | + fp = stdout; |
| 42 | + |
| 43 | + fprintf(fp, "\n%s version %s %s\n\n", OPENDCP_NAME, OPENDCP_VERSION, OPENDCP_COPYRIGHT); |
| 44 | + fprintf(fp, "Usage:\n"); |
| 45 | + fprintf(fp, " opendcp_extract -i <file> [options ...]\n\n"); |
| 46 | + fprintf(fp, "Required:\n"); |
| 47 | + fprintf(fp, " -i | --input <file> - input mxf file\n"); |
| 48 | + fprintf(fp, "\n"); |
| 49 | + fprintf(fp, "Options:\n"); |
| 50 | + fprintf(fp, " -s | --start <frame> - start frame\n"); |
| 51 | + fprintf(fp, " -d | --end <frame> - end frame\n"); |
| 52 | + fprintf(fp, " -l | --log_level <level> - Sets the log level 0:Quiet, 1:Error, 2:Warn (default), 3:Info, 4:Debug\n"); |
| 53 | + fprintf(fp, " -k | --key <key> - set encryption key (this enables encryption)\n"); |
| 54 | + fprintf(fp, " -h | --help - show help\n"); |
| 55 | + fprintf(fp, " -v | --version - show version\n"); |
| 56 | + fprintf(fp, "\n\n"); |
| 57 | + |
| 58 | + fclose(fp); |
| 59 | + exit(0); |
| 60 | +} |
| 61 | + |
| 62 | +int total = 0; |
| 63 | +int val = 0; |
| 64 | + |
| 65 | +int frame_done_cb(void *p) { |
| 66 | + val++; |
| 67 | + UNUSED(p); |
| 68 | + progress_bar(); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +int write_done_cb(void *p) { |
| 74 | + UNUSED(p); |
| 75 | + printf("\n MXF Complete\n"); |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +void progress_bar() { |
| 81 | + int x; |
| 82 | + int step = 20; |
| 83 | + float c = (float)step / total * (float)val; |
| 84 | + |
| 85 | + printf(" MXF Creation ["); |
| 86 | + |
| 87 | + for (x = 0; x < step; x++) { |
| 88 | + if (c > x) { |
| 89 | + printf("="); |
| 90 | + } |
| 91 | + else { |
| 92 | + printf(" "); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + printf("] 100%% [%d/%d]\r", val, total); |
| 97 | + fflush(stdout); |
| 98 | +} |
| 99 | + |
| 100 | +int main (int argc, char **argv) { |
| 101 | + int c; |
| 102 | + opendcp_t *opendcp; |
| 103 | + char *filename = NULL; |
| 104 | + int key_id_flag = 0; |
| 105 | + |
| 106 | + if (argc <= 1) { |
| 107 | + dcp_usage(); |
| 108 | + } |
| 109 | + |
| 110 | + opendcp = opendcp_create(); |
| 111 | + |
| 112 | + /* set initial values */ |
| 113 | + opendcp->log_level = LOG_WARN; |
| 114 | + opendcp->ns = XML_NS_SMPTE; |
| 115 | + |
| 116 | + /* parse options */ |
| 117 | + while (1) |
| 118 | + { |
| 119 | + static struct option long_options[] = |
| 120 | + { |
| 121 | + {"key", required_argument, 0, 'k'}, |
| 122 | + {"help", required_argument, 0, 'h'}, |
| 123 | + {"input", required_argument, 0, 'i'}, |
| 124 | + {"start", required_argument, 0, 's'}, |
| 125 | + {"log_level", required_argument, 0, 'l'}, |
| 126 | + {"version", no_argument, 0, 'v'}, |
| 127 | + {0, 0, 0, 0} |
| 128 | + }; |
| 129 | + |
| 130 | + /* getopt_long stores the option index here. */ |
| 131 | + int option_index = 0; |
| 132 | + |
| 133 | + c = getopt_long (argc, argv, "i:k:s:l:hv", |
| 134 | + long_options, &option_index); |
| 135 | + |
| 136 | + /* Detect the end of the options. */ |
| 137 | + if (c == -1) |
| 138 | + { break; } |
| 139 | + |
| 140 | + switch (c) |
| 141 | + { |
| 142 | + case 0: |
| 143 | + |
| 144 | + /* If this option set a flag, do nothing else now. */ |
| 145 | + if (long_options[option_index].flag != 0) |
| 146 | + { break; } |
| 147 | + |
| 148 | + printf ("option %s", long_options[option_index].name); |
| 149 | + |
| 150 | + if (optarg) |
| 151 | + { printf (" with arg %s", optarg); } |
| 152 | + |
| 153 | + printf ("\n"); |
| 154 | + break; |
| 155 | + |
| 156 | + case 's': |
| 157 | + opendcp->mxf.start_frame = atoi(optarg); |
| 158 | + |
| 159 | + if (opendcp->mxf.start_frame < 1) { |
| 160 | + dcp_fatal(opendcp, "Start frame must be greater than 0"); |
| 161 | + } |
| 162 | + |
| 163 | + break; |
| 164 | + |
| 165 | + case 'i': |
| 166 | + filename = optarg; |
| 167 | + break; |
| 168 | + |
| 169 | + case 'l': |
| 170 | + opendcp->log_level = atoi(optarg); |
| 171 | + break; |
| 172 | + |
| 173 | + case 'h': |
| 174 | + dcp_usage(); |
| 175 | + break; |
| 176 | + |
| 177 | + case 'k': |
| 178 | + if (!is_key(optarg)) { |
| 179 | + dcp_fatal(opendcp, "Invalid encryption key format"); |
| 180 | + } |
| 181 | + |
| 182 | + if (hex2bin(optarg, opendcp->mxf.key_value, 16)) { |
| 183 | + dcp_fatal(opendcp, "Invalid encryption key format"); |
| 184 | + } |
| 185 | + |
| 186 | + opendcp->mxf.key_flag = 1; |
| 187 | + |
| 188 | + break; |
| 189 | + |
| 190 | + case 'v': |
| 191 | + version(); |
| 192 | + break; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + opendcp_log_init(opendcp->log_level); |
| 197 | + |
| 198 | + if (opendcp->log_level > 0) { |
| 199 | + printf("\nOpenDCP Extract %s %s\n", OPENDCP_VERSION, OPENDCP_COPYRIGHT); |
| 200 | + } |
| 201 | + |
| 202 | + /* set the callbacks (optional) for the mxf writer */ |
| 203 | + if (opendcp->log_level > 0 && opendcp->log_level < 3) { |
| 204 | + opendcp->mxf.frame_done.callback = frame_done_cb; |
| 205 | + opendcp->mxf.file_done.callback = write_done_cb; |
| 206 | + } |
| 207 | + |
| 208 | + int class = get_file_essence_class(filename, 1); |
| 209 | + |
| 210 | + if (opendcp->log_level > 0 && opendcp->log_level < 3) { progress_bar(); } |
| 211 | + |
| 212 | + total = opendcp->mxf.duration; |
| 213 | + |
| 214 | + if (read_j2k_mxf(opendcp, filename) != 0 ) { |
| 215 | + OPENDCP_LOG(LOG_INFO, "Could not READ MXF file"); |
| 216 | + } |
| 217 | + else { |
| 218 | + OPENDCP_LOG(LOG_INFO, "MXF extract complete"); |
| 219 | + } |
| 220 | + |
| 221 | + if (opendcp->log_level > 0) { |
| 222 | + printf("\n"); |
| 223 | + } |
| 224 | + |
| 225 | + opendcp_delete(opendcp); |
| 226 | + |
| 227 | + exit(0); |
| 228 | +} |
0 commit comments