1 /*****************************************************************************
2 * oggspots.c: OggSpots decoder module.
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
6 * Authors: Michael Taenzer <neo@nhng.de>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
33 #include <vlc_image.h>
38 /*****************************************************************************
39 * decoder_sys_t : oggspots decoder descriptor
40 *****************************************************************************/
54 image_handler_t
* p_image
;
62 /*****************************************************************************
64 *****************************************************************************/
65 static int OpenDecoder (vlc_object_t
*);
66 static int OpenPacketizer(vlc_object_t
*);
67 static void CloseDecoder (vlc_object_t
*);
69 static int DecodeVideo (decoder_t
*, block_t
*);
70 static block_t
* Packetize (decoder_t
*, block_t
**);
71 static int ProcessHeader(decoder_t
*);
72 static void* ProcessPacket(decoder_t
*, block_t
*);
73 static void Flush (decoder_t
*);
74 static picture_t
* DecodePacket (decoder_t
*, block_t
*);
77 /*****************************************************************************
79 *****************************************************************************/
82 set_category(CAT_INPUT
)
83 set_subcategory(SUBCAT_INPUT_VCODEC
)
84 set_shortname("OggSpots")
85 set_description(N_("OggSpots video decoder"))
86 set_capability("video decoder", 10)
87 set_callbacks(OpenDecoder
, CloseDecoder
)
88 add_shortcut("oggspots")
91 set_description(N_("OggSpots video packetizer"))
92 set_capability("packetizer", 10)
93 set_callbacks(OpenPacketizer
, CloseDecoder
)
94 add_shortcut("oggspots")
97 static int OpenCommon(vlc_object_t
* p_this
, bool b_packetizer
)
99 decoder_t
* p_dec
= (decoder_t
*)p_this
;
100 decoder_sys_t
* p_sys
;
102 if (p_dec
->fmt_in
.i_codec
!= VLC_CODEC_OGGSPOTS
) {
106 /* Allocate the memory needed to store the decoder's structure */
107 p_sys
= malloc(sizeof(*p_sys
));
111 p_dec
->p_sys
= p_sys
;
112 p_sys
->b_packetizer
= b_packetizer
;
113 p_sys
->b_has_headers
= false;
114 p_sys
->i_pts
= VLC_TICK_INVALID
;
116 /* Initialize image handler */
117 p_sys
->p_image
= image_HandlerCreate(p_dec
);
118 if (p_sys
->p_image
== NULL
) {
125 p_dec
->fmt_out
.i_codec
= VLC_CODEC_OGGSPOTS
;
126 p_dec
->pf_packetize
= Packetize
;
130 p_dec
->fmt_out
.i_codec
= VLC_CODEC_RGBA
;
131 p_dec
->pf_decode
= DecodeVideo
;
134 p_dec
->pf_flush
= Flush
;
139 /*****************************************************************************
140 * OpenDecoder: probe the decoder and return score
141 *****************************************************************************/
142 static int OpenDecoder(vlc_object_t
* p_this
)
144 return OpenCommon(p_this
, false);
147 static int OpenPacketizer(vlc_object_t
* p_this
)
149 return OpenCommon(p_this
, true);
152 /****************************************************************************
153 * DecodeBlock: the whole thing
154 ****************************************************************************
155 * This function must be fed with ogg packets.
156 ****************************************************************************/
157 static void* DecodeBlock(decoder_t
* p_dec
, block_t
* p_block
)
159 decoder_sys_t
* p_sys
= p_dec
->p_sys
;
161 /* Check for headers */
162 if (!p_sys
->b_has_headers
) {
163 if (ProcessHeader(p_dec
)) {
164 block_Release(p_block
);
167 p_sys
->b_has_headers
= true;
170 return ProcessPacket(p_dec
, p_block
);
173 static int DecodeVideo( decoder_t
*p_dec
, block_t
*p_block
)
175 if( p_block
== NULL
) /* No Drain */
176 return VLCDEC_SUCCESS
;
178 picture_t
*p_pic
= DecodeBlock( p_dec
, p_block
);
180 decoder_QueueVideo( p_dec
, p_pic
);
181 return VLCDEC_SUCCESS
;
184 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
186 if( pp_block
== NULL
) /* No Drain */
188 block_t
*p_block
= *pp_block
; *pp_block
= NULL
;
189 if( p_block
== NULL
)
191 return DecodeBlock( p_dec
, p_block
);
194 /*****************************************************************************
195 * ProcessHeader: process OggSpots header.
196 *****************************************************************************/
197 static int ProcessHeader(decoder_t
* p_dec
)
199 decoder_sys_t
* p_sys
= p_dec
->p_sys
;
200 const uint8_t* p_extra
;
203 uint64_t i_granulerate_numerator
;
204 uint64_t i_granulerate_denominator
;
206 /* The OggSpots header is always 52 bytes */
207 if (p_dec
->fmt_in
.i_extra
!= 52) {
210 p_extra
= p_dec
->fmt_in
.p_extra
;
212 /* Identification string */
213 if ( memcmp(p_extra
, "SPOTS\0\0", 8) ) {
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) {
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) {
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;
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);
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';
251 msg_Warn(p_dec
, "Unsupported image format: %s", psz_image_type
);
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]);
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;
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. */
269 if (p_sys
->b_packetizer
) {
270 void* p_new_extra
= realloc(p_dec
->fmt_out
.p_extra
,
271 p_dec
->fmt_in
.i_extra
);
272 if (unlikely(p_new_extra
== NULL
)) {
275 p_dec
->fmt_out
.p_extra
= p_new_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
);
284 /*****************************************************************************
286 *****************************************************************************/
287 static void Flush(decoder_t
* p_dec
)
289 decoder_sys_t
* p_sys
= p_dec
->p_sys
;
291 p_sys
->i_pts
= VLC_TICK_INVALID
;
294 /*****************************************************************************
295 * ProcessPacket: processes an OggSpots packet.
296 *****************************************************************************/
297 static void* ProcessPacket(decoder_t
* p_dec
, block_t
* p_block
)
299 decoder_sys_t
* p_sys
= p_dec
->p_sys
;
302 if ( (p_block
->i_flags
& BLOCK_FLAG_DISCONTINUITY
) != 0 ) {
303 p_sys
->i_pts
= p_block
->i_pts
;
306 if ( (p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
) != 0 ) {
307 block_Release(p_block
);
311 /* Date management */
312 if (p_block
->i_pts
!= VLC_TICK_INVALID
&& p_block
->i_pts
!= p_sys
->i_pts
) {
313 p_sys
->i_pts
= p_block
->i_pts
;
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
;
322 p_block
->i_length
= p_sys
->i_pts
- p_block
->i_pts
;
327 p_buf
= DecodePacket(p_dec
, p_block
);
333 /*****************************************************************************
334 * DecodePacket: decodes an OggSpots packet.
335 *****************************************************************************/
336 static picture_t
* DecodePacket(decoder_t
* p_dec
, block_t
* p_block
)
338 decoder_sys_t
* p_sys
= p_dec
->p_sys
;
339 uint32_t i_img_offset
;
342 if (p_block
->i_buffer
< 20) {
343 msg_Dbg(p_dec
, "Packet too short");
348 i_img_offset
= GetDWLE(p_block
->p_buffer
);
349 if (i_img_offset
< 20) {
350 msg_Dbg(p_dec
, "Invalid byte offset");
355 if ( !memcmp(&p_block
->p_buffer
[4], "PNG", 3) ) {
356 p_dec
->fmt_in
.video
.i_chroma
= VLC_CODEC_PNG
;
358 else if ( !memcmp(&p_block
->p_buffer
[4], "JPEG", 4) ) {
359 p_dec
->fmt_in
.video
.i_chroma
= VLC_CODEC_JPEG
;
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';
366 msg_Dbg(p_dec
, "Unsupported image format: %s", psz_image_type
);
370 /* We currently ignore the rest of the header and let the image format
371 * handle the details */
373 p_block
->i_buffer
-= i_img_offset
;
374 p_block
->p_buffer
+= i_img_offset
;
376 p_pic
= image_Read(p_sys
->p_image
, p_block
,
378 &p_dec
->fmt_out
.video
);
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
);
390 block_Release(p_block
);
394 /*****************************************************************************
395 * CloseDecoder: OggSpots decoder destruction
396 *****************************************************************************/
397 static void CloseDecoder(vlc_object_t
* p_this
)
399 decoder_t
* p_dec
= (decoder_t
*)p_this
;
400 decoder_sys_t
* p_sys
= p_dec
->p_sys
;
402 image_HandlerDelete(p_sys
->p_image
);