File: decoder-h263v2.cc

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (225 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inet.h"
#include "rtp.h"
#include "decoder.h"
#include "renderer.h"

#include "libh263.h"

class H263plusDecoder : public Decoder {
public:
    H263plusDecoder();
    virtual ~H263plusDecoder();
    virtual void info(char* wrk) const;
    virtual void recv(const rtphdr* rh, const u_char* bp, int cc);
    int colorhist(u_int* hist) const;
    virtual void stats(char* wrk);
protected:
    void decode(const u_char* vh, const u_char* bp, int cc);
    virtual void redraw();

    /* for ucb h263 decoder */
    struct H263_STATE *decoder_state;

    /* packet statistics */
    u_int16_t  last_seq; /* sequence number */

    /* collecting data for a frame */
    char       bitstream[65536];   /* bitstream data */
    int        b_tr;               /* temporal reference */
    int        b_ts;               /* timestamp */
    int        b_off;              /* how much bitstream data we have */
    int        b_all;              /* got all packets for so far? */

    /* image */
    u_char     xxx_frame[160000];
};

static class H263plusDecoderMatcher : public Matcher {
    public:
	H263plusDecoderMatcher() : Matcher("decoder") {}
	TclObject* match(const char* id) {
		if (strcasecmp(id, "h263+") == 0)
			return (new H263plusDecoder());
		return (0);
	}
} dm_h263plus;

#define STAT_BAD_PSC	0
#define STAT_BAD_GOB	1
#define STAT_BAD_SYNTAX	2
#define STAT_BAD_FMT	3
#define STAT_FMT_CHANGE 4	/* # times fmt changed */
#define STAT_BAD_HEADER 5


H263plusDecoder::H263plusDecoder() : Decoder(2) /* , codec_(0), */ 
{
        /* no stats yet... */
#if 0
	stat_[STAT_BAD_PSC].name = "H261-Bad-PSC";
	stat_[STAT_BAD_GOB].name = "H261-Bad-GOB";
	stat_[STAT_BAD_SYNTAX].name = "H261-Bad-Syntax";
	stat_[STAT_BAD_FMT].name = "H261-Bad-fmt";
	stat_[STAT_FMT_CHANGE].name = "H261-Fmt-change";
	stat_[STAT_BAD_HEADER].name = "H261-Bad-Header";
	nstat_ = 6;
#endif

	decimation_ = 411;
	/*
	 * Assume CIF.  Picture header will trigger a resize if
	 * we encounter QCIF instead.
	 */
	inw_ = 0;
	inh_ = 0;

	/*XXX*/
	resize(inw_, inh_);

	decoder_state = h263_start();
}

H263plusDecoder::~H263plusDecoder()
{
        h263_stop(decoder_state);
}

void H263plusDecoder::info(char* wrk) const
{
	/* nop */
}

void H263plusDecoder::stats(char* wrk)
{
#if 0
	/* pull stats out of vic indepdendent P64Decoder */
	setstat(STAT_BAD_PSC, codec_->bad_psc());
	setstat(STAT_BAD_GOB, codec_->bad_GOBno());
	setstat(STAT_BAD_SYNTAX, codec_->bad_bits());
	setstat(STAT_BAD_FMT, codec_->bad_fmt());
#endif
	Decoder::stats(wrk);
}

int H263plusDecoder::colorhist(u_int* hist) const
{
#if 0
	const u_char* frm = codec_->frame();
	int w = inw_;
	int h = inh_;
	int s = w * h;
	colorhist_411_556(hist, frm, frm + s, frm + s + (s >> 2), w, h);
#endif
	return (1);
}

extern void
dump_paket(const unsigned char *data, int len);

void H263plusDecoder::recv(const rtphdr* rh, const u_char* bp, int cc)
{
    /* RTP header  */
    int mbit  = ntohs(rh->rh_flags) >> 7 & 1;
    int seq   = ntohs(rh->rh_seqno);
    int ts    = ntohl(rh->rh_ts);

    /* H.263 payload */
    u_char *pl = (u_char*)(rh + 1);
    /* 5 bits reserved */
    int pbit  = (pl[0] >> 2) & 1;          /* 1 bit */
    int vbit  = (pl[0] >> 1) & 1;          /* 1 bit */
    int plen  = ((pl[0] << 5) & 0x20) |    /* 6 bits */
	        ((pl[1] >> 3) & 0x1f);
    int pebit = pl[1] & 0x07;              /* 3 bits */

    int     bs    = 0;
    int     tid   = 0;
    int     trun  = 0;
    int     tsync = 0;
    int     psc   = 0;
    int     size;
    u_char* bts;

    if (vbit) {
	tid   = (bp[bs] >> 5) & 7;   /* 3 bits */
	trun  = (bp[bs] >> 1) & 15;  /* 4 bits */
	tsync = (bp[bs]     ) & 1;   /* 1 bit  */
	bs++;
    }

    /* H.263 bitstream */
    if (pbit && (bp[bs] & 0xfc) == 0x80) {
	/* start of a new frame */
	psc   = 1;
	b_tr  = (bp[bs] << 6) & 0xc0 | (bp[bs+1] >> 2) & 0x3f;
	b_ts  = ts;
	b_all = 1;

	bitstream[0] = 0;
	bitstream[1] = 0;
	b_off = 2;
    }

#ifdef DEBUG263
    /* DEBUG: dump packet data */
    fprintf(stderr,
	    "recv-h263: cc=%4d  "
	    "%s #0x%x ts=0x%x  "
	    "plen=%d pe=%d v=%d p=%d  "
	    "%s tr=%d\n",
	    cc,
	    mbit?"M":"-",seq,ts,
	    plen,pebit,vbit,pbit,
	    psc?"P":"-",b_tr);
    /* dump_paket((const unsigned char *)rh,64); */
#endif
    
    if (b_ts == ts && (psc || (last_seq+1 == seq))) {
	/* copy packet */
	memcpy(bitstream+b_off,bp,cc-bs);
	b_off += cc-bs;
    } else {
	/* oops - missing packet */
	b_all = 0;
    }
    last_seq = seq;

    if (mbit && b_all) {
	/* yea! have a complete frame */
	memcpy(bitstream+b_off,"\x00\x00\x80",3); /* add PSC */
	if (-1 != h263_decode_frame(decoder_state,bitstream)) {
	    if (decoder_state->width != inw_) {
		/*fprintf(stderr,"recv-h263: resize to %dx%d\n",
			decoder_state->width,decoder_state->height);*/
		resize(decoder_state->width, decoder_state->height);
		h263_stop(decoder_state);
		decoder_state = h263_start();
		h263_decode_frame(decoder_state,bitstream);
	    }

	    /* FIXME: avoid that memcpy -- hmm, not that easy :-( */
	    size = decoder_state->width*decoder_state->height;
	    memcpy(xxx_frame,            decoder_state->frame[0],size);
	    memcpy(xxx_frame + size,     decoder_state->frame[1],size/4);
	    memcpy(xxx_frame + size*5/4, decoder_state->frame[2],size/4);
#if 0
	    /* mark all changed */    
	    bts = rvts_;
	    for (int k = nblk_; --k >= 0; bts++)
		*bts = now_;
	    render_frame(xxx_frame);
#else
	    Decoder::redraw(xxx_frame);
#endif
	}
    }
}

void H263plusDecoder::redraw()
{
    if (decoder_state != 0)
	Decoder::redraw(xxx_frame);
}