r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / libmpeg3 / mpeg3demux.c
blob46a61b7e2de13a3d7231f94127f5de8c86aaf89d
1 #include "libmpeg3.h"
2 #include "mpeg3io.h"
3 #include "mpeg3protos.h"
4 #include "workarounds.h"
6 #include <errno.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <string.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];
20 /* Advance pointer */
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");
25 return result;
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;
36 return result;
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;
48 return result;
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;
60 return result;
63 static inline unsigned int packet_read_int32(mpeg3_demuxer_t *demuxer)
65 unsigned int a, b, c, d, result;
66 //printf(__FUNCTION__ " called\n");
67 a = demuxer->raw_data[demuxer->raw_offset++];
68 b = demuxer->raw_data[demuxer->raw_offset++];
69 c = demuxer->raw_data[demuxer->raw_offset++];
70 d = demuxer->raw_data[demuxer->raw_offset++];
71 result = (a << 24) | (b << 16) | (c << 8) | d;
73 return result;
76 static inline unsigned int packet_skip(mpeg3_demuxer_t *demuxer, long length)
78 //printf(__FUNCTION__ " called\n");
79 demuxer->raw_offset += length;
80 return 0;
83 static int get_adaptation_field(mpeg3_demuxer_t *demuxer)
85 long length;
86 int pcr_flag;
88 demuxer->adaptation_fields++;
89 /* get adaptation field length */
90 length = packet_read_char(demuxer);
92 if(length > 0)
94 /* get first byte */
95 pcr_flag = (packet_read_char(demuxer) >> 4) & 1;
97 if(pcr_flag)
99 unsigned long clk_ref_base = packet_read_int32(demuxer);
100 unsigned int clk_ref_ext = packet_read_int16(demuxer);
102 if (clk_ref_base > 0x7fffffff)
103 { /* correct for invalid numbers */
104 clk_ref_base = 0; /* ie. longer than 32 bits when multiplied by 2 */
105 clk_ref_ext = 0; /* multiplied by 2 corresponds to shift left 1 (<<=1) */
107 else
109 clk_ref_base <<= 1; /* Create space for bit */
110 clk_ref_base |= (clk_ref_ext >> 15); /* Take bit */
111 clk_ref_ext &= 0x01ff; /* Only lower 9 bits */
113 demuxer->time = ((double)clk_ref_base + clk_ref_ext / 300) / 90000;
114 if(length) packet_skip(demuxer, length - 7);
116 else
117 packet_skip(demuxer, length - 1);
120 return 0;
123 static int get_program_association_table(mpeg3_demuxer_t *demuxer)
125 demuxer->program_association_tables++;
126 demuxer->table_id = packet_read_char(demuxer);
127 demuxer->section_length = packet_read_int16(demuxer) & 0xfff;
128 demuxer->transport_stream_id = packet_read_int16(demuxer);
129 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
130 return 0;
133 static int get_data_buffer(mpeg3_demuxer_t *demuxer)
135 while(demuxer->raw_offset < demuxer->raw_size &&
136 demuxer->data_size < MPEG3_RAW_SIZE)
138 demuxer->data_buffer[demuxer->data_size++] =
139 demuxer->raw_data[demuxer->raw_offset++];
141 return 0;
144 static int get_pes_packet_header(mpeg3_demuxer_t *demuxer, unsigned long *pts, unsigned long *dts)
146 unsigned int pes_header_bytes = 0;
147 unsigned int pts_dts_flags;
148 int pes_header_data_length;
150 /* drop first 8 bits */
151 packet_read_char(demuxer);
152 pts_dts_flags = (packet_read_char(demuxer) >> 6) & 0x3;
153 pes_header_data_length = packet_read_char(demuxer);
156 /* Get Presentation Time stamps and Decoding Time Stamps */
157 if(pts_dts_flags == 2)
159 *pts = (packet_read_char(demuxer) >> 1) & 7; /* Only low 4 bits (7==1111) */
160 *pts <<= 15;
161 *pts |= (packet_read_int16(demuxer) >> 1);
162 *pts <<= 15;
163 *pts |= (packet_read_int16(demuxer) >> 1);
164 pes_header_bytes += 5;
166 else if(pts_dts_flags == 3)
168 *pts = (packet_read_char(demuxer) >> 1) & 7; /* Only low 4 bits (7==1111) */
169 *pts <<= 15;
170 *pts |= (packet_read_int16(demuxer) >> 1);
171 *pts <<= 15;
172 *pts |= (packet_read_int16(demuxer) >> 1);
173 *dts = (packet_read_char(demuxer) >> 1) & 7; /* Only low 4 bits (7==1111) */
174 *dts <<= 15;
175 *dts |= (packet_read_int16(demuxer) >> 1);
176 *dts <<= 15;
177 *dts |= (packet_read_int16(demuxer) >> 1);
178 pes_header_bytes += 10;
181 demuxer->time = (double)*pts / 90000;
182 //printf("get_pes_packet_header %f\n", demuxer->time);
184 /* extract other stuff here! */
185 packet_skip(demuxer, pes_header_data_length - pes_header_bytes);
186 return 0;
189 static int get_unknown_data(mpeg3_demuxer_t *demuxer)
191 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
192 return 0;
195 // Combine the pid and the stream id into one unit
196 #define CUSTOM_ID(pid, stream_id) (((pid << 8) | stream_id) & 0xffff)
199 static int get_pes_packet_data(mpeg3_demuxer_t *demuxer, unsigned int stream_id)
201 unsigned long pts = 0, dts = 0;
202 get_pes_packet_header(demuxer, &pts, &dts);
204 //printf("get_pes_packet_data %x\n", CUSTOM_ID(demuxer->pid, stream_id));
205 if(stream_id == 0xbd)
207 // Don't know if the next byte is the true stream id like in program stream
208 stream_id = 0x0;
210 if(demuxer->read_all)
211 demuxer->astream_table[CUSTOM_ID(demuxer->pid, stream_id)] = AUDIO_AC3;
212 if(demuxer->astream == -1)
213 demuxer->astream = CUSTOM_ID(demuxer->pid, stream_id);
215 if(CUSTOM_ID(demuxer->pid, stream_id) == demuxer->astream &&
216 demuxer->do_audio)
218 demuxer->pes_audio_time = (double)pts / 90000;
219 demuxer->audio_pid = demuxer->pid;
220 return get_data_buffer(demuxer);
223 else
224 if((stream_id >> 4) == 12 || (stream_id >> 4) == 13)
226 /* Just pick the first available stream if no ID is set */
227 if(demuxer->read_all)
228 demuxer->astream_table[CUSTOM_ID(demuxer->pid, stream_id)] = AUDIO_MPEG;
229 if(demuxer->astream == -1)
230 demuxer->astream = CUSTOM_ID(demuxer->pid, stream_id);
232 if(CUSTOM_ID(demuxer->pid, stream_id) == demuxer->astream &&
233 demuxer->do_audio)
235 demuxer->pes_audio_time = (double)pts / 90000;
236 demuxer->audio_pid = demuxer->pid;
238 return get_data_buffer(demuxer);
241 else
242 if((stream_id >> 4) == 14)
244 //printf("get_pes_packet_data video %x\n", stream_id);
245 /* Just pick the first available stream if no ID is set */
246 if(demuxer->read_all)
247 demuxer->vstream_table[CUSTOM_ID(demuxer->pid, stream_id)] = 1;
248 else
249 if(demuxer->vstream == -1)
250 demuxer->vstream = (CUSTOM_ID(demuxer->pid, stream_id));
253 //printf("get_pes_packet_data %x %d\n", stream_id, demuxer->payload_unit_start_indicator);
254 if(CUSTOM_ID(demuxer->pid, stream_id) == demuxer->vstream &&
255 demuxer->do_video)
257 demuxer->pes_video_time = (double)pts / 90000;
258 demuxer->video_pid = demuxer->pid;
260 * printf("get_pes_packet_data video %04x %llx\n",
261 * stream_id,
262 * mpeg3io_tell(demuxer->titles[demuxer->current_title]->fs));
263 * int i;
264 * for(i = demuxer->raw_offset; i < demuxer->raw_size; i++)
265 * printf("%02x ", demuxer->raw_data[i], stdout);
266 * printf("\n");
270 return get_data_buffer(demuxer);
273 else
275 return get_unknown_data(demuxer);
278 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
280 return 0;
283 static int get_pes_packet(mpeg3_demuxer_t *demuxer)
285 unsigned int stream_id;
287 demuxer->pes_packets++;
291 /* Skip startcode */
292 packet_read_int24(demuxer);
293 stream_id = packet_read_char(demuxer);
297 /* Skip pes packet length */
298 packet_read_int16(demuxer);
300 if(stream_id != MPEG3_PRIVATE_STREAM_2 &&
301 stream_id != MPEG3_PADDING_STREAM)
303 return get_pes_packet_data(demuxer, stream_id);
305 else
306 if(stream_id == MPEG3_PRIVATE_STREAM_2)
308 /* Dump private data! */
309 fprintf(stderr, "stream_id == MPEG3_PRIVATE_STREAM_2\n");
310 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
311 return 0;
313 else
314 if(stream_id == MPEG3_PADDING_STREAM)
316 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
317 return 0;
319 else
321 fprintf(stderr, "unknown stream_id in pes packet");
322 return 1;
324 return 0;
327 static int get_payload(mpeg3_demuxer_t *demuxer)
329 //printf("get_payload 1 %x %d\n", demuxer->pid, demuxer->payload_unit_start_indicator);
330 if(demuxer->payload_unit_start_indicator)
332 if(demuxer->pid==0)
333 get_program_association_table(demuxer);
334 else
335 if(packet_next_int24(demuxer) == MPEG3_PACKET_START_CODE_PREFIX)
336 get_pes_packet(demuxer);
337 else
338 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
340 else
342 //printf("get_payload 2\n");
343 if(demuxer->pid == demuxer->audio_pid && demuxer->do_audio)
345 get_data_buffer(demuxer);
347 else
348 if(demuxer->pid == demuxer->video_pid && demuxer->do_video)
350 get_data_buffer(demuxer);
352 else
353 packet_skip(demuxer, demuxer->raw_size - demuxer->raw_offset);
355 return 0;
358 /* Read a transport packet */
359 static int read_transport(mpeg3_demuxer_t *demuxer)
361 mpeg3_t *file = (mpeg3_t*)demuxer->file;
362 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
363 int result = 0;
364 unsigned int bits;
365 int table_entry;
367 /* Packet size is known for transport streams */
368 demuxer->raw_size = file->packet_size;
369 demuxer->raw_offset = 0;
371 if(result)
373 perror("read_transport");
374 return 1;
377 //printf("read transport 1\n");
378 // Search for Sync byte */
381 bits = mpeg3io_read_char(title->fs);
382 }while(!mpeg3io_eof(title->fs) && !result && bits != MPEG3_SYNC_BYTE);
385 * printf("read transport 2 bits=%x tell=%llx packet_size=%x\n",
386 * bits,
387 * mpeg3io_tell(title->fs),
388 * file->packet_size);
391 if(bits == MPEG3_SYNC_BYTE && !result)
393 demuxer->raw_data[0] = MPEG3_SYNC_BYTE;
394 result = mpeg3io_read_data(demuxer->raw_data + 1,
395 file->packet_size - 1,
396 title->fs);
398 else
399 return 1;
401 packet_read_char(demuxer);
402 bits = packet_read_int24(demuxer) & 0x00ffffff;
403 //printf("read transport 3 tell=%x bits=%x\n", mpeg3io_tell(title->fs), bits);
404 demuxer->transport_error_indicator = (bits >> 23) & 0x1;
405 demuxer->payload_unit_start_indicator = (bits >> 22) & 0x1;
406 demuxer->pid = (bits >> 8) & 0x00001fff;
407 demuxer->transport_scrambling_control = (bits >> 6) & 0x3;
408 demuxer->adaptation_field_control = (bits >> 4) & 0x3;
409 demuxer->continuity_counter = bits & 0xf;
410 //printf("read_transport 1 %llx %08x %d\n", mpeg3io_tell(title->fs), bits, demuxer->payload_unit_start_indicator);
412 //printf("read_transport 4 %x\n", demuxer->pid);
413 if(demuxer->transport_error_indicator)
415 fprintf(stderr, "demuxer->transport_error_indicator\n");
416 return 1;
419 //printf("read_transport 5 %x\n", demuxer->pid);
420 if (demuxer->pid == 0x1fff)
422 // return 0;
423 demuxer->is_padding = 1; /* padding; just go to next */
425 else
427 demuxer->is_padding = 0;
430 //printf("read_transport 6 %x\n", demuxer->pid);
431 /* Get pid from table */
432 for(table_entry = 0, result = 0;
433 table_entry < demuxer->total_pids;
434 table_entry++)
436 if(demuxer->pid == demuxer->pid_table[table_entry])
438 result = 1;
439 break;
443 //printf("read_transport 7 %x\n", demuxer->pid);
447 /* Not in pid table */
448 if(!result)
450 demuxer->pid_table[table_entry] = demuxer->pid;
451 demuxer->continuity_counters[table_entry] = demuxer->continuity_counter; /* init */
452 demuxer->total_pids++;
454 result = 0;
456 /* Abort if padding. Should abort after demuxer->pid == 0x1fff for speed. */
457 if(demuxer->is_padding) return 0;
460 #if 0
461 /* Check counters */
462 if(demuxer->pid != MPEG3_PROGRAM_ASSOCIATION_TABLE &&
463 demuxer->pid != MPEG3_CONDITIONAL_ACCESS_TABLE &&
464 (demuxer->adaptation_field_control == 1 || demuxer->adaptation_field_control == 3))
466 if(demuxer->continuity_counters[table_entry] != demuxer->continuity_counter)
468 // fprintf(stderr, "demuxer->continuity_counters[table_entry] != demuxer->continuity_counter\n");
469 /* Reset it */
470 demuxer->continuity_counters[table_entry] = demuxer->continuity_counter;
472 if(++(demuxer->continuity_counters[table_entry]) > 15) demuxer->continuity_counters[table_entry] = 0;
474 #endif
482 //printf("read_transport 8 %x %x\n", demuxer->adaptation_field_control, demuxer->pid);
483 if(demuxer->adaptation_field_control == 2 ||
484 demuxer->adaptation_field_control == 3)
485 result = get_adaptation_field(demuxer);
487 // Need to enter in astream and vstream table:
488 // PID ored with stream_id
489 if(demuxer->adaptation_field_control == 1 ||
490 demuxer->adaptation_field_control == 3)
491 result = get_payload(demuxer);
492 //printf("read_transport 10 %llx\n", mpeg3io_tell(title->fs));
494 return result;
497 static int get_system_header(mpeg3_demuxer_t *demuxer)
499 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
500 int length = mpeg3io_read_int16(title->fs);
501 mpeg3io_seek_relative(title->fs, length);
502 return 0;
505 static unsigned long get_timestamp(mpeg3_demuxer_t *demuxer)
507 unsigned long timestamp;
508 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
510 /* Only low 4 bits (7==1111) */
511 timestamp = (mpeg3io_read_char(title->fs) >> 1) & 7;
512 timestamp <<= 15;
513 timestamp |= (mpeg3io_read_int16(title->fs) >> 1);
514 timestamp <<= 15;
515 timestamp |= (mpeg3io_read_int16(title->fs) >> 1);
516 return timestamp;
519 static int get_pack_header(mpeg3_demuxer_t *demuxer)
521 unsigned long i, j;
522 unsigned long clock_ref, clock_ref_ext;
523 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
525 /* Get the time code */
526 if((mpeg3io_next_char(title->fs) >> 4) == 2)
528 /* MPEG-1 */
529 demuxer->time = (double)get_timestamp(demuxer) / 90000;
530 /* Skip 3 bytes */
531 mpeg3io_read_int24(title->fs);
533 else
534 if(mpeg3io_next_char(title->fs) & 0x40)
536 i = mpeg3io_read_int32(title->fs);
537 j = mpeg3io_read_int16(title->fs);
539 if(i & 0x40000000 || (i >> 28) == 2)
541 clock_ref = ((i & 0x38000000) << 3);
542 clock_ref |= ((i & 0x03fff800) << 4);
543 clock_ref |= ((i & 0x000003ff) << 5);
544 clock_ref |= ((j & 0xf800) >> 11);
545 clock_ref_ext = (j >> 1) & 0x1ff;
547 demuxer->time = (double)(clock_ref + clock_ref_ext / 300) / 90000;
548 /* Skip 3 bytes */
549 mpeg3io_read_int24(title->fs);
550 i = mpeg3io_read_char(title->fs) & 0x7;
552 /* stuffing */
553 mpeg3io_seek_relative(title->fs, i);
556 else
558 mpeg3io_seek_relative(title->fs, 2);
560 return 0;
563 /* Program packet reading core */
564 static int get_ps_pes_packet(mpeg3_demuxer_t *demuxer, unsigned int header)
566 unsigned long pts = 0, dts = 0;
567 int stream_id;
568 int pes_packet_length;
569 int64_t pes_packet_start;
570 long decryption_offset;
571 int i;
572 mpeg3_t *file = demuxer->file;
573 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
574 int scrambling = 0;
575 int data_start = demuxer->data_size;
576 /* Format if audio */
577 int do_pcm = 0;
579 stream_id = header & 0xff;
580 pes_packet_length = mpeg3io_read_int16(title->fs);
581 pes_packet_start = mpeg3io_tell(title->fs);
586 * printf(__FUNCTION__ " pes_packet_start=%llx pes_packet_length=%x data_size=%x\n",
587 * pes_packet_start,
588 * pes_packet_length,
589 * demuxer->data_size);
596 if(stream_id != MPEG3_PRIVATE_STREAM_2 &&
597 stream_id != MPEG3_PADDING_STREAM)
599 if((mpeg3io_next_char(title->fs) >> 6) == 0x02)
601 /* Get MPEG-2 packet */
602 int pes_header_bytes = 0;
603 int pts_dts_flags;
604 int pes_header_data_length;
607 demuxer->last_packet_decryption = mpeg3io_tell(title->fs);
608 scrambling = mpeg3io_read_char(title->fs) & 0x30;
609 //scrambling = 1;
610 /* Reset scrambling bit for the mpeg3cat utility */
611 // if(scrambling) demuxer->raw_data[demuxer->raw_offset - 1] &= 0xcf;
612 // Force packet length if scrambling
613 if(scrambling) pes_packet_length = 0x800 -
614 pes_packet_start +
615 demuxer->last_packet_start;
618 pts_dts_flags = (mpeg3io_read_char(title->fs) >> 6) & 0x3;
619 pes_header_data_length = mpeg3io_read_char(title->fs);
623 /* Get Presentation and Decoding Time Stamps */
624 if(pts_dts_flags == 2)
626 pts = (mpeg3io_read_char(title->fs) >> 1) & 7; /* Only low 4 bits (7==1111) */
627 pts <<= 15;
628 pts |= (mpeg3io_read_int16(title->fs) >> 1);
629 pts <<= 15;
630 pts |= (mpeg3io_read_int16(title->fs) >> 1);
631 pes_header_bytes += 5;
633 else
634 if(pts_dts_flags == 3)
636 pts = (mpeg3io_read_char(title->fs) >> 1) & 7; /* Only low 4 bits (7==1111) */
637 pts <<= 15;
638 pts |= (mpeg3io_read_int16(title->fs) >> 1);
639 pts <<= 15;
640 pts |= (mpeg3io_read_int16(title->fs) >> 1);
642 dts = (mpeg3io_read_char(title->fs) >> 1) & 7; /* Only low 4 bits (7==1111) */
643 dts <<= 15;
644 dts |= (mpeg3io_read_int16(title->fs) >> 1);
645 dts <<= 15;
646 dts |= (mpeg3io_read_int16(title->fs) >> 1);
647 pes_header_bytes += 10;
650 //printf("get_ps_pes_packet do_audio=%d do_video=%d pts=%x dts=%x\n",
651 // demuxer->do_audio, demuxer->do_video, pts, dts);
653 /* Skip unknown */
654 mpeg3io_seek_relative(title->fs,
655 pes_header_data_length - pes_header_bytes);
657 else
659 int pts_dts_flags;
660 /* Get MPEG-1 packet */
661 while(mpeg3io_next_char(title->fs) == 0xff)
663 mpeg3io_read_char(title->fs);
666 /* Skip STD buffer scale */
667 if((mpeg3io_next_char(title->fs) & 0x40) == 0x40)
669 mpeg3io_seek_relative(title->fs, 2);
672 /* Decide which timestamps are available */
673 pts_dts_flags = mpeg3io_next_char(title->fs);
675 if(pts_dts_flags >= 0x30)
677 /* Get the presentation and decoding time stamp */
678 pts = get_timestamp(demuxer);
679 dts = get_timestamp(demuxer);
681 else
682 if(pts_dts_flags >= 0x20)
684 /* Get just the presentation time stamp */
685 pts = get_timestamp(demuxer);
687 else
688 if(pts_dts_flags == 0x0f)
690 /* End of timestamps */
691 mpeg3io_read_char(title->fs);
693 else
695 return 1; /* Error */
699 /* Now extract the payload. */
700 if((stream_id >> 4) == 0xc || (stream_id >> 4) == 0xd)
702 /* Audio data */
703 /* Take first stream ID if -1 */
704 pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
709 if(demuxer->read_all)
710 demuxer->astream_table[stream_id & 0x0f] = AUDIO_MPEG;
711 else
712 if(demuxer->astream == -1)
713 demuxer->astream = stream_id & 0x0f;
718 if(pts > 0) demuxer->pes_audio_time = (double)pts / 60000;
721 if((stream_id & 0x0f) == demuxer->astream && demuxer->do_audio)
723 decryption_offset = mpeg3io_tell(title->fs) - demuxer->last_packet_start;
724 mpeg3io_read_data(demuxer->data_buffer + demuxer->data_size,
725 pes_packet_length,
726 title->fs);
727 demuxer->data_size += pes_packet_length;
729 else
731 mpeg3io_seek_relative(title->fs, pes_packet_length);
734 else
735 if((stream_id >> 4) == 0xe)
737 /* Video data */
738 /* Take first stream ID if -1 */
739 pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
743 if(demuxer->read_all)
744 demuxer->vstream_table[stream_id & 0x0f] = 1;
745 else
746 if(demuxer->vstream == -1)
747 demuxer->vstream = stream_id & 0x0f;
749 if(pts > 0) demuxer->pes_video_time = (double)pts / 60000;
753 if((stream_id & 0x0f) == demuxer->vstream && demuxer->do_video)
755 //printf(__FUNCTION__ " stream_id=%x size=%x\n", stream_id, pes_packet_length);
756 decryption_offset = mpeg3io_tell(title->fs) - demuxer->last_packet_start;
757 mpeg3io_read_data(demuxer->data_buffer + demuxer->data_size,
758 pes_packet_length,
759 title->fs);
761 demuxer->data_size += pes_packet_length;
763 else
765 mpeg3io_seek_relative(title->fs, pes_packet_length);
768 else
769 if((stream_id == 0xbd || stream_id == 0xbf) &&
770 mpeg3io_next_char(title->fs) != 0xff &&
771 ((mpeg3io_next_char(title->fs) & 0xf0) != 0x20))
773 int format;
774 /* DVD audio data */
775 /* Get the audio format */
776 if((mpeg3io_next_char(title->fs) & 0xf0) == 0xa0)
777 format = AUDIO_PCM;
778 else
779 format = AUDIO_AC3;
781 // Picks up bogus data if (& 0xf) or (& 0x7f)
782 stream_id = mpeg3io_next_char(title->fs);
783 if(pts > 0) demuxer->pes_audio_time = (double)pts / 60000;
785 //printf("get_ps_pes_packet %x\n", stream_id);
786 /* Take first stream ID if not building TOC. */
787 if(demuxer->read_all)
788 demuxer->astream_table[stream_id] = format;
789 else
790 if(demuxer->astream == -1)
791 demuxer->astream = stream_id;
799 //printf("get_ps_pes_packet 5 %x\n", format);
800 if(stream_id == demuxer->astream && demuxer->do_audio)
802 demuxer->aformat = format;
803 mpeg3io_read_int32(title->fs);
805 pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
806 decryption_offset = mpeg3io_tell(title->fs) - demuxer->last_packet_start;
808 if(format == AUDIO_PCM) do_pcm = 1;
809 //printf("get_ps_pes_packet 5 %x\n", decryption_offset);
813 mpeg3io_read_data(demuxer->data_buffer + demuxer->data_size,
814 pes_packet_length,
815 title->fs);
816 demuxer->data_size += pes_packet_length;
818 else
820 pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
821 mpeg3io_seek_relative(title->fs, pes_packet_length);
823 //printf("get_ps_pes_packet 6 %d\n", demuxer->astream_table[0x20]);
825 else
826 if(stream_id == 0xbc || 1)
828 pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
829 mpeg3io_seek_relative(title->fs, pes_packet_length);
832 else
833 if(stream_id == MPEG3_PRIVATE_STREAM_2 || stream_id == MPEG3_PADDING_STREAM)
835 pes_packet_length -= mpeg3io_tell(title->fs) - pes_packet_start;
836 mpeg3io_seek_relative(title->fs, pes_packet_length);
837 //printf(__FUNCTION__ " 2 %llx\n", mpeg3io_tell(title->fs));
844 // Advance 2048 bytes if scrambled. We might pick up a spurrius
845 // packet start code in the scrambled data otherwise.
846 if(scrambling &&
847 demuxer->last_packet_start + 0x800 > mpeg3io_tell(title->fs))
849 mpeg3io_seek_relative(title->fs,
850 demuxer->last_packet_start + 0x800 - mpeg3io_tell(title->fs));
857 // Descramble if desired
858 if(demuxer->data_size && scrambling)
860 //printf(__FUNCTION__ " data_size=%x decryption_offset=%x\n", demuxer->data_size, decryption_offset);
861 if(mpeg3_decrypt_packet(title->fs->css,
862 demuxer->data_buffer,
863 decryption_offset))
865 fprintf(stderr, "get_ps_pes_packet: Decryption not available\n");
866 return 1;
875 /* Synthesize PCM header and delete MPEG header for PCM data */
876 /* Must be done after decryption */
877 if(do_pcm)
879 unsigned char code;
880 int bits_code;
881 int bits;
882 int samplerate_code;
883 int samplerate;
884 unsigned char *output = demuxer->data_buffer + data_start;
885 int j;
887 /* Shift audio back */
888 code = output[1];
889 for(i = demuxer->data_size - 1, j = demuxer->data_size + PCM_HEADERSIZE - 3 - 1;
890 i >= data_start;
891 i--, j--)
892 *(demuxer->data_buffer + j) = *(demuxer->data_buffer + i);
893 demuxer->data_size += PCM_HEADERSIZE - 3;
895 bits_code = (code >> 6) & 3;
896 samplerate_code = (code & 0x10);
899 output[0] = 0x7f;
900 output[1] = 0x7f;
901 output[2] = 0x80;
902 output[3] = 0x7f;
903 /* Samplerate */
904 switch(samplerate_code)
906 case 1:
907 samplerate = 96000;
908 break;
909 default:
910 samplerate = 48000;
911 break;
914 *(int32_t*)(output + 4) = samplerate;
915 /* Bits */
916 switch(bits_code)
918 case 0: bits = 16; break;
919 case 1: bits = 20; break;
920 case 2: bits = 24; break;
921 default: bits = 16; break;
923 *(int32_t*)(output + 8) = bits;
924 /* Channels */
925 *(int32_t*)(output + 12) = (code & 0x7) + 1;
926 /* Framesize */
927 *(int32_t*)(output + 16) = pes_packet_length -
928 3 +
929 PCM_HEADERSIZE;
930 //printf(__FUNCTION__ " %02x%02x%02x %d\n", code1, code, code2, pes_packet_length - 3);
940 //printf(__FUNCTION__ " 3 %llx %x\n", mpeg3io_tell(title->fs), demuxer->data_size);
943 //if(mpeg3io_tell(title->fs) - demuxer->last_packet_start != 0x800)
944 //printf(__FUNCTION__ " packet size == %llx\n", mpeg3io_tell(title->fs) - demuxer->last_packet_start);
946 //printf(__FUNCTION__ " pes_audio_time=%f pes_video_time=%f\n", demuxer->pes_audio_time, demuxer->pes_video_time);
948 return 0;
951 int mpeg3demux_read_program(mpeg3_demuxer_t *demuxer)
953 int result = 0;
954 int count = 0;
955 mpeg3_t *file = demuxer->file;
956 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
957 unsigned int header = 0;
958 int pack_count = 0;
960 demuxer->data_size = 0;
961 //printf("mpeg3demux_read_program 1 %d %llx %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
963 if(mpeg3io_eof(title->fs)) return 1;
965 //printf("mpeg3demux_read_program 2 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
973 /* Search for next header */
974 /* Parse packet until the next packet start code */
975 while(!result && !mpeg3io_eof(title->fs))
977 header = mpeg3io_read_int32(title->fs);
981 if(header == MPEG3_PACK_START_CODE)
985 // Second start code in this call. Don't read it.
986 if(pack_count)
988 mpeg3io_seek_relative(title->fs, -4);
989 break;
992 demuxer->last_packet_start = mpeg3io_tell(title->fs) - 4;
993 result = get_pack_header(demuxer);
994 //printf("mpeg3demux_read_program MPEG3_PACK_START_CODE %d\n", result);
995 pack_count++;
1000 else
1001 if(header == MPEG3_SYSTEM_START_CODE && pack_count)
1006 result = get_system_header(demuxer);
1007 //printf("mpeg3demux_read_program MPEG3_SYSTEM_START_CODE %d\n", result);
1012 else
1013 if((header >> 8) == MPEG3_PACKET_START_CODE_PREFIX && pack_count)
1017 result = get_ps_pes_packet(demuxer, header);
1018 //printf("mpeg3demux_read_program MPEG3_PACKET_START_CODE_PREFIX %d %08x\n", result, header);
1023 else
1025 // Try again.
1026 //printf(__FUNCTION__ " %llx %08x\n", mpeg3io_tell(title->fs), header);
1027 mpeg3io_seek_relative(title->fs, -3);
1030 //printf("mpeg3demux_read_program 3 %d %x %llx\n", result, title->fs->current_byte, title->fs->total_bytes);
1038 // Ignore errors in the parsing. Just quit if eof.
1039 result = 0;
1046 demuxer->last_packet_end = mpeg3io_tell(title->fs);
1049 * if(demuxer->last_packet_end - demuxer->last_packet_start != 0x800)
1050 * printf(__FUNCTION__ " packet_size=%llx data_size=%x pack_count=%d packet_start=%llx packet_end=%llx\n",
1051 * demuxer->last_packet_end - demuxer->last_packet_start,
1052 * demuxer->data_size,
1053 * pack_count,
1054 * demuxer->last_packet_start,
1055 * demuxer->last_packet_end);
1057 //printf("mpeg3demux_read_program 5 %d\n", result);
1059 //printf("read_program 3\n");
1060 // if(!result) result = mpeg3io_eof(title->fs);
1062 return result;
1065 static double lookup_time_offset(mpeg3_demuxer_t *demuxer, long byte)
1067 int i;
1068 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
1070 if(!title->timecode_table_size) return 0;
1072 for(i = title->timecode_table_size - 1;
1073 i >= 0 && title->timecode_table[i].start_byte > byte;
1074 i--)
1076 if(i < 0) i = 0;
1077 return title->timecode_table[i].absolute_start_time - title->timecode_table[i].start_time;
1116 int mpeg3_advance_timecode(mpeg3_demuxer_t *demuxer)
1118 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
1119 int result = 0;
1120 int do_seek = 0;
1121 int last_timecode = demuxer->current_timecode;
1122 //printf("mpeg3_advance_timecode 0\n");fflush(stdout);
1129 /* Don't do anything when constructing timecode table */
1130 if(!title->timecode_table ||
1131 !title->timecode_table_size ||
1132 demuxer->read_all) return 0;
1138 //printf("mpeg3_advance_timecode 1\n");fflush(stdout);
1139 if(!demuxer->reverse)
1145 /* Get inside the current timecode */
1146 if(mpeg3io_tell(title->fs) < title->timecode_table[demuxer->current_timecode].start_byte)
1148 mpeg3io_seek(title->fs,
1149 title->timecode_table[demuxer->current_timecode].start_byte);
1156 /* Get the next timecode */
1157 while(!result &&
1158 (mpeg3io_tell(title->fs) >= title->timecode_table[demuxer->current_timecode].end_byte ||
1159 demuxer->current_program != title->timecode_table[demuxer->current_timecode].program))
1161 demuxer->current_timecode++;
1163 if(demuxer->current_timecode >= title->timecode_table_size)
1165 demuxer->current_timecode = 0;
1166 if(demuxer->current_title + 1 < demuxer->total_titles)
1168 mpeg3demux_open_title(demuxer, ++demuxer->current_title);
1169 do_seek = 1;
1171 else
1173 mpeg3io_seek(title->fs, mpeg3io_total_bytes(title->fs));
1174 result = 1;
1179 title = demuxer->titles[demuxer->current_title];
1186 //if(last_timecode != demuxer->current_timecode && demuxer->do_video)
1187 // printf("using title %d cell %x-%x\n", demuxer->current_title, title->timecode_table[demuxer->current_timecode].start_byte, title->timecode_table[demuxer->current_timecode].end_byte);
1189 //printf("2 %d\n", title->timecode_table[demuxer->current_timecode].program);
1195 if(!result && do_seek)
1197 //printf("current_cell=%d\n", demuxer->current_timecode);
1198 mpeg3io_seek(title->fs,
1199 title->timecode_table[demuxer->current_timecode].start_byte);
1202 else
1214 /* Get the previous timecode */
1215 while(!result &&
1216 (mpeg3io_tell(title->fs) < title->timecode_table[demuxer->current_timecode].start_byte ||
1217 demuxer->current_program != title->timecode_table[demuxer->current_timecode].program))
1220 * printf("mpeg3_reverse_timecode %d %d %d %d\n",
1221 * mpeg3io_tell(title->fs),
1222 * demuxer->current_timecode,
1223 * title->timecode_table[demuxer->current_timecode].start_byte,
1224 * title->timecode_table[demuxer->current_timecode].end_byte);
1227 demuxer->current_timecode--;
1228 if(demuxer->current_timecode < 0)
1230 if(demuxer->current_title > 0)
1232 //printf("advance_timecode 2 %d\n", demuxer->current_title);
1233 mpeg3demux_open_title(demuxer, --demuxer->current_title);
1234 title = demuxer->titles[demuxer->current_title];
1235 // Seek to end since we opened at the beginning of the next title
1236 mpeg3io_seek(title->fs, title->total_bytes);
1237 //printf("advance_timecode 3 %d %d\n", demuxer->current_title, mpeg3io_tell(title->fs));
1238 demuxer->current_timecode = title->timecode_table_size - 1;
1239 do_seek = 1;
1241 else
1243 mpeg3io_seek(title->fs, 0);
1244 demuxer->current_timecode = 0;
1245 result = 1;
1256 if(!result && do_seek)
1258 mpeg3io_seek(title->fs,
1259 title->timecode_table[demuxer->current_timecode].start_byte);
1267 //printf("mpeg3_advance_timecode 2 %d\n", demuxer->current_title);fflush(stdout);
1268 return result;
1284 /* Read packet in the forward direction */
1285 int mpeg3_read_next_packet(mpeg3_demuxer_t *demuxer)
1287 int result = 0;
1288 long current_position;
1289 mpeg3_t *file = demuxer->file;
1290 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
1291 demuxer->data_size = 0;
1292 demuxer->data_position = 0;
1294 //printf("mpeg3_read_next_packet 1 %d %llx\n", demuxer->current_title, mpeg3io_tell(title->fs));
1295 /* Switch to forward direction. */
1296 if(demuxer->reverse)
1298 if(file->packet_size > 0)
1300 if(!result) result = mpeg3io_seek_relative(title->fs, file->packet_size);
1302 else
1304 if(!result) result = mpeg3io_next_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
1305 if(!result) result = mpeg3io_next_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
1308 demuxer->reverse = 0;
1310 //printf("mpeg3_read_next_packet 4 %d\n", result);
1319 /* Read packets until the output buffer is full */
1320 if(!result)
1324 result = mpeg3_advance_timecode(demuxer);
1326 if(!result)
1328 demuxer->time_offset = lookup_time_offset(demuxer, mpeg3io_tell(title->fs));
1330 if(file->is_transport_stream)
1332 //printf("mpeg3_read_next_packet: 1 %f\n", demuxer->time);
1333 result = read_transport(demuxer);
1334 //printf("mpeg3_read_next_packet: 2 %f\n", demuxer->time);
1336 else
1337 if(file->is_program_stream)
1339 result = mpeg3demux_read_program(demuxer);
1341 else
1343 /* Read elementary stream. */
1344 //printf("mpeg3_read_next_packet: 3\n");
1345 result = mpeg3io_read_data(demuxer->data_buffer,
1346 file->packet_size, title->fs);
1347 if(!result) demuxer->data_size = file->packet_size;
1350 //printf("mpeg3_read_next_packet 2 %x %lx\n", demuxer->data_size, mpeg3io_tell(title->fs));
1351 }while(!result &&
1352 demuxer->data_size == 0 &&
1353 (demuxer->do_audio || demuxer->do_video));
1356 //printf("mpeg3_read_next_packet 2 %dd %d %llx\n", result, demuxer->current_title, mpeg3io_tell(title->fs));
1357 return result;
1375 /* Read the packet right before the packet we're currently on. */
1376 int mpeg3_read_prev_packet(mpeg3_demuxer_t *demuxer)
1378 int result = 0;
1379 mpeg3_t *file = demuxer->file;
1380 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
1381 int64_t current_position = -1;
1383 demuxer->data_size = 0;
1384 demuxer->data_position = 0;
1392 //printf("mpeg3_read_prev_packet 1 %d %d %llx\n", result, demuxer->current_title, mpeg3io_tell(title->fs));
1393 /* Switch to reverse direction */
1394 if(!demuxer->reverse)
1396 if(file->packet_size > 0)
1398 result = mpeg3io_seek_relative(title->fs, -file->packet_size);
1400 else
1402 result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
1405 demuxer->reverse = 1;
1412 //printf("mpeg3_read_prev_packet 1 %d %d %llx\n", result, demuxer->current_title, mpeg3io_tell(title->fs));
1417 // Go to beginning of previous packet
1419 if(file->packet_size > 0)
1421 if(!result) result = mpeg3io_seek_relative(title->fs, -file->packet_size);
1423 else
1425 if(!result) result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
1428 //printf("mpeg3_read_prev_packet 3 %x %x\n", title->fs->current_byte, title->fs->buffer_position);
1430 // Advance title
1431 if(!result) result = mpeg3_advance_timecode(demuxer);
1433 //printf("mpeg3_read_prev_packet 2 %p->%p->%p\n", title, title->fs, title->fs->fd);
1434 if(!result) demuxer->time_offset = lookup_time_offset(demuxer, mpeg3io_tell(title->fs));
1436 // Read packet and then rewind it
1437 if(file->is_transport_stream && !result)
1439 result = read_transport(demuxer);
1441 if(!mpeg3io_bof(title->fs))
1443 result = mpeg3io_seek_relative(title->fs, -file->packet_size);
1445 //printf("mpeg3_read_prev_packet 4 %x %x\n", title->fs->current_byte, title->fs->buffer_position);
1447 else
1448 if(file->is_program_stream && !result)
1450 //printf("mpeg3_read_prev_packet 2 %x\n", title->fs->current_byte);
1451 current_position = mpeg3io_tell(title->fs);
1453 // Read packet
1454 result = mpeg3demux_read_program(demuxer);
1455 //printf("mpeg3_read_prev_packet 3 %x\n", title->fs->current_byte);
1457 // Rewind packet
1458 while(mpeg3io_tell(title->fs) > current_position)
1460 if(!mpeg3io_bof(title->fs))
1461 result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
1462 else
1464 mpeg3_advance_timecode(demuxer);
1465 if(!mpeg3io_bof(title->fs))
1466 result = mpeg3io_prev_code(title->fs, MPEG3_PACK_START_CODE, MPEG3_RAW_SIZE);
1469 //printf("mpeg3_read_prev_packet 4 %x\n", title->fs->current_byte);
1471 else
1472 if(!result)
1474 /* Elementary stream */
1475 /* Read the packet forwards and seek back to the start */
1476 result = mpeg3io_read_data(demuxer->data_buffer,
1477 file->packet_size,
1478 title->fs);
1480 if(!result)
1482 demuxer->data_size = file->packet_size;
1483 result = mpeg3io_seek_relative(title->fs, -file->packet_size);
1486 }while(!result &&
1487 demuxer->data_size == 0 &&
1488 (demuxer->do_audio || demuxer->do_video));
1491 //printf("mpeg3_read_prev_packet 5 %d %d %llx\n", result, demuxer->current_title, mpeg3io_tell(title->fs));
1492 return result;
1496 /* For audio */
1497 int mpeg3demux_read_data(mpeg3_demuxer_t *demuxer,
1498 unsigned char *output,
1499 long size)
1501 long i;
1502 int result = 0;
1503 mpeg3_t *file = demuxer->file;
1504 int64_t current_position;
1505 demuxer->error_flag = 0;
1507 if(demuxer->data_position >= 0)
1509 /* Read forwards */
1510 for(i = 0; i < size && !result; )
1512 int fragment_size = size - i;
1513 if(fragment_size > demuxer->data_size - demuxer->data_position)
1514 fragment_size = demuxer->data_size - demuxer->data_position;
1515 memcpy(output + i, demuxer->data_buffer + demuxer->data_position, fragment_size);
1516 demuxer->data_position += fragment_size;
1517 i += fragment_size;
1519 if(i < size)
1521 result = mpeg3_read_next_packet(demuxer);
1525 else
1527 /* Read backwards a full packet. */
1528 /* Only good for reading less than the size of a full packet, but */
1529 /* this routine should only be used for searching for previous markers. */
1530 current_position = demuxer->data_position;
1531 result = mpeg3_read_prev_packet(demuxer);
1532 if(!result) demuxer->data_position = demuxer->data_size + current_position;
1533 memcpy(output, demuxer->data_buffer + demuxer->data_position, size);
1534 demuxer->data_position += size;
1537 demuxer->error_flag = result;
1538 return result;
1541 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t *demuxer)
1543 demuxer->error_flag = 0;
1545 if(demuxer->data_position >= demuxer->data_size)
1546 demuxer->error_flag = mpeg3_read_next_packet(demuxer);
1547 demuxer->next_char = demuxer->data_buffer[demuxer->data_position++];
1549 return demuxer->next_char;
1552 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t *demuxer)
1554 demuxer->error_flag = 0;
1555 demuxer->data_position--;
1556 if(demuxer->data_position < 0)
1558 //printf("mpeg3demux_read_prev_char_packet 1\n");
1559 demuxer->error_flag = mpeg3_read_prev_packet(demuxer);
1560 //printf("mpeg3demux_read_prev_char_packet 2\n");
1561 if(!demuxer->error_flag) demuxer->data_position = demuxer->data_size - 1;
1563 demuxer->next_char = demuxer->data_buffer[demuxer->data_position];
1564 return demuxer->next_char;
1567 static mpeg3demux_timecode_t* next_timecode(mpeg3_demuxer_t *demuxer,
1568 int *current_title,
1569 int *current_timecode,
1570 int current_program)
1572 int done = 0;
1573 while(!done)
1575 /* Increase timecode number */
1576 //printf(__FUNCTION__ " 1\n");
1577 if(*current_timecode < demuxer->titles[*current_title]->timecode_table_size - 1)
1579 //printf(__FUNCTION__ " 2\n");
1580 (*current_timecode)++;
1581 if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
1582 return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
1583 //printf(__FUNCTION__ " 3\n");
1585 else
1586 /* Increase title number */
1587 if(*current_title < demuxer->total_titles - 1)
1589 //printf(__FUNCTION__ " 4\n");
1590 (*current_title)++;
1591 (*current_timecode) = 0;
1592 if(*current_timecode < *current_timecode < demuxer->titles[*current_title]->timecode_table_size)
1593 if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
1594 return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
1595 //printf(__FUNCTION__ " 5\n");
1597 else
1598 /* End of disk */
1599 done = 1;
1600 //printf(__FUNCTION__ " 6\n");
1602 return 0;
1605 static mpeg3demux_timecode_t* prev_timecode(mpeg3_demuxer_t *demuxer,
1606 int *current_title,
1607 int *current_timecode,
1608 int current_program)
1610 int done = 0;
1611 while(!done)
1613 /* Increase timecode number */
1614 if(*current_timecode > 0)
1616 (*current_timecode)--;
1617 if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
1618 return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
1620 else
1621 /* Increase title number */
1622 if(*current_title > 0)
1624 (*current_title)--;
1625 (*current_timecode) = demuxer->titles[*current_title]->timecode_table_size - 1;
1626 if(demuxer->titles[*current_title]->timecode_table[*current_timecode].program == current_program)
1627 return &(demuxer->titles[*current_title]->timecode_table[*current_timecode]);
1629 else
1630 /* End of disk */
1631 done = 1;
1634 return 0;
1637 int mpeg3demux_open_title(mpeg3_demuxer_t *demuxer, int title_number)
1639 mpeg3_title_t *title;
1641 //printf("mpeg3demux_open_title 1\n");
1642 if(title_number < demuxer->total_titles)
1644 if(demuxer->current_title >= 0)
1646 mpeg3io_close_file(demuxer->titles[demuxer->current_title]->fs);
1647 demuxer->current_title = -1;
1650 //printf("mpeg3demux_open_title %p %p %d\n", demuxer, demuxer->titles, title_number);
1651 title = demuxer->titles[title_number];
1653 if(mpeg3io_open_file(title->fs))
1655 demuxer->error_flag = 1;
1656 fprintf(stderr, "mpeg3demux_open_title %s: %s", title->fs->path, strerror(errno));
1658 else
1660 demuxer->current_title = title_number;
1664 //printf("mpeg3demux_open_title 2\n");
1665 // demuxer->current_timecode = 0;
1667 return demuxer->error_flag;
1670 /* Assign program numbers to interleaved programs */
1671 int mpeg3demux_assign_programs(mpeg3_demuxer_t *demuxer)
1673 int current_program = 0;
1674 int current_title = 0;
1675 int current_timecode = 0;
1676 double current_time;
1677 mpeg3demux_timecode_t *timecode;
1678 int total_programs = 1;
1679 int i, j;
1680 int program_exists, last_program_assigned = 0;
1681 int total_timecodes;
1682 mpeg3_title_t **titles = demuxer->titles;
1684 //printf(__FUNCTION__ " 1\n");
1685 for(i = 0, total_timecodes = 0; i < demuxer->total_titles; i++)
1687 total_timecodes += demuxer->titles[i]->timecode_table_size;
1688 for(j = 0; j < demuxer->titles[i]->timecode_table_size; j++)
1690 timecode = &demuxer->titles[i]->timecode_table[j];
1691 if(timecode->program > total_programs - 1)
1692 total_programs = timecode->program + 1;
1696 //printf(__FUNCTION__ " 2\n");
1697 /* Assign absolute timecodes in each program. */
1698 for(current_program = 0;
1699 current_program < total_programs;
1700 current_program++)
1702 current_time = 0;
1703 current_title = 0;
1704 current_timecode = -1;
1706 //printf(__FUNCTION__ " 3\n");
1707 while(timecode = next_timecode(demuxer,
1708 &current_title,
1709 &current_timecode,
1710 current_program))
1713 //printf(__FUNCTION__ " 4 %p\n", timecode);
1714 timecode->absolute_start_time = current_time;
1717 * printf("mpeg3demux_assign_programs %p end: %f start: %f\n",
1718 * timecode,
1719 * timecode->end_time,
1720 * timecode->start_time);
1723 current_time += fabs(timecode->end_time - timecode->start_time);
1724 //printf(__FUNCTION__ " 4.1 %p\n", timecode);
1725 timecode->absolute_end_time = current_time;
1726 //printf(__FUNCTION__ " 4.2 %p\n", timecode);
1728 //printf(__FUNCTION__ " 5\n");
1731 //printf(__FUNCTION__ " 6\n");
1732 //for(i = 0; i < demuxer->total_titles; i++) mpeg3_dump_title(demuxer->titles[i]);
1734 demuxer->current_program = 0;
1735 return 0;
1738 int mpeg3demux_copy_titles(mpeg3_demuxer_t *dst, mpeg3_demuxer_t *src)
1740 long i;
1741 mpeg3_t *file = dst->file;
1742 mpeg3_title_t *dst_title, *src_title;
1744 dst->total_titles = src->total_titles;
1745 dst->total_programs = src->total_programs;
1746 for(i = 0; i < MPEG3_MAX_STREAMS; i++)
1748 dst->astream_table[i] = src->astream_table[i];
1749 dst->vstream_table[i] = src->vstream_table[i];
1751 for(i = 0; i < src->total_titles; i++)
1753 src_title = src->titles[i];
1754 dst_title = dst->titles[i] = mpeg3_new_title(file, src->titles[i]->fs->path);
1755 mpeg3_copy_title(dst_title, src_title);
1758 mpeg3demux_open_title(dst, src->current_title);
1759 dst->current_timecode = 0;
1760 return 0;
1763 /* ==================================================================== */
1764 /* Entry points */
1765 /* ==================================================================== */
1767 mpeg3_demuxer_t* mpeg3_new_demuxer(mpeg3_t *file, int do_audio, int do_video, int stream_id)
1769 mpeg3_demuxer_t *demuxer = calloc(1, sizeof(mpeg3_demuxer_t));
1770 int i;
1772 /* The demuxer will change the default packet size for its own use. */
1773 demuxer->file = file;
1774 demuxer->do_audio = do_audio;
1775 demuxer->do_video = do_video;
1777 /* Allocate buffer + padding */
1778 demuxer->raw_data = calloc(1, MPEG3_RAW_SIZE);
1779 demuxer->data_buffer = calloc(1, MPEG3_RAW_SIZE);
1780 /* System specific variables */
1781 demuxer->audio_pid = stream_id;
1782 demuxer->video_pid = stream_id;
1783 demuxer->astream = stream_id;
1784 demuxer->vstream = stream_id;
1785 demuxer->current_title = -1;
1786 demuxer->pes_audio_time = -1;
1787 demuxer->pes_video_time = -1;
1788 //printf("mpeg3_new_demuxer %f\n", demuxer->time);
1789 return demuxer;
1792 int mpeg3_delete_demuxer(mpeg3_demuxer_t *demuxer)
1794 int i;
1796 if(demuxer->current_title >= 0)
1798 mpeg3io_close_file(demuxer->titles[demuxer->current_title]->fs);
1801 for(i = 0; i < demuxer->total_titles; i++)
1803 mpeg3_delete_title(demuxer->titles[i]);
1806 free(demuxer->data_buffer);
1807 free(demuxer->raw_data);
1808 free(demuxer);
1809 return 0;
1812 /* Need a timecode table to do this */
1813 double mpeg3demux_length(mpeg3_demuxer_t *demuxer)
1815 mpeg3_title_t *title;
1816 int i, j;
1817 double length;
1819 for(i = demuxer->total_titles - 1; i >= 0; i--)
1821 title = demuxer->titles[i];
1822 for(j = title->timecode_table_size - 1; j >= 0; j--)
1824 if(title->timecode_table[j].program == demuxer->current_program)
1827 * printf("mpeg3demux_length %f %f %f %f\n",
1828 * title->timecode_table[j].end_time,
1829 * title->timecode_table[j].start_time,
1830 * title->timecode_table[j].absolute_start_time,
1831 * title->timecode_table[j].end_time - title->timecode_table[j].start_time);
1833 return title->timecode_table[j].end_time -
1834 title->timecode_table[j].start_time +
1835 title->timecode_table[j].absolute_start_time;
1840 return 1;
1843 int mpeg3demux_eof(mpeg3_demuxer_t *demuxer)
1845 if(demuxer->current_title >= 0)
1847 if(mpeg3io_eof(demuxer->titles[demuxer->current_title]->fs) &&
1848 demuxer->current_title >= demuxer->total_titles - 1)
1849 return 1;
1852 return 0;
1855 int mpeg3demux_bof(mpeg3_demuxer_t *demuxer)
1857 if(demuxer->current_title >= 0)
1859 if(mpeg3io_bof(demuxer->titles[demuxer->current_title]->fs) &&
1860 demuxer->current_title <= 0)
1861 return 1;
1863 return 0;
1866 void mpeg3demux_start_reverse(mpeg3_demuxer_t *demuxer)
1868 demuxer->reverse = 1;
1871 void mpeg3demux_start_forward(mpeg3_demuxer_t *demuxer)
1873 demuxer->reverse = 0;
1876 /* Seek to a byte in the current title */
1877 int mpeg3demux_seek_byte(mpeg3_demuxer_t *demuxer, int64_t byte)
1879 int64_t current_position;
1880 mpeg3_t *file = demuxer->file;
1881 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
1883 demuxer->data_position = 0;
1884 demuxer->data_size = 0;
1886 demuxer->error_flag = mpeg3io_seek(title->fs, byte);
1888 if(!demuxer->error_flag &&
1889 file->is_transport_stream &&
1890 file->packet_size)
1892 /* Get on a packet boundary only for transport streams. */
1893 current_position = mpeg3io_tell(title->fs);
1894 if(byte % file->packet_size)
1896 demuxer->error_flag |= mpeg3io_seek(title->fs,
1897 current_position -
1898 (current_position % file->packet_size));
1903 // Get current cell
1904 for(demuxer->current_timecode = 0;
1905 demuxer->current_timecode < title->timecode_table_size;
1906 demuxer->current_timecode++)
1908 if(title->timecode_table[demuxer->current_timecode].start_byte <= byte &&
1909 title->timecode_table[demuxer->current_timecode].end_byte > byte)
1911 break;
1915 if(demuxer->current_timecode >= title->timecode_table_size)
1916 demuxer->current_timecode = title->timecode_table_size - 1;
1919 return demuxer->error_flag;
1922 int mpeg3demux_seek_end(mpeg3_demuxer_t *demuxer)
1924 mpeg3demux_open_title(demuxer, demuxer->total_titles - 1);
1925 demuxer->current_timecode = 0;
1926 return mpeg3demux_seek_byte(demuxer, demuxer->titles[demuxer->current_title]->total_bytes);
1929 int mpeg3demux_seek_start(mpeg3_demuxer_t *demuxer)
1931 mpeg3demux_open_title(demuxer, 0);
1932 demuxer->current_timecode = 0;
1933 return mpeg3demux_seek_byte(demuxer, 0);
1936 /* For programs streams and toc seek to a time */
1937 int mpeg3demux_seek_time(mpeg3_demuxer_t *demuxer, double new_time)
1939 int i, j, done = 0, result = 0;
1940 int64_t byte_offset, new_byte_offset;
1941 double guess = 0, minimum = 65535;
1942 mpeg3_title_t *title;
1943 mpeg3demux_timecode_t *timecode;
1945 demuxer->error_flag = 0;
1947 i = 0;
1948 j = 0;
1949 title = demuxer->titles[i];
1950 timecode = &title->timecode_table[j];
1953 /* Get the title and timecode of the new position */
1954 while(!demuxer->error_flag &&
1955 !(timecode->absolute_start_time <= new_time &&
1956 timecode->absolute_end_time > new_time &&
1957 timecode->program == demuxer->current_program))
1959 /* Next timecode */
1960 j++;
1961 if(j >= title->timecode_table_size)
1963 i++;
1964 j = 0;
1965 if(i >= demuxer->total_titles)
1967 demuxer->error_flag = 1;
1968 return 1;
1972 title = demuxer->titles[i];
1973 timecode = &title->timecode_table[j];
1976 //printf("mpeg3demux_seek_time 1 %d %f %f %f\n", i, timecode->absolute_start_time, timecode->absolute_end_time, new_time);
1977 if(demuxer->current_title != i)
1978 mpeg3demux_open_title(demuxer, i);
1987 /* Guess the new byte position by interpolating */
1988 demuxer->current_timecode = j;
1990 byte_offset = ((new_time - timecode->absolute_start_time) /
1991 (timecode->absolute_end_time - timecode->absolute_start_time) *
1992 (timecode->end_byte - timecode->start_byte) +
1993 timecode->start_byte);
1994 //printf("mpeg3demux_seek_time %f %f\n", new_time, byte_offset);
2005 /* Binary search */
2006 while(!done && !result && byte_offset >= 0)
2008 result = mpeg3demux_seek_byte(demuxer, (long)byte_offset);
2009 //printf("seek_time 0 byte %.0f want %f result %d\n", byte_offset, new_time, result);
2011 if(!result)
2013 result = mpeg3_read_next_packet(demuxer);
2014 // printf("seek_time 1 guess %f want %f\n", guess, new_time);
2015 guess = demuxer->time + demuxer->time_offset;
2017 if(fabs(new_time - guess) >= fabs(minimum)) done = 1;
2018 else
2020 minimum = guess - new_time;
2021 new_byte_offset = byte_offset + ((new_time - guess) /
2022 (timecode->end_time - timecode->start_time) *
2023 (timecode->end_byte - timecode->start_byte));
2024 if(fabs(new_byte_offset - byte_offset) < 1) done = 1;
2025 byte_offset = new_byte_offset;
2035 //printf("mpeg3demux_seek_time 1\n");
2038 /* Get one packet before the packet just read */
2039 if(!result && byte_offset > 0 && minimum > 0)
2041 mpeg3_read_prev_packet(demuxer);
2042 //printf("mpeg3demux_seek_time 2\n");
2043 mpeg3_read_prev_packet(demuxer);
2046 //printf("mpeg3demux_seek_time 3\n");
2049 //printf("mpeg3demux_seek_time 2 %d %d %d\n", demuxer->current_title, demuxer->current_timecode, mpeg3demux_tell(demuxer));
2050 demuxer->error_flag = result;
2051 return result;
2066 int mpeg3demux_seek_percentage(mpeg3_demuxer_t *demuxer, double percentage)
2068 int64_t absolute_position;
2069 int64_t relative_position;
2070 int i, new_title;
2071 mpeg3_title_t *title;
2072 int64_t total_bytes = mpeg3demux_movie_size(demuxer);
2074 demuxer->error_flag = 0;
2076 absolute_position = (int64_t)(percentage * total_bytes);
2084 /* Get the title the byte is inside */
2085 for(new_title = 0, total_bytes = 0; new_title < demuxer->total_titles; new_title++)
2087 total_bytes += demuxer->titles[new_title]->total_bytes;
2088 if(absolute_position < total_bytes) break;
2091 if(new_title >= demuxer->total_titles)
2093 new_title = demuxer->total_titles - 1;
2102 /* Got a title */
2103 title = demuxer->titles[new_title];
2104 total_bytes -= title->total_bytes;
2105 relative_position = absolute_position - total_bytes;
2114 /* Get the timecode the byte is inside */
2115 for(demuxer->current_timecode = 0;
2116 demuxer->current_timecode < title->timecode_table_size;
2117 demuxer->current_timecode++)
2119 if(title->timecode_table[demuxer->current_timecode].start_byte <= relative_position &&
2120 title->timecode_table[demuxer->current_timecode].end_byte > relative_position)
2122 break;
2126 if(demuxer->current_timecode >= title->timecode_table_size)
2127 demuxer->current_timecode = title->timecode_table_size - 1;
2135 /* Get the nearest timecode in the same program */
2136 while(demuxer->current_timecode < title->timecode_table_size - 1 &&
2137 title->timecode_table[demuxer->current_timecode].program != demuxer->current_program)
2139 demuxer->current_timecode++;
2143 * printf("seek percentage 1 %d %d %d %d\n", demuxer->current_title,
2144 * demuxer->current_timecode,
2145 * title->timecode_table[demuxer->current_timecode].start_byte,
2146 * title->timecode_table[demuxer->current_timecode].end_byte);
2149 /* Open the new title and seek to the correct byte */
2150 if(new_title != demuxer->current_title)
2152 demuxer->error_flag = mpeg3demux_open_title(demuxer, new_title);
2156 * printf("seek percentage 2 %d %d %d %d\n", demuxer->current_title,
2157 * demuxer->current_timecode,
2158 * title->timecode_table[demuxer->current_timecode].start_byte,
2159 * title->timecode_table[demuxer->current_timecode].end_byte);
2162 if(!demuxer->error_flag)
2163 demuxer->error_flag = mpeg3io_seek(title->fs, relative_position);
2164 //printf("mpeg3demux_seek_percentage %lld %lld\n", total_bytes, relative_position);
2166 return demuxer->error_flag;
2170 // Lots of optimization errors
2171 double mpeg3demux_tell_percentage(mpeg3_demuxer_t *demuxer)
2173 double total_bytes = 0;
2174 double position = 0;
2175 int i;
2177 demuxer->error_flag = 0;
2178 position = mpeg3io_tell_gcc(demuxer->titles[demuxer->current_title]->fs);
2181 for(i = 0; i < demuxer->total_titles; i++)
2183 double new_bytes = mpeg3_total_bytes_gcc(demuxer->titles[i]);
2184 if(i == demuxer->current_title)
2186 position = mpeg3_add_double_gcc(total_bytes, position);
2188 total_bytes = mpeg3_add_double_gcc(total_bytes, new_bytes);
2191 return position / total_bytes;
2194 double mpeg3demux_get_time(mpeg3_demuxer_t *demuxer)
2196 return demuxer->time;
2199 double mpeg3demux_audio_pts(mpeg3_demuxer_t *demuxer)
2201 return demuxer->pes_audio_time;
2204 double mpeg3demux_video_pts(mpeg3_demuxer_t *demuxer)
2206 return demuxer->pes_video_time;
2209 void mpeg3demux_reset_pts(mpeg3_demuxer_t *demuxer)
2211 demuxer->pes_audio_time = -1;
2212 demuxer->pes_video_time = -1;
2215 double mpeg3demux_scan_pts(mpeg3_demuxer_t *demuxer)
2217 double start_percentage = mpeg3demux_tell_percentage(demuxer);
2218 int64_t start_position = mpeg3demux_tell_absolute(demuxer);
2219 int64_t end_position = start_position + MPEG3_PTS_RANGE;
2220 int64_t current_position = start_position;
2221 int result = 0;
2223 mpeg3demux_reset_pts(demuxer);
2224 while(!result &&
2225 current_position < end_position &&
2226 ((demuxer->do_audio && demuxer->pes_audio_time < 0) ||
2227 (demuxer->do_video && demuxer->pes_video_time < 0)))
2229 result = mpeg3_read_next_packet(demuxer);
2230 current_position = mpeg3demux_tell_absolute(demuxer);
2233 // Seek back to starting point
2234 mpeg3demux_seek_percentage(demuxer, start_percentage);
2236 //printf("mpeg3demux_scan_pts %d %f\n", demuxer->do_audio, demuxer->pes_video_time);
2237 if(demuxer->do_audio) return demuxer->pes_audio_time;
2238 if(demuxer->do_video) return demuxer->pes_video_time;
2241 int mpeg3demux_goto_pts(mpeg3_demuxer_t *demuxer, double pts)
2243 int64_t start_position = mpeg3demux_tell_absolute(demuxer);
2244 int64_t end_position = start_position + MPEG3_PTS_RANGE;
2245 int64_t current_position = start_position;
2246 int result = 0;
2247 printf("mpeg3demux_goto_pts 1 %lld %f\n", mpeg3demux_tell_absolute(demuxer), pts);
2249 // Search forward for nearest pts
2250 mpeg3demux_reset_pts(demuxer);
2251 while(!result && current_position < end_position)
2253 result = mpeg3_read_next_packet(demuxer);
2254 // printf("mpeg3demux_goto_pts 2 %lld %f %f\n",
2255 // current_position,
2256 // demuxer->pes_audio_time,
2257 // fabs(pts - demuxer->pes_audio_time));
2258 if(demuxer->pes_audio_time > pts) break;
2259 current_position = mpeg3demux_tell_absolute(demuxer);
2262 // Search backward for nearest pts
2263 end_position = current_position - MPEG3_PTS_RANGE;
2264 mpeg3_read_prev_packet(demuxer);
2265 while(!result &&
2266 current_position > end_position)
2268 result = mpeg3_read_prev_packet(demuxer);
2269 // printf("mpeg3demux_goto_pts 3 %lld %f %f\n",
2270 // current_position,
2271 // demuxer->pes_audio_time,
2272 // fabs(pts - demuxer->pes_audio_time));
2273 if(demuxer->pes_audio_time < pts) break;
2274 current_position = mpeg3demux_tell_absolute(demuxer);
2276 printf("mpeg3demux_goto_pts 4 %lld %f\n", mpeg3demux_tell_absolute(demuxer), demuxer->pes_audio_time);
2279 int64_t mpeg3demux_tell_absolute(mpeg3_demuxer_t *demuxer)
2281 int i;
2282 int64_t result = 0;
2283 for(i = 0; i < demuxer->current_title; i++)
2285 result += demuxer->titles[i]->total_bytes;
2287 result += mpeg3io_tell(demuxer->titles[demuxer->current_title]->fs);
2288 return result;
2291 int64_t mpeg3demux_tell(mpeg3_demuxer_t *demuxer)
2293 return mpeg3io_tell(demuxer->titles[demuxer->current_title]->fs);
2296 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t *demuxer)
2298 int64_t result = 0;
2299 int i;
2300 for(i = 0; i < demuxer->total_titles; i++)
2301 result += demuxer->titles[i]->total_bytes;
2302 return result;
2305 int mpeg3demux_tell_title(mpeg3_demuxer_t *demuxer)
2307 return demuxer->current_title;
2310 int64_t mpeg3demuxer_total_bytes(mpeg3_demuxer_t *demuxer)
2312 mpeg3_title_t *title = demuxer->titles[demuxer->current_title];
2313 return title->total_bytes;
2316 mpeg3_demuxer_t* mpeg3_get_demuxer(mpeg3_t *file)
2318 if(file->is_program_stream || file->is_transport_stream)
2320 if(file->total_astreams) return file->atrack[0]->demuxer;
2321 else
2322 if(file->total_vstreams) return file->vtrack[0]->demuxer;
2324 return 0;