Changeset 3974 for vic/branches/mpeg4/codec/decoder-h264.cpp
- Timestamp:
- 03/23/07 18:11:29 (6 years ago)
- Files:
-
- 1 modified
-
vic/branches/mpeg4/codec/decoder-h264.cpp (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
vic/branches/mpeg4/codec/decoder-h264.cpp
r3942 r3974 4 4 #include <assert.h> 5 5 #include <iostream> 6 #include <errno.h> 6 7 #include "inet.h" 7 8 #include "rtp.h" … … 11 12 #include "databuffer.h" 12 13 #include "ffmpeg_codec.h" 14 #include "rtp_h264_depayloader.h" 15 16 17 #define SDP_LINE_LEN 10000 13 18 14 19 //#define DIRECT_DISPLAY 1 15 16 using namespace std; 17 18 extern "C" UCHAR * video_frame; 20 //using namespace std; 21 //extern "C" UCHAR * video_frame; 22 19 23 20 24 class H264Decoder:public Decoder … … 24 28 ~H264Decoder(); 25 29 30 void handleSDP(); 26 31 virtual void recv(pktbuf *); 27 32 int colorhist(u_int * hist) const; … … 31 36 32 37 /* packet statistics */ 33 u _int16_t last_seq; /* sequence number */38 uint16_t last_seq; /* sequence number */ 34 39 35 40 UCHAR bitstream[MAX_CODED_SIZE]; /* bitstream data */ 36 41 37 42 /* collecting data for a frame */ 38 int idx;43 uint16_t idx; 39 44 int last_mbit; 40 45 int last_iframe; … … 44 49 UCHAR xxx_frame[MAX_FRAME_SIZE]; 45 50 FFMpegCodec h264; 46 PacketBuffer *stream; 47 51 PacketBuffer *stream; //SV: probably this is going to be substituted by the AVPacket->data when we call decode()... 52 H264Depayloader *h264depayloader; 48 53 49 54 //For DEBUG 50 FILE *fptr; 55 //FILE *fptr; 56 FILE *sdp_fptr; 51 57 }; 52 58 … … 68 74 69 75 70 H264Decoder::H264Decoder():Decoder( 2)76 H264Decoder::H264Decoder():Decoder(0 /* 0 byte extra header */) 71 77 { /* , codec_(0), */ 72 78 79 80 //Barz: ============================================= 73 81 decimation_ = 411; 74 82 /* … … 79 87 inh_ = 0; 80 88 81 /*XXX*/ resize(inw_, inh_); 89 /*XXX*/ 90 resize(inw_, inh_); 82 91 83 92 // libavcodec … … 89 98 last_iframe = 0; 90 99 last_seq = 0; 100 //=================================================== 101 102 103 //SV: =============================================== 104 //Create payloader 105 h264depayloader = new H264Depayloader; 106 107 handleSDP(); 108 //make sure all codec params are set up properly before decoding 109 110 //check whether AVPacket struct can be used in stead of Barz's PacketBuffer 111 //=================================================== 112 91 113 92 114 //256 packets, each 1600 byte (default will not exceed 1600 byte) 93 115 //cout << "new PacketBuffer..\n"; 94 stream = new PacketBuffer(1024, 1600); 116 stream = new PacketBuffer(1024, 1600); //SV: 1024 = ??? 95 117 startPkt = false; 118 } 119 120 void 121 H264Decoder::handleSDP() 122 { 123 char SdpFilename[SDP_LINE_LEN]; 124 char *line, *sdp_string; 125 char *SdpLine=NULL; 126 int n_char; 127 size_t nBytes = 0; 128 ssize_t SdpRead; 129 char defaultSDP[]="a=rtpmap:96 H264/90000\na=fmtp:96 profile-level-id=00000d; packetization-mode=1\n"; 130 131 sprintf(SdpFilename, "%s/default.sdp", getenv("HOME")); 96 132 97 133 //fptr = fopen("out.m4v", "w"); 134 if ((sdp_fptr = fopen(SdpFilename, "r")) != NULL ) { 135 //fprintf(stderr, "H264_RTP: Opened SDP file %s for read.\n", SdpFilename); 136 137 //Read SDP file into struct 138 //fprintf(stderr, "H264_RTP: Spitting SDP ================================================\n"); 139 while ((SdpRead = getline(&SdpLine, &nBytes, sdp_fptr)) != -1) { 140 //fprintf(stderr, "H264_RTP: Read %d bytes from SDP file.\n", SdpRead); 141 //fprintf(stderr, "%s", SdpLine); 142 //call SDP parse h264 routine 143 h264depayloader->parse_h264_sdp_line(h264.c, h264depayloader->h264_extradata, SdpLine); 144 } 145 if (SdpLine) 146 free(SdpLine); 147 148 } else { 149 fprintf(stderr, "H264_RTP: Couldn't open SDP file %s to read. Errno = %d\n", SdpFilename, errno); 150 fprintf(stderr, "H264_RTP: Using default SDP: %s \n", defaultSDP); 151 152 line=defaultSDP; 153 do { 154 n_char = strcspn(line, "\n"); 155 SdpLine = (char *)malloc(n_char+1); 156 memset(SdpLine, '\0', n_char+1); 157 strncpy(SdpLine, line, n_char); 158 line += n_char + 1; 159 h264depayloader->parse_h264_sdp_line(h264.c, h264depayloader->h264_extradata, SdpLine); 160 free(SdpLine); 161 } while (n_char != 0); 162 //fprintf(stderr, "H264_RTP: Done spitting SDP ===========================================\n"); 163 } 98 164 } 99 165 … … 101 167 { 102 168 delete stream; 169 delete h264depayloader; 103 170 //fclose(fptr); 171 if (sdp_fptr != NULL) fclose(sdp_fptr); 172 //fprintf(stderr, "H264_RTP: Closed SDP file.\n"); 104 173 } 105 174 106 175 int H264Decoder::colorhist(u_int * hist) const 107 176 { 177 UNUSED(hist); 178 108 179 return (1); 109 180 } … … 115 186 u_char *bp = pb->dp + hdrsize; 116 187 int cc = pb->len - hdrsize; 117 static int iframe_c = 0, pframe_c = 0;188 //static int iframe_c = 0, pframe_c = 0; 118 189 119 190 int mbit = ntohs(rh->rh_flags) >> 7 & 1; 120 int seq = ntohs(rh->rh_seqno);191 uint16_t seq = ntohs(rh->rh_seqno); 121 192 int ts = ntohl(rh->rh_ts); 122 193 194 195 196 //Barz: ============================================= 123 197 if (!startPkt) { 124 198 stream->clear(); 125 199 startPkt = true; 126 200 idx = seq; 127 last_seq = seq - 1;201 last_seq = seq - 1; 128 202 } 129 203 … … 134 208 135 209 if (abs(seq - last_seq) > 5) { 136 debug_msg("h264dec: sequece interrupt!\n");210 //fprintf(stderr, "H264_RTP: sequece interrupt!\n"); 137 211 idx = seq; 138 212 pktIdx = 0; 139 213 stream->clear(); 140 214 }else if (last_seq + 1 != seq) { 141 /* oops - missing packet */ 142 debug_msg("h264dec: missing packet\n"); 143 } 215 // oops - missing packet 216 //fprintf(stderr, "H264_RTP: missing packet\n"); 217 } 218 219 //=================================================== 220 144 221 145 222 //copy packet 146 stream->write(pktIdx, cc, (char *) bp); 147 223 //stream->write(pktIdx, cc, (char *) bp); 224 //fprintf(stderr, "H264_RTP: -------------------------------- seq = %d, m=%d, ts=%d, cc=%d\n", seq, mbit, ts, cc); 225 //fprintf(stderr, "H264_RTP: pktIdx = %d\n", pktIdx); 226 int yo = h264depayloader->h264_handle_packet(h264depayloader->h264_extradata, pktIdx, stream, /*ts,*/ bp, cc); 227 //fprintf(stderr, "H264_RTP: h264_handle_packet = %d\n", yo); 228 229 230 //Barz: ============================================= 231 148 232 last_seq = seq; 149 233 int len=0; 150 151 234 if (mbit) { 152 235 stream->setTotalPkts(pktIdx + 1); 153 236 154 DataBuffer *f; 237 DataBuffer *f; 155 238 if (stream->isComplete()) { 156 f = stream->getStream();239 f = stream->getStream(); 157 240 len = h264.decode((UCHAR *) f->getData(), f->getDataSize(), xxx_frame); 158 241 } 159 242 160 243 if (len < 0) { 161 debug_msg("h264dec: frame error\n");244 //fprintf(stderr, "H264_RTP: frame error\n"); 162 245 } 163 246 … … 170 253 Decoder::redraw(xxx_frame); 171 254 } 172 stream->clear();255 stream->clear(); 173 256 idx = seq+1; 174 175 } 257 } 258 259 //=================================================== 260 176 261 pb->release(); 177 262 }
