Advertisement
Guest User

Untitled

a guest
Aug 15th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. int decoderTest() {
  2.     AVFormatContext *pFormatCtx = NULL;
  3.     AVCodecContext  *pCodecCtx = NULL;
  4.  
  5.     PixelFormat original_pixelFormat;
  6.     int             i, videoStream;
  7.     AVPacket        packet;
  8.     TriStreamSocket MyConnection;
  9.     Info = new StreamInfo();
  10.     int w, h;
  11.  
  12.     TriStreamCoder triDecoder(Info);
  13.  
  14.     Info->gopSize(4);
  15.     Info->bitRate(0);
  16.     Info->frameRate(15);
  17.     Info->codecId(13);
  18.     Info->imageWidth(1280);
  19.     Info->imageHeight(720);
  20.  
  21.     int width = 1280;
  22.     int height = 720;
  23.  
  24.     TriStreamPackage *triPkg=NULL;
  25.     system("rm -rf *.ppm");
  26.     av_register_all();
  27.     avformat_network_init();
  28.  
  29.     cout << "Registered codecs" << endl;
  30.     //char* stream = "rtsp://172.16.50.36:8554/para.m4e";
  31.     //char* stream = "rtsp://10.1.93.76/VideoInput/1/mpeg4/1";
  32.     char* stream = "rtsp://192.168.0.20/stream1";
  33.     cout << "Start Transcoding.." << endl;
  34.     while(1)
  35.     {
  36.         cout << "Opening RTSP stream: " << stream  << " ..........................." << endl;
  37.         if( avformat_open_input(&pFormatCtx, stream, NULL,NULL)!=0 ) {
  38.             cout << "Error opening rtsp stream\n" << endl;
  39.             return -1; // Couldn't open file (
  40.         }
  41.         cout << "RTSP Stream succesfully opened!" << endl;
  42.         if(av_find_stream_info(pFormatCtx)<0){
  43.             cout << "Error: Couldn't find stream information !\n" << endl;
  44.             return -1;
  45.         }
  46.         cout << "Found stream information!\n" << endl;
  47.         videoStream=-1;
  48.         for(i=0; i<pFormatCtx->nb_streams; i++) {
  49.             //NEW:
  50.             if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  51.                 videoStream=i;
  52.                 break;
  53.             }
  54.         }
  55.         if(videoStream==-1) {
  56.             cout << "Error: VideoStream not found" << endl;
  57.             return -1;
  58.         } else {
  59.             cout << "Found videostream! \n" << endl;
  60.         }
  61.  
  62.  
  63.         AVCodec *pCodec;
  64.  
  65.         int decoder = 0;
  66.  
  67.         // Load automatic decoder:
  68.         if(decoder == 0) {
  69.             pCodecCtx=pFormatCtx->streams[videoStream]->codec;
  70.             pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  71.  
  72.             if(pCodec==NULL) {
  73.                 fprintf(stderr, "Unsupported codec!\n");
  74.                 return -1; // Codec not found
  75.             }
  76.             if(avcodec_open(pCodecCtx, pCodec)<0)
  77.                 return -1; // Could not open codec
  78.             // settings for swscaler:
  79.             w = pCodecCtx->width;
  80.             h = pCodecCtx->height;
  81.             original_pixelFormat =  pCodecCtx->pix_fmt;
  82.         }
  83.  
  84.         //Load custom H264 decoder
  85.         else if (decoder == 1) {
  86.             pCodec = avcodec_find_decoder(CODEC_ID_H264);
  87.             if (!pCodec) {
  88.                 fprintf(stderr, "codec not found\n");
  89.                 exit(1);
  90.             }
  91.  
  92.             pCodecCtx = avcodec_alloc_context3(pCodec);
  93. //          picture= avcodec_alloc_frame();
  94.  
  95.             if(pCodec->capabilities&CODEC_CAP_TRUNCATED)
  96.                 pCodecCtx->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
  97.  
  98.             /* For some codecs, such as msmpeg4 and mpeg4, width and height
  99.                            MUST be initialized there because this information is not
  100.                            available in the bitstream. */
  101.  
  102.             /* open it */
  103.             if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
  104.                 fprintf(stderr, "could not open codec\n");
  105.                 exit(1);
  106.             }
  107.  
  108.             pCodecCtx->width = 1280;
  109.             pCodecCtx->height = 720;
  110.             pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
  111.  
  112.  
  113.             //settings for swscaler:
  114.             w = 1280;
  115.             h = 720;
  116.             original_pixelFormat = PIX_FMT_YUV420P;
  117.         }
  118.  
  119.  
  120.  
  121.         //Alloceer frames en variabelen .
  122.         AVFrame *pFrame = NULL;
  123.         AVFrame *pFrameRGB = NULL;
  124.         int numBytes = 0;
  125.         uint8_t  *buffer = NULL;
  126.         AVPacket unpackedPacket;
  127.         pFrame = avcodec_alloc_frame();
  128.         pFrameRGB = avcodec_alloc_frame();
  129.  
  130.         numBytes=avpicture_get_size(PIX_FMT_RGB24, width, height);
  131.         buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  132.         avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, width, height);
  133.  
  134.         Image* img;
  135.  
  136.         while(av_read_frame(pFormatCtx, &packet)>=0) {
  137.             int frameFinished = 0;
  138.             if(packet.stream_index==videoStream) {
  139.                 //Stop AVPacket in TriStreamPackage:
  140.                 AVPacket *p;
  141.                 triPkg = new TriStreamPackage(sizeof(AVPacket));
  142.                 memcpy(triPkg->encDataBuffer(), &packet, sizeof(AVPacket));
  143.  
  144.                 //Configureer TriStreamPackage:
  145.                 //triPkg->setKeyFrame(1);
  146.                 //triPkg->setSequenceNr(0);
  147.  
  148.                 // get extra_data..
  149.                 // static int av_mpeg4_decode_header (AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
  150.  
  151.                 //      uint8_t *extradata;
  152.                 //      printf("%d\n",(int)pCodecCtx->extradata);
  153.                 //      printf(" %02X% \n", pCodecCtx->extradata );
  154.                 //      printf("extradata size: %d\n", pCodecCtx->extradata_size);
  155.  
  156.  
  157.                 AVPacket *unpackedPkt = (AVPacket*)triPkg->encDataBuffer();
  158.                 //Tristreampackage decoded:
  159.                 cout << "calling avcodec_decode_video" << endl;
  160.                 if (avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, unpackedPkt) < 0 ){
  161.                     cout << "aborting.." << endl;
  162.                     abort();
  163.                 }
  164.                 cout << "Finished avcodec_decode_video" << endl;
  165.  
  166.  
  167.                 if(frameFinished) {
  168.                     //Converteer frames naar RGB voor opslag
  169.                     static struct SwsContext *img_convert_ctx;
  170.  
  171.                     if(img_convert_ctx == NULL) {
  172.  
  173.                         int w = pCodecCtx->width;
  174.                         int h = pCodecCtx->height;
  175.  
  176.                         img_convert_ctx = sws_getContext( w, h,
  177.                                 pCodecCtx->pix_fmt, w, h,
  178.                                 PIX_FMT_RGB24, SWS_BICUBIC,
  179.                                 NULL, NULL, NULL );
  180.  
  181.                         if(img_convert_ctx == NULL) {
  182.                             fprintf(stderr, "Cannot initialize the conversion context!\n");
  183.                             exit(1);
  184.                         }
  185.                     }
  186.  
  187.                     int ret = sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,
  188.                             0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  189.  
  190.                     if (ret < 0) abort();
  191.  
  192.                     //Sla frames op als images:
  193.                     cout << "Saving image: " << i << endl;
  194.                     if(++i<=10) {
  195.                         SaveFrame(pFrameRGB, w, h, i);
  196.                     } else {
  197.                         av_free_packet(&packet);
  198.                         exit(1);
  199.                     }
  200.                     av_free_packet(&packet);
  201.                 }
  202.             }
  203.         }
  204.     }
  205.     avcodec_close(pCodecCtx);
  206.     av_close_input_file(pFormatCtx);
  207.  
  208.     return 1;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement