Here you must free pmp_CodecCtx before returning. Possibly you should do this avcodecOpen before the mediaengine stuff above (move this call to line 780), so you don't need to undo that as well.