Hi all,  <div><br></div><div>Just getting started with the libraries and in need of a bit of help.</div><div><br></div><div>I have been building up a sample based off of the decoding_encoding example along with some sample code using AVFormat.</div>
<div><br></div><div>Right now, I am running into issues which I am having a hard time finding the cause of.   Here is the error along with the backtrace:</div><div><br></div><div><div><div>Program received signal EXC_BAD_ACCESS, Could not access memory.</div>
<div>Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000090</div><div>0x00000001000870b5 in ff_mov_write_packet ()</div><div>(gdb) bt</div><div>#0  0x00000001000870b5 in ff_mov_write_packet ()</div><div>#1  0x00007fff8e58cb28 in szone_malloc_should_clear ()</div>
<div>#2  0x00007fff8e57f71a in malloc_zone_memalign ()</div><div>#3  0x00007fff8e57ff4e in posix_memalign ()</div><div>#4  0x000000010000f9b5 in av_strdup ()</div></div></div><div><br></div><div>Full source is below.</div>
<div><br></div><div>Thanks for any help.</div><div><br></div><div>Best,</div><div>shawn</div><div><br></div><div>--</div><div><br></div><div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div>#include <string.h></div>
<div>#include <math.h></div><div><br></div><div>#include <libavcodec/avcodec.h></div><div>#include <libavformat/avformat.h></div><div>#include <libswscale/swscale.h></div><div>#include <libavutil/avutil.h></div>
<div><br></div><div>#undef exit</div><div><br></div><div>#define STREAM_DURATION   5.0 /* 5 seconds stream duration */</div><div>#define STREAM_FRAME_RATE 25 /* 25 images/s */</div><div>#define STREAM_NB_FRAMES  ((int)(STREAM_DURATION * STREAM_FRAME_RATE))</div>
<div>#define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */</div><div><br></div><div>static int sws_flags = SWS_BICUBIC;</div><div><br></div><div>int main(int argc, char **argv)</div><div>{</div><div>    // Output file</div>
<div>    const char *outputfile;</div><div>    </div><div>    // Struct to hold the output format details, audio codec, video codec, extension</div><div>    // <a href="http://ffmpeg.org/doxygen/trunk/structAVOutputFormat.html">http://ffmpeg.org/doxygen/trunk/structAVOutputFormat.html</a></div>
<div>    AVOutputFormat *avOutputFormat; // Was *fmt</div><div>    </div><div>    // <a href="http://ffmpeg.org/doxygen/trunk/structAVFormatContext.html">http://ffmpeg.org/doxygen/trunk/structAVFormatContext.html</a></div>
<div>    AVFormatContext *avFormatContext; // was *oc</div><div>    </div><div>    // Individual AVStreams for audio and video</div><div>    // <a href="http://ffmpeg.org/doxygen/trunk/structAVStream.html">http://ffmpeg.org/doxygen/trunk/structAVStream.html</a></div>
<div>    AVStream *video_avstream; // was *video_st and *st</div><div>    </div><div><span class="Apple-tab-span" style="white-space:pre">      </span>// Audio and Video Timestamps (presentation time stamp)</div><div>    double video_pts;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>int codec_id = AV_CODEC_ID_H264;</div><div>    AVCodec *codec;</div><div>    AVCodecContext *c= NULL;</div>
<div>    int i, ret, x, y, got_output;</div><div>    int write_ret;</div><div>    //FILE *f;</div><div>    AVFrame *frame;</div><div>    AVPacket pkt;</div><div>    uint8_t endcode[] = { 0, 0, 1, 0xb7 };</div><div><br></div>
<div>    printf("Encode video file\n");</div><div><br></div><div>    /* register all the codecs */</div><div>    //avcodec_register_all();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>// Intialize libavcodec - register all codecs and formats??????????</div>
<div>    av_register_all();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>// Set inpput filename and output filename</div><div>    outputfile = "outtestmov.mp4";</div><div>
<br></div><div>    // auto detect the output format from the name. default is mpeg.</div><div>    avOutputFormat = av_guess_format(NULL, outputfile, NULL);</div><div>    if (!avOutputFormat) {</div><div>        printf("Could not deduce output format from file extension: using MPEG.\n");</div>
<div>        avOutputFormat = av_guess_format("mpeg", NULL, NULL);</div><div>    }</div><div>    if (!avOutputFormat) {</div><div>        fprintf(stderr, "Could not find suitable output format\n");</div>
<div>        exit(1);</div><div>    }</div><div>    </div><div>    avOutputFormat->video_codec = codec_id;</div><div><br></div><div>    /// allocate the output media context, avFormatContext</div><div>    avFormatContext = avformat_alloc_context();</div>
<div>    if (!avFormatContext) {</div><div>        fprintf(stderr, "Memory error\n");</div><div>        exit(1);</div><div>    }</div><div>    // Set the output format of the newly allocated avFormatContext to our avOutputFormat</div>
<div>    avFormatContext->oformat = avOutputFormat;</div><div>    </div><div>    // Set the output filename to the outputfile</div><div>    snprintf(avFormatContext->filename, sizeof(avFormatContext->filename), "%s", outputfile);</div>
<div><br></div><div>    /* find the mpeg1 video encoder */</div><div>    //codec = avcodec_find_encoder(codec_id);</div><div>    codec = avcodec_find_encoder(avOutputFormat->video_codec);</div><div>    if (!codec) {</div>
<div>        fprintf(stderr, "Codec not found\n");</div><div>        exit(1);</div><div>    }</div><div><br></div><div>    // add the video stream using the default format codec and initialize the codecs</div><div>
    video_avstream = avformat_new_stream(avFormatContext, codec);</div><div>    if (!video_avstream) {</div><div>        fprintf(stderr, "Could not alloc stream\n");</div><div>        exit(1);</div><div>    }</div>
