title: Understanding FFMPEG Video Encoding
body: <p>Got this from the encoding example in ffmpeg. I can somewhat follow the authors example for audio encoding, but I find myself befuddled looking at the C code (I commented in block numbers to help me reference what I'm talking about)...</p>
  
  <pre><code>static void video_encode_example(const char *filename)
  {
  AVCodec *codec;
  AVCodecContext *c= NULL;
  int i, out_size, size, x, y, outbuf_size;
  FILE *f;
  AVFrame *picture;
  uint8_t *outbuf, *picture_buf;              //BLOCK ONE
  printf("Video encoding\n");
  
  /* find the mpeg1 video encoder */
  codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
  if (!codec) {
      fprintf(stderr, "codec not found\n");
      exit(1);                                //BLOCK TWO
  }
  
  c= avcodec_alloc_context();
  picture= avcodec_alloc_frame();
  /* put sample parameters */
  c-&gt;bit_rate = 400000;
  /* resolution must be a multiple of two */
  c-&gt;width = 352;
  c-&gt;height = 288;
  /* frames per second */
  c-&gt;time_base= (AVRational){1,25};
  c-&gt;gop_size = 10; /* emit one intra frame every ten frames */
  c-&gt;max_b_frames=1;
  c-&gt;pix_fmt = PIX_FMT_YUV420P;                   //BLOCK THREE
  
  /* open it */
  if (avcodec_open(c, codec) &lt; 0) {
      fprintf(stderr, "could not open codec\n");
      exit(1);
  }
  f = fopen(filename, "wb");
  if (!f) {
      fprintf(stderr, "could not open %s\n", filename);
      exit(1);
  }                                               //BLOCK FOUR
  
  /* alloc image and output buffer */
  outbuf_size = 100000;
  outbuf = malloc(outbuf_size);
  size = c-&gt;width * c-&gt;height;
  picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
  picture-&gt;data[0] = picture_buf;
  picture-&gt;data[1] = picture-&gt;data[0] + size;
  picture-&gt;data[2] = picture-&gt;data[1] + size / 4;
  picture-&gt;linesize[0] = c-&gt;width;
  picture-&gt;linesize[1] = c-&gt;width / 2;
  picture-&gt;linesize[2] = c-&gt;width / 2;              //BLOCK FIVE
  
  /* encode 1 second of video */
  for(i=0;i&lt;25;i++) {
      fflush(stdout);
      /* prepare a dummy image */
      /* Y */
      for(y=0;y&lt;c-&gt;height;y++) {
          for(x=0;x&lt;c-&gt;width;x++) {
              picture-&gt;data[0][y * picture-&gt;linesize[0] + x] = x + y + i * 3;
          }
      }                                            //BLOCK SIX
  
      /* Cb and Cr */
      for(y=0;y&lt;c-&gt;height/2;y++) {
          for(x=0;x&lt;c-&gt;width/2;x++) {
              picture-&gt;data[1][y * picture-&gt;linesize[1] + x] = 128 + y + i * 2;
              picture-&gt;data[2][y * picture-&gt;linesize[2] + x] = 64 + x + i * 5;
          }
      }                                           //BLOCK SEVEN
  
      /* encode the image */
      out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
      printf("encoding frame %3d (size=%5d)\n", i, out_size);
      fwrite(outbuf, 1, out_size, f);
  }                                              //BLOCK EIGHT
  
  /* get the delayed frames */
  for(; out_size; i++) {
      fflush(stdout);
      out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
      printf("write frame %3d (size=%5d)\n", i, out_size);
      fwrite(outbuf, 1, out_size, f);
  }                                             //BLOCK NINE
  
  /* add sequence end code to have a real mpeg file */
  outbuf[0] = 0x00;
  outbuf[1] = 0x00;
  outbuf[2] = 0x01;
  outbuf[3] = 0xb7;
  fwrite(outbuf, 1, 4, f);
  fclose(f);
  free(picture_buf);
  free(outbuf);
  avcodec_close(c);
  av_free(c);
  av_free(picture);
  }                                            //BLOCK TEN
  </code></pre>
  
  <p>Here's what I can get from the authors code block by block...</p>
  
  <p>BLOCK ONE: Initializing Variables and pointers. I couldn't find the AVFrame struct yet in the ffmpeg source code so I don't know what its referencing</p>
  
  <p>BLOCK TWO: Uses a codec from the file, if not found close. </p>
  
  <p>BLOCK THREE: Sets sample video parameters. Only thing I don't really get is gop size. I read about intra frames and I still don't get what they are.</p>
  
  <p>BLOCK FOUR: Open the file for writing...</p>
  
  <p>BLOCK FIVE: Here's where they really start losing me. Part is probably because I don't know exactly what AVFrame is, but why do they only use 3/2 of the image size?</p>
  
  <p>BLOCK SIX &amp; SEVEN: I don't understand what they are trying to accomplish with this math.</p>
  
  <p>BLOCK EIGHT: It looks like the avcodec function does all the work here, not concerned with that for the time being..</p>
  
  <p>BLOCK NINE: Since it's outside the 25 frame for loop I assume it gets the leftover frames?</p>
  
  <p>BLOCK TEN: Close, free mem, etc...</p>
  
  <p>I know this is a large block of code to be confused with, any input would be helpful. I got put in over my head at work. Thanks in advance SO.</p>