1 /*
2 * decoder-xvid.cc --
3 *
4 * XVID decoder
5 */
6
7 /* This code was originally derived from a cellb reference decoder from
8 * Sun Microsystems with the following copyright:
9 *
10 * Copyright (c) Sun Microsystems, Inc. 1992, 1993. All rights reserved.
11 *
12 * License is granted to copy, to use, and to make and to use derivative
13 * works for research and evaluation purposes, provided that Sun Microsystems is
14 * acknowledged in all documentation pertaining to any such copy or derivative
15 * work. Sun Microsystems grants no other licenses expressed or implied. The
16 * Sun Microsystems trade name should not be used in any advertising without
17 * its written permission.
18 *
19 * SUN MICROSYSTEMS MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF
20 * THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is provided "as is"
21 * without express or implied warranty of any kind.
22 *
23 * These notices must be retained in any copies of any part of this software.
24 */
25
26 #include <fstream.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <iostream.h>
32 #include "inet.h"
33 #include "rtp.h"
34 #include "decoder.h"
35 #include "bsd-endian.h"
36 #include "tclcl.h"
37 #include "renderer.h"
38 #include "pktbuf.h"
39 #include "postdct.h"
40
41 #define MPEG4IP
42 #include "decoder-xvid.h"
43 #include "xvid.h"
44
45 static class Mpeg4DecoderClass : public TclClass {
46 public:
47 Mpeg4DecoderClass() : TclClass("Module/VideoDecoder/MPEG4") {}
48 TclObject* create(int /* argc */, const char*const* /* argv */) {
49 return (new Mpeg4Decoder());
50 }
51 } dm_mpeg4;
52
53 Mpeg4Decoder::Mpeg4Decoder()
54 : Decoder(sizeof(mpeg4hdr)) //, csss_(420)
55 {
56 csss_ = 420;
57 slot1 = NULL;
58 slot2 = NULL;
59 current_time_stamp = 0;
60
61 initialised = false;
62 myhandle = NULL;
63 }
64
65 void Mpeg4Decoder::init()
66 {
67 XVID_INIT_PARAM xvid_param;
68 xvid_param.cpu_flags = 0;
69 result = xvid_init(NULL, 0, &xvid_param, NULL);
70 if (result != XVID_ERR_OK)
71 return;
72
73 XVID_DEC_PARAM dec_param;
74 dec_param.width = ntohs(mympeg4hdr->width);
75 dec_param.height = ntohs(mympeg4hdr->height);
76 result = xvid_decore(NULL, XVID_DEC_CREATE, &dec_param, NULL);
77 if (result != XVID_ERR_OK)
78 return;
79 myhandle = dec_param.handle;
80
81 resize (ntohs(mympeg4hdr->width), ntohs(mympeg4hdr->height));
82
83 initialised = true;
84 }
85
86 Mpeg4Decoder::~Mpeg4Decoder()
87 {
88 //delete codec_;
89 }
90
91 void Mpeg4Decoder::decode(slot * mp4frame, int len)
92 {
93 XVID_DEC_FRAME myframe;
94 myframe.bitstream = mp4frame->framebuf;
95 myframe.length = len;
96 myframe.colorspace = XVID_CSP_I420;
97 myframe.stride = ntohs(mympeg4hdr->width);
98 int temp = ntohs(mympeg4hdr->height) * ntohs(mympeg4hdr->width);
99 u_int8_t * myimage = (u_int8_t *) malloc (temp * 2);
100 myframe.image = myimage;
101 //1 parameter is useless, "general", dun need to care
102
103 result = xvid_decore(myhandle, XVID_DEC_DECODE, &myframe, NULL);
104 if (result != XVID_ERR_OK) {
105 return;
106 }
107 //to render every blk: refer to decoder.cc for information about rvts_
108 for (int i = 0; i < nblk_ ; i++) {
109 rvts_[i] = now_;
110 }
111
112 render_frame ((u_int8_t *) myframe.image);
113
114 free(myimage);
115
116 /*
117 FILE * fptr;
118 fptr = fopen ("/home/jingye/3rd_trial/decoded.xvid", "a");
119 fwrite (myframe.image, sizeof(char), (temp * 2), fptr);
120 fclose(fptr);
121 */
122 }
123
124
125 void Mpeg4Decoder::recv(pktbuf* bp)
126 {
127 myrtphdr = (rtphdr *) (bp->dp);
128 mympeg4hdr = (mpeg4hdr *) (myrtphdr + 1);
129 mp4stream = (u_int8_t *) (mympeg4hdr + 1);
130
131 if (!initialised) {
132 init ();
133 }
134
135 if (ntohs(mympeg4hdr->height) != inh_ || ntohs(mympeg4hdr->width) != inw_){
136 xvid_decore(myhandle, XVID_DEC_DESTROY, NULL, NULL);
137 init();
138 }
139
140 slot * frameslot;
141 frameslot = assemble (bp);
142 if (frameslot == NULL) {
143 bp->release();
144 return;
145 }
146 if (frameslot->timestamp > current_time_stamp) {
147 current_time_stamp = frameslot -> timestamp;
148 decode(frameslot, bp->len);
149 }
150 bp->release();
151 return;
152 }
153
154 slot * Mpeg4Decoder::assemble (pktbuf* bp)
155 {
156 slot * currentslot;
157 u_int32_t current_timestamp = ntohl(myrtphdr->rh_ts);
158
159 //deciding which slot to use for reassembling; if the packet arrives too late, it is dropped
160 if ( slot1 != NULL && slot1->timestamp == current_timestamp)
161 currentslot = slot1;
162 else if (slot2 != NULL && slot2->timestamp == current_timestamp)
163 currentslot = slot2;
164 else if (slot1 == NULL ) {
165 slot1 = (slot *)malloc (sizeof (slot));
166 currentslot = slot1;
167 currentslot->timestamp = current_timestamp;
168 currentslot->packets_left = ntohs(mympeg4hdr->num_of_packets);
169 } else if (slot2 == NULL) {
170 slot2 = (slot *)malloc (sizeof (slot));
171 currentslot = slot2;
172 currentslot->timestamp = current_timestamp;
173 currentslot->packets_left = ntohs(mympeg4hdr->num_of_packets);
174 } else if (current_timestamp > slot1->timestamp && slot2->timestamp > slot1->timestamp) {
175 currentslot = slot1;
176 currentslot->timestamp = current_timestamp;
177 currentslot->packets_left = ntohs(mympeg4hdr->num_of_packets);
178 } else if (current_timestamp > slot2->timestamp && slot1->timestamp > slot2->timestamp) {
179 currentslot = slot2;
180 currentslot->timestamp = current_timestamp;
181 currentslot->packets_left = ntohs(mympeg4hdr->num_of_packets);
182 } else
183 return NULL;
184
185 //find the position of the packet in the frame and insert
186 u_int8_t * temp;
187 temp = currentslot->framebuf + ntohl(mympeg4hdr->offset);
188 memcpy (temp, mp4stream, (bp->len - sizeof(rtphdr)- sizeof(mpeg4hdr)));
189 (currentslot->packets_left)--; //keep track of the number of packets left
190
191 if (currentslot->packets_left == 0)
192 return currentslot;
193 else
194 return NULL;
195
196 }
197
198 int Mpeg4Decoder::colorhist(u_int*) const
199 {
200 return 0;
201 }
202
203 void Mpeg4Decoder::redraw()
204 {
205 u_int8_t * dummy;
206 Decoder::redraw(dummy);
207 }
208
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.