1 /*
2  * Copyright (C) 2000-2018 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * demultiplexer for matroska streams: shared header
21  */
22 
23 #ifndef _DEMUX_MATROSKA_H_
24 #define _DEMUX_MATROSKA_H_
25 
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <zlib.h>
33 
34 #include <xine/xine_internal.h>
35 #include <xine/demux.h>
36 #include <xine/buffer.h>
37 #include "bswap.h"
38 
39 #include "ebml.h"
40 #include "matroska.h"
41 
42 #define NUM_PREVIEW_BUFFERS      10
43 
44 #define MAX_STREAMS             128
45 #define MAX_FRAMES              128
46 
47 #define WRAP_THRESHOLD        90000
48 
49 typedef struct {
50   int                  track_num;
51   off_t               *pos;
52   uint64_t            *timecode;
53   int                  num_entries;
54 
55 } matroska_index_t;
56 
57 typedef struct {
58 
59   demux_plugin_t       demux_plugin;
60 
61   xine_stream_t       *stream;
62 
63   input_plugin_t      *input;
64 
65   int                  status;
66 
67   ebml_parser_t       *ebml;
68 
69   /* segment element */
70   ebml_elem_t          segment;
71   uint64_t             timecode_scale;
72   int                  duration;            /* in millis */
73   int                  preview_sent;
74   int                  preview_mode;
75   char                *title;
76 
77   /* meta seek info */
78   int                  has_seekhead;
79   int                  seekhead_handled;
80 
81   /* seek info */
82   matroska_index_t    *indexes;
83   int                  num_indexes;
84   int                  first_cluster_found;
85   int                  skip_to_timecode;
86   int                  skip_for_track;
87 
88   /* tracks */
89   int                  num_tracks;
90   int                  num_video_tracks;
91   int                  num_audio_tracks;
92   int                  num_sub_tracks;
93 
94   matroska_track_t    *tracks[MAX_STREAMS];
95   size_t               compress_maxlen;
96 
97   /* maintain editions, number and capacity */
98   int                  num_editions, cap_editions;
99   matroska_edition_t **editions;
100 
101   /* block */
102   uint8_t             *block_data;
103   size_t               block_data_size;
104 
105   /* current tracks */
106   matroska_track_t    *video_track;   /* to remove */
107   matroska_track_t    *audio_track;   /* to remove */
108   matroska_track_t    *sub_track;     /* to remove */
109   uint64_t             last_timecode;
110 
111   int                  send_newpts;
112   int                  buf_flag_seek;
113 
114   /* seekhead parsing */
115   int                  top_level_list_size;
116   int                  top_level_list_max_size;
117   off_t               *top_level_list;
118 
119   /* event handling (chapter navigation) */
120   xine_event_queue_t  *event_queue;
121 } demux_matroska_t ;
122 
123 /* "entry points" for chapter handling.
124  * The parser descends into "Chapters" elements at the _parse_ function,
125  * and editions care about cleanup internally. */
126 int matroska_parse_chapters(demux_matroska_t*);
127 void matroska_free_editions(demux_matroska_t*);
128 
129 /* Search an edition for the chapter matching a given timecode.
130  *
131  * Return: chapter index, or -1 if none is found.
132  *
133  * TODO: does not handle chapter end times yet.
134  */
135 int matroska_get_chapter(demux_matroska_t*, uint64_t, matroska_edition_t**);
136 
137 #endif /* _DEMUX_MATROSKA_H_ */
138