Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 4.41 KB  |  hits: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. FFmpeg does not decode h264 stream
  2. VideoViewer.m
  3.  
  4. - (void)didReceiveFrame:(NSData*)frameData presentationTime:(NSDate*)presentationTime
  5. {
  6.     [VideoDecoder staticInitialize];
  7.     mConverter = [[VideoDecoder alloc] initWithCodec:kVCT_H264 colorSpace:kVCS_RGBA32 width:320 height:240 privateData:nil];
  8.  
  9.  
  10.     [mConverter decodeFrame:frameData];
  11.  
  12.     if ([mConverter isFrameReady]) {
  13.         UIImage *imageData =[mConverter getDecodedFrame];
  14.         if (imageData) {
  15.             [mVideoView setImage:imageData];
  16.             NSLog(@"decoded!");
  17.         }
  18.     }
  19. }
  20.  
  21. ---VideoDecoder.m---
  22. - (id)initWithCodec:(enum VideoCodecType)codecType
  23.          colorSpace:(enum VideoColorSpace)colorSpace
  24.               width:(int)width
  25.              height:(int)height
  26.         privateData:(NSData*)privateData {
  27.     if(self = [super init]) {
  28.  
  29.         codec = avcodec_find_decoder(CODEC_ID_H264);
  30.         codecCtx = avcodec_alloc_context();
  31.  
  32.         // Note: for H.264 RTSP streams, the width and height are usually not specified (width and height are 0).  
  33.         // These fields will become filled in once the first frame is decoded and the SPS is processed.
  34.         codecCtx->width = width;
  35.         codecCtx->height = height;
  36.  
  37.         codecCtx->extradata = av_malloc([privateData length]);
  38.         codecCtx->extradata_size = [privateData length];
  39.         [privateData getBytes:codecCtx->extradata length:codecCtx->extradata_size];
  40.         codecCtx->pix_fmt = PIX_FMT_RGBA;
  41. #ifdef SHOW_DEBUG_MV
  42.         codecCtx->debug_mv = 0xFF;
  43. #endif
  44.  
  45.         srcFrame = avcodec_alloc_frame();
  46.         dstFrame = avcodec_alloc_frame();
  47.  
  48.         int res = avcodec_open(codecCtx, codec);
  49.         if (res < 0)
  50.         {
  51.             NSLog(@"Failed to initialize decoder");
  52.         }
  53.  
  54.     }
  55.  
  56.     return self;    
  57. }
  58.  
  59. - (void)decodeFrame:(NSData*)frameData {
  60.  
  61.  
  62.     AVPacket packet = {0};
  63.     packet.data = (uint8_t*)[frameData bytes];
  64.     packet.size = [frameData length];
  65.  
  66.     int frameFinished=0;
  67.     NSLog(@"Packet size===>%d",packet.size);
  68.     // Is this a packet from the video stream?
  69.     if(packet.stream_index==0)
  70.     {
  71.         int res = avcodec_decode_video2(codecCtx, srcFrame, &frameFinished, &packet);
  72.         NSLog(@"Res value===>%d",res);
  73.         NSLog(@"frame data===>%d",(int)srcFrame->data);
  74.         if (res < 0)
  75.         {
  76.             NSLog(@"Failed to decode frame");
  77.         }
  78.     }
  79.     else
  80.     {
  81.         NSLog(@"No video stream found");
  82.     }
  83.  
  84.  
  85.     // Need to delay initializing the output buffers because we don't know the dimensions until we decode the first frame.
  86.     if (!outputInit) {
  87.         if (codecCtx->width > 0 && codecCtx->height > 0) {
  88. #ifdef _DEBUG
  89.             NSLog(@"Initializing decoder with frame size of: %dx%d", codecCtx->width, codecCtx->height);
  90. #endif
  91.  
  92.             outputBufLen = avpicture_get_size(PIX_FMT_RGBA, codecCtx->width, codecCtx->height);
  93.             outputBuf = av_malloc(outputBufLen);
  94.  
  95.             avpicture_fill((AVPicture*)dstFrame, outputBuf, PIX_FMT_RGBA, codecCtx->width, codecCtx->height);
  96.  
  97.             convertCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt,  codecCtx->width,
  98.                                         codecCtx->height, PIX_FMT_RGBA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  99.  
  100.             outputInit = YES;
  101.             frameFinished=1;
  102.         }
  103.         else {
  104.             NSLog(@"Could not get video output dimensions");
  105.         }
  106.     }
  107.  
  108.     if (frameFinished)
  109.         frameReady = YES;
  110.  
  111. }
  112.        
  113. 2011-05-16 20:16:04.223 RTSPTest1[41226:207] Packet size===>359
  114. [h264 @ 0x5815c00] no frame!
  115. 2011-05-16 20:16:04.223 RTSPTest1[41226:207] Res value===>-1
  116. 2011-05-16 20:16:04.224 RTSPTest1[41226:207] frame data===>101791200
  117. 2011-05-16 20:16:04.224 RTSPTest1[41226:207] Failed to decode frame
  118. 2011-05-16 20:16:04.225 RTSPTest1[41226:207] decoded!
  119. 2011-05-16 20:16:04.226 RTSPTest1[41226:207] Packet size===>424
  120. [h264 @ 0x5017c00] no frame!
  121. 2011-05-16 20:16:04.226 RTSPTest1[41226:207] Res value===>-1
  122. 2011-05-16 20:16:04.227 RTSPTest1[41226:207] frame data===>81002704
  123. 2011-05-16 20:16:04.227 RTSPTest1[41226:207] Failed to decode frame
  124. 2011-05-16 20:16:04.228 RTSPTest1[41226:207] decoded!
  125. 2011-05-16 20:16:04.229 RTSPTest1[41226:207] Packet size===>424
  126. [h264 @ 0x581d000] no frame!
  127. 2011-05-16 20:16:04.229 RTSPTest1[41226:207] Res value===>-1
  128. 2011-05-16 20:16:04.230 RTSPTest1[41226:207] frame data===>101791616
  129. 2011-05-16 20:16:04.230 RTSPTest1[41226:207] Failed to decode frame
  130. 2011-05-16 20:16:04.231 RTSPTest1[41226:207] decoded!
  131. . . . .  .