libavcodec/mpegvideo_enc.c:1096: error: Null Dereference
  pointer `c` last assigned on line 1075 could be null and is dereferenced at line 1096, column 5.

libavcodec/mpegvideo_enc.c:1072:1: start of procedure estimate_best_b_count()
1070. }
1071. 
1072. static int estimate_best_b_count(MpegEncContext *s)
      ^
1073. {
1074.     AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);

libavcodec/mpegvideo_enc.c:1074:5: Skipping avcodec_find_encoder(): empty list of specs
1072. static int estimate_best_b_count(MpegEncContext *s)
1073. {
1074.     AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
          ^
1075.     AVCodecContext *c = avcodec_alloc_context3(NULL);
1076.     AVFrame input[FF_MAX_B_FRAMES + 2];

libavcodec/mpegvideo_enc.c:1075:5: 
1073. {
1074.     AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
1075.     AVCodecContext *c = avcodec_alloc_context3(NULL);
          ^
1076.     AVFrame input[FF_MAX_B_FRAMES + 2];
1077.     const int scale = s->avctx->brd_scale;

libavcodec/options.c:123:1: start of procedure avcodec_alloc_context3()
  121. }
  122. 
  123. AVCodecContext *avcodec_alloc_context3(AVCodec *codec){
       ^
  124.     AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  125. 

libavcodec/options.c:124:5: 
  122. 
  123. AVCodecContext *avcodec_alloc_context3(AVCodec *codec){
  124.     AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
           ^
  125. 
  126.     if(avctx==NULL) return NULL;

libavutil/mem.c:64:1: start of procedure av_malloc()
    62.    linker will do it automatically. */
    63. 
    64. void *av_malloc(size_t size)
        ^
    65. {
    66.     void *ptr = NULL;

libavutil/mem.c:66:5: 
    64. void *av_malloc(size_t size)
    65. {
    66.     void *ptr = NULL;
            ^
    67. #if CONFIG_MEMALIGN_HACK
    68.     long diff;

libavutil/mem.c:71:5: 
    69. #endif
    70. 
    71.     assert(size);
            ^
    72. 
    73.     /* let's disallow possible ambiguous cases */

libavutil/mem.c:74:9: Taking false branch
    72. 
    73.     /* let's disallow possible ambiguous cases */
    74.     if (size > (INT_MAX-32) || !size)
                ^
    75.         return NULL;
    76. 

libavutil/mem.c:74:33: Taking false branch
    72. 
    73.     /* let's disallow possible ambiguous cases */
    74.     if (size > (INT_MAX-32) || !size)
                                        ^
    75.         return NULL;
    76. 

libavutil/mem.c:85:9: Taking false branch
    83.     ((char*)ptr)[-1]= diff;
    84. #elif HAVE_POSIX_MEMALIGN
    85.     if (posix_memalign(&ptr,32,size))
                ^
    86.         ptr = NULL;
    87. #elif HAVE_MEMALIGN

libavutil/mem.c:116:5: 
    114.     ptr = malloc(size);
    115. #endif
    116.     return ptr;
             ^
    117. }
    118. 

libavutil/mem.c:117:1: return from a call to av_malloc
    115. #endif
    116.     return ptr;
    117. }
         ^
    118. 
    119. void *av_realloc(void *ptr, size_t size)

libavcodec/options.c:126:8: Taking true branch
  124.     AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  125. 
  126.     if(avctx==NULL) return NULL;
              ^
  127. 
  128.     if(avcodec_get_context_defaults3(avctx, codec) < 0){

libavcodec/options.c:126:21: 
  124.     AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
  125. 
  126.     if(avctx==NULL) return NULL;
                           ^
  127. 
  128.     if(avcodec_get_context_defaults3(avctx, codec) < 0){

libavcodec/options.c:134:1: return from a call to avcodec_alloc_context3
  132. 
  133.     return avctx;
  134. }
       ^
  135. 
  136. int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)

libavcodec/mpegvideo_enc.c:1077:5: 
1075.     AVCodecContext *c = avcodec_alloc_context3(NULL);
1076.     AVFrame input[FF_MAX_B_FRAMES + 2];
1077.     const int scale = s->avctx->brd_scale;
          ^
1078.     int i, j, out_size, p_lambda, b_lambda, lambda2;
1079.     int outbuf_size  = s->width * s->height; // FIXME

libavcodec/mpegvideo_enc.c:1079:5: 
1077.     const int scale = s->avctx->brd_scale;
1078.     int i, j, out_size, p_lambda, b_lambda, lambda2;
1079.     int outbuf_size  = s->width * s->height; // FIXME
          ^
1080.     uint8_t *outbuf  = av_malloc(outbuf_size);
1081.     int64_t best_rd  = INT64_MAX;

libavcodec/mpegvideo_enc.c:1080:5: 
1078.     int i, j, out_size, p_lambda, b_lambda, lambda2;
1079.     int outbuf_size  = s->width * s->height; // FIXME
1080.     uint8_t *outbuf  = av_malloc(outbuf_size);
          ^
1081.     int64_t best_rd  = INT64_MAX;
1082.     int best_b_count = -1;

libavutil/mem.c:64:1: start of procedure av_malloc()
  62.    linker will do it automatically. */
  63. 
  64. void *av_malloc(size_t size)
      ^
  65. {
  66.     void *ptr = NULL;

libavutil/mem.c:66:5: 
  64. void *av_malloc(size_t size)
  65. {
  66.     void *ptr = NULL;
          ^
  67. #if CONFIG_MEMALIGN_HACK
  68.     long diff;

libavutil/mem.c:71:5: 
  69. #endif
  70. 
  71.     assert(size);
          ^
  72. 
  73.     /* let's disallow possible ambiguous cases */

libavutil/mem.c:74:9: Taking false branch
  72. 
  73.     /* let's disallow possible ambiguous cases */
  74.     if (size > (INT_MAX-32) || !size)
              ^
  75.         return NULL;
  76. 

libavutil/mem.c:74:33: Taking false branch
  72. 
  73.     /* let's disallow possible ambiguous cases */
  74.     if (size > (INT_MAX-32) || !size)
                                      ^
  75.         return NULL;
  76. 

libavutil/mem.c:85:9: Taking false branch
  83.     ((char*)ptr)[-1]= diff;
  84. #elif HAVE_POSIX_MEMALIGN
  85.     if (posix_memalign(&ptr,32,size))
              ^
  86.         ptr = NULL;
  87. #elif HAVE_MEMALIGN

libavutil/mem.c:116:5: 
  114.     ptr = malloc(size);
  115. #endif
  116.     return ptr;
           ^
  117. }
  118. 

libavutil/mem.c:117:1: return from a call to av_malloc
  115. #endif
  116.     return ptr;
  117. }
       ^
  118. 
  119. void *av_realloc(void *ptr, size_t size)

libavcodec/mpegvideo_enc.c:1081:5: 
1079.     int outbuf_size  = s->width * s->height; // FIXME
1080.     uint8_t *outbuf  = av_malloc(outbuf_size);
1081.     int64_t best_rd  = INT64_MAX;
          ^
1082.     int best_b_count = -1;
1083. 

libavcodec/mpegvideo_enc.c:1082:5: 
1080.     uint8_t *outbuf  = av_malloc(outbuf_size);
1081.     int64_t best_rd  = INT64_MAX;
1082.     int best_b_count = -1;
          ^
1083. 
1084.     assert(scale >= 0 && scale <= 3);

libavcodec/mpegvideo_enc.c:1084:5: 
1082.     int best_b_count = -1;
1083. 
1084.     assert(scale >= 0 && scale <= 3);
          ^
1085. 
1086.     //emms_c();

libavcodec/mpegvideo_enc.c:1088:5: 
1086.     //emms_c();
1087.     //s->next_picture_ptr->quality;
1088.     p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
          ^
1089.     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1090.     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];

libavcodec/mpegvideo_enc.c:1090:5: 
1088.     p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1089.     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1090.     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
          ^
1091.     if (!b_lambda) // FIXME we should do this somewhere else
1092.         b_lambda = p_lambda;

libavcodec/mpegvideo_enc.c:1091:10: Taking false branch
1089.     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1090.     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1091.     if (!b_lambda) // FIXME we should do this somewhere else
               ^
1092.         b_lambda = p_lambda;
1093.     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>

libavcodec/mpegvideo_enc.c:1093:5: 
1091.     if (!b_lambda) // FIXME we should do this somewhere else
1092.         b_lambda = p_lambda;
1093.     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
          ^
1094.                FF_LAMBDA_SHIFT;
1095. 

libavcodec/mpegvideo_enc.c:1096:5: 
1094.                FF_LAMBDA_SHIFT;
1095. 
1096.     c->width        = s->width  >> scale;
          ^
1097.     c->height       = s->height >> scale;
1098.     c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |

static int estimate_best_b_count(MpegEncContext *s)
{
    AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
    AVCodecContext *c = avcodec_alloc_context3(NULL);
    AVFrame input[FF_MAX_B_FRAMES + 2];
    const int scale = s->avctx->brd_scale;
    int i, j, out_size, p_lambda, b_lambda, lambda2;
    int outbuf_size  = s->width * s->height;
    uint8_t *outbuf  = av_malloc(outbuf_size);
    int64_t best_rd  = INT64_MAX;
    int best_b_count = -1;
    assert(scale >= 0 && scale <= 3);
    p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
    b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
    if (!b_lambda)
        b_lambda = p_lambda;
    lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
               FF_LAMBDA_SHIFT;
    c->width        = s->width  >> scale;
    c->height       = s->height >> scale;
    c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
                      CODEC_FLAG_INPUT_PRESERVED  ;
    c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;
    c->mb_decision  = s->avctx->mb_decision;
    c->me_cmp       = s->avctx->me_cmp;
    c->mb_cmp       = s->avctx->mb_cmp;
    c->me_sub_cmp   = s->avctx->me_sub_cmp;
    c->pix_fmt      = PIX_FMT_YUV420P;
    c->time_base    = s->avctx->time_base;
    c->max_b_frames = s->max_b_frames;
    if (avcodec_open2(c, codec, NULL) < 0)
        return -1;
    for (i = 0; i < s->max_b_frames + 2; i++) {
        int ysize = c->width * c->height;
        int csize = (c->width / 2) * (c->height / 2);
        Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
                                                s->next_picture_ptr;
        avcodec_get_frame_defaults(&input[i]);
        input[i].data[0]     = av_malloc(ysize + 2 * csize);
        input[i].data[1]     = input[i].data[0] + ysize;
        input[i].data[2]     = input[i].data[1] + csize;
        input[i].linesize[0] = c->width;
        input[i].linesize[1] =
        input[i].linesize[2] = c->width / 2;
        if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
            pre_input = *pre_input_ptr;
            if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
                pre_input.f.data[0] += INPLACE_OFFSET;
                pre_input.f.data[1] += INPLACE_OFFSET;
                pre_input.f.data[2] += INPLACE_OFFSET;
            }
            s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
                                 pre_input.f.data[0], pre_input.f.linesize[0],
                                 c->width,      c->height);
            s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
                                 pre_input.f.data[1], pre_input.f.linesize[1],
                                 c->width >> 1, c->height >> 1);
            s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
                                 pre_input.f.data[2], pre_input.f.linesize[2],
                                 c->width >> 1, c->height >> 1);
        }
    }
    for (j = 0; j < s->max_b_frames + 1; j++) {
        int64_t rd = 0;
        if (!s->input_picture[j])
            break;
        c->error[0] = c->error[1] = c->error[2] = 0;
        input[0].pict_type = AV_PICTURE_TYPE_I;
        input[0].quality   = 1 * FF_QP2LAMBDA;
        out_size           = avcodec_encode_video(c, outbuf,
                                                  outbuf_size, &input[0]);
        for (i = 0; i < s->max_b_frames + 1; i++) {
            int is_p = i % (j + 1) == j || i == s->max_b_frames;
            input[i + 1].pict_type = is_p ?
                                     AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
            input[i + 1].quality   = is_p ? p_lambda : b_lambda;
            out_size = avcodec_encode_video(c, outbuf, outbuf_size,
                                            &input[i + 1]);
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
        }
        while (out_size) {
            out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
        }
        rd += c->error[0] + c->error[1] + c->error[2];
        if (rd < best_rd) {
            best_rd = rd;
            best_b_count = j;
        }
    }
    av_freep(&outbuf);
    avcodec_close(c);
    av_freep(&c);
    for (i = 0; i < s->max_b_frames + 2; i++) {
        av_freep(&input[i].data[0]);
    }
    return best_b_count;
}
['static int estimate_best_b_count(MpegEncContext *s)\n{\n    AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);\n    AVCodecContext *c = avcodec_alloc_context3(NULL);\n    AVFrame input[FF_MAX_B_FRAMES + 2];\n    const int scale = s->avctx->brd_scale;\n    int i, j, out_size, p_lambda, b_lambda, lambda2;\n    int outbuf_size  = s->width * s->height;\n    uint8_t *outbuf  = av_malloc(outbuf_size);\n    int64_t best_rd  = INT64_MAX;\n    int best_b_count = -1;\n    assert(scale >= 0 && scale <= 3);\n    p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];\n    b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];\n    if (!b_lambda)\n        b_lambda = p_lambda;\n    lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>\n               FF_LAMBDA_SHIFT;\n    c->width        = s->width  >> scale;\n    c->height       = s->height >> scale;\n    c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |\n                      CODEC_FLAG_INPUT_PRESERVED  ;\n    c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;\n    c->mb_decision  = s->avctx->mb_decision;\n    c->me_cmp       = s->avctx->me_cmp;\n    c->mb_cmp       = s->avctx->mb_cmp;\n    c->me_sub_cmp   = s->avctx->me_sub_cmp;\n    c->pix_fmt      = PIX_FMT_YUV420P;\n    c->time_base    = s->avctx->time_base;\n    c->max_b_frames = s->max_b_frames;\n    if (avcodec_open2(c, codec, NULL) < 0)\n        return -1;\n    for (i = 0; i < s->max_b_frames + 2; i++) {\n        int ysize = c->width * c->height;\n        int csize = (c->width / 2) * (c->height / 2);\n        Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :\n                                                s->next_picture_ptr;\n        avcodec_get_frame_defaults(&input[i]);\n        input[i].data[0]     = av_malloc(ysize + 2 * csize);\n        input[i].data[1]     = input[i].data[0] + ysize;\n        input[i].data[2]     = input[i].data[1] + csize;\n        input[i].linesize[0] = c->width;\n        input[i].linesize[1] =\n        input[i].linesize[2] = c->width / 2;\n        if (pre_input_ptr && (!i || s->input_picture[i - 1])) {\n            pre_input = *pre_input_ptr;\n            if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {\n                pre_input.f.data[0] += INPLACE_OFFSET;\n                pre_input.f.data[1] += INPLACE_OFFSET;\n                pre_input.f.data[2] += INPLACE_OFFSET;\n            }\n            s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],\n                                 pre_input.f.data[0], pre_input.f.linesize[0],\n                                 c->width,      c->height);\n            s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],\n                                 pre_input.f.data[1], pre_input.f.linesize[1],\n                                 c->width >> 1, c->height >> 1);\n            s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],\n                                 pre_input.f.data[2], pre_input.f.linesize[2],\n                                 c->width >> 1, c->height >> 1);\n        }\n    }\n    for (j = 0; j < s->max_b_frames + 1; j++) {\n        int64_t rd = 0;\n        if (!s->input_picture[j])\n            break;\n        c->error[0] = c->error[1] = c->error[2] = 0;\n        input[0].pict_type = AV_PICTURE_TYPE_I;\n        input[0].quality   = 1 * FF_QP2LAMBDA;\n        out_size           = avcodec_encode_video(c, outbuf,\n                                                  outbuf_size, &input[0]);\n        for (i = 0; i < s->max_b_frames + 1; i++) {\n            int is_p = i % (j + 1) == j || i == s->max_b_frames;\n            input[i + 1].pict_type = is_p ?\n                                     AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;\n            input[i + 1].quality   = is_p ? p_lambda : b_lambda;\n            out_size = avcodec_encode_video(c, outbuf, outbuf_size,\n                                            &input[i + 1]);\n            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n        }\n        while (out_size) {\n            out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);\n            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n        }\n        rd += c->error[0] + c->error[1] + c->error[2];\n        if (rd < best_rd) {\n            best_rd = rd;\n            best_b_count = j;\n        }\n    }\n    av_freep(&outbuf);\n    avcodec_close(c);\n    av_freep(&c);\n    for (i = 0; i < s->max_b_frames + 2; i++) {\n        av_freep(&input[i].data[0]);\n    }\n    return best_b_count;\n}', 'AVCodecContext *avcodec_alloc_context3(AVCodec *codec){\n    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n    if(avctx==NULL) return NULL;\n    if(avcodec_get_context_defaults3(avctx, codec) < 0){\n        av_free(avctx);\n        return NULL;\n    }\n    return avctx;\n}', 'void *av_malloc(size_t size)\n{\n    void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n    long diff;\n#endif\n    assert(size);\n    if (size > (INT_MAX-32) || !size)\n        return NULL;\n#if CONFIG_MEMALIGN_HACK\n    ptr = malloc(size+32);\n    if(!ptr)\n        return ptr;\n    diff= ((-(long)ptr - 1)&31) + 1;\n    ptr = (char*)ptr + diff;\n    ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n    if (posix_memalign(&ptr,32,size))\n        ptr = NULL;\n#elif HAVE_MEMALIGN\n    ptr = memalign(32,size);\n#else\n    ptr = malloc(size);\n#endif\n    return ptr;\n}']