On Sat, Dec 29, 2012 at 10:50 AM, Shawn Van Every <span dir="ltr"><<a href="mailto:savanevery@gmail.com" target="_blank">savanevery@gmail.com</a>></span> wrote:<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="gmail_quote"><div class="im"><div><br></div><div>Program received signal EXC_BAD_ACCESS, Could not access memory.</div><div>Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000090</div>
</div><div>0x0000000100090095 in mov_write_header (s=0x7fff5fbff410) at movenc.c:3499</div><div>3499<span style="white-space:pre-wrap"> </span> if (!s->pb->seekable &&</div><div><br></div><div>
It looks like I either haven't instantiated AVFormatContext (s in movenc.c:3499) or AVIOContext (s->pb in movenc.c 3499) correctly. Unfortunately, I haven't seen AVIOContext used in any of the examples.</div>
<div><br></div><div>Thanks</div></div></blockquote><div><br></div><div>Ahh.. I missed actually opening the output file. Doing the following fixes that error:</div><div><br></div><div> /* open the output file, if needed */</div>
<div> if (!(avOutputFormat->flags & AVFMT_NOFILE)) {</div><div> if (avio_open(&avFormatContext->pb, outputfile, AVIO_FLAG_WRITE) < 0) {</div><div> fprintf(stderr, "Could not open '%s'\n", outputfile);</div>
<div> return 1;</div><div> }</div><div> }</div><div> </div></div><div>Now I have this: </div><div><br></div><div><div>mine(1228,0x7fff70ca4180) malloc: *** error for object 0x10180f800: pointer being freed was not allocated</div>
<div>*** set a breakpoint in malloc_error_break to debug</div><div><br></div><div>Program received signal SIGABRT, Aborted.</div><div>0x00007fff83cab212 in __pthread_kill ()</div><div>(gdb) bt</div><div>#0 0x00007fff83cab212 in __pthread_kill ()</div>
<div>#1 0x00007fff8332bb34 in pthread_kill ()</div><div>#2 0x00007fff8336fdfa in abort ()</div><div>#3 0x00007fff83343989 in free ()</div><div>#4 0x000000010000f6dc in av_freep (arg=0x101111628) at mem.c:185</div><div>
#5 0x00007fff855ba7e1 in start ()</div></div><div><br></div><div>Here is the updated full code if anyone feels like having a look:</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 audio_pts, video_pts;</div><div><br></div><div>///////////////////</div>
<div><br></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>///////</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> fprintf(stdout,"Format Name: %s\n",avOutputFormat->name);</div><div> fprintf(stdout,"Format Video Codec: %d\n",avOutputFormat->video_codec);</div>
<div> avOutputFormat->video_codec = codec_id;</div><div> fprintf(stdout,"Format Video Codec: %d\n",avOutputFormat->video_codec);</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><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>/*</div>
<div> f = fopen(filename, "wb");</div><div> if (!f) {</div><div> fprintf(stderr, "Could not open %s\n", filename);</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></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>//video_avstream->codec = c;</div><div><br></div><div>//////////////////////////////////</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> /* open the output file, if needed */</div>
<div> if (!(avOutputFormat->flags & AVFMT_NOFILE)) {</div><div> if (avio_open(&avFormatContext->pb, outputfile, AVIO_FLAG_WRITE) < 0) {</div><div> fprintf(stderr, "Could not open '%s'\n", outputfile);</div>
<div> return 1;</div><div> }</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> </div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>/*</div><div> if (c->coded_frame->pts != AV_NOPTS_VALUE) {</div>
<div> pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_avstream->time_base);</div><div> } else {</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>printf("pkt.pts AV_NOPTS_VALUE");</div>
<div> }</div><div> </div><div> pkt.pts = i;</div><div> </div><div> </div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>printf("pkt.pts:%5d\n",pkt.pts);</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span></div><div> printf("Write frame %3d (size=%5d)\n", i, pkt.size);</div><div> */</div><div> </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>// fwrite(pkt.data, 1, pkt.size, f);</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> /* get the delayed frames */</div>
<div>/*</div><div> for (got_output = 1; got_output; i++) {</div><div> fflush(stdout);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>ret = avcodec_encode_video2(c, &pkt, NULL, &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> printf("Write frame %3d (size=%5d)\n", i, pkt.size);</div>
<div>// fwrite(pkt.data, 1, pkt.size, f);</div><div><br></div><div> // write the compressed frame in the media file</div><div>//<span class="Apple-tab-span" style="white-space:pre"> </span>ret = av_interleaved_write_frame(avFormatContext, &pkt);</div>
<div><br></div><div> av_free_packet(&pkt);</div><div> }</div><div> }</div><div>*/</div><div><br></div><div> /* add sequence end code to have a real mpeg file */</div><div> //fwrite(endcode, 1, sizeof(endcode), f);</div>
<div> //fclose(f);</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> </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><div>
<br></div><div><br></div><div><br></div>