Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // buffer allocation code omitted
  2.  
  3. codec = avcodec_find_encoder(CODEC_ID_MSMPEG4V1);
  4.  
  5. if (!codec)
  6. {
  7.     NSLog("Couldn't init codec");
  8.     abort();
  9. }
  10.  
  11. codecContext = avcodec_alloc_context();
  12. codecContext->bit_rate = 384000;
  13. codecContext->width = FRAME_WIDTH;
  14. codecContext->height = FRAME_HEIGHT;
  15. codecContext->time_base = (AVRational) {
  16.     1,
  17.     FPS
  18. };
  19.  
  20. codecContext->pix_fmt = PIX_FMT_YUV420P;
  21.  
  22. int codec_open_result = avcodec_open(codecContext, codec);
  23.  
  24. if (codec_open_result < 0)
  25. {
  26.     NSLog(@"Couldn't open codec");
  27.     abort();
  28. }
  29.  
  30. /* previously defined
  31. #define FRAME_WIDTH  320
  32. #define FRAME_HEIGHT 480
  33. */
  34.  
  35. uint8_t *rgb_data = buffer + (frameSize * ((*totalCaptured - 1) % BUFFER_FRAME_MAX_COUNT));
  36.  
  37. uint8_t *rgb_src[3] = {
  38.     rgb_data,
  39.     NULL,
  40.     NULL
  41. };
  42.  
  43. int rgb_stride[3] = {
  44.     4 * FRAME_WIDTH,
  45.     0,
  46.     0
  47. };
  48.  
  49. uint8_t *src[4] = {
  50.     scaleBuffer,
  51.     scaleBuffer + FRAME_WIDTH * FRAME_HEIGHT,
  52.     scaleBuffer + ( FRAME_WIDTH * FRAME_HEIGHT ) + ( FRAME_WIDTH * FRAME_HEIGHT / 4 ),
  53.     NULL
  54. };
  55.  
  56. int stride[4] = {
  57.     FRAME_WIDTH,
  58.     FRAME_WIDTH / 2,
  59.     FRAME_WIDTH / 2,
  60.     0
  61. };
  62.  
  63. struct SwsContext *sws;
  64.  
  65. sws = sws_getContext(FRAME_WIDTH,       // src width
  66.                      FRAME_HEIGHT,      // src height
  67.                      PIX_FMT_RGB32,     // src pixel format ,
  68.                      FRAME_WIDTH,       // dest width
  69.                      FRAME_HEIGHT,      // dest height
  70.                      PIX_FMT_YUV420P,   // dest pix format
  71.                      SWS_BILINEAR,      // FLAGS
  72.                      NULL,  
  73.                      NULL,
  74.                      NULL);
  75.  
  76.  
  77. int sliceHeight = sws_scale(sws,
  78.                             rgb_src,
  79.                             rgb_stride,
  80.                             0,
  81.                             FRAME_HEIGHT,
  82.                             src,
  83.                             stride);
  84.                            
  85. // the "sliceHeight" variable evaluates to 480 in GDB at this point, which seems correct
  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. int out_size = avcodec_encode_video(codecContext, encodeBuffer, frameSize, currentPicture);
  106.  
  107. // the "out_size" variable evaluates to 4 in GDB, which seems incorrect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement