You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

417 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************************
  2. moov.c - A library for splitting Quicktime/MPEG4.
  3. Copyright (C) 2007-2009 CodeShop B.V.
  4. http://www.code-shop.com
  5. For licensing see the LICENSE file
  6. ******************************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif
  10. #ifdef _MSC_VER
  11. #define _CRTDBG_MAP_ALLOC
  12. #include <stdlib.h>
  13. #include <crtdbg.h>
  14. #endif
  15. #include "moov.h"
  16. #include "mp4_io.h"
  17. #include "mp4_reader.h"
  18. #include "output_bucket.h"
  19. #if defined(BUILDING_H264_STREAMING) || defined(BUILDING_MP4SPLIT)
  20. #include "output_mp4.h"
  21. #endif
  22. #if defined(BUILDING_SMOOTH_STREAMING) || defined(BUILDING_MP4SPLIT)
  23. #include "output_ismv.h"
  24. #endif
  25. #if defined(BUILDING_FLV_STREAMING) || defined(BUILDING_MP4SPLIT)
  26. #include "output_flv.h"
  27. #endif
  28. /*
  29. The QuickTime File Format PDF from Apple:
  30. http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
  31. http://developer.apple.com/documentation/QuickTime/QTFF/QTFFPreface/qtffPreface.html
  32. */
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <limits.h>
  37. #include <stdint.h>
  38. #include <sys/stat.h>
  39. #ifdef HAVE_STDINT_H
  40. # include <stdint.h>
  41. #endif
  42. #ifdef HAVE_INTTYPES_H
  43. # include <inttypes.h>
  44. #endif
  45. #ifdef WIN32
  46. #define stat _stat64
  47. #define strdup _strdup
  48. #endif
  49. char const* fragment_type_audio = "audio";
  50. char const* fragment_type_video = "video";
  51. /* Returns true when the test string is a prefix of the input */
  52. int starts_with(const char* input, const char* test)
  53. {
  54. while(*input && *test)
  55. {
  56. if(*input != *test)
  57. return 0;
  58. ++input;
  59. ++test;
  60. }
  61. return *test == '\0';
  62. }
  63. /* Returns true when the test string is a suffix of the input */
  64. int ends_with(const char* input, const char* test)
  65. {
  66. const char* it = input + strlen(input);
  67. const char* pit = test + strlen(test);
  68. while(it != input && pit != test)
  69. {
  70. if(*it != *pit)
  71. return 0;
  72. --it;
  73. --pit;
  74. }
  75. return pit == test;
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////
  78. // reported by everwanna:
  79. // av out of sync because:
  80. // audio track 0 without stss, seek to the exact time.
  81. // video track 1 with stss, seek to the nearest key frame time.
  82. //
  83. // fixed:
  84. // first pass we get the new aligned times for traks with an stss present
  85. // second pass is for traks without an stss
  86. static int get_aligned_start_and_end(struct mp4_context_t const* mp4_context,
  87. unsigned int start, unsigned int end,
  88. unsigned int* trak_sample_start,
  89. unsigned int* trak_sample_end)
  90. {
  91. unsigned int pass;
  92. struct moov_t* moov = mp4_context->moov;
  93. long moov_time_scale = moov->mvhd_->timescale_;
  94. for(pass = 0; pass != 2; ++pass)
  95. {
  96. unsigned int i;
  97. for(i = 0; i != moov->tracks_; ++i)
  98. {
  99. struct trak_t* trak = moov->traks_[i];
  100. struct stbl_t* stbl = trak->mdia_->minf_->stbl_;
  101. long trak_time_scale = trak->mdia_->mdhd_->timescale_;
  102. // 1st pass: stss present, 2nd pass: no stss present
  103. if(pass == 0 && !stbl->stss_)
  104. continue;
  105. if(pass == 1 && stbl->stss_)
  106. continue;
  107. // get start
  108. if(start == 0)
  109. {
  110. trak_sample_start[i] = start;
  111. }
  112. else
  113. {
  114. start = stts_get_sample(stbl->stts_,
  115. moov_time_to_trak_time(start, moov_time_scale, trak_time_scale));
  116. MP4_INFO("start=%u (trac time)\n", start);
  117. MP4_INFO("start=%.2f (seconds)\n",
  118. stts_get_time(stbl->stts_, start) / (float)trak_time_scale);
  119. start = stbl_get_nearest_keyframe(stbl, start + 1) - 1;
  120. MP4_INFO("start=%u (zero based keyframe)\n", start);
  121. trak_sample_start[i] = start;
  122. start = (unsigned int)(trak_time_to_moov_time(
  123. stts_get_time(stbl->stts_, start), moov_time_scale, trak_time_scale));
  124. MP4_INFO("start=%u (moov time)\n", start);
  125. MP4_INFO("start=%.2f (seconds)\n", start / (float)moov_time_scale);
  126. }
  127. // get end
  128. if(end == 0)
  129. {
  130. // The default is till-the-end of the track
  131. trak_sample_end[i] = trak->samples_size_;
  132. }
  133. else
  134. {
  135. end = stts_get_sample(stbl->stts_,
  136. moov_time_to_trak_time(end, moov_time_scale, trak_time_scale));
  137. MP4_INFO("end=%u (trac time)\n", end);
  138. MP4_INFO("end=%.2f (seconds)\n",
  139. stts_get_time(stbl->stts_, end) / (float)trak_time_scale);
  140. if(end >= trak->samples_size_)
  141. {
  142. end = trak->samples_size_;
  143. }
  144. else
  145. {
  146. end = stbl_get_nearest_keyframe(stbl, end + 1) - 1;
  147. }
  148. MP4_INFO("end=%u (zero based keyframe)\n", end);
  149. trak_sample_end[i] = end;
  150. // MP4_INFO("endframe=%u, samples_size_=%u\n", end, trak->samples_size_);
  151. end = (unsigned int)trak_time_to_moov_time(
  152. stts_get_time(stbl->stts_, end), moov_time_scale, trak_time_scale);
  153. MP4_INFO("end=%u (moov time)\n", end);
  154. MP4_INFO("end=%.2f (seconds)\n", end / (float)moov_time_scale);
  155. }
  156. }
  157. }
  158. MP4_INFO("start=%u\n", start);
  159. MP4_INFO("end=%u\n", end);
  160. if(end && start >= end)
  161. {
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////
  167. mp4_split_options_t* mp4_split_options_init()
  168. {
  169. mp4_split_options_t* options = (mp4_split_options_t*)
  170. malloc(sizeof(mp4_split_options_t));
  171. options->client_is_flash = 0;
  172. options->start = 0.0;
  173. options->start_integer = 0;
  174. options->end = 0.0;
  175. options->adaptive = 0;
  176. options->fragments = 0;
  177. options->output_format = OUTPUT_FORMAT_MP4;
  178. options->input_format = INPUT_FORMAT_MP4;
  179. options->fragment_type = NULL;
  180. options->fragment_bitrate = 0;
  181. options->fragment_track_id = 0;
  182. options->fragment_start = 0;
  183. options->seconds = 0;
  184. options->byte_offsets = 0;
  185. return options;
  186. }
  187. int mp4_split_options_set(struct mp4_split_options_t* options,
  188. const char* args_data,
  189. unsigned int args_size)
  190. {
  191. int result = 1;
  192. {
  193. const char* first = args_data;
  194. const char* last = first + args_size + 1;
  195. if(*first == '?')
  196. {
  197. ++first;
  198. }
  199. {
  200. char const* key = first;
  201. char const* val = NULL;
  202. int is_key = 1;
  203. size_t key_len = 0;
  204. float vbegin = 0.0f;
  205. float vend = 0.0f;
  206. while(first != last)
  207. {
  208. // the args_data is not necessarily 0 terminated, so fake it
  209. int ch = (first == last - 1) ? '\0' : *first;
  210. switch(ch)
  211. {
  212. case '=':
  213. val = first + 1;
  214. key_len = first - key;
  215. is_key = 0;
  216. break;
  217. case '&':
  218. case '\0':
  219. if(!is_key)
  220. {
  221. // make sure the value is zero-terminated (for strtod,atoi64)
  222. int val_len = first - val;
  223. char* valz = (char*)malloc(val_len + 1);
  224. memcpy(valz, val, val_len);
  225. valz[val_len] = '\0';
  226. if(!strncmp("client", key, key_len))
  227. {
  228. options->client_is_flash = starts_with(valz, "FLASH");
  229. } else
  230. if(!strncmp("start", key, key_len))
  231. {
  232. options->start = (float)(strtod(valz, NULL));
  233. options->start_integer = atoi64(valz);
  234. } else
  235. if(!strncmp("end", key, key_len))
  236. {
  237. options->end = (float)(strtod(valz, NULL));
  238. } else
  239. if(!strncmp("vbegin", key, key_len))
  240. {
  241. vbegin = (float)(strtod(valz, NULL));
  242. } else
  243. if(!strncmp("vend", key, key_len))
  244. {
  245. vend = (float)(strtod(valz, NULL));
  246. } else
  247. if(!strncmp("adaptive", key, key_len))
  248. {
  249. options->adaptive = 1;
  250. } else
  251. if(!strncmp("bitrate", key, key_len))
  252. {
  253. options->fragment_bitrate = (unsigned int)(atoi64(valz));
  254. } else
  255. if(!strncmp("video", key, key_len))
  256. {
  257. options->fragments = 1;
  258. options->fragment_type = fragment_type_video;
  259. options->fragment_start = atoi64(valz);
  260. } else
  261. if(!strncmp("audio", key, key_len))
  262. {
  263. options->fragments = 1;
  264. options->fragment_type = fragment_type_audio;
  265. options->fragment_start = atoi64(valz);
  266. } else
  267. if(!strncmp("format", key, key_len))
  268. {
  269. if(!strncmp("flv", val, val_len))
  270. {
  271. options->output_format = OUTPUT_FORMAT_FLV;
  272. } else
  273. if(!strncmp("ts", val, val_len))
  274. {
  275. options->output_format = OUTPUT_FORMAT_TS;
  276. }
  277. } else
  278. if(!strncmp("input", key, key_len))
  279. {
  280. if(!strncmp("flv", val, val_len))
  281. {
  282. options->input_format = INPUT_FORMAT_FLV;
  283. }
  284. }
  285. free(valz);
  286. }
  287. key = first + 1;
  288. val = NULL;
  289. is_key = 1;
  290. break;
  291. }
  292. ++first;
  293. }
  294. // If we have specified a begin point of the virtual video clip,
  295. // then adjust the start offset
  296. options->start += vbegin;
  297. // If we have specified an end, adjust it in case of a virtual video clip.
  298. if(options->end)
  299. {
  300. options->end += vbegin;
  301. }
  302. else
  303. {
  304. options->end = vend;
  305. }
  306. // Validate the start/end for the virtual video clip (begin).
  307. if(vbegin)
  308. {
  309. if(options->start < vbegin)
  310. result = 0;
  311. if(options->end && options->end < vbegin)
  312. result = 0;
  313. }
  314. // Validate the start/end for the virtual video clip (end).
  315. if(vend)
  316. {
  317. if(options->start > vend)
  318. result = 0;
  319. if(options->end && options->end > vend)
  320. result = 0;
  321. }
  322. }
  323. }
  324. return result;
  325. }
  326. void mp4_split_options_exit(struct mp4_split_options_t* options)
  327. {
  328. if(options->byte_offsets)
  329. {
  330. free(options->byte_offsets);
  331. }
  332. free(options);
  333. }
  334. extern int mp4_split(struct mp4_context_t* mp4_context,
  335. unsigned int* trak_sample_start,
  336. unsigned int* trak_sample_end,
  337. mp4_split_options_t const* options)
  338. {
  339. int result;
  340. float start_time = options->start;
  341. float end_time = options->end;
  342. moov_build_index(mp4_context, mp4_context->moov);
  343. {
  344. struct moov_t const* moov = mp4_context->moov;
  345. long moov_time_scale = moov->mvhd_->timescale_;
  346. unsigned int start = (unsigned int)(start_time * moov_time_scale + 0.5f);
  347. unsigned int end = (unsigned int)(end_time * moov_time_scale + 0.5f);
  348. // for every trak, convert seconds to sample (time-to-sample).
  349. // adjust sample to keyframe
  350. result = get_aligned_start_and_end(mp4_context, start, end,
  351. trak_sample_start, trak_sample_end);
  352. }
  353. return result;
  354. }
  355. uint64_t get_filesize(const char *path)
  356. {
  357. struct stat status;
  358. if(stat(path, &status))
  359. {
  360. printf("get_file_length(%s) stat: ", path);
  361. perror(NULL);
  362. return 0;
  363. }
  364. return status.st_size;
  365. }
  366. // End Of File