1 /*
2    This file is free software; you can redistribute it and/or
3    modify it under the terms of the GNU Lesser General Public
4    License as published by the Free Software Foundation; either
5    version 2.1 of the License, or (at your option) any later version.
6 
7    LiVES is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10    Lesser General Public License for more details.
11 
12    You should have received a copy of the GNU Lesser General Public
13    License along with LiVES; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 */
16 
17 // codec types - it would be very nice if the ffmpeg devs would put this in a header...
18 
19 // based on code (c) FFMpeg team
20 
21 typedef uint8_t lives_asf_guid[16];
22 
23 const lives_asf_guid lives_asf_header = {
24   0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C
25 };
26 
27 const lives_asf_guid lives_asf_file_header = {
28   0xA1, 0xDC, 0xAB, 0x8C, 0x47, 0xA9, 0xCF, 0x11, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65
29 };
30 
31 const lives_asf_guid lives_asf_stream_header = {
32   0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65
33 };
34 
35 const lives_asf_guid lives_asf_ext_stream_header = {
36   0xCB, 0xA5, 0xE6, 0x14, 0x72, 0xC6, 0x32, 0x43, 0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A
37 };
38 
39 const lives_asf_guid lives_asf_audio_stream = {
40   0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B
41 };
42 
43 /*
44   const lives_asf_guid lives_asf_audio_conceal_none = {
45   // 0x40, 0xa4, 0xf1, 0x49, 0x4ece, 0x11d0, 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6
46   // New value lifted from avifile
47   0x00, 0x57, 0xfb, 0x20, 0x55, 0x5B, 0xCF, 0x11, 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b
48   };
49 
50   const lives_asf_guid lives_asf_audio_conceal_spread = {
51   0x50, 0xCD, 0xC3, 0xBF, 0x8F, 0x61, 0xCF, 0x11, 0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20
52   };
53 */
54 const lives_asf_guid lives_asf_video_stream = {
55   0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B
56 };
57 /*
58   const lives_asf_guid lives_asf_video_conceal_none = {
59   0x00, 0x57, 0xFB, 0x20, 0x55, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B
60   };
61 
62 */
63 
64 const lives_asf_guid lives_asf_command_stream = {
65   0xC0, 0xCF, 0xDA, 0x59, 0xE6, 0x59, 0xD0, 0x11, 0xA3, 0xAC, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6
66 };
67 
68 const lives_asf_guid lives_asf_comment_header = {
69   0x33, 0x26, 0xb2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
70 };
71 
72 const lives_asf_guid lives_asf_codec_comment_header = {
73   0x40, 0x52, 0xD1, 0x86, 0x1D, 0x31, 0xD0, 0x11, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6
74 };
75 const lives_asf_guid lives_asf_codec_comment1_header = {
76   0x41, 0x52, 0xd1, 0x86, 0x1D, 0x31, 0xD0, 0x11, 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6
77 };
78 
79 const lives_asf_guid lives_asf_data_header = {
80   0x36, 0x26, 0xb2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
81 };
82 
83 const lives_asf_guid lives_asf_head1_guid = {
84   0xb5, 0x03, 0xbf, 0x5f, 0x2E, 0xA9, 0xCF, 0x11, 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65
85 };
86 
87 const lives_asf_guid lives_asf_head2_guid = {
88   0x11, 0xd2, 0xd3, 0xab, 0xBA, 0xA9, 0xCF, 0x11, 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65
89 };
90 
91 const lives_asf_guid lives_asf_extended_content_header = {
92   0x40, 0xA4, 0xD0, 0xD2, 0x07, 0xE3, 0xD2, 0x11, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50
93 };
94 
95 const lives_asf_guid lives_asf_simple_index_header = {
96   0x90, 0x08, 0x00, 0x33, 0xB1, 0xE5, 0xCF, 0x11, 0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB
97 };
98 
99 const lives_asf_guid lives_asf_ext_stream_embed_stream_header = {
100   0xe2, 0x65, 0xfb, 0x3a, 0xEF, 0x47, 0xF2, 0x40, 0xac, 0x2c, 0x70, 0xa9, 0x0d, 0x71, 0xd3, 0x43
101 };
102 
103 const lives_asf_guid lives_asf_ext_stream_audio_stream = {
104   0x9d, 0x8c, 0x17, 0x31, 0xE1, 0x03, 0x28, 0x45, 0xb5, 0x82, 0x3d, 0xf9, 0xdb, 0x22, 0xf5, 0x03
105 };
106 
107 const lives_asf_guid lives_asf_metadata_header = {
108   0xea, 0xcb, 0xf8, 0xc5, 0xaf, 0x5b, 0x77, 0x48, 0x84, 0x67, 0xaa, 0x8c, 0x44, 0xfa, 0x4c, 0xca
109 };
110 
111 const lives_asf_guid lives_asf_marker_header = {
112   0x01, 0xCD, 0x87, 0xF4, 0x51, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65
113 };
114 
115 const lives_asf_guid lives_asf_my_guid = {
116   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
117 };
118 
119 const lives_asf_guid lives_asf_language_guid = {
120   0xa9, 0x46, 0x43, 0x7c, 0xe0, 0xef, 0xfc, 0x4b, 0xb2, 0x29, 0x39, 0x3e, 0xde, 0x41, 0x5c, 0x85
121 };
122 
123 const lives_asf_guid lives_asf_content_encryption = {
124   0xfb, 0xb3, 0x11, 0x22, 0x23, 0xbd, 0xd2, 0x11, 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e
125 };
126 
127 const lives_asf_guid lives_asf_ext_content_encryption = {
128   0x14, 0xe6, 0x8a, 0x29, 0x22, 0x26, 0x17, 0x4c, 0xb9, 0x35, 0xda, 0xe0, 0x7e, 0xe9, 0x28, 0x9c
129 };
130 
131 const lives_asf_guid lives_asf_digital_signature = {
132   0xfc, 0xb3, 0x11, 0x22, 0x23, 0xbd, 0xd2, 0x11, 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e
133 };
134 
135 static const lives_asf_guid index_guid = {
136   0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
137 };
138 
139 static const lives_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
140   0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
141 };
142 
143 /* List of official tags at http://msdn.microsoft.com/en-us/library/dd743066(VS.85).aspx */
144 /*const AVMetadataConv lives_asf_metadata_conv[] = {
145   { "WM/AlbumArtist"     , "album_artist"},
146   { "WM/AlbumTitle"      , "album"       },
147   { "Author"             , "artist"      },
148   { "Description"        , "comment"     },
149   { "WM/Composer"        , "composer"    },
150   { "WM/EncodedBy"       , "encoded_by"  },
151   { "WM/EncodingSettings", "encoder"     },
152   { "WM/Genre"           , "genre"       },
153   { "WM/Language"        , "language"    },
154   { "WM/OriginalFilename", "filename"    },
155   { "WM/PartOfSet"       , "disc"        },
156   { "WM/Publisher"       , "publisher"   },
157   { "WM/Tool"            , "encoder"     },
158   { "WM/TrackNumber"     , "track"       },
159   { "WM/Track"           , "track"       },
160   //  { "Year"               , "date"        }, TODO: conversion year<->date
161   { 0 }
162   };*/
163 
164 #define ASF_PROBE_SIZE 16
165 
166 #define PACKET_SIZE 3200
167 
168 typedef struct {
169   int num;
170   unsigned char seq;
171   /* use for reading */
172   AVPacket pkt;
173   int frag_offset;
174   int timestamp;
175   int64_t duration;
176 
177   int ds_span;                /* descrambling  */
178   int ds_packet_size;
179   int ds_chunk_size;
180 
181   int64_t packet_pos;
182 
183   uint16_t stream_language_index;
184 
185   int      palette_changed;
186   uint32_t palette[256];
187 } ASFStream;
188 
189 typedef struct {
190   lives_asf_guid guid;                  ///< generated by client computer
191   uint64_t file_size;         /**< in bytes
192 			          invalid if broadcasting */
193   uint64_t create_time;       /**< time of creation, in 100-nanosecond units since 1.1.1601
194 			          invalid if broadcasting */
195   uint64_t play_time;         /**< play time, in 100-nanosecond units
196 			        invalid if broadcasting */
197   uint64_t send_time;         /**< time to send file, in 100-nanosecond units
198 			          invalid if broadcasting (could be ignored) */
199   uint32_t preroll;           /**< timestamp of the first packet, in milliseconds
200 			          if nonzero - subtract from time */
201   uint32_t ignore;            ///< preroll is 64bit - but let's just ignore it
202   uint32_t flags;             /**< 0x01 - broadcast
203 			          0x02 - seekable
204 			          rest is reserved should be 0 */
205   uint32_t min_pktsize;       /**< size of a data packet
206 			          invalid if broadcasting */
207   uint32_t max_pktsize;       /**< shall be the same as for min_pktsize
208 			          invalid if broadcasting */
209   uint32_t max_bitrate;       /**< bandwidth of stream in bps
210 			          should be the sum of bitrates of the
211 			          individual media streams */
212 } ASFMainHeader;
213 
214 typedef struct {
215   uint32_t packet_number;
216   uint16_t packet_count;
217 } ASFIndex;
218 
219 typedef struct {
220   uint32_t seqno;
221   int is_streamed;
222   int asfid2avid[128];                 ///< conversion table from asf ID 2 AVStream ID
223   ASFStream streams[128];              ///< it's max number and it's not that big
224   uint32_t stream_bitrates[128];       ///< max number of streams, bitrate for each (for streaming)
225   char stream_languages[128][6];       ///< max number of streams, language for each (RFC1766, e.g. en-US)
226   /* non streamed additonnal info */
227   uint64_t nb_packets;                 ///< how many packets are there in the file, invalid if broadcasting
228   int64_t duration;                    ///< in 100ns units
229   /* packet filling */
230   unsigned char multi_payloads_present;
231   int packet_size_left;
232   int packet_timestamp_start;
233   int packet_timestamp_end;
234   unsigned int packet_nb_payloads;
235   int packet_nb_frames;
236   uint8_t packet_buf[PACKET_SIZE];
237   /* only for reading */
238   uint64_t data_offset;                ///< beginning of the first data packet
239   uint64_t data_object_offset;         ///< data object offset (excl. GUID & size)
240   uint64_t data_object_size;           ///< size of the data object
241   int index_read;
242 
243   ASFMainHeader hdr;
244 
245   int packet_flags;
246   int packet_property;
247   int packet_timestamp;
248   int packet_segsizetype;
249   int packet_segments;
250   int packet_seq;
251   int packet_replic_size;
252   int packet_key_frame;
253   int packet_padsize;
254   unsigned int packet_frag_offset;
255   unsigned int packet_frag_size;
256   int64_t packet_frag_timestamp;
257   int packet_multi_size;
258   int packet_obj_size;
259   int packet_time_delta;
260   int packet_time_start;
261   int64_t packet_pos;
262 
263   int stream_index;
264 
265   int64_t last_indexed_pts;
266   ASFIndex *index_ptr;
267   uint32_t nb_index_count;
268   uint32_t nb_index_memory_alloc;
269   uint16_t maximum_packet;
270 
271   ASFStream *asf_st;                   ///< currently decoded stream
272 } ASFContext;
273 
274 #define FRAME_HEADER_SIZE 17
275 
276 typedef struct _index_entry index_entry;
277 
278 struct _index_entry {
279   index_entry *next; ///< ptr to next entry
280   uint32_t dts; ///< dts of keyframe
281   uint64_t offs;  ///< offset in file to asf packet header
282   uint8_t frag; ///< fragment number (counting only video fragments)
283 };
284 
285 typedef struct {
286   index_entry *idx;  ///< linked list of index (keyframes)
287 
288   int nclients;
289   lives_clip_data_t **clients;
290   pthread_mutex_t mutex;
291 } index_container_t;
292 
293 typedef struct {
294   int fd; ///< file handle 208 477373 22415 108fat32
295   int64_t input_position; /// current or next input postion
296   int64_t data_start; ///< offset of data start in file
297   int64_t hdr_start;  ///< file offset of current asf packet
298   int64_t start_dts;  ///< first video dts
299   int64_t frame_dts;
300   boolean have_start_dts;
301   boolean black_fill;
302   boolean inited;
303   size_t def_packet_size;
304   off_t filesize;
305   ASFContext *asf;
306   AVFormatContext *s;
307   AVCodecContext *ctx;
308   AVStream *st;
309   ASFStream *asf_st;
310   AVFrame *picture;
311   AVPacket avpkt;
312   int64_t last_frame; ///< last frame decoded
313   index_container_t *idxc;
314   index_entry *kframe; ///< current keyframe
315   int fragnum; ///< current fragment number
316 } lives_asf_priv_t;
317 
318 #ifndef GET_UTF16
319 #define GET_UTF16(val, GET_16BIT, ERROR)	\
320   val = GET_16BIT;				\
321   {						\
322     unsigned int hi = val - 0xD800;		\
323     if (hi < 0x800) {				\
324       val = GET_16BIT - 0xDC00;			\
325       if (val > 0x3FFU || hi > 0x3FFU)		\
326 	ERROR					\
327 	  val += (hi << 10) + 0x10000;		\
328     }						\
329   }
330 #endif
331 
332 #ifndef PUT_UTF8
333 #define PUT_UTF8(val, tmp, PUT_BYTE)			\
334   {							\
335     int bytes, shift;					\
336     uint32_t in = val;					\
337     if (in < 0x80) {					\
338       tmp = in;						\
339       PUT_BYTE						\
340         } else {					\
341       bytes = (av_log2(in) + 4) / 5;			\
342       shift = (bytes - 1) * 6;				\
343       tmp = (256 - (256 >> bytes)) | (in >> shift);	\
344       PUT_BYTE						\
345 	while (shift >= 6) {				\
346 	  shift -= 6;					\
347 	  tmp = 0x80 | ((in >> shift) & 0x3f);		\
348 	  PUT_BYTE					\
349             }						\
350     }							\
351   }
352 #endif
353 
354 #ifndef DO_2BITS
355 #define DO_2BITS(bits, var, defval)					\
356   switch (bits & 3)							\
357     {									\
358       int dummy = 0;							\
359     case 3: dummy = read(priv->fd, buffer, 4); var = get_le32int(buffer); priv->input_position += 4; rsize += 4; break; \
360     case 2: dummy = read(priv->fd, buffer, 2); var = get_le16int(buffer); priv->input_position += 2; rsize += 2; break; \
361     case 1: dummy = read(priv->fd, buffer, 1); var = *buffer; priv->input_position++; rsize++; break; \
362     default: var = defval; dummy = dummy; break;				\
363     }
364 #endif
365 
366 index_entry *index_upto(const lives_clip_data_t *, int pts);
367 
368