1 /*****************************************************************************
2  * oggspots.c: OggSpots decoder module.
3  *****************************************************************************
4  * Copyright (C) 2016 VLC authors and VideoLAN
5  * $Id: 4683b4c9b9c0e63f2ecf2b120c6ffe12935c52e8 $
6  *
7  * Authors: Michael Taenzer <neo@nhng.de>
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  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34 #include <vlc_image.h>
35 
36 #include <assert.h>
37 #include <limits.h>
38 
39 /*****************************************************************************
40  * decoder_sys_t : oggspots decoder descriptor
41  *****************************************************************************/
42 struct decoder_sys_t
43 {
44     /* Module mode */
45     bool b_packetizer;
46 
47     /*
48      * Input properties
49      */
50     bool b_has_headers;
51 
52     /*
53      * Image handler
54      */
55     image_handler_t* p_image;
56 
57     /*
58      * Common properties
59      */
60     mtime_t i_pts;
61 };
62 
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int  OpenDecoder   (vlc_object_t*);
67 static int  OpenPacketizer(vlc_object_t*);
68 static void CloseDecoder  (vlc_object_t*);
69 
70 static int        DecodeVideo  (decoder_t*, block_t*);
71 static block_t*   Packetize  (decoder_t*, block_t**);
72 static int        ProcessHeader(decoder_t*);
73 static void*      ProcessPacket(decoder_t*, block_t*);
74 static void       Flush        (decoder_t*);
75 static picture_t* DecodePacket (decoder_t*, block_t*);
76 
77 
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 
82 vlc_module_begin ()
set_category(CAT_INPUT)83     set_category(CAT_INPUT)
84     set_subcategory(SUBCAT_INPUT_VCODEC)
85     set_shortname("OggSpots")
86     set_description(N_("OggSpots video decoder"))
87     set_capability("video decoder", 10)
88     set_callbacks(OpenDecoder, CloseDecoder)
89     add_shortcut("oggspots")
90 
91     add_submodule ()
92     set_description(N_("OggSpots video packetizer"))
93     set_capability("packetizer", 10)
94     set_callbacks(OpenPacketizer, CloseDecoder)
95     add_shortcut("oggspots")
96 vlc_module_end ()
97 
98 /*****************************************************************************
99  * OpenDecoder: probe the decoder and return score
100  *****************************************************************************/
101 static int OpenDecoder(vlc_object_t* p_this)
102 {
103     decoder_t* p_dec = (decoder_t*)p_this;
104     decoder_sys_t* p_sys;
105 
106     if (p_dec->fmt_in.i_codec != VLC_CODEC_OGGSPOTS) {
107         return VLC_EGENERIC;
108     }
109 
110     /* Allocate the memory needed to store the decoder's structure */
111     p_sys = malloc(sizeof(*p_sys));
112     if (p_sys == NULL) {
113         return VLC_ENOMEM;
114     }
115     p_dec->p_sys = p_sys;
116     p_sys->b_packetizer = false;
117     p_sys->b_has_headers = false;
118     p_sys->i_pts = VLC_TS_INVALID;
119 
120     /* Initialize image handler */
121     p_sys->p_image = image_HandlerCreate(p_dec);
122     if (p_sys->p_image == NULL) {
123         free(p_sys);
124         return VLC_ENOMEM;
125     }
126 
127     /* Set output properties */
128     p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
129 
130     /* Set callbacks */
131     p_dec->pf_decode    = DecodeVideo;
132     p_dec->pf_packetize = Packetize;
133     p_dec->pf_flush     = Flush;
134 
135     return VLC_SUCCESS;
136 }
137 
OpenPacketizer(vlc_object_t * p_this)138 static int OpenPacketizer(vlc_object_t* p_this)
139 {
140     decoder_t* p_dec = (decoder_t*)p_this;
141 
142     int i_ret = OpenDecoder(p_this);
143 
144     if (i_ret == VLC_SUCCESS) {
145         p_dec->p_sys->b_packetizer = true;
146         p_dec->fmt_out.i_codec = VLC_CODEC_OGGSPOTS;
147     }
148 
149     return i_ret;
150 }
151 
152 /****************************************************************************
153  * DecodeBlock: the whole thing
154  ****************************************************************************
155  * This function must be fed with ogg packets.
156  ****************************************************************************/
DecodeBlock(decoder_t * p_dec,block_t * p_block)157 static void* DecodeBlock(decoder_t* p_dec, block_t* p_block)
158 {
159     decoder_sys_t* p_sys = p_dec->p_sys;
160 
161     /* Check for headers */
162     if (!p_sys->b_has_headers) {
163         if (ProcessHeader(p_dec)) {
164             block_Release(p_block);
165             return NULL;
166         }
167         p_sys->b_has_headers = true;
168     }
169 
170     return ProcessPacket(p_dec, p_block);
171 }
172 
DecodeVideo(decoder_t * p_dec,block_t * p_block)173 static int DecodeVideo( decoder_t *p_dec, block_t *p_block )
174 {
175     if( p_block == NULL ) /* No Drain */
176         return VLCDEC_SUCCESS;
177 
178     picture_t *p_pic = DecodeBlock( p_dec, p_block );
179     if( p_pic != NULL )
180         decoder_QueueVideo( p_dec, p_pic );
181     return VLCDEC_SUCCESS;
182 }
183 
Packetize(decoder_t * p_dec,block_t ** pp_block)184 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
185 {
186     if( pp_block == NULL ) /* No Drain */
187         return NULL;
188     block_t *p_block = *pp_block; *pp_block = NULL;
189     if( p_block == NULL )
190         return NULL;
191     return DecodeBlock( p_dec, p_block );
192 }
193 
194 /*****************************************************************************
195  * ProcessHeader: process OggSpots header.
196  *****************************************************************************/
ProcessHeader(decoder_t * p_dec)197 static int ProcessHeader(decoder_t* p_dec)
198 {
199     decoder_sys_t* p_sys = p_dec->p_sys;
200     const uint8_t* p_extra;
201     int i_major;
202     int i_minor;
203     uint64_t i_granulerate_numerator;
204     uint64_t i_granulerate_denominator;
205 
206     /* The OggSpots header is always 52 bytes */
207     if (p_dec->fmt_in.i_extra != 52) {
208         return VLC_EGENERIC;
209     }
210     p_extra = p_dec->fmt_in.p_extra;
211 
212     /* Identification string */
213     if ( memcmp(p_extra, "SPOTS\0\0", 8) ) {
214         return VLC_EGENERIC;
215     }
216 
217     /* Version number */
218     i_major = GetWLE(&p_extra[ 8]); /* major version num */
219     i_minor = GetWLE(&p_extra[10]); /* minor version num */
220     if (i_major != 0 || i_minor != 1) {
221         return VLC_EGENERIC;
222     }
223 
224     /* Granule rate */
225     i_granulerate_numerator   = GetQWLE(&p_extra[12]);
226     i_granulerate_denominator = GetQWLE(&p_extra[20]);
227     if (i_granulerate_numerator == 0 || i_granulerate_denominator == 0) {
228         return VLC_EGENERIC;
229     }
230 
231     /* The OggSpots spec contained an error and there are implementations out
232      * there that used the wrong value. So we detect that case and switch
233      * numerator and denominator in that case */
234     if (i_granulerate_numerator == 1 && i_granulerate_denominator == 30) {
235         i_granulerate_numerator   = 30;
236         i_granulerate_denominator = 1;
237     }
238 
239     /* Normalize granulerate */
240     vlc_ureduce(&p_dec->fmt_in.video.i_frame_rate,
241                 &p_dec->fmt_in.video.i_frame_rate_base,
242                 i_granulerate_numerator, i_granulerate_denominator, 0);
243 
244     /* Image format */
245     if (!p_sys->b_packetizer) {
246         if ( memcmp(&p_extra[32], "PNG", 3) && memcmp(&p_extra[32], "JPEG", 4) ) {
247             char psz_image_type[8+1];
248             strncpy(psz_image_type, (char*)&p_extra[32], 8);
249             psz_image_type[sizeof(psz_image_type)-1] = '\0';
250 
251             msg_Warn(p_dec, "Unsupported image format: %s", psz_image_type);
252         }
253     }
254 
255     /* Dimensions */
256     p_dec->fmt_out.video.i_width  = p_dec->fmt_out.video.i_visible_width  =
257             GetWLE(&p_extra[40]);
258     p_dec->fmt_out.video.i_height = p_dec->fmt_out.video.i_visible_height =
259             GetWLE(&p_extra[42]);
260 
261     /* We assume square pixels */
262     p_dec->fmt_out.video.i_sar_num = 1;
263     p_dec->fmt_out.video.i_sar_den = 1;
264 
265     /* We don't implement background color, alignment and options at the
266      * moment because the former doesn't seem necessary right now and the
267      * latter are underspecified. */
268 
269     if (p_sys->b_packetizer) {
270         void* p_extra = realloc(p_dec->fmt_out.p_extra,
271                                 p_dec->fmt_in.i_extra);
272         if (unlikely(p_extra == NULL)) {
273             return VLC_ENOMEM;
274         }
275         p_dec->fmt_out.p_extra = p_extra;
276         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
277         memcpy(p_dec->fmt_out.p_extra,
278                p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra);
279     }
280 
281     return VLC_SUCCESS;
282 }
283 
284 /*****************************************************************************
285  * Flush:
286  *****************************************************************************/
Flush(decoder_t * p_dec)287 static void Flush(decoder_t* p_dec)
288 {
289     decoder_sys_t* p_sys = p_dec->p_sys;
290 
291     p_sys->i_pts = VLC_TS_INVALID;
292 }
293 
294 /*****************************************************************************
295  * ProcessPacket: processes an OggSpots packet.
296  *****************************************************************************/
ProcessPacket(decoder_t * p_dec,block_t * p_block)297 static void* ProcessPacket(decoder_t* p_dec, block_t* p_block)
298 {
299     decoder_sys_t* p_sys = p_dec->p_sys;
300     void* p_buf;
301 
302     if ( (p_block->i_flags & BLOCK_FLAG_DISCONTINUITY) != 0 ) {
303         p_sys->i_pts = p_block->i_pts;
304     }
305 
306     if ( (p_block->i_flags & BLOCK_FLAG_CORRUPTED) != 0 ) {
307         block_Release(p_block);
308         return NULL;
309     }
310 
311     /* Date management */
312     if (p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts) {
313         p_sys->i_pts = p_block->i_pts;
314     }
315 
316     if (p_sys->b_packetizer) {
317         /* Date management */
318         /* FIXME: This is copied from theora but it looks wrong.
319          * p_block->i_length will always be zero. */
320         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
321 
322         p_block->i_length = p_sys->i_pts - p_block->i_pts;
323 
324         p_buf = p_block;
325     }
326     else {
327         p_buf = DecodePacket(p_dec, p_block);
328     }
329 
330     return p_buf;
331 }
332 
333 /*****************************************************************************
334  * DecodePacket: decodes an OggSpots packet.
335  *****************************************************************************/
DecodePacket(decoder_t * p_dec,block_t * p_block)336 static picture_t* DecodePacket(decoder_t* p_dec, block_t* p_block)
337 {
338     decoder_sys_t* p_sys = p_dec->p_sys;
339     uint32_t i_img_offset;
340     picture_t* p_pic;
341 
342     if (p_block->i_buffer < 20) {
343         msg_Dbg(p_dec, "Packet too short");
344         goto error;
345     }
346 
347     /* Byte offset */
348     i_img_offset = GetDWLE(p_block->p_buffer);
349     if (i_img_offset < 20) {
350         msg_Dbg(p_dec, "Invalid byte offset");
351         goto error;
352     }
353 
354     /* Image format */
355     if ( !memcmp(&p_block->p_buffer[4], "PNG", 3) ) {
356         p_dec->fmt_in.video.i_chroma = VLC_CODEC_PNG;
357     }
358     else if ( !memcmp(&p_block->p_buffer[4], "JPEG", 4) ) {
359         p_dec->fmt_in.video.i_chroma = VLC_CODEC_JPEG;
360     }
361     else {
362         char psz_image_type[8+1];
363         strncpy(psz_image_type, (char*)&p_block->p_buffer[4], 8);
364         psz_image_type[sizeof(psz_image_type)-1] = '\0';
365 
366         msg_Dbg(p_dec, "Unsupported image format: %s", psz_image_type);
367         goto error;
368     }
369 
370     /* We currently ignore the rest of the header and let the image format
371      * handle the details */
372 
373     p_block->i_buffer -= i_img_offset;
374     p_block->p_buffer += i_img_offset;
375 
376     p_pic = image_Read(p_sys->p_image, p_block,
377                        &p_dec->fmt_in.video,
378                        &p_dec->fmt_out.video);
379     if (p_pic == NULL) {
380         return NULL;
381     }
382 
383     p_pic->b_force = true;
384     p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
385     decoder_UpdateVideoFormat(p_dec);
386 
387     return p_pic;
388 
389 error:
390     block_Release(p_block);
391     return NULL;
392 }
393 
394 /*****************************************************************************
395  * CloseDecoder: OggSpots decoder destruction
396  *****************************************************************************/
CloseDecoder(vlc_object_t * p_this)397 static void CloseDecoder(vlc_object_t* p_this)
398 {
399     decoder_t* p_dec = (decoder_t*)p_this;
400     decoder_sys_t* p_sys = p_dec->p_sys;
401 
402     image_HandlerDelete(p_sys->p_image);
403     free(p_sys);
404 }
405