mpeg3demux.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:51k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. #include "libmpeg3.h"
  2. #include "mpeg3io.h"
  3. #include "mpeg3protos.h"
  4. #include "workarounds.h"
  5. #include <errno.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define ABS(x) ((x) >= 0 ? (x) : -(x))
  10. /* Don't advance pointer */
  11. static __inline unsigned char packet_next_char(mpeg3_demuxer_t *demuxer)
  12. {
  13. //printf(__FUNCTION__ " calledn");
  14. return demuxer->raw_data[demuxer->raw_offset];
  15. }
  16. /* Advance pointer */
  17. static unsigned char packet_read_char(mpeg3_demuxer_t *demuxer)
  18. {
  19. unsigned char result = demuxer->raw_data[demuxer->raw_offset++];
  20. //printf(__FUNCTION__ " calledn");
  21. return result;
  22. }
  23. static __inline unsigned int packet_read_int16(mpeg3_demuxer_t *demuxer)
  24. {
  25. unsigned int a, b, result;
  26. //printf(__FUNCTION__ " calledn");
  27. a = demuxer->raw_data[demuxer->raw_offset++];
  28. b = demuxer->raw_data[demuxer->raw_offset++];
  29. result = (a << 8) | b;
  30. return result;
  31. }
  32. static __inline unsigned int packet_next_int24(mpeg3_demuxer_t *demuxer)
  33. {
  34. unsigned int a, b, c, result;
  35. //printf(__FUNCTION__ " calledn");
  36. a = demuxer->raw_data[demuxer->raw_offset];
  37. b = demuxer->raw_data[demuxer->raw_offset + 1];
  38. c = demuxer->raw_data[demuxer->raw_offset + 2];
  39. result = (a << 16) | (b << 8) | c;
  40. return result;
  41. }
  42. static __inline unsigned int packet_read_int24(mpeg3_demuxer_t *demuxer)
  43. {
  44. unsigned int a, b, c, result;
  45. //printf(__FUNCTION__ " calledn");
  46. a = demuxer->raw_data[demuxer->raw_offset++];
  47. b = demuxer->raw_data[demuxer->raw_offset++];
  48. c = demuxer->raw_data[demuxer->raw_offset++];
  49. result = (a << 16) | (b << 8) | c;
  50. return result;
  51. }
  52. static __inline unsigned int packet_read_int32(mpeg3_demuxer_t *demuxer)
  53. {
  54. unsigned int a, b, c, d, result;
  55. //printf(__FUNCTION__ " calledn");
  56. a = demuxer->raw_data[demuxer->raw_offset++];
  57. b = demuxer->raw_data[demuxer->raw_offset++];
  58. c = demuxer->raw_data[demuxer->raw_offset++];
  59. d = demuxer->raw_data[demuxer->raw_offset++];
  60. result = (a << 24) | (b << 16) | (c << 8) | d;
  61. return result;
  62. }
  63. static __inline unsigned int packet_skip(mpeg3_demuxer_t *demuxer, long length)
  64. {
  65. //printf(__FUNCTION__ " calledn");
  66. demuxer->raw_offset += length;
  67. return 0;
  68. }
  69. static int get_adaptation_field(mpeg3_demuxer_t *demuxer)
  70. {
  71. long length;
  72. int pcr_flag;
  73. demuxer->adaptation_fields++;
  74. /* get adaptation field length */
  75. length = packet_read_char(demuxer);
  76. if(length > 0)
  77. {
  78. /* get first byte */
  79.    pcr_flag = (packet_read_char(demuxer) >> 4) & 1;           
  80. if(pcr_flag)
  81. {
  82.      unsigned long clk_ref_base = packet_read_int32(demuxer);
  83.      unsigned int clk_ref_ext = packet_read_int16(demuxer);
  84. if (clk_ref_base > 0x7fffffff)
  85. {   /* correct for invalid numbers */
  86. clk_ref_base = 0;               /* ie. longer than 32 bits when multiplied by 2 */
  87. clk_ref_ext = 0;                /* multiplied by 2 corresponds to shift left 1 (<<=1) */
  88. }
  89. else 
  90. {
  91. clk_ref_base <<= 1; /* Create space for bit */
  92. clk_ref_base |= (clk_ref_ext >> 15);          /* Take bit */
  93. clk_ref_ext &= 0x01ff;                        /* Only lower 9 bits */
  94. }
  95. demuxer->time = ((double)clk_ref_base + clk_ref_ext / 300) / 90000;
  96.      if(length) packet_skip(demuxer, length - 7);
  97. }
  98. else
  99. packet_skip(demuxer, length - 1);
  100. }
  101. return 0;
  102. }
  103. static int get_program_association_table(mpeg3_demuxer_t *demuxer)
  104. {
  105. demuxer->program_association_tables++;
  106. demuxer->table_id = packet_read_char(demuxer);
  107. demuxer->section_length = packet_read_int16(demuxer) & 0xfff;
  108. demuxer->transport_stream_id = packet_read_int16(demuxer);
  109. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  110. return 0;
  111. }
  112. static int get_data_buffer(mpeg3_demuxer_t *demuxer)
  113. {
  114. while(demuxer->raw_offset < demuxer->raw_size && 
  115. demuxer->data_size < MPEG3_RAW_SIZE)
  116. {
  117. demuxer->data_buffer[demuxer->data_size++] = 
  118. demuxer->raw_data[demuxer->raw_offset++];
  119. }
  120. return 0;
  121. }
  122. static int get_pes_packet_header(mpeg3_demuxer_t *demuxer, unsigned long *pts, unsigned long *dts)
  123. {
  124. unsigned int pes_header_bytes = 0;
  125. unsigned int pts_dts_flags;
  126. int pes_header_data_length;
  127. /* drop first 8 bits */
  128. packet_read_char(demuxer);  
  129. pts_dts_flags = (packet_read_char(demuxer) >> 6) & 0x3;
  130. pes_header_data_length = packet_read_char(demuxer);
  131. /* Get Presentation Time stamps and Decoding Time Stamps */
  132. if(pts_dts_flags == 2)
  133. {
  134. *pts = (packet_read_char(demuxer) >> 1) & 7;  /* Only low 4 bits (7==1111) */
  135. *pts <<= 15;
  136. *pts |= (packet_read_int16(demuxer) >> 1);
  137. *pts <<= 15;
  138. *pts |= (packet_read_int16(demuxer) >> 1);
  139. pes_header_bytes += 5;
  140. }
  141. else if(pts_dts_flags == 3)
  142. {      
  143. *pts = (packet_read_char(demuxer) >> 1) & 7;  /* Only low 4 bits (7==1111) */
  144. *pts <<= 15;
  145. *pts |= (packet_read_int16(demuxer) >> 1);
  146. *pts <<= 15;
  147. *pts |= (packet_read_int16(demuxer) >> 1);
  148. *dts = (packet_read_char(demuxer) >> 1) & 7;  /* Only low 4 bits (7==1111) */
  149. *dts <<= 15;
  150. *dts |= (packet_read_int16(demuxer) >> 1);
  151. *dts <<= 15;
  152. *dts |= (packet_read_int16(demuxer) >> 1);
  153. pes_header_bytes += 10;
  154.   }
  155. demuxer->time = (double)*pts / 90000;
  156. //printf("get_pes_packet_header %fn", demuxer->time);
  157. /* extract other stuff here! */
  158. packet_skip(demuxer, pes_header_data_length - pes_header_bytes);
  159. return 0;
  160. }
  161. static int get_unknown_data(mpeg3_demuxer_t *demuxer)
  162. {
  163. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  164. return 0;
  165. }
  166. // Combine the pid and the stream id into one unit
  167. #define CUSTOM_ID(pid, stream_id) (((pid << 8) | stream_id) & 0xffff)
  168. static int get_pes_packet_data(mpeg3_demuxer_t *demuxer, unsigned int stream_id)
  169. {
  170. unsigned long pts = 0, dts = 0;
  171. get_pes_packet_header(demuxer, &pts, &dts);
  172. //printf("get_pes_packet_data %xn", CUSTOM_ID(demuxer->pid, stream_id));
  173. if(stream_id == 0xbd)
  174. {
  175. // Don't know if the next byte is the true stream id like in program stream
  176. stream_id = 0x0;
  177. if(demuxer->read_all)
  178. demuxer->astream_table[CUSTOM_ID(demuxer->pid, stream_id)] = AUDIO_AC3;
  179. if(demuxer->astream == -1)
  180.     demuxer->astream = CUSTOM_ID(demuxer->pid, stream_id);
  181.      if(CUSTOM_ID(demuxer->pid, stream_id) == demuxer->astream && 
  182. demuxer->do_audio)
  183. {
  184. // get_pes_packet_header(demuxer, &pts, &dts);
  185. demuxer->pes_audio_time = pts;
  186. demuxer->audio_pid = demuxer->pid;
  187. return get_data_buffer(demuxer);
  188.      }
  189. }
  190. else
  191. if((stream_id >> 4) == 12 || (stream_id >> 4) == 13)
  192. {
  193. /* Just pick the first available stream if no ID is set */
  194. //printf("get_pes_packet_data audio %xn", stream_id);
  195. if(demuxer->read_all)
  196. demuxer->astream_table[CUSTOM_ID(demuxer->pid, stream_id)] = AUDIO_MPEG;
  197. if(demuxer->astream == -1)
  198.     demuxer->astream = CUSTOM_ID(demuxer->pid, stream_id);
  199.      if(CUSTOM_ID(demuxer->pid, stream_id) == demuxer->astream && 
  200. demuxer->do_audio)
  201. {
  202. // get_pes_packet_header(demuxer, &pts, &dts);
  203. demuxer->pes_audio_time = pts;
  204. demuxer->audio_pid = demuxer->pid;
  205. return get_data_buffer(demuxer);
  206.      }
  207. }
  208. else 
  209. if((stream_id >> 4) == 14)
  210. {
  211. //printf("get_pes_packet_data video %xn", stream_id);
  212. /* Just pick the first available stream if no ID is set */
  213. if(demuxer->read_all)
  214. demuxer->vstream_table[CUSTOM_ID(demuxer->pid, stream_id)] = 1;
  215. else
  216. if(demuxer->vstream == -1)
  217. demuxer->vstream = (CUSTOM_ID(demuxer->pid, stream_id));
  218. if(CUSTOM_ID(demuxer->pid, stream_id) == demuxer->vstream && 
  219. demuxer->do_video)
  220. {
  221. // get_pes_packet_header(demuxer, &pts, &dts);
  222. demuxer->pes_video_time = pts;
  223. demuxer->video_pid = demuxer->pid;
  224. return get_data_buffer(demuxer);
  225. }
  226. }
  227. else 
  228. {
  229. return get_unknown_data(demuxer);
  230. }
  231. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  232. return 0;
  233. }
  234. static int get_pes_packet(mpeg3_demuxer_t *demuxer)
  235. {
  236. unsigned int stream_id;
  237. demuxer->pes_packets++;
  238. /* Skip startcode */
  239. packet_read_int24(demuxer);
  240. stream_id = packet_read_char(demuxer);
  241. /* Skip pes packet length */
  242. packet_read_int16(demuxer);
  243. if(stream_id != MPEG3_PRIVATE_STREAM_2 && 
  244. stream_id != MPEG3_PADDING_STREAM)
  245. {
  246. return get_pes_packet_data(demuxer, stream_id);
  247. }
  248. else
  249. if(stream_id == MPEG3_PRIVATE_STREAM_2)
  250. {
  251. /* Dump private data! */
  252. fprintf(stderr, "stream_id == MPEG3_PRIVATE_STREAM_2n");
  253. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  254. return 0;
  255. }
  256. else
  257. if(stream_id == MPEG3_PADDING_STREAM)
  258. {
  259. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  260. return 0;
  261. }
  262. else
  263. {
  264.      fprintf(stderr, "unknown stream_id in pes packet");
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. static int get_payload(mpeg3_demuxer_t *demuxer)
  270. {
  271. //printf("get_payload 1 %x %dn", demuxer->pid, demuxer->payload_unit_start_indicator);
  272. if(demuxer->payload_unit_start_indicator)
  273. {
  274.      if(demuxer->pid==0) get_program_association_table(demuxer);
  275.      else 
  276. if(packet_next_int24(demuxer) == MPEG3_PACKET_START_CODE_PREFIX) get_pes_packet(demuxer);
  277.      else 
  278. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  279. }
  280. else
  281. {
  282. //printf("get_payload 2n");
  283.      if(demuxer->pid == demuxer->audio_pid && demuxer->do_audio)
  284. {
  285. get_data_buffer(demuxer);
  286. }
  287.      else 
  288. if(demuxer->pid == demuxer->video_pid && demuxer->do_video)
  289. {
  290. get_data_buffer(demuxer);
  291. }
  292.      else 
  293. packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
  294. }
  295. return 0;
  296. }
  297. /* Read a transport packet */
  298. static int read_transport(mpeg3_demuxer_t *demuxer)
  299. {
  300. mpeg3_t *file = (mpeg3_t*)demuxer->file;
  301. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  302. int result = 0;
  303. unsigned int bits;
  304. int table_entry;
  305. //printf("read_transport 1 %dn", file->packet_size);
  306. /* Packet size is known for transport streams */
  307. demuxer->raw_size = file->packet_size;
  308. demuxer->raw_offset = 0;
  309. if(result)
  310. {
  311. perror("read_transport");
  312. return 1;
  313. }
  314. //printf("read transport 1n");
  315. // Search for Sync byte */
  316. do
  317. {
  318. bits = mpeg3io_read_char(title->fs);
  319. }while(!mpeg3io_eof(title->fs) && !result && bits != MPEG3_SYNC_BYTE);
  320. //printf("read transport 2 bits=%x tell=%llx packet_size=%xn", bits, mpeg3io_tell(title->fs), file->packet_size);
  321. if(bits == MPEG3_SYNC_BYTE && !result)
  322. {
  323. demuxer->raw_data[0] = MPEG3_SYNC_BYTE;
  324. result = mpeg3io_read_data(demuxer->raw_data + 1, 
  325. file->packet_size - 1, 
  326. title->fs);
  327. }
  328. else
  329. return 1;
  330. packet_read_char(demuxer);
  331.     bits =  packet_read_int24(demuxer) & 0x00ffffff;
  332. //printf("read transport 3 tell=%x bits=%xn", mpeg3io_tell(title->fs), bits);
  333.     demuxer->transport_error_indicator = (bits >> 23) & 0x1;
  334.     demuxer->payload_unit_start_indicator = (bits >> 22) & 0x1;
  335.     demuxer->pid = (bits >> 8) & 0x00001fff;
  336.     demuxer->transport_scrambling_control = (bits >> 6) & 0x3;
  337.     demuxer->adaptation_field_control = (bits >> 4) & 0x3;
  338.     demuxer->continuity_counter = bits & 0xf;
  339. //printf("read_transport 4 %xn", demuxer->pid);
  340. if(demuxer->transport_error_indicator)
  341. {
  342. fprintf(stderr, "demuxer->transport_error_indicatorn");
  343. return 1;
  344. }
  345. //printf("read_transport 5 %xn", demuxer->pid);
  346.     if (demuxer->pid == 0x1fff)
  347. {
  348. demuxer->is_padding = 1;  /* padding; just go to next */
  349. return 0;
  350.     }
  351. else
  352. {
  353. demuxer->is_padding = 0;
  354. }
  355. //printf("read_transport 6 %xn", demuxer->pid);
  356. /* Get pid */
  357. for(table_entry = 0, result = 0; 
  358. table_entry < demuxer->total_pids; 
  359. table_entry++)
  360. {
  361. if(demuxer->pid == demuxer->pid_table[table_entry])
  362. {
  363. result = 1;
  364. break;
  365. }
  366. }
  367. //printf("read_transport 7 %xn", demuxer->pid);
  368. /* Not in pid table */
  369. if(!result)
  370. {
  371. demuxer->pid_table[table_entry] = demuxer->pid;
  372. demuxer->continuity_counters[table_entry] = demuxer->continuity_counter;  /* init */
  373. demuxer->total_pids++;
  374. //printf("read_transport program id detected %xn", demuxer->pid);
  375. }
  376. result = 0;
  377. #if 0
  378. /* Check counters */
  379.     if(demuxer->pid != MPEG3_PROGRAM_ASSOCIATION_TABLE && 
  380. demuxer->pid != MPEG3_CONDITIONAL_ACCESS_TABLE &&
  381.         (demuxer->adaptation_field_control == 1 || demuxer->adaptation_field_control == 3))
  382. {
  383. if(demuxer->continuity_counters[table_entry] != demuxer->continuity_counter)
  384. {
  385. // fprintf(stderr, "demuxer->continuity_counters[table_entry] != demuxer->continuity_countern");
  386. /* Reset it */
  387. demuxer->continuity_counters[table_entry] = demuxer->continuity_counter;
  388. }
  389. if(++(demuxer->continuity_counters[table_entry]) > 15) demuxer->continuity_counters[table_entry] = 0;
  390. }
  391. #endif
  392. //printf("read_transport 8 %x %xn", demuxer->adaptation_field_control, demuxer->pid);
  393.     if(demuxer->adaptation_field_control == 2 || 
  394. demuxer->adaptation_field_control == 3)
  395.      result = get_adaptation_field(demuxer);
  396. // Need to enter in astream and vstream table:
  397. // PID ored with stream_id
  398.     if(demuxer->adaptation_field_control == 1 || 
  399. demuxer->adaptation_field_control == 3)
  400.      result = get_payload(demuxer);
  401. return result;
  402. }
  403. static int get_system_header(mpeg3_demuxer_t *demuxer)
  404. {
  405. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  406. int length = mpeg3io_read_int16(title->fs);
  407. mpeg3io_seek_relative(title->fs, length);
  408. return 0;
  409. }
  410. static unsigned long get_timestamp(mpeg3_demuxer_t *demuxer)
  411. {
  412. unsigned long timestamp;
  413. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  414. /* Only low 4 bits (7==1111) */
  415. timestamp = (mpeg3io_read_char(title->fs) >> 1) & 7;  
  416. timestamp <<= 15;
  417. timestamp |= (mpeg3io_read_int16(title->fs) >> 1);
  418. timestamp <<= 15;
  419. timestamp |= (mpeg3io_read_int16(title->fs) >> 1);
  420. return timestamp;
  421. }
  422. static int get_pack_header(mpeg3_demuxer_t *demuxer)
  423. {
  424. unsigned long i, j;
  425. unsigned long clock_ref, clock_ref_ext;
  426. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  427. /* Get the time code */
  428. if((mpeg3io_next_char(title->fs) >> 4) == 2)
  429. {
  430. /* MPEG-1 */
  431. demuxer->time = (double)get_timestamp(demuxer) / 90000;
  432. /* Skip 3 bytes */
  433. mpeg3io_read_int24(title->fs);
  434. }
  435. else
  436. if(mpeg3io_next_char(title->fs) & 0x40)
  437. {
  438. i = mpeg3io_read_int32(title->fs);
  439. j = mpeg3io_read_int16(title->fs);
  440. if(i & 0x40000000 || (i >> 28) == 2)
  441. {
  442.      clock_ref = ((i & 0x38000000) << 3);
  443.      clock_ref |= ((i & 0x03fff800) << 4);
  444.      clock_ref |= ((i & 0x000003ff) << 5);
  445.      clock_ref |= ((j & 0xf800) >> 11);
  446.      clock_ref_ext = (j >> 1) & 0x1ff;
  447.     demuxer->time = (double)(clock_ref + clock_ref_ext / 300) / 90000;
  448. /* Skip 3 bytes */
  449. mpeg3io_read_int24(title->fs);
  450. i = mpeg3io_read_char(title->fs) & 0x7;
  451. /* stuffing */
  452. mpeg3io_seek_relative(title->fs, i);  
  453. }
  454. }
  455. else
  456. {
  457. mpeg3io_seek_relative(title->fs, 2);
  458. }
  459. return 0;
  460. }
  461. /* Program packet reading core */
  462. static int get_ps_pes_packet(mpeg3_demuxer_t *demuxer, unsigned int header)
  463. {
  464. unsigned long pts = 0, dts = 0;
  465. int stream_id;
  466. int pes_packet_length;
  467. long pes_packet_start;
  468. long decryption_offset;
  469. int i;
  470. mpeg3_t *file = demuxer->file;
  471. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  472. int scrambling = 0;
  473. stream_id = header & 0xff;
  474. pes_packet_length = mpeg3io_read_int16(title->fs);
  475. pes_packet_start = mpeg3io_tell(title->fs);
  476. if(stream_id != MPEG3_PRIVATE_STREAM_2 &&
  477. stream_id != MPEG3_PADDING_STREAM)
  478. {
  479. if((mpeg3io_next_char(title->fs) >> 6) == 0x02)
  480. {
  481. /* Get MPEG-2 packet */
  482. int pes_header_bytes = 0;
  483.      int pts_dts_flags;
  484. int pes_header_data_length;
  485. demuxer->last_packet_decryption = mpeg3io_tell(title->fs);
  486. scrambling = mpeg3io_read_char(title->fs) & 0x30;
  487. //scrambling = 1;
  488. /* Reset scrambling bit for the mpeg3cat utility */
  489. // if(scrambling) demuxer->raw_data[demuxer->raw_offset - 1] &= 0xcf;
  490.      pts_dts_flags = (mpeg3io_read_char(title->fs) >> 6) & 0x3;
  491. pes_header_data_length = mpeg3io_read_char(title->fs);
  492. /* Get Presentation and Decoding Time Stamps */
  493. if(pts_dts_flags == 2)
  494. {
  495. pts = (mpeg3io_read_char(title->fs) >> 1) & 7;  /* Only low 4 bits (7==1111) */
  496. pts <<= 15;
  497. pts |= (mpeg3io_read_int16(title->fs) >> 1);
  498. pts <<= 15;
  499. pts |= (mpeg3io_read_int16(title->fs) >> 1);
  500. pes_header_bytes += 5;
  501. }
  502.      else 
  503. if(pts_dts_flags == 3)
  504. {
  505.          pts = (mpeg3io_read_char(title->fs) >> 1) & 7;  /* Only low 4 bits (7==1111) */
  506.          pts <<= 15;
  507.          pts |= (mpeg3io_read_int16(title->fs) >> 1);
  508.          pts <<= 15;
  509.          pts |= (mpeg3io_read_int16(title->fs) >> 1);
  510.          dts = (mpeg3io_read_char(title->fs) >> 1) & 7;  /* Only low 4 bits (7==1111) */
  511.          dts <<= 15;
  512.          dts |= (mpeg3io_read_int16(title->fs) >> 1);
  513.          dts <<= 15;
  514.          dts |= (mpeg3io_read_int16(title->fs) >> 1);
  515.          pes_header_bytes += 10;
  516.      }
  517. /* Skip unknown */
  518.          mpeg3io_seek_relative(title->fs, 
  519. pes_header_data_length - pes_header_bytes);
  520. }
  521. else
  522. {
  523. int pts_dts_flags;
  524. /* Get MPEG-1 packet */
  525. while(mpeg3io_next_char(title->fs) == 0xff)
  526. {
  527. mpeg3io_read_char(title->fs);
  528. }
  529. /* Skip STD buffer scale */
  530. if((mpeg3io_next_char(title->fs) & 0x40) == 0x40)
  531. {
  532. mpeg3io_seek_relative(title->fs, 2);
  533. }
  534. /* Decide which timestamps are available */
  535. pts_dts_flags = mpeg3io_next_char(title->fs);
  536. if(pts_dts_flags >= 0x30)
  537. {
  538. /* Get the presentation and decoding time stamp */
  539. pts = get_timestamp(demuxer);
  540. dts = get_timestamp(demuxer);
  541. }
  542. else
  543. if(pts_dts_flags >= 0x20)
  544. {
  545. /* Get just the presentation time stamp */
  546. pts = get_timestamp(demuxer);
  547. }
  548. else
  549. if(pts_dts_flags == 0x0f)
  550. {
  551. /* End of timestamps */
  552. mpeg3io_read_char(title->fs);
  553. }
  554. else
  555. {
  556. return 1;     /* Error */
  557. }
  558. }
  559. /* Now extract the payload. */
  560. if((stream_id >> 4) == 0xc || (stream_id >> 4) == 0xd)
  561. {
  562. /* Audio data */
  563. /* Take first stream ID if -1 */
  564. pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
  565. if(demuxer->read_all)
  566. demuxer->astream_table[stream_id & 0x0f] = AUDIO_MPEG;
  567. else
  568. if(demuxer->astream == -1) 
  569. demuxer->astream = stream_id & 0x0f;
  570. demuxer->pes_audio_time = demuxer->time;
  571. if(pts) demuxer->pes_audio_time = pts;
  572. if((stream_id & 0x0f) == demuxer->astream && demuxer->do_audio)
  573. {
  574. decryption_offset = mpeg3io_tell(title->fs) - demuxer->last_packet_start;
  575. mpeg3io_read_data(demuxer->data_buffer + demuxer->data_size, 
  576. pes_packet_length, 
  577. title->fs);
  578. demuxer->data_size += pes_packet_length;
  579.    }
  580. else 
  581. {
  582.      mpeg3io_seek_relative(title->fs, pes_packet_length);
  583. }
  584. }
  585.      else 
  586. if((stream_id >> 4) == 0xe)
  587. {
  588. /* Video data */
  589. /* Take first stream ID if -1 */
  590. pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
  591. if(demuxer->read_all) 
  592. demuxer->vstream_table[stream_id & 0x0f] = 1;
  593. else
  594. if(demuxer->vstream == -1) 
  595. demuxer->vstream = stream_id & 0x0f;
  596.         demuxer->pes_video_time  = demuxer->time;;
  597.         if(pts) demuxer->pes_video_time = pts;
  598.          if((stream_id & 0x0f) == demuxer->vstream && demuxer->do_video)
  599. {
  600. //printf(__FUNCTION__ " stream_id=%x size=%xn", stream_id, pes_packet_length);
  601. decryption_offset = mpeg3io_tell(title->fs) - demuxer->last_packet_start;
  602. mpeg3io_read_data(demuxer->data_buffer + demuxer->data_size, 
  603. pes_packet_length, 
  604. title->fs);
  605. demuxer->data_size += pes_packet_length;
  606.         }
  607.      else 
  608. {
  609.      mpeg3io_seek_relative(title->fs, pes_packet_length);
  610.      }
  611.      }
  612.      else 
  613. if((stream_id == 0xbd || stream_id == 0xbf) && 
  614. mpeg3io_next_char(title->fs) != 0xff &&
  615. ((mpeg3io_next_char(title->fs) & 0xf0) != 0x20))
  616. {
  617. /* DVD audio data */
  618. /* Get the audio format */
  619. int format;
  620. //printf("get_ps_pes_packet 5 %xn", packet_next_char(demuxer) & 0xf0);
  621. if((mpeg3io_next_char(title->fs) & 0xf0) == 0xa0)
  622. format = AUDIO_PCM;
  623. else
  624. format = AUDIO_AC3;
  625. // Picks up bogus data if (& 0xf) or (& 0x7f)
  626. stream_id = mpeg3io_next_char(title->fs);
  627. demuxer->pes_audio_time = demuxer->time;
  628.         if(pts) demuxer->pes_audio_time = pts;
  629. //printf("get_ps_pes_packet %xn", stream_id);
  630. /* Take first stream ID if not building TOC. */
  631. if(demuxer->read_all)
  632. demuxer->astream_table[stream_id] = format;
  633. else
  634. if(demuxer->astream == -1)
  635. demuxer->astream = stream_id;
  636.        if(stream_id == demuxer->astream && demuxer->do_audio)
  637. {
  638. demuxer->aformat = format;
  639. mpeg3io_read_int32(title->fs);
  640. pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
  641. decryption_offset = mpeg3io_tell(title->fs) - demuxer->last_packet_start;
  642. mpeg3io_read_data(demuxer->data_buffer + demuxer->data_size, 
  643. pes_packet_length, 
  644. title->fs);
  645. demuxer->data_size += pes_packet_length;
  646.        }
  647.        else
  648. {
  649. pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
  650.      mpeg3io_seek_relative(title->fs, pes_packet_length);
  651.        }
  652. //printf("get_ps_pes_packet 6 %dn", demuxer->astream_table[0x20]);
  653.      }
  654.      else 
  655. if(stream_id == 0xbc || 1)
  656. {
  657. pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
  658.      mpeg3io_seek_relative(title->fs, pes_packet_length);
  659.      }
  660. }
  661.    else 
  662. if(stream_id == MPEG3_PRIVATE_STREAM_2 || stream_id == MPEG3_PADDING_STREAM)
  663. {
  664. pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
  665.      mpeg3io_seek_relative(title->fs, pes_packet_length);
  666.    }
  667. // Advance 2048 bytes if scrambled.  We might pick up a spurrius
  668. // packet start code in the scrambled data otherwise.
  669. if(scrambling && 
  670. demuxer->last_packet_start + 0x800 > mpeg3io_tell(title->fs))
  671. {
  672. mpeg3io_seek_relative(title->fs, 
  673. demuxer->last_packet_start + 0x800 - mpeg3io_tell(title->fs));
  674. }
  675. // Descramble if desired
  676. if(demuxer->data_size && scrambling)
  677. {
  678. //printf(__FUNCTION__ " data_size=%x decryption_offset=%xn", demuxer->data_size, decryption_offset);
  679. if(mpeg3_decrypt_packet(title->fs->css, 
  680. demuxer->data_buffer,
  681. decryption_offset))
  682. {
  683. fprintf(stderr, "get_ps_pes_packet: Decryption not availablen");
  684. return 1;
  685. }
  686. }
  687. //if(mpeg3io_tell(title->fs) - demuxer->last_packet_start != 0x800)
  688. // printf(__FUNCTION__ " packet size == %dn", mpeg3io_tell(title->fs) - demuxer->last_packet_start);
  689. return 0;
  690. }
  691. int mpeg3demux_read_program(mpeg3_demuxer_t *demuxer)
  692. {
  693. int result = 0;
  694. int count = 0;
  695. mpeg3_t *file = demuxer->file;
  696. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  697. unsigned int header = 0;
  698. int pack_count = 0;
  699. demuxer->data_size = 0;
  700. //printf("mpeg3demux_read_program 1 %d %x %llxn", result, title->fs->current_byte, title->fs->total_bytes);
  701. if(mpeg3io_eof(title->fs)) return 1;
  702. //printf("mpeg3demux_read_program 2 %d %x %llxn", result, title->fs->current_byte, title->fs->total_bytes);
  703. /* Search for next header */
  704. /* Parse packet until the next packet start code */
  705. while(!result && !mpeg3io_eof(title->fs))
  706. {
  707. header = mpeg3io_read_int32(title->fs);
  708. if(header == MPEG3_PACK_START_CODE)
  709. {
  710. // Start of next packet
  711. if(pack_count)
  712. {
  713. mpeg3io_seek_relative(title->fs, -4);
  714. break;
  715. }
  716. demuxer->last_packet_start = mpeg3io_tell(title->fs) - 4;
  717. result = get_pack_header(demuxer);
  718. //printf("mpeg3demux_read_program MPEG3_PACK_START_CODE %dn", result);
  719. pack_count++;
  720. }
  721. else
  722. if(header == MPEG3_SYSTEM_START_CODE && pack_count)
  723. {
  724.   result = get_system_header(demuxer);
  725. //printf("mpeg3demux_read_program MPEG3_SYSTEM_START_CODE %dn", result);
  726. }
  727. else
  728. if((header >> 8) == MPEG3_PACKET_START_CODE_PREFIX && pack_count)
  729. {
  730. result = get_ps_pes_packet(demuxer, header);
  731. //printf("mpeg3demux_read_program MPEG3_PACKET_START_CODE_PREFIX %d %08xn", result, header);
  732. }
  733. else
  734. {
  735. // Try again.
  736. mpeg3io_seek_relative(title->fs, -3);
  737. }
  738. }
  739. //printf("mpeg3demux_read_program 3 %d %x %llxn", result, title->fs->current_byte, title->fs->total_bytes);
  740. // Ignore errors in the parsing.  Just quit if eof.
  741. result = 0;
  742. demuxer->last_packet_end = mpeg3io_tell(title->fs);
  743. /*
  744.  * if(demuxer->last_packet_end - demuxer->last_packet_start != 0x800)
  745.  *  printf(__FUNCTION__ " packet_size=%x data_size=%x packet_start=%xn",
  746.  *  demuxer->last_packet_end - demuxer->last_packet_start,
  747.  *  demuxer->data_size,
  748.  *  demuxer->last_packet_start);
  749.  */
  750. //printf("mpeg3demux_read_program 5 %dn", result);
  751. //printf("read_program 3n");
  752. // if(!result) result = mpeg3io_eof(title->fs);
  753. return result;
  754. }
  755. static double lookup_time_offset(mpeg3_demuxer_t *demuxer, long byte)
  756. {
  757. int i;
  758. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  759. if(!title->timecode_table_size) return 0;
  760. for(i = title->timecode_table_size - 1; 
  761. i >= 0 && title->timecode_table[i].start_byte > byte;
  762. i--)
  763. ;
  764. if(i < 0) i = 0;
  765. return title->timecode_table[i].absolute_start_time - title->timecode_table[i].start_time;
  766. }
  767. int mpeg3_advance_timecode(mpeg3_demuxer_t *demuxer)
  768. {
  769. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  770. int result = 0;
  771. int do_seek = 0;
  772. int last_timecode = demuxer->current_timecode;
  773. //printf("mpeg3_advance_timecode 0n");fflush(stdout);
  774. /* Don't do anything when constructing timecode table */
  775. if(!title->timecode_table || 
  776. !title->timecode_table_size || 
  777. demuxer->read_all) return 0;
  778. //printf("mpeg3_advance_timecode 1n");fflush(stdout);
  779. if(!demuxer->reverse)
  780. {
  781. /* Get inside the current timecode */
  782. if(mpeg3io_tell(title->fs) < title->timecode_table[demuxer->current_timecode].start_byte)
  783. {
  784. mpeg3io_seek(title->fs, 
  785. title->timecode_table[demuxer->current_timecode].start_byte);
  786. }
  787. /* Get the next timecode */
  788. while(!result && 
  789. (mpeg3io_tell(title->fs) >= title->timecode_table[demuxer->current_timecode].end_byte ||
  790. demuxer->current_program != title->timecode_table[demuxer->current_timecode].program))
  791. {
  792. demuxer->current_timecode++;
  793. if(demuxer->current_timecode >= title->timecode_table_size)
  794. {
  795. demuxer->current_timecode = 0;
  796. if(demuxer->current_title + 1 < demuxer->total_titles)
  797. {
  798. mpeg3demux_open_title(demuxer, ++demuxer->current_title);
  799. do_seek = 1;
  800. }
  801. else
  802. {
  803. mpeg3io_seek(title->fs, mpeg3io_total_bytes(title->fs));
  804.   result = 1;
  805. }
  806. }
  807. title = demuxer->titles[demuxer->current_title];
  808. }
  809. //if(last_timecode != demuxer->current_timecode && demuxer->do_video)
  810. // printf("using title %d cell %x-%xn", demuxer->current_title, title->timecode_table[demuxer->current_timecode].start_byte, title->timecode_table[demuxer->current_timecode].end_byte);
  811. //printf("2 %dn", title->timecode_table[demuxer->current_timecode].program);
  812. if(!result && do_seek)
  813. {
  814. //printf("current_cell=%dn", demuxer->current_timecode);
  815. mpeg3io_seek(title->fs, 
  816. title->timecode_table[demuxer->current_timecode].start_byte);
  817. }
  818. }
  819. else
  820. {
  821. /* Get the previous timecode */
  822. while(!result && 
  823. (mpeg3io_tell(title->fs) < title->timecode_table[demuxer->current_timecode].start_byte ||
  824. demuxer->current_program != title->timecode_table[demuxer->current_timecode].program))
  825. {
  826. /*
  827.  * printf("mpeg3_reverse_timecode %d %d %d %dn", 
  828.  *  mpeg3io_tell(title->fs), 
  829.  *  demuxer->current_timecode,
  830.  *  title->timecode_table[demuxer->current_timecode].start_byte,
  831.  *  title->timecode_table[demuxer->current_timecode].end_byte);
  832.  */
  833. demuxer->current_timecode--;
  834. if(demuxer->current_timecode < 0)
  835. {
  836. if(demuxer->current_title > 0)
  837. {
  838. //printf("advance_timecode 2 %dn", demuxer->current_title);
  839. mpeg3demux_open_title(demuxer, --demuxer->current_title);
  840. title = demuxer->titles[demuxer->current_title];
  841. // Seek to end since we opened at the beginning of the next title
  842. mpeg3io_seek(title->fs, title->total_bytes);
  843. //printf("advance_timecode 3 %d %dn", demuxer->current_title, mpeg3io_tell(title->fs));
  844. demuxer->current_timecode = title->timecode_table_size - 1;
  845. do_seek = 1;
  846. }
  847. else
  848. {
  849. mpeg3io_seek(title->fs, 0);
  850. demuxer->current_timecode = 0;
  851. result = 1;
  852. }
  853. }
  854. }
  855. if(!result && do_seek)
  856. {
  857. mpeg3io_seek(title->fs, 
  858. title->timecode_table[demuxer->current_timecode].start_byte);
  859. }
  860. }
  861. //printf("mpeg3_advance_timecode 2 %dn", demuxer->current_title);fflush(stdout);
  862. return result;
  863. }
  864. /* Read packet in the forward direction */
  865. int mpeg3_read_next_packet(mpeg3_demuxer_t *demuxer)
  866. {
  867. int result = 0;
  868. long current_position;
  869. mpeg3_t *file = demuxer->file;
  870. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  871. demuxer->data_size = 0;
  872. demuxer->data_position = 0;
  873. //printf("mpeg3_read_next_packet 1 %d %llxn", demuxer->current_title, mpeg3io_tell(title->fs));
  874. /* Switch to forward direction. */
  875. if(demuxer->reverse)
  876. {
  877. if(file->packet_size > 0)
  878. {
  879. if(!result) result = mpeg3io_seek_relative(title->fs, file->packet_size);
  880. }
  881. else
  882. {
  883. if(!result) result = mpeg3io_next_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
  884. if(!result) result = mpeg3io_next_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
  885. }
  886. demuxer->reverse = 0;
  887. }
  888. //printf("mpeg3_read_next_packet 4 %dn", result);
  889. /* Read packets until the output buffer is full */
  890. if(!result)
  891. {
  892. do
  893. {
  894. result = mpeg3_advance_timecode(demuxer);
  895. if(!result)
  896. {
  897. demuxer->time_offset = lookup_time_offset(demuxer, mpeg3io_tell(title->fs));
  898. if(file->is_transport_stream)
  899. {
  900. //printf("mpeg3_read_next_packet: 1 %fn", demuxer->time);
  901. result = read_transport(demuxer);
  902. //printf("mpeg3_read_next_packet: 2 %fn", demuxer->time);
  903. }
  904. else
  905. if(file->is_program_stream)
  906. {
  907. result = mpeg3demux_read_program(demuxer);
  908. }
  909. else
  910. {
  911. /* Read elementary stream. */
  912. //printf("mpeg3_read_next_packet: 3n");
  913. result = mpeg3io_read_data(demuxer->data_buffer, 
  914. file->packet_size, title->fs);
  915. if(!result) demuxer->data_size = file->packet_size;
  916. }
  917. }
  918. //printf("mpeg3_read_next_packet 2 %x %lxn", demuxer->data_size, mpeg3io_tell(title->fs));
  919. }while(!result && 
  920. demuxer->data_size == 0 && 
  921. (demuxer->do_audio || demuxer->do_video));
  922. }
  923. //printf("mpeg3_read_next_packet 4 %dn", result);
  924. return result;
  925. }
  926. /* Read the packet right before the packet we're currently on. */
  927. int mpeg3_read_prev_packet(mpeg3_demuxer_t *demuxer)
  928. {
  929. int result = 0;
  930. mpeg3_t *file = demuxer->file;
  931. long current_position;
  932. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  933. demuxer->data_size = 0;
  934. demuxer->data_position = 0;
  935. //printf("mpeg3_read_prev_packet 1 %x %xn", title->fs->current_byte, title->fs->buffer_position);
  936. /* Switch to reverse direction */
  937. if(!demuxer->reverse)
  938. {
  939. if(file->packet_size > 0)
  940. {
  941. result = mpeg3io_seek_relative(title->fs, -file->packet_size);
  942. }
  943. else
  944. {
  945. result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
  946. }
  947. demuxer->reverse = 1;
  948. }
  949. //printf("mpeg3_read_prev_packet 2 %x %xn", title->fs->current_byte, title->fs->buffer_position);
  950. do
  951. {
  952. // Go to beginning of previous packet
  953. if(file->packet_size > 0)
  954. {
  955. if(!result) result = mpeg3io_seek_relative(title->fs, -file->packet_size);
  956. }
  957. else
  958. {
  959. if(!result) result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
  960. }
  961. //printf("mpeg3_read_prev_packet 3 %x %xn", title->fs->current_byte, title->fs->buffer_position);
  962. if(!result) result = mpeg3_advance_timecode(demuxer);
  963. //printf("mpeg3_read_prev_packet 2 %p->%p->%pn", title, title->fs, title->fs->fd);
  964. if(!result) demuxer->time_offset = lookup_time_offset(demuxer, mpeg3io_tell(title->fs));
  965. // Read packet and then rewind it
  966. if(file->is_transport_stream && !result)
  967. {
  968. result = read_transport(demuxer);
  969. if(!mpeg3io_bof(title->fs))
  970. {
  971. result = mpeg3io_seek_relative(title->fs, -file->packet_size);
  972. }
  973. //printf("mpeg3_read_prev_packet 4 %x %xn", title->fs->current_byte, title->fs->buffer_position);
  974. }
  975. else
  976. if(file->is_program_stream && !result)
  977. {
  978. result = mpeg3demux_read_program(demuxer);
  979. //printf("mpeg3_read_prev_packet 4 %x %xn", title->fs->current_byte);
  980. if(!mpeg3io_bof(title->fs))
  981. result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
  982. //printf("mpeg3_read_prev_packet 5 %x %xn", title->fs->current_byte);
  983. }
  984. else
  985. if(!result)
  986. {
  987. /* Elementary stream */
  988. /* Read the packet forwards and seek back to the start */
  989. result = mpeg3io_read_data(demuxer->data_buffer, 
  990. file->packet_size, 
  991. title->fs);
  992. if(!result) 
  993. {
  994. demuxer->data_size = file->packet_size;
  995. result = mpeg3io_seek_relative(title->fs, -file->packet_size);
  996. }
  997. }
  998. }while(!result && 
  999. demuxer->data_size == 0 && 
  1000. (demuxer->do_audio || demuxer->do_video));
  1001. //printf("mpeg3_read_prev_packet 4 %x %xn", title->fs->current_byte, title->fs->buffer_position);
  1002. return result;
  1003. }
  1004. /* For audio */
  1005. int mpeg3demux_read_data(mpeg3_demuxer_t *demuxer, 
  1006. unsigned char *output, 
  1007. long size)
  1008. {
  1009. long i;
  1010. int result = 0;
  1011. mpeg3_t *file = demuxer->file;
  1012. demuxer->error_flag = 0;
  1013. if(demuxer->data_position >= 0)
  1014. {
  1015. /* Read forwards */
  1016. for(i = 0; i < size && !result; )
  1017. {
  1018. int fragment_size = size - i;
  1019. if(fragment_size > demuxer->data_size - demuxer->data_position)
  1020. fragment_size = demuxer->data_size - demuxer->data_position;
  1021. memcpy(output + i, demuxer->data_buffer + demuxer->data_position, fragment_size);
  1022. demuxer->data_position += fragment_size;
  1023. i += fragment_size;
  1024. if(i < size)
  1025. {
  1026. result = mpeg3_read_next_packet(demuxer);
  1027. }
  1028. }
  1029. return i;
  1030. }
  1031. else
  1032. {
  1033. /* Read backwards a full packet. */
  1034. /* Only good for reading less than the size of a full packet, but */
  1035. /* this routine should only be used for searching for previous markers. */
  1036. long current_position = demuxer->data_position;
  1037. result = mpeg3_read_prev_packet(demuxer);
  1038. if(!result) demuxer->data_position = demuxer->data_size + current_position;
  1039. memcpy(output, demuxer->data_buffer + demuxer->data_position, size);
  1040. demuxer->data_position += size;
  1041. }
  1042. demuxer->error_flag = result;
  1043. return result;
  1044. }
  1045. unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t *demuxer)
  1046. {
  1047. demuxer->error_flag = 0;
  1048. if(demuxer->data_position >= demuxer->data_size)
  1049. demuxer->error_flag = mpeg3_read_next_packet(demuxer);
  1050. demuxer->next_char = demuxer->data_buffer[demuxer->data_position++];
  1051. return demuxer->next_char;
  1052. }
  1053. unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t *demuxer)
  1054. {
  1055. demuxer->error_flag = 0;
  1056. demuxer->data_position--;
  1057. if(demuxer->data_position < 0)
  1058. {
  1059. //printf("mpeg3demux_read_prev_char_packet 1n");
  1060. demuxer->error_flag = mpeg3_read_prev_packet(demuxer);
  1061. //printf("mpeg3demux_read_prev_char_packet 2n");
  1062. if(!demuxer->error_flag) demuxer->data_position = demuxer->data_size - 1;
  1063. }
  1064. demuxer->next_char = demuxer->data_buffer[demuxer->data_position];
  1065. return demuxer->next_char;
  1066. }
  1067. static mpeg3demux_timecode_t* next_timecode(mpeg3_demuxer_t *demuxer, 
  1068. int *current_title, 
  1069. int *current_timecode,
  1070. int current_program)
  1071. {
  1072. int done = 0;
  1073. while(!done)
  1074. {
  1075. /* Increase timecode number */
  1076. if(*current_timecode < demuxer->titles[*current_title]->timecode_table_size - 1) 
  1077. {
  1078. (*current_timecode)++;
  1079. if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
  1080. return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
  1081. }
  1082. else
  1083. /* Increase title number */
  1084. if(*current_title < demuxer->total_titles - 1)
  1085. {
  1086. (*current_title)++;
  1087. (*current_timecode) = 0;
  1088. if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
  1089. return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
  1090. }
  1091. else
  1092. /* End of disk */
  1093. done = 1;
  1094. }
  1095. return 0;
  1096. }
  1097. static mpeg3demux_timecode_t* prev_timecode(mpeg3_demuxer_t *demuxer, 
  1098. int *current_title, 
  1099. int *current_timecode,
  1100. int current_program)
  1101. {
  1102. int done = 0;
  1103. while(!done)
  1104. {
  1105. /* Increase timecode number */
  1106. if(*current_timecode > 0)
  1107. {
  1108. (*current_timecode)--;
  1109. if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
  1110. return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
  1111. }
  1112. else
  1113. /* Increase title number */
  1114. if(*current_title > 0)
  1115. {
  1116. (*current_title)--;
  1117. (*current_timecode) = demuxer->titles[*current_title]->timecode_table_size - 1;
  1118. if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
  1119. return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
  1120. }
  1121. else
  1122. /* End of disk */
  1123. done = 1;
  1124. }
  1125. return 0;
  1126. }
  1127. int mpeg3demux_open_title(mpeg3_demuxer_t *demuxer, int title_number)
  1128. {
  1129. mpeg3_title_t *title;
  1130. //printf("mpeg3demux_open_title 1n");
  1131. if(title_number < demuxer->total_titles)
  1132. {
  1133. if(demuxer->current_title >= 0)
  1134. {
  1135. mpeg3io_close_file(demuxer->titles[demuxer->current_title]->fs);
  1136. demuxer->current_title = -1;
  1137. }
  1138. //printf("mpeg3demux_open_title %p %p %dn", demuxer, demuxer->titles, title_number);
  1139. title = demuxer->titles[title_number];
  1140. if(mpeg3io_open_file(title->fs))
  1141. {
  1142. demuxer->error_flag = 1;
  1143. fprintf(stderr, "mpeg3demux_open_title %s: %s", title->fs->path, strerror(errno));
  1144. }
  1145. else
  1146. {
  1147. demuxer->current_title = title_number;
  1148. }
  1149. }
  1150. //printf("mpeg3demux_open_title 2n");
  1151. // demuxer->current_timecode = 0;
  1152. return demuxer->error_flag;
  1153. }
  1154. /* Assign program numbers to interleaved programs */
  1155. int mpeg3demux_assign_programs(mpeg3_demuxer_t *demuxer)
  1156. {
  1157. int current_program = 0;
  1158. int current_title = 0;
  1159. int current_timecode = 0;
  1160. double current_time;
  1161. mpeg3demux_timecode_t *timecode;
  1162. int total_programs = 1;
  1163. int i, j;
  1164. int program_exists, last_program_assigned = 0;
  1165. int total_timecodes;
  1166. mpeg3_title_t **titles = demuxer->titles;
  1167. for(i = 0, total_timecodes = 0; i < demuxer->total_titles; i++)
  1168. {
  1169. total_timecodes += demuxer->titles[i]->timecode_table_size;
  1170. for(j = 0; j < demuxer->titles[i]->timecode_table_size; j++)
  1171. {
  1172. timecode = &demuxer->titles[i]->timecode_table[j];
  1173. if(timecode->program > total_programs - 1)
  1174. total_programs = timecode->program + 1;
  1175. }
  1176. }
  1177. /* Assign absolute timecodes in each program. */
  1178. for(current_program = 0; 
  1179. current_program < total_programs; 
  1180. current_program++)
  1181. {
  1182. current_time = 0;
  1183. current_title = 0;
  1184. current_timecode = -1;
  1185. while(timecode = next_timecode(demuxer, 
  1186.     &current_title, 
  1187. &current_timecode, 
  1188. current_program))
  1189. {
  1190. timecode->absolute_start_time = current_time;
  1191. /*
  1192.  * printf("mpeg3demux_assign_programs %p end: %f start: %fn", 
  1193.  *  timecode,
  1194.  *  timecode->end_time,
  1195.  *  timecode->start_time);
  1196.  */
  1197. current_time += fabs(timecode->end_time - timecode->start_time);
  1198. timecode->absolute_end_time = current_time;
  1199. }
  1200. }
  1201. //for(i = 0; i < demuxer->total_titles; i++) mpeg3_dump_title(demuxer->titles[i]);
  1202. demuxer->current_program = 0;
  1203. return 0;
  1204. }
  1205. int mpeg3demux_copy_titles(mpeg3_demuxer_t *dst, mpeg3_demuxer_t *src)
  1206. {
  1207. long i;
  1208. mpeg3_t *file = dst->file;
  1209. mpeg3_title_t *dst_title, *src_title;
  1210. dst->total_titles = src->total_titles;
  1211. dst->total_programs = src->total_programs;
  1212. for(i = 0; i < MPEG3_MAX_STREAMS; i++)
  1213. {
  1214. dst->astream_table[i] = src->astream_table[i];
  1215. dst->vstream_table[i] = src->vstream_table[i];
  1216. }
  1217. for(i = 0; i < src->total_titles; i++)
  1218. {
  1219. src_title = src->titles[i];
  1220. dst_title = dst->titles[i] = mpeg3_new_title(file, src->titles[i]->fs->path);
  1221. mpeg3_copy_title(dst_title, src_title);
  1222. }
  1223. mpeg3demux_open_title(dst, src->current_title);
  1224. dst->current_timecode = 0;
  1225. return 0;
  1226. }
  1227. /* ==================================================================== */
  1228. /*                            Entry points */
  1229. /* ==================================================================== */
  1230. mpeg3_demuxer_t* mpeg3_new_demuxer(mpeg3_t *file, int do_audio, int do_video, int stream_id)
  1231. {
  1232. mpeg3_demuxer_t *demuxer = calloc(1, sizeof(mpeg3_demuxer_t));
  1233. int i;
  1234. /* The demuxer will change the default packet size for its own use. */
  1235. demuxer->file = file;
  1236. demuxer->do_audio = do_audio;
  1237. demuxer->do_video = do_video;
  1238. /* Allocate buffer + padding */
  1239. demuxer->raw_data = calloc(1, MPEG3_RAW_SIZE);
  1240. demuxer->data_buffer = calloc(1, MPEG3_RAW_SIZE);
  1241. /* System specific variables */
  1242. demuxer->audio_pid = stream_id;
  1243. demuxer->video_pid = stream_id;
  1244. demuxer->astream = stream_id;
  1245. demuxer->vstream = stream_id;
  1246. demuxer->current_title = -1;
  1247. //printf("mpeg3_new_demuxer %fn", demuxer->time);
  1248. return demuxer;
  1249. }
  1250. int mpeg3_delete_demuxer(mpeg3_demuxer_t *demuxer)
  1251. {
  1252. int i;
  1253. if(demuxer->current_title >= 0)
  1254. {
  1255. mpeg3io_close_file(demuxer->titles[demuxer->current_title]->fs);
  1256. }
  1257. for(i = 0; i < demuxer->total_titles; i++)
  1258. {
  1259. mpeg3_delete_title(demuxer->titles[i]);
  1260. }
  1261. free(demuxer->data_buffer);
  1262. free(demuxer->raw_data);
  1263. free(demuxer);
  1264. return 0;
  1265. }
  1266. /* Need a timecode table to do this */
  1267. double mpeg3demux_length(mpeg3_demuxer_t *demuxer)
  1268. {
  1269. mpeg3_title_t *title;
  1270. int i, j;
  1271. double length;
  1272. for(i = demuxer->total_titles - 1; i >= 0; i--)
  1273. {
  1274. title = demuxer->titles[i];
  1275. for(j = title->timecode_table_size - 1; j >= 0; j--)
  1276. {
  1277. if(title->timecode_table[j].program == demuxer->current_program)
  1278. {
  1279. /*
  1280.  * printf("mpeg3demux_length %f %f %f %fn", 
  1281.  *  title->timecode_table[j].end_time, 
  1282.  *  title->timecode_table[j].start_time,
  1283.  *  title->timecode_table[j].absolute_start_time,
  1284.  *  title->timecode_table[j].end_time - title->timecode_table[j].start_time);
  1285.  */
  1286. return title->timecode_table[j].end_time - 
  1287. title->timecode_table[j].start_time + 
  1288. title->timecode_table[j].absolute_start_time;
  1289. }
  1290. }
  1291. }
  1292. return 1;
  1293. }
  1294. int mpeg3demux_eof(mpeg3_demuxer_t *demuxer)
  1295. {
  1296. if(demuxer->current_title >= 0)
  1297. {
  1298. if(mpeg3io_eof(demuxer->titles[demuxer->current_title]->fs) &&
  1299. demuxer->current_title >= demuxer->total_titles - 1)
  1300. return 1;
  1301. }
  1302. return 0;
  1303. }
  1304. int mpeg3demux_bof(mpeg3_demuxer_t *demuxer)
  1305. {
  1306. if(demuxer->current_title >= 0)
  1307. {
  1308. if(mpeg3io_bof(demuxer->titles[demuxer->current_title]->fs) &&
  1309. demuxer->current_title <= 0)
  1310. return 1;
  1311. }
  1312. return 0;
  1313. }
  1314. void mpeg3demux_start_reverse(mpeg3_demuxer_t *demuxer)
  1315. {
  1316. demuxer->reverse = 1;
  1317. }
  1318. void mpeg3demux_start_forward(mpeg3_demuxer_t *demuxer)
  1319. {
  1320. demuxer->reverse = 0;
  1321. }
  1322. /* Seek to a byte in the current title */
  1323. int mpeg3demux_seek_byte(mpeg3_demuxer_t *demuxer, int64_t byte)
  1324. {
  1325. long current_position;
  1326. mpeg3_t *file = demuxer->file;
  1327. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  1328. demuxer->data_position = 0;
  1329. demuxer->data_size = 0;
  1330. demuxer->error_flag = mpeg3io_seek(title->fs, byte);
  1331. if(!demuxer->error_flag && 
  1332. file->is_transport_stream &&
  1333. file->packet_size)
  1334. {
  1335. /* Get on a packet boundary only for transport streams. */
  1336. current_position = mpeg3io_tell(title->fs);
  1337. if(byte % file->packet_size)
  1338. {
  1339. demuxer->error_flag |= mpeg3io_seek(title->fs, 
  1340. current_position - 
  1341. (current_position % file->packet_size));
  1342. }
  1343. }
  1344. // Get current cell
  1345. for(demuxer->current_timecode = 0; 
  1346. demuxer->current_timecode < title->timecode_table_size; 
  1347. demuxer->current_timecode++)
  1348. {
  1349. if(title->timecode_table[demuxer->current_timecode].start_byte <= byte &&
  1350. title->timecode_table[demuxer->current_timecode].end_byte > byte)
  1351. {
  1352. break;
  1353. }
  1354. }
  1355. if(demuxer->current_timecode >= title->timecode_table_size)
  1356. demuxer->current_timecode = title->timecode_table_size - 1;
  1357. return demuxer->error_flag;
  1358. }
  1359. int mpeg3demux_seek_end(mpeg3_demuxer_t *demuxer)
  1360. {
  1361. mpeg3demux_open_title(demuxer, demuxer->total_titles - 1);
  1362. demuxer->current_timecode = 0;
  1363. return mpeg3demux_seek_byte(demuxer, demuxer->titles[demuxer->current_title]->total_bytes);
  1364. }
  1365. int mpeg3demux_seek_start(mpeg3_demuxer_t *demuxer)
  1366. {
  1367. mpeg3demux_open_title(demuxer, 0);
  1368. demuxer->current_timecode = 0;
  1369. return mpeg3demux_seek_byte(demuxer, 0);
  1370. }
  1371. /* For programs streams and toc seek to a time */
  1372. int mpeg3demux_seek_time(mpeg3_demuxer_t *demuxer, double new_time)
  1373. {
  1374. int i, j, done = 0, result = 0;
  1375. int64_t byte_offset, new_byte_offset;
  1376. double guess = 0, minimum = 65535;
  1377. mpeg3_title_t *title;
  1378. mpeg3demux_timecode_t *timecode;
  1379. demuxer->error_flag = 0;
  1380. i = 0;
  1381. j = 0;
  1382. title = demuxer->titles[i];
  1383. timecode = &title->timecode_table[j];
  1384. /* Get the title and timecode of the new position */
  1385. while(!demuxer->error_flag &&
  1386. !(timecode->absolute_start_time <= new_time &&
  1387. timecode->absolute_end_time > new_time &&
  1388. timecode->program == demuxer->current_program))
  1389. {
  1390. /* Next timecode */
  1391. j++;
  1392. if(j >= title->timecode_table_size)
  1393. {
  1394. i++;
  1395. j = 0;
  1396. if(i >= demuxer->total_titles)
  1397. {
  1398. demuxer->error_flag = 1;
  1399. return 1;
  1400. }
  1401. }
  1402. title = demuxer->titles[i];
  1403. timecode = &title->timecode_table[j];
  1404. }
  1405. //printf("mpeg3demux_seek_time 1 %d %f %f %fn", i, timecode->absolute_start_time, timecode->absolute_end_time, new_time);
  1406. if(demuxer->current_title != i)
  1407.      mpeg3demux_open_title(demuxer, i);
  1408. /* Guess the new byte position by interpolating */
  1409. demuxer->current_timecode = j;
  1410. byte_offset = ((new_time - timecode->absolute_start_time) /
  1411. (timecode->absolute_end_time - timecode->absolute_start_time) *
  1412. (timecode->end_byte - timecode->start_byte) +
  1413. timecode->start_byte);
  1414. //printf("mpeg3demux_seek_time %f %fn", new_time, byte_offset);
  1415. /* Binary search */
  1416. while(!done && !result && byte_offset >= 0)
  1417. {
  1418. result = mpeg3demux_seek_byte(demuxer, (long)byte_offset);
  1419. //printf("seek_time 0 byte %.0f want %f result %dn", byte_offset, new_time, result); 
  1420. if(!result)
  1421. {
  1422. result = mpeg3_read_next_packet(demuxer);
  1423. // printf("seek_time 1 guess %f want %fn", guess, new_time); 
  1424. guess = demuxer->time + demuxer->time_offset;
  1425. if(fabs(new_time - guess) >= fabs(minimum)) done = 1;
  1426. else
  1427. {
  1428. minimum = guess - new_time;
  1429. new_byte_offset = byte_offset + ((new_time - guess) / 
  1430. (timecode->end_time - timecode->start_time) *
  1431. (timecode->end_byte - timecode->start_byte));
  1432. if(fabs(new_byte_offset - byte_offset) < 1) done = 1;
  1433. byte_offset = new_byte_offset;
  1434. }
  1435. }
  1436. }
  1437. //printf("mpeg3demux_seek_time 1n");
  1438. /* Get one packet before the packet just read */
  1439. if(!result && byte_offset > 0 && minimum > 0)
  1440. {
  1441. mpeg3_read_prev_packet(demuxer);
  1442. //printf("mpeg3demux_seek_time 2n");
  1443. mpeg3_read_prev_packet(demuxer);
  1444. }
  1445. //printf("mpeg3demux_seek_time 3n");
  1446. //printf("mpeg3demux_seek_time 2 %d %d %dn", demuxer->current_title, demuxer->current_timecode, mpeg3demux_tell(demuxer));
  1447. demuxer->error_flag = result;
  1448. return result;
  1449. }
  1450. int mpeg3demux_seek_percentage(mpeg3_demuxer_t *demuxer, double percentage)
  1451. {
  1452. double total_bytes = 0;
  1453. double absolute_position;
  1454. long relative_position;
  1455. int i, new_title;
  1456. mpeg3_title_t *title;
  1457. demuxer->error_flag = 0;
  1458. /* Get the absolute byte position; */
  1459. for(i = 0; i < demuxer->total_titles; i++)
  1460. total_bytes += demuxer->titles[i]->total_bytes;
  1461. absolute_position = percentage * total_bytes;
  1462. /* Get the title the byte is inside */
  1463. for(new_title = 0, total_bytes = 0; new_title < demuxer->total_titles; new_title++)
  1464. {
  1465. total_bytes += demuxer->titles[new_title]->total_bytes;
  1466. if(absolute_position < total_bytes) break;
  1467. }
  1468. if(new_title >= demuxer->total_titles)
  1469. {
  1470. new_title = demuxer->total_titles - 1;
  1471. }
  1472. /* Got a title */
  1473. title = demuxer->titles[new_title];
  1474. total_bytes -= title->total_bytes;
  1475. relative_position = (long)(absolute_position - total_bytes);
  1476. /* Get the timecode the byte is inside */
  1477. for(demuxer->current_timecode = 0; 
  1478. demuxer->current_timecode < title->timecode_table_size; 
  1479. demuxer->current_timecode++)
  1480. {
  1481. if(title->timecode_table[demuxer->current_timecode].start_byte <= relative_position &&
  1482. title->timecode_table[demuxer->current_timecode].end_byte > relative_position)
  1483. {
  1484. break;
  1485. }
  1486. }
  1487. if(demuxer->current_timecode >= title->timecode_table_size)
  1488. demuxer->current_timecode = title->timecode_table_size - 1;
  1489. /* Get the nearest timecode in the same program */
  1490. while(demuxer->current_timecode < title->timecode_table_size - 1 &&
  1491. title->timecode_table[demuxer->current_timecode].program != demuxer->current_program)
  1492. {
  1493. demuxer->current_timecode++;
  1494. }
  1495. /*
  1496.  * printf("seek percentage 1 %d %d %d %dn", demuxer->current_title, 
  1497.  *  demuxer->current_timecode,
  1498.  *  title->timecode_table[demuxer->current_timecode].start_byte,
  1499.  *  title->timecode_table[demuxer->current_timecode].end_byte);
  1500.  */
  1501. /* Open the new title and seek to the correct byte */
  1502. if(new_title != demuxer->current_title)
  1503. {
  1504. demuxer->error_flag = mpeg3demux_open_title(demuxer, new_title);
  1505. }
  1506. /*
  1507.  * printf("seek percentage 2 %d %d %d %dn", demuxer->current_title, 
  1508.  *  demuxer->current_timecode,
  1509.  *  title->timecode_table[demuxer->current_timecode].start_byte,
  1510.  *  title->timecode_table[demuxer->current_timecode].end_byte);
  1511.  */
  1512. if(!demuxer->error_flag)
  1513. demuxer->error_flag = mpeg3io_seek(title->fs, relative_position);
  1514. return demuxer->error_flag;
  1515. }
  1516. // Lots of optimization errors
  1517. double mpeg3demux_tell_percentage(mpeg3_demuxer_t *demuxer)
  1518. {
  1519. double total_bytes = 0;
  1520. double position = 0;
  1521. int i;
  1522. demuxer->error_flag = 0;
  1523. position = mpeg3io_tell_gcc(demuxer->titles[demuxer->current_title]->fs);
  1524. for(i = 0; i < demuxer->total_titles; i++)
  1525. {
  1526. double new_bytes = mpeg3_total_bytes_gcc(demuxer->titles[i]);
  1527. if(i == demuxer->current_title)
  1528. {
  1529. position = mpeg3_add_double_gcc(total_bytes, position);
  1530. }
  1531. total_bytes = mpeg3_add_double_gcc(total_bytes, new_bytes);
  1532. }
  1533. return position / total_bytes;
  1534. }
  1535. double mpeg3demux_get_time(mpeg3_demuxer_t *demuxer)
  1536. {
  1537. return demuxer->time;
  1538. }
  1539. int64_t mpeg3demux_tell(mpeg3_demuxer_t *demuxer)
  1540. {
  1541. return mpeg3io_tell(demuxer->titles[demuxer->current_title]->fs);
  1542. }
  1543. int mpeg3demux_tell_title(mpeg3_demuxer_t *demuxer)
  1544. {
  1545. return demuxer->current_title;
  1546. }
  1547. int64_t mpeg3demuxer_total_bytes(mpeg3_demuxer_t *demuxer)
  1548. {
  1549. mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
  1550. return title->total_bytes;
  1551. }
  1552. mpeg3_demuxer_t* mpeg3_get_demuxer(mpeg3_t *file)
  1553. {
  1554. if(file->is_program_stream || file->is_transport_stream)
  1555. {
  1556. if(file->total_astreams) return file->atrack[0]->demuxer;
  1557. else
  1558. if(file->total_vstreams) return file->vtrack[0]->demuxer;
  1559. }
  1560. return 0;
  1561. }