forked from gfto/mptsd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pidref.c
102 lines (94 loc) · 2.51 KB
/
pidref.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* pidref implementation
* Copyright (C) 2010-2011 Unix Solutions Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*/
#include <stdlib.h>
#include "libfuncs/log.h"
#include "libtsfuncs/tsfuncs.h"
#include "pidref.h"
PIDREF *pidref_init(int num, uint16_t base_pid) {
PIDREF *ref = calloc(1, sizeof(PIDREF));
ref->num = num;
ref->base_pid = base_pid;
ref->entries = calloc(ref->num, sizeof(PIDREF_ENTRY));
return ref;
}
void pidref_free(PIDREF **pref) {
PIDREF *ref = *pref;
if (!ref)
return;
FREE(ref->entries);
FREE(*pref);
}
int pidref_add(PIDREF *ref, uint16_t org_pid, uint16_t new_pid) {
int i;
if (!org_pid)
return 0;
for (i=0;i<ref->num;i++) {
PIDREF_ENTRY *entry = &ref->entries[i];
if (!entry->org_pid) {
entry->org_pid = org_pid;
entry->new_pid = new_pid;// KDKD1
return 1;
}
}
return 0;
}
int pidref_del(PIDREF *ref, uint16_t org_pid) {
int i;
if (!org_pid)
return 0;
for (i=0;i<ref->num;i++) {
PIDREF_ENTRY *entry = &ref->entries[i];
if (entry->org_pid == org_pid) {
entry->org_pid = 0;
entry->new_pid = 0;
return 1;
}
}
return 0;
}
uint16_t pidref_get_new_pid(PIDREF *ref, uint16_t org_pid) {
int i;
if (!org_pid)
return 0;
for (i=0;i<ref->num;i++) {
PIDREF_ENTRY *entry = &ref->entries[i];
if (entry->org_pid == org_pid) {
return entry->new_pid;
}
}
return 0;
}
int pidref_change_packet_pid(uint8_t *ts_packet, uint16_t packet_pid, PIDREF *ref) {
uint16_t new_pid = pidref_get_new_pid(ref, packet_pid);
if (new_pid) {
ts_packet_set_pid(ts_packet, new_pid);
return new_pid;
}
return 0;
}
void pidref_dump(PIDREF *ref) {
int i;
LOGf("pidref->base_pid = %04d\n", ref->base_pid);
LOGf("pidref->num = %d\n" , ref->num);
LOG ("pidref->entries org_pid new_pid\n");
for (i=0;i<ref->num;i++) {
PIDREF_ENTRY *entry = &ref->entries[i];
if (entry->org_pid)
LOGf("pidref->entry[%02d] = %04d %04d\n", i, entry->org_pid, entry->new_pid);
}
}