| OLD | NEW |
| (Empty) | |
| 1 /* Copyright (c) 2011 Xiph.Org Foundation |
| 2 Written by Jean-Marc Valin */ |
| 3 /* |
| 4 Redistribution and use in source and binary forms, with or without |
| 5 modification, are permitted provided that the following conditions |
| 6 are met: |
| 7 |
| 8 - Redistributions of source code must retain the above copyright |
| 9 notice, this list of conditions and the following disclaimer. |
| 10 |
| 11 - Redistributions in binary form must reproduce the above copyright |
| 12 notice, this list of conditions and the following disclaimer in the |
| 13 documentation and/or other materials provided with the distribution. |
| 14 |
| 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ |
| 27 |
| 28 #ifdef HAVE_CONFIG_H |
| 29 #include "config.h" |
| 30 #endif |
| 31 |
| 32 #include "opus.h" |
| 33 #include <stdio.h> |
| 34 #include <stdlib.h> |
| 35 #include <string.h> |
| 36 |
| 37 #define MAX_PACKETOUT 32000 |
| 38 |
| 39 void usage(char *argv0) |
| 40 { |
| 41 fprintf(stderr, "usage: %s [options] input_file output_file\n", argv0); |
| 42 } |
| 43 |
| 44 static void int_to_char(opus_uint32 i, unsigned char ch[4]) |
| 45 { |
| 46 ch[0] = i>>24; |
| 47 ch[1] = (i>>16)&0xFF; |
| 48 ch[2] = (i>>8)&0xFF; |
| 49 ch[3] = i&0xFF; |
| 50 } |
| 51 |
| 52 static opus_uint32 char_to_int(unsigned char ch[4]) |
| 53 { |
| 54 return ((opus_uint32)ch[0]<<24) | ((opus_uint32)ch[1]<<16) |
| 55 | ((opus_uint32)ch[2]<< 8) | (opus_uint32)ch[3]; |
| 56 } |
| 57 |
| 58 int main(int argc, char *argv[]) |
| 59 { |
| 60 int i, eof=0; |
| 61 FILE *fin, *fout; |
| 62 unsigned char packets[48][1500]; |
| 63 int len[48]; |
| 64 int rng[48]; |
| 65 OpusRepacketizer *rp; |
| 66 unsigned char output_packet[MAX_PACKETOUT]; |
| 67 int merge = 1, split=0; |
| 68 |
| 69 if (argc < 3) |
| 70 { |
| 71 usage(argv[0]); |
| 72 return EXIT_FAILURE; |
| 73 } |
| 74 for (i=1;i<argc-2;i++) |
| 75 { |
| 76 if (strcmp(argv[i], "-merge")==0) |
| 77 { |
| 78 merge = atoi(argv[i+1]); |
| 79 if(merge<1) |
| 80 { |
| 81 fprintf(stderr, "-merge parameter must be at least 1.\n"); |
| 82 return EXIT_FAILURE; |
| 83 } |
| 84 i++; |
| 85 } else if (strcmp(argv[i], "-split")==0) |
| 86 split = 1; |
| 87 else |
| 88 { |
| 89 fprintf(stderr, "Unknown option: %s\n", argv[i]); |
| 90 usage(argv[0]); |
| 91 return EXIT_FAILURE; |
| 92 } |
| 93 } |
| 94 fin = fopen(argv[argc-2], "r"); |
| 95 if(fin==NULL) |
| 96 { |
| 97 fprintf(stderr, "Error opening input file: %s\n", argv[argc-2]); |
| 98 return EXIT_FAILURE; |
| 99 } |
| 100 fout = fopen(argv[argc-1], "w"); |
| 101 if(fout==NULL) |
| 102 { |
| 103 fprintf(stderr, "Error opening output file: %s\n", argv[argc-1]); |
| 104 fclose(fin); |
| 105 return EXIT_FAILURE; |
| 106 } |
| 107 |
| 108 rp = opus_repacketizer_create(); |
| 109 while (!eof) |
| 110 { |
| 111 int err; |
| 112 int nb_packets=merge; |
| 113 opus_repacketizer_init(rp); |
| 114 for (i=0;i<nb_packets;i++) |
| 115 { |
| 116 unsigned char ch[4]; |
| 117 err = fread(ch, 1, 4, fin); |
| 118 len[i] = char_to_int(ch); |
| 119 /*fprintf(stderr, "in len = %d\n", len[i]);*/ |
| 120 if (len[i]>1500 || len[i]<0) |
| 121 { |
| 122 if (feof(fin)) |
| 123 { |
| 124 eof = 1; |
| 125 } else { |
| 126 fprintf(stderr, "Invalid payload length\n"); |
| 127 fclose(fin); |
| 128 fclose(fout); |
| 129 return EXIT_FAILURE; |
| 130 } |
| 131 break; |
| 132 } |
| 133 err = fread(ch, 1, 4, fin); |
| 134 rng[i] = char_to_int(ch); |
| 135 err = fread(packets[i], 1, len[i], fin); |
| 136 if (feof(fin)) |
| 137 { |
| 138 eof = 1; |
| 139 break; |
| 140 } |
| 141 err = opus_repacketizer_cat(rp, packets[i], len[i]); |
| 142 if (err!=OPUS_OK) |
| 143 { |
| 144 fprintf(stderr, "opus_repacketizer_cat() failed: %s\n", opus_strerro
r(err)); |
| 145 break; |
| 146 } |
| 147 } |
| 148 nb_packets = i; |
| 149 |
| 150 if (eof) |
| 151 break; |
| 152 |
| 153 if (!split) |
| 154 { |
| 155 err = opus_repacketizer_out(rp, output_packet, MAX_PACKETOUT); |
| 156 if (err>0) { |
| 157 unsigned char int_field[4]; |
| 158 int_to_char(err, int_field); |
| 159 if(fwrite(int_field, 1, 4, fout)!=4){ |
| 160 fprintf(stderr, "Error writing.\n"); |
| 161 return EXIT_FAILURE; |
| 162 } |
| 163 int_to_char(rng[nb_packets-1], int_field); |
| 164 if (fwrite(int_field, 1, 4, fout)!=4) { |
| 165 fprintf(stderr, "Error writing.\n"); |
| 166 return EXIT_FAILURE; |
| 167 } |
| 168 if (fwrite(output_packet, 1, err, fout)!=(unsigned)err) { |
| 169 fprintf(stderr, "Error writing.\n"); |
| 170 return EXIT_FAILURE; |
| 171 } |
| 172 /*fprintf(stderr, "out len = %d\n", err);*/ |
| 173 } else { |
| 174 fprintf(stderr, "opus_repacketizer_out() failed: %s\n", opus_strerro
r(err)); |
| 175 } |
| 176 } else { |
| 177 int nb_frames = opus_repacketizer_get_nb_frames(rp); |
| 178 for (i=0;i<nb_frames;i++) |
| 179 { |
| 180 err = opus_repacketizer_out_range(rp, i, i+1, output_packet, MAX_PAC
KETOUT); |
| 181 if (err>0) { |
| 182 unsigned char int_field[4]; |
| 183 int_to_char(err, int_field); |
| 184 if (fwrite(int_field, 1, 4, fout)!=4) { |
| 185 fprintf(stderr, "Error writing.\n"); |
| 186 return EXIT_FAILURE; |
| 187 } |
| 188 if (i==nb_frames-1) |
| 189 int_to_char(rng[nb_packets-1], int_field); |
| 190 else |
| 191 int_to_char(0, int_field); |
| 192 if (fwrite(int_field, 1, 4, fout)!=4) { |
| 193 fprintf(stderr, "Error writing.\n"); |
| 194 return EXIT_FAILURE; |
| 195 } |
| 196 if (fwrite(output_packet, 1, err, fout)!=(unsigned)err) { |
| 197 fprintf(stderr, "Error writing.\n"); |
| 198 return EXIT_FAILURE; |
| 199 } |
| 200 /*fprintf(stderr, "out len = %d\n", err);*/ |
| 201 } else { |
| 202 fprintf(stderr, "opus_repacketizer_out() failed: %s\n", opus_stre
rror(err)); |
| 203 } |
| 204 |
| 205 } |
| 206 } |
| 207 } |
| 208 |
| 209 fclose(fin); |
| 210 fclose(fout); |
| 211 return EXIT_SUCCESS; |
| 212 } |
| OLD | NEW |