Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int decoderTest() {
- AVFormatContext *pFormatCtx = NULL;
- AVCodecContext *pCodecCtx = NULL;
- PixelFormat original_pixelFormat;
- int i, videoStream;
- AVPacket packet;
- TriStreamSocket MyConnection;
- Info = new StreamInfo();
- int w, h;
- TriStreamCoder triDecoder(Info);
- Info->gopSize(4);
- Info->bitRate(0);
- Info->frameRate(15);
- Info->codecId(13);
- Info->imageWidth(1280);
- Info->imageHeight(720);
- int width = 1280;
- int height = 720;
- TriStreamPackage *triPkg=NULL;
- system("rm -rf *.ppm");
- av_register_all();
- avformat_network_init();
- cout << "Registered codecs" << endl;
- //char* stream = "rtsp://172.16.50.36:8554/para.m4e";
- //char* stream = "rtsp://10.1.93.76/VideoInput/1/mpeg4/1";
- char* stream = "rtsp://192.168.0.20/stream1";
- cout << "Start Transcoding.." << endl;
- while(1)
- {
- cout << "Opening RTSP stream: " << stream << " ..........................." << endl;
- if( avformat_open_input(&pFormatCtx, stream, NULL,NULL)!=0 ) {
- cout << "Error opening rtsp stream\n" << endl;
- return -1; // Couldn't open file (
- }
- cout << "RTSP Stream succesfully opened!" << endl;
- if(av_find_stream_info(pFormatCtx)<0){
- cout << "Error: Couldn't find stream information !\n" << endl;
- return -1;
- }
- cout << "Found stream information!\n" << endl;
- videoStream=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++) {
- //NEW:
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
- videoStream=i;
- break;
- }
- }
- if(videoStream==-1) {
- cout << "Error: VideoStream not found" << endl;
- return -1;
- } else {
- cout << "Found videostream! \n" << endl;
- }
- AVCodec *pCodec;
- int decoder = 0;
- // Load automatic decoder:
- if(decoder == 0) {
- pCodecCtx=pFormatCtx->streams[videoStream]->codec;
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL) {
- fprintf(stderr, "Unsupported codec!\n");
- return -1; // Codec not found
- }
- if(avcodec_open(pCodecCtx, pCodec)<0)
- return -1; // Could not open codec
- // settings for swscaler:
- w = pCodecCtx->width;
- h = pCodecCtx->height;
- original_pixelFormat = pCodecCtx->pix_fmt;
- }
- //Load custom H264 decoder
- else if (decoder == 1) {
- pCodec = avcodec_find_decoder(CODEC_ID_H264);
- if (!pCodec) {
- fprintf(stderr, "codec not found\n");
- exit(1);
- }
- pCodecCtx = avcodec_alloc_context3(pCodec);
- // picture= avcodec_alloc_frame();
- if(pCodec->capabilities&CODEC_CAP_TRUNCATED)
- pCodecCtx->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
- /* 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 (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
- fprintf(stderr, "could not open codec\n");
- exit(1);
- }
- pCodecCtx->width = 1280;
- pCodecCtx->height = 720;
- pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
- //settings for swscaler:
- w = 1280;
- h = 720;
- original_pixelFormat = PIX_FMT_YUV420P;
- }
- //Alloceer frames en variabelen .
- AVFrame *pFrame = NULL;
- AVFrame *pFrameRGB = NULL;
- int numBytes = 0;
- uint8_t *buffer = NULL;
- AVPacket unpackedPacket;
- pFrame = avcodec_alloc_frame();
- pFrameRGB = avcodec_alloc_frame();
- numBytes=avpicture_get_size(PIX_FMT_RGB24, width, height);
- buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
- avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, width, height);
- Image* img;
- while(av_read_frame(pFormatCtx, &packet)>=0) {
- int frameFinished = 0;
- if(packet.stream_index==videoStream) {
- //Stop AVPacket in TriStreamPackage:
- AVPacket *p;
- triPkg = new TriStreamPackage(sizeof(AVPacket));
- memcpy(triPkg->encDataBuffer(), &packet, sizeof(AVPacket));
- //Configureer TriStreamPackage:
- //triPkg->setKeyFrame(1);
- //triPkg->setSequenceNr(0);
- // get extra_data..
- // static int av_mpeg4_decode_header (AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
- // uint8_t *extradata;
- // printf("%d\n",(int)pCodecCtx->extradata);
- // printf(" %02X% \n", pCodecCtx->extradata );
- // printf("extradata size: %d\n", pCodecCtx->extradata_size);
- AVPacket *unpackedPkt = (AVPacket*)triPkg->encDataBuffer();
- //Tristreampackage decoded:
- cout << "calling avcodec_decode_video" << endl;
- if (avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, unpackedPkt) < 0 ){
- cout << "aborting.." << endl;
- abort();
- }
- cout << "Finished avcodec_decode_video" << endl;
- if(frameFinished) {
- //Converteer frames naar RGB voor opslag
- static struct SwsContext *img_convert_ctx;
- if(img_convert_ctx == NULL) {
- int w = pCodecCtx->width;
- int h = pCodecCtx->height;
- img_convert_ctx = sws_getContext( w, h,
- pCodecCtx->pix_fmt, w, h,
- PIX_FMT_RGB24, SWS_BICUBIC,
- NULL, NULL, NULL );
- if(img_convert_ctx == NULL) {
- fprintf(stderr, "Cannot initialize the conversion context!\n");
- exit(1);
- }
- }
- int ret = sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,
- 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
- if (ret < 0) abort();
- //Sla frames op als images:
- cout << "Saving image: " << i << endl;
- if(++i<=10) {
- SaveFrame(pFrameRGB, w, h, i);
- } else {
- av_free_packet(&packet);
- exit(1);
- }
- av_free_packet(&packet);
- }
- }
- }
- }
- avcodec_close(pCodecCtx);
- av_close_input_file(pFormatCtx);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement