mask xy scale, mask boundary only overlay, fix 8 char mask nm bug, rework maskgui...
[goodguy/cinelerra.git] / cinelerra-5.1 / libzmpeg3 / libzmpeg3.h
1 #ifndef LIBMPEG3_H
2 #define LIBMPEG3_H
3 #ifdef HAVE_LIBZMPEG
4
5 /* for quicktime build */
6 #define MAXFRAMESAMPLES 65536
7 #define ZDVB
8 #define USE_FUTEX
9
10 #ifndef __STDC_LIMIT_MACROS
11 #define __STDC_LIMIT_MACROS
12 #endif
13
14 #include <stdio.h>
15 #include <stdint.h>
16
17 typedef int (*zthumbnail_cb)(void *p, int trk);
18 typedef int (*zcc_text_cb)(int sid, int id, int sfrm, int efrm, const char *txt);
19
20 #ifdef __cplusplus
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <byteswap.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <errno.h>
29 #include <endian.h>
30 #include <signal.h>
31 #include <pthread.h>
32 #include <mntent.h>
33 #include <linux/cdrom.h>
34 #include <sys/ioctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/syscall.h>
39 #ifdef USE_FUTEX
40 #include <linux/futex.h>
41 #endif
42
43 extern "C" {
44 #include "a52.h"
45 }
46
47 #define ZMPEG3_MAJOR   2
48 #define ZMPEG3_MINOR   0
49 #define ZMPEG3_RELEASE 0
50 #define ZRENDERFARM_FS_PREFIX "vfs://"
51
52 #define ZIO_SINGLE_ACCESS zfs_t::io_SINGLE_ACCESS
53 #define ZIO_UNBUFFERED zfs_t::io_UNBUFFERED
54 #define ZIO_NONBLOCK zfs_t::io_NONBLOCK
55 #define ZIO_SEQUENTIAL zfs_t::io_SEQUENTIAL
56 #define ZIO_THREADED zfs_t::io_THREADED
57 #define ZIO_ERRFAIL zfs_t::io_ERRFAIL
58 #define ZIO_RETRIES 1
59
60 #define TOC_SAMPLE_OFFSETS zmpeg3_t::show_toc_SAMPLE_OFFSETS
61 #define TOC_AUDIO_INDEX zmpeg3_t::show_toc_AUDIO_INDEX
62 #define TOC_FRAME_OFFSETS zmpeg3_t::show_toc_FRAME_OFFSETS
63
64 #define ZMPEG3_PROC_CPUINFO "/proc/cpuinfo"
65 #if BYTE_ORDER == LITTLE_ENDIAN
66 #define ZMPEG3_LITTLE_ENDIAN 1
67 #else
68 #define ZMPEG3_LITTLE_ENDIAN 0
69 #endif
70
71 // Combine the pid and the stream id into one unit
72 #define CUSTOM_ID(pid, stream_id) (((pid << 8) | stream_id) & 0xffff)
73 #define CUSTOM_ID_PID(id) (id >> 8)
74 #define CUSTOM_ID_STREAMID(id) (id & 0xff)
75
76 #ifndef ZMAX
77 #define ZMAX(a, b) ((a) > (b) ? (a) : (b))
78 #endif
79
80 #ifndef ZMIN
81 #define ZMIN(a, b) ((a) < (b) ? (a) : (b))
82 #endif
83
84 #ifndef isizeof
85 #define isizeof(x) ((int)sizeof(x))
86 #endif
87 #ifndef lengthof
88 #define lengthof(x) ((int)(sizeof(x)/sizeof(x[0])))
89 #endif
90
91 #define zlikely(x)   __builtin_expect((x),1)
92 #define zunlikely(x) __builtin_expect((x),0)
93
94 #define bcd(n) ((((n)>>4)&0x0f)*10+((n)&0x0f))
95
96 #define new_memset(s) \
97   void *operator new(size_t n) { \
98     void * volatile t = (void*) new char[n]; \
99     memset(t,s,n); \
100     return t; \
101   } \
102   void operator delete(void *t,size_t n) { \
103     delete[](char*)t; \
104   } \
105   void *operator new[](size_t n) { \
106     void * volatile t = (void*) new char[n]; \
107     memset(t,s,n); \
108     return t; \
109   } \
110   void operator delete[](void *t,size_t n) { \
111     delete[](char*)t; \
112   }
113
114 #define znew(T,n) ((T*)memset(new T[n],0,(n)*sizeof(T)))
115 #define zmsg(fmt) printf("%s: " fmt, __func__)
116 #define zmsgs(fmt,args...) printf("%s: " fmt, __func__, args)
117 #define zerr(fmt) fprintf(stderr,"%s:err " fmt, __func__)
118 #define zerrs(fmt,args...) fprintf(stderr,"%s:err " fmt, __func__, args)
119 #define perr(fmt) do { char msg[512]; \
120  snprintf(&msg[0],sizeof(msg), "%s: " fmt,__func__); perror(&msg[0]); \
121 } while(0)
122 #define perrs(fmt,args...) do { char msg[512]; \
123  snprintf(&msg[0],sizeof(msg), "%s: " fmt,__func__,args); perror(&msg[0]); \
124 } while(0)
125
126 class zmpeg3_t;
127
128 class zmpeg3_t {
129 public:
130   new_memset(0);
131   class zlock_t;
132   class zblock_t;
133   class zrwlock_t;
134   class fs_t;
135   class bits_t;
136   class index_t;
137   class bitfont_t;
138   class timecode_t;
139   class audio_t;
140   class slice_buffer_t;
141   class video_t;
142   class slice_decoder_t;
143   class subtitle_t;
144   class demuxer_t;
145   class cacheframe_t;
146   class cache_t;
147   class atrack_t;
148   class vtrack_t;
149   class strack_t;
150 #ifdef ZDVB
151   class dvb_t;
152 #endif
153
154   enum {
155     // Error codes for the error_return variable
156     ERR_UNDEFINED_ERROR              = 1,
157     ERR_INVALID_TOC_VERSION          = 2,
158     ERR_TOC_DATE_MISMATCH            = 3,
159
160     TOC_PREFIX                       = 0x544f4320,
161     // This decreases with every new version
162     TOC_VERSION                      = 0x000000f9,
163     ID3_PREFIX                       = 0x494433,
164     IFO_PREFIX                       = 0x44564456,
165     // First byte to read when opening a file
166     START_BYTE                       = 0x0,
167     // Bytes read by mpeg3io at a time
168     MAX_IO_SIZE                      = 0x10000,
169     IO_SIZE                          = 0x80000,
170     SEQ_IO_SIZE                      = 0xc00000,
171     MAX_TS_PROBE                     = 0x800000,
172     MAX_PGM_PROBE                    = 0x800000,
173     // Largest possible packet
174     RAW_SIZE                         = 0x400000,
175     RIFF_CODE                        = 0x52494646,
176     BD_PACKET_SIZE                   = 192,
177     TS_PACKET_SIZE                   = 188,
178     DVD_PACKET_SIZE                  = 0x800,
179     ERR_PACKET_SIZE                  = 0x800,
180     IO_ERR_LIMIT                     = 2,
181     SYNC_BYTE                        = 0x47,
182     PROGRAM_END_CODE                 = 0x000001b9,
183     PACK_START_CODE                  = 0x000001ba,
184     SEQUENCE_START_CODE              = 0x000001b3,
185     SEQUENCE_END_CODE                = 0x000001b7,
186     SYSTEM_START_CODE                = 0x000001bb,
187     STRLEN                           = 1024,
188     // Maximum number of PIDs in one stream
189     PIDMAX                          = 256,
190     PROGRAM_ASSOCIATION_TABLE       = 0x00,
191     CONDITIONAL_ACCESS_TABLE        = 0x01,
192     PACKET_START_CODE_PREFIX        = 0x000001,
193     // NAV/PRIVATE_STREAM_2 are the same id
194     PRIVATE_STREAM_2                = 0xbf,
195     NAV_PACKET_STREAM               = 0xbf,
196     NAV_PCI_BYTES                   = 0x3d3,
197     NAV_DSI_BYTES                   = 0x3f9,
198     NAV_PCI_SSID                    = 0x00,
199     NAV_DSI_SSID                    = 0x01,
200     NAV_SRI_END_OF_CELL             = 0x3fffffff,
201     PADDING_STREAM                  = 0xbe,
202     GOP_START_CODE                  = 0x000001b8,
203     PICTURE_START_CODE              = 0x00000100,
204     EXT_START_CODE                  = 0x000001b5,
205     USER_START_CODE                 = 0x000001b2,
206     SLICE_MIN_START                 = 0x00000101,
207     SLICE_MAX_START                 = 0x000001af,
208     AC3_START_CODE                  = 0x0b77,
209     PCM_START_CODE                  = 0x7f7f807f,
210     MAX_CPUS                        = 256,
211     MAX_STREAMZ                     = 0x10000,
212     MAX_PACKSIZE                    = 262144,
213     MAX_CACHE_FRAMES                = 128,
214     // Maximum number of complete subtitles to buffer in a subtitle track
215     // or number of incomplete subtitles to buffer in demuxer.
216     MAX_SUBTITLES                   = 16,
217     // Positive difference before declaring timecodes discontinuous
218     CONTIGUOUS_THRESHOLD            = 10  ,
219     // Minimum number of seconds before interleaving programs
220     PROGRAM_THRESHOLD               = 5   ,
221     // Number of frames difference before absolute seeking
222     SEEK_THRESHOLD                  = 16  ,
223     // Size of chunk of audio in table of contents
224     AUDIO_CHUNKSIZE                 = 0x10000 ,
225     // Minimum amount of data required to read a video header in streaming mode.
226     VIDEO_STREAM_SIZE               = 0x100,
227     // Number of samples in audio history
228     AUDIO_HISTORY                   = 0x100000 ,
229     // max number of samples returned by decoder per input frame
230     AUDIO_MAX_DECODE                = 0x800 ,
231     // Range to scan for pts after byte seek
232     PTS_RANGE                       = 0x100000,
233     // stream type
234     FT_PROGRAM_STREAM               = 0x0001,
235     FT_TRANSPORT_STREAM             = 0x0002,
236     FT_AUDIO_STREAM                 = 0x0004,
237     FT_VIDEO_STREAM                 = 0x0008,
238     FT_IFO_FILE                     = 0x0010,
239     FT_BD_FILE                      = 0x0020,
240     SUBTITLE_HOLD_TIME              = 3,
241     CCAPTION_HOLD_TIME              = 15,
242   };
243
244   enum color_model {
245     // All color models supported for read_frame
246     cmdl_BGR888=0,
247     cmdl_BGRA8888=1,
248     cmdl_RGB565=2,
249     cmdl_RGB888=3,
250     cmdl_RGBA8888=4,
251     cmdl_RGBA16161616=5,
252     // Color models for the 601 to RGB conversion
253     // 601 not implemented for scalar code
254     cmdl_601_BGR888=7,
255     cmdl_601_BGRA8888=8,
256     cmdl_601_RGB888=9,
257     cmdl_601_RGBA8888=10,
258     cmdl_601_RGB565=11,
259     // next 2 are supported color models for read_yuvframe
260     //  also YVU420P/YVU422P with uv output_row ptrs reversed for read_frame
261     cmdl_YUV420P=12,
262     cmdl_YUV422P=13,
263     // the rest are supported color models for read_frame
264     cmdl_601_YUV420P=14,
265     cmdl_601_YUV422P=15,
266     cmdl_UYVY=16,
267     cmdl_YUYV=17,
268     cmdl_601_UYVY=18,
269     cmdl_601_YUYV=19,
270     cmdl_YUV888=20,
271     cmdl_YUVA8888=21,
272     cmdl_601_YUV888=22,
273     cmdl_601_YUVA8888=23,
274   };
275
276   // Values for audio input/output formats
277   enum audio_format {
278     afmt_IGNORE=-1,
279     afmt_UNKNOWN=0,
280     afmt_MPEG=1,
281     afmt_AC3=2,
282     afmt_PCM=3,
283     afmt_AAC=4,
284     afmt_JESUS=5,
285
286     atyp_NONE=0,
287     atyp_DOUBLE=1,
288     atyp_FLOAT=2,
289     atyp_INT=3,
290     atyp_SHORT=4,
291   };
292
293   // Table of contents sections
294   enum toc_section_type {
295     toc_FILE_TYPE_PROGRAM=0,
296     toc_FILE_TYPE_TRANSPORT=1,
297     toc_FILE_TYPE_AUDIO=2,
298     toc_FILE_TYPE_VIDEO=3,
299     toc_STREAM_AUDIO=4,
300     toc_STREAM_VIDEO=5,
301     toc_STREAM_SUBTITLE=6,
302     toc_OFFSETS_AUDIO=7,
303     toc_OFFSETS_VIDEO=8,
304     toc_ATRACK_COUNT=9,
305     toc_VTRACK_COUNT=10,
306     toc_STRACK_COUNT=11,
307     toc_TITLE_PATH=12,
308     toc_IFO_PALETTE=13,
309     toc_FILE_INFO=14,
310     toc_IFO_PLAYINFO=15,
311     toc_SRC_PROGRAM=16,
312   };
313
314
315 #ifdef USE_FUTEX
316 #define ZMPEG3_ZLOCK_INIT zzlock_t()
317   class zloc_t {
318   protected:
319     volatile int loc;
320     int zfutex(int op, int val, timespec *time=0) {
321       return syscall(SYS_futex,&loc,op,val,time,0,0);
322     }
323     int zyield();
324     int zgettid();
325   public:
326     int zwake(int nwakeups);
327     int zwait(int val);
328     int zwait() { return zwait(loc); }
329     static void zincr(volatile int &v) { /* atomic(++v) */
330       asm ( " lock incl %1\n" : "+m" (v) :: );
331     }
332     static void zdecr(volatile int &v) { /* atomic(--v) */
333       asm ( " lock decl %1\n" : "+m" (v) :: );
334     }
335     static char tdecr(volatile int &v) {
336       char ret; /* ret = atomic(--loc >= 0 ? 1 : 0) */
337       asm ( " lock decl %1\n setge %0\n" : "=r" (ret), "+m" (v) :: );
338       return ret;
339     }
340     static char tincr(volatile int &v) {
341       char ret; /* ret = atomic(++loc > 0 ? 1 : 0) */
342       asm ( " lock incl %1\n setg %0\n" : "=r" (ret), "+m" (v) :: );
343       return ret;
344     }
345     static int zcmpxchg(int old, int val, volatile int &v) {
346       int ret = old;
347       asm volatile( " lock\n cmpxchgl %2,%1\n"
348         : "+a" (ret), "+m" (v) :  "r" (val) : "memory" );
349       return ret;
350     }
351     static int zxchg(int val, volatile int &v) {
352       asm volatile( " xchgl %0,%1\n"
353         : "+r" (val), "+m" (v) :: "memory" );
354       return val;
355     }
356     static int zadd(int n, volatile int &v) {
357       int old, mod, val;
358       do { val = (old=v)+n; mod = zcmpxchg(old,val,v);
359       } while( mod != old );
360       return val;
361     }
362     static void zmfence() {
363       asm volatile ( " mfence\n" ::: "memory" );
364     }
365
366     zloc_t() : loc(-1) {}
367     ~zloc_t() {}
368   };
369
370   class zlock_t : zloc_t {
371   protected:
372     friend class zblock_t;
373     friend class zrwlock_t;
374     int zlock(int v);
375     int zunlock(int nwakeups=1);
376     static int zemsg1();
377   public:
378     int lock() {
379       int v, ret = zunlikely( (v=zcmpxchg(-1,0,loc)) >= 0 ) ? zlock(v) : 0;
380       return ret;
381     }
382     int unlock() {
383       if( zunlikely(loc < 0) ) { return zemsg1(); }
384       int v, ret = zunlikely( (v=zcmpxchg(0,-1,loc)) != 0 ) ? zunlock() : 0;
385       return ret;
386     }
387     zlock_t() {}
388     ~zlock_t() {}
389   };
390
391   class zblock_t : zlock_t {
392   public:
393     void block() { loc = 0; zwait(0); }
394     void unblock() { if( !loc ) { loc = -1; zwake(INT_MAX); } }
395     zblock_t() {}
396     ~zblock_t() {}
397   };
398
399   class zrwlock_t : zloc_t {
400     zlock_t lk;
401     void zenter();
402     void zleave();
403     void zwrite_enter(int r); // r==0: do nothing, r>0: write_lock
404     void zwrite_leave(int r); // r<0: write_lock while read_lock
405   public:
406     void enter() { zincr(loc); if( zunlikely( lk.loc >= 0 ) ) zenter(); }
407     void leave() { if( zunlikely( !tdecr(loc) ) ) zleave(); }
408     void write_enter(int r=1) { if( r ) zwrite_enter(r); }
409     void write_leave(int r=1) { if( r ) zwrite_leave(r); }
410     zrwlock_t() {}
411     ~zrwlock_t() {}
412   };
413
414 #else
415 #define ZMPEG3_ZLOCK_INIT { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER }
416
417   class zlock_t {
418   protected:
419     pthread_mutex_t zlock;
420   public:
421     void lock() { pthread_mutex_lock(&zlock); }
422     void unlock() { pthread_mutex_unlock(&zlock); }
423     zlock_t() { pthread_mutex_init(&zlock, 0); }
424     ~zlock_t() { pthread_mutex_destroy(&zlock); }
425   };
426
427   class zblock_t {
428     pthread_mutex_t zblock;
429     pthread_cond_t cond;
430   public:
431     zblock_t() {
432       pthread_mutex_init(&zblock, 0);
433       pthread_cond_init(&cond, 0);
434     }
435     ~zblock_t() {
436       pthread_mutex_destroy(&zblock);
437       pthread_cond_destroy(&cond);
438     }
439     void block() {
440       pthread_mutex_lock(&zblock);
441       pthread_cond_wait(&cond, &zblock);
442       pthread_mutex_unlock(&zblock);
443     }
444     void unblock() { pthread_cond_broadcast(&cond); }
445   };
446
447   class zrwlock_t : zlock_t {
448     volatile pthread_t blocking;
449     volatile int users;
450     zlock_t lk;
451     pthread_cond_t cond;
452     void wait() { pthread_cond_wait(&cond, &zlock); }
453     void wake() { pthread_cond_signal(&cond); }
454     void zwrite_enter(int r);
455     void zwrite_leave(int r);
456   public:
457     void enter();
458     void leave();
459     void write_enter(int r=1) { if( r ) zwrite_enter(r); }
460     void write_leave(int r=1) { if( r ) zwrite_leave(r); }
461     int count() { return users; }
462
463     zrwlock_t() { pthread_cond_init(&cond, 0); }
464     ~zrwlock_t() { pthread_cond_destroy(&cond); }
465   };
466
467 #endif
468
469   // I/O
470
471   // Filesystem structure
472   // We buffer in IO_SIZE buffers.  Stream IO would require back
473   // buffering a buffer since the stream must be rewound for packet headers,
474   // sequence start codes, format parsing, decryption, and mpeg3cat.
475
476   class fs_t {
477     int sync() {
478       return buffer->sync(current_byte);
479     }
480     int enter() {
481       buffer->lock();
482       return sync();
483     }
484     void leave() {
485       buffer->unlock();
486     }
487   public:
488     new_memset(0);
489     class css_t;
490
491     class css_t { // Encryption object
492     public:
493       class playkey_t;
494       class keytext_t;
495
496       typedef uint8_t key_t[5];
497       typedef uint8_t blk_t[5];
498       class playkey_t {
499       public:
500         int offset;
501         key_t key;
502       };
503       class keytext_t {
504       public:
505         keytext_t *lt, *rt;
506         uint8_t ktxt[4], ctxt[10];
507         keytext_t(const uint8_t *kp, const uint8_t *cp) {
508           lt = rt = 0;
509           memcpy(&ktxt[0],kp,sizeof(ktxt));
510           memcpy(&ctxt[0],cp,sizeof(ctxt));
511         }
512         ~keytext_t() {}
513       } *key_root;
514       void add_key_text(const uint8_t *kp, const uint8_t *cp);
515       uint8_t *find_key_text(const uint8_t *kp);
516       int attack_pattern(uint8_t const *sec, uint8_t *key);
517       void del_keytexts(keytext_t *p);
518     private:
519       void css_engine(int v,uint8_t const *input, blk_t &output);
520       void crypt_key1(int v, blk_t &key);
521       void crypt_key2(blk_t &key);
522       void crypt_bus_key(blk_t &key);
523       int get_asf();
524       int authenticate_drive(key_t &key);
525       int hostauth(dvd_authinfo *ai);
526       int get_title_key(key_t &key);
527       int get_disk_key(key_t &key);
528       int validate(int do_title);
529       int validate_path(int do_title);
530       int decrypt_title_key(uint8_t *dkey, uint8_t *tkey);
531     public:
532       key_t key1;
533       key_t key2;
534       key_t keycheck;
535       key_t title_key;
536       int lba, fsz, agid, encrypted;
537       char device_path[STRLEN];    // Device where the file is located
538       uint8_t disk_key[DVD_PACKET_SIZE];
539       char challenge[10];
540       int varient;
541       int fd;
542       char path[STRLEN];
543
544       css_t();
545       ~css_t();
546
547       int get_keys(char *fpath);
548       int decrypt_packet(uint8_t *sector, int offset);
549       int crack_title_key(key_t &ckey, key_t &tkey);
550     } css;
551
552     enum access_strategy {
553       io_SINGLE_ACCESS=1,
554       io_UNBUFFERED=2,
555       io_NONBLOCK=4,
556       io_SEQUENTIAL=8,
557       io_THREADED=16,
558       io_ERRFAIL=32,
559     };
560
561     class buffer_t {
562       void unblock();
563       void block();
564       void reader();
565       static void *reader(void *the_buffer);
566       void writer();
567       static void *writer(void *the_buffer);
568       int64_t read_in(int64_t len);
569       int read_fin(int64_t len);
570       int wait_in(int64_t pos);
571       int seek_in(int64_t pos);
572       int seek_to(int64_t pos, int64_t len);
573       int read_to(int64_t pos);
574       zmpeg3_t *src;
575       zlock_t io_lock;          // buffer_t access lock
576       zblock_t io_block;        // buffer_t access block
577       int errs;                 // number of consecutive errors
578       uint8_t *data;            // Readahead buffer
579       int alloc;                // buffer byte allocate
580       int size;                 // defined bytes in buffer
581       int in;                   // buffer byte in, owned by read_in
582       int out;                  // buffer byte out
583       int64_t out_pos;          // file offset next byte from last read
584       int64_t file_pos;         // file position of next readable byte
585       int64_t file_nudge;       // sync position offset
586       int fin;                  // locked version of in, synced with file_pos
587       pthread_t the_reader;     // reader thread if io_THREADED
588       pthread_t the_writer;     // writer thread if io_THREADED and recording
589       pthread_t owner;          // for debugging
590       int restarted;            // reader restarted
591       int reader_done;          // reader thread termination flag
592       int writer_done;          // writer thread termination flag
593       int wout;                 // write record file buffer byte out
594       int64_t write_pos;        // file_pos written to recd_fd
595       zblock_t write_lock;      // write record block
596     public:
597       new_memset(0);
598       buffer_t(zmpeg3_t *zsrc, int access);
599       ~buffer_t();
600       int access_type;          // io_ flags
601       int paused;               // reader discard data
602       int do_restart;           // reader restart buffer
603       int ref_count;            // number of fs_t referencing this
604       int open_count;           // number of is_open fs_t
605       int fd;                   // unbuffered file descriptor
606       FILE *fp;                 // buffered file handle
607       int open_file(char *path);
608       void close_file();
609       void reset();
610       void restart(int lk=1);
611       int64_t tell() { return out_pos; }
612       int64_t file_tell() { return file_pos; }
613       int sync(int64_t pos);
614       int errors() { return errs; }
615       uint32_t next_byte() {
616         return data[out];
617       }
618       uint32_t get_byte() {
619         uint32_t result = next_byte();
620         if( ++out >= alloc ) out = 0;
621         ++out_pos;
622         return result;
623       }
624       int read_out(uint8_t *bfr,int len);
625       int get_fd() {
626         return (access_type & io_UNBUFFERED) ? fd : fileno(fp);
627       }
628       void lock() {
629         if( (access_type & io_SINGLE_ACCESS) )
630           io_lock.lock();
631         if( !data ) data = new uint8_t[alloc];
632       }
633       void unlock() {
634         if( (access_type & io_SINGLE_ACCESS) )
635           io_lock.unlock();
636       }
637       void start_reader();
638       void stop_reader();
639       int start_record();
640       int stop_record();
641       int write_align(int sz);
642       void write_record(int sz, int mask);
643     } *buffer;
644
645     zmpeg3_t *src;
646     char path[STRLEN];
647     int64_t current_byte;       // Hypothetical position of file pointer
648     int64_t total_bytes;
649     int is_open;
650
651     fs_t(zmpeg3_t *zsrc, const char *fpath, int access=io_UNBUFFERED+io_SINGLE_ACCESS);
652     fs_t(zmpeg3_t *zsrc, int fd, int access=io_UNBUFFERED+io_SINGLE_ACCESS);
653     fs_t(zmpeg3_t *zsrc, FILE *fp, int access=io_SINGLE_ACCESS);
654     fs_t(fs_t &fs);
655     ~fs_t();
656     int open_file();
657     void close_file();
658     int errors() { return buffer ? buffer->errors() : 0; }
659     int get_fd() { return buffer ? buffer->get_fd() : -1; }
660     int read_data(uint8_t *bfr, int64_t len);
661     uint8_t next_char();
662     uint8_t read_char();
663     uint32_t read_uint16();
664     uint32_t read_uint24();
665     uint32_t read_uint32();
666     uint64_t read_uint64();
667     int seek(int64_t byte);
668     int seek_relative(int64_t bytes);
669     int64_t tell() { return current_byte; }
670     int64_t eof() { return current_byte >= total_bytes; }
671     int64_t bof() { return current_byte < 0; }
672     int64_t get_total_bytes();
673     int64_t ztotal_bytes() { return total_bytes; }
674     void chk_next() {
675       if( ++current_byte >= buffer->file_tell() ) sync();
676     }
677     void restart();
678     int pause_reader(int v);
679     int access() { return !buffer ? 0 : buffer->access_type; }
680     void sequential() { buffer->access_type |= io_SEQUENTIAL; }
681     int start_record(int bsz);
682     int stop_record();
683     void attach(fs_t *zfs);
684     void detach();
685     void close_buffer();
686   };
687
688
689   // Bitstream                                      
690   //    next bit in forward direction               
691   //  next bit in reverse direction |               
692   //                              v v               
693   // ... | | | | | | | | |1|1|1|1|1|1|              
694   //                     ^         ^                
695   //                     |         bit_number = 1   
696   //                     bfr_size = 6               
697
698   class bits_t {
699   public:
700     new_memset(0);
701     zmpeg3_t *src;
702     uint32_t bfr;   // bfr = buffer for bits
703     int bit_number; // position of pointer in bfr
704     int bfr_size;   // number of bits in bfr.  Should always be a multiple of 8
705     demuxer_t *demuxer; // Mpeg2 demuxer
706     uint8_t *input_ptr; // when input_ptr!=0, data is read from it instead of the demuxer.
707
708     bits_t(zmpeg3_t *zsrc, demuxer_t *demux);
709     ~bits_t();
710     int read_buffer(uint8_t *buffer, int byte);
711     void use_ptr(uint8_t *buffer);
712     int eos(uint8_t *ptr) {
713       return input_ptr - (bit_number+7)/8 >= ptr;
714     }
715     int eos(uint8_t *ptr,int bits) {
716       int n = bits - bit_number;
717       if( n <= 0 ) return 0;
718       return input_ptr + (n+7)/8 > ptr;
719     }
720     void use_demuxer();
721     void start_reverse();
722     void start_forward();
723     int refill_ptr();
724     int refill_noptr();
725     int refill() { return input_ptr ? refill_ptr() : refill_noptr(); }
726     int refill_reverse();
727     int open_title(int title);
728     int seek_byte(int64_t position);
729     void reset() { bfr_size = bit_number = 0; }
730     void reset(uint32_t v) { bfr = v; bfr_size = bit_number = 32; }
731     int get_bit_offset() { return bit_number & 7; }
732     void prev_byte_align() { bit_number = (bit_number+7) & ~7; }
733     void next_byte_align() { bit_number &= ~7; }
734     int bof() { return demuxer->zdata.bof() && demuxer->bof(); }
735     int eof() { return demuxer->zdata.eof() && demuxer->eof(); }
736     int error() { return demuxer->error(); }
737     int64_t tell();
738     int next_code(uint32_t zcode);
739     int prev_code(uint32_t zcode);
740     int next_start_code();
741     void pack_bits(uint32_t v, int n) {
742       bfr = (bfr << n) | v;
743       if( (bfr_size+=n) > (int)(8*sizeof(bfr)) )
744          bfr_size = 8*sizeof(bfr);
745       bit_number += n;
746     }
747     void pack_8bits(uint32_t v) { pack_bits(v, 8); }
748     void fill_bits_ptr(int bits) {
749       while( bit_number < bits ) { pack_8bits(*input_ptr++); }
750     }
751     void fill_bits_noptr(int bits) {
752       while( bit_number < bits ) { pack_8bits(demuxer->read_char()); }
753     }
754     void fill_bits(int bits) {
755       return input_ptr ? fill_bits_ptr(bits) : fill_bits_noptr(bits);
756     }
757     uint32_t show_bits_noptr(int bits) {
758       if( bits <= 0 ) return 0;
759       fill_bits_noptr(bits);
760       return (bfr >> (bit_number-bits)) & (0xffffffff >> (32-bits));
761     }
762     uint32_t show_bit_noptr(int bits) { return show_bits_noptr(1); }
763     uint32_t show_byte_noptr() { return show_bits_noptr(8); }
764     uint32_t show_bits24_noptr() { return show_bits_noptr(24); }
765     uint32_t show_bits32_noptr() { fill_bits_noptr(32); return bfr; }
766     int show_bits(int bits) {
767       if( bits <= 0 ) return 0;
768       fill_bits(bits);
769       return (bfr >> (bit_number-bits)) & (0xffffffff >> (32-bits));
770     }
771     uint32_t get_bits_noptr(int bits) {
772       uint32_t result = show_bits_noptr(bits);
773       bit_number -= bits;
774       return result;
775     }
776     uint32_t get_bit_noptr() { return get_bits_noptr(1); }
777     int get_bits(int bits) {
778       uint32_t result = show_bits(bits);
779       bit_number -= bits;
780       return result;
781     }
782     uint32_t get_byte_noptr() { return get_bits_noptr(8); }
783     uint32_t get_bits24_noptr() { return get_bits_noptr(24); }
784     void fill_bits_reverse(int bits) {
785       int n = (bit_number & ~7);
786       if( n ) { bfr >>= n;  bfr_size -= n;  bit_number -= n; }
787       n = bit_number + bits;
788       while( bfr_size < n ) {
789         uint32_t prev_byte = input_ptr ?
790           *--input_ptr : demuxer->read_prev_char();
791         bfr |= prev_byte << bfr_size;
792         bfr_size += 8;
793       }
794     }
795     int show_bits_reverse(int bits) {
796       fill_bits_reverse(bits);
797       return (bfr >> bit_number) & (0xffffffff >> (32 - bits));
798     }
799     int get_bits_reverse(int bits) {
800       uint32_t result = show_bits_reverse(bits);
801       bit_number += bits;
802       return result;
803     }
804   };
805
806
807   class index_t {
808   public:
809     new_memset(0);
810     float **index_data; // Buffer of frames for index.  A frame is a high/low pair.
811     int index_allocated; // Number of frames allocated in each index channel.
812     int index_channels; // Number of index channels allocated
813     int index_size; // Number of high/low pairs in index channel
814     int index_zoom; // Downsampling of index buffers when constructing index
815
816     index_t();
817     ~index_t();
818   };
819
820
821   class bitfont_t {
822   public:
823     class bitchar_t;
824     static bitfont_t *fonts[];
825     static int total_fonts;
826     static int font_refs;
827     static void init_fonts();
828     static void destroy_fonts();
829     class static_init_t {
830     public:
831       static_init_t();
832       ~static_init_t();
833     };
834     class bitchar_t {
835     public:
836       uint16_t ch;
837       int16_t left, right;
838       int16_t ascent, decent;
839       uint8_t *bitmap;
840     } *chrs;
841     int16_t nch, idx, idy, imy;
842   };
843   static bitfont_t *bitfont(int style,int pen_size,int italics,int size);
844
845   class timecode_t {
846   public:
847     new_memset(0);
848     int hour;
849     int minute;
850     int second;
851     int frame;
852
853     timecode_t() {}
854     ~timecode_t() {}
855   };
856
857
858   class audio_t {
859   public:
860     new_memset(0);
861     class imdct_complex_t;
862     class imdct_al_table_t;
863     class audio_decoder_layer_t;
864     class audio_decoder_ac3_t;
865     class audio_decoder_pcm_t;
866     class ac3audblk_t; /*???*/
867
868     enum {
869       AC3_N                          = 512,
870       MAXFRAMESIZE                   = 4096,
871       MAXFRAMESAMPLEZ                = MAXFRAMESAMPLES,
872       HDRCMPMASK                     = 0xfffffd00,
873       SBLIMIT                        = 32,
874       SSLIMIT                        = 18,
875       SCALE_BLOCK                    = 12,
876       MPEG3AUDIO_PADDING             = 1024,
877       MAX_AC3_FRAMESIZE              = 1920 * 2 + 512,
878     };
879
880     enum exponent_type { // Exponent strategy constants
881       exp_REUSE=0,
882       exp_D15=1,
883       exp_D25=2,
884       exp_D45=3,
885     };
886
887     enum delta_type { // Delta bit allocation constants
888       bit_REUSE=0,
889       bit_NEW=1,
890       bit_NONE=2,
891       bit_RESERVED=3,
892     };
893
894     enum mpg_md { // Values for mode
895       md_STEREO=0,
896       md_JOINT_STEREO=1,
897       md_DUAL_CHANNEL=2,
898       md_MONO=3,
899     };
900
901     enum { PCM_HEADERSIZE = 20, };
902
903     enum id3_state {
904       id3_IDLE=0, // No ID3 tag found
905       id3_HEADER=1, // Reading header
906       id3_SKIP=2, // Skipping ID3 tag
907     };
908
909     // IMDCT variables
910     class imdct_complex_t {
911     public:
912       float real;
913       float imag;
914     };
915
916     class imdct_al_table_t {
917     public:
918       short bits;
919       short d;
920     };
921
922     zmpeg3_t *src;
923     atrack_t *track;
924
925     class l3_info_t {
926     public:
927       int scfsi;
928       uint32_t part2_3_length;
929       uint32_t big_values;
930       uint32_t scalefac_compress;
931       uint32_t block_type;
932       uint32_t mixed_block_flag;
933       uint32_t table_select[3];
934       uint32_t subblock_gain[3];
935       uint32_t maxband[3];
936       uint32_t maxbandl;
937       uint32_t maxb;
938       uint32_t region1start;
939       uint32_t region2start;
940       uint32_t preflag;
941       uint32_t scalefac_scale;
942       uint32_t count1table_select;
943       float *full_gain[3];
944       float *pow2gain;
945     };
946
947     class l3_sideinfo_t {
948     public:
949       uint32_t main_data_begin;
950       uint32_t private_bits;
951       class { public: l3_info_t gr[2]; } ch[2];
952     };
953
954     class audio_decoder_layer_t {
955       static int tabsel_123[2][3][16];
956       static long freqs[9];
957       static float decwin[512 + 32];
958       static float cos64[16], cos32[8], cos16[4], cos8[2], cos4[1];
959       static float *pnts[5];
960       static int grp_3tab[32 * 3];   /* used: 27 */
961       static int grp_5tab[128 * 3];  /* used: 125 */
962       static int grp_9tab[1024 * 3]; /* used: 729 */
963       static float muls[27][64];     /* also used by layer 1 */
964       static float gainpow2[256 + 118 + 4];
965       static long intwinbase[257];
966       static float ispow[8207];
967       static float aa_ca[8], aa_cs[8];
968       static float win[4][36];
969       static float win1[4][36];
970       static float COS1[12][6];
971       static float COS9[9];
972       static float COS6_1, COS6_2;
973       static float tfcos36[9];
974       static float tfcos12[3];
975       static float cos9[3], cos18[3];
976       static float tan1_1[16], tan2_1[16], tan1_2[16], tan2_2[16];
977       static float pow1_1[2][16], pow2_1[2][16];
978       static float pow1_2[2][16], pow2_2[2][16];
979       static int longLimit[9][23];
980       static int shortLimit[9][14];
981       static class bandInfoStruct {
982       public:
983         int longIdx[23], longDiff[22];
984         int shortIdx[14], shortDiff[13];
985       } bandInfo[9];
986       static int mapbuf0[9][152];
987       static int mapbuf1[9][156];
988       static int mapbuf2[9][44];
989       static int *map[9][3];
990       static int *mapend[9][3];
991       static uint32_t n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
992       static uint32_t i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
993
994       static short tab0 [  1], tab1 [  7], tab2  [17],  tab3[ 17];
995       static short tab5 [ 31], tab6 [ 31], tab7  [71],  tab8[ 71];
996       static short tab9 [ 71], tab10[127], tab11[127], tab12[127];
997       static short tab13[511], tab15[511], tab16[511], tab24[511];
998       static short tab_c0[31], tab_c1[31];
999       static class huffman_t {
1000       public:
1001         unsigned int linbits;
1002         short *table;
1003       } ht[32], htc[2];
1004
1005       static int dct64_1(float *out0,float *out1,float *b1,float *b2,
1006         float *samples);
1007       static int dct64(float *a,float *b,float *c);
1008       static int dct36(float *inbuf,float *o1,float *o2,float *wintab,
1009         float *tsbuf);
1010       static int dct12(float *in,float *rawout1,float *rawout2,float *wi,
1011         float *ts);
1012
1013       int get_scale_factors_1(int *scf,l3_info_t *l3_info,int ch,int gr);
1014       int get_scale_factors_2(int *scf,l3_info_t *l3_info,int i_stereo);
1015       int dequantize_sample(float xr[SBLIMIT][SSLIMIT],int *scf,
1016          l3_info_t *l3_info,int sfreq,int part2bits);
1017       int get_side_info(l3_sideinfo_t *si,int channels,int ms_stereo,
1018          long sfreq,int single,int lsf);
1019       int hybrid(float fsIn[SBLIMIT][SSLIMIT],
1020          float tsOut[SSLIMIT][SBLIMIT],int ch,l3_info_t *l3_info);
1021       int antialias(float xr[SBLIMIT][SSLIMIT],l3_info_t *l3_info);
1022       int calc_i_stereo(float xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
1023          l3_info_t *l3_info,int sfreq,int ms_stereo,int lsf);
1024       int synth_stereo(float *bandPtr,int channel,float *out, int *pnt);
1025       int init_audio(zmpeg3_t *zsrc, atrack_t *ztrack, int zformat);
1026       int synths_reset();
1027       int select_table();
1028       int step_one(uint8_t *bit_alloc, int *scale);
1029       int step_two(uint8_t *bit_alloc, float fraction[2][4][SBLIMIT],
1030          int *scale, int x1);
1031     public:
1032       new_memset(0);
1033       bits_t *stream;
1034       // Layer 3
1035       uint8_t *bsbuf, *prev_bsbuf;
1036       uint8_t bsspace[2][MAXFRAMESIZE + 512]; // MAXFRAMESIZE
1037       int bsnum;
1038       // For mp3 current framesize without header.  For AC3 current framesize with header.
1039       long framesize;
1040       long prev_framesize, past_framesize;
1041       int channels;
1042       int samplerate;
1043       int single;
1044       int sampling_frequency_code;
1045       int error_protection;
1046       int mode;
1047       int mode_ext;
1048       int lsf;
1049       long ssize;
1050       int mpeg35;
1051       int padding;
1052       int layer;
1053       int extension;
1054       int copyright;
1055       int original;
1056       int emphasis;
1057       int bitrate;
1058       // Static variable in synthesizer
1059       int bo;                      
1060       // Ignore first frame after a seek
1061       int first_frame;
1062       float synth_stereo_buffs[2][2][0x110];
1063       float synth_mono_buff[64];
1064       float mp3_block[2][2][SBLIMIT * SSLIMIT];
1065       int mp3_blc[2];
1066
1067       // State of ID3 parsing
1068       int id3_state;
1069       int id3_current_byte;
1070       int id3_size;
1071       // Layer 2
1072       int bitrate_index;
1073       imdct_al_table_t *alloc;
1074       int jsbound;
1075       int II_sblimit;
1076       uint8_t layer2_scfsi_buf[64];
1077
1078       void layer_reset();
1079       static int layer_check(uint8_t *data);
1080       static int id3_check(uint8_t *data);
1081       int layer3_header(uint8_t *data);
1082       audio_decoder_layer_t();
1083       ~audio_decoder_layer_t();
1084       int do_layer2(uint8_t *zframe,int zframe_size,float **zoutput,int render);
1085       int do_layer3(uint8_t *zframe,int zframe_size,float **zoutput,int render);
1086       int init_layer2();
1087       int init_layer3();
1088       int init_decode_tables();
1089     } *layer_decoder;
1090
1091     class audio_decoder_ac3_t {
1092     public:
1093       new_memset(0);
1094       static int ac3_samplerates[3];
1095
1096       bits_t *stream;
1097       int samplerate;
1098       int bitrate;
1099       int flags;
1100       int channels;
1101       a52_state_t *state;
1102       sample_t *output;
1103       int framesize;
1104       static int ac3_check(uint8_t *data);
1105       int ac3_header(uint8_t *data);
1106       int do_ac3(uint8_t *zframe, int zframe_size, float **zoutput, int render);
1107       audio_decoder_ac3_t();
1108       ~audio_decoder_ac3_t();
1109     } *ac3_decoder;
1110
1111     class audio_decoder_pcm_t {
1112     public:
1113       new_memset(0);
1114       int samplerate;
1115       int bits;
1116       int channels;
1117       int framesize;
1118       int pcm_header(uint8_t *data);
1119       int do_pcm(uint8_t *zframe, int zframe_size, float **zoutput, int render);
1120       audio_decoder_pcm_t() {}
1121       ~audio_decoder_pcm_t() {}
1122     } *pcm_decoder;
1123
1124     int channels;            // channels from read_header
1125     int samplerate;          // samplerate from read_header
1126     int framenum;            // Number of current frame being decoded
1127     int framesize;           // Size of frame including header
1128     int64_t start_byte;      // First byte of audio data in the file
1129     int output_channels;     // number of defined output buffers
1130     float **output;          // Output from synthesizer in linear floats
1131     int output_size;         // Number of pcm samples in the buffer
1132     int output_allocated;    // Allocated number of samples in output
1133     int64_t output_position; // Sample position in file of start of output buffer
1134     int64_t sample_seek;     // Perform a seek to the sample
1135     int64_t byte_seek;       // Perform a seek to the absolute byte
1136     // +/- number of samples of difference between audio and video
1137     int seek_correction;
1138     // Buffer containing current packet
1139     uint8_t packet_buffer[MAXFRAMESIZE];
1140     // Position in packet buffer of next byte to read
1141     int packet_position;
1142
1143     audio_t() {}
1144     int init_audio(zmpeg3_t *zsrc, atrack_t *ztrack, int zformat);
1145     ~audio_t();
1146
1147     int rewind_audio();
1148     int read_header();
1149     void update_channels();
1150     int audio_pts_padding();
1151     int audio_pts_skipping(int samples);
1152     void update_audio_history();
1153     int read_frame(int render);
1154     int get_length();
1155     int seek();
1156     int seek_byte(int64_t byte);
1157     int seek_sample(long sample);
1158     int read_raw(uint8_t *output,long *size,long max_size);
1159     void shift_audio(int64_t diff);
1160     int decode_audio(void *output_v,int atyp,int channel,int len);
1161     int64_t audio_position() { return output_position + output_size; }
1162   };
1163
1164   class VLCtab_t {
1165   public:
1166     int8_t val, len;
1167   };
1168
1169   class DCTtab_t {
1170   public:
1171     int8_t run, level;
1172     int16_t len;
1173   };
1174
1175   class slice_buffer_t {
1176     static VLCtab_t DClumtab0[32];
1177     static VLCtab_t DClumtab1[16];
1178     static VLCtab_t DCchromtab0[32];
1179     static VLCtab_t DCchromtab1[32];
1180   
1181   public:
1182     slice_buffer_t *next;    // avail list link
1183     uint8_t *data;           // Buffer for holding the slice data
1184     video_t *video;          // decoder
1185     int buffer_size;         // Size of buffer
1186     int buffer_allocation;   // Space allocated for buffer 
1187     int buffer_position;     // Position in buffer
1188     uint32_t bits;
1189     int bits_size;
1190
1191     int eob() { return buffer_position >= buffer_size; }
1192     uint32_t get_data_byte() { return data[buffer_position++]; }
1193     void fill_bits(int nbits) {
1194       while( bits_size < nbits ) {
1195         bits <<= 8;
1196         if( !eob() ) bits |= get_data_byte();
1197         bits_size += 8;
1198       }
1199     }
1200     void flush_bits(int nbits) { fill_bits(nbits); bits_size -= nbits; }
1201     void flush_bit() { flush_bits(1); }
1202     uint32_t show_bits(int nbits) {
1203       fill_bits(nbits);
1204       return (bits >> (bits_size-nbits)) & (0xffffffff >> (32-nbits));
1205     }
1206     uint32_t get_bits(int nbits) {
1207       uint32_t result = show_bits(nbits);
1208       bits_size -= nbits;
1209       return result;
1210     }
1211     uint32_t get_bit() { return get_bits(1); }
1212     int get_dc_lum();
1213     int get_dc_chrom();
1214     uint8_t *expand_buffer(int bfrsz);
1215     void fill_buffer(bits_t *vstream);
1216     int ext_bit_info();
1217
1218     slice_buffer_t();
1219     ~slice_buffer_t();
1220   };
1221
1222
1223   class video_t {
1224     static uint8_t zig_zag_scan_nommx[64]; // zig-zag scan
1225     static uint8_t alternate_scan_nommx[64]; // alternate scan
1226     // default intra quantization matrix
1227     static uint8_t default_intra_quantizer_matrix[64];
1228     // Frame rate table must agree with the one in the encoder
1229     static double frame_rate_table[16];
1230
1231     inline void recon_comp(uint8_t *s, uint8_t *d,
1232            int lx, int lx2, int h, int type);
1233     void recon(uint8_t *src[], int sfield,
1234            uint8_t *dst[], int dfield, int lx, int lx2,
1235            int w, int h, int x, int y, int dx, int dy, int addflag);
1236     int delete_decoder();
1237     void init_scantables();
1238     int read_frame_backend(int skip_bframes);
1239     int *get_scaletable(int input_w, int output_w);
1240     int get_gop_header();
1241     long gop_to_frame(timecode_t *gop_timecode);
1242     int seek();
1243
1244   public:
1245     new_memset(0);
1246     class cc_t;
1247
1248     enum chroma_format {
1249       cfmt_420=1,
1250       cfmt_422=2,
1251       cfmt_444=3,
1252     };
1253
1254     enum ext_start_code {
1255       ext_id_SEQ=1,
1256       ext_id_DISP=2,
1257       ext_id_QUANT=3,
1258       ext_id_SEQSCAL=5,
1259       ext_id_PANSCAN=7,
1260       ext_id_CODING=8,
1261       ext_id_SPATSCAL=9,
1262       ext_id_TEMPSCAL=10,
1263     };
1264
1265     enum picture_type {
1266       pic_type_I=1,
1267       pic_type_P=2,
1268       pic_type_B=3,
1269       pic_type_D=4,
1270     };
1271
1272     enum picture_structure {
1273       pics_TOP_FIELD=1,
1274       pics_BOTTOM_FIELD=2,
1275       pics_FRAME_PICTURE=3,
1276     };
1277
1278     slice_buffer_t *slice_buffers, *avail_slice_buffers;
1279     int total_slice_buffers;
1280     void allocate_slice_buffers();
1281     void delete_slice_buffers();
1282     void reallocate_slice_buffers();
1283     zlock_t slice_lock, slice_wait, slice_active;
1284     int slice_wait_locked, slice_active_locked;
1285
1286     zmpeg3_t *src;
1287     vtrack_t *track;
1288
1289     // ================================= Seeking variables =========================
1290     bits_t *vstream;
1291     int decoder_initted;
1292     uint8_t **output_rows;     // Output frame buffer supplied by user
1293     int in_x, in_y, in_w, in_h, out_w, out_h; // Output dimensions
1294     int row_span;
1295     int *x_table, *y_table;          // Location of every output pixel in the input
1296     int color_model;
1297     int want_yvu;                    // Want to return a YUV frame
1298     char *y_output, *u_output, *v_output; // Output pointers for a YUV frame
1299
1300     int blockreadsize;
1301     int maxframe;          // Max value of frame num to read
1302     int64_t byte_seek;     // Perform absolute byte seek before the next frame is read
1303     int frame_seek;        // Perform a frame seek before the next frame is read
1304     int framenum;          // Number of the next frame to be decoded
1305     int last_number;       // Last framenum rendered
1306     int ref_frames;        // ref_frames since last seek
1307     int found_seqhdr;
1308     int bitrate;
1309     timecode_t gop_timecode;     // Timecode for the last GOP header read.
1310     int has_gops; // Some streams have no GOPs so try sequence start codes instead
1311
1312     class cc_t {        // close captioning (atsc only)
1313       video_t *video;
1314       uint8_t data[3][128];   // reorder buffers
1315       int frs_history, font_size;
1316       void decode(int idx);
1317     public:
1318       class svc_t;
1319   
1320       enum {
1321         MX_SVC = 64, MX_WIN=8,MX_CX=48,MX_CY=16,
1322         /* frame struct history */
1323         frs_tf1 = 0x01, /* top field in data[1] */
1324         frs_bf2 = 0x02, /* bottom field in data[2] */
1325         frs_frm = 0x04, /* frame in data[1] */
1326         frs_prc = 0x08, /* frame processed */
1327         st_unmap=-1, st_hidden=0, st_visible=1, /* state */
1328         dir_rt=0, dir_lt=1, dir_up=2, dir_dn=3, /* print/scroll/effect dir */
1329         jfy_left=0, jfy_right=1, jfy_center=2, jfy_full=3, /* justify */
1330         eft_snap = 0, eft_fade = 1, eft_wipe = 2, /* effect */
1331         oty_solid=0, oty_flash=1, oty_translucent=2,
1332         oty_transparent=3, bdr_width=5, /* opacity/border width */
1333         edg_none=0, edg_raised=1, edg_depressed=2, edg_uniform=3,
1334         edg_shadow_left=4, edg_shadow_right=5, /* edge type */
1335         psz_small=0, psz_standard=1, psz_large=2, /* pen size */
1336         fst_default=0, fst_mono_serif=1, fst_prop_serif=2,
1337         fst_mono_sans=3, fst_prop_sans=4, fst_casual=5,
1338         fst_cursive=6, fst_small_caps=7, /* font style */
1339         fsz_small=0, fsz_normal=1, fsz_large=2, /* font size */
1340         tag_dialog=0, tag_source_id=1, tag_device=2,
1341         tag_dialog_2=3, tag_voiceover=4, tag_audible_transl=5,
1342         tag_subtitle_transl=6, tag_voice_descr=7, tag_lyrics=8,
1343         tag_effect_descr=9, tag_score_descr=10, tag_expletive=11,
1344         tag_not_displayable=15, /* text tag */
1345         ofs_subscript=0, ofs_normal=1, ofs_superscript=2, /* offset */
1346       };
1347       /* color RGB 2:2:2 (lsb = B). */
1348       class svc_t {
1349         cc_t *cc;
1350         int sid, ctrk, size;
1351         uint8_t *bp, *ep;
1352         uint8_t data[128];
1353       public:
1354         class chr_t {
1355         public:
1356           uint16_t glyph;
1357           uint8_t fg_opacity : 2;
1358           uint8_t fg_color : 6;
1359           uint8_t bg_opacity : 2;
1360           uint8_t bg_color : 6;
1361           uint8_t pen_size : 2;
1362           uint8_t edge_color : 6;
1363           uint8_t italics : 1;
1364           uint8_t edge_type : 3;
1365           uint8_t underline : 1;
1366           uint8_t font_style : 3; 
1367           uint8_t font_size : 2; 
1368           uint8_t offset : 2; 
1369           uint8_t text_tag : 4;
1370           uint8_t rsvd2;
1371         } *bfrs;
1372         chr_t *get_bfr();
1373         void bfr_put(chr_t *&bfr);
1374
1375         class win_t;
1376         class win_t {
1377         public:
1378           class chr_attr_t;
1379           int8_t id, st, pdir, sdir;
1380           uint8_t cx, cy, cw, ch; /* pen/dim in bytes */
1381           int ax, ay, x, y, bw; /* anchor/pen/border in pixels */
1382           uint32_t fg_yuva, bg_yuva, edge_yuva; /* pen color */
1383           uint32_t fill_yuva, border_yuva; /* window color */
1384           int start_frame, end_frame;
1385           svc_t *svc;
1386           subtitle_t *subtitle;
1387           uint8_t anchor_point, anchor_horizontal, anchor_vertical;
1388           uint8_t anchor_relative, row_lock, col_lock, dirty;
1389           uint8_t priority, window_style, pen_style;
1390           /* pen style properties */
1391           uint8_t pen_size, font_style, offset, italics, underline;
1392           uint8_t fg_color, fg_opacity, bg_color, bg_opacity;
1393           uint8_t text_tag, edge_color, edge_type, scale;
1394           /* window style properties */
1395           uint8_t justify, wordwrap, fill_opacity, fill_color;
1396           uint8_t display_effect, effect_speed, effect_direction;
1397           uint8_t border_type, border_color, border_width;
1398           chr_t *bfr;
1399
1400           win_t();
1401           ~win_t();
1402           void reset(int i,svc_t *svc);
1403           int BS();   int FF();   int CR();   int HCR();
1404           int init(int ncw, int nch);
1405           int resize(int ncw, int nch);
1406           static int is_breakable(int b);
1407           int wrap();
1408           int ovfl();
1409           int store(int ch);
1410           void set_window_style(int i);
1411           void set_pen_style(int i);
1412           void default_window();
1413           void print_buffer();
1414           void hbar(int x, int y, int ww, int hh, int lm, int rm, int clr);
1415           void vbar(int x, int y, int ww, int hh, int tm, int bm, int clr);
1416           void border(int ilt, int olt, int irt, int ort,
1417                       int iup, int oup, int idn, int odn);
1418           void put_chr(chr_t *chr);
1419           static bitfont_t *chr_font(chr_t *bp, int scale);
1420           void render();
1421           void output_text();
1422           void set_state(int8_t st);
1423         } win[MX_WIN], *curw;    
1424         svc_t(cc_t *cc);
1425         ~svc_t();
1426         int id() { return sid; }
1427         int trk() { return ctrk; }
1428         void append(uint8_t *bp, int len);
1429         void decode();
1430         int render();
1431         void reset(int sid,cc_t *cc);
1432         int CWx(int id);  int DFz(int id);
1433         int DFx(int id);  int RSVD();   int ETX();
1434         int BS();   int FF();   int CR();   int HCR();
1435         int RST();  int DLC();  int DLW();  int DLY();
1436         int CLW();  int DSW();  int HDW();  int TGW();
1437         int SPA();  int SPC();  int SPL();  int SWA();
1438         int command(int cmd);
1439         static uint32_t pen_yuva(int color, int opacity);
1440       } svc;
1441   
1442       cc_t(video_t *video);
1443       ~cc_t();
1444       void reset();
1445       void get_atsc_data(bits_t *v);
1446       void decode();
1447       void reorder();
1448       zcc_text_cb text_cb;
1449     } *cc;
1450     cc_t *get_cc();
1451     int subtitle_track; // Subtitle track to composite if >= 0
1452     int show_subtitle(int strk);
1453
1454     // These are only available from elementary streams.
1455     int frames_per_gop;       // Frames per GOP after the first GOP.
1456     int first_gop_frames;     // Frames in the first GOP.
1457     int first_frame;     // Number of first frame stored in timecode
1458     int last_frame;      // Last frame in file
1459
1460     // ================================= Compression variables =====================
1461     // Malloced frame buffers.  2 refframes are swapped in and out.
1462     // while only 1 auxframe is used.
1463     uint8_t *yuv_buffer[5];  // Make YVU buffers contiguous for all frames
1464     uint8_t *oldrefframe[3], *refframe[3], *auxframe[3];
1465     uint8_t *llframe0[3], *llframe1[3];
1466     uint8_t *zigzag_scan_table;
1467     uint8_t *alternate_scan_table;
1468     // Source for the next frame presentation
1469     uint8_t *output_src[3];
1470     // Pointers to frame buffers.
1471     uint8_t *newframe[3];
1472     uint8_t *tdat;
1473     int horizontal_size, vertical_size, mb_width, mb_height;
1474     int coded_picture_width,  coded_picture_height;
1475     int chroma_format, chrom_width, chrom_height, blk_cnt;
1476     int8_t pict_type, pict_struct, field_sequence, intravlc;
1477     int8_t forw_r_size, back_r_size, full_forw, full_back;
1478     int8_t prog_seq, prog_frame, repeatfirst, secondfield;
1479     int8_t h_forw_r_size, v_forw_r_size, h_back_r_size, v_back_r_size;
1480     int8_t dc_prec, topfirst, frame_pred_dct, conceal_mv;
1481     union {
1482       struct { int8_t got_top, got_bottom, repeat_fields, current_field; };
1483       int32_t repeat_data;
1484     };
1485     int skip_bframes;
1486     int frame_time, seek_time;
1487     int stwc_table_index, llw, llh, hm, hn, vm, vn;
1488     int lltempref, llx0, lly0, llprog_frame, llfieldsel;
1489     int matrix_coefficients;
1490     int framerate_code;
1491     double frame_rate;
1492     int *cr_to_r, *crb_to_g, *cb_to_b;
1493     int intra_quantizer_matrix[64], non_intra_quantizer_matrix[64];
1494     int chroma_intra_quantizer_matrix[64], chroma_non_intra_quantizer_matrix[64];
1495     int mpeg2;
1496     int skim;                     // skip reconstruction/motion
1497     int thumb, ithumb;            // thumbnail scan/ thumbnail frame ready 
1498     int qscale_type, altscan;     // picture coding extension
1499     int pict_scal;                // picture spatial scalable extension
1500     int scalable_mode;            // sequence scalable extension
1501
1502     // Subtitling frame
1503     uint8_t *subtitle_frame[3];
1504
1505     video_t(zmpeg3_t *zsrc, vtrack_t *ztrack);
1506     ~video_t();
1507     void init_video();
1508     int seek_video() { return seek(); }
1509     void reset_subtitles();
1510     int eof();
1511
1512     int set_cpus(int cpus);
1513     int set_mmx(int use_mmx);
1514     void cache_frame();
1515     int drop_frames(long frames, int cache_it);
1516     int colormodel();
1517     int read_frame(uint8_t **output_rows,
1518       int in_x, int in_y, int in_w, int in_h,
1519       int out_w, int out_h, int color_model);
1520     int read_yuvframe(char *y_output, char *u_output, char *v_output,
1521       int in_x, int in_y, int in_w, int in_h);
1522     int read_yuvframe_ptr(char **y_output, char **u_output, char **v_output);
1523     int read_raw(uint8_t *output, long *size, long max_size);
1524     int seek_frame(long frame);
1525     int seek_byte(int64_t byte);
1526     int rewind_video(int preload=1);
1527     int previous_frame();
1528     int init_decoder();
1529     void new_output();
1530     int reconstruct( int bx, int by, int mb_type, int motion_type,
1531            int PMV[2][2][2], int mv_field_sel[2][2],
1532            int dmvector[2], int stwtype);
1533     void dump();
1534     slice_buffer_t *get_slice_buffer();
1535     void put_slice_buffer(slice_buffer_t* buffer);
1536     int get_macroblocks();
1537     int display_second_field();
1538     int video_pts_padding();
1539     int video_pts_skipping();
1540     int read_picture();
1541     int get_picture();
1542     int get_seq_hdr();
1543     int sequence_extension();
1544     int sequence_display_extension();
1545     int quant_matrix_extension();
1546     int sequence_scalable_extension();
1547     int picture_display_extension();
1548     int picture_coding_extension();
1549     int picture_spatial_scalable_extension();
1550     int picture_temporal_scalable_extension();
1551     int ext_user_data();
1552     int get_picture_hdr();
1553     int find_header();
1554     int get_header();
1555     void overlay_subtitle(subtitle_t *subtitle);
1556     void decode_subtitle();
1557     int init_output();
1558     int dither_frame(uint8_t *yy, uint8_t *uu, uint8_t *vv,uint8_t **output_rows);
1559     int dither_frame444(uint8_t *yy, uint8_t *uu, uint8_t *vv);
1560     int dithertop(uint8_t *yy, uint8_t *uu, uint8_t *vv);
1561     int dithertop444(uint8_t *yy, uint8_t *uu, uint8_t *vv);
1562     int ditherbot(uint8_t *yy, uint8_t *uu, uint8_t *vv);
1563     int ditherbot444(uint8_t *yy, uint8_t *uu, uint8_t *vv);
1564     int present_frame(uint8_t *yy, uint8_t *uu, uint8_t *vv);
1565     void calc_dmv(int DMV[][2],int *dmvector,int mvx,int mvy);
1566     int is_refframe() { return pict_type == pic_type_B ? 0 :
1567       pict_struct == pics_FRAME_PICTURE ? 1 : secondfield;
1568     }
1569   };
1570
1571
1572   class slice_decoder_t {
1573   public:
1574     static VLCtab_t PMBtab0[8];
1575     static VLCtab_t PMBtab1[8];
1576     static VLCtab_t BMBtab0[16];
1577     static VLCtab_t BMBtab1[8];
1578     static VLCtab_t spIMBtab[16];
1579     static VLCtab_t spPMBtab0[16];
1580     static VLCtab_t spPMBtab1[16];
1581     static VLCtab_t spBMBtab0[14];
1582     static VLCtab_t spBMBtab1[12];
1583     static VLCtab_t spBMBtab2[8];
1584     static VLCtab_t SNRMBtab[8];
1585     static VLCtab_t MVtab0[8];
1586     static VLCtab_t MVtab1[8];
1587     static VLCtab_t MVtab2[12];
1588     static VLCtab_t CBPtab0[32];
1589     static VLCtab_t CBPtab1[64];
1590     static VLCtab_t CBPtab2[8];
1591     static VLCtab_t MBAtab1[16];
1592     static VLCtab_t MBAtab2[104];
1593
1594     static DCTtab_t DCTtabfirst[12];
1595     static DCTtab_t DCTtabnext[12];
1596     static DCTtab_t DCTtab0[60];
1597     static DCTtab_t DCTtab0a[252];
1598     static DCTtab_t DCTtab1[8];
1599     static DCTtab_t DCTtab1a[8];
1600     static DCTtab_t DCTtab2[16];
1601     static DCTtab_t DCTtab3[16];
1602     static DCTtab_t DCTtab4[16];
1603     static DCTtab_t DCTtab5[16];
1604     static DCTtab_t DCTtab6[16];
1605
1606     // non-linear quantization coefficient table
1607     static uint8_t non_linear_mquant_table[32];
1608
1609     enum macroblock_type {
1610       mb_INTRA=1,
1611       mb_PATTERN=2,
1612       mb_BACKWARD=4,
1613       mb_FORWARD=8,
1614       mb_QUANT=16,
1615       mb_WEIGHT=32,
1616       mb_CLASS4=64,
1617     };
1618
1619     enum motion_type {
1620       mc_FIELD=1,
1621       mc_FRAME=2,
1622       mc_16X8=2,
1623       mc_DMV=3,
1624     };
1625
1626     enum mv_format {
1627       mv_FIELD=0,
1628       mv_FRAME=1,
1629     };
1630
1631     enum scalable_mode {
1632       sc_NONE=0,
1633       sc_DP=1,
1634       sc_SPAT=2,
1635       sc_SNR=3,
1636       sc_TEMP=4,
1637     };
1638
1639     slice_decoder_t *next;
1640     zmpeg3_t *src;
1641     video_t *video;
1642     pthread_t owner;        // for debugging
1643     slice_buffer_t *slice_buffer;
1644     int fault;
1645     int done;
1646     int quant_scale;
1647     int pri_brk;            // slice/macroblock
1648     short block[12][64];
1649     int sparse[12];
1650     pthread_t tid;          // ID of thread
1651     zlock_t input_lock;
1652
1653     int val, sign, idx;
1654     int get_cbp();
1655     int clear_block(int comp, int size);
1656     static uint16_t *DCTlutab[3];
1657     static uint16_t lu_pack(DCTtab_t *lp) {
1658       int run = lp->run>=64 ? lp->run-32 : lp->run;
1659       return run | ((lp->len-1)<<6) | (lp->level<<10);
1660     }
1661     static int lu_run(uint16_t lu) { return lu & 0x3f; }
1662     static int lu_level(uint16_t lu) { return lu >> 10; }
1663     static int lu_len(uint16_t lu) { return ((lu>>6) & 0x0f) + 1; }
1664     static void init_lut(uint16_t *&lutbl, DCTtab_t *tabn, DCTtab_t *tab0, DCTtab_t *tab1);
1665     static void init_tables();
1666     int get_coef(uint16_t *lut);
1667     int get_mpg2_coef(uint16_t *lut);
1668     void get_intra_block(int comp, int dc_dct_pred[]);
1669     void get_mpg2_intra_block(int comp, int dc_dct_pred[]);
1670     void get_inter_block(int comp);
1671     void get_mpg2_inter_block(int comp);
1672     int get_slice_hdr();
1673     int decode_slice();
1674     void slice_loop();
1675     static void *the_slice_loop(void *the_slice_decoder);
1676     int get_mv();
1677     int get_dmv();
1678     void motion_vector(int *PMV, int *dmvector, int h_r_size, int v_r_size,
1679       int dmv, int mvscale, int full_pel_vector);
1680     int motion_vectors(int PMV[2][2][2], int dmvector[2], int mv_field_sel[2][2],
1681       int s, int mv_count, int mv_format, int h_r_size, int v_r_size,
1682       int dmv, int mvscale);
1683     int get_macroblock_address();
1684     inline int getsp_imb_type();
1685     inline int getsp_pmb_type();
1686     inline int getsp_bmb_type();
1687     inline int get_imb_type();
1688     inline int get_pmb_type();
1689     inline int get_bmb_type();
1690     inline int get_dmb_type();
1691     inline int get_snrmb_type();
1692     int get_mb_type();
1693     int macroblock_modes(int *pmb_type,int *pstwtype, int *pstwclass,
1694        int *pmotion_type,int *pmv_count,int *pmv_format,int *pdmv,
1695        int *pmvscale,int *pdct_type);
1696     int add_block(int comp,int bx,int by,int dct_type,int addflag);
1697     static void idct_conversion(short* block);
1698     int get_active_slice_buffer();
1699
1700     slice_decoder_t();
1701     ~slice_decoder_t();
1702   } *slice_decoders, *avail_slice_decoders;
1703
1704   int total_slice_decoders;
1705   slice_buffer_t *active_slice_buffers;
1706   zlock_t decoder_lock, decoder_active;
1707   int decoder_active_locked;
1708   void allocate_slice_decoders();
1709   void delete_slice_decoders();
1710   void reallocate_slice_decoders();
1711   void decode_slice(slice_buffer_t* buffer);
1712   
1713
1714   class subtitle_t {
1715   public:
1716     new_memset(0);
1717     uint8_t *data;   // Raw data of subtitle
1718     int data_allocated, data_used;
1719     int id;         // Number of stream starting at 0x20
1720     int done;       // -1 avail, 0 read in, 1 decode
1721     int active;     // 1 subtitle is contructed, -1 subtitle is close caption
1722     int draw;       // -1 never draw, 0 draw based on frame time, 1 always draw
1723     int64_t offset; // Program offset of start of subtitle
1724     int force;      // 1 Force display, -1 force delete
1725     // image in YUV444
1726     int w, h, sz;
1727     uint8_t *image_y, *image_u, *image_v, *image_a;
1728     int x1, x2, y1, y2;
1729     int start_time; // Time after detection of subtitle to display it in msec
1730     int stop_time;  // Time after detection of subtitle to hide it in msec
1731     int palette[4]; // Indexes in the main palette
1732     int alpha[4];
1733     int start_frame, stop_frame; // framenum start/stop drawing
1734     int frame_time; // presentation frame_time at creation
1735
1736     subtitle_t();
1737     subtitle_t(int nid, int ww, int hh);
1738     ~subtitle_t();
1739
1740     void realloc_data(int count);
1741     int decompress_subtitle(zmpeg3_t *zsrc);
1742     void set_image_size(int isz);
1743     void set_image_size(int ww, int hh);
1744     int decode(video_t *video);
1745   };
1746
1747   class demuxer_t {
1748     friend class bits_t;
1749
1750     uint8_t packet_next_char() { return raw_data[raw_offset]; }
1751     uint8_t packet_read_char() { return raw_data[raw_offset++]; }
1752     uint32_t packet_read_int16() {
1753       uint32_t a = raw_data[raw_offset++];
1754       uint32_t b = raw_data[raw_offset++];
1755       return (a << 8) | b;
1756     }
1757     uint32_t packet_next_int24() {
1758       uint32_t a = raw_data[raw_offset+0];
1759       uint32_t b = raw_data[raw_offset+1];
1760       uint32_t c = raw_data[raw_offset+2];
1761       return (a << 16) | (b << 8) | c;
1762     }
1763     uint32_t packet_read_int24() {
1764       uint32_t a = raw_data[raw_offset++];
1765       uint32_t b = raw_data[raw_offset++];
1766       uint32_t c = raw_data[raw_offset++];
1767       return (a << 16) | (b << 8) | c;
1768     }
1769     uint32_t packet_read_int32() {
1770       uint32_t a = raw_data[raw_offset++];
1771       uint32_t b = raw_data[raw_offset++];
1772       uint32_t c = raw_data[raw_offset++];
1773       uint32_t d = raw_data[raw_offset++];
1774       return (a << 24) | (b << 16) | (c << 8) | d;
1775     }
1776     void packet_skip(int length) { raw_offset += length; }
1777     int get_adaptation_field();
1778     int get_program_association_table();
1779     int get_transport_payload(int is_audio, int is_video);
1780     int get_pes_packet_header(uint64_t *pts, uint64_t *dts);
1781     int get_unknown_data();
1782     int get_transport_pes_packet();
1783     int get_pes_packet();
1784     int get_payload();
1785     int read_transport();
1786     int get_system_header();
1787     uint64_t get_timestamp();
1788     int get_pack_header();
1789     int get_program_payload(int bytes, int is_audio, int is_video);
1790     int handle_scrambling(int decryption_offset);
1791     void del_subtitle(int idx);
1792     subtitle_t* get_subtitle(int id,int64_t offset);
1793     void handle_subtitle(zmpeg3_t *src, int stream_id, int bytes);
1794     int handle_pcm(int bytes);
1795     int get_program_pes_packet(uint32_t header);
1796     int previous_code(uint32_t zcode);
1797
1798   public:
1799     new_memset(0);
1800     class zstream_t;
1801     class title_t;
1802     class nav_t {
1803     public:
1804 #include "nav.h"
1805       new_memset(0);
1806     } *nav;
1807
1808     zmpeg3_t* src;
1809     uint8_t *raw_data; // One unparsed packet.
1810     int raw_offset; // Offset in raw_data of read pointer
1811     int raw_size; // Amount loaded in last raw_data
1812
1813     // Elementary stream data when only one stream is to be read.
1814     // Erased in every call to read a packet.
1815     class zstream_t {
1816     public:
1817       new_memset(0);
1818       uint8_t *buffer;
1819       int allocated; // Allocation of data_buffer
1820       int size; // Position in data_buffer of write pointer
1821       int position; // Position in data_buffer of read pointer
1822       int start; // Start of the next pes packet
1823       int length() { return size - position; }
1824       bool eof() { return position >= size; }
1825       bool bof() { return position == 0; }
1826     } zdata;
1827     // Elementary stream data when all streams are to be read.  There is no
1828     // read pointer since data is expected to be copied directly to a track.
1829     // Some packets contain audio and video.  Further division into
1830     // stream ID may be needed.
1831     zstream_t zaudio;
1832     zstream_t zvideo;
1833
1834     subtitle_t *subtitles[MAX_SUBTITLES];
1835     int total_subtitles;
1836
1837     // What type of data to read, which track is accessing
1838     atrack_t *do_audio;
1839     vtrack_t *do_video;
1840     int read_all;
1841     // Direction of reads
1842     int reverse;
1843     // Set to 1 when eof or attempt to read before beginning
1844     int error_flag;
1845     int discontinuity;
1846     // Temp variables for returning
1847     uint8_t next_char;
1848     // Info for mpeg3cat
1849     int64_t last_packet_start;
1850     int64_t last_packet_end;
1851     int64_t last_packet_decryption;
1852
1853   // Table of contents
1854     class title_t { // Titles
1855       void extend_cell_table();
1856     public:
1857       new_memset(0);
1858       class cell_t;
1859
1860       zmpeg3_t *src;
1861       fs_t *fs;
1862       int64_t total_bytes; // Total bytes in title file.
1863       int64_t start_byte; // Absolute starting byte of the title in the stream
1864       int64_t end_byte; // Absolute ending byte of the title in the stream + 1
1865       // May get rid of time values and rename to a cell offset table.
1866       // May also get rid of end byte.
1867
1868       class cell_t {
1869       public:
1870         new_memset(0);
1871         int64_t title_start; // Starting byte of cell in the title (start_byte)
1872         int64_t title_end;  // Ending byte of cell in the title (end_byte)
1873         int64_t program_start; // Starting byte of the cell in the program
1874         int64_t program_end; // Ending byte of the cell in the program
1875         double cell_time; // play time at end of cell, -1 if unknown
1876         int cell_no; // cell in original cell table
1877         int discontinuity;
1878
1879         cell_t() {}
1880         ~cell_t() {}
1881       } *cell_table; // Timecode table
1882       int cell_table_size; // Number of entries
1883       int cell_table_allocation; // Number of available slots
1884
1885       title_t(zmpeg3_t *zsrc, char *fpath);
1886       title_t(zmpeg3_t *zsrc);
1887       ~title_t();
1888       title_t(title_t &title);
1889       void new_cell(int cell_no,
1890         int64_t title_start, int64_t title_end,
1891         int64_t program_start, int64_t program_end,
1892         int discontinuity);
1893       void new_cell(int64_t byte_end) { // default cell
1894         new_cell(0, 0, byte_end, 0, byte_end, 0);
1895       }
1896       int dump_title();
1897       int print_cells(FILE *output);
1898     } *titles[MAX_STREAMZ];
1899     int total_titles;
1900     int current_title; // Title currently being used
1901
1902     // Tables of every stream ID encountered
1903     int8_t astream_table[MAX_STREAMZ];  // afmt index
1904     int8_t vstream_table[MAX_STREAMZ];  // 1 if video
1905     int8_t sstream_table[MAX_STREAMZ];  // 1 if subtitle
1906
1907     // Cell in the current title currently used
1908     int title_cell;
1909     int64_t nav_cell_next_vobu;
1910     int64_t nav_cell_end_byte;
1911
1912     // Byte position in current program.
1913     int64_t program_byte;
1914     // Total bytes in all titles
1915     int64_t total_bytes;
1916     // The end of the current stream in the current program
1917     int64_t stream_end;
1918
1919     int transport_error_indicator;
1920     int payload_unit_start_indicator;
1921     int pid; // PID of last packet
1922     int stream_id; // Stream ID of last packet
1923     int custom_id; // Custom ID of last packet
1924     int transport_scrambling_control;
1925     int adaptation_field_control;
1926     int continuity_counter;
1927     int is_padding;
1928     int pid_table[PIDMAX];
1929     int continuity_counters[PIDMAX];
1930     int total_pids;
1931     int adaptation_fields;
1932     double time;           // Time in seconds
1933     uint32_t last_code;
1934     int audio_pid;
1935     int video_pid;
1936     int subtitle_pid;
1937     int got_audio;
1938     int got_video;
1939     // if subtitle object was created in last packet
1940     int got_subtitle;
1941     // When only one stream is to be read, these store the stream IDs
1942     // Audio stream ID being decoded.  -1 = select first ID in stream
1943     int astream;     
1944     // Video stream ID being decoded.  -1 = select first ID in stream
1945     int vstream;
1946     // Multiplexed streams have the audio type
1947     // Format of the audio derived from multiplexing codes
1948     int aformat;      
1949     int program_association_tables;
1950     int table_id;
1951     int section_length;
1952     int transport_stream_id;
1953     int pes_packets;
1954     double pes_audio_time;  // Presentation Time stamps
1955     double pes_video_time;
1956     int pes_audio_pid;      // custom_id of timestamp
1957     int pes_video_pid;
1958     // Cause the stream parameters to be dumped in human readable format
1959     int dump;
1960
1961     demuxer_t(zmpeg3_t *zsrc,atrack_t *do_aud,vtrack_t *do_vid,int cust_id);
1962     ~demuxer_t();
1963
1964     int create_title(int full_scan=0);
1965     int64_t tell_byte() { return program_byte; }
1966     int64_t absolute_position() {
1967       return titles[current_title]->fs->tell() + titles[current_title]->start_byte;
1968     }
1969     bool eof();
1970     bool bof();
1971     bool error() { return error_flag; }
1972     int64_t movie_size();
1973     double get_time() { return time; }
1974     int get_cell(int no, title_t::cell_t *&v);
1975     void start_reverse();
1976     void start_forward();
1977     int seek_byte(int64_t byte);
1978     double scan_pts();
1979     int goto_pts(double pts);
1980     int seek_phys();
1981     int read_program();
1982     int64_t prog2abs_fwd(int64_t byte, int64_t *nbyte, int *ntitle, int *ncell);
1983     int64_t prog2abs_rev(int64_t byte, int64_t *nbyte, int *ntitle, int *ncell);
1984     int64_t program_to_absolute(int64_t byte, int64_t *nbyte=0, int *ntitle=0, int *ncell=0);
1985     int64_t absolute_to_program(int64_t byte);
1986     int64_t title_bytes();
1987     void append_data(uint8_t *data, int bytes);
1988     void shift_data();
1989     int open_title(int title_number);
1990     int copy_titles(demuxer_t *dst);
1991     void end_title(int64_t end_byte);
1992     int next_code(uint32_t zcode);
1993     int prev_code(uint32_t zcode);
1994     uint8_t read_char_packet();
1995     uint8_t read_prev_char_packet();
1996     uint8_t read_char() {
1997       if( zunlikely(zdata.eof()) ) return read_char_packet();
1998       return zdata.buffer[zdata.position++];
1999     }
2000     uint8_t read_prev_char();
2001     int read_data(uint8_t *output,int size);
2002     int read_next_packet();
2003     int read_prev_packet();
2004     void skip_video_frame();
2005     int64_t next_cell();
2006     int64_t playinfo_next_cell();
2007     double video_pts() { double pts = pes_video_time; pes_video_time = -1.; return pts; }
2008     double audio_pts() { double pts = pes_audio_time; pes_audio_time = -1.; return pts; }
2009     void reset_pts();
2010     void set_audio_pts(uint64_t pts, const double denom);
2011     void set_video_pts(uint64_t pts, const double denom);
2012     int current_cell_no();
2013   };
2014
2015   class cacheframe_t {
2016   public:
2017     new_memset(0);
2018     uint8_t *y, *u, *v;
2019     int y_alloc, u_alloc, v_alloc;
2020     int y_size, u_size, v_size;
2021     int64_t frame_number;
2022     uint32_t age;
2023
2024     cacheframe_t() {}
2025     ~cacheframe_t() {}
2026   };
2027
2028   class cache_t {
2029     int extend_cache();
2030   public:
2031     new_memset(0);
2032     cacheframe_t *frames;
2033     int total;
2034     int allocation;
2035     uint32_t seq;
2036
2037     cache_t() {}
2038     ~cache_t();
2039     void clear();
2040     void reset() { total = 0; }
2041     void put_frame(int64_t zframe_number,
2042       uint8_t *zy, uint8_t *zu, uint8_t *zv,
2043       int zy_size, int zu_size, int zv_size);
2044     int get_frame( int64_t frame_number,
2045         uint8_t **zy, uint8_t **zu, uint8_t **zv);
2046     int has_frame(int64_t frame_number);
2047     int64_t memory_usage();
2048   };
2049
2050   fs_t *fs; // Store entry path here
2051   demuxer_t *demuxer; // Master title tables copied to all tracks
2052   int iopened; // file opened by user (before init)
2053
2054 // Media specific
2055
2056   int total_atracks;
2057   class atrack_t {
2058     void extend_sample_offsets();
2059   public:
2060     new_memset(0);
2061     int channels;
2062     int sample_rate;
2063     demuxer_t *demuxer;
2064     audio_t *audio;
2065     int64_t current_position;
2066     int64_t total_samples;
2067     int format;               // format of audio
2068     int number, pid;
2069     long nudge;
2070     // If we got the header information yet.  Used in streaming mode.
2071     int got_header;
2072     // Pointer to master table of contents when the TOC is read.
2073     // Pointer to private table when the TOC is being created
2074     // Stores the absolute byte of each audio chunk
2075     int64_t *sample_offsets;
2076     int total_sample_offsets;
2077     int sample_offsets_allocated;
2078     // If this sample offset table must be deleted by the track
2079     int private_offsets;
2080     // End of stream in table of contents construction
2081     int64_t audio_eof;
2082     // Starting byte of previous/current packet for making TOC
2083     int64_t prev_offset, curr_offset;
2084     double audio_time;     // audio pts based time
2085     int askip;             // skipping
2086     int64_t pts_position;  // sample position at pts
2087     double pts_origin;
2088     double pts_starttime;  // demuxer first pts
2089     double pts_offset;     // pts offset due to discontinuity
2090     double frame_pts;
2091     void reset_pts();
2092     double last_pts() { return pts_starttime+audio_time-pts_offset; }
2093
2094     atrack_t(zmpeg3_t *zsrc, int custom_id, int format, demuxer_t *demux, int no);
2095     ~atrack_t();
2096
2097     int calculate_format(zmpeg3_t *src);
2098     int handle_audio_data(int track_number);
2099     int handle_audio(int track_number);
2100     int64_t track_position() { return current_position+nudge; }
2101     void append_samples(int64_t offset);
2102     void update_frame_pts() { if( frame_pts < 0 ) frame_pts = demuxer->audio_pts(); }
2103     void update_audio_time();
2104     double get_audio_time();
2105     double pts_audio_time(double pts) {
2106       return pts_starttime<0. ? pts_starttime : pts-pts_starttime+pts_offset;
2107     }
2108     int64_t apparent_position();
2109   } *atrack[MAX_STREAMZ];
2110
2111   int total_vtracks;
2112   class vtrack_t {
2113     void extend_frame_offsets();
2114     void extend_keyframe_numbers();
2115   public:
2116     new_memset(0);
2117     int width, height;
2118     uint32_t tcode;
2119     double frame_rate;
2120     float aspect_ratio;
2121     demuxer_t *demuxer;
2122     video_t *video; // Video decoding object
2123     long current_position;  // Number of next frame to be played
2124     long total_frames;      // Total frames in the file
2125     int number, pid;
2126     slice_buffer_t *slice;  // toc slice data
2127     uint8_t *sbp;
2128     int sb_size;
2129     // Pointer to master table of contents when the TOC is read.
2130     // Pointer to private table when the TOC is being created
2131     // Stores the absolute byte of each frame
2132     int64_t *frame_offsets;
2133     int total_frame_offsets;
2134     int frame_offsets_allocated;
2135     int *keyframe_numbers;
2136     int total_keyframe_numbers;
2137     int keyframe_numbers_allocated;
2138     // Starting byte of previous/current packet for making TOC
2139     int64_t prev_frame_offset;
2140     int64_t prev_offset, curr_offset;
2141     int vskip;             // skipping
2142     double video_time;     // reshuffled pts
2143     // End of stream in table of contents construction
2144     int64_t video_eof;
2145     int got_top;
2146     int got_keyframe;
2147     cache_t *frame_cache;
2148     // If these tables must be deleted by the track
2149     int private_offsets;
2150     int pts_position;      // framenum at pts
2151     double pts_origin;
2152     double pts_starttime;  // first pts
2153     double pts_offset;     // pts offset due to discontinuity
2154     double refframe_pts;   // last refframe pts
2155     double frame_pts;
2156     double pts_video_time(double pts) {
2157       return pts_starttime<0. ? pts_starttime : pts-pts_starttime+pts_offset;
2158     }
2159     void reset_pts();
2160     double last_pts() { return pts_starttime+video_time-pts_offset; }
2161
2162     vtrack_t(zmpeg3_t *zsrc, int custom_id, demuxer_t *demux, int no);
2163     ~vtrack_t();
2164
2165     int handle_video_data(int track_number, int prev_data_size);
2166     int handle_video(int track_number);
2167     int find_keyframe_index(int64_t frame);
2168     void append_frame(int64_t offset, int is_keyframe);
2169     void update_frame_pts() { if( frame_pts < 0 ) frame_pts = demuxer->video_pts(); }
2170     void update_video_time();
2171     double get_video_time();
2172     int apparent_position();
2173   } *vtrack[MAX_STREAMZ];
2174
2175   // Subtitle track
2176   // Stores the program offsets of subtitle images.
2177   // Only used for seeking off of table of contents for editing.
2178   // Doesn't have its own demuxer but hangs off the video demuxer.
2179   int total_stracks;
2180   class strack_t {
2181     void extend_offsets();
2182     void extend_subtitles();
2183   public:
2184     new_memset(0);
2185     zrwlock_t rwlock;
2186     int id; // 0x2X
2187     video_t *video;   // if non-zero, decoder owns track
2188     int64_t *offsets; // Offsets in program of subtitle packets
2189     int total_offsets;
2190     int allocated_offsets;
2191     subtitle_t *subtitles[MAX_SUBTITLES]; // Last subtitle objects found in stream.
2192     int total_subtitles;
2193
2194     strack_t(int zid, video_t *vid=0);
2195     strack_t(strack_t &strack);
2196     ~strack_t();
2197
2198     int append_subtitle(subtitle_t *subtitle, int lock=1);
2199     void append_subtitle_offset(int64_t program_offset);
2200     void del_subtitle(subtitle_t *subtitle, int lock=0);
2201     void del_subtitle(int i, int lock=0);
2202     void del_all_subtitles();
2203   } *strack[MAX_STREAMZ];
2204
2205   static uint64_t get8bytes(uint8_t *buf) {
2206     return bswap_64(*((uint64_t *)buf));
2207   }
2208   static uint32_t get4bytes(uint8_t *buf) {
2209     return bswap_32(*((uint32_t *)buf));
2210   }
2211   static uint32_t get2bytes(uint8_t *buf) {
2212     return bswap_16(*((uint16_t *)buf));
2213   }
2214
2215   class icell_t {
2216   public:
2217     new_memset(0);
2218     int64_t start_byte, end_byte;  // Bytes relative to start of stream.
2219     short vob_id, cell_id;         // cell address vob/cell id
2220     uint32_t inlv;                 // bit vector of allowed interleaves
2221     short angle, discon;           // angle interleave no, discontinuity flag
2222
2223     int has_inlv(int i) { return (inlv >> i) & 1; }
2224   };
2225
2226   class icell_table_t {
2227   public:
2228     icell_t *cells;
2229     long total_cells;
2230     long cells_allocated;
2231     icell_table_t() { cells = 0;  total_cells = cells_allocated = 0; }
2232     ~icell_table_t();
2233     icell_t *append_cell();
2234   };
2235
2236   class ifo_t {
2237   public:
2238     new_memset(0);
2239     enum ifo_id_type {
2240       id_NUM_MENU_VOBS          = 0,
2241       id_NUM_TITLE_VOBS         = 1,
2242       id_MAT                    = 0,
2243       id_PTT                    = 1,
2244       id_TSP                    = 1,
2245       id_TITLE_PGCI             = 2,
2246       id_MENU_PGCI              = 3,
2247       id_TMT                    = 4,
2248       id_MENU_CELL_ADDR         = 5,
2249       id_MENU_VOBU_ADDR_MAP     = 6,
2250       id_TITLE_CELL_ADDR        = 7,
2251       id_TITLE_VOBU_ADDR_MAP    = 8,
2252     };
2253
2254     uint32_t num_menu_vobs;
2255     uint32_t vob_start;
2256     uint32_t empirical;               // scan for audio/subtitle stream data
2257     uint8_t *data[10];
2258     int fd;                           // file descriptor
2259     int64_t pos;                      // offset of ifo file on device 
2260     int current_vob_id;
2261     int max_inlv;
2262     int64_t current_byte;
2263 #include "ifo.h"
2264
2265     int type_vts() {
2266       return !strncmp((char*)data[id_MAT], "DVDVIDEO-VTS", 12) ? 0 : -1;
2267     }
2268     int type_vmg() {
2269       return !strncmp((char*)data[id_MAT], "DVDVIDEO-VMG", 12) ? 0 : -1;
2270     }
2271
2272     void get_playlist(zmpeg3_t *zsrc);
2273     int ifo_read(long pos, long count, uint8_t *data);
2274     int read_mat();
2275
2276     ifo_t(int zfd, long zpos);
2277     ~ifo_t();
2278     int ifo_close();
2279     int init_tables();
2280     int get_table(int64_t offset,unsigned long tbl_id);
2281     void get_palette(zmpeg3_t *zsrc);
2282     void get_header(demuxer_t *demux);
2283     void get_playinfo(zmpeg3_t *zsrc, icell_table_t *icell_addrs);
2284     void icell_addresses(icell_table_t *cell_addrs);
2285     int64_t ifo_chapter_cell_time(zmpeg3_t *zsrc, int chapter);
2286     void get_ititle(zmpeg3_t *zsrc, int chapter=0);
2287     void icell_map(zmpeg3_t *zsrc, icell_table_t *icell_addrs);
2288     int chk(int vts_title, int chapter, int inlv, int angle,
2289       icell_table_t *icell_addrs, int &sectors, int &pcells, int &max_angle);
2290   };
2291
2292   // Table of contents storage
2293   int64_t **frame_offsets;
2294   int64_t **sample_offsets;
2295   int **keyframe_numbers;
2296   int64_t *video_eof;
2297   int64_t *audio_eof;
2298   int *total_frame_offsets;
2299   int *total_sample_offsets;
2300   int64_t *total_samples;
2301   int *total_keyframe_numbers;
2302   // Handles changes in channel count after the start of a stream
2303   int *channel_counts, *nudging;
2304   // Indexes for audio tracks
2305   index_t **indexes;
2306   int total_indexes;
2307   double cell_time; // time at start of last cell
2308   // toc/skim thumbnail data callback
2309   zthumbnail_cb thumbnail_fn;
2310   void *thumbnail_priv;
2311
2312   // Number of bytes to devote to the index of a single track
2313   // in the index building process.
2314   int64_t index_bytes;
2315   int file_type, log_errs;
2316   int err_logging(int v=-1) { int lv = log_errs; if( v>=0 ) log_errs=v; return lv; }
2317   // Only one of these is set to 1 to specify what kind of stream we have.
2318   int is_transport_stream() { return file_type & FT_TRANSPORT_STREAM; }
2319   int is_program_stream() { return file_type & FT_PROGRAM_STREAM; }
2320   int is_audio_stream() { return file_type & FT_AUDIO_STREAM; } // Elemental stream
2321   int is_video_stream() { return file_type & FT_VIDEO_STREAM; } // Elemental stream
2322   int is_ifo_file() { return file_type & FT_IFO_FILE; } // dvd ifo file
2323   // Special kind of transport stream for BD or AVC-HD
2324   int is_bd() { return 0; /* file_type & FT_BD_FILE; */ }
2325   int packet_size; // > 0 if known otherwise determine empirically for every packet
2326   // Type and stream for getting current absolute byte
2327   int last_type_read;  // 1 - audio   2 - video
2328   int last_stream_read;
2329   int vts_title;  // video titleset title number (counting from 0)
2330   int total_vts_titles;
2331   int interleave, angle; // active program sector interleave/angle index
2332   int total_interleaves;
2333   int cpus; // number of decode threads
2334   int seekable; // Filesystem is seekable.  Also means the file isn't a stream.
2335   int last_cell_no; // last known cell no in TOC build
2336   int pts_padding; // use pts data to add padding to sync damaged data
2337   FILE *toc_fp; // For building TOC, the output file.
2338   int recd_fd; // record file
2339   int64_t recd_pos; // record file size
2340   /* The first color palette in the IFO file.  Used for subtitles.
2341    * Byte order: YUVX * 16 */
2342   int have_palette;
2343   uint8_t palette[16 * 4];
2344   // Date of source file index was created from.
2345   // Used to compare DVD source file to table of contents source.
2346   int64_t source_date;
2347   // playback cell table
2348   icell_table_t *playinfo;
2349
2350   int calculate_packet_size();
2351   int get_file_type(int *toc_atracks, int *toc_vtracks, const char *title_path);
2352   int read_toc(int *toc_atracks, int *toc_vtracks, const char *title_path);
2353   enum { show_toc_SAMPLE_OFFSETS=1, show_toc_AUDIO_INDEX=2, show_toc_FRAME_OFFSETS=4 };
2354   int show_toc(int flags);
2355   int handle_nudging();
2356   void divide_index(int track_number);
2357   int update_index(int track_number,int flush);
2358   static ifo_t *ifo_open(int fd,long pos);
2359   int read_ifo();
2360   atrack_t *new_atrack_t(int custom_id, int format,
2361     demuxer_t *demux, int number);
2362   vtrack_t *new_vtrack_t(int custom_id, demuxer_t *demux, int number);
2363   audio_t *new_audio_t(atrack_t *ztrack, int zformat);
2364   video_t *new_video_t(vtrack_t *ztrack);
2365   strack_t* get_strack_id(int id, video_t *vid);
2366   strack_t* get_strack(int number);
2367   strack_t* create_strack(int id, video_t *vid=0);
2368   int display_subtitle(int stream, int sid, int id,
2369     uint8_t *yp, uint8_t *up, uint8_t *vp, uint8_t *ap,
2370     int x, int y, int w, int h, double start_msecs, double stop_msecs);
2371   int delete_subtitle(int stream, int sid, int id);
2372   static int clip(int v, int mn, int mx) {
2373     if( v > mx ) return mx;
2374     if( v < mn ) return mn;
2375     return v;
2376   }
2377   static uint8_t clip(int32_t v) {
2378     return ((uint8_t)v) == v ? v : clip(v,0,255);
2379   }
2380   void handle_subtitle();
2381   void handle_cell(int this_cell_no);
2382
2383 #ifdef ZDVB
2384   class dvb_t {
2385     uint8_t *xbfr, *eob;
2386     uint32_t get8bits()  { return xbfr < eob ? *xbfr++ : 0; }
2387     uint32_t get16bits() { uint32_t v = get8bits();  return (v<<8) | get8bits(); }
2388     uint32_t get24bits() { uint32_t v = get16bits(); return (v<<8) | get8bits(); }
2389     uint32_t get32bits() { uint32_t v = get24bits(); return (v<<8) | get8bits(); }
2390   public:
2391     class mgt_t;  // master guide table
2392     class vct_t;  // virtual channel table
2393     class rrt_t;  // rating region table
2394     class eit_t;  // event information table
2395     class ett_t;  // extended text table
2396     class stt_t;  // system time table
2397
2398     class mgt_t {
2399     public:
2400       new_memset(0);
2401
2402       dvb_t *dvb;
2403       uint32_t ver;
2404       int items;
2405       class mitem_t {
2406       public:
2407         new_memset(0);
2408         uint8_t *bfr;
2409         int bfr_size, bfr_alloc;
2410         int src_id, tbl_id, tbl_len;
2411         uint16_t type;
2412         uint16_t pid;
2413         uint32_t version;
2414         uint32_t size;
2415         void init(int len);
2416         void clear();
2417         void extract(uint8_t *dat, int len);
2418         int bfr_len() { return  tbl_len - bfr_size; }
2419         mitem_t() {}
2420         mitem_t(int zpid) : pid(zpid) { bfr=0; bfr_size=bfr_alloc=0; }
2421         ~mitem_t() { clear(); }
2422       } *mitems;
2423       void clear();
2424       void extract();
2425       mitem_t *search(uint16_t pid);
2426       mgt_t(dvb_t *p) : dvb(p) {}
2427       ~mgt_t() { clear(); }
2428     } *mgt;
2429
2430     class vct_t {
2431     public:
2432       new_memset(0);
2433       class vitem_t;
2434
2435       dvb_t *dvb;
2436       uint32_t version;
2437       uint32_t transport_stream_id;
2438       int items, items_allocated;
2439
2440       class vitem_t {
2441       public:
2442         new_memset(0);
2443         class ch_elts_t;
2444
2445         uint16_t short_name[7+1];
2446         uint32_t major_channel_number;
2447         uint32_t minor_channel_number;
2448         uint32_t modulation_mode;
2449         uint32_t carrier_frequency;
2450         uint32_t channel_TSID;
2451         uint32_t program_number;
2452         uint32_t etm_location;
2453         uint8_t access_controlled;
2454         uint8_t hidden;
2455         uint8_t path_select;
2456         uint8_t out_of_band;
2457         uint32_t service_type;
2458         uint32_t source_id;
2459         uint32_t pcr_pid;
2460         int num_ch_elts;
2461         class ch_elts_t {
2462         public:
2463           uint32_t stream_id;
2464           uint32_t pes_pid;
2465           uint8_t  code_639[3];
2466           void extract(dvb_t *dvb);
2467         } elts[10];
2468         void extract(dvb_t *dvb);
2469       } **vct_tbl;
2470
2471       vct_t(dvb_t *p) : dvb(p) {}
2472       ~vct_t() { clear(); }
2473       void extract();
2474       void clear();
2475       int search(vitem_t &new_item);
2476       void append(vitem_t &new_item);
2477     } *tvct, *cvct;
2478
2479     class rrt_t {
2480     public:
2481       new_memset(0);
2482       class ritem_t;
2483
2484       dvb_t *dvb;
2485       uint32_t version;
2486       uint8_t region_nlen;
2487       uint8_t region_name[2*32+1];
2488       int items;
2489
2490       class ritem_t {
2491       public:
2492         new_memset(0);
2493         class rating_vt;
2494
2495         uint8_t dim_nlen;
2496         uint8_t dim_name[2*20+1];
2497         uint8_t graduated_scale;
2498         uint8_t num_values;
2499         class rating_vt {
2500         public:
2501           uint8_t rating_nlen;
2502           uint8_t rating_name[2*8+1];
2503           uint8_t rating_tlen;
2504           uint8_t rating_text[2*150+1];
2505           void extract(dvb_t *dvb);
2506         } *abrevs;
2507         void extract(dvb_t *dvb);
2508         void clear();
2509         ritem_t() {}
2510         ~ritem_t() { clear(); }
2511       } *ratings;
2512
2513       rrt_t(dvb_t *p) : dvb(p) {}
2514       ~rrt_t() { clear(); }
2515       void extract();
2516       void clear();
2517     } *rrt;
2518
2519     class eit_t {
2520     public:
2521       new_memset(0);
2522       class einfo_t;
2523
2524       dvb_t *dvb;
2525       eit_t *next;
2526       int id;
2527       uint32_t version;
2528       uint32_t source_id;
2529       int items, nitems;
2530
2531       class einfo_t {
2532       public:
2533         new_memset(0);
2534         uint16_t event_id;
2535         uint16_t location;
2536         uint32_t start_time;
2537         uint32_t seconds;
2538         uint8_t title_tlen;
2539         uint8_t *title_text;
2540         void extract(dvb_t *dvb);
2541         void clear();
2542         einfo_t() {}
2543         ~einfo_t() { clear(); }
2544       } *infos;
2545
2546       eit_t(dvb_t *p, int id) : dvb(p), id(id) {}
2547       ~eit_t() { clear(); }
2548       void extract();
2549       int search(uint16_t evt_id);
2550       void clear();
2551     } *eit;
2552
2553     class ett_t {
2554     public:
2555       new_memset(0);
2556       class etext_t;
2557
2558       dvb_t *dvb;
2559       ett_t *next;
2560       int id;
2561
2562       class etext_t {
2563       public:
2564         new_memset(0);
2565
2566         etext_t *next;
2567         int id;
2568         uint32_t version;
2569         uint16_t table_id;
2570         uint32_t etm_id;
2571         uint16_t msg_tlen;
2572         uint8_t *msg_text;
2573         void extract(dvb_t *dvb, uint32_t eid);
2574         void clear();
2575         etext_t(int id) : id(id) {}
2576         ~etext_t() { clear(); }
2577       } *texts;
2578
2579       ett_t(dvb_t *p, int id) : dvb(p), id(id) {}
2580       ~ett_t() { clear(); }
2581       void extract();
2582       void clear();
2583     } *ett;
2584
2585     class stt_t {
2586       dvb_t *dvb;
2587     public:
2588       new_memset(0);
2589
2590       uint32_t version;
2591       uint16_t table_id;
2592       uint32_t system_time;
2593       uint8_t utc_offset;
2594       uint16_t daylight_saving;
2595
2596       stt_t(dvb_t *p) : dvb(p) {}
2597       ~stt_t() { clear(); }
2598       void extract();
2599       void clear();
2600     } *stt;
2601
2602     uint32_t stt_start_time;
2603     int64_t stt_offset() {
2604       return (365*10+7)*24*3600L - (stt ? stt->utc_offset : 0);
2605     }
2606
2607     int64_t get_system_time() {
2608       return stt && stt->system_time ? stt->system_time + stt_offset() : -1;
2609     }
2610     zmpeg3_t *src;
2611     int sect_len;
2612     int stream_id;
2613     uint32_t version;
2614     int cur_next;
2615     int sect_num;
2616     int proto_ver;
2617     int empirical;
2618
2619     int bfr_pos()  { return xbfr - active->bfr; }
2620     int bfr_len()  { return active->bfr_size - bfr_pos(); }
2621     void skip_bfr(int n);
2622     void skp_bfr(int n) { if( n > 0 ) skip_bfr(n); }
2623
2624     int text_length, text_allocated;
2625     char *text;
2626     int get_text(uint8_t *dat, int bytes);
2627     void append_text(char *dat, int bytes);
2628     char *mstring(uint8_t *bp, int len);
2629
2630     dvb_t(zmpeg3_t *zsrc);
2631     ~dvb_t();
2632     void reset();
2633     void extract();
2634
2635     mgt_t::mitem_t *active;
2636     mgt_t::mitem_t *atsc_pid(int pid);
2637     mgt_t::mitem_t base_pid;
2638
2639     int atsc_tables(demuxer_t *demux, int pid);
2640     void skip_descr(int descr_len);
2641
2642     int channel_count();
2643     int signal_time();
2644     int get_channel(int n, int &major, int &minor);
2645     int get_station_id(int n, char *name);
2646     int total_astreams(int n, int &count);
2647     int astream_number(int n, int ord, int &stream, char *enc=0);
2648     int total_vstreams(int n, int &count);
2649     int vstream_number(int n, int ord, int &stream);
2650     int read_dvb(demuxer_t *demux);
2651     int get_chan_info(int n, int ord, int i, char *txt, int len);
2652   } dvb;
2653
2654 #endif
2655
2656   // Interface
2657
2658   // Get version information
2659   static int zmajor()   { return ZMPEG3_MAJOR; };
2660   static int zminor()   { return ZMPEG3_MINOR; };
2661   static int zrelease() { return ZMPEG3_RELEASE; };
2662
2663   // Check for file compatibility.  Return 1 if compatible.
2664   static int check_sig(char *path);
2665
2666   // Open the MPEG stream.
2667   // An error code is put into *ret if it fails and error_return is nonzero.
2668   zmpeg3_t(const char *path);
2669   zmpeg3_t(const char *path, int &ret, int access=fs_t::io_UNBUFFERED, const char *title_path=0);
2670   zmpeg3_t(int fd, int &ret, int access=0);
2671   zmpeg3_t(FILE *fp, int &ret, int access=0);
2672   int init(const char *title_path=0);
2673   ~zmpeg3_t();
2674
2675   // Performance
2676   int set_cpus(int cpus);
2677   int set_pts_padding(int v);
2678
2679   // Query the MPEG3 stream about audio.
2680   int has_audio();
2681   int total_astreams();
2682   int audio_channels(int stream);
2683   double sample_rate(int stream);
2684   const char *audio_format(int stream);
2685
2686   // Total length obtained from the timecode.
2687   // For DVD files, this is unreliable.
2688   long audio_nudge(int stream); 
2689   long audio_samples(int stream); 
2690   int set_sample(long sample, int stream);
2691   long get_sample(int stream);
2692
2693   // Stream defines the number of the multiplexed stream to read.
2694   // If output argument is null the audio is not rendered.
2695   // Read a PCM buffer of audio from 1 channel and advance the position.
2696   int read_audio(void *output_v, int type, int channel, long samples, int stream);
2697   int read_audio(double *output_d, int channel, long samples, int stream);
2698   int read_audio(float *output_f, int channel, long samples, int stream);
2699   int read_audio(int *output_i, int channel, long samples, int stream);
2700   int read_audio(short *output_s, int channel, long samples, int stream);
2701   // Read a PCM buffer of audio from 1 channel without advancing the position.
2702   int reread_audio(void *output_v, int type, int channel, long samples, int stream);
2703   int reread_audio(double *output_d, int channel, long samples, int stream);
2704   int reread_audio(float *output_f, int channel, long samples, int stream);
2705   int reread_audio(int *output_i, int channel, long samples, int stream);
2706   int reread_audio(short *output_s, int channel, long samples, int stream);
2707
2708   // Read the next compressed audio chunk.  Store the size in size and return a 
2709   // Stream defines the number of the multiplexed stream to read.
2710   int read_audio_chunk(uint8_t *output, long *size, long max_size, int stream);
2711
2712   // Query the stream about video.
2713   int has_video();
2714   int total_vstreams();
2715   int video_width(int stream);
2716   int video_height(int stream);
2717   int coded_width(int stream);
2718   int coded_height(int stream);
2719   int video_pid(int stream);
2720   float aspect_ratio(int stream); // aspect ratio.  0 if none
2721   double frame_rate(int stream);  // Frames/sec
2722
2723   long video_frames(int stream);  // total length - for TOC files only.
2724   int set_frame(long frame, int stream); // Seek to a frame
2725   // int skip_frames(); /*???*/
2726   long get_frame(int stream);            // Tell current position
2727
2728   // Total bytes.  Used for absolute byte seeking.
2729   int64_t get_bytes();
2730
2731   // Seek all the tracks to the absolute byte in the 
2732   // file.  This eliminates the need for tocs but doesn't 
2733   // give frame accuracy.
2734   int seek_byte(int64_t byte);
2735   int64_t tell_byte();
2736
2737   int previous_frame(int stream);
2738   int end_of_audio(int stream);
2739   int end_of_video(int stream);
2740
2741   // Give the seconds time in the last packet read
2742   double get_time();
2743   double get_audio_time(int stream);
2744   double get_video_time(int stream);
2745   int get_cell_time(int no, double &time);
2746
2747   // Read input frame and scale to the output frame
2748   // The last row of **output_rows must contain 4 extra bytes for scratch work.
2749   int read_frame(uint8_t **output_rows, // row start pointers
2750     int in_x, int in_y, int in_w, int in_h, // Location in input frame
2751     int out_w, int out_h, // Dimensions of output_rows
2752     int color_model, int stream);
2753
2754   // Get the colormodel being used natively by the stream
2755   int colormodel(int stream);
2756   // Set the row stride to be used in read_yuvframe
2757   int set_rowspan(int bytes, int stream);
2758
2759   // Read a frame in the native color model used by the stream. 
2760   // The Y, U, and V planes are copied into the y, u, and v
2761   // BUFFERS Provided.
2762   // The input is cropped to the dimensions given but not scaled.
2763   int read_yuvframe(char *y_output, char *u_output, char *v_output,
2764     int in_x, int in_y, int in_w, int in_h, int stream);
2765
2766   // Read a frame in the native color model used by the stream. 
2767   // The Y, U, and V planes are not copied but the _output pointers
2768   // are redirected to the frame buffer.
2769   int read_yuvframe_ptr(char **y_output, char **u_output, char **v_output,
2770     int stream);
2771
2772   // Drop frames number of frames
2773   int drop_frames(long frames, int stream);
2774
2775   // Read the next compressed frame including headers.
2776   // Store the size in size and return a 1 if error.
2777   // Stream defines the number of the multiplexed stream to read.
2778   int read_video_chunk(uint8_t *output, long *size, long max_size,
2779     int stream);
2780
2781   // Master control
2782   int get_total_vts_titles();
2783   int set_vts_title(int title=-1);
2784   int get_total_interleaves();
2785   int set_interleave(int inlv=-1);
2786   int set_angle(int a=-1);
2787   int set_program(int no); // legacy
2788
2789   // Memory used by video caches.
2790   int64_t memory_usage();
2791   // write incoming datastream on file descr fd
2792   //   buffer access must be io_THREADED.  useful to record dvb device data
2793   //   bsz records up to bsz bytes of past buffer data in write_align
2794   int start_record(int fd, int bsz=0);
2795   int stop_record();
2796   void write_record(uint8_t *data, int len);
2797   // limit position for start of last full packet
2798   int64_t record_position() { return recd_pos - packet_size + 1; }
2799   // restart streams, discard device buffer and reinit file positions
2800   void restart();
2801   // enable/disable data xfr from reader thread
2802   int pause_reader(int v);
2803
2804   // subtitle functions
2805   // get number of subtitle tracks
2806   int subtitle_tracks() { return total_stracks; }
2807   // Enable overlay of a subtitle track.
2808   // track - the number of the subtitle track starting from 0
2809   // The same subtitle track is overlayed for all video tracks.
2810   // track=-1 to disable subtitles.
2811   int show_subtitle(int stream, int strk);
2812
2813   // Table of contents generation
2814   // Begin constructing table of contents
2815   static zmpeg3_t *start_toc(const char *path, const char *toc_path,
2816     int program=0, int64_t *total_bytes=0);
2817   // Set the maximum number of bytes per index track
2818   void set_index_bytes(int64_t bytes) { index_bytes = bytes; }
2819   // Process one packet
2820   int do_toc(int64_t *bytes_processed);
2821   // Write table of contents
2822   void stop_toc();
2823   int set_thumbnail_callback(int trk, int skim, int thumb, zthumbnail_cb fn, void *p);
2824   int get_thumbnail(int trk, int64_t &frn, uint8_t *&t, int &w, int &h);
2825   int set_cc_text_callback(int trk, zcc_text_cb fn);
2826
2827   // Get modification date of source file from table of contents.
2828   // Used to compare DVD source file to table of contents source.
2829   int64_t get_source_date() { return source_date; }
2830   // Get modification date of source file from source file.
2831   static int64_t calculate_source_date(char *path);
2832   static int64_t calculate_source_date(int fd);
2833   // Table of contents queries
2834   int index_tracks();
2835   int index_channels(int track);
2836   int index_zoom();
2837   int index_size(int track);
2838   float* index_data(int track, int channel);
2839   // Returns 1 if the file has a table of contents
2840   int has_toc() {
2841     return frame_offsets || sample_offsets ? 1 : 0;
2842   }
2843   // Return the path of the title number or 0 if no more titles.
2844   char* title_path(int number) {
2845     return number>=0 && number<demuxer->total_titles ?
2846       demuxer->titles[number]->fs->path : 0;
2847   }
2848
2849   static inline void complete_path(char *full_path, char *path) {
2850     char dir[STRLEN];
2851     if( path[0] != '/' && getcwd(dir, sizeof(dir)) )
2852       snprintf(full_path, STRLEN, "%s/%s", dir, path);
2853     else
2854       strcpy(full_path, path);
2855   }
2856
2857   static inline void get_directory(char *directory, char *path) {
2858     char *ptr = strrchr(path, '/');
2859     int i = 0;
2860     if( ptr ) {
2861       int n = ptr-path;
2862       while( i < n ) {
2863         directory[i] = path[i];
2864         ++i;
2865       }
2866     }
2867     if( i == 0 ) directory[i++] = '.';
2868     directory[i] = 0;
2869   }
2870
2871   static inline void get_filename(char *filename, char *path) {
2872     char *ptr = strrchr(path, '/');
2873     if( ptr ) ++ptr;
2874     else ptr = path;
2875     strcpy(filename, ptr);
2876   }
2877
2878   static inline void joinpath(char *path, char *dir, char *filename) {
2879     snprintf(path, STRLEN, "%s/%s", dir, filename);
2880   }
2881
2882   static inline int64_t path_total_bytes(char *path) {
2883     struct stat64 st;
2884     return stat64(path, &st )<0 ? 0 : st.st_size;
2885   }
2886
2887 };
2888
2889 typedef zmpeg3_t::zlock_t                       zzlock_t;
2890 typedef zmpeg3_t::zblock_t                      zzblock_t;
2891 typedef zmpeg3_t::zrwlock_t                     zzrwlock_t;
2892 typedef zmpeg3_t::fs_t                          zfs_t;
2893 typedef   zfs_t::css_t                          zcss_t;
2894 typedef     zcss_t::key_t                       zkey_t;
2895 typedef     zcss_t::blk_t                       zblk_t;
2896 typedef     zcss_t::playkey_t                   zplaykey_t;
2897 typedef   zfs_t::buffer_t                       zbuffer_t;
2898 typedef zmpeg3_t::bits_t                        zbits_t;
2899 typedef zmpeg3_t::index_t                       zindex_t;
2900 typedef zmpeg3_t::bitfont_t                     zbitfont_t;
2901 typedef   zbitfont_t::bitchar_t                 zbitchar_t;
2902 typedef   zbitfont_t::static_init_t             zstatic_init_t;
2903 typedef zmpeg3_t::timecode_t                    ztimecode_t;
2904 typedef zmpeg3_t::audio_t                       zaudio_t;
2905 typedef   zaudio_t::imdct_complex_t             zimdct_complex_t;
2906 typedef   zaudio_t::imdct_al_table_t            zimdct_al_table_t;
2907 typedef   zaudio_t::audio_decoder_layer_t       zaudio_decoder_layer_t;
2908 typedef   zaudio_t::audio_decoder_ac3_t         zaudio_decoder_ac3_t;
2909 typedef   zaudio_t::audio_decoder_pcm_t         zaudio_decoder_pcm_t;
2910 typedef zmpeg3_t::VLCtab_t                      zVLCtab_t;
2911 typedef zmpeg3_t::DCTtab_t                      zDCTtab_t;
2912 typedef zmpeg3_t::slice_buffer_t                zslice_buffer_t;
2913 typedef zmpeg3_t::video_t                       zvideo_t;
2914 typedef   zvideo_t::cc_t                        zcc_t;
2915 typedef     zcc_t::svc_t                        zsvc_t;
2916 typedef       zsvc_t::win_t                     zwin_t;
2917 typedef       zsvc_t::chr_t                     zchr_t;
2918 typedef zmpeg3_t::slice_decoder_t               zslice_decoder_t;
2919 typedef zmpeg3_t::subtitle_t                    zsubtitle_t;
2920 typedef zmpeg3_t::demuxer_t                     zdemuxer_t;
2921 typedef   zdemuxer_t::zstream_t                 zzstream_t;
2922 typedef   zdemuxer_t::title_t                   ztitle_t;
2923 typedef     ztitle_t::cell_t                    zcell_t;
2924 typedef zmpeg3_t::cacheframe_t                  zcacheframe_t;
2925 typedef zmpeg3_t::cache_t                       zcache_t;
2926 typedef zmpeg3_t::atrack_t                      zatrack_t;
2927 typedef zmpeg3_t::vtrack_t                      zvtrack_t;
2928 typedef zmpeg3_t::strack_t                      zstrack_t;
2929 typedef zmpeg3_t::icell_t                       zicell_t;
2930 typedef zmpeg3_t::icell_table_t                 zicell_table_t;
2931 typedef zmpeg3_t::ifo_t                         zifo_t;
2932 #ifdef ZDVB
2933 typedef zmpeg3_t::dvb_t                         zdvb_t;
2934 typedef   zdvb_t::mgt_t                         zmgt_t;
2935 typedef     zmgt_t::mitem_t                     zmitem_t;
2936 typedef   zdvb_t::vct_t                         zvct_t;
2937 typedef     zvct_t::vitem_t                     zvitem_t;
2938 typedef       zvitem_t::ch_elts_t               zch_elts_t;
2939 typedef   zdvb_t::rrt_t                         zrrt_t;
2940 typedef     zrrt_t::ritem_t                     zritem_t;
2941 typedef       zritem_t::rating_vt               zrating_vt;
2942 typedef   zdvb_t::eit_t                         zeit_t;
2943 typedef     zeit_t::einfo_t                     zeinfo_t;
2944 typedef   zdvb_t::ett_t                         zett_t;
2945 typedef     zett_t::etext_t                     zetext_t;
2946 typedef   zdvb_t::stt_t                         zstt_t;
2947 #endif
2948
2949 /* legacy */
2950 extern "C" {
2951 #else
2952 typedef struct {} zmpeg3_t;
2953 #endif /*__cplusplus */
2954 typedef zmpeg3_t mpeg3_t;
2955
2956 int mpeg3_major(void);
2957 int mpeg3_minor(void);
2958 int mpeg3_release(void);
2959 int mpeg3_check_sig(char *path);
2960 int mpeg3_is_program_stream(zmpeg3_t * zsrc);
2961 int mpeg3_is_transport_stream(zmpeg3_t * zsrc);
2962 int mpeg3_is_video_stream(zmpeg3_t * zsrc);
2963 int mpeg3_is_audio_stream(zmpeg3_t * zsrc);
2964 int mpeg3_is_ifo_file(zmpeg3_t * zsrc);
2965 int mpeg3_create_title(zmpeg3_t * zsrc,int full_scan);
2966 zmpeg3_t *mpeg3_open(const char *path,int *error_return);
2967 zmpeg3_t *mpeg3_open_title(const char *title_path,const char *path,int *error_return);
2968 zmpeg3_t *mpeg3_zopen(const char *title_path,const char *path,int *error_return, int access);
2969 zmpeg3_t *mpeg3_open_copy(const char *path,zmpeg3_t *old_src,int *error_return);
2970 int mpeg3_close(zmpeg3_t *zsrc);
2971 int mpeg3_set_pts_padding(zmpeg3_t *zsrc, int v);
2972 int mpeg3_set_cpus(zmpeg3_t *zsrc,int cpus);
2973 int mpeg3_has_audio(zmpeg3_t *zsrc);
2974 int mpeg3_total_astreams(zmpeg3_t *zsrc);
2975 int mpeg3_audio_channels(zmpeg3_t *zsrc,int stream);
2976 int mpeg3_sample_rate(zmpeg3_t *zsrc,int stream);
2977 const char *mpeg3_audio_format(zmpeg3_t *zsrc,int stream);
2978 long mpeg3_get_audio_nudge(zmpeg3_t *zsrc,int stream);
2979 long mpeg3_audio_samples(zmpeg3_t *zsrc,int stream);
2980 int mpeg3_set_sample(zmpeg3_t *zsrc,long sample,int stream);
2981 long mpeg3_get_sample(zmpeg3_t *zsrc,int stream);
2982 int mpeg3_read_audio(zmpeg3_t *zsrc,float *output_f,short *output_i,int channel,
2983    long samples,int stream);
2984 int mpeg3_read_audio_d(zmpeg3_t *zsrc,double *output_d, int channel,
2985    long samples, int stream);
2986 int mpeg3_read_audio_f(zmpeg3_t *zsrc,float *output_f, int channel,
2987    long samples, int stream);
2988 int mpeg3_read_audio_i(zmpeg3_t *zsrc,int *output_i, int channel,
2989    long samples, int stream);
2990 int mpeg3_read_audio_s(zmpeg3_t *zsrc,short *output_s, int channel,
2991    long samples, int stream);
2992 int mpeg3_reread_audio_d(zmpeg3_t *zsrc,double *output_d, int channel,
2993    long samples, int stream);
2994 int mpeg3_reread_audio_f(zmpeg3_t *zsrc,float *output_f, int channel,
2995    long samples, int stream);
2996 int mpeg3_reread_audio_i(zmpeg3_t *zsrc,int *output_i, int channel,
2997    long samples, int stream);
2998 int mpeg3_reread_audio_s(zmpeg3_t *zsrc,short *output_s, int channel,
2999    long samples, int stream);
3000 int mpeg3_reread_audio(zmpeg3_t *zsrc,float *output_f,short *output_i,
3001    int channel,long samples,int stream);
3002 int mpeg3_read_audio_chunk(zmpeg3_t *zsrc,unsigned char *output,long *size,
3003    long max_size,int stream);
3004 int mpeg3_has_video(zmpeg3_t *zsrc);
3005 int mpeg3_total_vstreams(zmpeg3_t *zsrc);
3006 int mpeg3_video_width(zmpeg3_t *zsrc,int stream);
3007 int mpeg3_video_height(zmpeg3_t *zsrc,int stream);
3008 int mpeg3_coded_width(zmpeg3_t *zsrc,int stream);
3009 int mpeg3_coded_height(zmpeg3_t *zsrc,int stream);
3010 int mpeg3_video_pid(zmpeg3_t *zsrc,int stream);
3011 float mpeg3_aspect_ratio(zmpeg3_t *zsrc,int stream);
3012 double mpeg3_frame_rate(zmpeg3_t *zsrc,int stream);
3013 long mpeg3_video_frames(zmpeg3_t *zsrc,int stream);
3014 int mpeg3_set_frame(zmpeg3_t *zsrc,long frame,int stream);
3015 //int mpeg3_skip_frames(zmpeg3_t *zsrc);
3016 long mpeg3_get_frame(zmpeg3_t *zsrc,int stream);
3017 int64_t mpeg3_get_bytes(zmpeg3_t *zsrc);
3018 int mpeg3_seek_byte(zmpeg3_t *zsrc,int64_t byte);
3019 int64_t mpeg3_tell_byte(zmpeg3_t *zsrc);
3020 int mpeg3_previous_frame(zmpeg3_t *zsrc,int stream);
3021 int mpeg3_end_of_audio(zmpeg3_t *zsrc,int stream);
3022 int mpeg3_end_of_video(zmpeg3_t *zsrc,int stream);
3023 double mpeg3_get_time(zmpeg3_t *zsrc);
3024 double mpeg3_get_audio_time(zmpeg3_t *zsrc, int stream);
3025 double mpeg3_get_video_time(zmpeg3_t *zsrc, int stream);
3026 double mpeg3_get_cell_time(zmpeg3_t *zsrc, int no, double *time);
3027 int mpeg3_read_frame(zmpeg3_t *zsrc,unsigned char **output_rows,int in_x,
3028    int in_y,int in_w,int in_h,int out_w,int out_h,int color_model,int stream);
3029 int mpeg3_colormodel(zmpeg3_t *zsrc,int stream);
3030 int mpeg3_set_rowspan(zmpeg3_t *zsrc,int bytes,int stream);
3031 int mpeg3_read_yuvframe(zmpeg3_t *zsrc,char *y_output,char *u_output,
3032    char *v_output,int in_x,int in_y,int in_w,int in_h,int stream);
3033 int mpeg3_read_yuvframe_ptr(zmpeg3_t *zsrc,char **y_output,char **u_output,
3034    char **v_output,int stream);
3035 int mpeg3_drop_frames(zmpeg3_t *zsrc,long frames,int stream);
3036 int mpeg3_read_video_chunk(zmpeg3_t *zsrc,unsigned char *output,long *size,
3037    long max_size,int stream);
3038 int mpeg3_get_total_vts_titles(zmpeg3_t *zsrc);
3039 int mpeg3_set_vts_title(zmpeg3_t *zsrc,int title); // toc build only
3040 int mpeg3_get_total_interleaves(zmpeg3_t *zsrc);
3041 int mpeg3_set_interleave(zmpeg3_t *zsrc,int inlv); // toc build only
3042 int mpeg3_set_angle(zmpeg3_t *zsrc,int a); // toc build only
3043 int mpeg3_set_program(zmpeg3_t *zsrc,int no); // toc build only
3044 int64_t mpeg3_memory_usage(zmpeg3_t *zsrc);
3045 int mpeg3_get_thumbnail(zmpeg3_t *zsrc, int trk,
3046    int64_t *frn, uint8_t **t, int *w, int *h);
3047 int mpeg3_set_thumbnail_callback(zmpeg3_t *zsrc, int trk,
3048    int skim, int thumb, zthumbnail_cb fn, void *p);
3049 int mpeg3_set_cc_text_callback(zmpeg3_t *zsrc, int trk, zcc_text_cb fn);
3050 int mpeg3_subtitle_tracks(zmpeg3_t *zsrc);
3051 int mpeg3_show_subtitle(zmpeg3_t *zsrc,int vtrk, int strk);
3052 int mpeg3_display_subtitle(zmpeg3_t *zsrc,int stream,int sid,int id,
3053    uint8_t *yp,uint8_t *up,uint8_t *vp,uint8_t *ap,
3054    int x,int y,int w,int h,double start_msecs,double stop_msecs);
3055 int mpeg3_delete_subtitle(zmpeg3_t *zsrc,int stream,int sid,int id);
3056 zmpeg3_t *mpeg3_start_toc(char *path,char *toc_path,int program,int64_t *total_bytes);
3057 void mpeg3_set_index_bytes(zmpeg3_t *zsrc,int64_t bytes);
3058 int mpeg3_do_toc(zmpeg3_t *zsrc,int64_t *bytes_processed);
3059 void mpeg3_stop_toc(zmpeg3_t *zsrc);
3060 int64_t mpeg3_get_source_date(zmpeg3_t *zsrc);
3061 int64_t mpeg3_calculate_source_date(char *path);
3062 int mpeg3_index_tracks(zmpeg3_t *zsrc);
3063 int mpeg3_index_channels(zmpeg3_t *zsrc,int track);
3064 int mpeg3_index_zoom(zmpeg3_t *zsrc);
3065 int mpeg3_index_size(zmpeg3_t *zsrc,int track);
3066 float *mpeg3_index_data(zmpeg3_t *zsrc,int track,int channel);
3067 int mpeg3_has_toc(zmpeg3_t *zsrc);
3068 char *mpeg3_title_path(zmpeg3_t *zsrc,int number);
3069 // hooks needed for quicktime
3070 typedef struct {} mpeg3_layer_t;
3071 int mpeg3audio_dolayer3(mpeg3_layer_t *audio,
3072    char *frame, int frame_size, float **output, int render);
3073 int mpeg3_layer_header(mpeg3_layer_t *layer_data, unsigned char *data);
3074 void mpeg3_layer_reset(mpeg3_layer_t *zlayer_data);
3075 mpeg3_layer_t* mpeg3_new_layer();
3076 void mpeg3_delete_layer(mpeg3_layer_t *audio);
3077 // hooks needed for mplexlo
3078 void mpeg3_skip_video_frame(mpeg3_t *zsrc, int stream);
3079 int64_t mpeg3_video_tell_byte(mpeg3_t *zsrc, int stream);
3080 int64_t mpeg3_audio_tell_byte(mpeg3_t *zsrc, int stream);
3081 #ifdef ZDVB
3082 int mpeg3_dvb_channel_count(zmpeg3_t *zsrc);
3083 int mpeg3_dvb_get_channel(mpeg3_t *zsrc,int n, int *major, int *minor);
3084 int mpeg3_dvb_get_station_id(mpeg3_t *zsrc,int n, char *name);
3085 int mpeg3_dvb_total_astreams(mpeg3_t *zsrc,int n, int *count);
3086 int mpeg3_dvb_astream_number(mpeg3_t *zsrc,int n, int ord, int *stream, char *enc);
3087 int mpeg3_dvb_total_vstreams(mpeg3_t *zsrc,int n, int *count);
3088 int mpeg3_dvb_vstream_number(mpeg3_t *zsrc,int n, int ord, int *stream);
3089 int mpeg3_dvb_get_chan_info(mpeg3_t *zsrc,int n, int ord, int i, char *cp, int len);
3090 int mpeg3_dvb_get_system_time(mpeg3_t *zsrc, int64_t *tm);
3091 #endif
3092 #ifdef __cplusplus
3093 }
3094 #endif
3095 #endif
3096 #endif