3 #include "mpeg3protos.h"
4 #include "workarounds.h"
11 #define ABS(x) ((x) >= 0 ? (x) : -(x))
13 /* Don't advance pointer */
14 static inline unsigned char packet_next_char(mpeg3_demuxer_t
*demuxer
)
16 //printf(__FUNCTION__ " called\n");
17 return demuxer
->raw_data
[demuxer
->raw_offset
];
21 static unsigned char packet_read_char(mpeg3_demuxer_t
*demuxer
)
23 unsigned char result
= demuxer
->raw_data
[demuxer
->raw_offset
++];
24 //printf(__FUNCTION__ " called\n");
28 static inline unsigned int packet_read_int16(mpeg3_demuxer_t
*demuxer
)
30 unsigned int a
, b
, result
;
31 //printf(__FUNCTION__ " called\n");
32 a
= demuxer
->raw_data
[demuxer
->raw_offset
++];
33 b
= demuxer
->raw_data
[demuxer
->raw_offset
++];
34 result
= (a
<< 8) | b
;
39 static inline unsigned int packet_next_int24(mpeg3_demuxer_t
*demuxer
)
41 unsigned int a
, b
, c
, result
;
42 //printf(__FUNCTION__ " called\n");
43 a
= demuxer
->raw_data
[demuxer
->raw_offset
];
44 b
= demuxer
->raw_data
[demuxer
->raw_offset
+ 1];
45 c
= demuxer
->raw_data
[demuxer
->raw_offset
+ 2];
46 result
= (a
<< 16) | (b
<< 8) | c
;
51 static inline unsigned int packet_read_int24(mpeg3_demuxer_t
*demuxer
)
53 unsigned int a
, b
, c
, result
;
54 //printf(__FUNCTION__ " called\n");
55 a
= demuxer
->raw_data
[demuxer
->raw_offset
++];
56 b
= demuxer
->raw_data
[demuxer
->raw_offset
++];
57 c
= demuxer
->raw_data
[demuxer
->raw_offset
++];
58 result
= (a
<< 16) | (b
<< 8) | c
;
63 static inline unsigned int packet_read_int32(mpeg3_demuxer_t
*demuxer
)
65 unsigned int a
, b
, c
, d
, result
;
66 a
= demuxer
->raw_data
[demuxer
->raw_offset
++];
67 b
= demuxer
->raw_data
[demuxer
->raw_offset
++];
68 c
= demuxer
->raw_data
[demuxer
->raw_offset
++];
69 d
= demuxer
->raw_data
[demuxer
->raw_offset
++];
70 result
= (a
<< 24) | (b
<< 16) | (c
<< 8) | d
;
75 static inline unsigned int packet_skip(mpeg3_demuxer_t
*demuxer
, int length
)
77 demuxer
->raw_offset
+= length
;
81 static int get_adaptation_field(mpeg3_demuxer_t
*demuxer
)
86 //printf("get_adaptation_field %d\n", demuxer->adaptation_field_control);
87 demuxer
->adaptation_fields
++;
88 /* get adaptation field length */
89 length
= packet_read_char(demuxer
);
94 pcr_flag
= (packet_read_char(demuxer
) >> 4) & 1;
98 unsigned int clk_ref_base
= packet_read_int32(demuxer
);
99 unsigned int clk_ref_ext
= packet_read_int16(demuxer
);
101 if (clk_ref_base
> 0x7fffffff)
103 /* correct for invalid numbers */
104 clk_ref_base
= 0; /* ie. longer than 32 bits when multiplied by 2 */
105 clk_ref_ext
= 0; /* multiplied by 2 corresponds to shift left 1 (<<=1) */
109 /* Create space for bit */
111 clk_ref_base
|= (clk_ref_ext
>> 15); /* Take bit */
112 clk_ref_ext
&= 0x01ff; /* Only lower 9 bits */
114 demuxer
->time
= ((double)clk_ref_base
+ clk_ref_ext
/ 300) / 90000;
115 if(length
) packet_skip(demuxer
, length
- 7);
119 printf(" pcr_flag=%x time=%f\n", pcr_flag
, demuxer
->time
);
123 packet_skip(demuxer
, length
- 1);
129 static int get_program_association_table(mpeg3_demuxer_t
*demuxer
)
131 demuxer
->program_association_tables
++;
132 demuxer
->table_id
= packet_read_char(demuxer
);
133 demuxer
->section_length
= packet_read_int16(demuxer
) & 0xfff;
134 demuxer
->transport_stream_id
= packet_read_int16(demuxer
);
135 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
138 printf(" table_id=0x%x section_length=%d transport_stream_id=0x%x\n",
140 demuxer
->section_length
,
141 demuxer
->transport_stream_id
);
146 static int get_transport_payload(mpeg3_demuxer_t
*demuxer
,
150 int bytes
= demuxer
->raw_size
- demuxer
->raw_offset
;
154 printf("get_transport_payload: got negative payload size!\n");
158 * if(demuxer->data_size + bytes > MPEG3_RAW_SIZE)
159 * bytes = MPEG3_RAW_SIZE - demuxer->data_size;
162 if(demuxer
->read_all
&& is_audio
)
164 memcpy(demuxer
->audio_buffer
+ demuxer
->audio_size
,
165 demuxer
->raw_data
+ demuxer
->raw_offset
,
167 demuxer
->audio_size
+= bytes
;
170 if(demuxer
->read_all
&& is_video
)
172 memcpy(demuxer
->video_buffer
+ demuxer
->video_size
,
173 demuxer
->raw_data
+ demuxer
->raw_offset
,
175 demuxer
->video_size
+= bytes
;
179 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
180 demuxer
->raw_data
+ demuxer
->raw_offset
,
182 demuxer
->data_size
+= bytes
;
185 demuxer
->raw_offset
+= bytes
;
189 static int get_pes_packet_header(mpeg3_demuxer_t
*demuxer
,
193 unsigned int pes_header_bytes
= 0;
194 unsigned int pts_dts_flags
;
195 int pes_header_data_length
;
197 /* drop first 8 bits */
198 packet_read_char(demuxer
);
199 pts_dts_flags
= (packet_read_char(demuxer
) >> 6) & 0x3;
200 pes_header_data_length
= packet_read_char(demuxer
);
203 /* Get Presentation Time stamps and Decoding Time Stamps */
204 if(pts_dts_flags
== 2)
206 *pts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
208 *pts
|= (packet_read_int16(demuxer
) >> 1);
210 *pts
|= (packet_read_int16(demuxer
) >> 1);
211 pes_header_bytes
+= 5;
214 if(pts_dts_flags
== 3)
216 *pts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
218 *pts
|= (packet_read_int16(demuxer
) >> 1);
220 *pts
|= (packet_read_int16(demuxer
) >> 1);
221 *dts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
223 *dts
|= (packet_read_int16(demuxer
) >> 1);
225 *dts
|= (packet_read_int16(demuxer
) >> 1);
226 pes_header_bytes
+= 10;
229 demuxer
->time
= (double)*pts
/ 90000;
233 printf(" pts_dts_flags=0x%02x pts=%f dts=%f\n",
235 (double)*pts
/ 90000,
236 (double)*dts
/ 90000);
240 /* extract other stuff here! */
241 packet_skip(demuxer
, pes_header_data_length
- pes_header_bytes
);
245 static int get_unknown_data(mpeg3_demuxer_t
*demuxer
)
247 int bytes
= demuxer
->raw_size
- demuxer
->raw_offset
;
248 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
249 demuxer
->raw_data
+ demuxer
->raw_offset
,
251 demuxer
->data_size
+= bytes
;
252 demuxer
->raw_offset
+= bytes
;
258 static int get_pes_packet_data(mpeg3_demuxer_t
*demuxer
)
260 unsigned int pts
= 0, dts
= 0;
261 get_pes_packet_header(demuxer
, &pts
, &dts
);
264 if(demuxer
->stream_id
== 0xbd)
267 // Don't know if the next byte is the true stream id like in program stream
268 demuxer
->stream_id
= 0x0;
269 demuxer
->got_audio
= 1;
270 demuxer
->custom_id
= demuxer
->pid
;
272 if(demuxer
->read_all
)
273 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_AC3
;
274 if(demuxer
->astream
== -1)
275 demuxer
->astream
= demuxer
->custom_id
;
279 printf(" 0x%x bytes AC3 audio\n", demuxer
->raw_size
- demuxer
->raw_offset
);
282 if((demuxer
->custom_id
== demuxer
->astream
&&
283 demuxer
->do_audio
) ||
286 demuxer
->pes_audio_time
= (double)pts
/ 90000;
287 demuxer
->audio_pid
= demuxer
->pid
;
288 return get_transport_payload(demuxer
, 1, 0);
293 if((demuxer
->stream_id
>> 4) == 12 || (demuxer
->stream_id
>> 4) == 13)
296 demuxer
->custom_id
= demuxer
->pid
;
297 demuxer
->got_audio
= 1;
299 /* Just pick the first available stream if no ID is set */
300 if(demuxer
->read_all
)
301 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_MPEG
;
302 if(demuxer
->astream
== -1)
303 demuxer
->astream
= demuxer
->custom_id
;
307 printf(" 0x%x bytes MP2 audio\n", demuxer
->raw_size
- demuxer
->raw_offset
);
310 if((demuxer
->custom_id
== demuxer
->astream
&&
311 demuxer
->do_audio
) ||
314 demuxer
->pes_audio_time
= (double)pts
/ 90000;
315 demuxer
->audio_pid
= demuxer
->pid
;
317 return get_transport_payload(demuxer
, 1, 0);
321 if((demuxer
->stream_id
>> 4) == 14)
324 demuxer
->custom_id
= demuxer
->pid
;
325 demuxer
->got_video
= 1;
328 /* Just pick the first available stream if no ID is set */
329 if(demuxer
->read_all
)
330 demuxer
->vstream_table
[demuxer
->custom_id
] = 1;
332 if(demuxer
->vstream
== -1)
333 demuxer
->vstream
= demuxer
->custom_id
;
338 printf(" 0x%x bytes video data\n", demuxer
->raw_size
- demuxer
->raw_offset
);
341 if((demuxer
->custom_id
== demuxer
->vstream
&&
342 demuxer
->do_video
) ||
345 demuxer
->pes_video_time
= (double)pts
/ 90000;
346 demuxer
->video_pid
= demuxer
->pid
;
348 * printf("get_pes_packet_data video %04x %llx\n",
350 * mpeg3io_tell(demuxer->titles[demuxer->current_title]->fs));
352 * for(i = demuxer->raw_offset; i < demuxer->raw_size; i++)
353 * printf("%02x ", demuxer->raw_data[i], stdout);
358 return get_transport_payload(demuxer
, 0, 1);
362 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
367 static int get_pes_packet(mpeg3_demuxer_t
*demuxer
)
369 demuxer
->pes_packets
++;
374 packet_read_int24(demuxer
);
375 demuxer
->stream_id
= packet_read_char(demuxer
);
380 printf(" stream_id=0x%02x\n", demuxer
->stream_id
);
383 /* Skip pes packet length */
384 packet_read_int16(demuxer
);
386 if(demuxer
->stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
387 demuxer
->stream_id
!= MPEG3_PADDING_STREAM
)
389 return get_pes_packet_data(demuxer
);
392 if(demuxer
->stream_id
== MPEG3_PRIVATE_STREAM_2
)
394 /* Dump private data! */
395 fprintf(stderr
, "stream_id == MPEG3_PRIVATE_STREAM_2\n");
396 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
400 if(demuxer
->stream_id
== MPEG3_PADDING_STREAM
)
402 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
407 fprintf(stderr
, "unknown stream_id in pes packet");
413 static int get_payload(mpeg3_demuxer_t
*demuxer
)
415 //printf("get_payload 1 %x %d\n", demuxer->pid, demuxer->payload_unit_start_indicator);
416 if(demuxer
->payload_unit_start_indicator
)
418 if(demuxer
->pid
== 0)
419 get_program_association_table(demuxer
);
421 if(packet_next_int24(demuxer
) == MPEG3_PACKET_START_CODE_PREFIX
)
422 get_pes_packet(demuxer
);
424 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
430 printf(" 0x%x bytes elementary data\n", demuxer
->raw_size
- demuxer
->raw_offset
);
433 if(demuxer
->pid
== demuxer
->audio_pid
&&
434 (demuxer
->do_audio
||
437 if(demuxer
->do_audio
) demuxer
->got_audio
= 1;
438 get_transport_payload(demuxer
, 1, 0);
439 //printf("get_payload 1 %x\n", demuxer->custom_id);
442 if(demuxer
->pid
== demuxer
->video_pid
&&
443 (demuxer
->do_video
||
446 if(demuxer
->do_video
) demuxer
->got_video
= 1;
447 get_transport_payload(demuxer
, 0, 1);
451 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
457 /* Read a transport packet */
458 static int read_transport(mpeg3_demuxer_t
*demuxer
)
461 mpeg3_t
*file
= (mpeg3_t
*)demuxer
->file
;
462 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
467 /* Packet size is known for transport streams */
468 demuxer
->raw_size
= file
->packet_size
;
469 demuxer
->raw_offset
= 0;
470 demuxer
->stream_id
= 0;
471 demuxer
->got_audio
= 0;
472 demuxer
->got_video
= 0;
473 demuxer
->custom_id
= -1;
478 perror("read_transport");
482 //printf("read transport 1 %llx %llx\n", title->fs->current_byte, title->fs->total_bytes);
483 // Search for Sync byte */
486 bits
= mpeg3io_read_char(title
->fs
);
487 }while(!mpeg3io_eof(title
->fs
) && !result
&& bits
!= MPEG3_SYNC_BYTE
);
490 if(mpeg3io_eof(title
->fs
) || result
) return 1;
492 * printf("read transport 2 bits=%x tell=%llx packet_size=%x\n",
494 * mpeg3io_tell(title->fs),
495 * file->packet_size);
498 if(bits
== MPEG3_SYNC_BYTE
&& !result
)
500 demuxer
->raw_data
[0] = MPEG3_SYNC_BYTE
;
501 result
= mpeg3io_read_data(demuxer
->raw_data
+ 1,
502 file
->packet_size
- 1,
507 // Assume transport streams are all one program
508 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
513 packet_read_char(demuxer
);
514 bits
= packet_read_int24(demuxer
) & 0x00ffffff;
515 //printf("read transport 3 tell=%x bits=%x\n", mpeg3io_tell(title->fs), bits);
516 demuxer
->transport_error_indicator
= (bits
>> 23) & 0x1;
517 demuxer
->payload_unit_start_indicator
= (bits
>> 22) & 0x1;
518 demuxer
->pid
= demuxer
->custom_id
= (bits
>> 8) & 0x00001fff;
520 demuxer
->transport_scrambling_control
= (bits
>> 6) & 0x3;
521 demuxer
->adaptation_field_control
= (bits
>> 4) & 0x3;
522 demuxer
->continuity_counter
= bits
& 0xf;
524 //printf("read_transport 1 %x\n", demuxer->pid);
526 // This caused an audio track to not be created.
527 if(demuxer
->transport_error_indicator
)
530 "demuxer->transport_error_indicator at %llx\n",
531 mpeg3io_tell(title
->fs
));
532 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
537 //printf("read_transport 5 %x\n", demuxer->pid);
538 if (demuxer
->pid
== 0x1fff)
540 demuxer
->is_padding
= 1; /* padding; just go to next */
544 demuxer
->is_padding
= 0;
547 //printf("read_transport 6 %x\n", demuxer->pid);
548 /* Get pid from table */
549 for(table_entry
= 0, result
= 0;
550 table_entry
< demuxer
->total_pids
;
553 if(demuxer
->pid
== demuxer
->pid_table
[table_entry
])
560 //printf("read_transport 7 %x\n", demuxer->pid);
564 /* Not in pid table */
567 demuxer
->pid_table
[table_entry
] = demuxer
->pid
;
568 demuxer
->continuity_counters
[table_entry
] = demuxer
->continuity_counter
; /* init */
569 demuxer
->total_pids
++;
575 printf("0x%llx pid=0x%02x continuity=0x%02x padding=%d adaptation=%d unit_start=%d\n",
576 demuxer
->program_byte
,
578 demuxer
->continuity_counter
,
580 demuxer
->adaptation_field_control
,
581 demuxer
->payload_unit_start_indicator
);
585 /* Abort if padding. Should abort after demuxer->pid == 0x1fff for speed. */
586 if(demuxer
->is_padding
)
588 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
596 if(demuxer
->pid
!= MPEG3_PROGRAM_ASSOCIATION_TABLE
&&
597 demuxer
->pid
!= MPEG3_CONDITIONAL_ACCESS_TABLE
&&
598 (demuxer
->adaptation_field_control
== 1 || demuxer
->adaptation_field_control
== 3))
600 if(demuxer
->continuity_counters
[table_entry
] != demuxer
->continuity_counter
)
602 // fprintf(stderr, "demuxer->continuity_counters[table_entry] != demuxer->continuity_counter\n");
604 demuxer
->continuity_counters
[table_entry
] = demuxer
->continuity_counter
;
606 if(++(demuxer
->continuity_counters
[table_entry
]) > 15) demuxer
->continuity_counters
[table_entry
] = 0;
616 if(demuxer
->adaptation_field_control
& 0x2)
617 result
= get_adaptation_field(demuxer
);
619 // Need to enter in astream and vstream table:
620 // PID ored with stream_id
621 if(demuxer
->adaptation_field_control
& 0x1)
622 result
= get_payload(demuxer
);
624 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
629 static int get_system_header(mpeg3_demuxer_t
*demuxer
)
631 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
632 int length
= mpeg3io_read_int16(title
->fs
);
633 mpeg3io_seek_relative(title
->fs
, length
);
637 static unsigned int get_timestamp(mpeg3_demuxer_t
*demuxer
)
639 unsigned int timestamp
;
640 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
642 /* Only low 4 bits (7==1111) */
643 timestamp
= (mpeg3io_read_char(title
->fs
) >> 1) & 7;
645 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
647 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
651 static int get_pack_header(mpeg3_demuxer_t
*demuxer
)
654 unsigned int clock_ref
, clock_ref_ext
;
655 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
657 /* Get the time code */
658 if((mpeg3io_next_char(title
->fs
) >> 4) == 2)
661 demuxer
->time
= (double)get_timestamp(demuxer
) / 90000;
663 mpeg3io_read_int24(title
->fs
);
666 if(mpeg3io_next_char(title
->fs
) & 0x40)
668 i
= mpeg3io_read_int32(title
->fs
);
669 j
= mpeg3io_read_int16(title
->fs
);
671 if(i
& 0x40000000 || (i
>> 28) == 2)
673 clock_ref
= ((i
& 0x38000000) << 3);
674 clock_ref
|= ((i
& 0x03fff800) << 4);
675 clock_ref
|= ((i
& 0x000003ff) << 5);
676 clock_ref
|= ((j
& 0xf800) >> 11);
677 clock_ref_ext
= (j
>> 1) & 0x1ff;
679 demuxer
->time
= (double)(clock_ref
+ clock_ref_ext
/ 300) / 90000;
681 mpeg3io_read_int24(title
->fs
);
682 i
= mpeg3io_read_char(title
->fs
) & 0x7;
685 mpeg3io_seek_relative(title
->fs
, i
);
690 mpeg3io_seek_relative(title
->fs
, 2);
697 static int get_program_payload(mpeg3_demuxer_t
*demuxer
,
702 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
704 if(demuxer
->read_all
&& is_audio
)
706 mpeg3io_read_data(demuxer
->audio_buffer
+ demuxer
->audio_size
,
709 demuxer
->audio_size
+= bytes
;
712 if(demuxer
->read_all
&& is_video
)
714 mpeg3io_read_data(demuxer
->video_buffer
+ demuxer
->video_size
,
717 demuxer
->video_size
+= bytes
;
721 mpeg3io_read_data(demuxer
->data_buffer
+ demuxer
->data_size
,
724 demuxer
->data_size
+= bytes
;
736 static int handle_scrambling(mpeg3_demuxer_t
*demuxer
,
737 int decryption_offset
)
739 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
741 // Advance 2048 bytes if scrambled. We might pick up a spurrius
742 // packet start code in the scrambled data otherwise.
743 if(demuxer
->last_packet_start
+ 0x800 > mpeg3io_tell(title
->fs
))
745 mpeg3io_seek_relative(title
->fs
,
746 demuxer
->last_packet_start
+ 0x800 - mpeg3io_tell(title
->fs
));
751 // Descramble if desired.
752 if(demuxer
->data_size
||
753 demuxer
->audio_size
||
756 unsigned char *buffer_ptr
= 0;
757 if(demuxer
->data_size
) buffer_ptr
= demuxer
->data_buffer
;
759 if(demuxer
->audio_size
) buffer_ptr
= demuxer
->audio_buffer
;
761 if(demuxer
->video_size
) buffer_ptr
= demuxer
->video_buffer
;
764 //printf(__FUNCTION__ " data_size=%x decryption_offset=%x\n", demuxer->data_size, decryption_offset);
765 if(mpeg3_decrypt_packet(title
->fs
->css
,
769 fprintf(stderr
, "handle_scrambling: Decryption not available\n");
782 static int handle_pcm(mpeg3_demuxer_t
*demuxer
, int bytes
)
784 /* Synthesize PCM header and delete MPEG header for PCM data */
785 /* Must be done after decryption */
791 unsigned char *output
= 0;
792 unsigned char *data_buffer
= 0;
800 if(demuxer
->read_all
&& demuxer
->audio_size
)
802 output
= demuxer
->audio_buffer
+ demuxer
->audio_start
;
803 data_buffer
= demuxer
->audio_buffer
;
804 data_start
= demuxer
->audio_start
;
805 data_size
= &demuxer
->audio_size
;
809 output
= demuxer
->data_buffer
+ demuxer
->data_start
;
810 data_buffer
= demuxer
->data_buffer
;
811 data_start
= demuxer
->data_start
;
812 data_size
= &demuxer
->data_size
;
818 /* Shift audio back */
820 for(i
= *data_size
- 1, j
= *data_size
+ PCM_HEADERSIZE
- 3 - 1;
823 *(data_buffer
+ j
) = *(data_buffer
+ i
);
824 *data_size
+= PCM_HEADERSIZE
- 3;
826 bits_code
= (code
>> 6) & 3;
827 samplerate_code
= (code
& 0x10);
835 switch(samplerate_code
)
845 *(int32_t*)(output
+ 4) = samplerate
;
849 case 0: bits
= 16; break;
850 case 1: bits
= 20; break;
851 case 2: bits
= 24; break;
852 default: bits
= 16; break;
854 *(int32_t*)(output
+ 8) = bits
;
856 *(int32_t*)(output
+ 12) = (code
& 0x7) + 1;
858 *(int32_t*)(output
+ 16) = bytes
-
865 //printf(__FUNCTION__ " %02x%02x%02x %d\n", code1, code, code2, pes_packet_length - 3);
873 /* Program packet reading core */
874 static int get_ps_pes_packet(mpeg3_demuxer_t
*demuxer
, unsigned int header
)
876 unsigned int pts
= 0, dts
= 0;
877 int pes_packet_length
;
878 int64_t pes_packet_start
;
879 int decryption_offset
;
881 mpeg3_t
*file
= demuxer
->file
;
882 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
884 /* Format if audio */
887 demuxer
->data_start
= demuxer
->data_size
;
888 demuxer
->audio_start
= demuxer
->audio_size
;
889 demuxer
->video_start
= demuxer
->video_size
;
891 demuxer
->stream_id
= header
& 0xff;
892 pes_packet_length
= mpeg3io_read_int16(title
->fs
);
893 pes_packet_start
= mpeg3io_tell(title
->fs
);
898 * printf(__FUNCTION__ " pes_packet_start=%llx pes_packet_length=%x data_size=%x\n",
901 * demuxer->data_size);
908 if(demuxer
->stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
909 demuxer
->stream_id
!= MPEG3_PADDING_STREAM
)
911 if((mpeg3io_next_char(title
->fs
) >> 6) == 0x02)
913 /* Get MPEG-2 packet */
914 int pes_header_bytes
= 0;
916 int pes_header_data_length
;
919 demuxer
->last_packet_decryption
= mpeg3io_tell(title
->fs
);
920 scrambling
= mpeg3io_read_char(title
->fs
) & 0x30;
922 /* Reset scrambling bit for the mpeg3cat utility */
923 // if(scrambling) demuxer->raw_data[demuxer->raw_offset - 1] &= 0xcf;
924 // Force packet length if scrambling
925 if(scrambling
) pes_packet_length
= 0x800 -
927 demuxer
->last_packet_start
;
930 pts_dts_flags
= (mpeg3io_read_char(title
->fs
) >> 6) & 0x3;
931 pes_header_data_length
= mpeg3io_read_char(title
->fs
);
935 /* Get Presentation and Decoding Time Stamps */
936 if(pts_dts_flags
== 2)
938 pts
= get_timestamp(demuxer
);
941 printf("pts=%d\n", pts
);
943 pes_header_bytes
+= 5;
946 if(pts_dts_flags
== 3)
948 pts
= get_timestamp(demuxer
);
949 dts
= get_timestamp(demuxer
);
952 printf("pts=%d dts=%d\n", pts
, dts
);
955 * pts = (mpeg3io_read_char(title->fs) >> 1) & 7;
957 * pts |= (mpeg3io_read_int16(title->fs) >> 1);
959 * pts |= (mpeg3io_read_int16(title->fs) >> 1);
963 * dts = (mpeg3io_read_char(title->fs) >> 1) & 7;
965 * dts |= (mpeg3io_read_int16(title->fs) >> 1);
967 * dts |= (mpeg3io_read_int16(title->fs) >> 1);
969 pes_header_bytes
+= 10;
972 //printf("get_ps_pes_packet do_audio=%d do_video=%d pts=%x dts=%x\n",
973 // demuxer->do_audio, demuxer->do_video, pts, dts);
976 mpeg3io_seek_relative(title
->fs
,
977 pes_header_data_length
- pes_header_bytes
);
982 /* Get MPEG-1 packet */
983 while(mpeg3io_next_char(title
->fs
) == 0xff)
985 mpeg3io_read_char(title
->fs
);
988 /* Skip STD buffer scale */
989 if((mpeg3io_next_char(title
->fs
) & 0x40) == 0x40)
991 mpeg3io_seek_relative(title
->fs
, 2);
994 /* Decide which timestamps are available */
995 pts_dts_flags
= mpeg3io_next_char(title
->fs
);
997 if(pts_dts_flags
>= 0x30)
999 /* Get the presentation and decoding time stamp */
1000 pts
= get_timestamp(demuxer
);
1001 dts
= get_timestamp(demuxer
);
1004 if(pts_dts_flags
>= 0x20)
1006 /* Get just the presentation time stamp */
1007 pts
= get_timestamp(demuxer
);
1010 if(pts_dts_flags
== 0x0f)
1012 /* End of timestamps */
1013 mpeg3io_read_char(title
->fs
);
1017 return 1; /* Error */
1021 /* Now extract the payload. */
1022 if((demuxer
->stream_id
>> 4) == 0xc || (demuxer
->stream_id
>> 4) == 0xd)
1025 /* Take first stream ID if -1 */
1026 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1028 demuxer
->got_audio
= 1;
1029 demuxer
->custom_id
= demuxer
->stream_id
& 0x0f;
1032 if(demuxer
->read_all
)
1033 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_MPEG
;
1035 if(demuxer
->astream
== -1)
1036 demuxer
->astream
= demuxer
->custom_id
;
1041 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
1044 if(demuxer
->custom_id
== demuxer
->astream
&&
1045 demuxer
->do_audio
||
1048 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1051 printf(" MP2 audio data offset=%llx custom_id=%x size=%x\n",
1052 demuxer
->program_byte
,
1057 get_program_payload(demuxer
, pes_packet_length
, 1, 0);
1061 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1065 if((demuxer
->stream_id
>> 4) == 0xe)
1068 /* Take first stream ID if -1 */
1069 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1071 demuxer
->got_video
= 1;
1072 demuxer
->custom_id
= demuxer
->stream_id
& 0x0f;
1075 if(demuxer
->read_all
)
1076 demuxer
->vstream_table
[demuxer
->custom_id
] = 1;
1078 if(demuxer
->vstream
== -1)
1079 demuxer
->vstream
= demuxer
->custom_id
;
1081 if(pts
> 0) demuxer
->pes_video_time
= (double)pts
/ 60000;
1085 if(demuxer
->custom_id
== demuxer
->vstream
&&
1086 demuxer
->do_video
||
1089 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1092 printf(" video offset=%llx custom_id=%x size=%x\n",
1093 demuxer
->program_byte
,
1099 get_program_payload(demuxer
,
1108 printf(" skipping video size=%x\n", pes_packet_length
);
1110 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1114 if((demuxer
->stream_id
== 0xbd || demuxer
->stream_id
== 0xbf) &&
1115 mpeg3io_next_char(title
->fs
) != 0xff &&
1116 ((mpeg3io_next_char(title
->fs
) & 0xf0) != 0x20))
1119 /* DVD audio data */
1120 /* Get the audio format */
1121 if((mpeg3io_next_char(title
->fs
) & 0xf0) == 0xa0)
1127 // Picks up bogus data if (& 0xf) or (& 0x7f)
1128 demuxer
->stream_id
= mpeg3io_next_char(title
->fs
);
1129 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
1131 demuxer
->got_audio
= 1;
1132 demuxer
->custom_id
= demuxer
->stream_id
;
1135 /* Take first stream ID if not building TOC. */
1136 if(demuxer
->read_all
)
1137 demuxer
->astream_table
[demuxer
->custom_id
] = format
;
1139 if(demuxer
->astream
== -1)
1140 demuxer
->astream
= demuxer
->custom_id
;
1148 if(demuxer
->custom_id
== demuxer
->astream
&&
1149 demuxer
->do_audio
||
1152 demuxer
->aformat
= format
;
1153 mpeg3io_read_int32(title
->fs
);
1155 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1156 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1158 if(format
== AUDIO_PCM
) do_pcm
= 1;
1159 //printf("get_ps_pes_packet 5 %x\n", decryption_offset);
1163 printf(" AC3 audio data size=%x\n", pes_packet_length
);
1166 get_program_payload(demuxer
,
1173 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1174 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1176 //printf("get_ps_pes_packet 6 %d\n", demuxer->astream_table[0x20]);
1179 if(demuxer
->stream_id
== 0xbc || 1)
1181 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1182 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1186 if(demuxer
->stream_id
== MPEG3_PRIVATE_STREAM_2
|| demuxer
->stream_id
== MPEG3_PADDING_STREAM
)
1188 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1189 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1195 if(scrambling
) handle_scrambling(demuxer
, decryption_offset
);
1199 if(do_pcm
) handle_pcm(demuxer
, pes_packet_length
);
1211 int mpeg3demux_read_program(mpeg3_demuxer_t
*demuxer
)
1215 mpeg3_t
*file
= demuxer
->file
;
1216 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1217 unsigned int header
= 0;
1220 demuxer
->got_audio
= 0;
1221 demuxer
->got_video
= 0;
1222 demuxer
->stream_id
= 0;
1223 demuxer
->custom_id
= -1;
1225 if(mpeg3io_eof(title
->fs
)) return 1;
1227 //printf("mpeg3demux_read_program 2 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
1235 /* Search for next header */
1236 /* Parse packet until the next packet start code */
1237 while(!result
&& !mpeg3io_eof(title
->fs
))
1239 header
= mpeg3io_read_int32(title
->fs
);
1242 if(header
== MPEG3_PACK_START_CODE
)
1246 // Second start code in this call. Don't read it.
1249 mpeg3io_seek_relative(title
->fs
, -4);
1253 demuxer
->last_packet_start
= mpeg3io_tell(title
->fs
) - 4;
1254 result
= get_pack_header(demuxer
);
1261 if(header
== MPEG3_SYSTEM_START_CODE
&& pack_count
)
1266 result
= get_system_header(demuxer
);
1272 if((header
>> 8) == MPEG3_PACKET_START_CODE_PREFIX
&& pack_count
)
1276 result
= get_ps_pes_packet(demuxer
, header
);
1284 mpeg3io_seek_relative(title
->fs
, -3);
1294 // Ignore errors in the parsing. Just quit if eof.
1302 demuxer
->last_packet_end
= mpeg3io_tell(title
->fs
);
1305 * if(demuxer->last_packet_end - demuxer->last_packet_start != 0x800)
1306 * printf(__FUNCTION__ " packet_size=%llx data_size=%x pack_count=%d packet_start=%llx packet_end=%llx\n",
1307 * demuxer->last_packet_end - demuxer->last_packet_start,
1308 * demuxer->data_size,
1310 * demuxer->last_packet_start,
1311 * demuxer->last_packet_end);
1313 //printf("mpeg3demux_read_program 5 %d\n", result);
1315 //printf("read_program 3\n");
1316 // if(!result) result = mpeg3io_eof(title->fs);
1318 demuxer
->program_byte
=
1319 mpeg3_absolute_to_program(demuxer
, mpeg3io_tell(title
->fs
) +
1327 // Point the current title and current cell to the program byte.
1328 static int get_current_cell(mpeg3_demuxer_t
*demuxer
)
1333 /* Find first cell on or after current position */
1334 if(demuxer
->reverse
)
1336 for(demuxer
->current_title
= demuxer
->total_titles
- 1;
1337 demuxer
->current_title
>= 0;
1338 demuxer
->current_title
--)
1340 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1341 for(demuxer
->title_cell
= title
->cell_table_size
- 1;
1342 demuxer
->title_cell
>= 0;
1343 demuxer
->title_cell
--)
1345 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1346 if(cell
->program_start
< demuxer
->program_byte
&&
1347 cell
->program
== demuxer
->current_program
)
1350 if(demuxer
->program_byte
> cell
->program_end
)
1351 demuxer
->program_byte
= cell
->program_end
;
1360 demuxer
->current_title
= 0;
1361 demuxer
->title_cell
= 0;
1367 for(demuxer
->current_title
= 0;
1368 demuxer
->current_title
< demuxer
->total_titles
;
1369 demuxer
->current_title
++)
1371 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1372 for(demuxer
->title_cell
= 0;
1373 demuxer
->title_cell
< title
->cell_table_size
;
1374 demuxer
->title_cell
++)
1376 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1377 if(cell
->program_end
> demuxer
->program_byte
&&
1378 cell
->program
== demuxer
->current_program
)
1381 if(demuxer
->program_byte
< cell
->program_start
)
1382 demuxer
->program_byte
= cell
->program_start
;
1391 demuxer
->current_title
= demuxer
->total_titles
- 1;
1392 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1393 demuxer
->title_cell
= title
->cell_table_size
- 1;
1399 //printf("get_current_cell 2 %p %d %d\n", demuxer, demuxer->current_title, demuxer->title_cell);
1407 int mpeg3_seek_phys(mpeg3_demuxer_t
*demuxer
)
1411 // This happens if the video header is bigger than the buffer size in a table
1412 // of contents scan.
1413 if(demuxer
->current_title
< 0 ||
1414 demuxer
->current_title
>= demuxer
->total_titles
)
1416 printf("mpeg3_seek_phys demuxer=%p read_all=%d do_audio=%d do_video=%d demuxer->current_title=%d\n",
1421 demuxer
->current_title
);
1425 if(!demuxer
->titles
) return 1;
1427 //printf("%d %d\n", demuxer->current_title, demuxer->title_cell);
1428 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1430 if(!title
->cell_table
) return 1;
1432 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1435 /* Don't do anything if we're in the cell and it's the right program */
1436 if(demuxer
->reverse
)
1438 if(demuxer
->program_byte
> cell
->program_start
&&
1439 demuxer
->program_byte
<= cell
->program_end
&&
1440 cell
->program
== demuxer
->current_program
)
1448 if(demuxer
->stream_end
> 0 &&
1449 demuxer
->program_byte
>= demuxer
->stream_end
) return 1;
1452 if(demuxer
->program_byte
>= cell
->program_start
&&
1453 demuxer
->program_byte
< cell
->program_end
&&
1454 cell
->program
== demuxer
->current_program
)
1460 // Need to change cells if we get here.
1461 int last_cell
= demuxer
->title_cell
;
1462 int last_title
= demuxer
->current_title
;
1463 int64_t last_byte
= demuxer
->program_byte
;
1466 result
= get_current_cell(demuxer
);
1468 if(result
) return 1;
1470 if(demuxer
->current_title
!= last_title
)
1472 mpeg3demux_open_title(demuxer
, demuxer
->current_title
);
1475 title
= demuxer
->titles
[demuxer
->current_title
];
1476 cell
= &title
->cell_table
[demuxer
->title_cell
];
1480 mpeg3io_seek(title
->fs
,
1481 demuxer
->program_byte
- cell
->program_start
+ cell
->title_start
);
1492 static int next_code(mpeg3_demuxer_t
*demuxer
,
1495 uint32_t result
= 0;
1497 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1498 mpeg3_fs_t
*fd
= title
->fs
;
1500 while(result
!= code
&&
1503 title
= demuxer
->titles
[demuxer
->current_title
];
1505 result
|= (unsigned char)mpeg3io_read_char(title
->fs
);
1506 demuxer
->program_byte
++;
1507 error
= mpeg3_seek_phys(demuxer
);
1521 /* Read packet in the forward direction */
1522 int mpeg3_read_next_packet(mpeg3_demuxer_t
*demuxer
)
1524 if(demuxer
->current_title
< 0) return 1;
1527 int current_position
;
1528 mpeg3_t
*file
= demuxer
->file
;
1529 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1532 //printf("mpeg3_read_next_packet 1 %lld\n", demuxer->program_byte);
1533 /* Reset output buffer */
1534 demuxer
->data_size
= 0;
1535 demuxer
->data_position
= 0;
1536 demuxer
->audio_size
= 0;
1537 demuxer
->video_size
= 0;
1539 /* Switch to forward direction. */
1540 if(demuxer
->reverse
)
1542 /* Don't reread anything if we're at the beginning of the file. */
1543 if(demuxer
->program_byte
< 0)
1545 demuxer
->program_byte
= 0;
1546 result
= mpeg3_seek_phys(demuxer
);
1547 /* First character was the -1 byte which brings us to 0 after this function. */
1551 /* Transport or elementary stream */
1552 if(file
->packet_size
> 0)
1554 demuxer
->program_byte
+= file
->packet_size
;
1555 result
= mpeg3_seek_phys(demuxer
);
1559 /* Packet just read */
1560 if(!result
) result
= next_code(demuxer
,
1561 MPEG3_PACK_START_CODE
);
1563 if(!result
) result
= next_code(demuxer
,
1564 MPEG3_PACK_START_CODE
);
1567 demuxer
->reverse
= 0;
1576 /* Read packets until the output buffer is full. */
1577 /* Read a single packet if not fetching audio or video. */
1582 title
= demuxer
->titles
[demuxer
->current_title
];
1586 if(file
->is_transport_stream
)
1588 result
= mpeg3_seek_phys(demuxer
);
1589 if(!result
) result
= read_transport(demuxer
);
1592 if(file
->is_program_stream
)
1594 result
= mpeg3_seek_phys(demuxer
);
1595 if(!result
) result
= mpeg3demux_read_program(demuxer
);
1598 if(demuxer
->read_all
&& file
->is_audio_stream
)
1600 /* Read elementary stream. */
1601 result
= mpeg3io_read_data(demuxer
->audio_buffer
,
1602 file
->packet_size
, title
->fs
);
1603 demuxer
->audio_size
= file
->packet_size
;
1604 demuxer
->program_byte
+= file
->packet_size
;
1605 result
|= mpeg3_seek_phys(demuxer
);
1608 if(demuxer
->read_all
&& file
->is_video_stream
)
1610 /* Read elementary stream. */
1611 result
= mpeg3io_read_data(demuxer
->video_buffer
,
1612 file
->packet_size
, title
->fs
);
1613 demuxer
->video_size
= file
->packet_size
;
1614 demuxer
->program_byte
+= file
->packet_size
;
1615 result
|= mpeg3_seek_phys(demuxer
);
1619 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1620 file
->packet_size
, title
->fs
);
1621 demuxer
->data_size
= file
->packet_size
;
1622 demuxer
->program_byte
+= file
->packet_size
;
1623 result
|= mpeg3_seek_phys(demuxer
);
1627 demuxer
->data_size
== 0 &&
1628 (demuxer
->do_audio
|| demuxer
->do_video
));
1637 static int prev_code(mpeg3_demuxer_t
*demuxer
,
1640 uint32_t result
= 0;
1642 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1643 mpeg3_fs_t
*fd
= title
->fs
;
1646 while(result
!= code
&&
1647 demuxer
->program_byte
> 0 &&
1651 title
= demuxer
->titles
[demuxer
->current_title
];
1652 mpeg3io_seek(title
->fs
, demuxer
->program_byte
- title
->start_byte
- 1LL);
1653 result
|= ((uint32_t)mpeg3io_read_char(title
->fs
)) << 24;
1654 demuxer
->program_byte
--;
1655 error
= mpeg3_seek_phys(demuxer
);
1664 /* Read the packet right before the packet we're currently on. */
1665 int mpeg3_read_prev_packet(mpeg3_demuxer_t
*demuxer
)
1668 mpeg3_t
*file
= demuxer
->file
;
1669 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1671 demuxer
->data_size
= 0;
1672 demuxer
->data_position
= 0;
1676 //printf("mpeg3_read_prev_packet 1 %d %d %llx\n", result, demuxer->current_title, mpeg3io_tell(title->fs));
1677 /* Switch to reverse direction */
1678 if(!demuxer
->reverse
)
1680 demuxer
->reverse
= 1;
1682 /* Transport stream or elementary stream case */
1683 if(file
->packet_size
> 0)
1685 demuxer
->program_byte
-= file
->packet_size
;
1686 result
= mpeg3_seek_phys(demuxer
);
1689 /* Program stream */
1691 result
= prev_code(demuxer
,
1692 MPEG3_PACK_START_CODE
);
1703 /* Go to beginning of previous packet */
1706 title
= demuxer
->titles
[demuxer
->current_title
];
1708 /* Transport stream or elementary stream case */
1709 if(file
->packet_size
> 0)
1711 demuxer
->program_byte
-= file
->packet_size
;
1712 result
= mpeg3_seek_phys(demuxer
);
1716 if(!result
) result
= prev_code(demuxer
,
1717 MPEG3_PACK_START_CODE
);
1722 /* Read packet and then rewind it */
1723 title
= demuxer
->titles
[demuxer
->current_title
];
1724 if(file
->is_transport_stream
&& !result
)
1726 result
= read_transport(demuxer
);
1728 if(demuxer
->program_byte
> 0)
1730 demuxer
->program_byte
-= file
->packet_size
;
1731 result
= mpeg3_seek_phys(demuxer
);
1735 if(file
->is_program_stream
&& !result
)
1737 int64_t current_position
= demuxer
->program_byte
;
1740 result
= mpeg3demux_read_program(demuxer
);
1743 while(demuxer
->program_byte
> current_position
&&
1746 result
= prev_code(demuxer
,
1747 MPEG3_PACK_START_CODE
);
1753 /* Elementary stream */
1754 /* Read the packet forwards and seek back to the start */
1755 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1761 demuxer
->data_size
= file
->packet_size
;
1762 result
= mpeg3io_seek(title
->fs
, demuxer
->program_byte
);
1766 demuxer
->data_size
== 0 &&
1767 (demuxer
->do_audio
|| demuxer
->do_video
));
1775 int mpeg3demux_read_data(mpeg3_demuxer_t
*demuxer
,
1776 unsigned char *output
,
1780 demuxer
->error_flag
= 0;
1782 if(demuxer
->data_position
>= 0)
1786 for(i
= 0; i
< size
&& !result
; )
1788 int fragment_size
= size
- i
;
1789 if(fragment_size
> demuxer
->data_size
- demuxer
->data_position
)
1790 fragment_size
= demuxer
->data_size
- demuxer
->data_position
;
1791 memcpy(output
+ i
, demuxer
->data_buffer
+ demuxer
->data_position
, fragment_size
);
1792 demuxer
->data_position
+= fragment_size
;
1797 result
= mpeg3_read_next_packet(demuxer
);
1803 int current_position
;
1804 /* Read backwards a full packet. */
1805 /* Only good for reading less than the size of a full packet, but */
1806 /* this routine should only be used for searching for previous markers. */
1807 current_position
= demuxer
->data_position
;
1808 result
= mpeg3_read_prev_packet(demuxer
);
1809 if(!result
) demuxer
->data_position
= demuxer
->data_size
+ current_position
;
1810 memcpy(output
, demuxer
->data_buffer
+ demuxer
->data_position
, size
);
1811 demuxer
->data_position
+= size
;
1814 //printf("mpeg3demux_read_data 2\n");
1815 demuxer
->error_flag
= result
;
1819 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t
*demuxer
)
1821 demuxer
->error_flag
= 0;
1822 demuxer
->next_char
= -1;
1824 if(demuxer
->data_position
>= demuxer
->data_size
)
1826 demuxer
->error_flag
= mpeg3_read_next_packet(demuxer
);
1829 if(!demuxer
->error_flag
)
1830 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
++];
1832 return demuxer
->next_char
;
1835 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t
*demuxer
)
1837 demuxer
->error_flag
= 0;
1838 demuxer
->data_position
--;
1839 if(demuxer
->data_position
< 0)
1841 demuxer
->error_flag
= mpeg3_read_prev_packet(demuxer
);
1842 if(!demuxer
->error_flag
) demuxer
->data_position
= demuxer
->data_size
- 1;
1845 if(demuxer
->data_position
>= 0)
1847 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
];
1849 return demuxer
->next_char
;
1854 int mpeg3demux_open_title(mpeg3_demuxer_t
*demuxer
, int title_number
)
1856 mpeg3_title_t
*title
;
1858 if(title_number
< demuxer
->total_titles
&&
1861 if(demuxer
->current_title
>= 0)
1863 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
1864 demuxer
->current_title
= -1;
1867 title
= demuxer
->titles
[title_number
];
1869 if(mpeg3io_open_file(title
->fs
))
1871 demuxer
->error_flag
= 1;
1872 fprintf(stderr
, "mpeg3demux_open_title %s: %s", title
->fs
->path
, strerror(errno
));
1876 demuxer
->current_title
= title_number
;
1881 fprintf(stderr
, "mpeg3demux_open_title title_number = %d\n",
1886 return demuxer
->error_flag
;
1889 int mpeg3demux_copy_titles(mpeg3_demuxer_t
*dst
, mpeg3_demuxer_t
*src
)
1892 mpeg3_t
*file
= dst
->file
;
1893 mpeg3_title_t
*dst_title
, *src_title
;
1895 dst
->total_titles
= src
->total_titles
;
1896 dst
->total_programs
= src
->total_programs
;
1897 for(i
= 0; i
< MPEG3_MAX_STREAMS
; i
++)
1899 dst
->astream_table
[i
] = src
->astream_table
[i
];
1900 dst
->vstream_table
[i
] = src
->vstream_table
[i
];
1902 for(i
= 0; i
< src
->total_titles
; i
++)
1904 src_title
= src
->titles
[i
];
1905 dst_title
= dst
->titles
[i
] = mpeg3_new_title(file
,
1906 src
->titles
[i
]->fs
->path
);
1907 mpeg3_copy_title(dst_title
, src_title
);
1910 mpeg3demux_open_title(dst
, src
->current_title
);
1911 dst
->title_cell
= 0;
1915 /* ==================================================================== */
1917 /* ==================================================================== */
1919 mpeg3_demuxer_t
* mpeg3_new_demuxer(mpeg3_t
*file
, int do_audio
, int do_video
, int custom_id
)
1921 mpeg3_demuxer_t
*demuxer
= calloc(1, sizeof(mpeg3_demuxer_t
));
1924 /* The demuxer will change the default packet size for its own use. */
1925 demuxer
->file
= file
;
1926 demuxer
->do_audio
= do_audio
;
1927 demuxer
->do_video
= do_video
;
1929 /* Allocate buffer + padding */
1930 demuxer
->raw_data
= calloc(1, MPEG3_RAW_SIZE
);
1931 demuxer
->data_buffer
= calloc(1, MPEG3_RAW_SIZE
);
1932 demuxer
->data_allocated
= MPEG3_RAW_SIZE
;
1934 demuxer
->audio_buffer
= calloc(1, MPEG3_RAW_SIZE
);
1935 demuxer
->audio_allocated
= MPEG3_RAW_SIZE
;
1937 demuxer
->video_buffer
= calloc(1, MPEG3_RAW_SIZE
);
1938 demuxer
->video_allocated
= MPEG3_RAW_SIZE
;
1940 /* System specific variables */
1941 demuxer
->audio_pid
= custom_id
;
1942 demuxer
->video_pid
= custom_id
;
1943 demuxer
->astream
= custom_id
;
1944 demuxer
->vstream
= custom_id
;
1945 demuxer
->current_title
= -1;
1946 demuxer
->pes_audio_time
= -1;
1947 demuxer
->pes_video_time
= -1;
1948 //printf("mpeg3_new_demuxer %f\n", demuxer->time);
1949 demuxer
->stream_end
= -1;
1954 int mpeg3_delete_demuxer(mpeg3_demuxer_t
*demuxer
)
1958 if(demuxer
->current_title
>= 0)
1960 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
1963 for(i
= 0; i
< demuxer
->total_titles
; i
++)
1965 mpeg3_delete_title(demuxer
->titles
[i
]);
1968 if(demuxer
->data_buffer
) free(demuxer
->data_buffer
);
1969 if(demuxer
->raw_data
) free(demuxer
->raw_data
);
1970 if(demuxer
->audio_buffer
) free(demuxer
->audio_buffer
);
1971 if(demuxer
->video_buffer
) free(demuxer
->video_buffer
);
1977 int mpeg3demux_eof(mpeg3_demuxer_t
*demuxer
)
1979 mpeg3_t
*file
= demuxer
->file
;
1982 if(demuxer
->current_title
>= 0)
1984 if(mpeg3io_eof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
1985 demuxer
->current_title
>= demuxer
->total_titles
- 1)
1991 if(demuxer
->data_position
>= demuxer
->data_size
) return 1;
1997 int mpeg3demux_bof(mpeg3_demuxer_t
*demuxer
)
1999 if(demuxer
->current_title
>= 0)
2001 if(mpeg3io_bof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
2002 demuxer
->current_title
<= 0)
2008 void mpeg3demux_start_reverse(mpeg3_demuxer_t
*demuxer
)
2010 demuxer
->reverse
= 1;
2013 void mpeg3demux_start_forward(mpeg3_demuxer_t
*demuxer
)
2015 demuxer
->reverse
= 0;
2018 /* Seek to absolute byte */
2019 int mpeg3demux_seek_byte(mpeg3_demuxer_t
*demuxer
, int64_t byte
)
2021 mpeg3_t
*file
= demuxer
->file
;
2025 * for(i = 0; i < demuxer->total_titles; i++) mpeg3_dump_title(demuxer->titles[i]);
2028 demuxer
->program_byte
= byte
;
2029 demuxer
->data_position
= 0;
2030 demuxer
->data_size
= 0;
2032 /* Get on a packet boundary only for transport streams. */
2033 if(file
->is_transport_stream
&&
2036 demuxer
->program_byte
-= demuxer
->program_byte
% file
->packet_size
;
2039 int result
= mpeg3_seek_phys(demuxer
);
2041 * printf("mpeg3demux_seek_byte 1 %d %d %lld %lld\n",
2042 * demuxer->do_video, result, byte, demuxer->program_byte);
2055 double mpeg3demux_get_time(mpeg3_demuxer_t
*demuxer
)
2057 return demuxer
->time
;
2060 double mpeg3demux_audio_pts(mpeg3_demuxer_t
*demuxer
)
2062 return demuxer
->pes_audio_time
;
2065 double mpeg3demux_video_pts(mpeg3_demuxer_t
*demuxer
)
2067 return demuxer
->pes_video_time
;
2070 void mpeg3demux_reset_pts(mpeg3_demuxer_t
*demuxer
)
2072 demuxer
->pes_audio_time
= -1;
2073 demuxer
->pes_video_time
= -1;
2076 double mpeg3demux_scan_pts(mpeg3_demuxer_t
*demuxer
)
2078 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
2079 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
2080 int64_t current_position
= start_position
;
2083 mpeg3demux_reset_pts(demuxer
);
2085 current_position
< end_position
&&
2086 ((demuxer
->do_audio
&& demuxer
->pes_audio_time
< 0) ||
2087 (demuxer
->do_video
&& demuxer
->pes_video_time
< 0)))
2089 result
= mpeg3_read_next_packet(demuxer
);
2090 current_position
= mpeg3demux_tell_byte(demuxer
);
2093 // Seek back to starting point
2094 mpeg3demux_seek_byte(demuxer
, start_position
);
2096 if(demuxer
->do_audio
) return demuxer
->pes_audio_time
;
2097 if(demuxer
->do_video
) return demuxer
->pes_video_time
;
2100 int mpeg3demux_goto_pts(mpeg3_demuxer_t
*demuxer
, double pts
)
2102 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
2103 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
2104 int64_t current_position
= start_position
;
2107 // Search forward for nearest pts
2108 mpeg3demux_reset_pts(demuxer
);
2109 while(!result
&& current_position
< end_position
)
2111 result
= mpeg3_read_next_packet(demuxer
);
2112 if(demuxer
->pes_audio_time
> pts
) break;
2113 current_position
= mpeg3demux_tell_byte(demuxer
);
2116 // Search backward for nearest pts
2117 end_position
= current_position
- MPEG3_PTS_RANGE
;
2118 mpeg3_read_prev_packet(demuxer
);
2120 current_position
> end_position
)
2122 result
= mpeg3_read_prev_packet(demuxer
);
2123 if(demuxer
->pes_audio_time
< pts
) break;
2124 current_position
= mpeg3demux_tell_byte(demuxer
);
2129 int64_t mpeg3demux_tell_byte(mpeg3_demuxer_t
*demuxer
)
2131 return demuxer
->program_byte
;
2134 int mpeg3demux_tell_program(mpeg3_demuxer_t
*demuxer
)
2136 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2137 if(!title
->cell_table
|| !title
->cell_table_size
) return 0;
2138 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
2139 return cell
->program
;
2142 int64_t mpeg3_absolute_to_program(mpeg3_demuxer_t
*demuxer
,
2147 //fprintf(stderr, "%d\n", demuxer->data_size);
2148 // Can only offset to current cell since we can't know what cell the
2149 // byte corresponds to.
2150 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2151 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
2155 cell
->program_start
;
2158 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t
*demuxer
)
2160 if(!demuxer
->total_bytes
)
2164 for(i
= 0; i
< demuxer
->total_titles
; i
++)
2166 mpeg3_title_t
*title
= demuxer
->titles
[i
];
2167 for(j
= 0; j
< title
->cell_table_size
; j
++)
2169 mpeg3_cell_t
*cell
= &title
->cell_table
[j
];
2170 if(cell
->program
== demuxer
->current_program
)
2171 result
+= cell
->program_end
- cell
->program_start
;
2173 // result += demuxer->titles[i]->total_bytes;
2175 demuxer
->total_bytes
= result
;
2177 return demuxer
->total_bytes
;
2180 int64_t mpeg3demuxer_title_bytes(mpeg3_demuxer_t
*demuxer
)
2182 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2183 return title
->total_bytes
;
2186 mpeg3_demuxer_t
* mpeg3_get_demuxer(mpeg3_t
*file
)
2188 if(file
->is_program_stream
|| file
->is_transport_stream
)
2190 if(file
->total_astreams
) return file
->atrack
[0]->demuxer
;
2192 if(file
->total_vstreams
) return file
->vtrack
[0]->demuxer
;
2197 void mpeg3demux_append_data(mpeg3_demuxer_t
*demuxer
,
2198 unsigned char *data
,
2201 int new_data_size
= demuxer
->data_size
+ bytes
;
2202 if(new_data_size
>= demuxer
->data_allocated
)
2204 demuxer
->data_allocated
= new_data_size
* 2;
2205 demuxer
->data_buffer
= realloc(demuxer
->data_buffer
,
2206 demuxer
->data_allocated
);
2209 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
2212 demuxer
->data_size
+= bytes
;
2215 void mpeg3demux_shift_data(mpeg3_demuxer_t
*demuxer
,
2219 for(i
= 0, j
= bytes
; j
< demuxer
->data_size
; i
++, j
++)
2221 demuxer
->data_buffer
[i
] = demuxer
->data_buffer
[j
];
2223 demuxer
->data_size
-= bytes
;
2224 demuxer
->data_position
-= bytes
;