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 fprintf(stderr
, "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;
161 //printf("get_transport_payload 2 %d %d %d\n", bytes, demuxer->read_all, is_audio);
163 if(demuxer
->read_all
&& is_audio
)
166 * if(demuxer->pid == 0x1100)
167 * printf("get_transport_payload 1 0x%x %d\n", demuxer->audio_pid, bytes);
169 memcpy(demuxer
->audio_buffer
+ demuxer
->audio_size
,
170 demuxer
->raw_data
+ demuxer
->raw_offset
,
172 demuxer
->audio_size
+= bytes
;
175 if(demuxer
->read_all
&& is_video
)
177 //printf("get_transport_payload 2\n");
178 memcpy(demuxer
->video_buffer
+ demuxer
->video_size
,
179 demuxer
->raw_data
+ demuxer
->raw_offset
,
181 demuxer
->video_size
+= bytes
;
185 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
186 demuxer
->raw_data
+ demuxer
->raw_offset
,
188 demuxer
->data_size
+= bytes
;
190 * printf("get_transport_payload 10 pid=0x%x bytes=0x%x data_size=0x%x\n",
193 * demuxer->data_size);
197 demuxer
->raw_offset
+= bytes
;
201 static int get_pes_packet_header(mpeg3_demuxer_t
*demuxer
,
205 unsigned int pes_header_bytes
= 0;
206 unsigned int pts_dts_flags
;
207 int pes_header_data_length
;
209 /* drop first 8 bits */
210 packet_read_char(demuxer
);
211 pts_dts_flags
= (packet_read_char(demuxer
) >> 6) & 0x3;
212 pes_header_data_length
= packet_read_char(demuxer
);
215 /* Get Presentation Time stamps and Decoding Time Stamps */
216 if(pts_dts_flags
== 2)
218 *pts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
220 *pts
|= (packet_read_int16(demuxer
) >> 1);
222 *pts
|= (packet_read_int16(demuxer
) >> 1);
223 pes_header_bytes
+= 5;
226 if(pts_dts_flags
== 3)
228 *pts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
230 *pts
|= (packet_read_int16(demuxer
) >> 1);
232 *pts
|= (packet_read_int16(demuxer
) >> 1);
233 *dts
= (packet_read_char(demuxer
) >> 1) & 7; /* Only low 4 bits (7==1111) */
235 *dts
|= (packet_read_int16(demuxer
) >> 1);
237 *dts
|= (packet_read_int16(demuxer
) >> 1);
238 pes_header_bytes
+= 10;
241 demuxer
->time
= (double)*pts
/ 90000;
245 printf(" pts_dts_flags=0x%02x pts=%f dts=%f\n",
247 (double)*pts
/ 90000,
248 (double)*dts
/ 90000);
252 /* extract other stuff here! */
253 packet_skip(demuxer
, pes_header_data_length
- pes_header_bytes
);
257 static int get_unknown_data(mpeg3_demuxer_t
*demuxer
)
259 int bytes
= demuxer
->raw_size
- demuxer
->raw_offset
;
260 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
261 demuxer
->raw_data
+ demuxer
->raw_offset
,
263 demuxer
->data_size
+= bytes
;
264 demuxer
->raw_offset
+= bytes
;
270 static int get_transport_pes_packet(mpeg3_demuxer_t
*demuxer
)
272 unsigned int pts
= 0, dts
= 0;
273 get_pes_packet_header(demuxer
, &pts
, &dts
);
276 if(demuxer
->stream_id
== 0xbd ||
278 demuxer
->stream_id
== 0xfd)
280 //printf("get_transport_pes_packet %x\n", demuxer->pid);
282 demuxer
->stream_id
= 0x0;
283 demuxer
->got_audio
= 1;
284 demuxer
->custom_id
= demuxer
->pid
;
286 if(demuxer
->read_all
)
287 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_AC3
;
288 if(demuxer
->astream
== -1)
289 demuxer
->astream
= demuxer
->custom_id
;
293 printf("get_transport_pes_packet: offset=%llx 0x%x bytes AC3 custom_id=0x%x astream=0x%x do_audio=%d\n",
294 mpeg3io_tell(demuxer
->titles
[demuxer
->current_title
]->fs
),
295 demuxer
->raw_size
- demuxer
->raw_offset
,
301 if((demuxer
->custom_id
== demuxer
->astream
&&
302 demuxer
->do_audio
) ||
305 demuxer
->pes_audio_time
= (double)pts
/ 90000;
306 demuxer
->audio_pid
= demuxer
->pid
;
307 return get_transport_payload(demuxer
, 1, 0);
312 if((demuxer
->stream_id
>> 4) == 12 || (demuxer
->stream_id
>> 4) == 13)
315 demuxer
->custom_id
= demuxer
->pid
;
316 demuxer
->got_audio
= 1;
318 /* Just pick the first available stream if no ID is set */
319 if(demuxer
->read_all
)
320 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_MPEG
;
321 if(demuxer
->astream
== -1)
322 demuxer
->astream
= demuxer
->custom_id
;
326 printf(" 0x%x bytes MP2 audio\n", demuxer
->raw_size
- demuxer
->raw_offset
);
329 if((demuxer
->custom_id
== demuxer
->astream
&&
330 demuxer
->do_audio
) ||
333 demuxer
->pes_audio_time
= (double)pts
/ 90000;
334 demuxer
->audio_pid
= demuxer
->pid
;
336 return get_transport_payload(demuxer
, 1, 0);
340 if((demuxer
->stream_id
>> 4) == 14)
343 demuxer
->custom_id
= demuxer
->pid
;
344 demuxer
->got_video
= 1;
347 /* Just pick the first available stream if no ID is set */
348 if(demuxer
->read_all
)
349 demuxer
->vstream_table
[demuxer
->custom_id
] = 1;
351 if(demuxer
->vstream
== -1)
352 demuxer
->vstream
= demuxer
->custom_id
;
357 printf(" 0x%x bytes video data\n", demuxer
->raw_size
- demuxer
->raw_offset
);
360 if((demuxer
->custom_id
== demuxer
->vstream
&&
361 demuxer
->do_video
) ||
364 demuxer
->pes_video_time
= (double)pts
/ 90000;
365 demuxer
->video_pid
= demuxer
->pid
;
368 return get_transport_payload(demuxer
, 0, 1);
372 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
377 static int get_pes_packet(mpeg3_demuxer_t
*demuxer
)
379 demuxer
->pes_packets
++;
384 packet_read_int24(demuxer
);
385 demuxer
->stream_id
= packet_read_char(demuxer
);
390 printf(" stream_id=0x%02x\n", demuxer
->stream_id
);
393 /* Skip pes packet length */
394 packet_read_int16(demuxer
);
396 if(demuxer
->stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
397 demuxer
->stream_id
!= MPEG3_PADDING_STREAM
)
399 return get_transport_pes_packet(demuxer
);
402 if(demuxer
->stream_id
== MPEG3_PRIVATE_STREAM_2
)
404 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
408 if(demuxer
->stream_id
== MPEG3_PADDING_STREAM
)
410 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
415 fprintf(stderr
, "unknown stream_id in pes packet");
421 static int get_payload(mpeg3_demuxer_t
*demuxer
)
423 //printf("get_payload 1 pid=0x%x unit_start=%d\n", demuxer->pid, demuxer->payload_unit_start_indicator);
424 if(demuxer
->payload_unit_start_indicator
)
426 if(demuxer
->pid
== 0)
427 get_program_association_table(demuxer
);
429 if(packet_next_int24(demuxer
) == MPEG3_PACKET_START_CODE_PREFIX
)
430 get_pes_packet(demuxer
);
432 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
438 printf(" 0x%x bytes elementary data\n", demuxer
->raw_size
- demuxer
->raw_offset
);
442 * if(demuxer->pid == 0x1100) printf("get_payload 1 0x%x\n", demuxer->audio_pid);
444 if(demuxer
->pid
== demuxer
->audio_pid
&&
445 (demuxer
->do_audio
||
448 if(demuxer
->do_audio
) demuxer
->got_audio
= 1;
452 printf("get_payload: offset=%llx 0x%x bytes AC3 pid=0x%x\n",
453 mpeg3io_tell(demuxer
->titles
[demuxer
->current_title
]->fs
),
454 demuxer
->raw_size
- demuxer
->raw_offset
,
458 get_transport_payload(demuxer
, 1, 0);
461 if(demuxer
->pid
== demuxer
->video_pid
&&
462 (demuxer
->do_video
||
465 if(demuxer
->do_video
) demuxer
->got_video
= 1;
466 get_transport_payload(demuxer
, 0, 1);
469 if(demuxer
->read_all
)
471 get_transport_payload(demuxer
, 0, 0);
472 // packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
478 /* Read a transport packet */
479 static int read_transport(mpeg3_demuxer_t
*demuxer
)
482 mpeg3_t
*file
= (mpeg3_t
*)demuxer
->file
;
483 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
488 /* Packet size is known for transport streams */
489 demuxer
->raw_size
= file
->packet_size
;
490 demuxer
->raw_offset
= 0;
491 demuxer
->stream_id
= 0;
492 demuxer
->got_audio
= 0;
493 demuxer
->got_video
= 0;
494 demuxer
->custom_id
= -1;
499 perror("read_transport");
503 //fprintf(stderr, "read transport 1 %llx %llx\n", title->fs->current_byte, title->fs->total_bytes);
507 mpeg3io_read_int32(title
->fs
);
509 // Search for Sync byte */
512 bits
= mpeg3io_read_char(title
->fs
);
513 }while(!mpeg3io_eof(title
->fs
) && !result
&& bits
!= MPEG3_SYNC_BYTE
);
516 if(mpeg3io_eof(title
->fs
) || result
) return 1;
518 if(bits
== MPEG3_SYNC_BYTE
&& !result
)
520 // Store byte just read as first byte
521 demuxer
->raw_data
[0] = MPEG3_SYNC_BYTE
;
524 int fragment_size
= file
->packet_size
- 1;
529 demuxer
->raw_size
-= 4;
531 result
= mpeg3io_read_data(demuxer
->raw_data
+ 1,
538 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
545 packet_read_char(demuxer
);
546 bits
= packet_read_int24(demuxer
) & 0x00ffffff;
547 demuxer
->transport_error_indicator
= (bits
>> 23) & 0x1;
548 demuxer
->payload_unit_start_indicator
= (bits
>> 22) & 0x1;
549 demuxer
->pid
= demuxer
->custom_id
= (bits
>> 8) & 0x00001fff;
551 demuxer
->transport_scrambling_control
= (bits
>> 6) & 0x3;
552 demuxer
->adaptation_field_control
= (bits
>> 4) & 0x3;
553 demuxer
->continuity_counter
= bits
& 0xf;
556 // This caused an audio track to not be created.
557 if(demuxer
->transport_error_indicator
)
559 // fprintf(stderr, "demuxer->transport_error_indicator at %llx\n",
560 // mpeg3io_tell(title->fs));
561 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
566 if (demuxer
->pid
== 0x1fff)
568 demuxer
->is_padding
= 1; /* padding; just go to next */
572 demuxer
->is_padding
= 0;
575 /* Get pid from table */
576 for(table_entry
= 0, result
= 0;
577 table_entry
< demuxer
->total_pids
;
580 if(demuxer
->pid
== demuxer
->pid_table
[table_entry
])
590 /* Not in pid table */
591 if(!result
&& demuxer
->total_pids
< MPEG3_PIDMAX
)
593 demuxer
->pid_table
[table_entry
] = demuxer
->pid
;
594 demuxer
->continuity_counters
[table_entry
] = demuxer
->continuity_counter
; /* init */
595 demuxer
->total_pids
++;
601 printf("offset=0x%llx pid=0x%02x continuity=0x%02x padding=%d adaptation=%d unit_start=%d\n",
602 demuxer
->program_byte
,
604 demuxer
->continuity_counter
,
606 demuxer
->adaptation_field_control
,
607 demuxer
->payload_unit_start_indicator
);
611 /* Abort if padding. Should abort after demuxer->pid == 0x1fff for speed. */
612 if(demuxer
->is_padding
)
614 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
622 if(demuxer
->adaptation_field_control
& 0x2)
623 result
= get_adaptation_field(demuxer
);
625 // Need to enter in astream and vstream table:
626 // PID ored with stream_id
627 if(demuxer
->adaptation_field_control
& 0x1)
628 result
= get_payload(demuxer
);
630 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
635 static int get_system_header(mpeg3_demuxer_t
*demuxer
)
637 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
638 int length
= mpeg3io_read_int16(title
->fs
);
639 mpeg3io_seek_relative(title
->fs
, length
);
643 static unsigned int get_timestamp(mpeg3_demuxer_t
*demuxer
)
645 unsigned int timestamp
;
646 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
648 /* Only low 4 bits (7==1111) */
649 timestamp
= (mpeg3io_read_char(title
->fs
) >> 1) & 7;
651 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
653 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
657 static int get_pack_header(mpeg3_demuxer_t
*demuxer
)
660 unsigned int clock_ref
, clock_ref_ext
;
661 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
663 /* Get the time code */
664 if((mpeg3io_next_char(title
->fs
) >> 4) == 2)
667 demuxer
->time
= (double)get_timestamp(demuxer
) / 90000;
669 mpeg3io_read_int24(title
->fs
);
672 if(mpeg3io_next_char(title
->fs
) & 0x40)
674 i
= mpeg3io_read_int32(title
->fs
);
675 j
= mpeg3io_read_int16(title
->fs
);
677 if(i
& 0x40000000 || (i
>> 28) == 2)
679 clock_ref
= ((i
& 0x38000000) << 3);
680 clock_ref
|= ((i
& 0x03fff800) << 4);
681 clock_ref
|= ((i
& 0x000003ff) << 5);
682 clock_ref
|= ((j
& 0xf800) >> 11);
683 clock_ref_ext
= (j
>> 1) & 0x1ff;
685 demuxer
->time
= (double)(clock_ref
+ clock_ref_ext
/ 300) / 90000;
687 mpeg3io_read_int24(title
->fs
);
688 i
= mpeg3io_read_char(title
->fs
) & 0x7;
691 mpeg3io_seek_relative(title
->fs
, i
);
696 mpeg3io_seek_relative(title
->fs
, 2);
703 static int get_program_payload(mpeg3_demuxer_t
*demuxer
,
708 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
710 if(demuxer
->read_all
&& is_audio
)
712 mpeg3io_read_data(demuxer
->audio_buffer
+ demuxer
->audio_size
,
715 demuxer
->audio_size
+= bytes
;
718 if(demuxer
->read_all
&& is_video
)
720 mpeg3io_read_data(demuxer
->video_buffer
+ demuxer
->video_size
,
723 demuxer
->video_size
+= bytes
;
727 mpeg3io_read_data(demuxer
->data_buffer
+ demuxer
->data_size
,
730 demuxer
->data_size
+= bytes
;
742 static int handle_scrambling(mpeg3_demuxer_t
*demuxer
,
743 int decryption_offset
)
745 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
747 // Advance 2048 bytes if scrambled. We might pick up a spurrius
748 // packet start code in the scrambled data otherwise.
749 if(demuxer
->last_packet_start
+ 0x800 > mpeg3io_tell(title
->fs
))
751 mpeg3io_seek_relative(title
->fs
,
752 demuxer
->last_packet_start
+ 0x800 - mpeg3io_tell(title
->fs
));
757 // Descramble if desired.
758 if(demuxer
->data_size
||
759 demuxer
->audio_size
||
762 unsigned char *buffer_ptr
= 0;
763 if(demuxer
->data_size
) buffer_ptr
= demuxer
->data_buffer
;
765 if(demuxer
->audio_size
) buffer_ptr
= demuxer
->audio_buffer
;
767 if(demuxer
->video_size
) buffer_ptr
= demuxer
->video_buffer
;
770 //printf(__FUNCTION__ " data_size=%x decryption_offset=%x\n", demuxer->data_size, decryption_offset);
771 if(mpeg3_decrypt_packet(title
->fs
->css
,
775 fprintf(stderr
, "handle_scrambling: Decryption not available\n");
783 static void remove_subtitle(mpeg3_demuxer_t
*demuxer
, int number
)
786 mpeg3_delete_subtitle(demuxer
->subtitles
[number
]);
787 for(j
= number
; j
< demuxer
->total_subtitles
- 1; j
++)
789 demuxer
->subtitles
[j
] = demuxer
->subtitles
[j
+ 1];
791 demuxer
->total_subtitles
--;
794 static void remove_subtitle_ptr(mpeg3_demuxer_t
*demuxer
, mpeg3_subtitle_t
*ptr
)
796 int i
, number
= demuxer
->total_subtitles
;
797 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
799 if(demuxer
->subtitles
[i
] == ptr
)
806 for(i
= number
; i
< demuxer
->total_subtitles
- 1; i
++)
808 demuxer
->subtitles
[i
] = demuxer
->subtitles
[i
+ 1];
810 demuxer
->total_subtitles
--;
813 /* Create new subtitle object if none with the same id is in the table */
814 static mpeg3_subtitle_t
* new_subtitle(mpeg3_demuxer_t
*demuxer
,
818 mpeg3_subtitle_t
*subtitle
= 0;
821 /* Get subtitle object for the stream and total subtitle objects for the stream */
824 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
826 if(demuxer
->subtitles
[i
]->id
== id
) total
++;
828 if(demuxer
->subtitles
[i
]->id
== id
&&
829 !demuxer
->subtitles
[i
]->done
&&
832 subtitle
= demuxer
->subtitles
[i
];
837 /* Make new subtitle object */
840 demuxer
->total_subtitles
++;
841 demuxer
->subtitles
= realloc(demuxer
->subtitles
,
842 sizeof(mpeg3_subtitle_t
*) * demuxer
->total_subtitles
);
843 subtitle
= demuxer
->subtitles
[demuxer
->total_subtitles
- 1] =
844 calloc(sizeof(mpeg3_subtitle_t
), 1);
846 subtitle
->offset
= offset
;
849 /* Delete oldest subtitle */
850 if(total
> MPEG3_MAX_SUBTITLES
)
852 for(i
= 0; i
< demuxer
->total_subtitles
- 1; i
++)
854 if(demuxer
->subtitles
[i
]->id
== id
)
856 remove_subtitle(demuxer
, i
);
866 static void handle_subtitle(mpeg3_t
*file
,
867 mpeg3_demuxer_t
*demuxer
,
868 mpeg3_subtitle_t
*subtitle
,
871 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
874 /* Allocate memory */
877 subtitle
->data
= malloc(bytes
);
881 subtitle
->data
= realloc(subtitle
->data
,
882 subtitle
->size
+ bytes
);
885 /* Add data to latest subtitle packet. */
886 mpeg3io_read_data(subtitle
->data
+ subtitle
->size
,
891 * static FILE *test = 0;
892 * if(!test) test = fopen("/tmp/debug", "w");
893 * if(stream_id == 0x20) fwrite(subtitle->data + subtitle->size, pes_packet_length, 1, test);
896 subtitle
->size
+= bytes
;
898 /* Assume complete subtitle object has been loaded and align it in the buffer */
899 for(i
= 0; i
< subtitle
->size
- 1; i
++)
901 /* Assume next two bytes are size of the subtitle */
902 unsigned char *ptr
= subtitle
->data
+ i
;
903 int packet_size
= (ptr
[0] << 8) | (ptr
[1]);
905 /* i is the first byte of a subtitle packet and the packet is complete */
906 if(i
+ packet_size
<= subtitle
->size
&&
907 *(ptr
+ packet_size
- 1) == 0xff)
911 /* Move trailing data to a new object */
912 /* This just crashed and we don't think it's supposed to happen. */
914 * if(i + packet_size < subtitle->size)
916 * mpeg3_subtitle_t *subtitle2 = new_subtitle(demuxer,
918 * demuxer->program_byte);
919 * subtitle2->size = subtitle->size - i - packet_size;
920 * subtitle2->data = malloc(subtitle2->size);
921 * memcpy(subtitle2->data, subtitle->data + i + packet_size, subtitle2->size);
922 * printf("mpeg3demux handle_subtitle: 2 subtitles in packet subtitle2->size=%d\n", subtitle2->size);
926 /* Move packet to subtitle track */
927 memcpy(subtitle
->data
, subtitle
->data
+ i
, packet_size
);
928 subtitle
->size
= packet_size
;
931 remove_subtitle_ptr(demuxer
, subtitle
);
932 mpeg3_strack_t
*strack
= mpeg3_create_strack(file
, subtitle
->id
);
933 mpeg3_append_subtitle(strack
, subtitle
);
934 demuxer
->got_subtitle
= 1;
936 * printf("handle_subtitle: id=0x%x pid=%x size=%d offset=0x%llx table_size=%d\n",
941 * demuxer->total_subtitles);
950 static int handle_pcm(mpeg3_demuxer_t
*demuxer
, int bytes
)
952 /* Synthesize PCM header and delete MPEG header for PCM data */
953 /* Must be done after decryption */
959 unsigned char *output
= 0;
960 unsigned char *data_buffer
= 0;
968 if(demuxer
->read_all
&& demuxer
->audio_size
)
970 output
= demuxer
->audio_buffer
+ demuxer
->audio_start
;
971 data_buffer
= demuxer
->audio_buffer
;
972 data_start
= demuxer
->audio_start
;
973 data_size
= &demuxer
->audio_size
;
977 output
= demuxer
->data_buffer
+ demuxer
->data_start
;
978 data_buffer
= demuxer
->data_buffer
;
979 data_start
= demuxer
->data_start
;
980 data_size
= &demuxer
->data_size
;
986 /* Shift audio back */
988 for(i
= *data_size
- 1, j
= *data_size
+ PCM_HEADERSIZE
- 3 - 1;
991 *(data_buffer
+ j
) = *(data_buffer
+ i
);
992 *data_size
+= PCM_HEADERSIZE
- 3;
994 bits_code
= (code
>> 6) & 3;
995 samplerate_code
= (code
& 0x10);
1003 switch(samplerate_code
)
1013 *(int32_t*)(output
+ 4) = samplerate
;
1017 case 0: bits
= 16; break;
1018 case 1: bits
= 20; break;
1019 case 2: bits
= 24; break;
1020 default: bits
= 16; break;
1022 *(int32_t*)(output
+ 8) = bits
;
1024 *(int32_t*)(output
+ 12) = (code
& 0x7) + 1;
1026 *(int32_t*)(output
+ 16) = bytes
-
1033 //printf(__FUNCTION__ " %02x%02x%02x %d\n", code1, code, code2, pes_packet_length - 3);
1041 /* Program packet reading core */
1042 static int get_program_pes_packet(mpeg3_demuxer_t
*demuxer
, unsigned int header
)
1044 unsigned int pts
= 0, dts
= 0;
1045 int pes_packet_length
;
1046 int64_t pes_packet_start
;
1047 int decryption_offset
;
1049 mpeg3_t
*file
= demuxer
->file
;
1050 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1052 /* Format if audio */
1055 demuxer
->data_start
= demuxer
->data_size
;
1056 demuxer
->audio_start
= demuxer
->audio_size
;
1057 demuxer
->video_start
= demuxer
->video_size
;
1059 demuxer
->stream_id
= header
& 0xff;
1060 pes_packet_length
= mpeg3io_read_int16(title
->fs
);
1061 pes_packet_start
= mpeg3io_tell(title
->fs
);
1066 * printf(__FUNCTION__ " pes_packet_start=%llx pes_packet_length=%x data_size=%x\n",
1068 * pes_packet_length,
1069 * demuxer->data_size);
1076 if(demuxer
->stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
1077 demuxer
->stream_id
!= MPEG3_PADDING_STREAM
)
1079 if((mpeg3io_next_char(title
->fs
) >> 6) == 0x02)
1081 /* Get MPEG-2 packet */
1082 int pes_header_bytes
= 0;
1084 int pes_header_data_length
;
1087 demuxer
->last_packet_decryption
= mpeg3io_tell(title
->fs
);
1088 scrambling
= mpeg3io_read_char(title
->fs
) & 0x30;
1090 /* Reset scrambling bit for the mpeg3cat utility */
1091 // if(scrambling) demuxer->raw_data[demuxer->raw_offset - 1] &= 0xcf;
1092 // Force packet length if scrambling
1093 if(scrambling
) pes_packet_length
= 0x800 -
1095 demuxer
->last_packet_start
;
1098 pts_dts_flags
= (mpeg3io_read_char(title
->fs
) >> 6) & 0x3;
1099 pes_header_data_length
= mpeg3io_read_char(title
->fs
);
1103 /* Get Presentation and Decoding Time Stamps */
1104 if(pts_dts_flags
== 2)
1106 pts
= get_timestamp(demuxer
);
1109 printf("pts=%d\n", pts
);
1111 pes_header_bytes
+= 5;
1114 if(pts_dts_flags
== 3)
1116 pts
= get_timestamp(demuxer
);
1117 dts
= get_timestamp(demuxer
);
1120 printf("pts=%d dts=%d\n", pts
, dts
);
1123 * pts = (mpeg3io_read_char(title->fs) >> 1) & 7;
1125 * pts |= (mpeg3io_read_int16(title->fs) >> 1);
1127 * pts |= (mpeg3io_read_int16(title->fs) >> 1);
1131 * dts = (mpeg3io_read_char(title->fs) >> 1) & 7;
1133 * dts |= (mpeg3io_read_int16(title->fs) >> 1);
1135 * dts |= (mpeg3io_read_int16(title->fs) >> 1);
1137 pes_header_bytes
+= 10;
1140 //printf("get_program_pes_packet do_audio=%d do_video=%d pts=%x dts=%x\n",
1141 // demuxer->do_audio, demuxer->do_video, pts, dts);
1144 mpeg3io_seek_relative(title
->fs
,
1145 pes_header_data_length
- pes_header_bytes
);
1150 /* Get MPEG-1 packet */
1151 while(mpeg3io_next_char(title
->fs
) == 0xff)
1153 mpeg3io_read_char(title
->fs
);
1156 /* Skip STD buffer scale */
1157 if((mpeg3io_next_char(title
->fs
) & 0x40) == 0x40)
1159 mpeg3io_seek_relative(title
->fs
, 2);
1162 /* Decide which timestamps are available */
1163 pts_dts_flags
= mpeg3io_next_char(title
->fs
);
1165 if(pts_dts_flags
>= 0x30)
1167 /* Get the presentation and decoding time stamp */
1168 pts
= get_timestamp(demuxer
);
1169 dts
= get_timestamp(demuxer
);
1172 if(pts_dts_flags
>= 0x20)
1174 /* Get just the presentation time stamp */
1175 pts
= get_timestamp(demuxer
);
1178 if(pts_dts_flags
== 0x0f)
1180 /* End of timestamps */
1181 mpeg3io_read_char(title
->fs
);
1185 return 1; /* Error */
1189 /* Now extract the payload. */
1190 if((demuxer
->stream_id
>> 4) == 0xc || (demuxer
->stream_id
>> 4) == 0xd)
1193 /* Take first stream ID if -1 */
1194 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1196 demuxer
->got_audio
= 1;
1197 demuxer
->custom_id
= demuxer
->stream_id
& 0x0f;
1200 if(demuxer
->read_all
)
1201 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_MPEG
;
1203 if(demuxer
->astream
== -1)
1204 demuxer
->astream
= demuxer
->custom_id
;
1209 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
1212 if(demuxer
->custom_id
== demuxer
->astream
&&
1213 demuxer
->do_audio
||
1216 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1219 printf(" MP2 audio data offset=%llx custom_id=%x size=%x\n",
1220 demuxer
->program_byte
,
1225 get_program_payload(demuxer
, pes_packet_length
, 1, 0);
1229 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1233 if((demuxer
->stream_id
>> 4) == 0xe)
1236 /* Take first stream ID if -1 */
1237 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1239 demuxer
->got_video
= 1;
1240 demuxer
->custom_id
= demuxer
->stream_id
& 0x0f;
1243 if(demuxer
->read_all
)
1244 demuxer
->vstream_table
[demuxer
->custom_id
] = 1;
1246 if(demuxer
->vstream
== -1)
1247 demuxer
->vstream
= demuxer
->custom_id
;
1249 if(pts
> 0) demuxer
->pes_video_time
= (double)pts
/ 60000;
1253 if(demuxer
->custom_id
== demuxer
->vstream
&&
1254 demuxer
->do_video
||
1257 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1260 printf(" video offset=%llx custom_id=%x size=%x\n",
1261 demuxer
->program_byte
,
1267 get_program_payload(demuxer
,
1276 printf(" skipping video size=%x\n", pes_packet_length
);
1278 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1282 if((demuxer
->stream_id
== 0xbd || demuxer
->stream_id
== 0xbf) &&
1283 mpeg3io_next_char(title
->fs
) != 0xff &&
1284 ((mpeg3io_next_char(title
->fs
) & 0xf0) == 0x20))
1286 /* DVD subtitle data */
1287 mpeg3_subtitle_t
*subtitle
= 0;
1288 int stream_id
= demuxer
->stream_id
= mpeg3io_read_char(title
->fs
);
1289 subtitle
= new_subtitle(demuxer
,
1291 demuxer
->program_byte
);
1293 /* Get data length */
1294 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1296 handle_subtitle(file
, demuxer
, subtitle
, pes_packet_length
);
1297 //printf("mpeg3_demux id=0x%02x size=%d total_size=%d\n", stream_id, pes_packet_length, subtitle->size);
1301 if((demuxer
->stream_id
== 0xbd || demuxer
->stream_id
== 0xbf) &&
1302 mpeg3io_next_char(title
->fs
) != 0xff &&
1303 ((mpeg3io_next_char(title
->fs
) & 0xf0) == 0x80))
1306 /* DVD audio data */
1307 /* Get the audio format */
1308 if((mpeg3io_next_char(title
->fs
) & 0xf0) == 0xa0)
1314 // Picks up bogus data if (& 0xf) or (& 0x7f)
1315 demuxer
->stream_id
= mpeg3io_next_char(title
->fs
);
1316 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
1318 demuxer
->got_audio
= 1;
1319 demuxer
->custom_id
= demuxer
->stream_id
;
1322 /* Take first stream ID if not building TOC. */
1323 if(demuxer
->read_all
)
1324 demuxer
->astream_table
[demuxer
->custom_id
] = format
;
1326 if(demuxer
->astream
== -1)
1327 demuxer
->astream
= demuxer
->custom_id
;
1335 if(demuxer
->custom_id
== demuxer
->astream
&&
1336 demuxer
->do_audio
||
1339 demuxer
->aformat
= format
;
1340 mpeg3io_read_int32(title
->fs
);
1342 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1343 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1345 if(format
== AUDIO_PCM
) do_pcm
= 1;
1346 //printf("get_program_pes_packet 5 %x\n", decryption_offset);
1350 printf(" AC3 audio data size=%x\n", pes_packet_length
);
1353 get_program_payload(demuxer
,
1360 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1361 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1363 //printf("get_program_pes_packet 6 %d\n", demuxer->astream_table[0x20]);
1366 if(demuxer
->stream_id
== 0xbc || 1)
1368 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1369 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1373 if(demuxer
->stream_id
== MPEG3_PRIVATE_STREAM_2
|| demuxer
->stream_id
== MPEG3_PADDING_STREAM
)
1375 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1376 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1382 if(scrambling
) handle_scrambling(demuxer
, decryption_offset
);
1386 if(do_pcm
) handle_pcm(demuxer
, pes_packet_length
);
1398 int mpeg3demux_read_program(mpeg3_demuxer_t
*demuxer
)
1402 mpeg3_t
*file
= demuxer
->file
;
1403 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1404 unsigned int header
= 0;
1407 demuxer
->got_audio
= 0;
1408 demuxer
->got_video
= 0;
1409 demuxer
->stream_id
= 0;
1410 demuxer
->custom_id
= -1;
1411 demuxer
->got_subtitle
= 0;
1413 if(mpeg3io_eof(title
->fs
)) return 1;
1415 //printf("mpeg3demux_read_program 2 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
1423 /* Search for next header */
1424 /* Parse packet until the next packet start code */
1425 while(!result
&& !mpeg3io_eof(title
->fs
))
1427 header
= mpeg3io_read_int32(title
->fs
);
1430 if(header
== MPEG3_PACK_START_CODE
)
1434 // Second start code in this call. Don't read it.
1437 mpeg3io_seek_relative(title
->fs
, -4);
1441 demuxer
->last_packet_start
= mpeg3io_tell(title
->fs
) - 4;
1442 result
= get_pack_header(demuxer
);
1449 if(header
== MPEG3_SYSTEM_START_CODE
&& pack_count
)
1454 result
= get_system_header(demuxer
);
1460 if((header
>> 8) == MPEG3_PACKET_START_CODE_PREFIX
&& pack_count
)
1464 result
= get_program_pes_packet(demuxer
, header
);
1472 mpeg3io_seek_relative(title
->fs
, -3);
1482 // Ignore errors in the parsing. Just quit if eof.
1490 demuxer
->last_packet_end
= mpeg3io_tell(title
->fs
);
1493 * if(demuxer->last_packet_end - demuxer->last_packet_start != 0x800)
1494 * printf(__FUNCTION__ " packet_size=%llx data_size=%x pack_count=%d packet_start=%llx packet_end=%llx\n",
1495 * demuxer->last_packet_end - demuxer->last_packet_start,
1496 * demuxer->data_size,
1498 * demuxer->last_packet_start,
1499 * demuxer->last_packet_end);
1501 //printf("mpeg3demux_read_program 5 %d\n", result);
1503 //printf("read_program 3\n");
1504 // if(!result) result = mpeg3io_eof(title->fs);
1506 demuxer
->program_byte
=
1507 mpeg3_absolute_to_program(demuxer
, mpeg3io_tell(title
->fs
) +
1515 // Point the current title and current cell to the program byte.
1516 static int get_current_cell(mpeg3_demuxer_t
*demuxer
)
1521 /* Find first cell on or after current position */
1522 if(demuxer
->reverse
)
1524 for(demuxer
->current_title
= demuxer
->total_titles
- 1;
1525 demuxer
->current_title
>= 0;
1526 demuxer
->current_title
--)
1528 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1529 for(demuxer
->title_cell
= title
->cell_table_size
- 1;
1530 demuxer
->title_cell
>= 0;
1531 demuxer
->title_cell
--)
1533 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1534 if(cell
->program_start
< demuxer
->program_byte
&&
1535 cell
->program
== demuxer
->current_program
)
1538 if(demuxer
->program_byte
> cell
->program_end
)
1539 demuxer
->program_byte
= cell
->program_end
;
1548 demuxer
->current_title
= 0;
1549 demuxer
->title_cell
= 0;
1555 for(demuxer
->current_title
= 0;
1556 demuxer
->current_title
< demuxer
->total_titles
;
1557 demuxer
->current_title
++)
1559 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1560 for(demuxer
->title_cell
= 0;
1561 demuxer
->title_cell
< title
->cell_table_size
;
1562 demuxer
->title_cell
++)
1564 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1565 if(cell
->program_end
> demuxer
->program_byte
&&
1566 cell
->program
== demuxer
->current_program
)
1569 if(demuxer
->program_byte
< cell
->program_start
)
1570 demuxer
->program_byte
= cell
->program_start
;
1579 demuxer
->current_title
= demuxer
->total_titles
- 1;
1580 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1581 demuxer
->title_cell
= title
->cell_table_size
- 1;
1587 //printf("get_current_cell 2 %p %d %d\n", demuxer, demuxer->current_title, demuxer->title_cell);
1595 int mpeg3_seek_phys(mpeg3_demuxer_t
*demuxer
)
1599 // This happens if the video header is bigger than the buffer size in a table
1600 // of contents scan.
1601 if(demuxer
->current_title
< 0 ||
1602 demuxer
->current_title
>= demuxer
->total_titles
)
1604 printf("mpeg3_seek_phys demuxer=%p read_all=%d do_audio=%d do_video=%d demuxer->current_title=%d\n",
1609 demuxer
->current_title
);
1613 if(!demuxer
->titles
) return 1;
1615 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1616 //printf("mpeg3_seek_phys: %p %d %d\n", title->cell_table, demuxer->current_title, demuxer->title_cell);
1618 if(!title
->cell_table
) return 1;
1620 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1623 /* Don't do anything if we're in the cell and it's the right program */
1624 if(demuxer
->reverse
)
1626 if(demuxer
->program_byte
> cell
->program_start
&&
1627 demuxer
->program_byte
<= cell
->program_end
&&
1628 cell
->program
== demuxer
->current_program
)
1636 //printf("mpeg3_seek_phys %lld %lld\n", demuxer->program_byte, demuxer->stream_end);
1637 if(demuxer
->stream_end
> 0 &&
1638 demuxer
->program_byte
>= demuxer
->stream_end
) return 1;
1641 if(demuxer
->program_byte
>= cell
->program_start
&&
1642 demuxer
->program_byte
< cell
->program_end
&&
1643 cell
->program
== demuxer
->current_program
)
1649 // Need to change cells if we get here.
1650 int last_cell
= demuxer
->title_cell
;
1651 int last_title
= demuxer
->current_title
;
1652 int64_t last_byte
= demuxer
->program_byte
;
1655 result
= get_current_cell(demuxer
);
1657 if(result
) return 1;
1659 if(demuxer
->current_title
!= last_title
)
1661 mpeg3demux_open_title(demuxer
, demuxer
->current_title
);
1664 title
= demuxer
->titles
[demuxer
->current_title
];
1665 cell
= &title
->cell_table
[demuxer
->title_cell
];
1669 mpeg3io_seek(title
->fs
,
1670 demuxer
->program_byte
- cell
->program_start
+ cell
->title_start
);
1681 static int next_code(mpeg3_demuxer_t
*demuxer
,
1684 uint32_t result
= 0;
1686 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1687 mpeg3_fs_t
*fd
= title
->fs
;
1689 while(result
!= code
&&
1692 title
= demuxer
->titles
[demuxer
->current_title
];
1694 result
|= (unsigned char)mpeg3io_read_char(title
->fs
);
1695 demuxer
->program_byte
++;
1696 error
= mpeg3_seek_phys(demuxer
);
1710 /* Read packet in the forward direction */
1711 int mpeg3_read_next_packet(mpeg3_demuxer_t
*demuxer
)
1713 if(demuxer
->current_title
< 0) return 1;
1716 int current_position
;
1717 mpeg3_t
*file
= demuxer
->file
;
1718 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1721 /* Reset output buffer */
1722 demuxer
->data_size
= 0;
1723 demuxer
->data_position
= 0;
1724 demuxer
->audio_size
= 0;
1725 demuxer
->video_size
= 0;
1727 /* Switch to forward direction. */
1728 if(demuxer
->reverse
)
1730 /* Don't reread anything if we're at the beginning of the file. */
1731 if(demuxer
->program_byte
< 0)
1733 demuxer
->program_byte
= 0;
1734 result
= mpeg3_seek_phys(demuxer
);
1735 /* First character was the -1 byte which brings us to 0 after this function. */
1739 /* Transport or elementary stream */
1740 if(file
->packet_size
> 0)
1742 demuxer
->program_byte
+= file
->packet_size
;
1743 result
= mpeg3_seek_phys(demuxer
);
1747 /* Packet just read */
1748 if(!result
) result
= next_code(demuxer
,
1749 MPEG3_PACK_START_CODE
);
1751 if(!result
) result
= next_code(demuxer
,
1752 MPEG3_PACK_START_CODE
);
1755 demuxer
->reverse
= 0;
1764 /* Read packets until the output buffer is full. */
1765 /* Read a single packet if not fetching audio or video. */
1770 title
= demuxer
->titles
[demuxer
->current_title
];
1771 //printf("mpeg3_read_next_packet 10 %llx\n", mpeg3io_tell(title->fs));
1775 if(file
->is_transport_stream
)
1777 result
= mpeg3_seek_phys(demuxer
);
1778 if(!result
) result
= read_transport(demuxer
);
1781 if(file
->is_program_stream
)
1783 result
= mpeg3_seek_phys(demuxer
);
1784 if(!result
) result
= mpeg3demux_read_program(demuxer
);
1787 if(demuxer
->read_all
&& file
->is_audio_stream
)
1789 /* Read elementary stream. */
1790 result
= mpeg3io_read_data(demuxer
->audio_buffer
,
1791 file
->packet_size
, title
->fs
);
1792 demuxer
->audio_size
= file
->packet_size
;
1793 demuxer
->program_byte
+= file
->packet_size
;
1794 result
|= mpeg3_seek_phys(demuxer
);
1797 if(demuxer
->read_all
&& file
->is_video_stream
)
1799 /* Read elementary stream. */
1800 result
= mpeg3io_read_data(demuxer
->video_buffer
,
1801 file
->packet_size
, title
->fs
);
1802 demuxer
->video_size
= file
->packet_size
;
1803 demuxer
->program_byte
+= file
->packet_size
;
1804 result
|= mpeg3_seek_phys(demuxer
);
1808 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1809 file
->packet_size
, title
->fs
);
1810 demuxer
->data_size
= file
->packet_size
;
1811 demuxer
->program_byte
+= file
->packet_size
;
1812 //printf("mpeg3_read_next_packet 1 %llx\n", mpeg3demux_tell_byte(demuxer));
1813 result
|= mpeg3_seek_phys(demuxer
);
1814 //printf("mpeg3_read_next_packet 2 %llx\n", mpeg3demux_tell_byte(demuxer));
1818 * printf("mpeg3_read_next_packet 100 result=%d data_size=0x%x\n",
1820 * demuxer->data_size);
1823 demuxer
->data_size
== 0 &&
1824 (demuxer
->do_audio
|| demuxer
->do_video
));
1833 static int prev_code(mpeg3_demuxer_t
*demuxer
,
1836 uint32_t result
= 0;
1838 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1839 mpeg3_fs_t
*fd
= title
->fs
;
1842 while(result
!= code
&&
1843 demuxer
->program_byte
> 0 &&
1847 title
= demuxer
->titles
[demuxer
->current_title
];
1848 mpeg3io_seek(title
->fs
, demuxer
->program_byte
- title
->start_byte
- 1LL);
1849 result
|= ((uint32_t)mpeg3io_read_char(title
->fs
)) << 24;
1850 demuxer
->program_byte
--;
1851 error
= mpeg3_seek_phys(demuxer
);
1860 /* Read the packet right before the packet we're currently on. */
1861 int mpeg3_read_prev_packet(mpeg3_demuxer_t
*demuxer
)
1864 mpeg3_t
*file
= demuxer
->file
;
1865 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1867 demuxer
->data_size
= 0;
1868 demuxer
->data_position
= 0;
1872 /* Switch to reverse direction */
1873 if(!demuxer
->reverse
)
1875 demuxer
->reverse
= 1;
1877 /* Transport stream or elementary stream case */
1878 if(file
->packet_size
> 0)
1880 demuxer
->program_byte
-= file
->packet_size
;
1881 result
= mpeg3_seek_phys(demuxer
);
1884 /* Program stream */
1886 result
= prev_code(demuxer
,
1887 MPEG3_PACK_START_CODE
);
1898 /* Go to beginning of previous packet */
1901 title
= demuxer
->titles
[demuxer
->current_title
];
1903 /* Transport stream or elementary stream case */
1904 if(file
->packet_size
> 0)
1906 printf("mpeg3_read_prev_packet 1 result=%d title=%d tell=%llx program_byte=%llx\n", result
, demuxer
->current_title
, mpeg3io_tell(title
->fs
), demuxer
->program_byte
);
1907 demuxer
->program_byte
-= file
->packet_size
;
1908 result
= mpeg3_seek_phys(demuxer
);
1909 printf("mpeg3_read_prev_packet 100 result=%d title=%d tell=%llx program_byte=%llx\n", result
, demuxer
->current_title
, mpeg3io_tell(title
->fs
), demuxer
->program_byte
);
1913 if(!result
) result
= prev_code(demuxer
,
1914 MPEG3_PACK_START_CODE
);
1919 /* Read packet and then rewind it */
1920 title
= demuxer
->titles
[demuxer
->current_title
];
1921 if(file
->is_transport_stream
&& !result
)
1923 result
= read_transport(demuxer
);
1925 if(demuxer
->program_byte
> 0)
1927 demuxer
->program_byte
-= file
->packet_size
;
1928 result
= mpeg3_seek_phys(demuxer
);
1932 if(file
->is_program_stream
&& !result
)
1934 int64_t current_position
= demuxer
->program_byte
;
1937 result
= mpeg3demux_read_program(demuxer
);
1940 while(demuxer
->program_byte
> current_position
&&
1943 result
= prev_code(demuxer
,
1944 MPEG3_PACK_START_CODE
);
1950 /* Elementary stream */
1951 /* Read the packet forwards and seek back to the start */
1952 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1958 demuxer
->data_size
= file
->packet_size
;
1959 result
= mpeg3io_seek(title
->fs
, demuxer
->program_byte
);
1963 demuxer
->data_size
== 0 &&
1964 (demuxer
->do_audio
|| demuxer
->do_video
));
1972 int mpeg3demux_read_data(mpeg3_demuxer_t
*demuxer
,
1973 unsigned char *output
,
1977 demuxer
->error_flag
= 0;
1978 //printf("mpeg3demux_read_data 1\n");
1980 if(demuxer
->data_position
>= 0)
1984 for(i
= 0; i
< size
&& !result
; )
1986 int fragment_size
= size
- i
;
1987 if(fragment_size
> demuxer
->data_size
- demuxer
->data_position
)
1988 fragment_size
= demuxer
->data_size
- demuxer
->data_position
;
1989 memcpy(output
+ i
, demuxer
->data_buffer
+ demuxer
->data_position
, fragment_size
);
1990 demuxer
->data_position
+= fragment_size
;
1995 result
= mpeg3_read_next_packet(demuxer
);
1998 * printf("mpeg3demux_read_data 10 offset=%llx pid=0x%x bytes=0x%x i=0x%x\n",
1999 * mpeg3demux_tell_byte(demuxer),
2001 * demuxer->data_size,
2005 //printf("mpeg3demux_read_data 10\n");
2009 int current_position
;
2010 /* Read backwards a full packet. */
2011 /* Only good for reading less than the size of a full packet, but */
2012 /* this routine should only be used for searching for previous markers. */
2013 current_position
= demuxer
->data_position
;
2014 result
= mpeg3_read_prev_packet(demuxer
);
2015 if(!result
) demuxer
->data_position
= demuxer
->data_size
+ current_position
;
2016 memcpy(output
, demuxer
->data_buffer
+ demuxer
->data_position
, size
);
2017 demuxer
->data_position
+= size
;
2020 //printf("mpeg3demux_read_data 2\n");
2021 demuxer
->error_flag
= result
;
2025 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t
*demuxer
)
2027 demuxer
->error_flag
= 0;
2028 demuxer
->next_char
= -1;
2030 if(demuxer
->data_position
>= demuxer
->data_size
)
2032 demuxer
->error_flag
= mpeg3_read_next_packet(demuxer
);
2035 if(!demuxer
->error_flag
)
2036 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
++];
2038 return demuxer
->next_char
;
2041 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t
*demuxer
)
2043 demuxer
->error_flag
= 0;
2044 demuxer
->data_position
--;
2045 if(demuxer
->data_position
< 0)
2047 demuxer
->error_flag
= mpeg3_read_prev_packet(demuxer
);
2048 if(!demuxer
->error_flag
) demuxer
->data_position
= demuxer
->data_size
- 1;
2051 if(demuxer
->data_position
>= 0)
2053 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
];
2055 return demuxer
->next_char
;
2060 int mpeg3demux_open_title(mpeg3_demuxer_t
*demuxer
, int title_number
)
2062 mpeg3_title_t
*title
;
2064 if(title_number
< demuxer
->total_titles
&&
2067 if(demuxer
->current_title
>= 0)
2069 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
2070 demuxer
->current_title
= -1;
2073 title
= demuxer
->titles
[title_number
];
2075 if(mpeg3io_open_file(title
->fs
))
2077 demuxer
->error_flag
= 1;
2078 fprintf(stderr
, "mpeg3demux_open_title %s: %s", title
->fs
->path
, strerror(errno
));
2082 demuxer
->current_title
= title_number
;
2087 fprintf(stderr
, "mpeg3demux_open_title title_number = %d\n",
2092 return demuxer
->error_flag
;
2095 int mpeg3demux_copy_titles(mpeg3_demuxer_t
*dst
, mpeg3_demuxer_t
*src
)
2098 mpeg3_t
*file
= dst
->file
;
2099 mpeg3_title_t
*dst_title
, *src_title
;
2101 dst
->total_titles
= src
->total_titles
;
2102 dst
->total_programs
= src
->total_programs
;
2104 for(i
= 0; i
< MPEG3_MAX_STREAMS
; i
++)
2106 dst
->astream_table
[i
] = src
->astream_table
[i
];
2107 dst
->vstream_table
[i
] = src
->vstream_table
[i
];
2110 for(i
= 0; i
< src
->total_titles
; i
++)
2112 src_title
= src
->titles
[i
];
2113 dst_title
= dst
->titles
[i
] = mpeg3_new_title(file
,
2114 src
->titles
[i
]->fs
->path
);
2115 mpeg3_copy_title(dst_title
, src_title
);
2118 mpeg3demux_open_title(dst
, src
->current_title
);
2119 dst
->title_cell
= 0;
2123 /* ==================================================================== */
2125 /* ==================================================================== */
2127 mpeg3_demuxer_t
* mpeg3_new_demuxer(mpeg3_t
*file
, int do_audio
, int do_video
, int custom_id
)
2129 mpeg3_demuxer_t
*demuxer
= calloc(1, sizeof(mpeg3_demuxer_t
));
2132 /* The demuxer will change the default packet size for its own use. */
2133 demuxer
->file
= file
;
2134 demuxer
->do_audio
= do_audio
;
2135 demuxer
->do_video
= do_video
;
2137 /* Allocate buffer + padding */
2138 demuxer
->raw_data
= calloc(1, MPEG3_RAW_SIZE
);
2139 demuxer
->data_buffer
= calloc(1, MPEG3_RAW_SIZE
);
2140 demuxer
->data_allocated
= MPEG3_RAW_SIZE
;
2142 demuxer
->audio_buffer
= calloc(1, MPEG3_RAW_SIZE
);
2143 demuxer
->audio_allocated
= MPEG3_RAW_SIZE
;
2145 demuxer
->video_buffer
= calloc(1, MPEG3_RAW_SIZE
);
2146 demuxer
->video_allocated
= MPEG3_RAW_SIZE
;
2148 /* System specific variables */
2149 demuxer
->audio_pid
= custom_id
;
2150 demuxer
->video_pid
= custom_id
;
2151 demuxer
->astream
= custom_id
;
2152 demuxer
->vstream
= custom_id
;
2153 demuxer
->current_title
= -1;
2154 demuxer
->pes_audio_time
= -1;
2155 demuxer
->pes_video_time
= -1;
2156 //printf("mpeg3_new_demuxer %f\n", demuxer->time);
2157 demuxer
->stream_end
= -1;
2162 int mpeg3_delete_demuxer(mpeg3_demuxer_t
*demuxer
)
2166 if(demuxer
->current_title
>= 0)
2168 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
2171 for(i
= 0; i
< demuxer
->total_titles
; i
++)
2173 mpeg3_delete_title(demuxer
->titles
[i
]);
2176 if(demuxer
->data_buffer
) free(demuxer
->data_buffer
);
2177 if(demuxer
->raw_data
) free(demuxer
->raw_data
);
2178 if(demuxer
->audio_buffer
) free(demuxer
->audio_buffer
);
2179 if(demuxer
->video_buffer
) free(demuxer
->video_buffer
);
2180 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
2182 mpeg3_delete_subtitle(demuxer
->subtitles
[i
]);
2184 if(demuxer
->subtitles
) free(demuxer
->subtitles
);
2190 int mpeg3demux_eof(mpeg3_demuxer_t
*demuxer
)
2192 mpeg3_t
*file
= demuxer
->file
;
2195 if(demuxer
->current_title
>= 0)
2197 if(mpeg3io_eof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
2198 demuxer
->current_title
>= demuxer
->total_titles
- 1)
2202 // Same operation performed in mpeg3_seek_phys
2203 if(demuxer
->stream_end
> 0 &&
2204 demuxer
->program_byte
>= demuxer
->stream_end
) return 1;
2208 if(demuxer
->data_position
>= demuxer
->data_size
) return 1;
2214 int mpeg3demux_bof(mpeg3_demuxer_t
*demuxer
)
2216 if(demuxer
->current_title
>= 0)
2218 if(mpeg3io_bof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
2219 demuxer
->current_title
<= 0)
2225 void mpeg3demux_start_reverse(mpeg3_demuxer_t
*demuxer
)
2227 demuxer
->reverse
= 1;
2230 void mpeg3demux_start_forward(mpeg3_demuxer_t
*demuxer
)
2232 demuxer
->reverse
= 0;
2235 /* Seek to absolute byte */
2236 int mpeg3demux_seek_byte(mpeg3_demuxer_t
*demuxer
, int64_t byte
)
2238 mpeg3_t
*file
= demuxer
->file
;
2242 * for(i = 0; i < demuxer->total_titles; i++) mpeg3_dump_title(demuxer->titles[i]);
2245 demuxer
->program_byte
= byte
;
2246 demuxer
->data_position
= 0;
2247 demuxer
->data_size
= 0;
2249 /* Get on a packet boundary only for transport streams. */
2250 if(file
->is_transport_stream
&&
2253 demuxer
->program_byte
-= demuxer
->program_byte
% file
->packet_size
;
2256 int result
= mpeg3_seek_phys(demuxer
);
2258 * printf("mpeg3demux_seek_byte 1 %d %d %lld %lld\n",
2259 * demuxer->do_video, result, byte, demuxer->program_byte);
2266 int mpeg3_finished_subtitles(mpeg3_demuxer_t
*demuxer
, int id
)
2270 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
2272 if(demuxer
->subtitles
[i
]->done
&&
2274 demuxer
->subtitles
[i
]->id
== id
)) total
++;
2285 double mpeg3demux_get_time(mpeg3_demuxer_t
*demuxer
)
2287 return demuxer
->time
;
2290 double mpeg3demux_audio_pts(mpeg3_demuxer_t
*demuxer
)
2292 return demuxer
->pes_audio_time
;
2295 double mpeg3demux_video_pts(mpeg3_demuxer_t
*demuxer
)
2297 return demuxer
->pes_video_time
;
2300 void mpeg3demux_reset_pts(mpeg3_demuxer_t
*demuxer
)
2302 demuxer
->pes_audio_time
= -1;
2303 demuxer
->pes_video_time
= -1;
2306 double mpeg3demux_scan_pts(mpeg3_demuxer_t
*demuxer
)
2308 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
2309 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
2310 int64_t current_position
= start_position
;
2313 mpeg3demux_reset_pts(demuxer
);
2315 current_position
< end_position
&&
2316 ((demuxer
->do_audio
&& demuxer
->pes_audio_time
< 0) ||
2317 (demuxer
->do_video
&& demuxer
->pes_video_time
< 0)))
2319 result
= mpeg3_read_next_packet(demuxer
);
2320 current_position
= mpeg3demux_tell_byte(demuxer
);
2323 // Seek back to starting point
2324 mpeg3demux_seek_byte(demuxer
, start_position
);
2326 if(demuxer
->do_audio
) return demuxer
->pes_audio_time
;
2327 if(demuxer
->do_video
) return demuxer
->pes_video_time
;
2330 int mpeg3demux_goto_pts(mpeg3_demuxer_t
*demuxer
, double pts
)
2332 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
2333 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
2334 int64_t current_position
= start_position
;
2337 // Search forward for nearest pts
2338 mpeg3demux_reset_pts(demuxer
);
2339 while(!result
&& current_position
< end_position
)
2341 result
= mpeg3_read_next_packet(demuxer
);
2342 if(demuxer
->pes_audio_time
> pts
) break;
2343 current_position
= mpeg3demux_tell_byte(demuxer
);
2346 // Search backward for nearest pts
2347 end_position
= current_position
- MPEG3_PTS_RANGE
;
2348 mpeg3_read_prev_packet(demuxer
);
2350 current_position
> end_position
)
2352 result
= mpeg3_read_prev_packet(demuxer
);
2353 if(demuxer
->pes_audio_time
< pts
) break;
2354 current_position
= mpeg3demux_tell_byte(demuxer
);
2359 int64_t mpeg3demux_tell_byte(mpeg3_demuxer_t
*demuxer
)
2361 return demuxer
->program_byte
;
2364 int mpeg3demux_tell_program(mpeg3_demuxer_t
*demuxer
)
2366 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2367 if(!title
->cell_table
|| !title
->cell_table_size
) return 0;
2368 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
2369 return cell
->program
;
2372 int64_t mpeg3_absolute_to_program(mpeg3_demuxer_t
*demuxer
,
2377 //fprintf(stderr, "%d\n", demuxer->data_size);
2378 // Can only offset to current cell since we can't know what cell the
2379 // byte corresponds to.
2380 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2381 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
2385 cell
->program_start
;
2388 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t
*demuxer
)
2390 if(!demuxer
->total_bytes
)
2394 for(i
= 0; i
< demuxer
->total_titles
; i
++)
2396 mpeg3_title_t
*title
= demuxer
->titles
[i
];
2397 for(j
= 0; j
< title
->cell_table_size
; j
++)
2399 mpeg3_cell_t
*cell
= &title
->cell_table
[j
];
2400 if(cell
->program
== demuxer
->current_program
)
2401 result
+= cell
->program_end
- cell
->program_start
;
2403 // result += demuxer->titles[i]->total_bytes;
2405 demuxer
->total_bytes
= result
;
2407 return demuxer
->total_bytes
;
2410 int64_t mpeg3demuxer_title_bytes(mpeg3_demuxer_t
*demuxer
)
2412 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2413 return title
->total_bytes
;
2416 mpeg3_demuxer_t
* mpeg3_get_demuxer(mpeg3_t
*file
)
2418 if(file
->is_program_stream
|| file
->is_transport_stream
)
2420 if(file
->total_astreams
) return file
->atrack
[0]->demuxer
;
2422 if(file
->total_vstreams
) return file
->vtrack
[0]->demuxer
;
2427 void mpeg3demux_append_data(mpeg3_demuxer_t
*demuxer
,
2428 unsigned char *data
,
2431 int new_data_size
= demuxer
->data_size
+ bytes
;
2432 if(new_data_size
>= demuxer
->data_allocated
)
2434 demuxer
->data_allocated
= new_data_size
* 2;
2435 demuxer
->data_buffer
= realloc(demuxer
->data_buffer
,
2436 demuxer
->data_allocated
);
2439 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
2442 demuxer
->data_size
+= bytes
;
2445 void mpeg3demux_shift_data(mpeg3_demuxer_t
*demuxer
,
2449 for(i
= 0, j
= bytes
; j
< demuxer
->data_size
; i
++, j
++)
2451 demuxer
->data_buffer
[i
] = demuxer
->data_buffer
[j
];
2453 demuxer
->data_size
-= bytes
;
2454 demuxer
->data_position
-= bytes
;