Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     uint8_t *rgb_data = buffer + (frameSize * ((*totalCaptured - 1) % BUFFER_FRAME_MAX_COUNT));
  2.    
  3.     uint8_t *rgb_src[3] = {
  4.         rgb_data,
  5.         NULL,
  6.         NULL
  7.     };
  8.    
  9.     int rgb_stride[3] = {
  10.         4 * FRAME_WIDTH,
  11.         0,
  12.         0
  13.     };
  14.    
  15.     /*
  16.      * Our array should look like this, according to the Y'UV spec:
  17.      * [ ======================= Y' =======================, =========== U ===========, =========== V =========== ]
  18.      *
  19.      * where
  20.      *      the length of Y' == frame_width * frame_height
  21.      * and
  22.      *      the length of U  == frame_width * frame_height / 4
  23.      * and
  24.      *      the length of V  == frame_width * frame_height / 4 (same as U)
  25.      *
  26.      *
  27.      * For reference, an RGB array looks like this:
  28.      *
  29.      *   [pixel]  [pixel]  [pixel]  [pixel]
  30.      * [ A,B,G,R, A,G,B,R, A,G,B,R, A,B,G,R...(cont'd)]
  31.      *
  32.      * (four bytes per pixel, packed)
  33.      */
  34.     uint8_t *src[4] = {
  35.         /*
  36.          * First pointer should point to a section of data that starts
  37.          * at zero and should be as many bytes as there are pixels.  The
  38.          * Y' value in Y'UV needs one byte per pixel.
  39.          */
  40.         scaleBuffer,
  41.         /*
  42.          * Second pointer should start at 1byte * frame_width * frame_height
  43.          * and should be long enough to contain one fourth the amount of data as
  44.          * the Y' value, since one U component of Y'UV contains data for four Y' components
  45.          */
  46.         scaleBuffer + FRAME_WIDTH * FRAME_HEIGHT,
  47.         /*
  48.          * Third pointer should be same as previous.  both the U and V components in Y'UV
  49.          * contain one forth the amount of data as the Y' component.
  50.          */
  51.         scaleBuffer + ( FRAME_WIDTH * FRAME_HEIGHT ) + ( FRAME_WIDTH * FRAME_HEIGHT / 4 ),
  52.         /*
  53.          * No data needed for the fourth row since we're all accounted for in the first three
  54.          */
  55.         NULL
  56.     };
  57.    
  58.     int stride[4] = {
  59.         FRAME_WIDTH,
  60.         FRAME_WIDTH / 2,
  61.         FRAME_WIDTH / 2,
  62.         0
  63.     };
  64.    
  65.     struct SwsContext *sws;
  66.    
  67.         // get the scale context
  68.     sws = sws_getContext(FRAME_WIDTH,       // src width
  69.                          FRAME_HEIGHT,      // src height
  70.                          PIX_FMT_RGB32,     // src pixel format ,
  71.                          FRAME_WIDTH,       // dest width
  72.                          FRAME_HEIGHT,      // dest height
  73.                          PIX_FMT_YUV420P,   // dest pix format
  74.                          SWS_BILINEAR,      // FLAGS
  75.                          NULL,  
  76.                          NULL,
  77.                          NULL);
  78.    
  79.     int sliceHeight = sws_scale(sws,
  80.                               rgb_src,
  81.                               rgb_stride,
  82.                               0,
  83.                               FRAME_HEIGHT,
  84.                               src,
  85.                               stride);
  86.    
  87.     if (sliceHeight <= 0)
  88.     {
  89.         NSLog(@"couldn't scale");
  90.         abort();
  91.     }
  92.    
  93.     sws_freeContext(sws);
  94.    
  95.     currentPicture->data[0] = src[0];
  96.     currentPicture->data[1] = src[1];
  97.     currentPicture->data[2] = src[2];
  98.     currentPicture->data[3] = src[3];
  99.    
  100.     currentPicture->linesize[0] = stride[0];
  101.     currentPicture->linesize[1] = stride[1];
  102.     currentPicture->linesize[2] = stride[2];
  103.     currentPicture->linesize[3] = stride[3];
  104.    
  105.     out_size = avcodec_encode_video(codecContext, encodeBuffer, frameSize, currentPicture);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement