public int avcodec_open(H264Decoder codec) { int ret = -1; /* If there is a user-supplied mutex locking routine, call it. */ /* if (ff_lockmgr_cb) { if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) return -1; } */ /* entangled_thread_counter++; if(entangled_thread_counter != 1){ av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); goto end; } */ //if(this.codec!=null || codec == null) // return ret; this.priv_data = new H264Context(); //priv_data.av_opt_set_defaults(); if (this.coded_width != 0 && this.coded_height != 0) this.avcodec_set_dimensions(this.coded_width, this.coded_height); else if (this.width != 0 && this.height != 0) this.avcodec_set_dimensions(this.width, this.height); if ((this.coded_width != 0 || this.coded_height != 0 || this.width != 0 || this.height != 0) && (av_image_check_size(this.coded_width, this.coded_height, 0, this) < 0 || av_image_check_size(this.width, this.height, 0, this) < 0)) { //av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n"); this.avcodec_set_dimensions(0, 0); } /* if the decoder init function was already called previously, free the already allocated subtitle_header before overwriting it */ //?????????????????????? //if (codec.decode!=0) // av_freep(&this.subtitle_header); //#define SANE_NB_CHANNELS 128U //if (this.channels > 128) { // ret = -1; // return ret; // } this.codec = codec; //if ((this.codec_type == AVMEDIA_TYPE_UNKNOWN || this.codec_type == codec.type) && // this.codec_id == CODEC_ID_NONE) { // this.codec_type = codec.type; this.codec_id = codec.id; //} //if (this.codec_id != codec.id || (this.codec_type != codec.type // && this.codec_type != AVMEDIA_TYPE_ATTACHMENT)) { //av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n"); // return ret; //} this.frame_number = 0; if (this.codec.max_lowres < this.lowres) { //av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n", // this.codec.max_lowres); return ret; } ret = this.codec.init(this); if (ret < 0) { return ret; } // this ret = 0; return ret; }
public int avcodec_close() { if (this.codec != null) this.codec.close(this); avcodec_default_free_buffers(); this.coded_frame = null; this.priv_data = null; this.codec = null; return 0; }
private void Init() { avpkt.av_init_packet(); // Set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) Arrays.Fill(inbuf, INBUF_SIZE, MpegEncContext.FF_INPUT_BUFFER_PADDING_SIZE + INBUF_SIZE, (sbyte)0); // Find the mpeg1 video decoder codec = new H264Decoder(); if (codec == null) { throw (new Exception("codec not found")); } c = MpegEncContext.avcodec_alloc_context(); picture = AVFrame.avcodec_alloc_frame(); // We do not send complete frames if ((codec.capabilities & H264Decoder.CODEC_CAP_TRUNCATED) != 0) { c.flags |= MpegEncContext.CODEC_FLAG_TRUNCATED; } // For some codecs, such as msmpeg4 and mpeg4, width and height // MUST be initialized there because this information is not // available in the bitstream. // Open it if (c.avcodec_open(codec) < 0) { throw (new Exception("could not open codec")); } // The codec gives us the frame size, in samples frame = 0; // avpkt must contain exactly 1 NAL Unit in order for decoder to decode correctly. // thus we must read until we get next NAL header before sending it to decoder. // Find 1st NAL cacheRead[0] = fin.ReadByte(); cacheRead[1] = fin.ReadByte(); cacheRead[2] = fin.ReadByte(); while (!(cacheRead[0] == 0x00 && cacheRead[1] == 0x00 && cacheRead[2] == 0x01)) { cacheRead[0] = cacheRead[1]; cacheRead[1] = cacheRead[2]; cacheRead[2] = fin.ReadByte(); if (cacheRead[2] == -1) throw(new EndOfStreamException()); } // while // 4 first bytes always indicate NAL header inbuf_int[0] = inbuf_int[1] = inbuf_int[2] = 0x00; inbuf_int[3] = 0x01; hasMoreNAL = true; }