Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. - (void)ffmpegProcessFrames
  2. {
  3.         // get a pointer sitting at the most recent image data
  4.         //  - we're doing "totalCaptured - 1" because at this point we've already captured the frame, so totalCaptured will reflect
  5.         //    the index after the last frame has been captured.  we want a pointer at the beginning of this data
  6.         //  - % BUFFER_FRAME_MAX_COUNT : since our data buffer is circular - meaning once it gets to the end of the allocated memory,
  7.         //    it starts filling up at the beginning again, we want to compensate for each time we reach the end of the buffer
  8.         //  - * frameSize : frameSize represents the number of bytes per image
  9.         //  - buffer + : we want to get a pointer that sits at the head of the image we want to process, so take the buffer and add
  10.         //    the appropriate number of bytes to get to the current image
  11.     uint8_t *rgb_data = buffer + (frameSize * ((*totalCaptured - 1) % BUFFER_FRAME_MAX_COUNT));
  12.    
  13.     uint8_t *rgb_src[3] = {
  14.         rgb_data,
  15.         NULL,
  16.         NULL
  17.     };
  18.    
  19.     int rgb_stride[3] = {
  20.         4 * FRAME_WIDTH,
  21.         0,
  22.         0
  23.     };
  24.    
  25.     uint8_t *origDataPtr;
  26.     uint8_t *data = av_malloc(4 * FRAME_WIDTH * FRAME_HEIGHT);
  27.     origDataPtr = &(*data);
  28.    
  29.         // i believe this is for planar image representation
  30.     uint8_t *src[4] = {
  31.         data,
  32.         data + FRAME_WIDTH * FRAME_HEIGHT,
  33.         data + FRAME_WIDTH * FRAME_HEIGHT * 2,
  34.         data + FRAME_WIDTH * FRAME_HEIGHT * 3
  35.     };
  36.    
  37.     int stride[4] = {
  38.         FRAME_WIDTH,
  39.         FRAME_WIDTH,
  40.         FRAME_WIDTH,
  41.         FRAME_WIDTH
  42.     };
  43.    
  44.     struct SwsContext *sws;
  45.    
  46.         // get the scale context
  47.     sws = sws_getContext(FRAME_WIDTH, // src width
  48.                 FRAME_HEIGHT, // src height
  49.                 PIX_FMT_RGB32, // src pixel format ,
  50.                 FRAME_WIDTH, // dest width
  51.                 FRAME_HEIGHT, // dest height
  52.                 PIX_FMT_YUVA420P, // dest pix format
  53.                 SWS_BILINEAR, // FLAGS
  54.                 NULL,  
  55.                 NULL,
  56.                 NULL);
  57.    
  58.     int sliceHeight = sws_scale(sws,
  59.                 rgb_src,
  60.                 rgb_stride,
  61.                 0,
  62.                 FRAME_HEIGHT,
  63.                 src,
  64.                 stride);
  65.    
  66.     sws_freeContext(sws);
  67.    
  68.    
  69.     x264_image_t img;
  70.    
  71.     img.i_csp = X264_CSP_I420;
  72.     img.i_plane = 4;
  73.    
  74.     for (int i = 0; i < 4; i++)
  75.     {
  76.         img.i_stride[i] = stride[i];
  77.         img.plane[i] = src[i];
  78.     }
  79.  
  80.     x264_picture_t picvar;
  81.     x264_picture_t *pic = &picvar;
  82.    
  83.     x264_picture_alloc(pic, X264_CSP_I420, FRAME_WIDTH, FRAME_HEIGHT);
  84.    
  85.     pic->img = img;
  86.    
  87.    
  88.         // encode
  89.     x264_picture_t pic_out;
  90.     x264_nal_t *nal;
  91.     int i_nal;
  92.     int i_frame_size = 0;
  93.    
  94.         i_frame_size = x264_encoder_encode( encoderHandle, &nal, &i_nal, pic, &pic_out );
  95.    
  96.     for (int i = 0; i < 4; i++)
  97.         src[i] = NULL;
  98.     /*
  99.     the following line logs this message:
  100.     ProjectName(532,0x854600) malloc: *** error for object 0x615f5f5e: Non-aligned pointer being freed
  101.     *** set a breakpoint in malloc_error_break to debug
  102.     */
  103.     av_freep(origDataPtr);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement