repacketizer_demo.c 6.4 KB

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