/src/vlc/modules/packetizer/mjpeg.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * mjpeg.c: MPEG packetizer |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2018 VideoLabs, VLC authors and VideoLAN |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation; either version 2.1 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program 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 Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
19 | | *****************************************************************************/ |
20 | | |
21 | | /***************************************************************************** |
22 | | * Preamble |
23 | | *****************************************************************************/ |
24 | | |
25 | | #ifdef HAVE_CONFIG_H |
26 | | # include "config.h" |
27 | | #endif |
28 | | |
29 | | #include <vlc_common.h> |
30 | | #include <vlc_plugin.h> |
31 | | #include <vlc_block.h> |
32 | | #include <vlc_codec.h> |
33 | | #include <vlc_block_helper.h> |
34 | | #include "packetizer_helper.h" |
35 | | |
36 | | /***************************************************************************** |
37 | | * Local prototypes |
38 | | *****************************************************************************/ |
39 | | typedef struct |
40 | | { |
41 | | packetizer_t packetizer; |
42 | | int i_next_block_flags; |
43 | | date_t date; |
44 | | } decoder_sys_t; |
45 | | |
46 | | static const uint8_t p_mjpg_startcode[4] = { 0xFF, 0xD8, 0xFF, 0xE0 }; |
47 | | |
48 | | static inline const uint8_t * startcode_Find( const uint8_t *p, const uint8_t *end ) |
49 | 41.3k | { |
50 | 156k | while( p + 3 < end ) |
51 | 137k | { |
52 | 137k | p = memchr( p, 0xFF, end - p ); |
53 | 137k | if( !p || end - p < 4 ) |
54 | 22.1k | break; |
55 | 115k | if( p[1] == 0xD8 && p[2] == 0xFF && (p[3] & 0xE0) == 0xE0 ) |
56 | 271 | return p; |
57 | 115k | p++; |
58 | 115k | } |
59 | 41.1k | return NULL; |
60 | 41.3k | } |
61 | | |
62 | | /***************************************************************************** |
63 | | * Packetize: |
64 | | *****************************************************************************/ |
65 | | static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) |
66 | 2.23k | { |
67 | 2.23k | decoder_sys_t *p_sys = p_dec->p_sys; |
68 | 2.23k | return packetizer_Packetize( &p_sys->packetizer, pp_block ); |
69 | 2.23k | } |
70 | | |
71 | | static void PacketizeFlush( decoder_t *p_dec ) |
72 | 0 | { |
73 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
74 | |
|
75 | 0 | date_Set( &p_sys->date, VLC_TICK_INVALID ); |
76 | 0 | p_sys->i_next_block_flags = BLOCK_FLAG_DISCONTINUITY; |
77 | 0 | packetizer_Flush( &p_sys->packetizer ); |
78 | 0 | } |
79 | | |
80 | | /***************************************************************************** |
81 | | * Helpers: |
82 | | *****************************************************************************/ |
83 | | static void PacketizeReset( void *p_private, bool b_flush ) |
84 | 0 | { |
85 | 0 | VLC_UNUSED(b_flush); |
86 | 0 | decoder_t *p_dec = p_private; |
87 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
88 | |
|
89 | 0 | date_Set( &p_sys->date, VLC_TICK_INVALID ); |
90 | 0 | p_sys->i_next_block_flags = BLOCK_FLAG_DISCONTINUITY; |
91 | 0 | } |
92 | | |
93 | | static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block ) |
94 | 124 | { |
95 | 124 | decoder_t *p_dec = p_private; |
96 | 124 | decoder_sys_t *p_sys = p_dec->p_sys; |
97 | | |
98 | 124 | const uint8_t *p_buf = &p_block->p_buffer[2]; |
99 | 124 | size_t i_buf = p_block->i_buffer - 2; |
100 | | |
101 | 129 | while( i_buf > 4 && p_buf[0] == 0xFF ) |
102 | 124 | { |
103 | 124 | size_t i_size = 2 + GetWBE( &p_buf[2] ); |
104 | 124 | if( i_size > i_buf ) |
105 | 119 | break; |
106 | 5 | if( p_buf[1] == 0xC0 && i_buf > 9 ) |
107 | 0 | { |
108 | 0 | uint16_t i_height = GetWBE( &p_buf[5] ); |
109 | 0 | uint16_t i_width = GetWBE( &p_buf[7] ); |
110 | 0 | if( i_height && i_width && |
111 | 0 | (p_dec->fmt_out.video.i_height != i_height || |
112 | 0 | p_dec->fmt_out.video.i_width != i_width) ) |
113 | 0 | { |
114 | 0 | p_dec->fmt_out.video.i_width = |
115 | 0 | p_dec->fmt_out.video.i_visible_width = i_width; |
116 | 0 | p_dec->fmt_out.video.i_height = |
117 | 0 | p_dec->fmt_out.video.i_visible_height = i_height; |
118 | 0 | } |
119 | 0 | break; |
120 | 0 | } |
121 | 5 | i_buf -= i_size; |
122 | 5 | p_buf += i_size; |
123 | 5 | } |
124 | | |
125 | 124 | if( p_block->i_dts == VLC_TICK_INVALID ) |
126 | 49 | p_block->i_dts = p_block->i_pts; |
127 | 75 | else if( p_block->i_pts == VLC_TICK_INVALID ) |
128 | 2 | p_block->i_pts = p_block->i_dts; |
129 | | |
130 | 124 | vlc_tick_t i_prev_dts = date_Get( &p_sys->date ); |
131 | 124 | if( p_block->i_dts != VLC_TICK_INVALID ) |
132 | 75 | { |
133 | 75 | date_Set( &p_sys->date, p_block->i_dts ); |
134 | 75 | } |
135 | 49 | else if( p_dec->fmt_in->video.i_frame_rate && |
136 | 0 | p_dec->fmt_in->video.i_frame_rate_base ) |
137 | 0 | { |
138 | 0 | date_Increment( &p_sys->date, 1 ); |
139 | 0 | p_block->i_dts = p_block->i_pts = date_Get( &p_sys->date ); |
140 | 0 | } |
141 | | |
142 | 124 | if( i_prev_dts != VLC_TICK_INVALID && p_block->i_dts != VLC_TICK_INVALID ) |
143 | 71 | p_block->i_length = p_block->i_dts - i_prev_dts; |
144 | | |
145 | 124 | *pb_ts_used = true; |
146 | | |
147 | 124 | p_block->i_flags = p_sys->i_next_block_flags | BLOCK_FLAG_TYPE_I; |
148 | 124 | p_sys->i_next_block_flags = 0; |
149 | | |
150 | 124 | return p_block; |
151 | 124 | } |
152 | | |
153 | | static int PacketizeValidate( void *p_private, block_t *p_au ) |
154 | 124 | { |
155 | 124 | VLC_UNUSED(p_private); |
156 | 124 | VLC_UNUSED(p_au); |
157 | 124 | return VLC_SUCCESS; |
158 | 124 | } |
159 | | |
160 | | /***************************************************************************** |
161 | | * Close: |
162 | | *****************************************************************************/ |
163 | | static void Close( vlc_object_t *p_this ) |
164 | 7 | { |
165 | 7 | decoder_t *p_dec = (decoder_t*)p_this; |
166 | 7 | decoder_sys_t *p_sys = p_dec->p_sys; |
167 | | |
168 | 7 | packetizer_Clean( &p_sys->packetizer ); |
169 | | |
170 | 7 | free( p_sys ); |
171 | 7 | } |
172 | | |
173 | | /***************************************************************************** |
174 | | * Open: |
175 | | *****************************************************************************/ |
176 | | static int Open( vlc_object_t *p_this ) |
177 | | { |
178 | | decoder_t *p_dec = (decoder_t*)p_this; |
179 | | decoder_sys_t *p_sys; |
180 | | |
181 | | if( p_dec->fmt_in->i_codec != VLC_CODEC_MJPG ) |
182 | | return VLC_EGENERIC; |
183 | | |
184 | | p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) ); |
185 | | if( !p_dec->p_sys ) |
186 | | return VLC_ENOMEM; |
187 | | |
188 | | p_sys->i_next_block_flags = 0; |
189 | | |
190 | | if( p_dec->fmt_in->video.i_frame_rate && |
191 | | p_dec->fmt_in->video.i_frame_rate_base ) |
192 | | { |
193 | | date_Init( &p_sys->date, p_dec->fmt_in->video.i_frame_rate, |
194 | | p_dec->fmt_in->video.i_frame_rate_base ); |
195 | | } |
196 | | else |
197 | | date_Init( &p_sys->date, 30000, 1001 ); |
198 | | |
199 | | es_format_Copy( &p_dec->fmt_out, p_dec->fmt_in ); |
200 | | |
201 | | /* Misc init */ |
202 | | packetizer_Init( &p_sys->packetizer, |
203 | | p_mjpg_startcode, sizeof(p_mjpg_startcode), startcode_Find, |
204 | | NULL, 0, 295, |
205 | | PacketizeReset, PacketizeParse, PacketizeValidate, NULL, |
206 | | p_dec ); |
207 | | |
208 | | p_dec->pf_packetize = Packetize; |
209 | | p_dec->pf_flush = PacketizeFlush; |
210 | | p_dec->pf_get_cc = NULL; |
211 | | |
212 | | return VLC_SUCCESS; |
213 | | } |
214 | | |
215 | | /***************************************************************************** |
216 | | * Module descriptor |
217 | | *****************************************************************************/ |
218 | | |
219 | 170 | vlc_module_begin () |
220 | 85 | set_subcategory( SUBCAT_SOUT_PACKETIZER ) |
221 | 85 | set_description( N_("MJPEG video packetizer") ) |
222 | 85 | set_capability( "video packetizer", 50 ) |
223 | 170 | set_callbacks( Open, Close ) |
224 | 85 | vlc_module_end () |