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 /* Dump private data! */
405 fprintf(stderr
, "stream_id == MPEG3_PRIVATE_STREAM_2\n");
406 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
410 if(demuxer
->stream_id
== MPEG3_PADDING_STREAM
)
412 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
417 fprintf(stderr
, "unknown stream_id in pes packet");
423 static int get_payload(mpeg3_demuxer_t
*demuxer
)
425 //printf("get_payload 1 pid=0x%x unit_start=%d\n", demuxer->pid, demuxer->payload_unit_start_indicator);
426 if(demuxer
->payload_unit_start_indicator
)
428 if(demuxer
->pid
== 0)
429 get_program_association_table(demuxer
);
431 if(packet_next_int24(demuxer
) == MPEG3_PACKET_START_CODE_PREFIX
)
432 get_pes_packet(demuxer
);
434 packet_skip(demuxer
, demuxer
->raw_size
- demuxer
->raw_offset
);
440 printf(" 0x%x bytes elementary data\n", demuxer
->raw_size
- demuxer
->raw_offset
);
444 * if(demuxer->pid == 0x1100) printf("get_payload 1 0x%x\n", demuxer->audio_pid);
446 if(demuxer
->pid
== demuxer
->audio_pid
&&
447 (demuxer
->do_audio
||
450 if(demuxer
->do_audio
) demuxer
->got_audio
= 1;
454 printf("get_payload: offset=%llx 0x%x bytes AC3 pid=0x%x\n",
455 mpeg3io_tell(demuxer
->titles
[demuxer
->current_title
]->fs
),
456 demuxer
->raw_size
- demuxer
->raw_offset
,
460 get_transport_payload(demuxer
, 1, 0);
463 if(demuxer
->pid
== demuxer
->video_pid
&&
464 (demuxer
->do_video
||
467 if(demuxer
->do_video
) demuxer
->got_video
= 1;
468 get_transport_payload(demuxer
, 0, 1);
471 if(demuxer
->read_all
)
473 get_transport_payload(demuxer
, 0, 0);
474 // packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
480 /* Read a transport packet */
481 static int read_transport(mpeg3_demuxer_t
*demuxer
)
484 mpeg3_t
*file
= (mpeg3_t
*)demuxer
->file
;
485 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
490 /* Packet size is known for transport streams */
491 demuxer
->raw_size
= file
->packet_size
;
492 demuxer
->raw_offset
= 0;
493 demuxer
->stream_id
= 0;
494 demuxer
->got_audio
= 0;
495 demuxer
->got_video
= 0;
496 demuxer
->custom_id
= -1;
501 perror("read_transport");
505 //fprintf(stderr, "read transport 1 %llx %llx\n", title->fs->current_byte, title->fs->total_bytes);
509 mpeg3io_read_int32(title
->fs
);
511 // Search for Sync byte */
514 bits
= mpeg3io_read_char(title
->fs
);
515 }while(!mpeg3io_eof(title
->fs
) && !result
&& bits
!= MPEG3_SYNC_BYTE
);
518 if(mpeg3io_eof(title
->fs
) || result
) return 1;
520 if(bits
== MPEG3_SYNC_BYTE
&& !result
)
522 // Store byte just read as first byte
523 demuxer
->raw_data
[0] = MPEG3_SYNC_BYTE
;
526 int fragment_size
= file
->packet_size
- 1;
531 demuxer
->raw_size
-= 4;
533 result
= mpeg3io_read_data(demuxer
->raw_data
+ 1,
540 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
547 packet_read_char(demuxer
);
548 bits
= packet_read_int24(demuxer
) & 0x00ffffff;
549 demuxer
->transport_error_indicator
= (bits
>> 23) & 0x1;
550 demuxer
->payload_unit_start_indicator
= (bits
>> 22) & 0x1;
551 demuxer
->pid
= demuxer
->custom_id
= (bits
>> 8) & 0x00001fff;
553 demuxer
->transport_scrambling_control
= (bits
>> 6) & 0x3;
554 demuxer
->adaptation_field_control
= (bits
>> 4) & 0x3;
555 demuxer
->continuity_counter
= bits
& 0xf;
558 // This caused an audio track to not be created.
559 if(demuxer
->transport_error_indicator
)
561 // fprintf(stderr, "demuxer->transport_error_indicator at %llx\n",
562 // mpeg3io_tell(title->fs));
563 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
568 if (demuxer
->pid
== 0x1fff)
570 demuxer
->is_padding
= 1; /* padding; just go to next */
574 demuxer
->is_padding
= 0;
577 /* Get pid from table */
578 for(table_entry
= 0, result
= 0;
579 table_entry
< demuxer
->total_pids
;
582 if(demuxer
->pid
== demuxer
->pid_table
[table_entry
])
592 /* Not in pid table */
593 if(!result
&& demuxer
->total_pids
< MPEG3_PIDMAX
)
595 demuxer
->pid_table
[table_entry
] = demuxer
->pid
;
596 demuxer
->continuity_counters
[table_entry
] = demuxer
->continuity_counter
; /* init */
597 demuxer
->total_pids
++;
603 printf("offset=0x%llx pid=0x%02x continuity=0x%02x padding=%d adaptation=%d unit_start=%d\n",
604 demuxer
->program_byte
,
606 demuxer
->continuity_counter
,
608 demuxer
->adaptation_field_control
,
609 demuxer
->payload_unit_start_indicator
);
613 /* Abort if padding. Should abort after demuxer->pid == 0x1fff for speed. */
614 if(demuxer
->is_padding
)
616 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
624 if(demuxer
->adaptation_field_control
& 0x2)
625 result
= get_adaptation_field(demuxer
);
627 // Need to enter in astream and vstream table:
628 // PID ored with stream_id
629 if(demuxer
->adaptation_field_control
& 0x1)
630 result
= get_payload(demuxer
);
632 demuxer
->program_byte
= mpeg3io_tell(title
->fs
) +
637 static int get_system_header(mpeg3_demuxer_t
*demuxer
)
639 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
640 int length
= mpeg3io_read_int16(title
->fs
);
641 mpeg3io_seek_relative(title
->fs
, length
);
645 static unsigned int get_timestamp(mpeg3_demuxer_t
*demuxer
)
647 unsigned int timestamp
;
648 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
650 /* Only low 4 bits (7==1111) */
651 timestamp
= (mpeg3io_read_char(title
->fs
) >> 1) & 7;
653 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
655 timestamp
|= (mpeg3io_read_int16(title
->fs
) >> 1);
659 static int get_pack_header(mpeg3_demuxer_t
*demuxer
)
662 unsigned int clock_ref
, clock_ref_ext
;
663 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
665 /* Get the time code */
666 if((mpeg3io_next_char(title
->fs
) >> 4) == 2)
669 demuxer
->time
= (double)get_timestamp(demuxer
) / 90000;
671 mpeg3io_read_int24(title
->fs
);
674 if(mpeg3io_next_char(title
->fs
) & 0x40)
676 i
= mpeg3io_read_int32(title
->fs
);
677 j
= mpeg3io_read_int16(title
->fs
);
679 if(i
& 0x40000000 || (i
>> 28) == 2)
681 clock_ref
= ((i
& 0x38000000) << 3);
682 clock_ref
|= ((i
& 0x03fff800) << 4);
683 clock_ref
|= ((i
& 0x000003ff) << 5);
684 clock_ref
|= ((j
& 0xf800) >> 11);
685 clock_ref_ext
= (j
>> 1) & 0x1ff;
687 demuxer
->time
= (double)(clock_ref
+ clock_ref_ext
/ 300) / 90000;
689 mpeg3io_read_int24(title
->fs
);
690 i
= mpeg3io_read_char(title
->fs
) & 0x7;
693 mpeg3io_seek_relative(title
->fs
, i
);
698 mpeg3io_seek_relative(title
->fs
, 2);
705 static int get_program_payload(mpeg3_demuxer_t
*demuxer
,
710 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
712 if(demuxer
->read_all
&& is_audio
)
714 mpeg3io_read_data(demuxer
->audio_buffer
+ demuxer
->audio_size
,
717 demuxer
->audio_size
+= bytes
;
720 if(demuxer
->read_all
&& is_video
)
722 mpeg3io_read_data(demuxer
->video_buffer
+ demuxer
->video_size
,
725 demuxer
->video_size
+= bytes
;
729 mpeg3io_read_data(demuxer
->data_buffer
+ demuxer
->data_size
,
732 demuxer
->data_size
+= bytes
;
744 static int handle_scrambling(mpeg3_demuxer_t
*demuxer
,
745 int decryption_offset
)
747 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
749 // Advance 2048 bytes if scrambled. We might pick up a spurrius
750 // packet start code in the scrambled data otherwise.
751 if(demuxer
->last_packet_start
+ 0x800 > mpeg3io_tell(title
->fs
))
753 mpeg3io_seek_relative(title
->fs
,
754 demuxer
->last_packet_start
+ 0x800 - mpeg3io_tell(title
->fs
));
759 // Descramble if desired.
760 if(demuxer
->data_size
||
761 demuxer
->audio_size
||
764 unsigned char *buffer_ptr
= 0;
765 if(demuxer
->data_size
) buffer_ptr
= demuxer
->data_buffer
;
767 if(demuxer
->audio_size
) buffer_ptr
= demuxer
->audio_buffer
;
769 if(demuxer
->video_size
) buffer_ptr
= demuxer
->video_buffer
;
772 //printf(__FUNCTION__ " data_size=%x decryption_offset=%x\n", demuxer->data_size, decryption_offset);
773 if(mpeg3_decrypt_packet(title
->fs
->css
,
777 fprintf(stderr
, "handle_scrambling: Decryption not available\n");
785 static void remove_subtitle(mpeg3_demuxer_t
*demuxer
, int number
)
788 mpeg3_delete_subtitle(demuxer
->subtitles
[number
]);
789 for(j
= number
; j
< demuxer
->total_subtitles
- 1; j
++)
791 demuxer
->subtitles
[j
] = demuxer
->subtitles
[j
+ 1];
793 demuxer
->total_subtitles
--;
796 static void remove_subtitle_ptr(mpeg3_demuxer_t
*demuxer
, mpeg3_subtitle_t
*ptr
)
798 int i
, number
= demuxer
->total_subtitles
;
799 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
801 if(demuxer
->subtitles
[i
] == ptr
)
808 for(i
= number
; i
< demuxer
->total_subtitles
- 1; i
++)
810 demuxer
->subtitles
[i
] = demuxer
->subtitles
[i
+ 1];
812 demuxer
->total_subtitles
--;
815 /* Create new subtitle object if none with the same id is in the table */
816 static mpeg3_subtitle_t
* new_subtitle(mpeg3_demuxer_t
*demuxer
,
820 mpeg3_subtitle_t
*subtitle
= 0;
823 /* Get subtitle object for the stream and total subtitle objects for the stream */
826 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
828 if(demuxer
->subtitles
[i
]->id
== id
) total
++;
830 if(demuxer
->subtitles
[i
]->id
== id
&&
831 !demuxer
->subtitles
[i
]->done
&&
834 subtitle
= demuxer
->subtitles
[i
];
839 /* Make new subtitle object */
842 demuxer
->total_subtitles
++;
843 demuxer
->subtitles
= realloc(demuxer
->subtitles
,
844 sizeof(mpeg3_subtitle_t
*) * demuxer
->total_subtitles
);
845 subtitle
= demuxer
->subtitles
[demuxer
->total_subtitles
- 1] =
846 calloc(sizeof(mpeg3_subtitle_t
), 1);
848 subtitle
->offset
= offset
;
851 /* Delete oldest subtitle */
852 if(total
> MPEG3_MAX_SUBTITLES
)
854 for(i
= 0; i
< demuxer
->total_subtitles
- 1; i
++)
856 if(demuxer
->subtitles
[i
]->id
== id
)
858 remove_subtitle(demuxer
, i
);
868 static void handle_subtitle(mpeg3_t
*file
,
869 mpeg3_demuxer_t
*demuxer
,
870 mpeg3_subtitle_t
*subtitle
,
873 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
876 /* Allocate memory */
879 subtitle
->data
= malloc(bytes
);
883 subtitle
->data
= realloc(subtitle
->data
,
884 subtitle
->size
+ bytes
);
887 /* Add data to latest subtitle packet. */
888 mpeg3io_read_data(subtitle
->data
+ subtitle
->size
,
893 * static FILE *test = 0;
894 * if(!test) test = fopen("/tmp/debug", "w");
895 * if(stream_id == 0x20) fwrite(subtitle->data + subtitle->size, pes_packet_length, 1, test);
898 subtitle
->size
+= bytes
;
900 /* Assume complete subtitle object has been loaded and align it in the buffer */
901 for(i
= 0; i
< subtitle
->size
- 1; i
++)
903 /* Assume next two bytes are size of the subtitle */
904 unsigned char *ptr
= subtitle
->data
+ i
;
905 int packet_size
= (ptr
[0] << 8) | (ptr
[1]);
907 /* i is the first byte of a subtitle packet and the packet is complete */
908 if(i
+ packet_size
<= subtitle
->size
&&
909 *(ptr
+ packet_size
- 1) == 0xff)
913 /* Move trailing data to a new object */
914 /* This just crashed and we don't think it's supposed to happen. */
916 * if(i + packet_size < subtitle->size)
918 * mpeg3_subtitle_t *subtitle2 = new_subtitle(demuxer,
920 * demuxer->program_byte);
921 * subtitle2->size = subtitle->size - i - packet_size;
922 * subtitle2->data = malloc(subtitle2->size);
923 * memcpy(subtitle2->data, subtitle->data + i + packet_size, subtitle2->size);
924 * printf("mpeg3demux handle_subtitle: 2 subtitles in packet subtitle2->size=%d\n", subtitle2->size);
928 /* Move packet to subtitle track */
929 memcpy(subtitle
->data
, subtitle
->data
+ i
, packet_size
);
930 subtitle
->size
= packet_size
;
933 remove_subtitle_ptr(demuxer
, subtitle
);
934 mpeg3_strack_t
*strack
= mpeg3_create_strack(file
, subtitle
->id
);
935 mpeg3_append_subtitle(strack
, subtitle
);
936 demuxer
->got_subtitle
= 1;
938 * printf("handle_subtitle: id=0x%x pid=%x size=%d offset=0x%llx table_size=%d\n",
943 * demuxer->total_subtitles);
952 static int handle_pcm(mpeg3_demuxer_t
*demuxer
, int bytes
)
954 /* Synthesize PCM header and delete MPEG header for PCM data */
955 /* Must be done after decryption */
961 unsigned char *output
= 0;
962 unsigned char *data_buffer
= 0;
970 if(demuxer
->read_all
&& demuxer
->audio_size
)
972 output
= demuxer
->audio_buffer
+ demuxer
->audio_start
;
973 data_buffer
= demuxer
->audio_buffer
;
974 data_start
= demuxer
->audio_start
;
975 data_size
= &demuxer
->audio_size
;
979 output
= demuxer
->data_buffer
+ demuxer
->data_start
;
980 data_buffer
= demuxer
->data_buffer
;
981 data_start
= demuxer
->data_start
;
982 data_size
= &demuxer
->data_size
;
988 /* Shift audio back */
990 for(i
= *data_size
- 1, j
= *data_size
+ PCM_HEADERSIZE
- 3 - 1;
993 *(data_buffer
+ j
) = *(data_buffer
+ i
);
994 *data_size
+= PCM_HEADERSIZE
- 3;
996 bits_code
= (code
>> 6) & 3;
997 samplerate_code
= (code
& 0x10);
1005 switch(samplerate_code
)
1015 *(int32_t*)(output
+ 4) = samplerate
;
1019 case 0: bits
= 16; break;
1020 case 1: bits
= 20; break;
1021 case 2: bits
= 24; break;
1022 default: bits
= 16; break;
1024 *(int32_t*)(output
+ 8) = bits
;
1026 *(int32_t*)(output
+ 12) = (code
& 0x7) + 1;
1028 *(int32_t*)(output
+ 16) = bytes
-
1035 //printf(__FUNCTION__ " %02x%02x%02x %d\n", code1, code, code2, pes_packet_length - 3);
1043 /* Program packet reading core */
1044 static int get_program_pes_packet(mpeg3_demuxer_t
*demuxer
, unsigned int header
)
1046 unsigned int pts
= 0, dts
= 0;
1047 int pes_packet_length
;
1048 int64_t pes_packet_start
;
1049 int decryption_offset
;
1051 mpeg3_t
*file
= demuxer
->file
;
1052 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1054 /* Format if audio */
1057 demuxer
->data_start
= demuxer
->data_size
;
1058 demuxer
->audio_start
= demuxer
->audio_size
;
1059 demuxer
->video_start
= demuxer
->video_size
;
1061 demuxer
->stream_id
= header
& 0xff;
1062 pes_packet_length
= mpeg3io_read_int16(title
->fs
);
1063 pes_packet_start
= mpeg3io_tell(title
->fs
);
1068 * printf(__FUNCTION__ " pes_packet_start=%llx pes_packet_length=%x data_size=%x\n",
1070 * pes_packet_length,
1071 * demuxer->data_size);
1078 if(demuxer
->stream_id
!= MPEG3_PRIVATE_STREAM_2
&&
1079 demuxer
->stream_id
!= MPEG3_PADDING_STREAM
)
1081 if((mpeg3io_next_char(title
->fs
) >> 6) == 0x02)
1083 /* Get MPEG-2 packet */
1084 int pes_header_bytes
= 0;
1086 int pes_header_data_length
;
1089 demuxer
->last_packet_decryption
= mpeg3io_tell(title
->fs
);
1090 scrambling
= mpeg3io_read_char(title
->fs
) & 0x30;
1092 /* Reset scrambling bit for the mpeg3cat utility */
1093 // if(scrambling) demuxer->raw_data[demuxer->raw_offset - 1] &= 0xcf;
1094 // Force packet length if scrambling
1095 if(scrambling
) pes_packet_length
= 0x800 -
1097 demuxer
->last_packet_start
;
1100 pts_dts_flags
= (mpeg3io_read_char(title
->fs
) >> 6) & 0x3;
1101 pes_header_data_length
= mpeg3io_read_char(title
->fs
);
1105 /* Get Presentation and Decoding Time Stamps */
1106 if(pts_dts_flags
== 2)
1108 pts
= get_timestamp(demuxer
);
1111 printf("pts=%d\n", pts
);
1113 pes_header_bytes
+= 5;
1116 if(pts_dts_flags
== 3)
1118 pts
= get_timestamp(demuxer
);
1119 dts
= get_timestamp(demuxer
);
1122 printf("pts=%d dts=%d\n", pts
, dts
);
1125 * pts = (mpeg3io_read_char(title->fs) >> 1) & 7;
1127 * pts |= (mpeg3io_read_int16(title->fs) >> 1);
1129 * pts |= (mpeg3io_read_int16(title->fs) >> 1);
1133 * dts = (mpeg3io_read_char(title->fs) >> 1) & 7;
1135 * dts |= (mpeg3io_read_int16(title->fs) >> 1);
1137 * dts |= (mpeg3io_read_int16(title->fs) >> 1);
1139 pes_header_bytes
+= 10;
1142 //printf("get_program_pes_packet do_audio=%d do_video=%d pts=%x dts=%x\n",
1143 // demuxer->do_audio, demuxer->do_video, pts, dts);
1146 mpeg3io_seek_relative(title
->fs
,
1147 pes_header_data_length
- pes_header_bytes
);
1152 /* Get MPEG-1 packet */
1153 while(mpeg3io_next_char(title
->fs
) == 0xff)
1155 mpeg3io_read_char(title
->fs
);
1158 /* Skip STD buffer scale */
1159 if((mpeg3io_next_char(title
->fs
) & 0x40) == 0x40)
1161 mpeg3io_seek_relative(title
->fs
, 2);
1164 /* Decide which timestamps are available */
1165 pts_dts_flags
= mpeg3io_next_char(title
->fs
);
1167 if(pts_dts_flags
>= 0x30)
1169 /* Get the presentation and decoding time stamp */
1170 pts
= get_timestamp(demuxer
);
1171 dts
= get_timestamp(demuxer
);
1174 if(pts_dts_flags
>= 0x20)
1176 /* Get just the presentation time stamp */
1177 pts
= get_timestamp(demuxer
);
1180 if(pts_dts_flags
== 0x0f)
1182 /* End of timestamps */
1183 mpeg3io_read_char(title
->fs
);
1187 return 1; /* Error */
1191 /* Now extract the payload. */
1192 if((demuxer
->stream_id
>> 4) == 0xc || (demuxer
->stream_id
>> 4) == 0xd)
1195 /* Take first stream ID if -1 */
1196 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1198 demuxer
->got_audio
= 1;
1199 demuxer
->custom_id
= demuxer
->stream_id
& 0x0f;
1202 if(demuxer
->read_all
)
1203 demuxer
->astream_table
[demuxer
->custom_id
] = AUDIO_MPEG
;
1205 if(demuxer
->astream
== -1)
1206 demuxer
->astream
= demuxer
->custom_id
;
1211 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
1214 if(demuxer
->custom_id
== demuxer
->astream
&&
1215 demuxer
->do_audio
||
1218 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1221 printf(" MP2 audio data offset=%llx custom_id=%x size=%x\n",
1222 demuxer
->program_byte
,
1227 get_program_payload(demuxer
, pes_packet_length
, 1, 0);
1231 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1235 if((demuxer
->stream_id
>> 4) == 0xe)
1238 /* Take first stream ID if -1 */
1239 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1241 demuxer
->got_video
= 1;
1242 demuxer
->custom_id
= demuxer
->stream_id
& 0x0f;
1245 if(demuxer
->read_all
)
1246 demuxer
->vstream_table
[demuxer
->custom_id
] = 1;
1248 if(demuxer
->vstream
== -1)
1249 demuxer
->vstream
= demuxer
->custom_id
;
1251 if(pts
> 0) demuxer
->pes_video_time
= (double)pts
/ 60000;
1255 if(demuxer
->custom_id
== demuxer
->vstream
&&
1256 demuxer
->do_video
||
1259 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1262 printf(" video offset=%llx custom_id=%x size=%x\n",
1263 demuxer
->program_byte
,
1269 get_program_payload(demuxer
,
1278 printf(" skipping video size=%x\n", pes_packet_length
);
1280 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1284 if((demuxer
->stream_id
== 0xbd || demuxer
->stream_id
== 0xbf) &&
1285 mpeg3io_next_char(title
->fs
) != 0xff &&
1286 ((mpeg3io_next_char(title
->fs
) & 0xf0) == 0x20))
1288 /* DVD subtitle data */
1289 mpeg3_subtitle_t
*subtitle
= 0;
1290 int stream_id
= demuxer
->stream_id
= mpeg3io_read_char(title
->fs
);
1291 subtitle
= new_subtitle(demuxer
,
1293 demuxer
->program_byte
);
1295 /* Get data length */
1296 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1298 handle_subtitle(file
, demuxer
, subtitle
, pes_packet_length
);
1299 //printf("mpeg3_demux id=0x%02x size=%d total_size=%d\n", stream_id, pes_packet_length, subtitle->size);
1303 if((demuxer
->stream_id
== 0xbd || demuxer
->stream_id
== 0xbf) &&
1304 mpeg3io_next_char(title
->fs
) != 0xff &&
1305 ((mpeg3io_next_char(title
->fs
) & 0xf0) == 0x80))
1308 /* DVD audio data */
1309 /* Get the audio format */
1310 if((mpeg3io_next_char(title
->fs
) & 0xf0) == 0xa0)
1316 // Picks up bogus data if (& 0xf) or (& 0x7f)
1317 demuxer
->stream_id
= mpeg3io_next_char(title
->fs
);
1318 if(pts
> 0) demuxer
->pes_audio_time
= (double)pts
/ 60000;
1320 demuxer
->got_audio
= 1;
1321 demuxer
->custom_id
= demuxer
->stream_id
;
1324 /* Take first stream ID if not building TOC. */
1325 if(demuxer
->read_all
)
1326 demuxer
->astream_table
[demuxer
->custom_id
] = format
;
1328 if(demuxer
->astream
== -1)
1329 demuxer
->astream
= demuxer
->custom_id
;
1337 if(demuxer
->custom_id
== demuxer
->astream
&&
1338 demuxer
->do_audio
||
1341 demuxer
->aformat
= format
;
1342 mpeg3io_read_int32(title
->fs
);
1344 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1345 decryption_offset
= mpeg3io_tell(title
->fs
) - demuxer
->last_packet_start
;
1347 if(format
== AUDIO_PCM
) do_pcm
= 1;
1348 //printf("get_program_pes_packet 5 %x\n", decryption_offset);
1352 printf(" AC3 audio data size=%x\n", pes_packet_length
);
1355 get_program_payload(demuxer
,
1362 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1363 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1365 //printf("get_program_pes_packet 6 %d\n", demuxer->astream_table[0x20]);
1368 if(demuxer
->stream_id
== 0xbc || 1)
1370 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1371 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1375 if(demuxer
->stream_id
== MPEG3_PRIVATE_STREAM_2
|| demuxer
->stream_id
== MPEG3_PADDING_STREAM
)
1377 pes_packet_length
-= mpeg3io_tell(title
->fs
) - pes_packet_start
;
1378 mpeg3io_seek_relative(title
->fs
, pes_packet_length
);
1384 if(scrambling
) handle_scrambling(demuxer
, decryption_offset
);
1388 if(do_pcm
) handle_pcm(demuxer
, pes_packet_length
);
1400 int mpeg3demux_read_program(mpeg3_demuxer_t
*demuxer
)
1404 mpeg3_t
*file
= demuxer
->file
;
1405 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1406 unsigned int header
= 0;
1409 demuxer
->got_audio
= 0;
1410 demuxer
->got_video
= 0;
1411 demuxer
->stream_id
= 0;
1412 demuxer
->custom_id
= -1;
1413 demuxer
->got_subtitle
= 0;
1415 if(mpeg3io_eof(title
->fs
)) return 1;
1417 //printf("mpeg3demux_read_program 2 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
1425 /* Search for next header */
1426 /* Parse packet until the next packet start code */
1427 while(!result
&& !mpeg3io_eof(title
->fs
))
1429 header
= mpeg3io_read_int32(title
->fs
);
1432 if(header
== MPEG3_PACK_START_CODE
)
1436 // Second start code in this call. Don't read it.
1439 mpeg3io_seek_relative(title
->fs
, -4);
1443 demuxer
->last_packet_start
= mpeg3io_tell(title
->fs
) - 4;
1444 result
= get_pack_header(demuxer
);
1451 if(header
== MPEG3_SYSTEM_START_CODE
&& pack_count
)
1456 result
= get_system_header(demuxer
);
1462 if((header
>> 8) == MPEG3_PACKET_START_CODE_PREFIX
&& pack_count
)
1466 result
= get_program_pes_packet(demuxer
, header
);
1474 mpeg3io_seek_relative(title
->fs
, -3);
1484 // Ignore errors in the parsing. Just quit if eof.
1492 demuxer
->last_packet_end
= mpeg3io_tell(title
->fs
);
1495 * if(demuxer->last_packet_end - demuxer->last_packet_start != 0x800)
1496 * printf(__FUNCTION__ " packet_size=%llx data_size=%x pack_count=%d packet_start=%llx packet_end=%llx\n",
1497 * demuxer->last_packet_end - demuxer->last_packet_start,
1498 * demuxer->data_size,
1500 * demuxer->last_packet_start,
1501 * demuxer->last_packet_end);
1503 //printf("mpeg3demux_read_program 5 %d\n", result);
1505 //printf("read_program 3\n");
1506 // if(!result) result = mpeg3io_eof(title->fs);
1508 demuxer
->program_byte
=
1509 mpeg3_absolute_to_program(demuxer
, mpeg3io_tell(title
->fs
) +
1517 // Point the current title and current cell to the program byte.
1518 static int get_current_cell(mpeg3_demuxer_t
*demuxer
)
1523 /* Find first cell on or after current position */
1524 if(demuxer
->reverse
)
1526 for(demuxer
->current_title
= demuxer
->total_titles
- 1;
1527 demuxer
->current_title
>= 0;
1528 demuxer
->current_title
--)
1530 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1531 for(demuxer
->title_cell
= title
->cell_table_size
- 1;
1532 demuxer
->title_cell
>= 0;
1533 demuxer
->title_cell
--)
1535 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1536 if(cell
->program_start
< demuxer
->program_byte
&&
1537 cell
->program
== demuxer
->current_program
)
1540 if(demuxer
->program_byte
> cell
->program_end
)
1541 demuxer
->program_byte
= cell
->program_end
;
1550 demuxer
->current_title
= 0;
1551 demuxer
->title_cell
= 0;
1557 for(demuxer
->current_title
= 0;
1558 demuxer
->current_title
< demuxer
->total_titles
;
1559 demuxer
->current_title
++)
1561 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1562 for(demuxer
->title_cell
= 0;
1563 demuxer
->title_cell
< title
->cell_table_size
;
1564 demuxer
->title_cell
++)
1566 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1567 if(cell
->program_end
> demuxer
->program_byte
&&
1568 cell
->program
== demuxer
->current_program
)
1571 if(demuxer
->program_byte
< cell
->program_start
)
1572 demuxer
->program_byte
= cell
->program_start
;
1581 demuxer
->current_title
= demuxer
->total_titles
- 1;
1582 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1583 demuxer
->title_cell
= title
->cell_table_size
- 1;
1589 //printf("get_current_cell 2 %p %d %d\n", demuxer, demuxer->current_title, demuxer->title_cell);
1597 int mpeg3_seek_phys(mpeg3_demuxer_t
*demuxer
)
1601 // This happens if the video header is bigger than the buffer size in a table
1602 // of contents scan.
1603 if(demuxer
->current_title
< 0 ||
1604 demuxer
->current_title
>= demuxer
->total_titles
)
1606 printf("mpeg3_seek_phys demuxer=%p read_all=%d do_audio=%d do_video=%d demuxer->current_title=%d\n",
1611 demuxer
->current_title
);
1615 if(!demuxer
->titles
) return 1;
1617 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1618 //printf("mpeg3_seek_phys: %p %d %d\n", title->cell_table, demuxer->current_title, demuxer->title_cell);
1620 if(!title
->cell_table
) return 1;
1622 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
1625 /* Don't do anything if we're in the cell and it's the right program */
1626 if(demuxer
->reverse
)
1628 if(demuxer
->program_byte
> cell
->program_start
&&
1629 demuxer
->program_byte
<= cell
->program_end
&&
1630 cell
->program
== demuxer
->current_program
)
1638 //printf("mpeg3_seek_phys %lld %lld\n", demuxer->program_byte, demuxer->stream_end);
1639 if(demuxer
->stream_end
> 0 &&
1640 demuxer
->program_byte
>= demuxer
->stream_end
) return 1;
1643 if(demuxer
->program_byte
>= cell
->program_start
&&
1644 demuxer
->program_byte
< cell
->program_end
&&
1645 cell
->program
== demuxer
->current_program
)
1651 // Need to change cells if we get here.
1652 int last_cell
= demuxer
->title_cell
;
1653 int last_title
= demuxer
->current_title
;
1654 int64_t last_byte
= demuxer
->program_byte
;
1657 result
= get_current_cell(demuxer
);
1659 if(result
) return 1;
1661 if(demuxer
->current_title
!= last_title
)
1663 mpeg3demux_open_title(demuxer
, demuxer
->current_title
);
1666 title
= demuxer
->titles
[demuxer
->current_title
];
1667 cell
= &title
->cell_table
[demuxer
->title_cell
];
1671 mpeg3io_seek(title
->fs
,
1672 demuxer
->program_byte
- cell
->program_start
+ cell
->title_start
);
1683 static int next_code(mpeg3_demuxer_t
*demuxer
,
1686 uint32_t result
= 0;
1688 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1689 mpeg3_fs_t
*fd
= title
->fs
;
1691 while(result
!= code
&&
1694 title
= demuxer
->titles
[demuxer
->current_title
];
1696 result
|= (unsigned char)mpeg3io_read_char(title
->fs
);
1697 demuxer
->program_byte
++;
1698 error
= mpeg3_seek_phys(demuxer
);
1712 /* Read packet in the forward direction */
1713 int mpeg3_read_next_packet(mpeg3_demuxer_t
*demuxer
)
1715 if(demuxer
->current_title
< 0) return 1;
1718 int current_position
;
1719 mpeg3_t
*file
= demuxer
->file
;
1720 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1723 /* Reset output buffer */
1724 demuxer
->data_size
= 0;
1725 demuxer
->data_position
= 0;
1726 demuxer
->audio_size
= 0;
1727 demuxer
->video_size
= 0;
1729 /* Switch to forward direction. */
1730 if(demuxer
->reverse
)
1732 /* Don't reread anything if we're at the beginning of the file. */
1733 if(demuxer
->program_byte
< 0)
1735 demuxer
->program_byte
= 0;
1736 result
= mpeg3_seek_phys(demuxer
);
1737 /* First character was the -1 byte which brings us to 0 after this function. */
1741 /* Transport or elementary stream */
1742 if(file
->packet_size
> 0)
1744 demuxer
->program_byte
+= file
->packet_size
;
1745 result
= mpeg3_seek_phys(demuxer
);
1749 /* Packet just read */
1750 if(!result
) result
= next_code(demuxer
,
1751 MPEG3_PACK_START_CODE
);
1753 if(!result
) result
= next_code(demuxer
,
1754 MPEG3_PACK_START_CODE
);
1757 demuxer
->reverse
= 0;
1766 /* Read packets until the output buffer is full. */
1767 /* Read a single packet if not fetching audio or video. */
1772 title
= demuxer
->titles
[demuxer
->current_title
];
1773 //printf("mpeg3_read_next_packet 10 %llx\n", mpeg3io_tell(title->fs));
1777 if(file
->is_transport_stream
)
1779 result
= mpeg3_seek_phys(demuxer
);
1780 if(!result
) result
= read_transport(demuxer
);
1783 if(file
->is_program_stream
)
1785 result
= mpeg3_seek_phys(demuxer
);
1786 if(!result
) result
= mpeg3demux_read_program(demuxer
);
1789 if(demuxer
->read_all
&& file
->is_audio_stream
)
1791 /* Read elementary stream. */
1792 result
= mpeg3io_read_data(demuxer
->audio_buffer
,
1793 file
->packet_size
, title
->fs
);
1794 demuxer
->audio_size
= file
->packet_size
;
1795 demuxer
->program_byte
+= file
->packet_size
;
1796 result
|= mpeg3_seek_phys(demuxer
);
1799 if(demuxer
->read_all
&& file
->is_video_stream
)
1801 /* Read elementary stream. */
1802 result
= mpeg3io_read_data(demuxer
->video_buffer
,
1803 file
->packet_size
, title
->fs
);
1804 demuxer
->video_size
= file
->packet_size
;
1805 demuxer
->program_byte
+= file
->packet_size
;
1806 result
|= mpeg3_seek_phys(demuxer
);
1810 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1811 file
->packet_size
, title
->fs
);
1812 demuxer
->data_size
= file
->packet_size
;
1813 demuxer
->program_byte
+= file
->packet_size
;
1814 //printf("mpeg3_read_next_packet 1 %llx\n", mpeg3demux_tell_byte(demuxer));
1815 result
|= mpeg3_seek_phys(demuxer
);
1816 //printf("mpeg3_read_next_packet 2 %llx\n", mpeg3demux_tell_byte(demuxer));
1820 * printf("mpeg3_read_next_packet 100 result=%d data_size=0x%x\n",
1822 * demuxer->data_size);
1825 demuxer
->data_size
== 0 &&
1826 (demuxer
->do_audio
|| demuxer
->do_video
));
1835 static int prev_code(mpeg3_demuxer_t
*demuxer
,
1838 uint32_t result
= 0;
1840 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1841 mpeg3_fs_t
*fd
= title
->fs
;
1844 while(result
!= code
&&
1845 demuxer
->program_byte
> 0 &&
1849 title
= demuxer
->titles
[demuxer
->current_title
];
1850 mpeg3io_seek(title
->fs
, demuxer
->program_byte
- title
->start_byte
- 1LL);
1851 result
|= ((uint32_t)mpeg3io_read_char(title
->fs
)) << 24;
1852 demuxer
->program_byte
--;
1853 error
= mpeg3_seek_phys(demuxer
);
1862 /* Read the packet right before the packet we're currently on. */
1863 int mpeg3_read_prev_packet(mpeg3_demuxer_t
*demuxer
)
1866 mpeg3_t
*file
= demuxer
->file
;
1867 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
1869 demuxer
->data_size
= 0;
1870 demuxer
->data_position
= 0;
1874 /* Switch to reverse direction */
1875 if(!demuxer
->reverse
)
1877 demuxer
->reverse
= 1;
1879 /* Transport stream or elementary stream case */
1880 if(file
->packet_size
> 0)
1882 demuxer
->program_byte
-= file
->packet_size
;
1883 result
= mpeg3_seek_phys(demuxer
);
1886 /* Program stream */
1888 result
= prev_code(demuxer
,
1889 MPEG3_PACK_START_CODE
);
1900 /* Go to beginning of previous packet */
1903 title
= demuxer
->titles
[demuxer
->current_title
];
1905 /* Transport stream or elementary stream case */
1906 if(file
->packet_size
> 0)
1908 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
);
1909 demuxer
->program_byte
-= file
->packet_size
;
1910 result
= mpeg3_seek_phys(demuxer
);
1911 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
);
1915 if(!result
) result
= prev_code(demuxer
,
1916 MPEG3_PACK_START_CODE
);
1921 /* Read packet and then rewind it */
1922 title
= demuxer
->titles
[demuxer
->current_title
];
1923 if(file
->is_transport_stream
&& !result
)
1925 result
= read_transport(demuxer
);
1927 if(demuxer
->program_byte
> 0)
1929 demuxer
->program_byte
-= file
->packet_size
;
1930 result
= mpeg3_seek_phys(demuxer
);
1934 if(file
->is_program_stream
&& !result
)
1936 int64_t current_position
= demuxer
->program_byte
;
1939 result
= mpeg3demux_read_program(demuxer
);
1942 while(demuxer
->program_byte
> current_position
&&
1945 result
= prev_code(demuxer
,
1946 MPEG3_PACK_START_CODE
);
1952 /* Elementary stream */
1953 /* Read the packet forwards and seek back to the start */
1954 result
= mpeg3io_read_data(demuxer
->data_buffer
,
1960 demuxer
->data_size
= file
->packet_size
;
1961 result
= mpeg3io_seek(title
->fs
, demuxer
->program_byte
);
1965 demuxer
->data_size
== 0 &&
1966 (demuxer
->do_audio
|| demuxer
->do_video
));
1974 int mpeg3demux_read_data(mpeg3_demuxer_t
*demuxer
,
1975 unsigned char *output
,
1979 demuxer
->error_flag
= 0;
1980 //printf("mpeg3demux_read_data 1\n");
1982 if(demuxer
->data_position
>= 0)
1986 for(i
= 0; i
< size
&& !result
; )
1988 int fragment_size
= size
- i
;
1989 if(fragment_size
> demuxer
->data_size
- demuxer
->data_position
)
1990 fragment_size
= demuxer
->data_size
- demuxer
->data_position
;
1991 memcpy(output
+ i
, demuxer
->data_buffer
+ demuxer
->data_position
, fragment_size
);
1992 demuxer
->data_position
+= fragment_size
;
1997 result
= mpeg3_read_next_packet(demuxer
);
2000 * printf("mpeg3demux_read_data 10 offset=%llx pid=0x%x bytes=0x%x i=0x%x\n",
2001 * mpeg3demux_tell_byte(demuxer),
2003 * demuxer->data_size,
2007 //printf("mpeg3demux_read_data 10\n");
2011 int current_position
;
2012 /* Read backwards a full packet. */
2013 /* Only good for reading less than the size of a full packet, but */
2014 /* this routine should only be used for searching for previous markers. */
2015 current_position
= demuxer
->data_position
;
2016 result
= mpeg3_read_prev_packet(demuxer
);
2017 if(!result
) demuxer
->data_position
= demuxer
->data_size
+ current_position
;
2018 memcpy(output
, demuxer
->data_buffer
+ demuxer
->data_position
, size
);
2019 demuxer
->data_position
+= size
;
2022 //printf("mpeg3demux_read_data 2\n");
2023 demuxer
->error_flag
= result
;
2027 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t
*demuxer
)
2029 demuxer
->error_flag
= 0;
2030 demuxer
->next_char
= -1;
2032 if(demuxer
->data_position
>= demuxer
->data_size
)
2034 demuxer
->error_flag
= mpeg3_read_next_packet(demuxer
);
2037 if(!demuxer
->error_flag
)
2038 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
++];
2040 return demuxer
->next_char
;
2043 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t
*demuxer
)
2045 demuxer
->error_flag
= 0;
2046 demuxer
->data_position
--;
2047 if(demuxer
->data_position
< 0)
2049 demuxer
->error_flag
= mpeg3_read_prev_packet(demuxer
);
2050 if(!demuxer
->error_flag
) demuxer
->data_position
= demuxer
->data_size
- 1;
2053 if(demuxer
->data_position
>= 0)
2055 demuxer
->next_char
= demuxer
->data_buffer
[demuxer
->data_position
];
2057 return demuxer
->next_char
;
2062 int mpeg3demux_open_title(mpeg3_demuxer_t
*demuxer
, int title_number
)
2064 mpeg3_title_t
*title
;
2066 if(title_number
< demuxer
->total_titles
&&
2069 if(demuxer
->current_title
>= 0)
2071 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
2072 demuxer
->current_title
= -1;
2075 title
= demuxer
->titles
[title_number
];
2077 if(mpeg3io_open_file(title
->fs
))
2079 demuxer
->error_flag
= 1;
2080 fprintf(stderr
, "mpeg3demux_open_title %s: %s", title
->fs
->path
, strerror(errno
));
2084 demuxer
->current_title
= title_number
;
2089 fprintf(stderr
, "mpeg3demux_open_title title_number = %d\n",
2094 return demuxer
->error_flag
;
2097 int mpeg3demux_copy_titles(mpeg3_demuxer_t
*dst
, mpeg3_demuxer_t
*src
)
2100 mpeg3_t
*file
= dst
->file
;
2101 mpeg3_title_t
*dst_title
, *src_title
;
2103 dst
->total_titles
= src
->total_titles
;
2104 dst
->total_programs
= src
->total_programs
;
2106 for(i
= 0; i
< MPEG3_MAX_STREAMS
; i
++)
2108 dst
->astream_table
[i
] = src
->astream_table
[i
];
2109 dst
->vstream_table
[i
] = src
->vstream_table
[i
];
2112 for(i
= 0; i
< src
->total_titles
; i
++)
2114 src_title
= src
->titles
[i
];
2115 dst_title
= dst
->titles
[i
] = mpeg3_new_title(file
,
2116 src
->titles
[i
]->fs
->path
);
2117 mpeg3_copy_title(dst_title
, src_title
);
2120 mpeg3demux_open_title(dst
, src
->current_title
);
2121 dst
->title_cell
= 0;
2125 /* ==================================================================== */
2127 /* ==================================================================== */
2129 mpeg3_demuxer_t
* mpeg3_new_demuxer(mpeg3_t
*file
, int do_audio
, int do_video
, int custom_id
)
2131 mpeg3_demuxer_t
*demuxer
= calloc(1, sizeof(mpeg3_demuxer_t
));
2134 /* The demuxer will change the default packet size for its own use. */
2135 demuxer
->file
= file
;
2136 demuxer
->do_audio
= do_audio
;
2137 demuxer
->do_video
= do_video
;
2139 /* Allocate buffer + padding */
2140 demuxer
->raw_data
= calloc(1, MPEG3_RAW_SIZE
);
2141 demuxer
->data_buffer
= calloc(1, MPEG3_RAW_SIZE
);
2142 demuxer
->data_allocated
= MPEG3_RAW_SIZE
;
2144 demuxer
->audio_buffer
= calloc(1, MPEG3_RAW_SIZE
);
2145 demuxer
->audio_allocated
= MPEG3_RAW_SIZE
;
2147 demuxer
->video_buffer
= calloc(1, MPEG3_RAW_SIZE
);
2148 demuxer
->video_allocated
= MPEG3_RAW_SIZE
;
2150 /* System specific variables */
2151 demuxer
->audio_pid
= custom_id
;
2152 demuxer
->video_pid
= custom_id
;
2153 demuxer
->astream
= custom_id
;
2154 demuxer
->vstream
= custom_id
;
2155 demuxer
->current_title
= -1;
2156 demuxer
->pes_audio_time
= -1;
2157 demuxer
->pes_video_time
= -1;
2158 //printf("mpeg3_new_demuxer %f\n", demuxer->time);
2159 demuxer
->stream_end
= -1;
2164 int mpeg3_delete_demuxer(mpeg3_demuxer_t
*demuxer
)
2168 if(demuxer
->current_title
>= 0)
2170 mpeg3io_close_file(demuxer
->titles
[demuxer
->current_title
]->fs
);
2173 for(i
= 0; i
< demuxer
->total_titles
; i
++)
2175 mpeg3_delete_title(demuxer
->titles
[i
]);
2178 if(demuxer
->data_buffer
) free(demuxer
->data_buffer
);
2179 if(demuxer
->raw_data
) free(demuxer
->raw_data
);
2180 if(demuxer
->audio_buffer
) free(demuxer
->audio_buffer
);
2181 if(demuxer
->video_buffer
) free(demuxer
->video_buffer
);
2182 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
2184 mpeg3_delete_subtitle(demuxer
->subtitles
[i
]);
2186 if(demuxer
->subtitles
) free(demuxer
->subtitles
);
2192 int mpeg3demux_eof(mpeg3_demuxer_t
*demuxer
)
2194 mpeg3_t
*file
= demuxer
->file
;
2197 if(demuxer
->current_title
>= 0)
2199 if(mpeg3io_eof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
2200 demuxer
->current_title
>= demuxer
->total_titles
- 1)
2204 // Same operation performed in mpeg3_seek_phys
2205 if(demuxer
->stream_end
> 0 &&
2206 demuxer
->program_byte
>= demuxer
->stream_end
) return 1;
2210 if(demuxer
->data_position
>= demuxer
->data_size
) return 1;
2216 int mpeg3demux_bof(mpeg3_demuxer_t
*demuxer
)
2218 if(demuxer
->current_title
>= 0)
2220 if(mpeg3io_bof(demuxer
->titles
[demuxer
->current_title
]->fs
) &&
2221 demuxer
->current_title
<= 0)
2227 void mpeg3demux_start_reverse(mpeg3_demuxer_t
*demuxer
)
2229 demuxer
->reverse
= 1;
2232 void mpeg3demux_start_forward(mpeg3_demuxer_t
*demuxer
)
2234 demuxer
->reverse
= 0;
2237 /* Seek to absolute byte */
2238 int mpeg3demux_seek_byte(mpeg3_demuxer_t
*demuxer
, int64_t byte
)
2240 mpeg3_t
*file
= demuxer
->file
;
2244 * for(i = 0; i < demuxer->total_titles; i++) mpeg3_dump_title(demuxer->titles[i]);
2247 demuxer
->program_byte
= byte
;
2248 demuxer
->data_position
= 0;
2249 demuxer
->data_size
= 0;
2251 /* Get on a packet boundary only for transport streams. */
2252 if(file
->is_transport_stream
&&
2255 demuxer
->program_byte
-= demuxer
->program_byte
% file
->packet_size
;
2258 int result
= mpeg3_seek_phys(demuxer
);
2260 * printf("mpeg3demux_seek_byte 1 %d %d %lld %lld\n",
2261 * demuxer->do_video, result, byte, demuxer->program_byte);
2268 int mpeg3_finished_subtitles(mpeg3_demuxer_t
*demuxer
, int id
)
2272 for(i
= 0; i
< demuxer
->total_subtitles
; i
++)
2274 if(demuxer
->subtitles
[i
]->done
&&
2276 demuxer
->subtitles
[i
]->id
== id
)) total
++;
2287 double mpeg3demux_get_time(mpeg3_demuxer_t
*demuxer
)
2289 return demuxer
->time
;
2292 double mpeg3demux_audio_pts(mpeg3_demuxer_t
*demuxer
)
2294 return demuxer
->pes_audio_time
;
2297 double mpeg3demux_video_pts(mpeg3_demuxer_t
*demuxer
)
2299 return demuxer
->pes_video_time
;
2302 void mpeg3demux_reset_pts(mpeg3_demuxer_t
*demuxer
)
2304 demuxer
->pes_audio_time
= -1;
2305 demuxer
->pes_video_time
= -1;
2308 double mpeg3demux_scan_pts(mpeg3_demuxer_t
*demuxer
)
2310 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
2311 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
2312 int64_t current_position
= start_position
;
2315 mpeg3demux_reset_pts(demuxer
);
2317 current_position
< end_position
&&
2318 ((demuxer
->do_audio
&& demuxer
->pes_audio_time
< 0) ||
2319 (demuxer
->do_video
&& demuxer
->pes_video_time
< 0)))
2321 result
= mpeg3_read_next_packet(demuxer
);
2322 current_position
= mpeg3demux_tell_byte(demuxer
);
2325 // Seek back to starting point
2326 mpeg3demux_seek_byte(demuxer
, start_position
);
2328 if(demuxer
->do_audio
) return demuxer
->pes_audio_time
;
2329 if(demuxer
->do_video
) return demuxer
->pes_video_time
;
2332 int mpeg3demux_goto_pts(mpeg3_demuxer_t
*demuxer
, double pts
)
2334 int64_t start_position
= mpeg3demux_tell_byte(demuxer
);
2335 int64_t end_position
= start_position
+ MPEG3_PTS_RANGE
;
2336 int64_t current_position
= start_position
;
2339 // Search forward for nearest pts
2340 mpeg3demux_reset_pts(demuxer
);
2341 while(!result
&& current_position
< end_position
)
2343 result
= mpeg3_read_next_packet(demuxer
);
2344 if(demuxer
->pes_audio_time
> pts
) break;
2345 current_position
= mpeg3demux_tell_byte(demuxer
);
2348 // Search backward for nearest pts
2349 end_position
= current_position
- MPEG3_PTS_RANGE
;
2350 mpeg3_read_prev_packet(demuxer
);
2352 current_position
> end_position
)
2354 result
= mpeg3_read_prev_packet(demuxer
);
2355 if(demuxer
->pes_audio_time
< pts
) break;
2356 current_position
= mpeg3demux_tell_byte(demuxer
);
2361 int64_t mpeg3demux_tell_byte(mpeg3_demuxer_t
*demuxer
)
2363 return demuxer
->program_byte
;
2366 int mpeg3demux_tell_program(mpeg3_demuxer_t
*demuxer
)
2368 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2369 if(!title
->cell_table
|| !title
->cell_table_size
) return 0;
2370 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
2371 return cell
->program
;
2374 int64_t mpeg3_absolute_to_program(mpeg3_demuxer_t
*demuxer
,
2379 //fprintf(stderr, "%d\n", demuxer->data_size);
2380 // Can only offset to current cell since we can't know what cell the
2381 // byte corresponds to.
2382 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2383 mpeg3_cell_t
*cell
= &title
->cell_table
[demuxer
->title_cell
];
2387 cell
->program_start
;
2390 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t
*demuxer
)
2392 if(!demuxer
->total_bytes
)
2396 for(i
= 0; i
< demuxer
->total_titles
; i
++)
2398 mpeg3_title_t
*title
= demuxer
->titles
[i
];
2399 for(j
= 0; j
< title
->cell_table_size
; j
++)
2401 mpeg3_cell_t
*cell
= &title
->cell_table
[j
];
2402 if(cell
->program
== demuxer
->current_program
)
2403 result
+= cell
->program_end
- cell
->program_start
;
2405 // result += demuxer->titles[i]->total_bytes;
2407 demuxer
->total_bytes
= result
;
2409 return demuxer
->total_bytes
;
2412 int64_t mpeg3demuxer_title_bytes(mpeg3_demuxer_t
*demuxer
)
2414 mpeg3_title_t
*title
= demuxer
->titles
[demuxer
->current_title
];
2415 return title
->total_bytes
;
2418 mpeg3_demuxer_t
* mpeg3_get_demuxer(mpeg3_t
*file
)
2420 if(file
->is_program_stream
|| file
->is_transport_stream
)
2422 if(file
->total_astreams
) return file
->atrack
[0]->demuxer
;
2424 if(file
->total_vstreams
) return file
->vtrack
[0]->demuxer
;
2429 void mpeg3demux_append_data(mpeg3_demuxer_t
*demuxer
,
2430 unsigned char *data
,
2433 int new_data_size
= demuxer
->data_size
+ bytes
;
2434 if(new_data_size
>= demuxer
->data_allocated
)
2436 demuxer
->data_allocated
= new_data_size
* 2;
2437 demuxer
->data_buffer
= realloc(demuxer
->data_buffer
,
2438 demuxer
->data_allocated
);
2441 memcpy(demuxer
->data_buffer
+ demuxer
->data_size
,
2444 demuxer
->data_size
+= bytes
;
2447 void mpeg3demux_shift_data(mpeg3_demuxer_t
*demuxer
,
2451 for(i
= 0, j
= bytes
; j
< demuxer
->data_size
; i
++, j
++)
2453 demuxer
->data_buffer
[i
] = demuxer
->data_buffer
[j
];
2455 demuxer
->data_size
-= bytes
;
2456 demuxer
->data_position
-= bytes
;