<div><br></div><div>    if (video_avstream->codec == NULL) {</div><div>    <span class="Apple-tab-span" style="white-space:pre">       </span>fprintf(stderr, "AVStream codec is NULL\n");</div><div>    <span class="Apple-tab-span" style="white-space:pre">   </span>exit(1);</div>
<div>    }</div><div><br></div><div>    //c = avcodec_alloc_context3(codec);</div><div>    c = video_avstream->codec;</div><div>    //c->codec_id = avOutputFormat->video_codec;</div><div>    if (!c) {</div><div>        fprintf(stderr, "Could not allocate video codec context\n");</div>
<div>        exit(1);</div><div>    }</div><div><br></div><div>    /* put sample parameters */</div><div>    c->bit_rate = 400000;</div><div>    /* resolution must be a multiple of two */</div><div>    c->width = 352;</div>
<div>    c->height = 288;</div><div>    /* frames per second */</div><div>    c->time_base= (AVRational){1,25};</div><div>    c->gop_size = 10; /* emit one intra frame every ten frames */</div><div>    c->max_b_frames=1;</div>
<div>    c->pix_fmt = AV_PIX_FMT_YUV420P;</div><div><br></div><div>    if(codec_id == AV_CODEC_ID_H264)</div><div>        av_opt_set(c->priv_data, "preset", "slow", 0);</div><div><br></div><div>    /* open it */</div>
<div>    if (avcodec_open2(c, codec, NULL) < 0) {</div><div>        fprintf(stderr, "Could not open codec\n");</div><div>        exit(1);</div><div>    }</div><div><span class="Apple-tab-span" style="white-space:pre">  </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>//video_avstream->codec = c;</div><div><span class="Apple-tab-span" style="white-space:pre">      </span></div><div>    frame = avcodec_alloc_frame();</div><div>
    if (!frame) {</div><div>        fprintf(stderr, "Could not allocate video frame\n");</div><div>        exit(1);</div><div>    }</div><div>    frame->format = c->pix_fmt;</div><div>    frame->width  = c->width;</div>
