libavformat/mpegtsenc.c:465: error: Null Dereference
  pointer `pcr_st` last assigned on line 387 could be null and is dereferenced at line 465, column 13.

libavformat/mpegtsenc.c:382:1: start of procedure mpegts_write_header()
380. }
381. 
382. static int mpegts_write_header(AVFormatContext *s)
     ^
383. {
384.     MpegTSWrite *ts = s->priv_data;

libavformat/mpegtsenc.c:384:5: 
382. static int mpegts_write_header(AVFormatContext *s)
383. {
384.     MpegTSWrite *ts = s->priv_data;
         ^
385.     MpegTSWriteStream *ts_st;
386.     MpegTSService *service;

libavformat/mpegtsenc.c:387:5: 
385.     MpegTSWriteStream *ts_st;
386.     MpegTSService *service;
387.     AVStream *st, *pcr_st = NULL;
         ^
388.     AVMetadataTag *title;
389.     int i;

libavformat/mpegtsenc.c:392:5: 
390.     const char *service_name;
391. 
392.     ts->tsid = DEFAULT_TSID;
         ^
393.     ts->onid = DEFAULT_ONID;
394.     /* allocate a single DVB service */

libavformat/mpegtsenc.c:393:5: 
391. 
392.     ts->tsid = DEFAULT_TSID;
393.     ts->onid = DEFAULT_ONID;
         ^
394.     /* allocate a single DVB service */
395.     title = av_metadata_get(s->metadata, "title", NULL, 0);

libavformat/mpegtsenc.c:395:5: Skipping av_metadata_get(): empty list of specs
393.     ts->onid = DEFAULT_ONID;
394.     /* allocate a single DVB service */
395.     title = av_metadata_get(s->metadata, "title", NULL, 0);
         ^
396.     service_name = title ? title->value : DEFAULT_SERVICE_NAME;
397.     service = mpegts_add_service(ts, DEFAULT_SID,

libavformat/mpegtsenc.c:396:20: Condition is true
394.     /* allocate a single DVB service */
395.     title = av_metadata_get(s->metadata, "title", NULL, 0);
396.     service_name = title ? title->value : DEFAULT_SERVICE_NAME;
                        ^
397.     service = mpegts_add_service(ts, DEFAULT_SID,
398.                                  DEFAULT_PROVIDER_NAME, service_name);

libavformat/mpegtsenc.c:396:5: 
394.     /* allocate a single DVB service */
395.     title = av_metadata_get(s->metadata, "title", NULL, 0);
396.     service_name = title ? title->value : DEFAULT_SERVICE_NAME;
         ^
397.     service = mpegts_add_service(ts, DEFAULT_SID,
398.                                  DEFAULT_PROVIDER_NAME, service_name);

libavformat/mpegtsenc.c:397:5: Skipping mpegts_add_service(): empty list of specs
395.     title = av_metadata_get(s->metadata, "title", NULL, 0);
396.     service_name = title ? title->value : DEFAULT_SERVICE_NAME;
397.     service = mpegts_add_service(ts, DEFAULT_SID,
         ^
398.                                  DEFAULT_PROVIDER_NAME, service_name);
399.     service->pmt.write_packet = section_write_packet;

libavformat/mpegtsenc.c:399:5: 
397.     service = mpegts_add_service(ts, DEFAULT_SID,
398.                                  DEFAULT_PROVIDER_NAME, service_name);
399.     service->pmt.write_packet = section_write_packet;
         ^
400.     service->pmt.opaque = s;
401.     service->pmt.cc = 15;

libavformat/mpegtsenc.c:400:5: 
398.                                  DEFAULT_PROVIDER_NAME, service_name);
399.     service->pmt.write_packet = section_write_packet;
400.     service->pmt.opaque = s;
         ^
401.     service->pmt.cc = 15;
402. 

libavformat/mpegtsenc.c:401:5: 
399.     service->pmt.write_packet = section_write_packet;
400.     service->pmt.opaque = s;
401.     service->pmt.cc = 15;
         ^
402. 
403.     ts->pat.pid = PAT_PID;

libavformat/mpegtsenc.c:403:5: 
401.     service->pmt.cc = 15;
402. 
403.     ts->pat.pid = PAT_PID;
         ^
404.     ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
405.     ts->pat.write_packet = section_write_packet;

libavformat/mpegtsenc.c:404:5: 
402. 
403.     ts->pat.pid = PAT_PID;
404.     ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
         ^
405.     ts->pat.write_packet = section_write_packet;
406.     ts->pat.opaque = s;

libavformat/mpegtsenc.c:405:5: 
403.     ts->pat.pid = PAT_PID;
404.     ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
405.     ts->pat.write_packet = section_write_packet;
         ^
406.     ts->pat.opaque = s;
407. 

libavformat/mpegtsenc.c:406:5: 
404.     ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
405.     ts->pat.write_packet = section_write_packet;
406.     ts->pat.opaque = s;
         ^
407. 
408.     ts->sdt.pid = SDT_PID;

libavformat/mpegtsenc.c:408:5: 
406.     ts->pat.opaque = s;
407. 
408.     ts->sdt.pid = SDT_PID;
         ^
409.     ts->sdt.cc = 15;
410.     ts->sdt.write_packet = section_write_packet;

libavformat/mpegtsenc.c:409:5: 
407. 
408.     ts->sdt.pid = SDT_PID;
409.     ts->sdt.cc = 15;
         ^
410.     ts->sdt.write_packet = section_write_packet;
411.     ts->sdt.opaque = s;

libavformat/mpegtsenc.c:410:5: 
408.     ts->sdt.pid = SDT_PID;
409.     ts->sdt.cc = 15;
410.     ts->sdt.write_packet = section_write_packet;
         ^
411.     ts->sdt.opaque = s;
412. 

libavformat/mpegtsenc.c:411:5: 
409.     ts->sdt.cc = 15;
410.     ts->sdt.write_packet = section_write_packet;
411.     ts->sdt.opaque = s;
         ^
412. 
413.     /* assign pids to each stream */

libavformat/mpegtsenc.c:414:9: 
412. 
413.     /* assign pids to each stream */
414.     for(i = 0;i < s->nb_streams; i++) {
             ^
415.         st = s->streams[i];
416.         ts_st = av_mallocz(sizeof(MpegTSWriteStream));

libavformat/mpegtsenc.c:414:15: Loop condition is false. Leaving loop
412. 
413.     /* assign pids to each stream */
414.     for(i = 0;i < s->nb_streams; i++) {
                   ^
415.         st = s->streams[i];
416.         ts_st = av_mallocz(sizeof(MpegTSWriteStream));

libavformat/mpegtsenc.c:444:9: Taking false branch
442. 
443.     /* if no video stream, use the first stream as PCR */
444.     if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
             ^
445.         pcr_st = s->streams[0];
446.         ts_st = pcr_st->priv_data;

libavformat/mpegtsenc.c:450:20: Condition is true
448.     }
449. 
450.     ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
                        ^
451. 
452.     if (ts->mux_rate > 1) {

libavformat/mpegtsenc.c:450:5: 
448.     }
449. 
450.     ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
         ^
451. 
452.     if (ts->mux_rate > 1) {

libavformat/mpegtsenc.c:452:9: Taking false branch
450.     ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
451. 
452.     if (ts->mux_rate > 1) {
             ^
453.         service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
454.             (TS_PACKET_SIZE * 8 * 1000);

libavformat/mpegtsenc.c:463:9: 
461.     } else {
462.         /* Arbitrary values, PAT/PMT could be written on key frames */
463.         ts->sdt_packet_period = 200;
             ^
464.         ts->pat_packet_period = 40;
465.         if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {

libavformat/mpegtsenc.c:464:9: 
462.         /* Arbitrary values, PAT/PMT could be written on key frames */
463.         ts->sdt_packet_period = 200;
464.         ts->pat_packet_period = 40;
             ^
465.         if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {
466.             if (!pcr_st->codec->frame_size) {

libavformat/mpegtsenc.c:465:13: 
463.         ts->sdt_packet_period = 200;
464.         ts->pat_packet_period = 40;
465.         if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {
                 ^
466.             if (!pcr_st->codec->frame_size) {
467.                 av_log(s, AV_LOG_WARNING, "frame size not set\n");

static int mpegts_write_header(AVFormatContext *s)
{
    MpegTSWrite *ts = s->priv_data;
    MpegTSWriteStream *ts_st;
    MpegTSService *service;
    AVStream *st, *pcr_st = NULL;
    AVMetadataTag *title;
    int i;
    const char *service_name;
    ts->tsid = DEFAULT_TSID;
    ts->onid = DEFAULT_ONID;
    title = av_metadata_get(s->metadata, "title", NULL, 0);
    service_name = title ? title->value : DEFAULT_SERVICE_NAME;
    service = mpegts_add_service(ts, DEFAULT_SID,
                                 DEFAULT_PROVIDER_NAME, service_name);
    service->pmt.write_packet = section_write_packet;
    service->pmt.opaque = s;
    service->pmt.cc = 15;
    ts->pat.pid = PAT_PID;
    ts->pat.cc = 15;
    ts->pat.write_packet = section_write_packet;
    ts->pat.opaque = s;
    ts->sdt.pid = SDT_PID;
    ts->sdt.cc = 15;
    ts->sdt.write_packet = section_write_packet;
    ts->sdt.opaque = s;
    for(i = 0;i < s->nb_streams; i++) {
        st = s->streams[i];
        ts_st = av_mallocz(sizeof(MpegTSWriteStream));
        if (!ts_st)
            goto fail;
        st->priv_data = ts_st;
        ts_st->service = service;
        ts_st->pid = DEFAULT_START_PID + i;
        ts_st->payload_pts = AV_NOPTS_VALUE;
        ts_st->payload_dts = AV_NOPTS_VALUE;
        ts_st->first_pts_check = 1;
        ts_st->cc = 15;
        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
            service->pcr_pid == 0x1fff) {
            service->pcr_pid = ts_st->pid;
            pcr_st = st;
        }
        if (st->codec->codec_id == CODEC_ID_AAC &&
            st->codec->extradata_size > 0) {
            ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
            if (!ts_st->adts)
                return AVERROR_NOMEM;
            if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
                                         st->codec->extradata_size) < 0)
                return -1;
        }
    }
    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
        pcr_st = s->streams[0];
        ts_st = pcr_st->priv_data;
        service->pcr_pid = ts_st->pid;
    }
    ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
    if (ts->mux_rate > 1) {
        service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
            (TS_PACKET_SIZE * 8 * 1000);
        ts->sdt_packet_period      = (ts->mux_rate * SDT_RETRANS_TIME) /
            (TS_PACKET_SIZE * 8 * 1000);
        ts->pat_packet_period      = (ts->mux_rate * PAT_RETRANS_TIME) /
            (TS_PACKET_SIZE * 8 * 1000);
        ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
    } else {
        ts->sdt_packet_period = 200;
        ts->pat_packet_period = 40;
        if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {
            if (!pcr_st->codec->frame_size) {
                av_log(s, AV_LOG_WARNING, "frame size not set\n");
                service->pcr_packet_period =
                    pcr_st->codec->sample_rate/(10*512);
            } else {
                service->pcr_packet_period =
                    pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
            }
        } else {
            service->pcr_packet_period =
                pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
        }
    }
    service->pcr_packet_count = service->pcr_packet_period;
    ts->pat_packet_count = ts->pat_packet_period-1;
    ts->sdt_packet_count = ts->sdt_packet_period-1;
    av_log(s, AV_LOG_INFO,
           "muxrate %d bps, pcr every %d pkts, "
           "sdt every %d, pat/pmt every %d pkts\n",
           ts->mux_rate, service->pcr_packet_period,
           ts->sdt_packet_period, ts->pat_packet_period);
    put_flush_packet(s->pb);
    return 0;
 fail:
    for(i = 0;i < s->nb_streams; i++) {
        st = s->streams[i];
        av_free(st->priv_data);
    }
    return -1;
}
['static int mpegts_write_header(AVFormatContext *s)\n{\n    MpegTSWrite *ts = s->priv_data;\n    MpegTSWriteStream *ts_st;\n    MpegTSService *service;\n    AVStream *st, *pcr_st = NULL;\n    AVMetadataTag *title;\n    int i;\n    const char *service_name;\n    ts->tsid = DEFAULT_TSID;\n    ts->onid = DEFAULT_ONID;\n    title = av_metadata_get(s->metadata, "title", NULL, 0);\n    service_name = title ? title->value : DEFAULT_SERVICE_NAME;\n    service = mpegts_add_service(ts, DEFAULT_SID,\n                                 DEFAULT_PROVIDER_NAME, service_name);\n    service->pmt.write_packet = section_write_packet;\n    service->pmt.opaque = s;\n    service->pmt.cc = 15;\n    ts->pat.pid = PAT_PID;\n    ts->pat.cc = 15;\n    ts->pat.write_packet = section_write_packet;\n    ts->pat.opaque = s;\n    ts->sdt.pid = SDT_PID;\n    ts->sdt.cc = 15;\n    ts->sdt.write_packet = section_write_packet;\n    ts->sdt.opaque = s;\n    for(i = 0;i < s->nb_streams; i++) {\n        st = s->streams[i];\n        ts_st = av_mallocz(sizeof(MpegTSWriteStream));\n        if (!ts_st)\n            goto fail;\n        st->priv_data = ts_st;\n        ts_st->service = service;\n        ts_st->pid = DEFAULT_START_PID + i;\n        ts_st->payload_pts = AV_NOPTS_VALUE;\n        ts_st->payload_dts = AV_NOPTS_VALUE;\n        ts_st->first_pts_check = 1;\n        ts_st->cc = 15;\n        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&\n            service->pcr_pid == 0x1fff) {\n            service->pcr_pid = ts_st->pid;\n            pcr_st = st;\n        }\n        if (st->codec->codec_id == CODEC_ID_AAC &&\n            st->codec->extradata_size > 0) {\n            ts_st->adts = av_mallocz(sizeof(*ts_st->adts));\n            if (!ts_st->adts)\n                return AVERROR_NOMEM;\n            if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,\n                                         st->codec->extradata_size) < 0)\n                return -1;\n        }\n    }\n    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {\n        pcr_st = s->streams[0];\n        ts_st = pcr_st->priv_data;\n        service->pcr_pid = ts_st->pid;\n    }\n    ts->mux_rate = s->mux_rate ? s->mux_rate : 1;\n    if (ts->mux_rate > 1) {\n        service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /\n            (TS_PACKET_SIZE * 8 * 1000);\n        ts->sdt_packet_period      = (ts->mux_rate * SDT_RETRANS_TIME) /\n            (TS_PACKET_SIZE * 8 * 1000);\n        ts->pat_packet_period      = (ts->mux_rate * PAT_RETRANS_TIME) /\n            (TS_PACKET_SIZE * 8 * 1000);\n        ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE);\n    } else {\n        ts->sdt_packet_period = 200;\n        ts->pat_packet_period = 40;\n        if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {\n            if (!pcr_st->codec->frame_size) {\n                av_log(s, AV_LOG_WARNING, "frame size not set\\n");\n                service->pcr_packet_period =\n                    pcr_st->codec->sample_rate/(10*512);\n            } else {\n                service->pcr_packet_period =\n                    pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);\n            }\n        } else {\n            service->pcr_packet_period =\n                pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);\n        }\n    }\n    service->pcr_packet_count = service->pcr_packet_period;\n    ts->pat_packet_count = ts->pat_packet_period-1;\n    ts->sdt_packet_count = ts->sdt_packet_period-1;\n    av_log(s, AV_LOG_INFO,\n           "muxrate %d bps, pcr every %d pkts, "\n           "sdt every %d, pat/pmt every %d pkts\\n",\n           ts->mux_rate, service->pcr_packet_period,\n           ts->sdt_packet_period, ts->pat_packet_period);\n    put_flush_packet(s->pb);\n    return 0;\n fail:\n    for(i = 0;i < s->nb_streams; i++) {\n        st = s->streams[i];\n        av_free(st->priv_data);\n    }\n    return -1;\n}']