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 //printf(__FUNCTION__ " called\n");
67 a
= demuxer
->raw_data
[demuxer
->raw_offset
++];
68 b
= demuxer
->raw_data
[demuxer
->raw_offset
++];
69 c
= demuxer
->raw_data
[demuxer
->raw_offset
++];
70 d
= demuxer
->raw_data
[demuxer
->raw_offset
++];
71 result
= (a
<< 24) | (b
<< 16) | (c
<< 8) | d
;
76 static inline unsigned int packet_skip(mpeg3_demuxer_t
*demuxer
, long length
)
78 //printf(__FUNCTION__ " called\n");
79 demuxer
->raw_offset
+= length
;
83 static int get_adaptation_field(mpeg3_demuxer_t
*demuxer
)
88 demuxer
->adaptation_fields
++;
89 /* get adaptation field length */
90 length
= packet_read_char(demuxer
);
95 pcr_flag
= (packet_read_char(demuxer
) >> 4) & 1;
99 unsigned long clk_ref_base
= packet_read_int32(demuxer
);
100 unsigned int clk_ref_ext
= packet_read_int16(demuxer
);
102 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 clk_ref_base
<<= 1; /* Create space for bit */
110 clk_ref_base
|= (clk_ref_ext
>> 15); /* Take bit */
111 clk_ref_ext
&= 0x01ff; /* Only lower 9 bits */
113 demuxer
->time
= ((double)clk_ref_base
+ clk_ref_ext
/ 300) / 90000;
114 if(length
) packet_skip(demuxer
, length
- 7);
117 packet_skip(demuxer
, length
- 1);
123 static int get_program_association_table(mpeg3_demuxer_t
*demuxer
)
125 demuxer
->program_association_tables
++;
126 demuxer
->table_id
= packet_read_char(demuxer
);
127 demuxer
->section_length
= packet_read_int16(demuxer
) & 0xfff;
128 demuxer
->transport_stream_id
= packet_read_int16(demuxer
);
129 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
133 static int get_data_buffer(mpeg3_demuxer_t
*demuxer
)
135 while(demuxer
->raw_offset
< demuxer
->raw_size
&&
136 demuxer
->data_size
< MPEG3_RAW_SIZE
)
138 demuxer
->data_buffer
[demuxer
->data_size
++] =
139 demuxer
->raw_data
[demuxer
->raw_offset
++];
144 static int get_pes_packet_header(mpeg3_demuxer_t
*demuxer
, unsigned long *pts
, unsigned long *dts
)
146 unsigned int pes_header_bytes
= 0;
147 unsigned int pts_dts_flags
;
148 int pes_header_data_length
;
150 /* drop first 8 bits */
151 packet_read_char(demuxer
);
152 pts_dts_flags
= (packet_read_char(demuxer
) >> 6) & 0x3;
153 pes_header_data_length
= packet_read_char(demuxer
);
156 /* Get Presentation Time stamps and Decoding Time Stamps */
157 if(pts_dts_flags
== 2)
159 *pts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
161 *pts
|= (packet_read_int16(demuxer
) >> 1);
163 *pts
|= (packet_read_int16(demuxer
) >> 1);
164 pes_header_bytes
+= 5;
166 else if(pts_dts_flags
== 3)
168 *pts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
170 *pts
|= (packet_read_int16(demuxer
) >> 1);
172 *pts
|= (packet_read_int16(demuxer
) >> 1);
173 *dts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
175 *dts
|= (packet_read_int16(demuxer
) >> 1);
177 *dts
|= (packet_read_int16(demuxer
) >> 1);
178 pes_header_bytes
+= 10;
181 demuxer
->time
= (double)*pts
/ 90000;
182 //printf("get_pes_packet_header %f\n", demuxer->time);
184 /* extract other stuff here! */
185 packet_skip(demuxer
, pes_header_data_length
- pes_header_bytes
);
189 static int get_unknown_data(mpeg3_demuxer_t
*demuxer
)
191 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
195 // Combine the pid and the stream id into one unit
196 #define CUSTOM_ID(pid, stream_id) (((pid << 8) | stream_id) & 0xffff)
199 static int get_pes_packet_data(mpeg3_demuxer_t
*demuxer
, unsigned int stream_id
)
201 unsigned long pts
= 0, dts
= 0;
202 get_pes_packet_header(demuxer
, &pts
, &dts
);
205 //printf("get_pes_packet_data %x\n", CUSTOM_ID(demuxer->pid, stream_id));
206 if(stream_id
== 0xbd)
208 // Don't know if the next byte is the true stream id like in program stream
211 if(demuxer
->read_all
)
212 demuxer
->astream_table
[CUSTOM_ID(demuxer
->pid
, stream_id
)] = AUDIO_AC3
;
213 if(demuxer
->astream
== -1)
214 demuxer
->astream
= CUSTOM_ID(demuxer
->pid
, stream_id
);
216 if(CUSTOM_ID(demuxer
->pid
, stream_id
) == demuxer
->astream
&&
219 demuxer
->pes_audio_time
= (double)pts
/ 90000;
220 demuxer
->audio_pid
= demuxer
->pid
;
221 return get_data_buffer(demuxer
);
225 if((stream_id
>> 4) == 12 || (stream_id
>> 4) == 13)
227 /* Just pick the first available stream if no ID is set */
228 if(demuxer
->read_all
)
229 demuxer
->astream_table
[CUSTOM_ID(demuxer
->pid
, stream_id
)] = AUDIO_MPEG
;
230 if(demuxer
->astream
== -1)
231 demuxer
->astream
= CUSTOM_ID(demuxer
->pid
, stream_id
);
233 if(CUSTOM_ID(demuxer
->pid
, stream_id
) == demuxer
->astream
&&
236 demuxer
->pes_audio_time
= (double)pts
/ 90000;
237 demuxer
->audio_pid
= demuxer
->pid
;
239 return get_data_buffer(demuxer
);
243 if((stream_id
>> 4) == 14)
245 //printf("get_pes_packet_data video %x\n", stream_id);
246 /* Just pick the first available stream if no ID is set */
247 if(demuxer
->read_all
)
248 demuxer
->vstream_table
[CUSTOM_ID(demuxer
->pid
, stream_id
)] = 1;
250 if(demuxer
->vstream
== -1)
251 demuxer
->vstream
= (CUSTOM_ID(demuxer
->pid
, stream_id
));
254 //printf("get_pes_packet_data %x %d\n", stream_id, demuxer->payload_unit_start_indicator);
255 if(CUSTOM_ID(demuxer
->pid
, stream_id
) == demuxer
->vstream
&&
258 demuxer
->pes_video_time
= (double)pts
/ 90000;
259 demuxer
->video_pid
= demuxer
->pid
;
261 * printf("get_pes_packet_data video %04x %llx\n",
263 * mpeg3io_tell(demuxer->titles[demuxer->current_title]->fs));
265 * for(i = demuxer->raw_offset; i < demuxer->raw_size; i++)
266 * printf("%02x ", demuxer->raw_data[i], stdout);
271 return get_data_buffer(demuxer
);
276 return get_unknown_data(demuxer
);
279 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
284 static int get_pes_packet(mpeg3_demuxer_t
*demuxer
)
286 unsigned int stream_id
;
288 demuxer
->pes_packets
++;
293 packet_read_int24(demuxer
);
294 stream_id
= packet_read_char(demuxer
);
298 /* Skip pes packet length */
299 packet_read_int16(demuxer
);
301 if(stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
302 stream_id
!= MPEG3_PADDING_STREAM
)
304 return get_pes_packet_data(demuxer
, stream_id
);
307 if(stream_id
== MPEG3_PRIVATE_STREAM_2
)
309 /* Dump private data! */
310 fprintf(stderr
, "stream_id == MPEG3_PRIVATE_STREAM_2\n");
311 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
315 if(stream_id
== MPEG3_PADDING_STREAM
)
317 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
322 fprintf(stderr
, "unknown stream_id in pes packet");
328 static int get_payload(mpeg3_demuxer_t
*demuxer
)
330 //printf("get_payload 1 %x %d\n", demuxer->pid, demuxer->payload_unit_start_indicator);
331 if(demuxer
->payload_unit_start_indicator
)
334 get_program_association_table(demuxer
);
336 if(packet_next_int24(demuxer
) == MPEG3_PACKET_START_CODE_PREFIX
)
337 get_pes_packet(demuxer
);
339 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
343 //printf("get_payload 2\n");
344 if(demuxer
->pid
== demuxer
->audio_pid
&& demuxer
->do_audio
)
346 get_data_buffer(demuxer
);
349 if(demuxer
->pid
== demuxer
->video_pid
&& demuxer
->do_video
)
351 get_data_buffer(demuxer
);
354 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
359 /* Read a transport packet */
360 static int read_transport(mpeg3_demuxer_t
*demuxer
)
362 mpeg3_t
*file
= (mpeg3_t
*)demuxer
->file
;
363 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
368 /* Packet size is known for transport streams */
369 demuxer
->raw_size
= file
->packet_size
;
370 demuxer
->raw_offset
= 0;
374 perror("read_transport");
378 //printf("read transport 1 %llx %llx\n", title->fs->current_byte, title->fs->total_bytes);
379 // Search for Sync byte */
382 bits
= mpeg3io_read_char(title
->fs
);
383 }while(!mpeg3io_eof(title
->fs
) && !result
&& bits
!= MPEG3_SYNC_BYTE
);
386 if(mpeg3io_eof(title
->fs
) || result
) return 1;
388 * printf("read transport 2 bits=%x tell=%llx packet_size=%x\n",
390 * mpeg3io_tell(title->fs),
391 * file->packet_size);
394 if(bits
== MPEG3_SYNC_BYTE
&& !result
)
396 demuxer
->raw_data
[0] = MPEG3_SYNC_BYTE
;
397 result
= mpeg3io_read_data(demuxer
->raw_data
+ 1,
398 file
->packet_size
- 1,
403 demuxer
->absolute_byte
= mpeg3io_tell(title
->fs
) +
408 packet_read_char(demuxer
);
409 bits
= packet_read_int24(demuxer
) & 0x00ffffff;
410 //printf("read transport 3 tell=%x bits=%x\n", mpeg3io_tell(title->fs), bits);
411 demuxer
->transport_error_indicator
= (bits
>> 23) & 0x1;
412 demuxer
->payload_unit_start_indicator
= (bits
>> 22) & 0x1;
413 demuxer
->pid
= (bits
>> 8) & 0x00001fff;
414 demuxer
->transport_scrambling_control
= (bits
>> 6) & 0x3;
415 demuxer
->adaptation_field_control
= (bits
>> 4) & 0x3;
416 demuxer
->continuity_counter
= bits
& 0xf;
417 //printf("read_transport 1 %llx %08x %d\n", mpeg3io_tell(title->fs), bits, demuxer->payload_unit_start_indicator);
419 //printf("read_transport 4 %x\n", demuxer->pid);
420 if(demuxer
->transport_error_indicator
)
422 fprintf(stderr
, "demuxer->transport_error_indicator\n");
423 demuxer
->absolute_byte
= mpeg3io_tell(title
->fs
) +
428 //printf("read_transport 5 %x\n", demuxer->pid);
429 if (demuxer
->pid
== 0x1fff)
431 demuxer
->is_padding
= 1; /* padding; just go to next */
435 demuxer
->is_padding
= 0;
438 //printf("read_transport 6 %x\n", demuxer->pid);
439 /* Get pid from table */
440 for(table_entry
= 0, result
= 0;
441 table_entry
< demuxer
->total_pids
;
444 if(demuxer
->pid
== demuxer
->pid_table
[table_entry
])
451 //printf("read_transport 7 %x\n", demuxer->pid);
455 /* Not in pid table */
458 demuxer
->pid_table
[table_entry
] = demuxer
->pid
;
459 demuxer
->continuity_counters
[table_entry
] = demuxer
->continuity_counter
; /* init */
460 demuxer
->total_pids
++;
464 /* Abort if padding. Should abort after demuxer->pid == 0x1fff for speed. */
465 if(demuxer
->is_padding
)
467 demuxer
->absolute_byte
= mpeg3io_tell(title
->fs
) +
475 if(demuxer
->pid
!= MPEG3_PROGRAM_ASSOCIATION_TABLE
&&
476 demuxer
->pid
!= MPEG3_CONDITIONAL_ACCESS_TABLE
&&
477 (demuxer
->adaptation_field_control
== 1 || demuxer
->adaptation_field_control
== 3))
479 if(demuxer
->continuity_counters
[table_entry
] != demuxer
->continuity_counter
)
481 // fprintf(stderr, "demuxer->continuity_counters[table_entry] != demuxer->continuity_counter\n");
483 demuxer
->continuity_counters
[table_entry
] = demuxer
->continuity_counter
;
485 if(++(demuxer
->continuity_counters
[table_entry
]) > 15) demuxer
->continuity_counters
[table_entry
] = 0;
495 if(demuxer
->adaptation_field_control
== 2 ||
496 demuxer
->adaptation_field_control
== 3)
497 result
= get_adaptation_field(demuxer
);
499 // Need to enter in astream and vstream table:
500 // PID ored with stream_id
501 if(demuxer
->adaptation_field_control
== 1 ||
502 demuxer
->adaptation_field_control
== 3)
503 result
= get_payload(demuxer
);
505 demuxer
->absolute_byte
= mpeg3io_tell(title
->fs
) +
510 static int get_system_header(mpeg3_demuxer_t
*demuxer
)
512 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
513 int length
= mpeg3io_read_int16(title
->fs
);
514 mpeg3io_seek_relative(title
->fs
, length
);
518 static unsigned long get_timestamp(mpeg3_demuxer_t
*demuxer
)
520 unsigned long timestamp
;
521 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
523 /* Only low 4 bits (7==1111) */
524 timestamp
= (mpeg3io_read_char(title
->fs
) >> 1) & 7;
526 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
528 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
532 static int get_pack_header(mpeg3_demuxer_t
*demuxer
)
535 unsigned long clock_ref
, clock_ref_ext
;
536 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
538 /* Get the time code */
539 if((mpeg3io_next_char(title
->fs
) >> 4) == 2)
542 demuxer
->time
= (double)get_timestamp(demuxer
) / 90000;
544 mpeg3io_read_int24(title
->fs
);
547 if(mpeg3io_next_char(title
->fs
) & 0x40)
549 i
= mpeg3io_read_int32(title
->fs
);
550 j
= mpeg3io_read_int16(title
->fs
);
552 if(i
& 0x40000000 || (i
>> 28) == 2)
554 clock_ref
= ((i
& 0x38000000) << 3);
555 clock_ref
|= ((i
& 0x03fff800) << 4);
556 clock_ref
|= ((i
& 0x000003ff) << 5);
557 clock_ref
|= ((j
& 0xf800) >> 11);
558 clock_ref_ext
= (j
>> 1) & 0x1ff;
560 demuxer
->time
= (double)(clock_ref
+ clock_ref_ext
/ 300) / 90000;
562 mpeg3io_read_int24(title
->fs
);
563 i
= mpeg3io_read_char(title
->fs
) & 0x7;
566 mpeg3io_seek_relative(title
->fs
, i
);
571 mpeg3io_seek_relative(title
->fs
, 2);
576 /* Program packet reading core */
577 static int get_ps_pes_packet(mpeg3_demuxer_t
*demuxer
, unsigned int header
)
579 unsigned long pts
= 0, dts
= 0;
581 int pes_packet_length
;
582 int64_t pes_packet_start
;
583 long decryption_offset
;
585 mpeg3_t
*file
= demuxer
->file
;
586 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
588 int data_start
= demuxer
->data_size
;
589 /* Format if audio */
592 stream_id
= header
& 0xff;
593 pes_packet_length
= mpeg3io_read_int16(title
->fs
);
594 pes_packet_start
= mpeg3io_tell(title
->fs
);
599 * printf(__FUNCTION__ " pes_packet_start=%llx pes_packet_length=%x data_size=%x\n",
602 * demuxer->data_size);
609 if(stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
610 stream_id
!= MPEG3_PADDING_STREAM
)
612 if((mpeg3io_next_char(title
->fs
) >> 6) == 0x02)
614 /* Get MPEG-2 packet */
615 int pes_header_bytes
= 0;
617 int pes_header_data_length
;
620 demuxer
->last_packet_decryption
= mpeg3io_tell(title
->fs
);
621 scrambling
= mpeg3io_read_char(title
->fs
) & 0x30;
623 /* Reset scrambling bit for the mpeg3cat utility */
624 // if(scrambling) demuxer->raw_data[demuxer->raw_offset - 1] &= 0xcf;
625 // Force packet length if scrambling
626 if(scrambling
) pes_packet_length
= 0x800 -
628 demuxer
->last_packet_start
;
631 pts_dts_flags
= (mpeg3io_read_char(title
->fs
) >> 6) & 0x3;
632 pes_header_data_length
= mpeg3io_read_char(title
->fs
);
636 /* Get Presentation and Decoding Time Stamps */
637 if(pts_dts_flags
== 2)
639 pts
= (mpeg3io_read_char(title
->fs
) >> 1) & 7; /* Only low 4 bits (7==1111) */
641 pts
|= (mpeg3io_read_int16(title
->fs
) >> 1);
643 pts
|= (mpeg3io_read_int16(title
->fs
) >> 1);
644 pes_header_bytes
+= 5;
647 if(pts_dts_flags
== 3)
649 pts
= (mpeg3io_read_char(title
->fs
) >> 1) & 7; /* Only low 4 bits (7==1111) */
651 pts
|= (mpeg3io_read_int16(title
->fs
) >> 1);
653 pts
|= (mpeg3io_read_int16(title
->fs
) >> 1);
655 dts
= (mpeg3io_read_char(title
->fs
) >> 1) & 7; /* Only low 4 bits (7==1111) */
657 dts
|= (mpeg3io_read_int16(title
->fs
) >> 1);
659 dts
|= (mpeg3io_read_int16(title
->fs
) >> 1);
660 pes_header_bytes
+= 10;
663 //printf("get_ps_pes_packet do_audio=%d do_video=%d pts=%x dts=%x\n",
664 // demuxer->do_audio, demuxer->do_video, pts, dts);
667 mpeg3io_seek_relative(title
->fs
,
668 pes_header_data_length
- pes_header_bytes
);
673 /* Get MPEG-1 packet */
674 while(mpeg3io_next_char(title
->fs
) == 0xff)
676 mpeg3io_read_char(title
->fs
);
679 /* Skip STD buffer scale */
680 if((mpeg3io_next_char(title
->fs
) & 0x40) == 0x40)
682 mpeg3io_seek_relative(title
->fs
, 2);
685 /* Decide which timestamps are available */
686 pts_dts_flags
= mpeg3io_next_char(title
->fs
);
688 if(pts_dts_flags
>= 0x30)
690 /* Get the presentation and decoding time stamp */
691 pts
= get_timestamp(demuxer
);
692 dts
= get_timestamp(demuxer
);
695 if(pts_dts_flags
>= 0x20)
697 /* Get just the presentation time stamp */
698 pts
= get_timestamp(demuxer
);
701 if(pts_dts_flags
== 0x0f)
703 /* End of timestamps */
704 mpeg3io_read_char(title
->fs
);
708 return 1; /* Error */
712 /* Now extract the payload. */
713 if((stream_id
>> 4) == 0xc || (stream_id
>> 4) == 0xd)
716 /* Take first stream ID if -1 */
717 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
722 if(demuxer
->read_all
)
723 demuxer
->astream_table
[stream_id
& 0x0f] = AUDIO_MPEG
;
725 if(demuxer
->astream
== -1)
726 demuxer
->astream
= stream_id
& 0x0f;
731 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
734 if((stream_id
& 0x0f) == demuxer
->astream
&& demuxer
->do_audio
)
736 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
737 mpeg3io_read_data(demuxer
->data_buffer
+ demuxer
->data_size
,
740 demuxer
->data_size
+= pes_packet_length
;
744 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
748 if((stream_id
>> 4) == 0xe)
751 /* Take first stream ID if -1 */
752 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
756 if(demuxer
->read_all
)
757 demuxer
->vstream_table
[stream_id
& 0x0f] = 1;
759 if(demuxer
->vstream
== -1)
760 demuxer
->vstream
= stream_id
& 0x0f;
762 if(pts
> 0) demuxer
->pes_video_time
= (double)pts
/ 60000;
766 if((stream_id
& 0x0f) == demuxer
->vstream
&& demuxer
->do_video
)
768 //printf(__FUNCTION__ " stream_id=%x size=%x\n", stream_id, pes_packet_length);
769 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
770 mpeg3io_read_data(demuxer
->data_buffer
+ demuxer
->data_size
,
774 demuxer
->data_size
+= pes_packet_length
;
778 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
782 if((stream_id
== 0xbd || stream_id
== 0xbf) &&
783 mpeg3io_next_char(title
->fs
) != 0xff &&
784 ((mpeg3io_next_char(title
->fs
) & 0xf0) != 0x20))
788 /* Get the audio format */
789 if((mpeg3io_next_char(title
->fs
) & 0xf0) == 0xa0)
794 // Picks up bogus data if (& 0xf) or (& 0x7f)
795 stream_id
= mpeg3io_next_char(title
->fs
);
796 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
798 //printf("get_ps_pes_packet %x\n", stream_id);
799 /* Take first stream ID if not building TOC. */
800 if(demuxer
->read_all
)
801 demuxer
->astream_table
[stream_id
] = format
;
803 if(demuxer
->astream
== -1)
804 demuxer
->astream
= stream_id
;
812 //printf("get_ps_pes_packet 5 %x\n", format);
813 if(stream_id
== demuxer
->astream
&& demuxer
->do_audio
)
815 demuxer
->aformat
= format
;
816 mpeg3io_read_int32(title
->fs
);
818 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
819 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
821 if(format
== AUDIO_PCM
) do_pcm
= 1;
822 //printf("get_ps_pes_packet 5 %x\n", decryption_offset);
826 mpeg3io_read_data(demuxer
->data_buffer
+ demuxer
->data_size
,
829 demuxer
->data_size
+= pes_packet_length
;
833 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
834 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
836 //printf("get_ps_pes_packet 6 %d\n", demuxer->astream_table[0x20]);
839 if(stream_id
== 0xbc || 1)
841 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
842 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
846 if(stream_id
== MPEG3_PRIVATE_STREAM_2
|| stream_id
== MPEG3_PADDING_STREAM
)
848 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
849 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
850 //printf(__FUNCTION__ " 2 %llx\n", mpeg3io_tell(title->fs));
857 // Advance 2048 bytes if scrambled. We might pick up a spurrius
858 // packet start code in the scrambled data otherwise.
860 demuxer
->last_packet_start
+ 0x800 > mpeg3io_tell(title
->fs
))
862 mpeg3io_seek_relative(title
->fs
,
863 demuxer
->last_packet_start
+ 0x800 - mpeg3io_tell(title
->fs
));
870 // Descramble if desired
871 if(demuxer
->data_size
&& scrambling
)
873 //printf(__FUNCTION__ " data_size=%x decryption_offset=%x\n", demuxer->data_size, decryption_offset);
874 if(mpeg3_decrypt_packet(title
->fs
->css
,
875 demuxer
->data_buffer
,
878 fprintf(stderr
, "get_ps_pes_packet: Decryption not available\n");
888 /* Synthesize PCM header and delete MPEG header for PCM data */
889 /* Must be done after decryption */
897 unsigned char *output
= demuxer
->data_buffer
+ data_start
;
900 /* Shift audio back */
902 for(i
= demuxer
->data_size
- 1, j
= demuxer
->data_size
+ PCM_HEADERSIZE
- 3 - 1;
905 *(demuxer
->data_buffer
+ j
) = *(demuxer
->data_buffer
+ i
);
906 demuxer
->data_size
+= PCM_HEADERSIZE
- 3;
908 bits_code
= (code
>> 6) & 3;
909 samplerate_code
= (code
& 0x10);
917 switch(samplerate_code
)
927 *(int32_t*)(output
+ 4) = samplerate
;
931 case 0: bits
= 16; break;
932 case 1: bits
= 20; break;
933 case 2: bits
= 24; break;
934 default: bits
= 16; break;
936 *(int32_t*)(output
+ 8) = bits
;
938 *(int32_t*)(output
+ 12) = (code
& 0x7) + 1;
940 *(int32_t*)(output
+ 16) = pes_packet_length
-
943 //printf(__FUNCTION__ " %02x%02x%02x %d\n", code1, code, code2, pes_packet_length - 3);
953 //printf(__FUNCTION__ " 3 %llx %x\n", mpeg3io_tell(title->fs), demuxer->data_size);
956 //if(mpeg3io_tell(title->fs) - demuxer->last_packet_start != 0x800)
957 //printf(__FUNCTION__ " packet size == %llx\n", mpeg3io_tell(title->fs) - demuxer->last_packet_start);
959 //printf(__FUNCTION__ " pes_audio_time=%f pes_video_time=%f\n", demuxer->pes_audio_time, demuxer->pes_video_time);
964 int mpeg3demux_read_program(mpeg3_demuxer_t
*demuxer
)
968 mpeg3_t
*file
= demuxer
->file
;
969 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
970 unsigned int header
= 0;
973 demuxer
->data_size
= 0;
974 //printf("mpeg3demux_read_program 1 %d %llx %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
976 if(mpeg3io_eof(title
->fs
)) return 1;
978 //printf("mpeg3demux_read_program 2 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
986 /* Search for next header */
987 /* Parse packet until the next packet start code */
988 while(!result
&& !mpeg3io_eof(title
->fs
))
990 header
= mpeg3io_read_int32(title
->fs
);
994 if(header
== MPEG3_PACK_START_CODE
)
998 // Second start code in this call. Don't read it.
1001 mpeg3io_seek_relative(title
->fs
, -4);
1005 demuxer
->last_packet_start
= mpeg3io_tell(title
->fs
) - 4;
1006 result
= get_pack_header(demuxer
);
1007 //printf("mpeg3demux_read_program MPEG3_PACK_START_CODE %d\n", result);
1014 if(header
== MPEG3_SYSTEM_START_CODE
&& pack_count
)
1019 result
= get_system_header(demuxer
);
1020 //printf("mpeg3demux_read_program MPEG3_SYSTEM_START_CODE %d\n", result);
1026 if((header
>> 8) == MPEG3_PACKET_START_CODE_PREFIX
&& pack_count
)
1030 result
= get_ps_pes_packet(demuxer
, header
);
1031 //printf("mpeg3demux_read_program MPEG3_PACKET_START_CODE_PREFIX %d %08x\n", result, header);
1039 //printf(__FUNCTION__ " %llx %08x\n", mpeg3io_tell(title->fs), header);
1040 mpeg3io_seek_relative(title
->fs
, -3);
1043 //printf("mpeg3demux_read_program 3 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
1051 // Ignore errors in the parsing. Just quit if eof.
1059 demuxer
->last_packet_end
= mpeg3io_tell(title
->fs
);
1062 * if(demuxer->last_packet_end - demuxer->last_packet_start != 0x800)
1063 * printf(__FUNCTION__ " packet_size=%llx data_size=%x pack_count=%d packet_start=%llx packet_end=%llx\n",
1064 * demuxer->last_packet_end - demuxer->last_packet_start,
1065 * demuxer->data_size,
1067 * demuxer->last_packet_start,
1068 * demuxer->last_packet_end);
1070 //printf("mpeg3demux_read_program 5 %d\n", result);
1072 //printf("read_program 3\n");
1073 // if(!result) result = mpeg3io_eof(title->fs);
1075 demuxer
->absolute_byte
= mpeg3io_tell(title
->fs
) +
1118 int mpeg3_advance_cell(mpeg3_demuxer_t
*demuxer
)
1120 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1122 mpeg3io_seek(title
->fs
, demuxer
->absolute_byte
- title
->start_byte
);
1124 //printf("mpeg3_advance_cell %llx %llx\n",demuxer->absolute_byte , title->start_byte);
1125 /* Don't do anything when constructing cell table */
1126 if(!title
->cell_table
||
1127 !title
->cell_table_size
||
1133 mpeg3demux_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1136 //printf("mpeg3_advance_cell 1\n");
1137 /* Don't do anything if we're in the cell and it's the right program */
1138 if(demuxer
->reverse
)
1141 * printf("mpeg3_advance_cell 1 %llx %llx %llx %llx %llx\n",
1142 * demuxer->absolute_byte,
1145 * title->start_byte,
1146 * title->end_byte);fflush(stdout);
1148 if(demuxer
->absolute_byte
> cell
->start_byte
+ title
->start_byte
&&
1149 demuxer
->absolute_byte
<= cell
->end_byte
+ title
->start_byte
&&
1150 cell
->program
== demuxer
->current_program
)
1157 if(demuxer
->absolute_byte
>= cell
->start_byte
+ title
->start_byte
&&
1158 demuxer
->absolute_byte
< cell
->end_byte
+ title
->start_byte
&&
1159 cell
->program
== demuxer
->current_program
)
1163 //printf("mpeg3_advance_cell 10\n");
1165 int last_cell
= demuxer
->title_cell
;
1166 int last_title
= demuxer
->current_title
;
1167 int64_t last_byte
= demuxer
->absolute_byte
;
1171 * printf("mpeg3_advance_cell 2 %llx %llx %llx %llx %llx\n",
1172 * demuxer->absolute_byte,
1173 * cell->start_byte + title->start_byte,
1174 * cell->end_byte + title->start_byte,
1175 * title->start_byte,
1176 * title->end_byte);fflush(stdout);
1179 /* Find first cell on or after current position */
1180 if(demuxer
->reverse
)
1182 for(demuxer
->current_title
= demuxer
->total_titles
- 1;
1183 demuxer
->current_title
>= 0;
1184 demuxer
->current_title
--)
1186 title
= demuxer
->titles
[demuxer
->current_title
];
1187 if(title
->start_byte
< demuxer
->absolute_byte
)
1189 for(demuxer
->title_cell
= title
->cell_table_size
- 1;
1190 demuxer
->title_cell
>= 0;
1191 demuxer
->title_cell
--)
1193 cell
= &title
->cell_table
[demuxer
->title_cell
];
1194 if(cell
->start_byte
+ title
->start_byte
< demuxer
->absolute_byte
&&
1195 cell
->program
== demuxer
->current_program
)
1198 if(demuxer
->absolute_byte
> cell
->end_byte
+ title
->start_byte
)
1199 demuxer
->absolute_byte
= cell
->end_byte
+ title
->start_byte
;
1209 demuxer
->current_title
= 0;
1210 demuxer
->title_cell
= 0;
1213 //printf("mpeg3_advance_cell 10\n");
1217 //printf("mpeg3_advance_cell 20\n");
1218 for(demuxer
->current_title
= 0;
1219 demuxer
->current_title
< demuxer
->total_titles
;
1220 demuxer
->current_title
++)
1222 title
= demuxer
->titles
[demuxer
->current_title
];
1223 if(title
->end_byte
> demuxer
->absolute_byte
)
1225 for(demuxer
->title_cell
= 0;
1226 demuxer
->title_cell
< title
->cell_table_size
;
1227 demuxer
->title_cell
++)
1229 cell
= &title
->cell_table
[demuxer
->title_cell
];
1230 if(cell
->end_byte
+ title
->start_byte
> demuxer
->absolute_byte
&&
1231 cell
->program
== demuxer
->current_program
)
1234 if(demuxer
->absolute_byte
< cell
->start_byte
+ title
->start_byte
)
1235 demuxer
->absolute_byte
= cell
->start_byte
+ title
->start_byte
;
1245 demuxer
->current_title
= demuxer
->total_titles
- 1;
1246 title
= demuxer
->titles
[demuxer
->current_title
];
1247 demuxer
->title_cell
= title
->cell_table_size
- 1;
1250 //printf("mpeg3_advance_cell 30\n");
1255 * printf("mpeg3_advance_cell 40 %llx %llx %llx %llx %llx\n",
1256 * demuxer->absolute_byte,
1257 * cell->start_byte + title->start_byte,
1258 * cell->end_byte + title->start_byte,
1259 * title->start_byte,
1260 * title->end_byte);fflush(stdout);
1264 if(demuxer
->current_title
!= last_title
)
1266 mpeg3demux_open_title(demuxer
, demuxer
->current_title
);
1269 title
= demuxer
->titles
[demuxer
->current_title
];
1270 if(demuxer
->absolute_byte
!= title
->start_byte
+ mpeg3io_tell(title
->fs
))
1272 mpeg3io_seek(title
->fs
, demuxer
->absolute_byte
- title
->start_byte
);
1275 //printf("mpeg3_advance_cell 40\n", demuxer->absolute_byte);
1285 static int next_code(mpeg3_demuxer_t
*demuxer
,
1288 uint32_t result
= 0;
1290 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1291 mpeg3_fs_t
*fd
= title
->fs
;
1293 while(result
!= code
&&
1296 title
= demuxer
->titles
[demuxer
->current_title
];
1298 result
|= (unsigned char)mpeg3io_read_char(title
->fs
);
1299 demuxer
->absolute_byte
++;
1300 error
= mpeg3_advance_cell(demuxer
);
1314 /* Read packet in the forward direction */
1315 int mpeg3_read_next_packet(mpeg3_demuxer_t
*demuxer
)
1318 long current_position
;
1319 mpeg3_t
*file
= demuxer
->file
;
1320 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1322 * printf("mpeg3_read_next_packet 1 %d %llx\n", result, demuxer->absolute_byte);
1326 /* Reset output buffer */
1327 demuxer
->data_size
= 0;
1328 demuxer
->data_position
= 0;
1330 /* Switch to forward direction. */
1331 if(demuxer
->reverse
)
1333 /* Don't reread anything if we're at the beginning of the file. */
1334 if(demuxer
->absolute_byte
< 0)
1336 demuxer
->absolute_byte
= 0;
1337 result
= mpeg3_advance_cell(demuxer
);
1338 /* First character was the -1 byte which brings us to 0 after this function. */
1342 /* Transport or elementary stream */
1343 if(file
->packet_size
> 0)
1345 demuxer
->absolute_byte
+= file
->packet_size
;
1346 result
= mpeg3_advance_cell(demuxer
);
1350 /* Packet just read */
1351 if(!result
) result
= next_code(demuxer
,
1352 MPEG3_PACK_START_CODE
);
1354 if(!result
) result
= next_code(demuxer
,
1355 MPEG3_PACK_START_CODE
);
1358 demuxer
->reverse
= 0;
1366 //printf("mpeg3_read_next_packet 2 %llx\n", demuxer->absolute_byte);
1368 /* Read packets until the output buffer is full */
1373 title
= demuxer
->titles
[demuxer
->current_title
];
1377 if(file
->is_transport_stream
)
1379 result
= read_transport(demuxer
);
1380 if(!result
) result
= mpeg3_advance_cell(demuxer
);
1383 if(file
->is_program_stream
)
1385 result
= mpeg3demux_read_program(demuxer
);
1386 if(!result
) result
= mpeg3_advance_cell(demuxer
);
1390 /* Read elementary stream. */
1391 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1392 file
->packet_size
, title
->fs
);
1395 demuxer
->data_size
= file
->packet_size
;
1396 demuxer
->absolute_byte
+= file
->packet_size
;
1397 result
= mpeg3_advance_cell(demuxer
);
1402 demuxer
->data_size
== 0 &&
1403 (demuxer
->do_audio
|| demuxer
->do_video
));
1405 //printf("mpeg3_read_next_packet 3\n");
1408 * printf("mpeg3_read_next_packet 2 %d %llx\n", result, demuxer->absolute_byte);
1417 static int prev_code(mpeg3_demuxer_t
*demuxer
,
1420 uint32_t result
= 0;
1422 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1423 mpeg3_fs_t
*fd
= title
->fs
;
1426 while(result
!= code
&&
1427 demuxer
->absolute_byte
> 0 &&
1431 title
= demuxer
->titles
[demuxer
->current_title
];
1432 mpeg3io_seek(title
->fs
, demuxer
->absolute_byte
- title
->start_byte
- 1LL);
1433 //printf("1 %llx %llx\n", demuxer->absolute_byte, title->start_byte);fflush(stdout);
1434 result
|= ((uint32_t)mpeg3io_read_char(title
->fs
)) << 24;
1435 //printf("2\n");fflush(stdout);
1436 demuxer
->absolute_byte
--;
1437 error
= mpeg3_advance_cell(demuxer
);
1446 /* Read the packet right before the packet we're currently on. */
1447 int mpeg3_read_prev_packet(mpeg3_demuxer_t
*demuxer
)
1450 mpeg3_t
*file
= demuxer
->file
;
1451 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1453 demuxer
->data_size
= 0;
1454 demuxer
->data_position
= 0;
1457 * printf("mpeg3_read_prev_packet 1 %d %d %d %d %llx\n",
1458 * demuxer->total_titles,
1459 * demuxer->current_title,
1460 * title->cell_table_size,
1461 * demuxer->title_cell,
1462 * demuxer->absolute_byte);fflush(stdout);
1466 //printf("mpeg3_read_prev_packet 1 %d %d %llx\n", result, demuxer->current_title, mpeg3io_tell(title->fs));
1467 /* Switch to reverse direction */
1468 if(!demuxer
->reverse
)
1470 demuxer
->reverse
= 1;
1472 /* Transport stream or elementary stream case */
1473 if(file
->packet_size
> 0)
1475 demuxer
->absolute_byte
-= file
->packet_size
;
1476 result
= mpeg3_advance_cell(demuxer
);
1479 /* Program stream */
1481 //printf("mpeg3_read_prev_packet 1 %d %llx\n", demuxer->current_title, demuxer->absolute_byte);
1482 result
= prev_code(demuxer
,
1483 MPEG3_PACK_START_CODE
);
1484 //printf("mpeg3_read_prev_packet 2 %d %llx\n", demuxer->current_title, demuxer->absolute_byte);
1495 /* Go to beginning of previous packet */
1498 title
= demuxer
->titles
[demuxer
->current_title
];
1501 * printf("mpeg3_read_prev_packet 2 %d %llx %llx\n",
1502 * demuxer->current_title,
1503 * demuxer->absolute_byte,
1504 * title->start_byte);
1507 /* Transport stream or elementary stream case */
1508 if(file
->packet_size
> 0)
1510 demuxer
->absolute_byte
-= file
->packet_size
;
1511 result
= mpeg3_advance_cell(demuxer
);
1515 if(!result
) result
= prev_code(demuxer
,
1516 MPEG3_PACK_START_CODE
);
1519 //printf("mpeg3_read_prev_packet 3 %d %llx\n", demuxer->current_title, demuxer->absolute_byte);
1522 /* Read packet and then rewind it */
1523 title
= demuxer
->titles
[demuxer
->current_title
];
1524 if(file
->is_transport_stream
&& !result
)
1526 result
= read_transport(demuxer
);
1528 if(demuxer
->absolute_byte
> 0)
1530 demuxer
->absolute_byte
-= file
->packet_size
;
1531 result
= mpeg3_advance_cell(demuxer
);
1535 if(file
->is_program_stream
&& !result
)
1537 int64_t current_position
= demuxer
->absolute_byte
;
1540 result
= mpeg3demux_read_program(demuxer
);
1542 //printf("mpeg3_read_prev_packet 5 %d %llx\n", demuxer->current_title, demuxer->absolute_byte);
1544 while(demuxer
->absolute_byte
> current_position
&&
1547 result
= prev_code(demuxer
,
1548 MPEG3_PACK_START_CODE
);
1550 //printf("mpeg3_read_prev_packet 6 %d %llx\n", demuxer->current_title, demuxer->absolute_byte);
1555 /* Elementary stream */
1556 /* Read the packet forwards and seek back to the start */
1557 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1563 demuxer
->data_size
= file
->packet_size
;
1564 result
= mpeg3io_seek(title
->fs
, demuxer
->absolute_byte
);
1567 //printf("mpeg3_read_prev_packet 4 %d %llx\n", demuxer->current_title, demuxer->absolute_byte);
1569 demuxer
->data_size
== 0 &&
1570 (demuxer
->do_audio
|| demuxer
->do_video
));
1573 //printf("mpeg3_read_prev_packet 5 %d %d %llx\n", result, demuxer->current_title, demuxer->absolute_byte);fflush(stdout);
1579 int mpeg3demux_read_data(mpeg3_demuxer_t
*demuxer
,
1580 unsigned char *output
,
1584 demuxer
->error_flag
= 0;
1586 //printf("mpeg3demux_read_data 1 %d\n", demuxer->data_position);
1587 if(demuxer
->data_position
>= 0)
1591 for(i
= 0; i
< size
&& !result
; )
1593 int fragment_size
= size
- i
;
1594 if(fragment_size
> demuxer
->data_size
- demuxer
->data_position
)
1595 fragment_size
= demuxer
->data_size
- demuxer
->data_position
;
1596 memcpy(output
+ i
, demuxer
->data_buffer
+ demuxer
->data_position
, fragment_size
);
1597 demuxer
->data_position
+= fragment_size
;
1602 result
= mpeg3_read_next_packet(demuxer
);
1608 int64_t current_position
;
1609 /* Read backwards a full packet. */
1610 /* Only good for reading less than the size of a full packet, but */
1611 /* this routine should only be used for searching for previous markers. */
1612 current_position
= demuxer
->data_position
;
1613 result
= mpeg3_read_prev_packet(demuxer
);
1614 if(!result
) demuxer
->data_position
= demuxer
->data_size
+ current_position
;
1615 memcpy(output
, demuxer
->data_buffer
+ demuxer
->data_position
, size
);
1616 demuxer
->data_position
+= size
;
1619 //printf("mpeg3demux_read_data 2\n");
1620 demuxer
->error_flag
= result
;
1624 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t
*demuxer
)
1626 demuxer
->error_flag
= 0;
1628 if(demuxer
->data_position
>= demuxer
->data_size
)
1629 demuxer
->error_flag
= mpeg3_read_next_packet(demuxer
);
1630 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
++];
1632 return demuxer
->next_char
;
1635 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t
*demuxer
)
1637 demuxer
->error_flag
= 0;
1638 demuxer
->data_position
--;
1639 if(demuxer
->data_position
< 0)
1641 demuxer
->error_flag
= mpeg3_read_prev_packet(demuxer
);
1642 if(!demuxer
->error_flag
) demuxer
->data_position
= demuxer
->data_size
- 1;
1644 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
];
1645 return demuxer
->next_char
;
1650 int mpeg3demux_open_title(mpeg3_demuxer_t
*demuxer
, int title_number
)
1652 mpeg3_title_t
*title
;
1654 if(title_number
< demuxer
->total_titles
)
1656 if(demuxer
->current_title
>= 0)
1658 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
1659 demuxer
->current_title
= -1;
1662 title
= demuxer
->titles
[title_number
];
1664 if(mpeg3io_open_file(title
->fs
))
1666 demuxer
->error_flag
= 1;
1667 fprintf(stderr
, "mpeg3demux_open_title %s: %s", title
->fs
->path
, strerror(errno
));
1671 demuxer
->current_title
= title_number
;
1676 return demuxer
->error_flag
;
1679 /* Assign program numbers to interleaved programs */
1680 int mpeg3demux_assign_programs(mpeg3_demuxer_t
*demuxer
)
1682 int current_program
= 0;
1683 int current_title
= 0;
1685 double current_time
;
1686 mpeg3demux_cell_t
*cell
;
1687 int total_programs
= 1;
1689 int program_exists
, last_program_assigned
= 0;
1691 mpeg3_title_t
**titles
= demuxer
->titles
;
1693 for(i
= 0, total_cells
= 0; i
< demuxer
->total_titles
; i
++)
1695 total_cells
+= demuxer
->titles
[i
]->cell_table_size
;
1696 for(j
= 0; j
< demuxer
->titles
[i
]->cell_table_size
; j
++)
1698 cell
= &demuxer
->titles
[i
]->cell_table
[j
];
1699 if(cell
->program
> total_programs
- 1)
1700 total_programs
= cell
->program
+ 1;
1706 demuxer
->current_program
= 0;
1710 int mpeg3demux_copy_titles(mpeg3_demuxer_t
*dst
, mpeg3_demuxer_t
*src
)
1713 mpeg3_t
*file
= dst
->file
;
1714 mpeg3_title_t
*dst_title
, *src_title
;
1716 dst
->total_titles
= src
->total_titles
;
1717 dst
->total_programs
= src
->total_programs
;
1718 for(i
= 0; i
< MPEG3_MAX_STREAMS
; i
++)
1720 dst
->astream_table
[i
] = src
->astream_table
[i
];
1721 dst
->vstream_table
[i
] = src
->vstream_table
[i
];
1723 for(i
= 0; i
< src
->total_titles
; i
++)
1725 src_title
= src
->titles
[i
];
1726 dst_title
= dst
->titles
[i
] = mpeg3_new_title(file
, src
->titles
[i
]->fs
->path
);
1727 mpeg3_copy_title(dst_title
, src_title
);
1730 mpeg3demux_open_title(dst
, src
->current_title
);
1731 dst
->title_cell
= 0;
1735 /* ==================================================================== */
1737 /* ==================================================================== */
1739 mpeg3_demuxer_t
* mpeg3_new_demuxer(mpeg3_t
*file
, int do_audio
, int do_video
, int stream_id
)
1741 mpeg3_demuxer_t
*demuxer
= calloc(1, sizeof(mpeg3_demuxer_t
));
1744 /* The demuxer will change the default packet size for its own use. */
1745 demuxer
->file
= file
;
1746 demuxer
->do_audio
= do_audio
;
1747 demuxer
->do_video
= do_video
;
1749 /* Allocate buffer + padding */
1750 demuxer
->raw_data
= calloc(1, MPEG3_RAW_SIZE
);
1751 demuxer
->data_buffer
= calloc(1, MPEG3_RAW_SIZE
);
1752 /* System specific variables */
1753 demuxer
->audio_pid
= stream_id
;
1754 demuxer
->video_pid
= stream_id
;
1755 demuxer
->astream
= stream_id
;
1756 demuxer
->vstream
= stream_id
;
1757 demuxer
->current_title
= -1;
1758 demuxer
->pes_audio_time
= -1;
1759 demuxer
->pes_video_time
= -1;
1760 //printf("mpeg3_new_demuxer %f\n", demuxer->time);
1766 int mpeg3_delete_demuxer(mpeg3_demuxer_t
*demuxer
)
1770 if(demuxer
->current_title
>= 0)
1772 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
1775 for(i
= 0; i
< demuxer
->total_titles
; i
++)
1777 mpeg3_delete_title(demuxer
->titles
[i
]);
1780 free(demuxer
->data_buffer
);
1781 free(demuxer
->raw_data
);
1787 int mpeg3demux_eof(mpeg3_demuxer_t
*demuxer
)
1789 if(demuxer
->current_title
>= 0)
1791 if(mpeg3io_eof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
1792 demuxer
->current_title
>= demuxer
->total_titles
- 1)
1799 int mpeg3demux_bof(mpeg3_demuxer_t
*demuxer
)
1801 if(demuxer
->current_title
>= 0)
1803 if(mpeg3io_bof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
1804 demuxer
->current_title
<= 0)
1810 void mpeg3demux_start_reverse(mpeg3_demuxer_t
*demuxer
)
1812 demuxer
->reverse
= 1;
1815 void mpeg3demux_start_forward(mpeg3_demuxer_t
*demuxer
)
1817 demuxer
->reverse
= 0;
1820 /* Seek to absolute byte */
1821 int mpeg3demux_seek_byte(mpeg3_demuxer_t
*demuxer
, int64_t byte
)
1823 mpeg3_t
*file
= demuxer
->file
;
1827 * for(i = 0; i < demuxer->total_titles; i++) mpeg3_dump_title(demuxer->titles[i]);
1830 demuxer
->absolute_byte
= byte
;
1831 demuxer
->data_position
= 0;
1832 demuxer
->data_size
= 0;
1834 /* Get on a packet boundary only for transport streams. */
1835 if(file
->is_transport_stream
&&
1838 demuxer
->absolute_byte
-= demuxer
->absolute_byte
% file
->packet_size
;
1841 int result
= mpeg3_advance_cell(demuxer
);
1854 int64_t current_position
;
1855 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1857 demuxer
->data_position
= 0;
1858 demuxer
->data_size
= 0;
1859 demuxer
->error_flag
= mpeg3io_seek(title
->fs
, byte
);
1861 if(!demuxer
->error_flag
&&
1862 file
->is_transport_stream
&&
1865 /* Get on a packet boundary only for transport streams. */
1866 current_position
= mpeg3io_tell(title
->fs
);
1867 if(byte
% file
->packet_size
)
1869 demuxer
->error_flag
|= mpeg3io_seek(title
->fs
,
1871 (current_position
% file
->packet_size
));
1875 demuxer
->absolute_byte
= title
->start_byte
+ mpeg3io_tell(title
->fs
);
1878 for(demuxer
->title_cell
= 0;
1879 demuxer
->title_cell
< title
->cell_table_size
;
1880 demuxer
->title_cell
++)
1882 if(title
->cell_table
[demuxer
->title_cell
].start_byte
<= byte
&&
1883 title
->cell_table
[demuxer
->title_cell
].end_byte
> byte
)
1889 if(demuxer
->title_cell
>= title
->cell_table_size
)
1890 demuxer
->title_cell
= title
->cell_table_size
- 1;
1893 return demuxer
->error_flag
;
1903 double mpeg3demux_get_time(mpeg3_demuxer_t
*demuxer
)
1905 return demuxer
->time
;
1908 double mpeg3demux_audio_pts(mpeg3_demuxer_t
*demuxer
)
1910 return demuxer
->pes_audio_time
;
1913 double mpeg3demux_video_pts(mpeg3_demuxer_t
*demuxer
)
1915 return demuxer
->pes_video_time
;
1918 void mpeg3demux_reset_pts(mpeg3_demuxer_t
*demuxer
)
1920 demuxer
->pes_audio_time
= -1;
1921 demuxer
->pes_video_time
= -1;
1924 double mpeg3demux_scan_pts(mpeg3_demuxer_t
*demuxer
)
1926 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
1927 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
1928 int64_t current_position
= start_position
;
1931 mpeg3demux_reset_pts(demuxer
);
1933 current_position
< end_position
&&
1934 ((demuxer
->do_audio
&& demuxer
->pes_audio_time
< 0) ||
1935 (demuxer
->do_video
&& demuxer
->pes_video_time
< 0)))
1937 result
= mpeg3_read_next_packet(demuxer
);
1938 current_position
= mpeg3demux_tell_byte(demuxer
);
1941 // Seek back to starting point
1942 mpeg3demux_seek_byte(demuxer
, start_position
);
1944 if(demuxer
->do_audio
) return demuxer
->pes_audio_time
;
1945 if(demuxer
->do_video
) return demuxer
->pes_video_time
;
1948 int mpeg3demux_goto_pts(mpeg3_demuxer_t
*demuxer
, double pts
)
1950 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
1951 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
1952 int64_t current_position
= start_position
;
1955 // Search forward for nearest pts
1956 mpeg3demux_reset_pts(demuxer
);
1957 while(!result
&& current_position
< end_position
)
1959 result
= mpeg3_read_next_packet(demuxer
);
1960 if(demuxer
->pes_audio_time
> pts
) break;
1961 current_position
= mpeg3demux_tell_byte(demuxer
);
1964 // Search backward for nearest pts
1965 end_position
= current_position
- MPEG3_PTS_RANGE
;
1966 mpeg3_read_prev_packet(demuxer
);
1968 current_position
> end_position
)
1970 result
= mpeg3_read_prev_packet(demuxer
);
1971 if(demuxer
->pes_audio_time
< pts
) break;
1972 current_position
= mpeg3demux_tell_byte(demuxer
);
1977 int64_t mpeg3demux_tell_byte(mpeg3_demuxer_t
*demuxer
)
1979 return demuxer
->absolute_byte
;
1985 for(i
= 0; i
< demuxer
->current_title
; i
++)
1987 result
+= demuxer
->titles
[i
]->total_bytes
;
1989 result
+= mpeg3io_tell(demuxer
->titles
[demuxer
->current_title
]->fs
);
1994 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t
*demuxer
)
1998 for(i
= 0; i
< demuxer
->total_titles
; i
++)
1999 result
+= demuxer
->titles
[i
]->total_bytes
;
2003 int64_t mpeg3demuxer_title_bytes(mpeg3_demuxer_t
*demuxer
)
2005 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2006 return title
->total_bytes
;
2009 mpeg3_demuxer_t
* mpeg3_get_demuxer(mpeg3_t
*file
)
2011 if(file
->is_program_stream
|| file
->is_transport_stream
)
2013 if(file
->total_astreams
) return file
->atrack
[0]->demuxer
;
2015 if(file
->total_vstreams
) return file
->vtrack
[0]->demuxer
;