<div>    frame->height = c->height;</div><div><br></div><div>    /* the image can be allocated by any means and av_image_alloc() is</div><div>     * just the most convenient way if av_malloc() is to be used */</div>
<div>    ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,</div><div>                         c->pix_fmt, 32);</div><div>    if (ret < 0) {</div><div>        fprintf(stderr, "Could not allocate raw picture buffer\n");</div>
<div>        exit(1);</div><div>    }</div><div><br></div><div>    // some formats want stream headers to be separate</div><div>    if(avFormatContext->oformat->flags & AVFMT_GLOBALHEADER)</div><div>        c->flags |= CODEC_FLAG_GLOBAL_HEADER;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>avformat_write_header(avFormatContext, NULL);</div><div><br></div><div>    /* encode 1 second of video */</div><div>    for(i=0;i<25;i++) {</div>
<div>        av_init_packet(&pkt);</div><div>        pkt.data = NULL;    // packet data will be allocated by the encoder</div><div>        pkt.size = 0;</div><div><br></div><div>        fflush(stdout);</div><div>        /* prepare a dummy image */</div>
<div>        /* Y */</div><div>        for(y=0;y<c->height;y++) {</div><div>            for(x=0;x<c->width;x++) {</div><div>                frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;</div>
<div>            }</div><div>        }</div><div><br></div><div>        /* Cb and Cr */</div><div>        for(y=0;y<c->height/2;y++) {</div><div>            for(x=0;x<c->width/2;x++) {</div><div>                frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;</div>
<div>                frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;</div><div>            }</div><div>        }</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>frame->pts = i;</div>
<div><br></div><div>        /* encode the image */</div><div>        ret = avcodec_encode_video2(c, &pkt, frame, &got_output);</div><div>        if (ret < 0) {</div><div>            fprintf(stderr, "Error encoding frame\n");</div>
<div>            exit(1);</div><div>        }</div><div><br></div><div>        if (got_output) {            </div><div>            /* write the compressed frame in the media file */</div><div>            write_ret = av_write_frame(avFormatContext, &pkt);</div>
<div>            if (write_ret < 0) {</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>            fprintf(stderr, "Error writing frame\n");</div><div>    <span class="Apple-tab-span" style="white-space:pre">  </span>        exit(1);</div>
<div>            }</div><div>            </div><div>            av_free_packet(&pkt);</div><div>        } else {</div><div>        <span class="Apple-tab-span" style="white-space:pre">    </span>printf("got_output false: %d", i);</div>
<div>        }</div><div>    }</div><div><br></div><div>    /* write the trailer, if any.  the trailer must be written</div><div>     * before you close the CodecContexts open when you wrote the</div><div>     * header; otherwise write_trailer may try to use memory that</div>
<div>     * was freed on av_codec_close() */</div><div>    av_write_trailer(avFormatContext);</div><div><br></div><div>    avcodec_close(c);</div><div>    av_free(c);</div><div>    av_freep(&frame->data[0]);</div><div>
    avcodec_free_frame(&frame);</div><div><br></div><div>    /* free the streams */</div><div>    for(i = 0; i < avFormatContext->nb_streams; i++) {</div><div>        av_freep(&avFormatContext->streams[i]->codec);</div>
<div>        av_freep(&avFormatContext->streams[i]);</div><div>    }</div><div><br></div><div>    if (!(avOutputFormat->flags & AVFMT_NOFILE)) {</div><div>        /* close the output file */</div><div>        avio_close(avFormatContext->pb);</div>
<div>    }</div><div><br></div><div>    /* free the stream */</div><div>    av_free(avFormatContext);</div><div>    </div><div>    printf("\n");</div><div>    return 0;</div><div>}</div></div><div><br></div>