packetizer: hevc: add poc debug
[vlc.git] / modules / codec / oggspots.c
blob4683b4c9b9c0e63f2ecf2b120c6ffe12935c52e8
1 /*****************************************************************************
2 * oggspots.c: OggSpots decoder module.
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Michael Taenzer <neo@nhng.de>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34 #include <vlc_image.h>
36 #include <assert.h>
37 #include <limits.h>
39 /*****************************************************************************
40 * decoder_sys_t : oggspots decoder descriptor
41 *****************************************************************************/
42 struct decoder_sys_t
44 /* Module mode */
45 bool b_packetizer;
48 * Input properties
50 bool b_has_headers;
53 * Image handler
55 image_handler_t* p_image;
58 * Common properties
60 mtime_t i_pts;
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*);
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*);
78 /*****************************************************************************
79 * Module descriptor
80 *****************************************************************************/
82 vlc_module_begin ()
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")
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 ()
98 /*****************************************************************************
99 * OpenDecoder: probe the decoder and return score
100 *****************************************************************************/
101 static int OpenDecoder(vlc_object_t* p_this)
103 decoder_t* p_dec = (decoder_t*)p_this;
104 decoder_sys_t* p_sys;
106 if (p_dec->fmt_in.i_codec != VLC_CODEC_OGGSPOTS) {
107 return VLC_EGENERIC;
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;
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;
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;
127 /* Set output properties */
128 p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
130 /* Set callbacks */
131 p_dec->pf_decode = DecodeVideo;
132 p_dec->pf_packetize = Packetize;
133 p_dec->pf_flush = Flush;
135 return VLC_SUCCESS;
138 static int OpenPacketizer(vlc_object_t* p_this)
140 decoder_t* p_dec = (decoder_t*)p_this;
142 int i_ret = OpenDecoder(p_this);
144 if (i_ret == VLC_SUCCESS) {
145 p_dec->p_sys->b_packetizer = true;
146 p_dec->fmt_out.i_codec = VLC_CODEC_OGGSPOTS;
149 return i_ret;
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);
165 return NULL;
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 );
179 if( p_pic != NULL )
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 */
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 );
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;
201 int i_major;
202 int i_minor;
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) {
208 return VLC_EGENERIC;
210 p_extra = p_dec->fmt_in.p_extra;
212 /* Identification string */
213 if ( memcmp(p_extra, "SPOTS\0\0", 8) ) {
214 return VLC_EGENERIC;
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;
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;
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);
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';
251 msg_Warn(p_dec, "Unsupported image format: %s", psz_image_type);
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]);
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_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;
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);
281 return VLC_SUCCESS;
284 /*****************************************************************************
285 * Flush:
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_TS_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;
300 void* p_buf;
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);
308 return NULL;
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;
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;
324 p_buf = p_block;
326 else {
327 p_buf = DecodePacket(p_dec, p_block);
330 return p_buf;
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;
340 picture_t* p_pic;
342 if (p_block->i_buffer < 20) {
343 msg_Dbg(p_dec, "Packet too short");
344 goto error;
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;
354 /* Image format */
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;
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';
366 msg_Dbg(p_dec, "Unsupported image format: %s", psz_image_type);
367 goto error;
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,
377 &p_dec->fmt_in.video,
378 &p_dec->fmt_out.video);
379 if (p_pic == NULL) {
380 return NULL;
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);
387 return p_pic;
389 error:
390 block_Release(p_block);
391 return NULL;
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);
403 free(p_sys);