]> git.sesse.net Git - vlc/blob - modules/demux/mxpeg_helper.h
decoder: do not wait for buffering when there is no data
[vlc] / modules / demux / mxpeg_helper.h
1 /*****************************************************************************
2  * mxpeg_helper.h: MXPEG helper functions
3  *****************************************************************************
4  * Copyright (C) 2012 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Sébastien Escudier
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * Finds FF XX in the first size byte of data
26  */
27 static uint8_t find_jpeg_marker(int *position, const uint8_t *data, int size)
28 {
29     for (int i = *position; i + 1 < size; i++) {
30         if (data[i] != 0xff)
31             continue;
32         if (data[i + 1] != 0xff) {
33             *position = i + 2;
34             return data[i + 1];
35         }
36     }
37     return 0xff;
38 }
39
40 /*
41  * Mxpeg frame format : http://developer.mobotix.com/docs/mxpeg_frame.html
42  * */
43 static bool IsMxpeg(stream_t *s)
44 {
45     const uint8_t *header;
46     int size = stream_Peek(s, &header, 256);
47     int position = 0;
48
49     if (find_jpeg_marker(&position, header, size) != 0xd8 || position > size-2)
50         return false;
51     if (find_jpeg_marker(&position, header, position + 2) != 0xe0)
52         return false;
53
54     if (position + 2 > size)
55         return false;
56
57     /* Skip this jpeg header */
58     uint32_t header_size = GetWBE(&header[position]);
59     position += header_size;
60
61     /* Get enough data to analyse the next header */
62     if (position + 6 > size)
63     {
64         size = position + 6;
65         if( stream_Peek (s, &header, size) < size )
66             return false;
67     }
68
69     if ( !(header[position] == 0xFF && header[position+1] == 0xFE) )
70         return false;
71     position += 2;
72     header_size = GetWBE (&header[position]);
73
74     /* Check if this is a MXF header. We may have a jpeg comment first */
75     if (!memcmp (&header[position+2], "MXF\0", 4) )
76         return true;
77
78     /* Skip the jpeg comment and find the MXF header after that */
79     size = position + header_size + 8; //8 = FF FE 00 00 M X F 00
80     if (stream_Peek(s, &header, size ) < size)
81         return false;
82
83     position += header_size;
84     if ( !(header[position] == 0xFF && header[position+1] == 0xFE) )
85         return false;
86
87     position += 4;
88
89     if (memcmp (&header[position], "MXF\0", 4) )
90         return false;
91
92     return true;
93 }