| 1 |
/* Copyright (C) 2008, 2009 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#define KATE_INTERNAL |
| 11 |
#include "kate_internal.h" |
| 12 |
|
| 13 |
#ifdef HAVE_STDLIB_H |
| 14 |
#include <stdlib.h> |
| 15 |
#endif |
| 16 |
#include "kate/kate.h" |
| 17 |
#include "kate_decode_state.h" |
| 18 |
|
| 19 |
kate_decode_state *kate_decode_state_create() |
| 20 |
{ |
| 21 |
kate_decode_state *kds; |
| 22 |
|
| 23 |
kds=(kate_decode_state*)kate_malloc(sizeof(kate_decode_state)); |
| 24 |
if (!kds) return NULL; |
| 25 |
kds->ki=NULL; |
| 26 |
kds->kc=NULL; |
| 27 |
kds->event=NULL; |
| 28 |
kds->n_active_events=0; |
| 29 |
kds->active_events=NULL; |
| 30 |
|
| 31 |
return kds; |
| 32 |
} |
| 33 |
|
| 34 |
int kate_decode_state_clear(kate_decode_state *kds,const kate_info *ki,int new) |
| 35 |
{ |
| 36 |
if (!kds || !ki) return KATE_E_INVALID_PARAMETER; |
| 37 |
|
| 38 |
if (kds->event) { |
| 39 |
kate_event_release(kds->event); |
| 40 |
kds->event=NULL; |
| 41 |
} |
| 42 |
|
| 43 |
if (new) { |
| 44 |
kds->event=kate_event_create(ki); |
| 45 |
if (!kds->event) return KATE_E_OUT_OF_MEMORY; |
| 46 |
kate_event_track(kds->event); |
| 47 |
} |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
int kate_decode_state_flush_events(kate_decode_state *kds,kate_int64_t granule) |
| 53 |
{ |
| 54 |
size_t n; |
| 55 |
|
| 56 |
if (!kds) return KATE_E_INVALID_PARAMETER; |
| 57 |
|
| 58 |
/* -1 will flush everything */ |
| 59 |
for (n=0;n<kds->n_active_events;++n) { |
| 60 |
if (granule<kds->active_events[n].start || granule>kds->active_events[n].end) { |
| 61 |
kds->active_events[n--]=kds->active_events[--kds->n_active_events]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
int kate_decode_state_find_event(kate_decode_state *kds,kate_int32_t id) |
| 69 |
{ |
| 70 |
size_t n; |
| 71 |
|
| 72 |
if (!kds) return KATE_E_INVALID_PARAMETER; |
| 73 |
if (id<0) return KATE_E_INVALID_PARAMETER; |
| 74 |
|
| 75 |
for (n=0;n<kds->n_active_events;++n) { |
| 76 |
if (kds->active_events[n].id==id) return 0; |
| 77 |
} |
| 78 |
|
| 79 |
return KATE_E_NOT_FOUND; |
| 80 |
} |
| 81 |
|
| 82 |
int kate_decode_state_add_event(kate_decode_state *kds,const kate_event *ev) |
| 83 |
{ |
| 84 |
size_t n; |
| 85 |
kate_active_event *events; |
| 86 |
int ret; |
| 87 |
|
| 88 |
if (!kds) return KATE_E_INVALID_PARAMETER; |
| 89 |
if (!ev) return KATE_E_INVALID_PARAMETER; |
| 90 |
|
| 91 |
ret=kate_check_add_overflow(kds->n_active_events,1,NULL); |
| 92 |
if (ret<0) return ret; |
| 93 |
|
| 94 |
/* check if it is there already */ |
| 95 |
for (n=0;n<kds->n_active_events;++n) { |
| 96 |
if (kds->active_events[n].id==ev->id) { |
| 97 |
return 1; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/* if it is not there, add it */ |
| 102 |
events=(kate_active_event*)kate_checked_realloc(kds->active_events,(kds->n_active_events+1),sizeof(kate_active_event)); |
| 103 |
if (!events) return KATE_E_OUT_OF_MEMORY; |
| 104 |
|
| 105 |
kds->active_events=events; |
| 106 |
kds->active_events[kds->n_active_events].id=ev->id; |
| 107 |
kds->active_events[kds->n_active_events].start=ev->start; |
| 108 |
kds->active_events[kds->n_active_events].end=ev->start+ev->duration-1; |
| 109 |
++kds->n_active_events; |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int kate_decode_state_destroy(kate_decode_state *kds) |
| 115 |
{ |
| 116 |
if (!kds) return KATE_E_INVALID_PARAMETER; |
| 117 |
|
| 118 |
kate_event_release(kds->event); |
| 119 |
|
| 120 |
if (kds->ki) kate_info_clear(kds->ki); |
| 121 |
if (kds->kc) kate_comment_clear(kds->kc); |
| 122 |
|
| 123 |
kate_free(kds->active_events); |
| 124 |
|
| 125 |
kate_free(kds); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |